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