1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
/* $Id: conffile.c 6733 2004-05-16 23:01:23Z rra $
**
** Routines for reading in incoming.conf-style config files.
*/
#include "config.h"
#include "clibrary.h"
#include "conffile.h"
#include "libinn.h"
static int getconfline(CONFFILE *F, char *buffer, int length) {
if (F->f) {
fgets(buffer, length, F->f);
if (ferror(F->f)) {
return 1;
}
} else if (F->array) {
strlcpy(buffer, F->array[F->lineno], F->sbuf);
}
F->lineno++;
if (strlen (F->buf) >= F->sbuf - 1) {
return 1; /* Line too long */
} else {
return 0;
}
}
static int cfeof(CONFFILE *F) {
if (F->f) {
return feof(F->f);
} else if (F->array) {
return (F->lineno == F->array_len);
} else {
return 1;
}
}
static char *CONFgetword(CONFFILE *F)
{
char *p;
char *s;
char *t;
char *word;
bool flag, comment;
if (!F) return (NULL); /* No conf file */
if (!F->buf || !F->buf[0]) {
if (cfeof (F)) return (NULL);
if (!F->buf) {
F->sbuf = BIG_BUFFER;
F->buf = xmalloc(F->sbuf);
}
if (getconfline(F, F->buf, F->sbuf) != 0)
return (NULL); /* Line too long */
}
do {
/* Ignore blank and comment lines. */
if ((p = strchr(F->buf, '\n')) != NULL)
*p = '\0';
for (p = F->buf; *p == ' ' || *p == '\t' ; p++);
flag = true;
if ((*p == '\0' || *p == '#') && !cfeof(F)) {
flag = false;
if (getconfline(F, F->buf, F->sbuf))
return (NULL); /* Line too long */
continue;
}
break;
} while (!cfeof(F) || !flag);
comment = false;
if (*p == '"') { /* double quoted string ? */
p++;
do {
for (t = p; (*t != '"' || (*t == '"' && *(t - 1) == '\\')) &&
*t != '\0'; t++);
if (*t == '\0') {
if (strlen(F->buf) >= F->sbuf - 2)
return (NULL); /* Line too long */
*t++ = '\n';
*t = '\0';
if (getconfline(F, t, F->sbuf - strlen(F->buf)))
return (NULL); /* Line too long */
if ((s = strchr(t, '\n')) != NULL)
*s = '\0';
}
else
break;
} while (!cfeof(F));
if (*t != '"')
return (NULL);
*t++ = '\0';
}
else {
for (t = p; *t != ' ' && *t != '\t' && *t != '\0'; t++)
if (*t == '#' && (t == p || *(t - 1) != '\\')) {
comment = true;
break;
}
if (*t != '\0')
*t++ = '\0';
}
if (*p == '\0' && cfeof(F)) return (NULL);
word = xstrdup (p);
p = F->buf;
if (!comment)
for (; *t != '\0'; t++)
*p++ = *t;
*p = '\0';
return (word);
}
CONFFILE *CONFfopen(char *filename)
{
FILE *f;
CONFFILE *ret;
f = fopen(filename, "r");
if (!f)
return(0);
ret = xmalloc(sizeof(CONFFILE));
if (!ret) {
fclose(f);
return(0);
}
ret->filename = xstrdup(filename);
ret->buf = 0;
ret->sbuf = 0;
ret->lineno = 0;
ret->f = f;
ret->array = NULL;
return(ret);
}
void CONFfclose(CONFFILE *f)
{
if (!f) return; /* No conf file */
fclose(f->f);
if (f->buf)
free(f->buf);
if (f->filename)
free(f->filename);
free(f);
}
CONFTOKEN *CONFgettoken(CONFTOKEN *toklist, CONFFILE *file)
{
char *word;
static CONFTOKEN ret = {CONFstring, 0};
int i;
if (ret.name) {
free(ret.name);
ret.name = 0;
}
word = CONFgetword(file);
if (!word)
return(0);
if (toklist) {
for (i = 0; toklist[i].type; i++) {
if (strcmp(word, toklist[i].name) == 0) {
free(word);
return(&toklist[i]);
}
}
}
ret.name = word;
return(&ret);
}
|