2008-07-16 02:39:30 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* config file parser helper
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2009-02-23 21:51:53 +05:30
|
|
|
* Also for use in uClibc (http://uclibc.org/) licensed under LGPLv2.1 or later.
|
2008-07-16 02:39:30 +05:30
|
|
|
*/
|
|
|
|
|
2011-06-18 12:53:09 +05:30
|
|
|
/* Uncomment to enable test applet */
|
|
|
|
////config:config PARSE
|
|
|
|
////config: bool "Uniform config file parser debugging applet: parse"
|
|
|
|
////config: default n
|
|
|
|
////config: help
|
|
|
|
////config: Typical usage of parse API:
|
|
|
|
////config: char *t[3];
|
|
|
|
////config: parser_t *p = config_open(filename);
|
|
|
|
////config: while (config_read(p, t, 3, 0, delimiters, flags)) { // 1..3 tokens
|
|
|
|
////config: bb_error_msg("TOKENS: '%s''%s''%s'", t[0], t[1], t[2]);
|
|
|
|
////config: }
|
|
|
|
////config: config_close(p);
|
|
|
|
|
|
|
|
////applet:IF_PARSE(APPLET(parse, BB_DIR_USR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-y += parse_config.o
|
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define parse_trivial_usage
|
2011-06-18 12:53:09 +05:30
|
|
|
//usage: "[-x] [-n MAXTOKENS] [-m MINTOKENS] [-d DELIMS] [-f FLAGS] FILE..."
|
|
|
|
//usage:#define parse_full_usage "\n\n"
|
|
|
|
//usage: " -x Suppress output (for benchmarking)"
|
2011-04-11 06:59:49 +05:30
|
|
|
|
2008-07-16 02:39:30 +05:30
|
|
|
#include "libbb.h"
|
|
|
|
|
2008-10-24 16:19:49 +05:30
|
|
|
#if defined ENABLE_PARSE && ENABLE_PARSE
|
2008-07-19 14:57:19 +05:30
|
|
|
int parse_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int parse_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
|
|
|
const char *delims = "# \t";
|
2011-06-18 12:53:09 +05:30
|
|
|
char **t;
|
2008-07-27 04:38:31 +05:30
|
|
|
unsigned flags = PARSE_NORMAL;
|
2008-07-19 14:57:19 +05:30
|
|
|
int mintokens = 0, ntokens = 128;
|
2011-06-18 12:53:09 +05:30
|
|
|
unsigned noout;
|
2008-07-27 04:38:31 +05:30
|
|
|
|
2008-07-19 14:57:19 +05:30
|
|
|
opt_complementary = "-1:n+:m+:f+";
|
2011-06-18 12:53:09 +05:30
|
|
|
noout = 1 & getopt32(argv, "xn:m:d:f:", &ntokens, &mintokens, &delims, &flags);
|
2008-07-19 14:57:19 +05:30
|
|
|
//argc -= optind;
|
|
|
|
argv += optind;
|
2011-06-18 12:53:09 +05:30
|
|
|
|
|
|
|
t = xmalloc(sizeof(t[0]) * ntokens);
|
2008-07-19 14:57:19 +05:30
|
|
|
while (*argv) {
|
2011-06-18 12:53:09 +05:30
|
|
|
int n;
|
2008-07-19 14:57:19 +05:30
|
|
|
parser_t *p = config_open(*argv);
|
2011-06-18 12:53:09 +05:30
|
|
|
while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) != 0) {
|
|
|
|
if (!noout) {
|
2008-07-19 14:57:19 +05:30
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
printf("[%s]", t[i]);
|
|
|
|
puts("");
|
|
|
|
}
|
|
|
|
}
|
2011-06-18 12:53:09 +05:30
|
|
|
config_close(p);
|
2008-07-19 14:57:19 +05:30
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-07-22 04:35:26 +05:30
|
|
|
parser_t* FAST_FUNC config_open2(const char *filename, FILE* FAST_FUNC (*fopen_func)(const char *path))
|
2008-07-16 02:39:30 +05:30
|
|
|
{
|
2008-07-27 04:38:31 +05:30
|
|
|
FILE* fp;
|
|
|
|
parser_t *parser;
|
|
|
|
|
|
|
|
fp = fopen_func(filename);
|
|
|
|
if (!fp)
|
|
|
|
return NULL;
|
|
|
|
parser = xzalloc(sizeof(*parser));
|
|
|
|
parser->fp = fp;
|
|
|
|
return parser;
|
2008-07-16 02:39:30 +05:30
|
|
|
}
|
|
|
|
|
2008-07-22 04:35:26 +05:30
|
|
|
parser_t* FAST_FUNC config_open(const char *filename)
|
|
|
|
{
|
|
|
|
return config_open2(filename, fopen_or_warn_stdin);
|
|
|
|
}
|
|
|
|
|
2008-07-17 17:29:13 +05:30
|
|
|
void FAST_FUNC config_close(parser_t *parser)
|
|
|
|
{
|
2008-07-27 04:38:31 +05:30
|
|
|
if (parser) {
|
2011-06-20 13:19:56 +05:30
|
|
|
if (PARSE_KEEP_COPY) /* compile-time constant */
|
|
|
|
free(parser->data);
|
2008-07-27 04:38:31 +05:30
|
|
|
fclose(parser->fp);
|
2011-06-20 13:19:56 +05:30
|
|
|
free(parser->line);
|
|
|
|
free(parser->nline);
|
2008-07-27 04:38:31 +05:30
|
|
|
free(parser);
|
|
|
|
}
|
2008-07-16 02:39:30 +05:30
|
|
|
}
|
|
|
|
|
2011-06-20 13:19:56 +05:30
|
|
|
/* This function reads an entire line from a text file,
|
|
|
|
* up to a newline, exclusive.
|
2011-06-17 07:07:43 +05:30
|
|
|
* Trailing '\' is recognized as line continuation.
|
2011-06-20 13:19:56 +05:30
|
|
|
* Returns -1 if EOF/error.
|
2011-06-17 07:07:43 +05:30
|
|
|
*/
|
2011-06-20 13:19:56 +05:30
|
|
|
static int get_line_with_continuation(parser_t *parser)
|
2011-06-17 07:07:43 +05:30
|
|
|
{
|
2011-06-20 13:19:56 +05:30
|
|
|
ssize_t len, nlen;
|
|
|
|
char *line;
|
|
|
|
|
|
|
|
len = getline(&parser->line, &parser->line_alloc, parser->fp);
|
|
|
|
if (len <= 0)
|
|
|
|
return len;
|
|
|
|
|
|
|
|
line = parser->line;
|
|
|
|
for (;;) {
|
|
|
|
parser->lineno++;
|
|
|
|
if (line[len - 1] == '\n')
|
|
|
|
len--;
|
|
|
|
if (len == 0 || line[len - 1] != '\\')
|
|
|
|
break;
|
|
|
|
len--;
|
|
|
|
|
|
|
|
nlen = getline(&parser->nline, &parser->nline_alloc, parser->fp);
|
|
|
|
if (nlen <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (parser->line_alloc < len + nlen + 1) {
|
|
|
|
parser->line_alloc = len + nlen + 1;
|
|
|
|
line = parser->line = xrealloc(line, parser->line_alloc);
|
2011-06-17 07:07:43 +05:30
|
|
|
}
|
2011-06-20 13:19:56 +05:30
|
|
|
memcpy(&line[len], parser->nline, nlen);
|
|
|
|
len += nlen;
|
2011-06-17 07:07:43 +05:30
|
|
|
}
|
2011-06-20 13:19:56 +05:30
|
|
|
|
|
|
|
line[len] = '\0';
|
|
|
|
return len;
|
2011-06-17 07:07:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-19 14:57:19 +05:30
|
|
|
/*
|
2008-07-27 04:38:31 +05:30
|
|
|
0. If parser is NULL return 0.
|
|
|
|
1. Read a line from config file. If nothing to read then return 0.
|
|
|
|
Handle continuation character. Advance lineno for each physical line.
|
2009-09-06 06:28:59 +05:30
|
|
|
Discard everything past comment character.
|
2008-07-27 04:38:31 +05:30
|
|
|
2. if PARSE_TRIM is set (default), remove leading and trailing delimiters.
|
2008-07-19 14:57:19 +05:30
|
|
|
3. If resulting line is empty goto 1.
|
2008-07-27 04:38:31 +05:30
|
|
|
4. Look for first delimiter. If !PARSE_COLLAPSE or !PARSE_TRIM is set then
|
|
|
|
remember the token as empty.
|
|
|
|
5. Else (default) if number of seen tokens is equal to max number of tokens
|
|
|
|
(token is the last one) and PARSE_GREEDY is set then the remainder
|
|
|
|
of the line is the last token.
|
|
|
|
Else (token is not last or PARSE_GREEDY is not set) just replace
|
|
|
|
first delimiter with '\0' thus delimiting the token.
|
|
|
|
6. Advance line pointer past the end of token. If number of seen tokens
|
|
|
|
is less than required number of tokens then goto 4.
|
|
|
|
7. Check the number of seen tokens is not less the min number of tokens.
|
|
|
|
Complain or die otherwise depending on PARSE_MIN_DIE.
|
2008-07-19 14:57:19 +05:30
|
|
|
8. Return the number of seen tokens.
|
|
|
|
|
2008-07-27 04:38:31 +05:30
|
|
|
mintokens > 0 make config_read() print error message if less than mintokens
|
2008-07-19 14:57:19 +05:30
|
|
|
(but more than 0) are found. Empty lines are always skipped (not warned about).
|
|
|
|
*/
|
|
|
|
#undef config_read
|
|
|
|
int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const char *delims)
|
2008-07-16 02:39:30 +05:30
|
|
|
{
|
2008-08-09 22:46:40 +05:30
|
|
|
char *line;
|
|
|
|
int ntokens, mintokens;
|
2011-06-17 07:07:43 +05:30
|
|
|
int t;
|
|
|
|
|
|
|
|
if (!parser)
|
|
|
|
return 0;
|
2008-07-27 04:38:31 +05:30
|
|
|
|
2010-06-26 07:30:52 +05:30
|
|
|
ntokens = (uint8_t)flags;
|
|
|
|
mintokens = (uint8_t)(flags >> 8);
|
2008-07-19 14:57:19 +05:30
|
|
|
|
2011-06-20 13:19:56 +05:30
|
|
|
again:
|
2008-08-09 22:46:40 +05:30
|
|
|
memset(tokens, 0, sizeof(tokens[0]) * ntokens);
|
2008-07-16 02:39:30 +05:30
|
|
|
|
2008-08-09 22:46:40 +05:30
|
|
|
/* Read one line (handling continuations with backslash) */
|
2011-06-20 13:19:56 +05:30
|
|
|
if (get_line_with_continuation(parser) < 0)
|
2008-08-09 22:46:40 +05:30
|
|
|
return 0;
|
2011-06-20 13:19:56 +05:30
|
|
|
|
|
|
|
line = parser->line;
|
2008-07-16 02:39:30 +05:30
|
|
|
|
2008-08-09 22:46:40 +05:30
|
|
|
/* Skip token in the start of line? */
|
|
|
|
if (flags & PARSE_TRIM)
|
|
|
|
line += strspn(line, delims + 1);
|
2008-07-16 02:39:30 +05:30
|
|
|
|
2008-08-09 22:46:40 +05:30
|
|
|
if (line[0] == '\0' || line[0] == delims[0])
|
|
|
|
goto again;
|
|
|
|
|
2011-06-20 13:19:56 +05:30
|
|
|
if (flags & PARSE_KEEP_COPY) {
|
|
|
|
free(parser->data);
|
2008-07-19 14:57:19 +05:30
|
|
|
parser->data = xstrdup(line);
|
2011-06-20 13:19:56 +05:30
|
|
|
}
|
2008-07-16 02:39:30 +05:30
|
|
|
|
2008-08-09 22:46:40 +05:30
|
|
|
/* Tokenize the line */
|
2010-06-26 07:30:52 +05:30
|
|
|
t = 0;
|
|
|
|
do {
|
2008-08-09 22:46:40 +05:30
|
|
|
/* Pin token */
|
|
|
|
tokens[t] = line;
|
|
|
|
|
|
|
|
/* Combine remaining arguments? */
|
|
|
|
if ((t != (ntokens-1)) || !(flags & PARSE_GREEDY)) {
|
|
|
|
/* Vanilla token, find next delimiter */
|
|
|
|
line += strcspn(line, delims[0] ? delims : delims + 1);
|
2008-07-19 14:57:19 +05:30
|
|
|
} else {
|
2008-08-09 22:46:40 +05:30
|
|
|
/* Combining, find comment char if any */
|
|
|
|
line = strchrnul(line, delims[0]);
|
|
|
|
|
|
|
|
/* Trim any extra delimiters from the end */
|
|
|
|
if (flags & PARSE_TRIM) {
|
|
|
|
while (strchr(delims + 1, line[-1]) != NULL)
|
|
|
|
line--;
|
|
|
|
}
|
2008-07-19 14:57:19 +05:30
|
|
|
}
|
2008-08-09 22:46:40 +05:30
|
|
|
|
|
|
|
/* Token not terminated? */
|
2010-06-26 07:30:52 +05:30
|
|
|
if (*line == delims[0])
|
2008-08-09 22:46:40 +05:30
|
|
|
*line = '\0';
|
2010-06-26 07:30:52 +05:30
|
|
|
else if (*line != '\0')
|
|
|
|
*line++ = '\0';
|
2008-08-09 22:46:40 +05:30
|
|
|
|
|
|
|
#if 0 /* unused so far */
|
|
|
|
if (flags & PARSE_ESCAPE) {
|
2010-10-24 00:36:06 +05:30
|
|
|
strcpy_and_process_escape_sequences(tokens[t], tokens[t]);
|
2008-07-16 02:39:30 +05:30
|
|
|
}
|
2008-08-09 22:46:40 +05:30
|
|
|
#endif
|
|
|
|
/* Skip possible delimiters */
|
|
|
|
if (flags & PARSE_COLLAPSE)
|
|
|
|
line += strspn(line, delims + 1);
|
2010-06-26 07:30:52 +05:30
|
|
|
|
|
|
|
t++;
|
|
|
|
} while (*line && *line != delims[0] && t < ntokens);
|
2008-07-16 02:39:30 +05:30
|
|
|
|
2008-08-09 22:46:40 +05:30
|
|
|
if (t < mintokens) {
|
2008-07-22 04:35:26 +05:30
|
|
|
bb_error_msg("bad line %u: %d tokens found, %d needed",
|
2008-08-09 22:46:40 +05:30
|
|
|
parser->lineno, t, mintokens);
|
2008-07-22 04:35:26 +05:30
|
|
|
if (flags & PARSE_MIN_DIE)
|
|
|
|
xfunc_die();
|
|
|
|
goto again;
|
|
|
|
}
|
2008-07-16 02:39:30 +05:30
|
|
|
|
2008-08-09 22:46:40 +05:30
|
|
|
return t;
|
2008-07-16 02:39:30 +05:30
|
|
|
}
|