645a9efe269a0b4a0596d07a5b11d33a56fd42f6
[mmh] / sbr / readconfig.c
1 /*
2 ** readconfig.c -- base routine to read nmh configuration files
3 **              -- such as nmh profile, context file, or mhn.defaults.
4 **
5 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
6 ** COPYRIGHT file in the root directory of the nmh distribution for
7 ** complete copyright information.
8 */
9
10 #include <sysexits.h>
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         { "attachment-header",  &attach_hdr },
21         { "sign-header",   &sign_hdr },
22         { "enc-header",    &enc_hdr },
23         { "context",       &context },
24         { "mh-sequences",  &mh_seq },
25         { "draft-folder",  &draftfolder },
26         { "listproc",      &listproc },
27         { "sendmail",      &sendmail },
28         { "trash-folder",  &trashfolder },
29         { "whatnowproc",   &whatnowproc },
30         { NULL, NULL }
31 };
32
33 static struct node **opp = NULL;
34
35
36 void
37 readconfig(struct node **npp, FILE *ib, char *file, int ctx)
38 {
39         enum state state;
40         struct field f = free_field;
41         struct node *np;
42         struct procstr *ps;
43
44         if (npp == NULL && (npp = opp) == NULL) {
45                 admonish(NULL, "bug: readconfig called but pump not primed");
46                 return;
47         }
48
49         for (state = FLD2;;) {
50                 switch (state = m_getfld2(state, &f, ib)) {
51                 case FLD2:
52                         np = (struct node *) mh_xmalloc(sizeof(*np));
53                         *npp = np;
54                         *(npp = &np->n_next) = NULL;
55                         np->n_name = getcpy(f.name);
56                         np->n_field = trimcpy(f.value);
57                         np->n_context = ctx;
58
59                         /*
60                         ** Now scan the list of `procs' and link in
61                         ** the field value to the global variable.
62                         */
63                         for (ps = procs; ps->procname; ps++) {
64                                 if (mh_strcasecmp(np->n_name,
65                                                 ps->procname) == 0) {
66                                         *ps->procnaddr = np->n_field;
67                                         break;
68                                 }
69                         }
70                         continue;
71
72                 case BODY2:
73                         adios(EX_CONFIG, NULL, "no blank lines are permitted in %s",
74                                         file);
75
76                 case FILEEOF2:
77                         break;
78
79                 default:
80                         adios(EX_CONFIG, NULL, "%s is poorly formatted", file);
81                 }
82                 break;
83         }
84
85         opp = npp;
86 }