f0b2094810bcd8c75cb6097b62a76dbaabbc45da
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>
29 #define LINEBUF_MAX 4096
31 __plugin
void *uci_malloc(struct uci_context
*ctx
, size_t size
)
37 UCI_THROW(ctx
, UCI_ERR_MEM
);
43 __plugin
void *uci_realloc(struct uci_context
*ctx
, void *ptr
, size_t size
)
45 ptr
= realloc(ptr
, size
);
47 UCI_THROW(ctx
, UCI_ERR_MEM
);
52 __plugin
char *uci_strdup(struct uci_context
*ctx
, const char *str
)
58 UCI_THROW(ctx
, UCI_ERR_MEM
);
63 /* Based on an efficient hash function published by D. J. Bernstein */
64 static unsigned int djbhash(unsigned int hash
, char *str
)
66 int len
= strlen(str
);
73 for(i
= 0; i
< len
; i
++) {
74 hash
= ((hash
<< 5) + hash
) + str
[i
];
76 return (hash
& 0x7FFFFFFF);
80 * validate strings for names and types, reject special characters
81 * for names, only alphanum and _ is allowed (shell compatibility)
82 * for types, we allow more characters
84 __plugin
bool uci_validate_str(const char *str
, bool name
)
90 unsigned char c
= *str
;
91 if (!isalnum(c
) && c
!= '_') {
92 if (name
|| (c
< 33) || (c
> 126))
100 static inline bool uci_validate_package(const char *str
)
102 return uci_validate_str(str
, false);
105 static inline bool uci_validate_type(const char *str
)
107 return uci_validate_str(str
, false);
110 static inline bool uci_validate_name(const char *str
)
112 return uci_validate_str(str
, true);
115 bool uci_validate_text(const char *str
)
118 unsigned char c
= *str
;
119 if ((c
== '\r') || (c
== '\n') ||
120 ((c
< 32) && (c
!= '\t')))
127 static void uci_alloc_parse_context(struct uci_context
*ctx
)
129 ctx
->pctx
= (struct uci_parse_context
*) uci_malloc(ctx
, sizeof(struct uci_parse_context
));
132 int uci_parse_ptr(struct uci_context
*ctx
, struct uci_ptr
*ptr
, char *str
)
138 UCI_ASSERT(ctx
, str
);
139 UCI_ASSERT(ctx
, ptr
);
141 memset(ptr
, 0, sizeof(struct uci_ptr
));
144 last
= strchr(str
, '=');
151 ptr
->package
= strsep(&str
, ".");
155 ptr
->section
= strsep(&str
, ".");
157 ptr
->target
= UCI_TYPE_PACKAGE
;
161 ptr
->option
= strsep(&str
, ".");
163 ptr
->target
= UCI_TYPE_SECTION
;
166 ptr
->target
= UCI_TYPE_OPTION
;
169 tmp
= strsep(&str
, ".");
174 if (ptr
->package
&& !uci_validate_package(ptr
->package
))
176 if (ptr
->section
&& !uci_validate_name(ptr
->section
))
177 ptr
->flags
|= UCI_LOOKUP_EXTENDED
;
178 if (ptr
->option
&& !uci_validate_name(ptr
->option
))
180 if (ptr
->value
&& !uci_validate_text(ptr
->value
))
186 memset(ptr
, 0, sizeof(struct uci_ptr
));
187 UCI_THROW(ctx
, UCI_ERR_PARSE
);
191 static void uci_parse_error(struct uci_context
*ctx
, char *pos
, char *reason
)
193 struct uci_parse_context
*pctx
= ctx
->pctx
;
195 pctx
->reason
= reason
;
196 pctx
->byte
= pos
- pctx
->buf
;
197 UCI_THROW(ctx
, UCI_ERR_PARSE
);
202 * Fetch a new line from the input stream and resize buffer if necessary
204 static void uci_getln(struct uci_context
*ctx
, int offset
)
206 struct uci_parse_context
*pctx
= ctx
->pctx
;
210 if (pctx
->buf
== NULL
) {
211 pctx
->buf
= uci_malloc(ctx
, LINEBUF
);
212 pctx
->bufsz
= LINEBUF
;
220 p
= fgets(p
, pctx
->bufsz
- ofs
, pctx
->file
);
225 if (pctx
->buf
[ofs
- 1] == '\n') {
227 pctx
->buf
[ofs
- 1] = 0;
231 if (pctx
->bufsz
> LINEBUF_MAX
/2)
232 uci_parse_error(ctx
, p
, "line too long");
235 pctx
->buf
= uci_realloc(ctx
, pctx
->buf
, pctx
->bufsz
);
240 * parse a character escaped by '\'
241 * returns true if the escaped character is to be parsed
242 * returns false if the escaped character is to be ignored
244 static inline bool parse_backslash(struct uci_context
*ctx
, char **str
)
249 /* undecoded backslash at the end of line, fetch the next line */
252 uci_getln(ctx
, *str
- ctx
->pctx
->buf
);
256 /* FIXME: decode escaped char, necessary? */
261 * move the string pointer forward until a non-whitespace character or
264 static void skip_whitespace(struct uci_context
*ctx
, char **str
)
267 while (**str
&& isspace(**str
))
271 if (!parse_backslash(ctx
, str
))
276 static inline void addc(char **dest
, char **src
)
284 * parse a double quoted string argument from the command line
286 static void parse_double_quote(struct uci_context
*ctx
, char **str
, char **target
)
290 /* skip quote character */
293 while ((c
= **str
)) {
300 if (!parse_backslash(ctx
, str
))
308 uci_parse_error(ctx
, *str
, "unterminated \"");
312 * parse a single quoted string argument from the command line
314 static void parse_single_quote(struct uci_context
*ctx
, char **str
, char **target
)
317 /* skip quote character */
320 while ((c
= **str
)) {
330 uci_parse_error(ctx
, *str
, "unterminated '");
334 * parse a string from the command line and detect the quoting style
336 static void parse_str(struct uci_context
*ctx
, char **str
, char **target
)
342 parse_single_quote(ctx
, str
, target
);
345 parse_double_quote(ctx
, str
, target
);
356 if (!parse_backslash(ctx
, str
))
363 } while (**str
&& !isspace(**str
));
367 * if the string was unquoted and we've stopped at a whitespace
368 * character, skip to the next one, because the whitespace will
369 * be overwritten by a null byte here
374 /* terminate the parsed string */
379 * extract the next argument from the command line
381 static char *next_arg(struct uci_context
*ctx
, char **str
, bool required
, bool name
)
387 skip_whitespace(ctx
, str
);
392 parse_str(ctx
, str
, &ptr
);
396 uci_parse_error(ctx
, *str
, "insufficient arguments");
400 if (name
&& !uci_validate_name(val
))
401 uci_parse_error(ctx
, val
, "invalid character in field");
407 int uci_parse_argument(struct uci_context
*ctx
, FILE *stream
, char **str
, char **result
)
410 UCI_ASSERT(ctx
, str
!= NULL
);
411 UCI_ASSERT(ctx
, result
!= NULL
);
413 if (ctx
->pctx
&& (ctx
->pctx
->file
!= stream
))
417 uci_alloc_parse_context(ctx
);
419 ctx
->pctx
->file
= stream
;
423 *str
= ctx
->pctx
->buf
;
426 *result
= next_arg(ctx
, str
, false, false);
433 * open a stream and go to the right position
435 * note: when opening for write and seeking to the beginning of
436 * the stream, truncate the file
438 static FILE *uci_open_stream(struct uci_context
*ctx
, const char *filename
, int pos
, bool write
, bool create
)
443 int mode
= (write
? O_RDWR
: O_RDONLY
);
448 if (!write
&& ((stat(filename
, &statbuf
) < 0) ||
449 ((statbuf
.st_mode
& S_IFMT
) != S_IFREG
))) {
450 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
453 fd
= open(filename
, mode
, UCI_FILEMODE
);
457 ret
= flock(fd
, (write
? LOCK_EX
: LOCK_SH
));
458 if ((ret
< 0) && (errno
!= ENOSYS
))
461 ret
= lseek(fd
, 0, pos
);
466 file
= fdopen(fd
, (write
? "w+" : "r"));
471 UCI_THROW(ctx
, UCI_ERR_IO
);
476 static void uci_close_stream(FILE *stream
)