1 /* $Id: conffile.c 6733 2004-05-16 23:01:23Z rra $
3 ** Routines for reading in incoming.conf-style config files.
11 static int getconfline(CONFFILE *F, char *buffer, int length) {
13 fgets(buffer, length, F->f);
17 } else if (F->array) {
18 strlcpy(buffer, F->array[F->lineno], F->sbuf);
21 if (strlen (F->buf) >= F->sbuf - 1) {
22 return 1; /* Line too long */
28 static int cfeof(CONFFILE *F) {
31 } else if (F->array) {
32 return (F->lineno == F->array_len);
38 static char *CONFgetword(CONFFILE *F)
46 if (!F) return (NULL); /* No conf file */
47 if (!F->buf || !F->buf[0]) {
48 if (cfeof (F)) return (NULL);
51 F->buf = xmalloc(F->sbuf);
53 if (getconfline(F, F->buf, F->sbuf) != 0)
54 return (NULL); /* Line too long */
57 /* Ignore blank and comment lines. */
58 if ((p = strchr(F->buf, '\n')) != NULL)
60 for (p = F->buf; *p == ' ' || *p == '\t' ; p++);
62 if ((*p == '\0' || *p == '#') && !cfeof(F)) {
64 if (getconfline(F, F->buf, F->sbuf))
65 return (NULL); /* Line too long */
69 } while (!cfeof(F) || !flag);
72 if (*p == '"') { /* double quoted string ? */
75 for (t = p; (*t != '"' || (*t == '"' && *(t - 1) == '\\')) &&
78 if (strlen(F->buf) >= F->sbuf - 2)
79 return (NULL); /* Line too long */
82 if (getconfline(F, t, F->sbuf - strlen(F->buf)))
83 return (NULL); /* Line too long */
84 if ((s = strchr(t, '\n')) != NULL)
95 for (t = p; *t != ' ' && *t != '\t' && *t != '\0'; t++)
96 if (*t == '#' && (t == p || *(t - 1) != '\\')) {
103 if (*p == '\0' && cfeof(F)) return (NULL);
107 for (; *t != '\0'; t++)
114 CONFFILE *CONFfopen(char *filename)
119 f = fopen(filename, "r");
122 ret = xmalloc(sizeof(CONFFILE));
127 ret->filename = xstrdup(filename);
136 void CONFfclose(CONFFILE *f)
138 if (!f) return; /* No conf file */
147 CONFTOKEN *CONFgettoken(CONFTOKEN *toklist, CONFFILE *file)
150 static CONFTOKEN ret = {CONFstring, 0};
157 word = CONFgetword(file);
161 for (i = 0; toklist[i].type; i++) {
162 if (strcmp(word, toklist[i].name) == 0) {