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.
30 static int sublevel = 0;
34 keyword_alloc(vector keywords, char *string,
35 int (*handler) (struct config *, vector),
36 int (*print) (struct config *, char *, int, void *), int unique)
38 struct keyword *keyword;
40 keyword = (struct keyword *) MALLOC(sizeof (struct keyword));
45 if (!vector_alloc_slot(keywords)) {
49 keyword->string = string;
50 keyword->handler = handler;
51 keyword->print = print;
52 keyword->unique = unique;
54 vector_set_slot(keywords, keyword);
60 install_sublevel(void)
66 install_sublevel_end(void)
72 _install_keyword(vector keywords, char *string,
73 int (*handler) (struct config *, vector),
74 int (*print) (struct config *, char *, int, void *), int unique)
77 struct keyword *keyword;
79 /* fetch last keyword */
80 keyword = VECTOR_SLOT(keywords, VECTOR_SIZE(keywords) - 1);
82 /* position to last sub level */
83 for (i = 0; i < sublevel; i++)
85 VECTOR_SLOT(keyword->sub, VECTOR_SIZE(keyword->sub) - 1);
87 /* First sub level allocation */
89 keyword->sub = vector_alloc();
94 /* add new sub keyword */
95 return keyword_alloc(keyword->sub, string, handler, print, unique);
99 free_keywords(vector keywords)
101 struct keyword *keyword;
107 for (i = 0; i < VECTOR_SIZE(keywords); i++) {
108 keyword = VECTOR_SLOT(keywords, i);
110 free_keywords(keyword->sub);
113 vector_free(keywords);
117 find_keyword(vector keywords, vector v, char * name)
119 struct keyword *keyword;
123 if (!name || !keywords)
131 for (i = 0; i < VECTOR_SIZE(v); i++) {
132 keyword = VECTOR_SLOT(v, i);
133 if ((strlen(keyword->string) == len) &&
134 !strcmp(keyword->string, name))
137 keyword = find_keyword(keywords, keyword->sub, name);
146 snprint_keyword(char *buff, int len, char *fmt, struct keyword *kw, void *data)
153 if (!kw || !kw->print)
157 if (fwd == len || *f == '\0')
167 fwd += snprintf(buff + fwd, len - fwd, "%s", kw->string);
170 conf = get_multipath_config();
171 r = kw->print(conf, buff + fwd, len - fwd, data);
172 put_multipath_config(conf);
173 if (!r) { /* no output if no value */
187 alloc_strvec(char *string)
189 char *cp, *start, *token;
199 /* Skip white spaces */
200 while ((isspace((int) *cp) || !isascii((int) *cp)) && *cp != '\0')
203 /* Return if there is only white spaces */
207 /* Return if string begin with a comment */
208 if (*cp == '!' || *cp == '#')
211 /* Create a vector and alloc each command piece */
212 strvec = vector_alloc();
219 if (!vector_alloc_slot(strvec))
236 } else if (!in_string && (*cp == '{' || *cp == '}')) {
247 (!isspace((int) *cp) && isascii((int) *cp) &&
248 *cp != '!' && *cp != '#' && *cp != '{' &&
249 *cp != '}')) && *cp != '\0' && *cp != '"')
252 token = MALLOC(strlen + 1);
257 memcpy(token, start, strlen);
258 *(token + strlen) = '\0';
260 vector_set_slot(strvec, token);
262 while ((isspace((int) *cp) || !isascii((int) *cp))
265 if (*cp == '\0' || *cp == '!' || *cp == '#')
274 read_line(FILE *stream, char *buf, int size)
278 if (fgets(buf, size, stream) == NULL)
280 strtok_r(buf, "\n\r", &p);
285 set_value(vector strvec)
287 char *str = VECTOR_SLOT(strvec, 1);
295 condlog(0, "option '%s' missing value",
296 (char *)VECTOR_SLOT(strvec, 0));
301 condlog(0, "option '%s' has empty value",
302 (char *)VECTOR_SLOT(strvec, 0));
306 alloc = MALLOC(sizeof (char) * (size + 1));
308 memcpy(alloc, str, size);
310 condlog(0, "can't allocate memeory for option '%s'",
311 (char *)VECTOR_SLOT(strvec, 0));
314 /* Even empty quotes counts as a value (An empty string) */
315 alloc = (char *) MALLOC(sizeof (char));
317 condlog(0, "can't allocate memeory for option '%s'",
318 (char *)VECTOR_SLOT(strvec, 0));
321 for (i = 2; i < VECTOR_SIZE(strvec); i++) {
322 str = VECTOR_SLOT(strvec, i);
325 condlog(0, "parse error for option '%s'",
326 (char *)VECTOR_SLOT(strvec, 0));
332 /* The first +1 is for the NULL byte. The rest are for the
333 * spaces between words */
334 len += strlen(str) + 1;
335 alloc = REALLOC(alloc, sizeof (char) * len);
338 condlog(0, "can't allocate memeory for option '%s'",
339 (char *)VECTOR_SLOT(strvec, 0));
343 strncat(alloc, " ", 1);
344 strncat(alloc, str, strlen(str));
349 /* non-recursive configuration stream handler */
350 static int kw_level = 0;
352 int warn_on_duplicates(vector uniques, char *str, char *file)
357 vector_foreach_slot(uniques, tmp, i) {
358 if (!strcmp(str, tmp)) {
359 condlog(1, "%s line %d, duplicate keyword: %s",
367 if (!vector_alloc_slot(uniques)) {
371 vector_set_slot(uniques, tmp);
375 void free_uniques(vector uniques)
380 vector_foreach_slot(uniques, tmp, i)
382 vector_free(uniques);
386 is_sublevel_keyword(char *str)
388 return (strcmp(str, "defaults") == 0 || strcmp(str, "blacklist") == 0 ||
389 strcmp(str, "blacklist_exceptions") == 0 ||
390 strcmp(str, "devices") == 0 || strcmp(str, "devices") == 0 ||
391 strcmp(str, "device") == 0 || strcmp(str, "multipaths") == 0 ||
392 strcmp(str, "multipath") == 0);
396 validate_config_strvec(vector strvec, char *file)
401 str = VECTOR_SLOT(strvec, 0);
403 condlog(0, "can't parse option on line %d of %s",
408 if (VECTOR_SIZE(strvec) > 1)
409 condlog(0, "ignoring extra data starting with '%s' on line %d of %s", (char *)VECTOR_SLOT(strvec, 1), line_nr, file);
413 condlog(0, "invalid keyword '%s' on line %d of %s",
417 if (is_sublevel_keyword(str)) {
418 str = VECTOR_SLOT(strvec, 1);
420 condlog(0, "missing '{' on line %d of %s",
422 else if (*str != '{')
423 condlog(0, "expecting '{' on line %d of %s. found '%s'",
425 else if (VECTOR_SIZE(strvec) > 2)
426 condlog(0, "ignoring extra data starting with '%s' on line %d of %s", (char *)VECTOR_SLOT(strvec, 2), line_nr, file);
429 str = VECTOR_SLOT(strvec, 1);
431 condlog(0, "missing value for option '%s' on line %d of %s",
432 (char *)VECTOR_SLOT(strvec, 0), line_nr, file);
436 if (VECTOR_SIZE(strvec) > 2)
437 condlog(0, "ignoring extra data starting with '%s' on line %d of %s", (char *)VECTOR_SLOT(strvec, 2), line_nr, file);
440 for (i = 2; i < VECTOR_SIZE(strvec); i++) {
441 str = VECTOR_SLOT(strvec, i);
443 condlog(0, "can't parse value on line %d of %s",
448 if (VECTOR_SIZE(strvec) > i + 1)
449 condlog(0, "ignoring extra data starting with '%s' on line %d of %s", (char *)VECTOR_SLOT(strvec, (i + 1)), line_nr, file);
453 condlog(0, "missing closing quotes on line %d of %s",
459 process_stream(struct config *conf, FILE *stream, vector keywords, char *file)
463 struct keyword *keyword;
469 uniques = vector_alloc();
473 buf = MALLOC(MAXBUF);
476 vector_free(uniques);
480 while (read_line(stream, buf, MAXBUF)) {
482 strvec = alloc_strvec(buf);
486 if (validate_config_strvec(strvec, file) != 0) {
491 str = VECTOR_SLOT(strvec, 0);
493 if (!strcmp(str, EOB)) {
498 condlog(0, "unmatched '%s' at line %d of %s",
502 for (i = 0; i < VECTOR_SIZE(keywords); i++) {
503 keyword = VECTOR_SLOT(keywords, i);
505 if (!strcmp(keyword->string, str)) {
506 if (keyword->unique &&
507 warn_on_duplicates(uniques, str, file)) {
512 if (keyword->handler) {
513 t = (*keyword->handler) (conf, strvec);
516 condlog(1, "multipath.conf +%d, parsing failed: %s",
522 r += process_stream(conf, stream,
529 if (i >= VECTOR_SIZE(keywords))
530 condlog(1, "%s line %d, invalid keyword: %s",
538 free_uniques(uniques);
542 /* Data initialization */
544 process_file(struct config *conf, char *file)
549 if (!conf->keywords) {
550 condlog(0, "No keywords alocated");
553 stream = fopen(file, "r");
555 condlog(0, "couldn't open configuration file '%s': %s",
556 file, strerror(errno));
560 /* Stream handling */
562 r = process_stream(conf, stream, conf->keywords, file);
564 //free_keywords(keywords);