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 misc utility functions and wrappers to standard
17 * functions, which throw exceptions upon failure.
19 #include <sys/types.h>
28 #define LINEBUF_MAX 4096
30 __plugin
void *uci_malloc(struct uci_context
*ctx
, size_t size
)
36 UCI_THROW(ctx
, UCI_ERR_MEM
);
42 __plugin
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 __plugin
char *uci_strdup(struct uci_context
*ctx
, const char *str
)
57 UCI_THROW(ctx
, UCI_ERR_MEM
);
62 /* Based on an efficient hash function published by D. J. Bernstein */
63 static unsigned int djbhash(unsigned int hash
, char *str
)
65 int len
= strlen(str
);
72 for(i
= 0; i
< len
; i
++) {
73 hash
= ((hash
<< 5) + hash
) + str
[i
];
75 return (hash
& 0x7FFFFFFF);
79 * validate strings for names and types, reject special characters
80 * for names, only alphanum and _ is allowed (shell compatibility)
81 * for types, we allow more characters
83 __plugin
bool uci_validate_str(const char *str
, bool name
)
90 if (!isalnum(c
) && c
!= '_') {
91 if (name
|| (c
< 33) || (c
> 126))
99 static inline bool uci_validate_name(const char *str
)
101 return uci_validate_str(str
, true);
104 static inline bool uci_validate_text(const char *str
)
107 if ((*str
== '\r') || (*str
== '\n') ||
108 ((*str
< 32) && (*str
!= '\t')))
115 static void uci_alloc_parse_context(struct uci_context
*ctx
)
117 ctx
->pctx
= (struct uci_parse_context
*) uci_malloc(ctx
, sizeof(struct uci_parse_context
));
120 int uci_parse_tuple(struct uci_context
*ctx
, char *str
, char **package
, char **section
, char **option
, char **value
)
125 UCI_ASSERT(ctx
, str
&& package
&& section
&& option
);
127 last
= strchr(str
, '=');
133 *package
= strsep(&str
, ".");
134 if (!*package
|| !uci_validate_str(*package
, false))
137 *section
= strsep(&str
, ".");
144 *option
= strsep(&str
, ".");
158 if (*section
&& *section
[0] && !uci_validate_name(*section
))
160 if (*option
&& !uci_validate_name(*option
))
162 if (value
&& *value
&& !uci_validate_text(*value
))
168 UCI_THROW(ctx
, UCI_ERR_PARSE
);
175 static void uci_parse_error(struct uci_context
*ctx
, char *pos
, char *reason
)
177 struct uci_parse_context
*pctx
= ctx
->pctx
;
179 pctx
->reason
= reason
;
180 pctx
->byte
= pos
- pctx
->buf
;
181 UCI_THROW(ctx
, UCI_ERR_PARSE
);
186 * Fetch a new line from the input stream and resize buffer if necessary
188 static void uci_getln(struct uci_context
*ctx
, int offset
)
190 struct uci_parse_context
*pctx
= ctx
->pctx
;
194 if (pctx
->buf
== NULL
) {
195 pctx
->buf
= uci_malloc(ctx
, LINEBUF
);
196 pctx
->bufsz
= LINEBUF
;
204 p
= fgets(p
, pctx
->bufsz
- ofs
, pctx
->file
);
209 if (pctx
->buf
[ofs
- 1] == '\n') {
211 pctx
->buf
[ofs
- 1] = 0;
215 if (pctx
->bufsz
> LINEBUF_MAX
/2)
216 uci_parse_error(ctx
, p
, "line too long");
219 pctx
->buf
= uci_realloc(ctx
, pctx
->buf
, pctx
->bufsz
);
224 * parse a character escaped by '\'
225 * returns true if the escaped character is to be parsed
226 * returns false if the escaped character is to be ignored
228 static inline bool parse_backslash(struct uci_context
*ctx
, char **str
)
233 /* undecoded backslash at the end of line, fetch the next line */
236 uci_getln(ctx
, *str
- ctx
->pctx
->buf
);
240 /* FIXME: decode escaped char, necessary? */
245 * move the string pointer forward until a non-whitespace character or
248 static void skip_whitespace(struct uci_context
*ctx
, char **str
)
251 while (**str
&& isspace(**str
))
255 if (!parse_backslash(ctx
, str
))
260 static inline void addc(char **dest
, char **src
)
268 * parse a double quoted string argument from the command line
270 static void parse_double_quote(struct uci_context
*ctx
, char **str
, char **target
)
274 /* skip quote character */
277 while ((c
= **str
)) {
284 if (!parse_backslash(ctx
, str
))
292 uci_parse_error(ctx
, *str
, "unterminated \"");
296 * parse a single quoted string argument from the command line
298 static void parse_single_quote(struct uci_context
*ctx
, char **str
, char **target
)
301 /* skip quote character */
304 while ((c
= **str
)) {
314 uci_parse_error(ctx
, *str
, "unterminated '");
318 * parse a string from the command line and detect the quoting style
320 static void parse_str(struct uci_context
*ctx
, char **str
, char **target
)
326 parse_single_quote(ctx
, str
, target
);
329 parse_double_quote(ctx
, str
, target
);
340 if (!parse_backslash(ctx
, str
))
347 } while (**str
&& !isspace(**str
));
351 * if the string was unquoted and we've stopped at a whitespace
352 * character, skip to the next one, because the whitespace will
353 * be overwritten by a null byte here
358 /* terminate the parsed string */
363 * extract the next argument from the command line
365 static char *next_arg(struct uci_context
*ctx
, char **str
, bool required
, bool name
)
371 skip_whitespace(ctx
, str
);
376 parse_str(ctx
, str
, &ptr
);
380 uci_parse_error(ctx
, *str
, "insufficient arguments");
384 if (name
&& !uci_validate_name(val
))
385 uci_parse_error(ctx
, val
, "invalid character in field");
391 int uci_parse_argument(struct uci_context
*ctx
, FILE *stream
, char **str
, char **result
)
394 UCI_ASSERT(ctx
, str
!= NULL
);
395 UCI_ASSERT(ctx
, result
!= NULL
);
398 if (ctx
->pctx
->file
!= stream
) {
402 uci_alloc_parse_context(ctx
);
403 ctx
->pctx
->file
= stream
;
407 *str
= ctx
->pctx
->buf
;
410 *result
= next_arg(ctx
, str
, false, false);
417 * open a stream and go to the right position
419 * note: when opening for write and seeking to the beginning of
420 * the stream, truncate the file
422 static FILE *uci_open_stream(struct uci_context
*ctx
, const char *filename
, int pos
, bool write
, bool create
)
427 int mode
= (write
? O_RDWR
: O_RDONLY
);
432 if (!write
&& ((stat(filename
, &statbuf
) < 0) ||
433 ((statbuf
.st_mode
& S_IFMT
) != S_IFREG
))) {
434 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
437 fd
= open(filename
, mode
, UCI_FILEMODE
);
441 if (flock(fd
, (write
? LOCK_EX
: LOCK_SH
)) < 0)
444 ret
= lseek(fd
, 0, pos
);
449 file
= fdopen(fd
, (write
? "w+" : "r"));
454 UCI_THROW(ctx
, UCI_ERR_IO
);
459 static void uci_close_stream(FILE *stream
)