more deps
[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 <ctype.h>
20
21 #define LINEBUF 128
22 #define LINEBUF_MAX 4096
23
24 /*
25 * Fetch a new line from the input stream and resize buffer if necessary
26 */
27 static void uci_getln(struct uci_context *ctx)
28 {
29 struct uci_parse_context *pctx = ctx->pctx;
30 char *p;
31 int ofs;
32
33 if (pctx->buf == NULL) {
34 pctx->buf = uci_malloc(ctx, LINEBUF);
35 pctx->bufsz = LINEBUF;
36 }
37
38 ofs = 0;
39 do {
40 p = &pctx->buf[ofs];
41 p[ofs] = 0;
42
43 p = fgets(p, pctx->bufsz - ofs, pctx->file);
44 if (!p || !p[ofs])
45 return;
46
47 ofs += strlen(p);
48 if (pctx->buf[ofs - 1] == '\n') {
49 pctx->line++;
50 pctx->buf[ofs - 1] = 0;
51 return;
52 }
53
54 if (pctx->bufsz > LINEBUF_MAX/2) {
55 pctx->byte = LINEBUF_MAX;
56 UCI_THROW(ctx, UCI_ERR_PARSE);
57 }
58
59 pctx->bufsz *= 2;
60 pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
61 } while (1);
62 }
63
64 /*
65 * Clean up all extra memory used by the parser
66 */
67 static void uci_parse_cleanup(struct uci_context *ctx)
68 {
69 struct uci_parse_context *pctx;
70
71 pctx = ctx->pctx;
72 if (!pctx)
73 return;
74
75 if (pctx->cfg)
76 uci_drop_file(pctx->cfg);
77 if (pctx->buf)
78 free(pctx->buf);
79 if (pctx->file)
80 fclose(pctx->file);
81
82 free(pctx);
83 }
84
85 /*
86 * move the string pointer forward until a non-whitespace character or
87 * EOL is reached
88 */
89 static void skip_whitespace(char **str)
90 {
91 while (**str && isspace(**str))
92 *str += 1;
93 }
94
95 /*
96 * parse a double quoted string argument from the command line
97 */
98 static char *parse_double_quote(char **str)
99 {
100 char *val;
101
102 *str += 1;
103 val = *str;
104 while (**str) {
105
106 /* skip escaped characters */
107 if (**str == '\\') {
108 *str += 2;
109 continue;
110 }
111
112 /* check for the end of the quoted string */
113 if (**str == '"') {
114 **str = 0;
115 *str += 1;
116 return val;
117 }
118 *str += 1;
119 }
120
121 return NULL;
122 }
123
124 /*
125 * parse a single quoted string argument from the command line
126 */
127 static char *parse_single_quote(char **str)
128 {
129 /* TODO: implement */
130 return NULL;
131 }
132
133 /*
134 * extract the next word from the command line (unquoted argument)
135 */
136 static char *parse_unquoted(char **str)
137 {
138 char *val;
139
140 val = *str;
141
142 while (**str && !isspace(**str))
143 *str += 1;
144
145 if (**str) {
146 **str = 0;
147 *str += 1;
148 }
149
150 return val;
151 }
152
153 /*
154 * extract the next argument from the command line
155 */
156 static char *next_arg(struct uci_context *ctx, char **str, bool required)
157 {
158 char *val;
159
160 skip_whitespace(str);
161 switch (**str) {
162 case '"':
163 val = parse_double_quote(str);
164 case '\'':
165 val = parse_single_quote(str);
166 case 0:
167 val = NULL;
168 default:
169 val = parse_unquoted(str);
170 }
171
172 if (required && !val) {
173 ctx->pctx->byte = *str - ctx->pctx->buf;
174 UCI_THROW(ctx, UCI_ERR_PARSE);
175 }
176
177 return val;
178 }
179
180 /*
181 * verify that the end of the line or command is reached.
182 * throw an error if extra arguments are given on the command line
183 */
184 static void assert_eol(struct uci_context *ctx, char **str)
185 {
186 char *tmp;
187
188 tmp = next_arg(ctx, str, false);
189 if (tmp && *tmp) {
190 ctx->pctx->byte = tmp - ctx->pctx->buf;
191 UCI_THROW(ctx, UCI_ERR_PARSE);
192 }
193 }
194
195 /*
196 * parse the 'config' uci command (open a section)
197 */
198 static void uci_parse_config(struct uci_context *ctx, char **str)
199 {
200 char *type, *name;
201
202 *str += strlen(*str) + 1;
203
204 if (!*str) {
205 ctx->pctx->byte = *str - ctx->pctx->buf;
206 UCI_THROW(ctx, UCI_ERR_PARSE);
207 }
208
209 type = next_arg(ctx, str, true);
210 name = next_arg(ctx, str, false);
211 assert_eol(ctx, str);
212
213 DPRINTF("Section<%s>: %s\n", type, name);
214 }
215
216 /*
217 * parse the 'option' uci command (open a value)
218 */
219 static void uci_parse_option(struct uci_context *ctx, char **str)
220 {
221 char *name, *value;
222
223 *str += strlen(*str) + 1;
224
225 name = next_arg(ctx, str, true);
226 value = next_arg(ctx, str, true);
227 assert_eol(ctx, str);
228
229 DPRINTF("\tOption: %s=\"%s\"\n", name, value);
230 }
231
232 /*
233 * parse a complete input line, split up combined commands by ';'
234 */
235 static void uci_parse_line(struct uci_context *ctx)
236 {
237 struct uci_parse_context *pctx = ctx->pctx;
238 char *word, *brk;
239
240 for (word = strtok_r(pctx->buf, ";", &brk);
241 word;
242 word = strtok_r(NULL, ";", &brk)) {
243
244 char *pbrk;
245 word = strtok_r(word, " \t", &pbrk);
246
247 switch(word[0]) {
248 case 'c':
249 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
250 uci_parse_config(ctx, &word);
251 break;
252 case 'o':
253 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
254 uci_parse_option(ctx, &word);
255 break;
256 default:
257 pctx->byte = word - pctx->buf;
258 UCI_THROW(ctx, UCI_ERR_PARSE);
259 break;
260 }
261 }
262 }
263
264 int uci_parse(struct uci_context *ctx, const char *name)
265 {
266 struct uci_parse_context *pctx;
267
268 UCI_HANDLE_ERR(ctx);
269 UCI_ASSERT(ctx, name != NULL);
270
271 /* make sure no memory from previous parse attempts is leaked */
272 uci_parse_cleanup(ctx);
273
274 pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
275 ctx->pctx = pctx;
276
277 /* TODO: use /etc/config/ */
278 pctx->file = fopen(name, "r");
279 if (!pctx->file)
280 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
281
282 pctx->cfg = uci_alloc_file(ctx, name);
283
284 while (!feof(pctx->file)) {
285 uci_getln(ctx);
286 if (*(pctx->buf))
287 uci_parse_line(ctx);
288 }
289
290 /* add to main config file list */
291 uci_list_add(&ctx->root, &pctx->cfg->list);
292 pctx->cfg = NULL;
293
294 /* if no error happened, we can get rid of the parser context now */
295 uci_parse_cleanup(ctx);
296
297 return 0;
298 }
299
300