[libmultipath] fix segfault when no config file
[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
25 /* local vars */
26 static int sublevel = 0;
27 vector keywords = NULL;
28 vector *keywords_addr = NULL;
29
30 void set_current_keywords (vector *k)
31 {
32         keywords_addr = k;
33         keywords = NULL;
34 }
35
36 int
37 keyword_alloc(vector keywords, char *string, int (*handler) (vector),
38                 int (*print) (char *, int, void *))
39 {
40         struct keyword *keyword;
41
42         keyword = (struct keyword *) MALLOC(sizeof (struct keyword));
43
44         if (!keyword)
45                 return 1;
46         
47         if (!vector_alloc_slot(keywords)) {
48                 FREE(keyword);
49                 return 1;
50         }
51         keyword->string = string;
52         keyword->handler = handler;
53         keyword->print = print;
54
55         vector_set_slot(keywords, keyword);
56
57         return 0;
58 }
59
60 int
61 install_keyword_root(char *string, int (*handler) (vector))
62 {
63         int r = keyword_alloc(keywords, string, handler, NULL);
64         if (!r)
65                 *keywords_addr = keywords;
66         return r;
67 }
68
69 void
70 install_sublevel(void)
71 {
72         sublevel++;
73 }
74
75 void
76 install_sublevel_end(void)
77 {
78         sublevel--;
79 }
80
81 int
82 install_keyword(char *string, int (*handler) (vector),
83                 int (*print) (char *, int, void *))
84 {
85         int i = 0;
86         struct keyword *keyword;
87
88         /* fetch last keyword */
89         keyword = VECTOR_SLOT(keywords, VECTOR_SIZE(keywords) - 1);
90
91         /* position to last sub level */
92         for (i = 0; i < sublevel; i++)
93                 keyword =
94                     VECTOR_SLOT(keyword->sub, VECTOR_SIZE(keyword->sub) - 1);
95
96         /* First sub level allocation */
97         if (!keyword->sub)
98                 keyword->sub = vector_alloc();
99
100         if (!keyword->sub)
101                 return 1;
102
103         /* add new sub keyword */
104         return keyword_alloc(keyword->sub, string, handler, print);
105 }
106
107 void
108 free_keywords(vector keywords)
109 {
110         struct keyword *keyword;
111         int i;
112
113         if (!keywords)
114                 return;
115
116         for (i = 0; i < VECTOR_SIZE(keywords); i++) {
117                 keyword = VECTOR_SLOT(keywords, i);
118                 if (keyword->sub)
119                         free_keywords(keyword->sub);
120                 FREE(keyword);
121         }
122         vector_free(keywords);
123 }
124
125 struct keyword *
126 find_keyword(vector v, char * name)
127 {
128         struct keyword *keyword;
129         int i;
130         int len;
131
132         if (!name || !keywords)
133                 return NULL;
134
135         if (!v)
136                 v = keywords;
137
138         len = strlen(name);
139
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))
144                         return keyword;
145                 if (keyword->sub) {
146                         keyword = find_keyword(keyword->sub, name);
147                         if (keyword)
148                                 return keyword;
149                 }
150         }
151         return NULL;
152 }
153
154 int
155 snprint_keyword(char *buff, int len, char *fmt, struct keyword *kw, void *data)
156 {
157         int r;
158         int fwd = 0;
159         char *f = fmt;
160
161         if (!kw || !kw->print)
162                 return 0;
163
164         do {
165                 if (fwd == len || *f == '\0')
166                         break;
167                 if (*f != '%') {
168                         *(buff + fwd) = *f;
169                         fwd++;
170                         continue;
171                 }
172                 f++;
173                 switch(*f) {
174                 case 'k':
175                         fwd += snprintf(buff + fwd, len - fwd, kw->string);
176                         break;
177                 case 'v':
178                         r = kw->print(buff + fwd, len - fwd, data);
179                         if (!r) { /* no output if no value */
180                                 buff = '\0';
181                                 return 0;
182                         }
183                         fwd += r;
184                         break;
185                 }
186                 if (fwd > len)
187                         fwd = len;
188         } while (*f++);
189         return fwd;
190 }
191
192 vector
193 alloc_strvec(char *string)
194 {
195         char *cp, *start, *token;
196         int strlen;
197         int in_string;
198         vector strvec;
199
200         if (!string)
201                 return NULL;
202
203         cp = string;
204
205         /* Skip white spaces */
206         while (isspace((int) *cp) && *cp != '\0')
207                 cp++;
208
209         /* Return if there is only white spaces */
210         if (*cp == '\0')
211                 return NULL;
212
213         /* Return if string begin with a comment */
214         if (*cp == '!' || *cp == '#')
215                 return NULL;
216
217         /* Create a vector and alloc each command piece */
218         strvec = vector_alloc();
219
220         if (!strvec)
221                 return NULL;
222
223         in_string = 0;
224         while (1) {
225                 if (!vector_alloc_slot(strvec))
226                         goto out;
227
228                 start = cp;
229                 if (*cp == '"') {
230                         cp++;
231                         token = MALLOC(2);
232
233                         if (!token)
234                                 goto out;
235
236                         *(token) = '"';
237                         *(token + 1) = '\0';
238                         if (in_string)
239                                 in_string = 0;
240                         else
241                                 in_string = 1;
242
243                 } else {
244                         while ((in_string || !isspace((int) *cp)) && *cp
245                                 != '\0' && *cp != '"')
246                                 cp++;
247                         strlen = cp - start;
248                         token = MALLOC(strlen + 1);
249
250                         if (!token)
251                                 goto out;
252
253                         memcpy(token, start, strlen);
254                         *(token + strlen) = '\0';
255                 }
256                 vector_set_slot(strvec, token);
257
258                 while (isspace((int) *cp) && *cp != '\0')
259                         cp++;
260                 if (*cp == '\0' || *cp == '!' || *cp == '#')
261                         return strvec;
262         }
263 out:
264         vector_free(strvec);
265         return NULL;
266 }
267
268 int
269 read_line(char *buf, int size)
270 {
271         int ch;
272         int count = 0;
273
274         while ((ch = fgetc(stream)) != EOF && (int) ch != '\n'
275                && (int) ch != '\r') {
276                 if (count < size)
277                         buf[count] = (int) ch;
278                 else
279                         break;
280                 count++;
281         }
282         return (ch == EOF) ? 0 : 1;
283 }
284
285 vector
286 read_value_block(void)
287 {
288         char *buf;
289         int i;
290         char *str = NULL;
291         char *dup;
292         vector vec = NULL;
293         vector elements = vector_alloc();
294
295         buf = (char *) MALLOC(MAXBUF);
296
297         if (!buf)
298                 return NULL;
299
300         if (!elements)
301                 goto out;
302
303         while (read_line(buf, MAXBUF)) {
304                 vec = alloc_strvec(buf);
305                 if (vec) {
306                         str = VECTOR_SLOT(vec, 0);
307                         if (!strcmp(str, EOB)) {
308                                 free_strvec(vec);
309                                 break;
310                         }
311
312                         if (VECTOR_SIZE(vec))
313                                 for (i = 0; i < VECTOR_SIZE(vec); i++) {
314                                         str = VECTOR_SLOT(vec, i);
315                                         dup = (char *) MALLOC(strlen(str) + 1);
316                                         memcpy(dup, str, strlen(str));
317
318                                         if (!vector_alloc_slot(elements))
319                                                 goto out1;
320
321                                         vector_set_slot(elements, dup);
322                                 }
323                         free_strvec(vec);
324                 }
325                 memset(buf, 0, MAXBUF);
326         }
327         FREE(buf);
328         return elements;
329 out1:
330         FREE(dup);
331 out:
332         FREE(buf);
333         return NULL;
334 }
335
336 int
337 alloc_value_block(vector strvec, void (*alloc_func) (vector))
338 {
339         char *buf;
340         char *str = NULL;
341         vector vec = NULL;
342
343         buf = (char *) MALLOC(MAXBUF);
344
345         if (!buf)
346                 return 1;
347
348         while (read_line(buf, MAXBUF)) {
349                 vec = alloc_strvec(buf);
350                 if (vec) {
351                         str = VECTOR_SLOT(vec, 0);
352                         if (!strcmp(str, EOB)) {
353                                 free_strvec(vec);
354                                 break;
355                         }
356
357                         if (VECTOR_SIZE(vec))
358                                 (*alloc_func) (vec);
359
360                         free_strvec(vec);
361                 }
362                 memset(buf, 0, MAXBUF);
363         }
364         FREE(buf);
365         return 0;
366 }
367
368 void *
369 set_value(vector strvec)
370 {
371         char *str = VECTOR_SLOT(strvec, 1);
372         int size = strlen(str);
373         int i = 0;
374         int len = 0;
375         char *alloc = NULL;
376         char *tmp;
377
378         if (*str == '"') {
379                 for (i = 2; i < VECTOR_SIZE(strvec); i++) {
380                         str = VECTOR_SLOT(strvec, i);
381                         len += strlen(str);
382                         if (!alloc)
383                                 alloc =
384                                     (char *) MALLOC(sizeof (char *) *
385                                                     (len + 1));
386                         else {
387                                 alloc =
388                                     REALLOC(alloc, sizeof (char *) * (len + 1));
389                                 tmp = VECTOR_SLOT(strvec, i-1);
390                                 if (*str != '"' && *tmp != '"')
391                                         strncat(alloc, " ", 1);
392                         }
393
394                         if (i != VECTOR_SIZE(strvec)-1)
395                                 strncat(alloc, str, strlen(str));
396                 }
397         } else {
398                 alloc = MALLOC(sizeof (char *) * (size + 1));
399                 memcpy(alloc, str, size);
400         }
401         return alloc;
402 }
403
404 /* non-recursive configuration stream handler */
405 static int kw_level = 0;
406 int
407 process_stream(vector keywords)
408 {
409         int i;
410         int r = 0;
411         struct keyword *keyword;
412         char *str;
413         char *buf;
414         vector strvec;
415
416         buf = MALLOC(MAXBUF);
417
418         if (!buf)
419                 return 1;
420
421         while (read_line(buf, MAXBUF)) {
422                 strvec = alloc_strvec(buf);
423                 memset(buf,0, MAXBUF);
424
425                 if (!strvec)
426                         continue;
427
428                 str = VECTOR_SLOT(strvec, 0);
429
430                 if (!strcmp(str, EOB) && kw_level > 0) {
431                         free_strvec(strvec);
432                         break;
433                 }
434
435                 for (i = 0; i < VECTOR_SIZE(keywords); i++) {
436                         keyword = VECTOR_SLOT(keywords, i);
437
438                         if (!strcmp(keyword->string, str)) {
439                                 if (keyword->handler)
440                                         r += (*keyword->handler) (strvec);
441
442                                 if (keyword->sub) {
443                                         kw_level++;
444                                         r += process_stream(keyword->sub);
445                                         kw_level--;
446                                 }
447                                 break;
448                         }
449                 }
450                 
451                 free_strvec(strvec);
452         }
453
454         FREE(buf);
455         return r;
456 }
457
458 /* Data initialization */
459 int
460 init_data(char *conf_file, void (*init_keywords) (void))
461 {
462         int r;
463
464         if (!keywords)
465                 keywords = vector_alloc();
466         if (!keywords)
467                 return 1;
468         stream = fopen(conf_file, "r");
469         if (!stream) {
470                 syslog(LOG_WARNING, "Configuration file open problem");
471                 return 1;
472         }
473
474         /* Init Keywords structure */
475         (*init_keywords) ();
476
477 /* Dump configuration *
478   vector_dump(keywords);
479   dump_keywords(keywords, 0);
480 */
481
482         /* Stream handling */
483         r = process_stream(keywords);
484         fclose(stream);
485         //free_keywords(keywords);
486
487         return r;
488 }