2350b24bd166f488351828f32f6814bdfb57516c
[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         { "lproc",         &lproc },
27         { "mailproc",      &mailproc },
28         { "mhlproc",       &mhlproc },
29         { "postproc",      &postproc },
30         { "rmmproc",       &rmmproc },
31         { "sendmail",      &sendmail },
32         { "sendproc",      &sendproc },
33         { "showmimeproc",  &showmimeproc },
34         { "showproc",      &showproc },
35         { "whatnowproc",   &whatnowproc },
36         { NULL, NULL }
37 };
38
39 static struct node **opp = NULL;
40
41
42 void
43 readconfig(struct node **npp, FILE *ib, char *file, int ctx)
44 {
45         register int state;
46         register char *cp;
47         char name[NAMESZ], field[BUFSIZ];
48         register struct node *np;
49         register struct procstr *ps;
50
51         if (npp == NULL && (npp = opp) == NULL) {
52                 admonish(NULL, "bug: readconfig called but pump not primed");
53                 return;
54         }
55
56         for (state = FLD;;) {
57                 switch (state = m_getfld(state, name, field, sizeof(field),
58                                 ib)) {
59                 case FLD:
60                 case FLDPLUS:
61                 case FLDEOF:
62                         np = (struct node *) mh_xmalloc(sizeof(*np));
63                         *npp = np;
64                         *(npp = &np->n_next) = NULL;
65                         np->n_name = getcpy(name);
66                         if (state == FLDPLUS) {
67                                 cp = getcpy(field);
68                                 while (state == FLDPLUS) {
69                                         state = m_getfld(state, name, field,
70                                                         sizeof(field), ib);
71                                         cp = add(field, cp);
72                                 }
73                                 np->n_field = trimcpy(cp);
74                                 free(cp);
75                         } else {
76                                 np->n_field = trimcpy(field);
77                         }
78                         np->n_context = ctx;
79
80                         /*
81                         ** Now scan the list of `procs' and link in
82                         ** the field value to the global variable.
83                         */
84                         for (ps = procs; ps->procname; ps++)
85                                 if (mh_strcasecmp(np->n_name,
86                                                 ps->procname) == 0) {
87                                         *ps->procnaddr = np->n_field;
88                                         break;
89                                 }
90                         if (state == FLDEOF)
91                                 break;
92                         continue;
93
94                 case BODY:
95                 case BODYEOF:
96                         adios(NULL, "no blank lines are permitted in %s",
97                                         file);
98
99                 case FILEEOF:
100                         break;
101
102                 default:
103                         adios(NULL, "%s is poorly formatted", file);
104                 }
105                 break;
106         }
107
108         opp = npp;
109 }