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