4c517156cbbfc0be53acdb433d0a9ec2b29ef85d
[project/uci.git] / parse.c
1 /*
2  * libuci - Library for the Unified Configuration Interface
3  * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
4  *
5  * this program is free software; you can redistribute it and/or modify
6  * it under the terms of the gnu lesser general public license version 2.1
7  * as published by the free software foundation
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.  See the
12  * GNU General Public License for more details.
13  */
14
15 /*
16  * This file contains the code for parsing uci config files
17  */
18
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <ctype.h>
24
25 #define LINEBUF 128
26 #define LINEBUF_MAX     4096
27
28 /*
29  * Fetch a new line from the input stream and resize buffer if necessary
30  */
31 static void uci_getln(struct uci_context *ctx)
32 {
33         struct uci_parse_context *pctx = ctx->pctx;
34         char *p;
35         int ofs;
36
37         if (pctx->buf == NULL) {
38                 pctx->buf = uci_malloc(ctx, LINEBUF);
39                 pctx->bufsz = LINEBUF;
40         }
41
42         ofs = 0;
43         do {
44                 p = &pctx->buf[ofs];
45                 p[ofs] = 0;
46
47                 p = fgets(p, pctx->bufsz - ofs, pctx->file);
48                 if (!p || !p[ofs])
49                         return;
50
51                 ofs += strlen(p);
52                 if (pctx->buf[ofs - 1] == '\n') {
53                         pctx->line++;
54                         pctx->buf[ofs - 1] = 0;
55                         return;
56                 }
57
58                 if (pctx->bufsz > LINEBUF_MAX/2) {
59                         pctx->byte = LINEBUF_MAX;
60                         UCI_THROW(ctx, UCI_ERR_PARSE);
61                 }
62
63                 pctx->bufsz *= 2;
64                 pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
65         } while (1);
66 }
67
68 /*
69  * Clean up all extra memory used by the parser
70  */
71 static void uci_parse_cleanup(struct uci_context *ctx)
72 {
73         struct uci_parse_context *pctx;
74
75         pctx = ctx->pctx;
76         if (!pctx)
77                 return;
78
79         if (pctx->cfg) {
80                 uci_list_del(&pctx->cfg->list);
81                 uci_drop_file(pctx->cfg);
82         }
83         if (pctx->buf)
84                 free(pctx->buf);
85         if (pctx->file)
86                 fclose(pctx->file);
87
88         free(pctx);
89 }
90
91 /*
92  * move the string pointer forward until a non-whitespace character or
93  * EOL is reached
94  */
95 static void skip_whitespace(char **str)
96 {
97         while (**str && isspace(**str))
98                 *str += 1;
99 }
100
101 static inline void addc(char **dest, char **src)
102 {
103         **dest = **src;
104         *dest += 1;
105         *src += 1;
106 }
107
108 static inline void parse_backslash(char **str, char **target)
109 {
110         /* skip backslash */
111         *str += 1;
112         /* FIXME: decode escaped characters? */
113         addc(target, str);
114 }
115
116 /*
117  * parse a double quoted string argument from the command line
118  */
119 static void parse_double_quote(struct uci_context *ctx, char **str, char **target)
120 {
121         char c;
122
123         /* skip quote character */
124         *str += 1;
125
126         while ((c = **str)) {
127                 switch(c) {
128                 case '\\':
129                         parse_backslash(str, target);
130                         continue;
131                 case '"':
132                         **target = 0;
133                         *str += 1;
134                         return;
135                 default:
136                         addc(target, str);
137                         break;
138                 }
139         }
140         ctx->pctx->byte = *str - ctx->pctx->buf;
141         UCI_THROW(ctx, UCI_ERR_PARSE);
142 }
143
144 /*
145  * parse a single quoted string argument from the command line
146  */
147 static void parse_single_quote(struct uci_context *ctx, char **str, char **target)
148 {
149         char c;
150         /* skip quote character */
151         *str += 1;
152
153         while ((c = **str)) {
154                 switch(c) {
155                 case '\'':
156                         **target = 0;
157                         *str += 1;
158                         return;
159                 default:
160                         addc(target, str);
161                 }
162         }
163         ctx->pctx->byte = *str - ctx->pctx->buf;
164         UCI_THROW(ctx, UCI_ERR_PARSE);
165 }
166
167 /*
168  * parse a string from the command line and detect the quoting style
169  */
170 static void parse_str(struct uci_context *ctx, char **str, char **target)
171 {
172         do {
173                 switch(**str) {
174                 case '\\':
175                         parse_backslash(str, target);
176                         continue;
177                 case '\'':
178                         parse_single_quote(ctx, str, target);
179                         break;
180                 case '"':
181                         parse_double_quote(ctx, str, target);
182                         break;
183                 case 0:
184                         goto done;
185                 default:
186                         addc(target, str);
187                         break;
188                 }
189         } while (**str && !isspace(**str));
190 done:
191
192         /* 
193          * if the string was unquoted and we've stopped at a whitespace
194          * character, skip to the next one, because the whitespace will
195          * be overwritten by a null byte here
196          */
197         if (**str)
198                 *str += 1;
199
200         /* terminate the parsed string */
201         **target = 0;
202 }
203
204 /*
205  * extract the next argument from the command line
206  */
207 static char *next_arg(struct uci_context *ctx, char **str, bool required)
208 {
209         char *val;
210         char *ptr;
211
212         val = ptr = *str;
213         skip_whitespace(str);
214         parse_str(ctx, str, &ptr);
215         if (required && !*val) {
216                 ctx->pctx->byte = *str - ctx->pctx->buf;
217                 UCI_THROW(ctx, UCI_ERR_PARSE);
218         }
219
220         return uci_strdup(ctx, val);
221 }
222
223 /*
224  * verify that the end of the line or command is reached.
225  * throw an error if extra arguments are given on the command line
226  */
227 static void assert_eol(struct uci_context *ctx, char **str)
228 {
229         char *tmp;
230
231         tmp = next_arg(ctx, str, false);
232         if (tmp && *tmp) {
233                 ctx->pctx->byte = tmp - ctx->pctx->buf;
234                 UCI_THROW(ctx, UCI_ERR_PARSE);
235         }
236 }
237
238 /*
239  * parse the 'config' uci command (open a section)
240  */
241 static void uci_parse_config(struct uci_context *ctx, char **str)
242 {
243         char *type, *name;
244
245         *str += strlen(*str) + 1;
246
247         if (!*str) {
248                 ctx->pctx->byte = *str - ctx->pctx->buf;
249                 UCI_THROW(ctx, UCI_ERR_PARSE);
250         }
251
252         type = next_arg(ctx, str, true);
253         name = next_arg(ctx, str, false);
254         assert_eol(ctx, str);
255 }
256
257 /*
258  * parse the 'option' uci command (open a value)
259  */
260 static void uci_parse_option(struct uci_context *ctx, char **str)
261 {
262         char *name, *value;
263
264         *str += strlen(*str) + 1;
265
266         name = next_arg(ctx, str, true);
267         value = next_arg(ctx, str, true);
268         assert_eol(ctx, str);
269
270         DPRINTF("\tOption: %s=\"%s\"\n", name, value);
271 }
272
273 /*
274  * parse a complete input line, split up combined commands by ';'
275  */
276 static void uci_parse_line(struct uci_context *ctx)
277 {
278         struct uci_parse_context *pctx = ctx->pctx;
279         char *word, *brk;
280
281         for (word = strtok_r(pctx->buf, ";", &brk);
282                  word;
283                  word = strtok_r(NULL, ";", &brk)) {
284
285                 char *pbrk;
286                 word = strtok_r(word, " \t", &pbrk);
287
288                 switch(word[0]) {
289                         case 'c':
290                                 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
291                                         uci_parse_config(ctx, &word);
292                                 break;
293                         case 'o':
294                                 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
295                                         uci_parse_option(ctx, &word);
296                                 break;
297                         default:
298                                 pctx->byte = word - pctx->buf;
299                                 UCI_THROW(ctx, UCI_ERR_PARSE);
300                                 break;
301                 }
302         }
303 }
304
305 int uci_load(struct uci_context *ctx, const char *name)
306 {
307         struct uci_parse_context *pctx;
308         struct stat statbuf;
309         char *filename;
310         bool confpath;
311
312         UCI_HANDLE_ERR(ctx);
313         UCI_ASSERT(ctx, name != NULL);
314
315         /* make sure no memory from previous parse attempts is leaked */
316         uci_parse_cleanup(ctx);
317
318         pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
319         ctx->pctx = pctx;
320
321         switch (name[0]) {
322         case '.':
323         case '/':
324                 /* absolute/relative path outside of /etc/config */
325                 filename = (char *) name;
326                 confpath = false;
327                 break;
328         default:
329                 filename = uci_malloc(ctx, strlen(name) + sizeof(UCI_CONFDIR) + 2);
330                 sprintf(filename, UCI_CONFDIR "/%s", name);
331                 confpath = true;
332                 break;
333         }
334
335         if ((stat(filename, &statbuf) < 0) ||
336                 ((statbuf.st_mode &  S_IFMT) != S_IFREG))
337                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
338
339         pctx->file = fopen(filename, "r");
340         if (filename != name)
341                 free(filename);
342
343         if (!pctx->file)
344                 UCI_THROW(ctx, UCI_ERR_IO);
345
346         pctx->cfg = uci_alloc_file(ctx, name);
347
348         while (!feof(pctx->file)) {
349                 uci_getln(ctx);
350                 if (pctx->buf[0])
351                         uci_parse_line(ctx);
352         }
353
354         /* add to main config file list */
355         uci_list_add(&ctx->root, &pctx->cfg->list);
356         pctx->cfg = NULL;
357
358         /* no error happened, we can get rid of the parser context now */
359         uci_parse_cleanup(ctx);
360
361         return 0;
362 }
363