]> diplodocus.org Git - nmh/blob - sbr/readconfig.c
Fixed source charset in mhfixmsg textcharset verbose output.
[nmh] / sbr / readconfig.c
1 /* readconfig.c -- base routine to read nmh configuration files
2 * -- such as nmh profile, context file, or mhn.defaults.
3 *
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include "h/mh.h"
10 #include "sbr/context_find.h"
11 #include "m_getfld.h"
12 #include "trimcpy.h"
13 #include "getcpy.h"
14 #include "readconfig.h"
15 #include "error.h"
16 #include "h/utils.h"
17
18 static void checkconfig();
19
20 struct procstr {
21 char *procname;
22 char **procnaddr;
23 };
24
25 static struct procstr procs[] = {
26 { "context", &context },
27 { "mh-sequences", &mh_seq },
28 { "buildmimeproc", &buildmimeproc },
29 { "fileproc", &fileproc },
30 { "formatproc", &formatproc },
31 { "incproc", &incproc },
32 { "lproc", &lproc },
33 { "mailproc", &mailproc },
34 { "mhlproc", &mhlproc },
35 { "moreproc", &moreproc },
36 { "packproc", &packproc },
37 { "postproc", &postproc },
38 { "rmmproc", &rmmproc },
39 { "sendproc", &sendproc },
40 { "showmimeproc", &showmimeproc },
41 { "showproc", &showproc },
42 { "whatnowproc", &whatnowproc },
43 { "whomproc", &whomproc },
44 { NULL, NULL }
45 };
46
47 static struct node **opp = NULL;
48
49
50 void
51 readconfig (struct node **npp, FILE *ib, const char *file, int ctx)
52 {
53 int state;
54 char *cp;
55 char name[NAMESZ], field[NMH_BUFSIZ];
56 struct node *np;
57 struct procstr *ps;
58 m_getfld_state_t gstate;
59
60 if (npp == NULL && (npp = opp) == NULL) {
61 inform("bug: readconfig called but pump not primed, continuing...");
62 return;
63 }
64
65 gstate = m_getfld_state_init(ib);
66 for (;;) {
67 int fieldsz = sizeof field;
68 switch (state = m_getfld2(&gstate, name, field, &fieldsz)) {
69 case FLD:
70 case FLDPLUS:
71 NEW(np);
72 *npp = np;
73 *(npp = &np->n_next) = NULL;
74 np->n_name = mh_xstrdup(name);
75 if (state == FLDPLUS) {
76 cp = mh_xstrdup(field);
77 while (state == FLDPLUS) {
78 fieldsz = sizeof field;
79 state = m_getfld2(&gstate, name, field, &fieldsz);
80 cp = add (field, cp);
81 }
82 np->n_field = trimcpy (cp);
83 free (cp);
84 } else {
85 np->n_field = trimcpy (field);
86 }
87 np->n_context = ctx;
88
89 /*
90 * Now scan the list of `procs' and link in the
91 * field value to the global variable.
92 */
93 for (ps = procs; ps->procname; ps++)
94 if (strcmp (np->n_name, ps->procname) == 0) {
95 *ps->procnaddr = np->n_field;
96 break;
97 }
98 continue;
99
100 case BODY:
101 die("no blank lines are permitted in %s", file);
102
103 case FILEEOF:
104 break;
105
106 default:
107 die("%s is poorly formatted", file);
108 }
109 break;
110 }
111 m_getfld_state_destroy (&gstate);
112
113 /*
114 * Special handling for the pager processes: lproc and moreproc.
115 *
116 * If they are not set by the profile, use the callers $PAGER if
117 * available, otherwise set them to DEFAULT_PAGER.
118 */
119 if (lproc == NULL) {
120 lproc = getenv("PAGER");
121 if (lproc == NULL || lproc[0] == '\0')
122 lproc = DEFAULT_PAGER;
123 }
124 if (moreproc == NULL) {
125 moreproc = getenv("PAGER");
126 if (moreproc == NULL || moreproc[0] == '\0')
127 moreproc = DEFAULT_PAGER;
128 }
129
130 if (opp == NULL) {
131 checkconfig();
132 }
133
134 opp = npp;
135 }
136
137
138 void
139 add_profile_entry (const char *key, const char *value)
140 {
141 struct node *newnode;
142
143 /* This inserts the new node at the beginning of m_defs because
144 that doesn't require traversing it or checking to see if it's
145 empty. */
146 NEW(newnode);
147 newnode->n_name = getcpy (key);
148 newnode->n_field = getcpy (value);
149 newnode->n_context = 0;
150 newnode->n_next = m_defs;
151 m_defs = newnode;
152 }
153
154
155 /* Check profile for issues to warn about. */
156 void
157 checkconfig() {
158 /* Check for duplicated non-null profile entries. Except
159 allow multiple profile entries named "#", because that's
160 what mh-profile(5) suggests using for comments.
161
162 Only do this check on the very first call from
163 context_read(), when opp is NULL. That way, entries in
164 mhn.defaults can be overridden without triggering
165 warnings.
166
167 Note that mhn.defaults, $MHN, $MHBUILD, $MHSHOW, and
168 $MHSTORE all put their entries into just one list, m_defs,
169 the same list that the profile uses. */
170 struct node *np;
171 bool has_post = false;
172 bool post_warning_disabled = false;
173 for (np = m_defs; np; np = np->n_next) {
174 if (*np->n_name) {
175 if (isatty(fileno(stderr))) {
176 /* Check for post component in profile. */
177 if (strcasecmp(np->n_name, "post") == 0) {
178 has_post = true;
179 } else if (strcasecmp(np->n_name, "postproc") == 0 &&
180 np->n_field != NULL) {
181 post_warning_disabled = true;
182 }
183 }
184
185 if (strcmp("#", np->n_name)) {
186 /* Yes, this is O(N^2). The profile should be small enough so
187 that's not a performance problem. */
188 struct node *np2;
189 for (np2 = np->n_next; np2; np2 = np2->n_next) {
190 if (! strcasecmp (np->n_name, np2->n_name)) {
191 inform("multiple \"%s\" profile components in %s, "
192 "ignoring \"%s\", continuing...",
193 np->n_name, defpath, np2->n_field);
194 }
195 }
196 }
197 }
198 }
199
200 if (has_post && ! post_warning_disabled) {
201 inform("post profile component will be ignored. To suppress "
202 "this warning,\n"
203 "either remove it, comment it with #:, or "
204 "add the following to %s:\npostproc: %s\n",
205 defpath, postproc);
206 }
207 }