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;
30 keyword_alloc(vector keywords, char *string, int (*handler) (vector),
31 int (*print) (char *, int, void *))
33 struct keyword *keyword;
35 keyword = (struct keyword *) MALLOC(sizeof (struct keyword));
40 if (!vector_alloc_slot(keywords)) {
44 keyword->string = string;
45 keyword->handler = handler;
46 keyword->print = print;
48 vector_set_slot(keywords, keyword);
54 install_keyword_root(char *string, int (*handler) (vector))
56 return keyword_alloc(keywords, string, handler, NULL);
60 install_sublevel(void)
66 install_sublevel_end(void)
72 install_keyword(char *string, int (*handler) (vector),
73 int (*print) (char *, int, void *))
76 struct keyword *keyword;
78 /* fetch last keyword */
79 keyword = VECTOR_SLOT(keywords, VECTOR_SIZE(keywords) - 1);
81 /* position to last sub level */
82 for (i = 0; i < sublevel; i++)
84 VECTOR_SLOT(keyword->sub, VECTOR_SIZE(keyword->sub) - 1);
86 /* First sub level allocation */
88 keyword->sub = vector_alloc();
93 /* add new sub keyword */
94 return keyword_alloc(keyword->sub, string, handler, print);
98 free_keywords(vector keywords)
100 struct keyword *keyword;
103 for (i = 0; i < VECTOR_SIZE(keywords); i++) {
104 keyword = VECTOR_SLOT(keywords, i);
106 free_keywords(keyword->sub);
109 vector_free(keywords);
113 find_keyword(vector v, char * name)
115 struct keyword *keyword;
119 if (!name || !keywords)
127 for (i = 0; i < VECTOR_SIZE(v); i++) {
128 keyword = VECTOR_SLOT(v, i);
129 if ((strlen(keyword->string) == len) &&
130 !strcmp(keyword->string, name))
133 keyword = find_keyword(keyword->sub, name);
142 snprint_keyword(char *buff, int len, char *fmt, struct keyword *kw, void *data)
148 if (!kw || !kw->print)
152 if (fwd == len || *f == '\0')
162 fwd += snprintf(buff + fwd, len - fwd, kw->string);
165 r = kw->print(buff + fwd, len - fwd, data);
166 if (!r) { /* no output if no value */
180 alloc_strvec(char *string)
182 char *cp, *start, *token;
192 /* Skip white spaces */
193 while (isspace((int) *cp) && *cp != '\0')
196 /* Return if there is only white spaces */
200 /* Return if string begin with a comment */
201 if (*cp == '!' || *cp == '#')
204 /* Create a vector and alloc each command piece */
205 strvec = vector_alloc();
212 if (!vector_alloc_slot(strvec))
231 while ((in_string || !isspace((int) *cp)) && *cp
232 != '\0' && *cp != '"')
235 token = MALLOC(strlen + 1);
240 memcpy(token, start, strlen);
241 *(token + strlen) = '\0';
243 vector_set_slot(strvec, token);
245 while (isspace((int) *cp) && *cp != '\0')
247 if (*cp == '\0' || *cp == '!' || *cp == '#')
256 read_line(char *buf, int size)
261 while ((ch = fgetc(stream)) != EOF && (int) ch != '\n'
262 && (int) ch != '\r') {
264 buf[count] = (int) ch;
269 return (ch == EOF) ? 0 : 1;
273 read_value_block(void)
280 vector elements = vector_alloc();
282 buf = (char *) MALLOC(MAXBUF);
290 while (read_line(buf, MAXBUF)) {
291 vec = alloc_strvec(buf);
293 str = VECTOR_SLOT(vec, 0);
294 if (!strcmp(str, EOB)) {
299 if (VECTOR_SIZE(vec))
300 for (i = 0; i < VECTOR_SIZE(vec); i++) {
301 str = VECTOR_SLOT(vec, i);
302 dup = (char *) MALLOC(strlen(str) + 1);
303 memcpy(dup, str, strlen(str));
305 if (!vector_alloc_slot(elements))
308 vector_set_slot(elements, dup);
312 memset(buf, 0, MAXBUF);
324 alloc_value_block(vector strvec, void (*alloc_func) (vector))
330 buf = (char *) MALLOC(MAXBUF);
335 while (read_line(buf, MAXBUF)) {
336 vec = alloc_strvec(buf);
338 str = VECTOR_SLOT(vec, 0);
339 if (!strcmp(str, EOB)) {
344 if (VECTOR_SIZE(vec))
349 memset(buf, 0, MAXBUF);
356 set_value(vector strvec)
358 char *str = VECTOR_SLOT(strvec, 1);
359 int size = strlen(str);
366 for (i = 2; i < VECTOR_SIZE(strvec); i++) {
367 str = VECTOR_SLOT(strvec, i);
371 (char *) MALLOC(sizeof (char *) *
375 REALLOC(alloc, sizeof (char *) * (len + 1));
376 tmp = VECTOR_SLOT(strvec, i-1);
377 if (*str != '"' && *tmp != '"')
378 strncat(alloc, " ", 1);
381 if (i != VECTOR_SIZE(strvec)-1)
382 strncat(alloc, str, strlen(str));
385 alloc = MALLOC(sizeof (char *) * (size + 1));
386 memcpy(alloc, str, size);
391 /* non-recursive configuration stream handler */
392 static int kw_level = 0;
394 process_stream(vector keywords)
398 struct keyword *keyword;
403 buf = MALLOC(MAXBUF);
408 while (read_line(buf, MAXBUF)) {
409 strvec = alloc_strvec(buf);
410 memset(buf,0, MAXBUF);
415 str = VECTOR_SLOT(strvec, 0);
417 if (!strcmp(str, EOB) && kw_level > 0) {
422 for (i = 0; i < VECTOR_SIZE(keywords); i++) {
423 keyword = VECTOR_SLOT(keywords, i);
425 if (!strcmp(keyword->string, str)) {
426 if (keyword->handler)
427 r += (*keyword->handler) (strvec);
431 r += process_stream(keyword->sub);
445 /* Data initialization */
447 init_data(char *conf_file, void (*init_keywords) (void))
452 keywords = vector_alloc();
455 stream = fopen(conf_file, "r");
457 syslog(LOG_WARNING, "Configuration file open problem");
461 /* Init Keywords structure */
464 /* Dump configuration *
465 vector_dump(keywords);
466 dump_keywords(keywords, 0);
469 /* Stream handling */
470 r = process_stream(keywords);
472 //free_keywords(keywords);