bcff1da22a44ce0364d15c85bf3802e3ab85c209
[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 <h/mh.h>
11 #include <h/utils.h>
12
13 struct procstr {
14         char *procname;
15         char **procnaddr;
16 };
17
18 static struct procstr procs[] = {
19         { "attachment-header",  &attach_hdr },
20         { "context",       &context },
21         { "mh-sequences",  &mh_seq },
22         { "backup-prefix", &backup_prefix },
23         { "draft-folder",  &draftfolder },
24         { "altmsg-link",   &altmsglink },
25         { "fileproc",      &fileproc },
26         { "listproc",      &listproc },
27         { "rmmproc",       &rmmproc },
28         { "sendmail",      &sendmail },
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         register int state;
40         register char *cp;
41         char name[NAMESZ], field[BUFSIZ];
42         register struct node *np;
43         register struct procstr *ps;
44
45         if (npp == NULL && (npp = opp) == NULL) {
46                 admonish(NULL, "bug: readconfig called but pump not primed");
47                 return;
48         }
49
50         for (state = FLD;;) {
51                 switch (state = m_getfld(state, name, field, sizeof(field),
52                                 ib)) {
53                 case FLD:
54                 case FLDPLUS:
55                 case FLDEOF:
56                         np = (struct node *) mh_xmalloc(sizeof(*np));
57                         *npp = np;
58                         *(npp = &np->n_next) = NULL;
59                         np->n_name = getcpy(name);
60                         if (state == FLDPLUS) {
61                                 cp = getcpy(field);
62                                 while (state == FLDPLUS) {
63                                         state = m_getfld(state, name, field,
64                                                         sizeof(field), ib);
65                                         cp = add(field, cp);
66                                 }
67                                 np->n_field = trimcpy(cp);
68                                 free(cp);
69                         } else {
70                                 np->n_field = trimcpy(field);
71                         }
72                         np->n_context = ctx;
73
74                         /*
75                         ** Now scan the list of `procs' and link in
76                         ** the field value to the global variable.
77                         */
78                         for (ps = procs; ps->procname; ps++)
79                                 if (mh_strcasecmp(np->n_name,
80                                                 ps->procname) == 0) {
81                                         *ps->procnaddr = np->n_field;
82                                         break;
83                                 }
84                         if (state == FLDEOF)
85                                 break;
86                         continue;
87
88                 case BODY:
89                 case BODYEOF:
90                         adios(NULL, "no blank lines are permitted in %s",
91                                         file);
92
93                 case FILEEOF:
94                         break;
95
96                 default:
97                         adios(NULL, "%s is poorly formatted", file);
98                 }
99                 break;
100         }
101
102         opp = npp;
103 }