2 * libuci - Library for the Unified Configuration Interface
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
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
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.
16 * This file contains wrappers to standard functions, which
17 * throw exceptions upon failure.
19 #include <sys/types.h>
28 #define LINEBUF_MAX 4096
30 static void *uci_malloc(struct uci_context
*ctx
, size_t size
)
36 UCI_THROW(ctx
, UCI_ERR_MEM
);
42 static void *uci_realloc(struct uci_context
*ctx
, void *ptr
, size_t size
)
44 ptr
= realloc(ptr
, size
);
46 UCI_THROW(ctx
, UCI_ERR_MEM
);
51 static char *uci_strdup(struct uci_context
*ctx
, const char *str
)
57 UCI_THROW(ctx
, UCI_ERR_MEM
);
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
67 static bool uci_validate_str(const char *str
, bool name
)
74 if (!isalnum(c
) && c
!= '_') {
75 if (name
|| (c
< 33) || (c
> 126))
83 static inline bool uci_validate_name(const char *str
)
85 return uci_validate_str(str
, true);
88 static void uci_alloc_parse_context(struct uci_context
*ctx
)
90 ctx
->pctx
= (struct uci_parse_context
*) uci_malloc(ctx
, sizeof(struct uci_parse_context
));
93 int uci_parse_tuple(struct uci_context
*ctx
, char *str
, char **package
, char **section
, char **option
, char **value
)
98 UCI_ASSERT(ctx
, str
&& package
&& section
&& option
);
100 last
= strchr(str
, '=');
106 *package
= strsep(&str
, ".");
107 if (!*package
|| !uci_validate_name(*package
))
110 *section
= strsep(&str
, ".");
114 *option
= strsep(&str
, ".");
128 if (*section
&& !uci_validate_name(*section
))
130 if (*option
&& !uci_validate_name(*option
))
136 UCI_THROW(ctx
, UCI_ERR_PARSE
);
143 static void uci_parse_error(struct uci_context
*ctx
, char *pos
, char *reason
)
145 struct uci_parse_context
*pctx
= ctx
->pctx
;
147 pctx
->reason
= reason
;
148 pctx
->byte
= pos
- pctx
->buf
;
149 UCI_THROW(ctx
, UCI_ERR_PARSE
);
154 * Fetch a new line from the input stream and resize buffer if necessary
156 static void uci_getln(struct uci_context
*ctx
, int offset
)
158 struct uci_parse_context
*pctx
= ctx
->pctx
;
162 if (pctx
->buf
== NULL
) {
163 pctx
->buf
= uci_malloc(ctx
, LINEBUF
);
164 pctx
->bufsz
= LINEBUF
;
172 p
= fgets(p
, pctx
->bufsz
- ofs
, pctx
->file
);
177 if (pctx
->buf
[ofs
- 1] == '\n') {
179 pctx
->buf
[ofs
- 1] = 0;
183 if (pctx
->bufsz
> LINEBUF_MAX
/2)
184 uci_parse_error(ctx
, p
, "line too long");
187 pctx
->buf
= uci_realloc(ctx
, pctx
->buf
, pctx
->bufsz
);
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
196 static inline bool parse_backslash(struct uci_context
*ctx
, char **str
)
201 /* undecoded backslash at the end of line, fetch the next line */
204 uci_getln(ctx
, *str
- ctx
->pctx
->buf
);
208 /* FIXME: decode escaped char, necessary? */
213 * move the string pointer forward until a non-whitespace character or
216 static void skip_whitespace(struct uci_context
*ctx
, char **str
)
219 while (**str
&& isspace(**str
))
223 if (!parse_backslash(ctx
, str
))
228 static inline void addc(char **dest
, char **src
)
236 * parse a double quoted string argument from the command line
238 static void parse_double_quote(struct uci_context
*ctx
, char **str
, char **target
)
242 /* skip quote character */
245 while ((c
= **str
)) {
252 if (!parse_backslash(ctx
, str
))
260 uci_parse_error(ctx
, *str
, "unterminated \"");
264 * parse a single quoted string argument from the command line
266 static void parse_single_quote(struct uci_context
*ctx
, char **str
, char **target
)
269 /* skip quote character */
272 while ((c
= **str
)) {
282 uci_parse_error(ctx
, *str
, "unterminated '");
286 * parse a string from the command line and detect the quoting style
288 static void parse_str(struct uci_context
*ctx
, char **str
, char **target
)
293 parse_single_quote(ctx
, str
, target
);
296 parse_double_quote(ctx
, str
, target
);
304 if (!parse_backslash(ctx
, str
))
311 } while (**str
&& !isspace(**str
));
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
322 /* terminate the parsed string */
327 * extract the next argument from the command line
329 static char *next_arg(struct uci_context
*ctx
, char **str
, bool required
, bool name
)
335 skip_whitespace(ctx
, str
);
336 parse_str(ctx
, str
, &ptr
);
339 uci_parse_error(ctx
, *str
, "insufficient arguments");
343 if (name
&& !uci_validate_name(val
))
344 uci_parse_error(ctx
, val
, "invalid character in field");
350 int uci_parse_argument(struct uci_context
*ctx
, FILE *stream
, char **str
, char **result
)
353 UCI_ASSERT(ctx
, str
!= NULL
);
354 UCI_ASSERT(ctx
, result
!= NULL
);
357 if (ctx
->pctx
->file
!= stream
) {
358 ctx
->internal
= true;
362 uci_alloc_parse_context(ctx
);
363 ctx
->pctx
->file
= stream
;
367 *str
= ctx
->pctx
->buf
;
370 *result
= next_arg(ctx
, str
, false, false);
377 * open a stream and go to the right position
379 * note: when opening for write and seeking to the beginning of
380 * the stream, truncate the file
382 static FILE *uci_open_stream(struct uci_context
*ctx
, const char *filename
, int pos
, bool write
, bool create
)
387 int mode
= (write
? O_RDWR
: O_RDONLY
);
392 if (!write
&& ((stat(filename
, &statbuf
) < 0) ||
393 ((statbuf
.st_mode
& S_IFMT
) != S_IFREG
))) {
394 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
397 fd
= open(filename
, mode
, UCI_FILEMODE
);
401 if (flock(fd
, (write
? LOCK_EX
: LOCK_SH
)) < 0)
404 ret
= lseek(fd
, 0, pos
);
409 file
= fdopen(fd
, (write
? "w+" : "r"));
414 UCI_THROW(ctx
, UCI_ERR_IO
);
419 static void uci_close_stream(FILE *stream
)