mhparam: Added more items for direct query and -debug listing.
[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", 2 },
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 char *empty = "";
36
37 /*
38 ** This list should contain all values of h/mh.h and config/config.c
39 ** TODO: Add the constants Nbby, MAXARGS, NUMATTRS, NAMESZ
40 */
41 static struct proc procs [] = {
42         { "#--Version--", &empty },
43         { "version",          &version_num },
44         { "version-str",      &version_str },
45
46         { "#--Path-and-File-Names--", &empty },
47         { "mypath",          &mypath },
48         { "mmhdir",          &mmhdir },
49         { "mmhpath",         &mmhpath },
50         { "profile",         &profile },
51         { "defpath",         &defpath },
52         { "context",         &context },
53         { "ctxpath",         &ctxpath },
54         { "mhetcdir",        &mhetcdir },
55         { "mailspool",       &mailspool },
56         { "mailstore",       &mailstore },
57         { "mh-sequences",    &mh_seq },
58
59         { "#--Default-Programs--", &empty },
60         { "editor",        &defaulteditor },
61         { "pager",         &defaultpager },
62         { "sendmail",      &sendmail },
63         { "listproc",      &listproc },
64         { "whatnowproc",   &whatnowproc },
65         { "mimetypequeryproc", &mimetypequeryproc },
66
67         { "#--Mail-Folder-Names--", &empty },
68         { "inbox",          &defaultfolder },
69         { "draftfolder",    &draftfolder },
70         { "trashfolder",    &trashfolder },
71
72         { "#--Profile-and-Context-Component-Names--", &empty },
73         { "curfolder-component",         &curfolder },
74         { "inbox-component",             &inbox },
75         { "mimetypequery-component",     &mimetypequery },
76         { "nmhstorage-component",        &nmhstorage },
77         { "nsequence-component",         &nsequence },
78         { "psequence-component",         &psequence },
79         { "usequence-component",         &usequence },
80
81         { "#--Mmh-specific-Mail-Header-Names--", &empty },
82         { "attachment-header", &attach_hdr },
83         { "enc-header",        &enc_hdr },
84         { "sign-header",       &sign_hdr },
85
86         { "#--File-Permissions--", &empty },
87         { "foldprot",      &foldprot },
88         { "msgprot",       &msgprot },
89
90         { "#--Template-File-Names--", &empty },
91         { "components",        &components },
92         { "digestcomps",       &digestcomps },
93         { "distcomps",         &distcomps },
94         { "forwcomps",         &forwcomps },
95         { "rcvdistcomps",      &rcvdistcomps },
96         { "replcomps",         &replcomps },
97         { "replgroupcomps",    &replgroupcomps },
98         { "mhlformat",         &mhlformat },
99         { "mhlreply",          &mhlreply },
100
101         { "#--Default-Sequence-Names--", &empty },
102         { "seq-all",          &seq_all },
103         { "seq-beyond",       &seq_beyond },
104         { "seq-cur",          &seq_cur },
105         { "seq-first",        &seq_first },
106         { "seq-last",         &seq_last },
107         { "seq-next",         &seq_next },
108         { "seq-prev",         &seq_prev },
109         { "seq-unseen",       &seq_unseen },
110         { "seq-neg",          &seq_neg },
111
112         { NULL,            NULL },
113 };
114
115
116 /*
117 ** static prototypes
118 */
119 static char *p_find(char *);
120
121
122 int
123 main(int argc, char **argv)
124 {
125         int i, compp = 0, missed = 0;
126         int all = 0, debug = 0;
127         int components = -1;
128         char *cp, buf[BUFSIZ], **argp;
129         char **arguments, *comps[MAXARGS];
130
131         invo_name = mhbasename(argv[0]);
132
133         context_read();
134
135         arguments = getarguments(invo_name, argc, argv, 1);
136         argp = arguments;
137
138         while ((cp = *argp++)) {
139                 if (*cp == '-') {
140                         switch (smatch(++cp, switches)) {
141                         case AMBIGSW:
142                                 ambigsw(cp, switches);
143                                 done(1);
144                         case UNKWNSW:
145                                 adios(NULL, "-%s unknown", cp);
146
147                         case HELPSW:
148                                 snprintf(buf, sizeof(buf), "%s [profile-components] [switches]", invo_name);
149                                 print_help(buf, switches, 1);
150                                 done(1);
151                         case VERSIONSW:
152                                 print_version(invo_name);
153                                 done(1);
154
155                         case COMPSW:
156                                 components = 1;
157                                 break;
158                         case NCOMPSW:
159                                 components = 0;
160                                 break;
161
162                         case ALLSW:
163                                 all = 1;
164                                 break;
165
166                         case DEBUGSW:
167                                 debug = 1;
168                                 break;
169                         }
170                 } else {
171                         comps[compp++] = cp;
172                 }
173         }
174
175         if (all) {
176                 struct node *np;
177
178                 if (compp)
179                         advise(NULL, "profile-components ignored with -all");
180
181                 if (components >= 0)
182                         advise(NULL, "-%scomponents ignored with -all",
183                                         components ? "" : "no");
184
185                 /* print all entries in context/profile list */
186                 for (np = m_defs; np; np = np->n_next)
187                         printf("%s: %s\n", np->n_name, np->n_field);
188
189         } else if (debug) {
190                 struct proc *ps;
191
192                 /*
193                 ** Print the current value of everything in
194                 ** procs array.  This will show their current
195                 ** value (as determined after context is read).
196                 */
197                 for (ps = procs; ps->p_name; ps++)
198                         printf("%s: %s\n", ps->p_name,
199                                         *ps->p_field ? *ps->p_field : "");
200
201         } else {
202                 if (components < 0)
203                         components = compp > 1;
204
205                 for (i = 0; i < compp; i++)  {
206                         register char *value;
207
208                         value = context_find(comps[i]);
209                         if (!value)
210                                 value = p_find(comps[i]);
211                         if (value) {
212                                 if (components)
213                                         printf("%s: ", comps[i]);
214
215                                 printf("%s\n", value);
216                         } else
217                                 missed++;
218                 }
219         }
220
221         done(missed);
222         return 1;
223 }
224
225
226 static char *
227 p_find(char *str)
228 {
229         struct proc *ps;
230
231         for (ps = procs; ps->p_name; ps++)
232                 if (!mh_strcasecmp(ps->p_name, str))
233                         return (*ps->p_field);
234
235         return NULL;
236 }