fe9846101b9f0e056e7be9a11a1f983080c93078
[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         { "foldprot",      &foldprot },
41         { "listproc",      &listproc },
42         { "mimetypequeryproc", &mimetypequeryproc },
43         { "msgprot",       &msgprot },
44         { "pager",         &defaultpager },
45         { "sendmail",      &sendmail },
46         { "version",       &version_num },
47         { "whatnowproc",   &whatnowproc },
48         { "etcdir",        &mhetcdir },
49         { "backup-prefix", &backup_prefix },
50         { "draft-folder",  &draftfolder },
51         { "trash-folder",  &trashfolder },
52         { NULL,            NULL },
53 };
54
55
56 /*
57 ** static prototypes
58 */
59 static char *p_find(char *);
60
61
62 int
63 main(int argc, char **argv)
64 {
65         int i, compp = 0, missed = 0;
66         int all = 0, debug = 0;
67         int components = -1;
68         char *cp, buf[BUFSIZ], **argp;
69         char **arguments, *comps[MAXARGS];
70
71         invo_name = mhbasename(argv[0]);
72
73         /* read user profile/context */
74         context_read();
75
76         arguments = getarguments(invo_name, argc, argv, 1);
77         argp = arguments;
78
79         while ((cp = *argp++)) {
80                 if (*cp == '-') {
81                         switch (smatch(++cp, switches)) {
82                         case AMBIGSW:
83                                 ambigsw(cp, switches);
84                                 done(1);
85                         case UNKWNSW:
86                                 adios(NULL, "-%s unknown", cp);
87
88                         case HELPSW:
89                                 snprintf(buf, sizeof(buf), "%s [profile-components] [switches]", invo_name);
90                                 print_help(buf, switches, 1);
91                                 done(1);
92                         case VERSIONSW:
93                                 print_version(invo_name);
94                                 done(1);
95
96                         case COMPSW:
97                                 components = 1;
98                                 break;
99                         case NCOMPSW:
100                                 components = 0;
101                                 break;
102
103                         case ALLSW:
104                                 all = 1;
105                                 break;
106
107                         case DEBUGSW:
108                                 debug = 1;
109                                 break;
110                         }
111                 } else {
112                         comps[compp++] = cp;
113                 }
114         }
115
116         if (all) {
117                 struct node *np;
118
119                 if (compp)
120                         advise(NULL, "profile-components ignored with -all");
121
122                 if (components >= 0)
123                         advise(NULL, "-%scomponents ignored with -all",
124                                         components ? "" : "no");
125
126                 /* print all entries in context/profile list */
127                 for (np = m_defs; np; np = np->n_next)
128                         printf("%s: %s\n", np->n_name, np->n_field);
129
130         } else if (debug) {
131                 struct proc *ps;
132
133                 /*
134                 ** Print the current value of everything in
135                 ** procs array.  This will show their current
136                 ** value (as determined after context is read).
137                 */
138                 for (ps = procs; ps->p_name; ps++)
139                         printf("%s: %s\n", ps->p_name,
140                                         *ps->p_field ? *ps->p_field : "");
141
142         } else {
143                 if (components < 0)
144                         components = compp > 1;
145
146                 for (i = 0; i < compp; i++)  {
147                         register char *value;
148
149                         value = context_find(comps[i]);
150                         if (!value)
151                                 value = p_find(comps[i]);
152                         if (value) {
153                                 if (components)
154                                         printf("%s: ", comps[i]);
155
156                                 printf("%s\n", value);
157                         } else
158                                 missed++;
159                 }
160         }
161
162         done(missed);
163         return 1;
164 }
165
166
167 static char *
168 p_find(char *str)
169 {
170         struct proc *ps;
171
172         for (ps = procs; ps->p_name; ps++)
173                 if (!mh_strcasecmp(ps->p_name, str))
174                         return (*ps->p_field);
175
176         return NULL;
177 }