Merge remote-tracking branch 'hannes/for-christophe'
[platform/upstream/multipath-tools.git] / libmultipath / parser.c
1 /*
2  * Part:        Configuration file parser/reader. Place into the dynamic
3  *              data structure representation the conf file
4  *
5  * Version:     $Id: parser.c,v 1.0.3 2003/05/11 02:28:03 acassen Exp $
6  *
7  * Author:      Alexandre Cassen, <acassen@linux-vs.org>
8  *
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.
13  *
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.
18  */
19
20 #include <syslog.h>
21
22 #include "parser.h"
23 #include "memory.h"
24 #include "debug.h"
25
26 /* local vars */
27 static int sublevel = 0;
28 static vector keywords = NULL;
29 static vector *keywords_addr = NULL;
30 static int line_nr;
31
32 void set_current_keywords (vector *k)
33 {
34         keywords_addr = k;
35         keywords = NULL;
36 }
37
38 int
39 keyword_alloc(vector keywords, char *string, int (*handler) (vector),
40                 int (*print) (char *, int, void *), int unique)
41 {
42         struct keyword *keyword;
43
44         keyword = (struct keyword *) MALLOC(sizeof (struct keyword));
45
46         if (!keyword)
47                 return 1;
48
49         if (!vector_alloc_slot(keywords)) {
50                 FREE(keyword);
51                 return 1;
52         }
53         keyword->string = string;
54         keyword->handler = handler;
55         keyword->print = print;
56         keyword->unique = unique;
57
58         vector_set_slot(keywords, keyword);
59
60         return 0;
61 }
62
63 int
64 install_keyword_root(char *string, int (*handler) (vector))
65 {
66         int r = keyword_alloc(keywords, string, handler, NULL, 1);
67         if (!r)
68                 *keywords_addr = keywords;
69         return r;
70 }
71
72 void
73 install_sublevel(void)
74 {
75         sublevel++;
76 }
77
78 void
79 install_sublevel_end(void)
80 {
81         sublevel--;
82 }
83
84 int
85 _install_keyword(char *string, int (*handler) (vector),
86                 int (*print) (char *, int, void *), int unique)
87 {
88         int i = 0;
89         struct keyword *keyword;
90
91         /* fetch last keyword */
92         keyword = VECTOR_SLOT(keywords, VECTOR_SIZE(keywords) - 1);
93
94         /* position to last sub level */
95         for (i = 0; i < sublevel; i++)
96                 keyword =
97                     VECTOR_SLOT(keyword->sub, VECTOR_SIZE(keyword->sub) - 1);
98
99         /* First sub level allocation */
100         if (!keyword->sub)
101                 keyword->sub = vector_alloc();
102
103         if (!keyword->sub)
104                 return 1;
105
106         /* add new sub keyword */
107         return keyword_alloc(keyword->sub, string, handler, print, unique);
108 }
109
110 void
111 free_keywords(vector keywords)
112 {
113         struct keyword *keyword;
114         int i;
115
116         if (!keywords)
117                 return;
118
119         for (i = 0; i < VECTOR_SIZE(keywords); i++) {
120                 keyword = VECTOR_SLOT(keywords, i);
121                 if (keyword->sub)
122                         free_keywords(keyword->sub);
123                 FREE(keyword);
124         }
125         vector_free(keywords);
126 }
127
128 struct keyword *
129 find_keyword(vector v, char * name)
130 {
131         struct keyword *keyword;
132         int i;
133         int len;
134
135         if (!name || !keywords)
136                 return NULL;
137
138         if (!v)
139                 v = keywords;
140
141         len = strlen(name);
142
143         for (i = 0; i < VECTOR_SIZE(v); i++) {
144                 keyword = VECTOR_SLOT(v, i);
145                 if ((strlen(keyword->string) == len) &&
146                     !strcmp(keyword->string, name))
147                         return keyword;
148                 if (keyword->sub) {
149                         keyword = find_keyword(keyword->sub, name);
150                         if (keyword)
151                                 return keyword;
152                 }
153         }
154         return NULL;
155 }
156
157 int
158 snprint_keyword(char *buff, int len, char *fmt, struct keyword *kw, void *data)
159 {
160         int r;
161         int fwd = 0;
162         char *f = fmt;
163
164         if (!kw || !kw->print)
165                 return 0;
166
167         do {
168                 if (fwd == len || *f == '\0')
169                         break;
170                 if (*f != '%') {
171                         *(buff + fwd) = *f;
172                         fwd++;
173                         continue;
174                 }
175                 f++;
176                 switch(*f) {
177                 case 'k':
178                         fwd += snprintf(buff + fwd, len - fwd, "%s", kw->string);
179                         break;
180                 case 'v':
181                         r = kw->print(buff + fwd, len - fwd, data);
182                         if (!r) { /* no output if no value */
183                                 buff = '\0';
184                                 return 0;
185                         }
186                         fwd += r;
187                         break;
188                 }
189                 if (fwd > len)
190                         fwd = len;
191         } while (*f++);
192         return fwd;
193 }
194
195 vector
196 alloc_strvec(char *string)
197 {
198         char *cp, *start, *token;
199         int strlen;
200         int in_string;
201         vector strvec;
202
203         if (!string)
204                 return NULL;
205
206         cp = string;
207
208         /* Skip white spaces */
209         while ((isspace((int) *cp) || !isascii((int) *cp)) && *cp != '\0')
210                 cp++;
211
212         /* Return if there is only white spaces */
213         if (*cp == '\0')
214                 return NULL;
215
216         /* Return if string begin with a comment */
217         if (*cp == '!' || *cp == '#')
218                 return NULL;
219
220         /* Create a vector and alloc each command piece */
221         strvec = vector_alloc();
222
223         if (!strvec)
224                 return NULL;
225
226         in_string = 0;
227         while (1) {
228                 if (!vector_alloc_slot(strvec))
229                         goto out;
230
231                 start = cp;
232                 if (*cp == '"') {
233                         cp++;
234                         token = MALLOC(2);
235
236                         if (!token)
237                                 goto out;
238
239                         *(token) = '"';
240                         *(token + 1) = '\0';
241                         if (in_string)
242                                 in_string = 0;
243                         else
244                                 in_string = 1;
245                 } else if (!in_string && (*cp == '{' || *cp == '}')) {
246                         token = MALLOC(2);
247
248                         if (!token)
249                                 goto out;
250
251                         *(token) = *cp;
252                         *(token + 1) = '\0';
253                         cp++;
254                 } else {
255                         while ((in_string ||
256                                 (!isspace((int) *cp) && isascii((int) *cp) &&
257                                  *cp != '!' && *cp != '#' && *cp != '{' &&
258                                  *cp != '}')) && *cp != '\0' && *cp != '"')
259                                 cp++;
260                         strlen = cp - start;
261                         token = MALLOC(strlen + 1);
262
263                         if (!token)
264                                 goto out;
265
266                         memcpy(token, start, strlen);
267                         *(token + strlen) = '\0';
268                 }
269                 vector_set_slot(strvec, token);
270
271                 while ((isspace((int) *cp) || !isascii((int) *cp))
272                        && *cp != '\0')
273                         cp++;
274                 if (*cp == '\0' || *cp == '!' || *cp == '#')
275                         return strvec;
276         }
277 out:
278         vector_free(strvec);
279         return NULL;
280 }
281
282 int
283 read_line(char *buf, int size)
284 {
285         int ch;
286         int count = 0;
287
288         while ((ch = fgetc(stream)) != EOF && (int) ch != '\n'
289                && (int) ch != '\r') {
290                 if (count < size)
291                         buf[count] = (int) ch;
292                 else
293                         break;
294                 count++;
295         }
296         return (ch == EOF) ? 0 : 1;
297 }
298
299 vector
300 read_value_block(void)
301 {
302         char *buf;
303         int i;
304         char *str = NULL;
305         char *dup;
306         vector vec = NULL;
307         vector elements = vector_alloc();
308
309         if (!elements)
310                 return NULL;
311
312         buf = (char *) MALLOC(MAXBUF);
313
314         if (!buf)
315                 return NULL;
316
317         while (read_line(buf, MAXBUF)) {
318                 vec = alloc_strvec(buf);
319                 if (vec) {
320                         str = VECTOR_SLOT(vec, 0);
321                         if (!strcmp(str, EOB)) {
322                                 free_strvec(vec);
323                                 break;
324                         }
325
326                         if (VECTOR_SIZE(vec))
327                                 for (i = 0; i < VECTOR_SIZE(vec); i++) {
328                                         str = VECTOR_SLOT(vec, i);
329                                         dup = (char *) MALLOC(strlen(str) + 1);
330                                         if (!dup)
331                                                 goto out;
332                                         memcpy(dup, str, strlen(str));
333
334                                         if (!vector_alloc_slot(elements)) {
335                                                 free_strvec(vec);
336                                                 goto out1;
337                                         }
338
339                                         vector_set_slot(elements, dup);
340                                 }
341                         free_strvec(vec);
342                 }
343                 memset(buf, 0, MAXBUF);
344         }
345         FREE(buf);
346         return elements;
347 out1:
348         FREE(dup);
349 out:
350         FREE(buf);
351         return NULL;
352 }
353
354 int
355 alloc_value_block(vector strvec, void (*alloc_func) (vector))
356 {
357         char *buf;
358         char *str = NULL;
359         vector vec = NULL;
360
361         buf = (char *) MALLOC(MAXBUF);
362
363         if (!buf)
364                 return 1;
365
366         while (read_line(buf, MAXBUF)) {
367                 vec = alloc_strvec(buf);
368                 if (vec) {
369                         str = VECTOR_SLOT(vec, 0);
370                         if (!strcmp(str, EOB)) {
371                                 free_strvec(vec);
372                                 break;
373                         }
374
375                         if (VECTOR_SIZE(vec))
376                                 (*alloc_func) (vec);
377
378                         free_strvec(vec);
379                 }
380                 memset(buf, 0, MAXBUF);
381         }
382         FREE(buf);
383         return 0;
384 }
385
386 void *
387 set_value(vector strvec)
388 {
389         char *str = VECTOR_SLOT(strvec, 1);
390         size_t size;
391         int i = 0;
392         int len = 0;
393         char *alloc = NULL;
394         char *tmp;
395
396         if (!str)
397                 return NULL;
398
399         size = strlen(str);
400         if (size == 0)
401                 return NULL;
402
403         if (*str == '"') {
404                 for (i = 2; i < VECTOR_SIZE(strvec); i++) {
405                         str = VECTOR_SLOT(strvec, i);
406                         len += strlen(str);
407                         if (!alloc)
408                                 alloc =
409                                     (char *) MALLOC(sizeof (char *) *
410                                                     (len + 1));
411                         else {
412                                 alloc =
413                                     REALLOC(alloc, sizeof (char *) * (len + 1));
414                                 tmp = VECTOR_SLOT(strvec, i-1);
415                                 if (alloc && *str != '"' && *tmp != '"')
416                                         strncat(alloc, " ", 1);
417                         }
418
419                         if (alloc && i != VECTOR_SIZE(strvec)-1)
420                                 strncat(alloc, str, strlen(str));
421                 }
422         } else {
423                 alloc = MALLOC(sizeof (char *) * (size + 1));
424                 if (alloc)
425                         memcpy(alloc, str, size);
426         }
427         return alloc;
428 }
429
430 /* non-recursive configuration stream handler */
431 static int kw_level = 0;
432
433 int warn_on_duplicates(vector uniques, char *str)
434 {
435         char *tmp;
436         int i;
437
438         vector_foreach_slot(uniques, tmp, i) {
439                 if (!strcmp(str, tmp)) {
440                         condlog(1, "multipath.conf line %d, duplicate keyword: %s", line_nr, str);
441                         return 0;
442                 }
443         }
444         tmp = strdup(str);
445         if (!tmp)
446                 return 1;
447         if (!vector_alloc_slot(uniques)) {
448                 free(tmp);
449                 return 1;
450         }
451         vector_set_slot(uniques, tmp);
452         return 0;
453 }
454
455 void free_uniques(vector uniques)
456 {
457         char *tmp;
458         int i;
459
460         vector_foreach_slot(uniques, tmp, i)
461                 free(tmp);
462         vector_free(uniques);
463 }
464
465 int
466 process_stream(vector keywords)
467 {
468         int i;
469         int r = 0;
470         struct keyword *keyword;
471         char *str;
472         char *buf;
473         vector strvec;
474         vector uniques;
475
476         uniques = vector_alloc();
477         if (!uniques)
478                 return 1;
479
480         buf = MALLOC(MAXBUF);
481
482         if (!buf) {
483                 vector_free(uniques);
484                 return 1;
485         }
486
487         while (read_line(buf, MAXBUF)) {
488                 line_nr++;
489                 strvec = alloc_strvec(buf);
490                 memset(buf,0, MAXBUF);
491
492                 if (!strvec)
493                         continue;
494
495                 str = VECTOR_SLOT(strvec, 0);
496
497                 if (!strcmp(str, EOB) && kw_level > 0) {
498                         free_strvec(strvec);
499                         break;
500                 }
501
502                 for (i = 0; i < VECTOR_SIZE(keywords); i++) {
503                         keyword = VECTOR_SLOT(keywords, i);
504
505                         if (!strcmp(keyword->string, str)) {
506                                 if (keyword->unique &&
507                                     warn_on_duplicates(uniques, str)) {
508                                                 r = 1;
509                                                 free_strvec(strvec);
510                                                 goto out;
511                                 }
512                                 if (keyword->handler)
513                                         r += (*keyword->handler) (strvec);
514
515                                 if (keyword->sub) {
516                                         kw_level++;
517                                         r += process_stream(keyword->sub);
518                                         kw_level--;
519                                 }
520                                 break;
521                         }
522                 }
523                 if (i >= VECTOR_SIZE(keywords))
524                         condlog(1, "multipath.conf +%d, invalid keyword: %s",
525                                 line_nr, str);
526
527                 free_strvec(strvec);
528         }
529
530 out:
531         FREE(buf);
532         free_uniques(uniques);
533         return r;
534 }
535
536 int alloc_keywords(void)
537 {
538         if (!keywords)
539                 keywords = vector_alloc();
540
541         if (!keywords)
542                 return 1;
543
544         return 0;
545 }
546
547 /* Data initialization */
548 int
549 init_data(char *conf_file, void (*init_keywords) (void))
550 {
551         int r;
552
553         stream = fopen(conf_file, "r");
554         if (!stream) {
555                 syslog(LOG_WARNING, "Configuration file open problem");
556                 return 1;
557         }
558
559         /* Init Keywords structure */
560         (*init_keywords) ();
561
562 /* Dump configuration *
563   vector_dump(keywords);
564   dump_keywords(keywords, 0);
565 */
566
567         /* Stream handling */
568         line_nr = 0;
569         r = process_stream(keywords);
570         fclose(stream);
571         //free_keywords(keywords);
572
573         return r;
574 }