Merge branch 'master' of git://git.kernel.org/pub/scm/linux/storage/multipath-tools/
[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, "%s", 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) || !isascii((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                 } else if (!in_string && (*cp == '{' || *cp == '}')) {
243                         token = MALLOC(2);
244
245                         if (!token)
246                                 goto out;
247
248                         *(token) = *cp;
249                         *(token + 1) = '\0';
250                         cp++;
251                 } else {
252                         while ((in_string ||
253                                 (!isspace((int) *cp) && isascii((int) *cp) &&
254                                  *cp != '!' && *cp != '#' && *cp != '{' &&
255                                  *cp != '}')) && *cp != '\0' && *cp != '"')
256                                 cp++;
257                         strlen = cp - start;
258                         token = MALLOC(strlen + 1);
259
260                         if (!token)
261                                 goto out;
262
263                         memcpy(token, start, strlen);
264                         *(token + strlen) = '\0';
265                 }
266                 vector_set_slot(strvec, token);
267
268                 while ((isspace((int) *cp) || !isascii((int) *cp))
269                        && *cp != '\0')
270                         cp++;
271                 if (*cp == '\0' || *cp == '!' || *cp == '#')
272                         return strvec;
273         }
274 out:
275         vector_free(strvec);
276         return NULL;
277 }
278
279 int
280 read_line(char *buf, int size)
281 {
282         int ch;
283         int count = 0;
284
285         while ((ch = fgetc(stream)) != EOF && (int) ch != '\n'
286                && (int) ch != '\r') {
287                 if (count < size)
288                         buf[count] = (int) ch;
289                 else
290                         break;
291                 count++;
292         }
293         return (ch == EOF) ? 0 : 1;
294 }
295
296 vector
297 read_value_block(void)
298 {
299         char *buf;
300         int i;
301         char *str = NULL;
302         char *dup;
303         vector vec = NULL;
304         vector elements = vector_alloc();
305
306         buf = (char *) MALLOC(MAXBUF);
307
308         if (!buf)
309                 return NULL;
310
311         if (!elements)
312                 goto out;
313
314         while (read_line(buf, MAXBUF)) {
315                 vec = alloc_strvec(buf);
316                 if (vec) {
317                         str = VECTOR_SLOT(vec, 0);
318                         if (!strcmp(str, EOB)) {
319                                 free_strvec(vec);
320                                 break;
321                         }
322
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);
327                                         memcpy(dup, str, strlen(str));
328
329                                         if (!vector_alloc_slot(elements))
330                                                 goto out1;
331
332                                         vector_set_slot(elements, dup);
333                                 }
334                         free_strvec(vec);
335                 }
336                 memset(buf, 0, MAXBUF);
337         }
338         FREE(buf);
339         return elements;
340 out1:
341         FREE(dup);
342 out:
343         FREE(buf);
344         return NULL;
345 }
346
347 int
348 alloc_value_block(vector strvec, void (*alloc_func) (vector))
349 {
350         char *buf;
351         char *str = NULL;
352         vector vec = NULL;
353
354         buf = (char *) MALLOC(MAXBUF);
355
356         if (!buf)
357                 return 1;
358
359         while (read_line(buf, MAXBUF)) {
360                 vec = alloc_strvec(buf);
361                 if (vec) {
362                         str = VECTOR_SLOT(vec, 0);
363                         if (!strcmp(str, EOB)) {
364                                 free_strvec(vec);
365                                 break;
366                         }
367
368                         if (VECTOR_SIZE(vec))
369                                 (*alloc_func) (vec);
370
371                         free_strvec(vec);
372                 }
373                 memset(buf, 0, MAXBUF);
374         }
375         FREE(buf);
376         return 0;
377 }
378
379 void *
380 set_value(vector strvec)
381 {
382         char *str = VECTOR_SLOT(strvec, 1);
383         int size = strlen(str);
384         int i = 0;
385         int len = 0;
386         char *alloc = NULL;
387         char *tmp;
388
389         if (*str == '"') {
390                 for (i = 2; i < VECTOR_SIZE(strvec); i++) {
391                         str = VECTOR_SLOT(strvec, i);
392                         len += strlen(str);
393                         if (!alloc)
394                                 alloc =
395                                     (char *) MALLOC(sizeof (char *) *
396                                                     (len + 1));
397                         else {
398                                 alloc =
399                                     REALLOC(alloc, sizeof (char *) * (len + 1));
400                                 tmp = VECTOR_SLOT(strvec, i-1);
401                                 if (*str != '"' && *tmp != '"')
402                                         strncat(alloc, " ", 1);
403                         }
404
405                         if (i != VECTOR_SIZE(strvec)-1)
406                                 strncat(alloc, str, strlen(str));
407                 }
408         } else {
409                 alloc = MALLOC(sizeof (char *) * (size + 1));
410                 memcpy(alloc, str, size);
411         }
412         return alloc;
413 }
414
415 /* non-recursive configuration stream handler */
416 static int kw_level = 0;
417 int
418 process_stream(vector keywords)
419 {
420         int i;
421         int r = 0;
422         struct keyword *keyword;
423         char *str;
424         char *buf;
425         vector strvec;
426
427         buf = MALLOC(MAXBUF);
428
429         if (!buf)
430                 return 1;
431
432         while (read_line(buf, MAXBUF)) {
433                 strvec = alloc_strvec(buf);
434                 memset(buf,0, MAXBUF);
435
436                 if (!strvec)
437                         continue;
438
439                 str = VECTOR_SLOT(strvec, 0);
440
441                 if (!strcmp(str, EOB) && kw_level > 0) {
442                         free_strvec(strvec);
443                         break;
444                 }
445
446                 for (i = 0; i < VECTOR_SIZE(keywords); i++) {
447                         keyword = VECTOR_SLOT(keywords, i);
448
449                         if (!strcmp(keyword->string, str)) {
450                                 if (keyword->handler)
451                                         r += (*keyword->handler) (strvec);
452
453                                 if (keyword->sub) {
454                                         kw_level++;
455                                         r += process_stream(keyword->sub);
456                                         kw_level--;
457                                 }
458                                 break;
459                         }
460                 }
461
462                 free_strvec(strvec);
463         }
464
465         FREE(buf);
466         return r;
467 }
468
469 /* Data initialization */
470 int
471 init_data(char *conf_file, void (*init_keywords) (void))
472 {
473         int r;
474
475         if (!keywords)
476                 keywords = vector_alloc();
477         if (!keywords)
478                 return 1;
479         stream = fopen(conf_file, "r");
480         if (!stream) {
481                 syslog(LOG_WARNING, "Configuration file open problem");
482                 return 1;
483         }
484
485         /* Init Keywords structure */
486         (*init_keywords) ();
487
488 /* Dump configuration *
489   vector_dump(keywords);
490   dump_keywords(keywords, 0);
491 */
492
493         /* Stream handling */
494         r = process_stream(keywords);
495         fclose(stream);
496         //free_keywords(keywords);
497
498         return r;
499 }