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>
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
|
2008-07-19 14:57:19 +05:30
|
|
|
#if ENABLE_PARSE
|
|
|
|
int parse_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int parse_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
|
|
|
const char *delims = "# \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;
|
2008-07-27 04:38:31 +05:30
|
|
|
|
2008-07-19 14:57:19 +05:30
|
|
|
opt_complementary = "-1:n+:m+:f+";
|
|
|
|
getopt32(argv, "n:m:d:f:", &ntokens, &mintokens, &delims, &flags);
|
|
|
|
//argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
while (*argv) {
|
|
|
|
parser_t *p = config_open(*argv);
|
|
|
|
if (p) {
|
|
|
|
int n;
|
|
|
|
char **t = xmalloc(sizeof(char *) * ntokens);
|
2008-07-20 18:31:56 +05:30
|
|
|
while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) != 0) {
|
2008-07-19 14:57:19 +05:30
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
printf("[%s]", t[i]);
|
|
|
|
puts("");
|
|
|
|
}
|
|
|
|
config_close(p);
|
|
|
|
}
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-07-16 02:39:30 +05:30
|
|
|
/*
|
|
|
|
|
|
|
|
Typical usage:
|
|
|
|
|
|
|
|
----- CUT -----
|
|
|
|
char *t[3]; // tokens placeholder
|
2008-07-19 14:57:19 +05:30
|
|
|
parser_t *p = config_open(filename);
|
|
|
|
if (p) {
|
2008-07-16 02:39:30 +05:30
|
|
|
// parse line-by-line
|
2008-07-19 14:57:19 +05:30
|
|
|
while (config_read(p, t, 3, 0, delimiters, flags)) { // 1..3 tokens
|
2008-07-16 02:39:30 +05:30
|
|
|
// use tokens
|
|
|
|
bb_error_msg("TOKENS: [%s][%s][%s]", t[0], t[1], t[2]);
|
|
|
|
}
|
|
|
|
...
|
|
|
|
// free parser
|
2008-07-19 14:57:19 +05:30
|
|
|
config_close(p);
|
2008-07-16 02:39:30 +05:30
|
|
|
}
|
|
|
|
----- CUT -----
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
static void config_free_data(parser_t *const parser)
|
2008-07-16 02:39:30 +05:30
|
|
|
{
|
2008-07-17 03:42:18 +05:30
|
|
|
free(parser->line);
|
2008-07-19 14:57:19 +05:30
|
|
|
parser->line = NULL;
|
2008-07-20 04:27:00 +05:30
|
|
|
if (PARSE_KEEP_COPY) { /* compile-time constant */
|
2008-07-19 14:57:19 +05:30
|
|
|
free(parser->data);
|
|
|
|
parser->data = NULL;
|
2008-07-20 04:27:00 +05:30
|
|
|
}
|
2008-07-17 17:29:13 +05:30
|
|
|
}
|
2008-07-19 14:57:19 +05:30
|
|
|
|
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) {
|
|
|
|
config_free_data(parser);
|
|
|
|
fclose(parser->fp);
|
|
|
|
free(parser);
|
|
|
|
}
|
2008-07-16 02:39:30 +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.
|
|
|
|
Discard everything past comment characher.
|
|
|
|
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
|
|
|
{
|
|
|
|
char *line, *q;
|
2008-07-27 04:38:31 +05:30
|
|
|
char comment;
|
2008-07-19 14:57:19 +05:30
|
|
|
int ii;
|
2008-07-27 04:38:31 +05:30
|
|
|
int ntokens;
|
|
|
|
int mintokens;
|
|
|
|
|
|
|
|
comment = *delims++;
|
|
|
|
ntokens = flags & 0xFF;
|
|
|
|
mintokens = (flags & 0xFF00) >> 8;
|
2008-07-19 14:57:19 +05:30
|
|
|
|
2008-07-22 04:35:26 +05:30
|
|
|
again:
|
2008-07-27 04:38:31 +05:30
|
|
|
memset(tokens, 0, sizeof(tokens[0]) * ntokens);
|
|
|
|
if (!parser)
|
|
|
|
return 0;
|
2008-07-17 17:29:13 +05:30
|
|
|
config_free_data(parser);
|
2008-07-16 02:39:30 +05:30
|
|
|
|
|
|
|
while (1) {
|
|
|
|
//TODO: speed up xmalloc_fgetline by internally using fgets, not fgetc
|
|
|
|
line = xmalloc_fgetline(parser->fp);
|
|
|
|
if (!line)
|
2008-07-19 14:57:19 +05:30
|
|
|
return 0;
|
2008-07-16 02:39:30 +05:30
|
|
|
|
|
|
|
parser->lineno++;
|
|
|
|
// handle continuations. Tito's code stolen :)
|
|
|
|
while (1) {
|
2008-07-17 17:29:13 +05:30
|
|
|
ii = strlen(line);
|
|
|
|
if (!ii)
|
|
|
|
goto next_line;
|
|
|
|
if (line[ii - 1] != '\\')
|
2008-07-16 02:39:30 +05:30
|
|
|
break;
|
|
|
|
// multi-line object
|
2008-07-17 17:29:13 +05:30
|
|
|
line[--ii] = '\0';
|
2008-07-16 02:39:30 +05:30
|
|
|
//TODO: add xmalloc_fgetline-like iface but with appending to existing str
|
|
|
|
q = xmalloc_fgetline(parser->fp);
|
2008-07-27 04:38:31 +05:30
|
|
|
if (!q)
|
|
|
|
break;
|
|
|
|
parser->lineno++;
|
|
|
|
line = xasprintf("%s%s", line, q);
|
|
|
|
free(q);
|
2008-07-16 02:39:30 +05:30
|
|
|
}
|
2008-07-27 04:38:31 +05:30
|
|
|
// discard comments
|
2008-07-16 02:39:30 +05:30
|
|
|
if (comment) {
|
|
|
|
q = strchrnul(line, comment);
|
|
|
|
*q = '\0';
|
2008-07-17 17:29:13 +05:30
|
|
|
ii = q - line;
|
2008-07-16 02:39:30 +05:30
|
|
|
}
|
2008-07-19 14:57:19 +05:30
|
|
|
// skip leading and trailing delimiters
|
2008-07-27 04:38:31 +05:30
|
|
|
if (flags & PARSE_TRIM) {
|
2008-07-19 14:57:19 +05:30
|
|
|
// skip leading
|
|
|
|
int n = strspn(line, delims);
|
|
|
|
if (n) {
|
|
|
|
ii -= n;
|
2008-07-23 01:46:55 +05:30
|
|
|
overlapping_strcpy(line, line + n);
|
2008-07-19 14:57:19 +05:30
|
|
|
}
|
|
|
|
// cut trailing
|
|
|
|
if (ii) {
|
|
|
|
while (strchr(delims, line[--ii]))
|
|
|
|
continue;
|
|
|
|
line[++ii] = '\0';
|
|
|
|
}
|
2008-07-16 02:39:30 +05:30
|
|
|
}
|
2008-07-19 14:57:19 +05:30
|
|
|
// if something still remains -> return it
|
2008-07-17 17:29:13 +05:30
|
|
|
if (ii)
|
2008-07-16 02:39:30 +05:30
|
|
|
break;
|
2008-07-17 17:29:13 +05:30
|
|
|
|
|
|
|
next_line:
|
2008-07-20 18:31:56 +05:30
|
|
|
// skip empty line
|
2008-07-16 02:39:30 +05:30
|
|
|
free(line);
|
|
|
|
}
|
2008-07-19 14:57:19 +05:30
|
|
|
// non-empty line found, parse and return the number of tokens
|
2008-07-16 02:39:30 +05:30
|
|
|
|
|
|
|
// store line
|
2008-07-17 17:29:13 +05:30
|
|
|
parser->line = line = xrealloc(line, ii + 1);
|
2008-07-20 04:27:00 +05:30
|
|
|
if (flags & PARSE_KEEP_COPY) {
|
2008-07-19 14:57:19 +05:30
|
|
|
parser->data = xstrdup(line);
|
2008-07-20 04:27:00 +05:30
|
|
|
}
|
2008-07-16 02:39:30 +05:30
|
|
|
|
2008-07-20 18:31:56 +05:30
|
|
|
// split line to tokens
|
2008-07-16 02:39:30 +05:30
|
|
|
ntokens--; // now it's max allowed token no
|
2008-07-20 18:31:56 +05:30
|
|
|
// N.B. non-empty remainder is also a token,
|
2008-07-19 14:57:19 +05:30
|
|
|
// so if ntokens <= 1, we just return the whole line
|
2008-07-27 04:38:31 +05:30
|
|
|
// N.B. if PARSE_GREEDY is set the remainder of the line is stuck to the last token
|
|
|
|
ii = 0;
|
|
|
|
while (*line && ii <= ntokens) {
|
2008-07-19 14:57:19 +05:30
|
|
|
//bb_info_msg("L[%s]", line);
|
2008-07-16 02:39:30 +05:30
|
|
|
// get next token
|
2008-07-27 04:38:31 +05:30
|
|
|
// at last token and need greedy token ->
|
|
|
|
if ((flags & PARSE_GREEDY) && (ii == ntokens)) {
|
2008-07-20 18:31:56 +05:30
|
|
|
// skip possible delimiters
|
2008-07-27 04:38:31 +05:30
|
|
|
if (flags & PARSE_COLLAPSE)
|
2008-07-20 18:31:56 +05:30
|
|
|
line += strspn(line, delims);
|
|
|
|
// don't cut the line
|
2008-07-19 14:57:19 +05:30
|
|
|
q = line + strlen(line);
|
|
|
|
} else {
|
|
|
|
// vanilla token. cut the line at the first delim
|
|
|
|
q = line + strcspn(line, delims);
|
2008-07-20 23:20:58 +05:30
|
|
|
if (*q) // watch out: do not step past the line end!
|
|
|
|
*q++ = '\0';
|
2008-07-19 14:57:19 +05:30
|
|
|
}
|
2008-07-16 02:39:30 +05:30
|
|
|
// pin token
|
2008-07-27 04:38:31 +05:30
|
|
|
if (!(flags & (PARSE_COLLAPSE | PARSE_TRIM)) || *line) {
|
2008-07-19 14:57:19 +05:30
|
|
|
//bb_info_msg("N[%d] T[%s]", ii, line);
|
2008-07-17 17:29:13 +05:30
|
|
|
tokens[ii++] = line;
|
2008-07-25 05:08:04 +05:30
|
|
|
// process escapes in token
|
2008-07-27 04:38:31 +05:30
|
|
|
#if 0 // unused so far
|
2008-07-25 05:08:04 +05:30
|
|
|
if (flags & PARSE_ESCAPE) {
|
|
|
|
char *s = line;
|
|
|
|
while (*s) {
|
|
|
|
if (*s == '\\') {
|
|
|
|
s++;
|
|
|
|
*line++ = bb_process_escape_sequence((const char **)&s);
|
|
|
|
} else {
|
|
|
|
*line++ = *s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*line = '\0';
|
|
|
|
}
|
2008-07-27 04:38:31 +05:30
|
|
|
#endif
|
2008-07-16 02:39:30 +05:30
|
|
|
}
|
|
|
|
line = q;
|
2008-07-20 23:20:58 +05:30
|
|
|
//bb_info_msg("A[%s]", line);
|
2008-07-17 17:29:13 +05:30
|
|
|
}
|
2008-07-16 02:39:30 +05:30
|
|
|
|
2008-07-22 04:35:26 +05:30
|
|
|
if (ii < mintokens) {
|
|
|
|
bb_error_msg("bad line %u: %d tokens found, %d needed",
|
|
|
|
parser->lineno, ii, mintokens);
|
|
|
|
if (flags & PARSE_MIN_DIE)
|
|
|
|
xfunc_die();
|
2008-07-27 04:38:31 +05:30
|
|
|
ntokens++;
|
2008-07-22 04:35:26 +05:30
|
|
|
goto again;
|
|
|
|
}
|
2008-07-16 02:39:30 +05:30
|
|
|
|
2008-07-17 17:29:13 +05:30
|
|
|
return ii;
|
2008-07-16 02:39:30 +05:30
|
|
|
}
|