46644ebbdb835f9da055faabaa09ce139dfceaa3
[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         { "context",       &context },
20         { "mh-sequences",  &mh_seq },
21         { "backup-prefix", &backup_prefix },
22         { "altmsg-link",   &altmsglink },
23         { "buildmimeproc", &buildmimeproc },
24         { "faceproc",      &faceproc },
25         { "fileproc",      &fileproc },
26         { "incproc",       &incproc },
27         { "installproc",   &installproc },
28         { "lproc",         &lproc },
29         { "mailproc",      &mailproc },
30         { "mhlproc",       &mhlproc },
31         { "moreproc",      &moreproc },
32         { "packproc",      &packproc },
33         { "postproc",      &postproc },
34         { "rmfproc",       &rmfproc },
35         { "rmmproc",       &rmmproc },
36         { "sendmail",      &sendmail },
37         { "sendproc",      &sendproc },
38         { "showmimeproc",  &showmimeproc },
39         { "showproc",      &showproc },
40         { "whatnowproc",   &whatnowproc },
41         { "whomproc",      &whomproc },
42         { NULL, NULL }
43 };
44
45 static struct node **opp = NULL;
46
47
48 void
49 readconfig (struct node **npp, FILE *ib, char *file, int ctx)
50 {
51         register int state;
52         register char *cp;
53         char name[NAMESZ], field[BUFSIZ];
54         register struct node *np;
55         register struct procstr *ps;
56
57         if (npp == NULL && (npp = opp) == NULL) {
58                 admonish (NULL, "bug: readconfig called but pump not primed");
59                 return;
60         }
61
62         for (state = FLD;;) {
63                 switch (state = m_getfld (state, name, field, sizeof(field), ib)) {
64                         case FLD:
65                         case FLDPLUS:
66                         case FLDEOF:
67                                 np = (struct node *) mh_xmalloc (sizeof(*np));
68                                 *npp = np;
69                                 *(npp = &np->n_next) = NULL;
70                                 np->n_name = getcpy (name);
71                                 if (state == FLDPLUS) {
72                                         cp = getcpy (field);
73                                         while (state == FLDPLUS) {
74                                                 state = m_getfld (state, name, field, sizeof(field), 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                                 if (state == FLDEOF)
94                                         break;
95                                 continue;
96
97                         case BODY:
98                         case BODYEOF:
99                                 adios (NULL, "no blank lines are permitted in %s", file);
100
101                         case FILEEOF:
102                                 break;
103
104                         default:
105                                 adios (NULL, "%s is poorly formatted", file);
106                 }
107                 break;
108         }
109
110         opp = npp;
111 }