remove accidentally committed junk
[project/uci.git] / util.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 wrappers to standard functions, which
17  * throw exceptions upon failure.
18  */
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/file.h>
22 #include <stdbool.h>
23 #include <unistd.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26
27 #define LINEBUF 32
28 #define LINEBUF_MAX     4096
29
30 static void *uci_malloc(struct uci_context *ctx, size_t size)
31 {
32         void *ptr;
33
34         ptr = malloc(size);
35         if (!ptr)
36                 UCI_THROW(ctx, UCI_ERR_MEM);
37         memset(ptr, 0, size);
38
39         return ptr;
40 }
41
42 static void *uci_realloc(struct uci_context *ctx, void *ptr, size_t size)
43 {
44         ptr = realloc(ptr, size);
45         if (!ptr)
46                 UCI_THROW(ctx, UCI_ERR_MEM);
47
48         return ptr;
49 }
50
51 static char *uci_strdup(struct uci_context *ctx, const char *str)
52 {
53         char *ptr;
54
55         ptr = strdup(str);
56         if (!ptr)
57                 UCI_THROW(ctx, UCI_ERR_MEM);
58
59         return ptr;
60 }
61
62 /*
63  * validate strings for names and types, reject special characters
64  * for names, only alphanum and _ is allowed (shell compatibility)
65  * for types, we allow more characters
66  */
67 static bool uci_validate_str(const char *str, bool name)
68 {
69         if (!*str)
70                 return false;
71
72         while (*str) {
73                 char c = *str;
74                 if (!isalnum(c) && c != '_') {
75                         if (name || (c < 33) || (c > 126))
76                                 return false;
77                 }
78                 str++;
79         }
80         return true;
81 }
82
83 static inline bool uci_validate_name(const char *str)
84 {
85         return uci_validate_str(str, true);
86 }
87
88 static void uci_alloc_parse_context(struct uci_context *ctx)
89 {
90         ctx->pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
91 }
92
93 int uci_parse_tuple(struct uci_context *ctx, char *str, char **package, char **section, char **option, char **value)
94 {
95         char *last = NULL;
96
97         UCI_HANDLE_ERR(ctx);
98         UCI_ASSERT(ctx, str && package && section && option);
99
100         last = strchr(str, '=');
101         if (last) {
102                 *last = 0;
103                 last++;
104         }
105
106         *package = strsep(&str, ".");
107         if (!*package || !uci_validate_name(*package))
108                 goto error;
109
110         *section = strsep(&str, ".");
111         if (!*section)
112                 goto lastval;
113
114         *option = strsep(&str, ".");
115         if (!*option)
116                 goto lastval;
117
118 lastval:
119         if (last) {
120                 if (!value)
121                         goto error;
122
123                 if (!*last)
124                         goto error;
125                 *value = last;
126         }
127
128         if (*section && !uci_validate_name(*section))
129                 goto error;
130         if (*option && !uci_validate_name(*option))
131                 goto error;
132
133         goto done;
134
135 error:
136         UCI_THROW(ctx, UCI_ERR_PARSE);
137
138 done:
139         return 0;
140 }
141
142
143 static void uci_parse_error(struct uci_context *ctx, char *pos, char *reason)
144 {
145         struct uci_parse_context *pctx = ctx->pctx;
146
147         pctx->reason = reason;
148         pctx->byte = pos - pctx->buf;
149         UCI_THROW(ctx, UCI_ERR_PARSE);
150 }
151
152
153 /*
154  * Fetch a new line from the input stream and resize buffer if necessary
155  */
156 static void uci_getln(struct uci_context *ctx, int offset)
157 {
158         struct uci_parse_context *pctx = ctx->pctx;
159         char *p;
160         int ofs;
161
162         if (pctx->buf == NULL) {
163                 pctx->buf = uci_malloc(ctx, LINEBUF);
164                 pctx->bufsz = LINEBUF;
165         }
166
167         ofs = offset;
168         do {
169                 p = &pctx->buf[ofs];
170                 p[ofs] = 0;
171
172                 p = fgets(p, pctx->bufsz - ofs, pctx->file);
173                 if (!p || !*p)
174                         return;
175
176                 ofs += strlen(p);
177                 if (pctx->buf[ofs - 1] == '\n') {
178                         pctx->line++;
179                         pctx->buf[ofs - 1] = 0;
180                         return;
181                 }
182
183                 if (pctx->bufsz > LINEBUF_MAX/2)
184                         uci_parse_error(ctx, p, "line too long");
185
186                 pctx->bufsz *= 2;
187                 pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
188         } while (1);
189 }
190
191 /* 
192  * parse a character escaped by '\'
193  * returns true if the escaped character is to be parsed
194  * returns false if the escaped character is to be ignored
195  */
196 static inline bool parse_backslash(struct uci_context *ctx, char **str)
197 {
198         /* skip backslash */
199         *str += 1;
200
201         /* undecoded backslash at the end of line, fetch the next line */
202         if (!**str) {
203                 *str += 1;
204                 uci_getln(ctx, *str - ctx->pctx->buf);
205                 return false;
206         }
207
208         /* FIXME: decode escaped char, necessary? */
209         return true;
210 }
211
212 /*
213  * move the string pointer forward until a non-whitespace character or
214  * EOL is reached
215  */
216 static void skip_whitespace(struct uci_context *ctx, char **str)
217 {
218 restart:
219         while (**str && isspace(**str))
220                 *str += 1;
221
222         if (**str == '\\') {
223                 if (!parse_backslash(ctx, str))
224                         goto restart;
225         }
226 }
227
228 static inline void addc(char **dest, char **src)
229 {
230         **dest = **src;
231         *dest += 1;
232         *src += 1;
233 }
234
235 /*
236  * parse a double quoted string argument from the command line
237  */
238 static void parse_double_quote(struct uci_context *ctx, char **str, char **target)
239 {
240         char c;
241
242         /* skip quote character */
243         *str += 1;
244
245         while ((c = **str)) {
246                 switch(c) {
247                 case '"':
248                         **target = 0;
249                         *str += 1;
250                         return;
251                 case '\\':
252                         if (!parse_backslash(ctx, str))
253                                 continue;
254                         /* fall through */
255                 default:
256                         addc(target, str);
257                         break;
258                 }
259         }
260         uci_parse_error(ctx, *str, "unterminated \"");
261 }
262
263 /*
264  * parse a single quoted string argument from the command line
265  */
266 static void parse_single_quote(struct uci_context *ctx, char **str, char **target)
267 {
268         char c;
269         /* skip quote character */
270         *str += 1;
271
272         while ((c = **str)) {
273                 switch(c) {
274                 case '\'':
275                         **target = 0;
276                         *str += 1;
277                         return;
278                 default:
279                         addc(target, str);
280                 }
281         }
282         uci_parse_error(ctx, *str, "unterminated '");
283 }
284
285 /*
286  * parse a string from the command line and detect the quoting style
287  */
288 static void parse_str(struct uci_context *ctx, char **str, char **target)
289 {
290         do {
291                 switch(**str) {
292                 case '\'':
293                         parse_single_quote(ctx, str, target);
294                         break;
295                 case '"':
296                         parse_double_quote(ctx, str, target);
297                         break;
298                 case '#':
299                         **str = 0;
300                         /* fall through */
301                 case 0:
302                         goto done;
303                 case '\\':
304                         if (!parse_backslash(ctx, str))
305                                 continue;
306                         /* fall through */
307                 default:
308                         addc(target, str);
309                         break;
310                 }
311         } while (**str && !isspace(**str));
312 done:
313
314         /* 
315          * if the string was unquoted and we've stopped at a whitespace
316          * character, skip to the next one, because the whitespace will
317          * be overwritten by a null byte here
318          */
319         if (**str)
320                 *str += 1;
321
322         /* terminate the parsed string */
323         **target = 0;
324 }
325
326 /*
327  * extract the next argument from the command line
328  */
329 static char *next_arg(struct uci_context *ctx, char **str, bool required, bool name)
330 {
331         char *val;
332         char *ptr;
333
334         val = ptr = *str;
335         skip_whitespace(ctx, str);
336         parse_str(ctx, str, &ptr);
337         if (!*val) {
338                 if (required)
339                         uci_parse_error(ctx, *str, "insufficient arguments");
340                 goto done;
341         }
342
343         if (name && !uci_validate_name(val))
344                 uci_parse_error(ctx, val, "invalid character in field");
345
346 done:
347         return val;
348 }
349
350 int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char **result)
351 {
352         UCI_HANDLE_ERR(ctx);
353         UCI_ASSERT(ctx, str != NULL);
354         UCI_ASSERT(ctx, result != NULL);
355
356         if (ctx->pctx) {
357                 if (ctx->pctx->file != stream) {
358                         ctx->internal = true;
359                         uci_cleanup(ctx);
360                 }
361         } else {
362                 uci_alloc_parse_context(ctx);
363                 ctx->pctx->file = stream;
364         }
365         if (!*str) {
366                 uci_getln(ctx, 0);
367                 *str = ctx->pctx->buf;
368         }
369
370         *result = next_arg(ctx, str, false, false);
371
372         return 0;
373 }
374
375
376 /*
377  * open a stream and go to the right position
378  *
379  * note: when opening for write and seeking to the beginning of
380  * the stream, truncate the file
381  */
382 static FILE *uci_open_stream(struct uci_context *ctx, const char *filename, int pos, bool write, bool create)
383 {
384         struct stat statbuf;
385         FILE *file = NULL;
386         int fd, ret;
387         int mode = (write ? O_RDWR : O_RDONLY);
388
389         if (create)
390                 mode |= O_CREAT;
391
392         if (!write && ((stat(filename, &statbuf) < 0) ||
393                 ((statbuf.st_mode &  S_IFMT) != S_IFREG))) {
394                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
395         }
396
397         fd = open(filename, mode, UCI_FILEMODE);
398         if (fd < 0)
399                 goto error;
400
401         if (flock(fd, (write ? LOCK_EX : LOCK_SH)) < 0)
402                 goto error;
403
404         ret = lseek(fd, 0, pos);
405
406         if (ret < 0)
407                 goto error;
408
409         file = fdopen(fd, (write ? "w+" : "r"));
410         if (file)
411                 goto done;
412
413 error:
414         UCI_THROW(ctx, UCI_ERR_IO);
415 done:
416         return file;
417 }
418
419 static void uci_close_stream(FILE *stream)
420 {
421         int fd;
422
423         if (!stream)
424                 return;
425
426         fd = fileno(stream);
427         flock(fd, LOCK_UN);
428         fclose(stream);
429 }
430
431