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();
309 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);
329 memcpy(dup, str, strlen(str));
331 if (!vector_alloc_slot(elements)) {
336 vector_set_slot(elements, dup);
340 memset(buf, 0, MAXBUF);
352 alloc_value_block(vector strvec, void (*alloc_func) (vector))
358 buf = (char *) MALLOC(MAXBUF);
363 while (read_line(buf, MAXBUF)) {
364 vec = alloc_strvec(buf);
366 str = VECTOR_SLOT(vec, 0);
367 if (!strcmp(str, EOB)) {
372 if (VECTOR_SIZE(vec))
377 memset(buf, 0, MAXBUF);
384 set_value(vector strvec)
386 char *str = VECTOR_SLOT(strvec, 1);
387 int size = strlen(str);
394 for (i = 2; i < VECTOR_SIZE(strvec); i++) {
395 str = VECTOR_SLOT(strvec, i);
399 (char *) MALLOC(sizeof (char *) *
403 REALLOC(alloc, sizeof (char *) * (len + 1));
404 tmp = VECTOR_SLOT(strvec, i-1);
405 if (alloc && *str != '"' && *tmp != '"')
406 strncat(alloc, " ", 1);
409 if (alloc && i != VECTOR_SIZE(strvec)-1)
410 strncat(alloc, str, strlen(str));
413 alloc = MALLOC(sizeof (char *) * (size + 1));
415 memcpy(alloc, str, size);
420 /* non-recursive configuration stream handler */
421 static int kw_level = 0;
423 process_stream(vector keywords)
427 struct keyword *keyword;
432 buf = MALLOC(MAXBUF);
437 while (read_line(buf, MAXBUF)) {
438 strvec = alloc_strvec(buf);
439 memset(buf,0, MAXBUF);
444 str = VECTOR_SLOT(strvec, 0);
446 if (!strcmp(str, EOB) && kw_level > 0) {
451 for (i = 0; i < VECTOR_SIZE(keywords); i++) {
452 keyword = VECTOR_SLOT(keywords, i);
454 if (!strcmp(keyword->string, str)) {
455 if (keyword->handler)
456 r += (*keyword->handler) (strvec);
460 r += process_stream(keyword->sub);
474 /* Data initialization */
476 init_data(char *conf_file, void (*init_keywords) (void))
481 keywords = vector_alloc();
484 stream = fopen(conf_file, "r");
486 syslog(LOG_WARNING, "Configuration file open problem");
490 /* Init Keywords structure */
493 /* Dump configuration *
494 vector_dump(keywords);
495 dump_keywords(keywords, 0);
498 /* Stream handling */
499 r = process_stream(keywords);
501 //free_keywords(keywords);