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