]> diplodocus.org Git - nmh/blob - sbr/readconfig.c
Merge branch 'newlock'
[nmh] / sbr / readconfig.c
1
2 /*
3 * readconfig.c -- base routine to read nmh configuration files
4 * -- such as nmh profile, context file, or mhn.defaults.
5 *
6 * This code is Copyright (c) 2002, by the authors of nmh. See the
7 * COPYRIGHT file in the root directory of the nmh distribution for
8 * complete copyright information.
9 */
10
11 #include <h/mh.h>
12 #include <h/utils.h>
13
14 struct procstr {
15 char *procname;
16 char **procnaddr;
17 };
18
19 static struct procstr procs[] = {
20 { "context", &context },
21 { "mh-sequences", &mh_seq },
22 { "buildmimeproc", &buildmimeproc },
23 { "fileproc", &fileproc },
24 { "formatproc", &formatproc },
25 { "incproc", &incproc },
26 { "lproc", &lproc },
27 { "mailproc", &mailproc },
28 { "mhlproc", &mhlproc },
29 { "moreproc", &moreproc },
30 { "mshproc", &mshproc },
31 { "packproc", &packproc },
32 { "postproc", &postproc },
33 { "rmmproc", &rmmproc },
34 { "sendproc", &sendproc },
35 { "showmimeproc", &showmimeproc },
36 { "showproc", &showproc },
37 { "vmhproc", &vmhproc },
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, char *file, int ctx)
48 {
49 register int state;
50 register char *cp;
51 char name[NAMESZ], field[BUFSIZ];
52 register struct node *np;
53 register struct procstr *ps;
54 m_getfld_state_t gstate = 0;
55
56 if (npp == NULL && (npp = opp) == NULL) {
57 admonish (NULL, "bug: readconfig called but pump not primed");
58 return;
59 }
60
61 for (;;) {
62 int fieldsz = sizeof field;
63 switch (state = m_getfld (&gstate, name, field, &fieldsz, ib)) {
64 case FLD:
65 case FLDPLUS:
66 np = (struct node *) mh_xmalloc (sizeof(*np));
67 *npp = np;
68 *(npp = &np->n_next) = NULL;
69 np->n_name = getcpy (name);
70 if (state == FLDPLUS) {
71 cp = getcpy (field);
72 while (state == FLDPLUS) {
73 fieldsz = sizeof field;
74 state = m_getfld (&gstate, name, field, &fieldsz, ib);
75 cp = add (field, cp);
76 }
77 np->n_field = trimcpy (cp);
78 free (cp);
79 } else {
80 np->n_field = trimcpy (field);
81 }
82 np->n_context = ctx;
83
84 /*
85 * Now scan the list of `procs' and link in the
86 * field value to the global variable.
87 */
88 for (ps = procs; ps->procname; ps++)
89 if (strcmp (np->n_name, ps->procname) == 0) {
90 *ps->procnaddr = np->n_field;
91 break;
92 }
93 continue;
94
95 case BODY:
96 adios (NULL, "no blank lines are permitted in %s", file);
97
98 case FILEEOF:
99 break;
100
101 default:
102 adios (NULL, "%s is poorly formatted", file);
103 }
104 break;
105 }
106 m_getfld_state_destroy (&gstate);
107
108 /*
109 * Special handling for the pager processes: lproc and moreproc.
110 *
111 * If they are not set by the profile, use the callers $PAGER if
112 * available, otherwise set them to DEFAULT_PAGER.
113 */
114 if (lproc == NULL) {
115 lproc = getenv("PAGER");
116 if (lproc == NULL || lproc[0] == '\0')
117 lproc = DEFAULT_PAGER;
118 }
119 if (moreproc == NULL) {
120 moreproc = getenv("PAGER");
121 if (moreproc == NULL || moreproc[0] == '\0')
122 moreproc = DEFAULT_PAGER;
123 }
124
125 if (opp == NULL) {
126 /* Check for duplicated non-null profile entries. Except
127 allow multiple profile entries named "#", because that's
128 what the mh-profile man page suggests using for comments.
129
130 Only do this check on the very first call from
131 context_read(), when opp is NULL. That way, entries in
132 mhn.defaults can be overridden without triggering
133 warnings.
134
135 Note that that mhn.defaults, $MHN, $MHBUILD, $MHSHOW, and
136 $MHSTORE all put their entries into just one list, m_defs,
137 the same list that the profile uses. */
138
139 struct node *np;
140 for (np = m_defs; np; np = np->n_next) {
141 /* Yes, this is O(N^2). The profile should be small enough so
142 that's not a performance problem. */
143 if (strlen (np->n_name) > 0 && strcmp ("#", np->n_name)) {
144 struct node *np2;
145 for (np2 = np->n_next; np2; np2 = np2->n_next) {
146 if (! mh_strcasecmp (np->n_name, np2->n_name)) {
147 admonish (NULL, "multiple \"%s\" profile components "
148 "in %s, ignoring \"%s\"",
149 np->n_name, defpath, np2->n_field);
150 }
151 }
152 }
153 }
154 }
155
156 opp = npp;
157 }