2 * Part: Configuration file parser/reader. Place into the dynamic
3 * data structure representation the conf file
5 * Version: $Id: parser.c,v 1.0.3 2003/05/11 02:28:03 acassen Exp $
7 * Author: Alexandre Cassen, <acassen@linux-vs.org>
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
26 static int sublevel = 0;
27 vector keywords = NULL;
28 vector *keywords_addr = NULL;
30 void set_current_keywords (vector *k)
37 keyword_alloc(vector keywords, char *string, int (*handler) (vector),
38 int (*print) (char *, int, void *))
40 struct keyword *keyword;
42 keyword = (struct keyword *) MALLOC(sizeof (struct keyword));
47 if (!vector_alloc_slot(keywords)) {
51 keyword->string = string;
52 keyword->handler = handler;
53 keyword->print = print;
55 vector_set_slot(keywords, keyword);
61 install_keyword_root(char *string, int (*handler) (vector))
63 int r = keyword_alloc(keywords, string, handler, NULL);
65 *keywords_addr = keywords;
70 install_sublevel(void)
76 install_sublevel_end(void)
82 install_keyword(char *string, int (*handler) (vector),
83 int (*print) (char *, int, void *))
86 struct keyword *keyword;
88 /* fetch last keyword */
89 keyword = VECTOR_SLOT(keywords, VECTOR_SIZE(keywords) - 1);
91 /* position to last sub level */
92 for (i = 0; i < sublevel; i++)
94 VECTOR_SLOT(keyword->sub, VECTOR_SIZE(keyword->sub) - 1);
96 /* First sub level allocation */
98 keyword->sub = vector_alloc();
103 /* add new sub keyword */
104 return keyword_alloc(keyword->sub, string, handler, print);
108 free_keywords(vector keywords)
110 struct keyword *keyword;
116 for (i = 0; i < VECTOR_SIZE(keywords); i++) {
117 keyword = VECTOR_SLOT(keywords, i);
119 free_keywords(keyword->sub);
122 vector_free(keywords);
126 find_keyword(vector v, char * name)
128 struct keyword *keyword;
132 if (!name || !keywords)
140 for (i = 0; i < VECTOR_SIZE(v); i++) {
141 keyword = VECTOR_SLOT(v, i);
142 if ((strlen(keyword->string) == len) &&
143 !strcmp(keyword->string, name))
146 keyword = find_keyword(keyword->sub, name);
155 snprint_keyword(char *buff, int len, char *fmt, struct keyword *kw, void *data)
161 if (!kw || !kw->print)
165 if (fwd == len || *f == '\0')
175 fwd += snprintf(buff + fwd, len - fwd, "%s", kw->string);
178 r = kw->print(buff + fwd, len - fwd, data);
179 if (!r) { /* no output if no value */
193 alloc_strvec(char *string)
195 char *cp, *start, *token;
205 /* Skip white spaces */
206 while ((isspace((int) *cp) || !isascii((int) *cp)) && *cp != '\0')
209 /* Return if there is only white spaces */
213 /* Return if string begin with a comment */
214 if (*cp == '!' || *cp == '#')
217 /* Create a vector and alloc each command piece */
218 strvec = vector_alloc();
225 if (!vector_alloc_slot(strvec))
242 } else if (!in_string && (*cp == '{' || *cp == '}')) {
253 (!isspace((int) *cp) && isascii((int) *cp) &&
254 *cp != '!' && *cp != '#' && *cp != '{' &&
255 *cp != '}')) && *cp != '\0' && *cp != '"')
258 token = MALLOC(strlen + 1);
263 memcpy(token, start, strlen);
264 *(token + strlen) = '\0';
266 vector_set_slot(strvec, token);
268 while ((isspace((int) *cp) || !isascii((int) *cp))
271 if (*cp == '\0' || *cp == '!' || *cp == '#')
280 read_line(char *buf, int size)
285 while ((ch = fgetc(stream)) != EOF && (int) ch != '\n'
286 && (int) ch != '\r') {
288 buf[count] = (int) ch;
293 return (ch == EOF) ? 0 : 1;
297 read_value_block(void)
304 vector elements = vector_alloc();
306 buf = (char *) MALLOC(MAXBUF);
314 while (read_line(buf, MAXBUF)) {
315 vec = alloc_strvec(buf);
317 str = VECTOR_SLOT(vec, 0);
318 if (!strcmp(str, EOB)) {
323 if (VECTOR_SIZE(vec))
324 for (i = 0; i < VECTOR_SIZE(vec); i++) {
325 str = VECTOR_SLOT(vec, i);
326 dup = (char *) MALLOC(strlen(str) + 1);
327 memcpy(dup, str, strlen(str));
329 if (!vector_alloc_slot(elements)) {
334 vector_set_slot(elements, dup);
338 memset(buf, 0, MAXBUF);
350 alloc_value_block(vector strvec, void (*alloc_func) (vector))
356 buf = (char *) MALLOC(MAXBUF);
361 while (read_line(buf, MAXBUF)) {
362 vec = alloc_strvec(buf);
364 str = VECTOR_SLOT(vec, 0);
365 if (!strcmp(str, EOB)) {
370 if (VECTOR_SIZE(vec))
375 memset(buf, 0, MAXBUF);
382 set_value(vector strvec)
384 char *str = VECTOR_SLOT(strvec, 1);
385 int size = strlen(str);
392 for (i = 2; i < VECTOR_SIZE(strvec); i++) {
393 str = VECTOR_SLOT(strvec, i);
397 (char *) MALLOC(sizeof (char *) *
401 REALLOC(alloc, sizeof (char *) * (len + 1));
402 tmp = VECTOR_SLOT(strvec, i-1);
403 if (*str != '"' && *tmp != '"')
404 strncat(alloc, " ", 1);
407 if (i != VECTOR_SIZE(strvec)-1)
408 strncat(alloc, str, strlen(str));
411 alloc = MALLOC(sizeof (char *) * (size + 1));
412 memcpy(alloc, str, size);
417 /* non-recursive configuration stream handler */
418 static int kw_level = 0;
420 process_stream(vector keywords)
424 struct keyword *keyword;
429 buf = MALLOC(MAXBUF);
434 while (read_line(buf, MAXBUF)) {
435 strvec = alloc_strvec(buf);
436 memset(buf,0, MAXBUF);
441 str = VECTOR_SLOT(strvec, 0);
443 if (!strcmp(str, EOB) && kw_level > 0) {
448 for (i = 0; i < VECTOR_SIZE(keywords); i++) {
449 keyword = VECTOR_SLOT(keywords, i);
451 if (!strcmp(keyword->string, str)) {
452 if (keyword->handler)
453 r += (*keyword->handler) (strvec);
457 r += process_stream(keyword->sub);
471 /* Data initialization */
473 init_data(char *conf_file, void (*init_keywords) (void))
478 keywords = vector_alloc();
481 stream = fopen(conf_file, "r");
483 syslog(LOG_WARNING, "Configuration file open problem");
487 /* Init Keywords structure */
490 /* Dump configuration *
491 vector_dump(keywords);
492 dump_keywords(keywords, 0);
495 /* Stream handling */
496 r = process_stream(keywords);
498 //free_keywords(keywords);