1440dc32752e5ba0ec0e48c804a8df6b10cab51b
[mmh] / uip / mhparam.c
1 /*
2 ** mhparam.c -- print mh_profile values
3 **
4 ** Originally contributed by
5 ** Jeffrey C Honig <Jeffrey_C_Honig@cornell.edu>
6 **
7 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
8 ** COPYRIGHT file in the root directory of the nmh distribution for
9 ** complete copyright information.
10 */
11
12 #include <h/mh.h>
13
14 static struct swit switches[] = {
15 #define COMPSW  0
16         { "components", 0 },
17 #define NCOMPSW  1
18         { "nocomponents", 0 },
19 #define ALLSW  2
20         { "all", 0 },
21 #define VERSIONSW 3
22         { "version", 0 },
23 #define HELPSW  4
24         { "help", 0 },
25 #define DEBUGSW   5
26         { "debug", -5 },
27         { NULL, 0 }
28 };
29
30 struct proc {
31         char *p_name;
32         char **p_field;
33 };
34
35 static struct proc procs [] = {
36         { "attachment-header",  &attach_hdr },
37         { "context",       &context },
38         { "mh-sequences",  &mh_seq },
39         { "editor",        &defaulteditor },
40         { "fileproc",      &fileproc },
41         { "foldprot",      &foldprot },
42         { "lproc",         &lproc },
43         { "mailproc",      &mailproc },
44         { "mhlproc",       &mhlproc },
45         { "mimetypequeryproc", &mimetypequeryproc },
46         { "msgprot",       &msgprot },
47         { "pager",         &defaultpager },
48         { "postproc",      &postproc },
49         { "rmmproc",       &rmmproc },
50         { "sendmail",      &sendmail },
51         { "sendproc",      &sendproc },
52         { "showmimeproc",  &showmimeproc },
53         { "showproc",      &showproc },
54         { "version",       &version_num },
55         { "whatnowproc",   &whatnowproc },
56         { "etcdir",        &mhetcdir },
57         { "backup-prefix", &backup_prefix },
58         { "altmsg-link",   &altmsglink },
59         { "draft-folder",  &draftfolder },
60         { NULL,            NULL },
61 };
62
63
64 /*
65 ** static prototypes
66 */
67 static char *p_find(char *);
68
69
70 int
71 main(int argc, char **argv)
72 {
73         int i, compp = 0, missed = 0;
74         int all = 0, debug = 0;
75         int components = -1;
76         char *cp, buf[BUFSIZ], **argp;
77         char **arguments, *comps[MAXARGS];
78
79         invo_name = mhbasename(argv[0]);
80
81         /* read user profile/context */
82         context_read();
83
84         arguments = getarguments(invo_name, argc, argv, 1);
85         argp = arguments;
86
87         while ((cp = *argp++)) {
88                 if (*cp == '-') {
89                         switch (smatch(++cp, switches)) {
90                         case AMBIGSW:
91                                 ambigsw(cp, switches);
92                                 done(1);
93                         case UNKWNSW:
94                                 adios(NULL, "-%s unknown", cp);
95
96                         case HELPSW:
97                                 snprintf(buf, sizeof(buf), "%s [profile-components] [switches]", invo_name);
98                                 print_help(buf, switches, 1);
99                                 done(1);
100                         case VERSIONSW:
101                                 print_version(invo_name);
102                                 done(1);
103
104                         case COMPSW:
105                                 components = 1;
106                                 break;
107                         case NCOMPSW:
108                                 components = 0;
109                                 break;
110
111                         case ALLSW:
112                                 all = 1;
113                                 break;
114
115                         case DEBUGSW:
116                                 debug = 1;
117                                 break;
118                         }
119                 } else {
120                         comps[compp++] = cp;
121                 }
122         }
123
124         if (all) {
125                 struct node *np;
126
127                 if (compp)
128                         advise(NULL, "profile-components ignored with -all");
129
130                 if (components >= 0)
131                         advise(NULL, "-%scomponents ignored with -all",
132                                         components ? "" : "no");
133
134                 /* print all entries in context/profile list */
135                 for (np = m_defs; np; np = np->n_next)
136                         printf("%s: %s\n", np->n_name, np->n_field);
137
138         } else if (debug) {
139                 struct proc *ps;
140
141                 /*
142                 ** Print the current value of everything in
143                 ** procs array.  This will show their current
144                 ** value (as determined after context is read).
145                 */
146                 for (ps = procs; ps->p_name; ps++)
147                         printf("%s: %s\n", ps->p_name,
148                                         *ps->p_field ? *ps->p_field : "");
149
150         } else {
151                 if (components < 0)
152                         components = compp > 1;
153
154                 for (i = 0; i < compp; i++)  {
155                         register char *value;
156
157                         value = context_find(comps[i]);
158                         if (!value)
159                                 value = p_find(comps[i]);
160                         if (value) {
161                                 if (components)
162                                         printf("%s: ", comps[i]);
163
164                                 printf("%s\n", value);
165                         } else
166                                 missed++;
167                 }
168         }
169
170         done(missed);
171         return 1;
172 }
173
174
175 static char *
176 p_find(char *str)
177 {
178         struct proc *ps;
179
180         for (ps = procs; ps->p_name; ps++)
181                 if (!mh_strcasecmp(ps->p_name, str))
182                         return (*ps->p_field);
183
184         return NULL;
185 }