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