X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=libbb%2Fparse_config.c;h=74f0524e53d211907736e1d49d79381e963c2cb3;hb=1fd1ea4395e520694bd9f8b1dc9e60af6442946d;hp=83dc997f6fffe7b5793677e4323ac67ddf307ea7;hpb=0f99d49ae680e675809428deace3c4fe839d323c;p=oweals%2Fbusybox.git diff --git a/libbb/parse_config.c b/libbb/parse_config.c index 83dc997f6..74f0524e5 100644 --- a/libbb/parse_config.c +++ b/libbb/parse_config.c @@ -5,17 +5,19 @@ * Copyright (C) 2008 by Vladimir Dronnikov * * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. + * Also for use in uClibc (http://uclibc.org/) licensed under LGPLv2.1 or later. */ #include "libbb.h" -#if ENABLE_PARSE +#if defined ENABLE_PARSE && 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"; - unsigned flags = 0; + unsigned flags = PARSE_NORMAL; int mintokens = 0, ntokens = 128; + opt_complementary = "-1:n+:m+:f+"; getopt32(argv, "n:m:d:f:", &ntokens, &mintokens, &delims, &flags); //argc -= optind; @@ -61,13 +63,15 @@ Typical usage: parser_t* FAST_FUNC config_open2(const char *filename, FILE* FAST_FUNC (*fopen_func)(const char *path)) { - parser_t *parser = xzalloc(sizeof(parser_t)); - /* empty file configures nothing */ - parser->fp = fopen_func(filename); - if (parser->fp) - return parser; - free(parser); - return NULL; + FILE* fp; + parser_t *parser; + + fp = fopen_func(filename); + if (!fp) + return NULL; + parser = xzalloc(sizeof(*parser)); + parser->fp = fp; + return parser; } parser_t* FAST_FUNC config_open(const char *filename) @@ -87,155 +91,129 @@ static void config_free_data(parser_t *const parser) void FAST_FUNC config_close(parser_t *parser) { - config_free_data(parser); - fclose(parser->fp); + if (parser) { + config_free_data(parser); + fclose(parser->fp); + free(parser); + } } /* -1. Read a line from config file. If nothing to read then bail out returning 0. - Handle continuation character. Advance lineno for each physical line. Cut comments. -2. if PARSE_DONT_TRIM is not set (default) skip leading and cut trailing delimiters, if any. +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. 3. If resulting line is empty goto 1. -4. Look for first delimiter. If PARSE_DONT_REDUCE or PARSE_DONT_TRIM is set then pin empty token. -5. Else (default) if number of seen tokens is equal to max number of tokens (token is the last one) - and PARSE_LAST_IS_GREEDY is set then pin the remainder of the line as the last token. - Else (token is not last or PARSE_LAST_IS_GREEDY is not set) just replace first delimiter with '\0' - thus delimiting token and pin it. -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. Control the number of seen tokens is not less the min number of tokens. Die if condition is not met. +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. 8. Return the number of seen tokens. -mintokens > 0 make config_read() exit with error message if less than mintokens +mintokens > 0 make config_read() print error message if less than mintokens (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) { - char *line, *q; - char comment = *delims++; - int ii; - int ntokens = flags & 0xFF; - int mintokens = (flags & 0xFF00) >> 8; - - again: - // N.B. this could only be used in read-in-one-go version, or when tokens use xstrdup(). TODO - //if (!parser->lineno || !(flags & PARSE_DONT_NULL)) - memset(tokens, 0, sizeof(tokens[0]) * ntokens); + char *line; + int ntokens, mintokens; + int t, len; + + ntokens = flags & 0xFF; + mintokens = (flags & 0xFF00) >> 8; + + if (parser == NULL) + return 0; + +again: + memset(tokens, 0, sizeof(tokens[0]) * ntokens); config_free_data(parser); - while (1) { -//TODO: speed up xmalloc_fgetline by internally using fgets, not fgetc - line = xmalloc_fgetline(parser->fp); - if (!line) - return 0; - - parser->lineno++; - // handle continuations. Tito's code stolen :) - while (1) { - ii = strlen(line); - if (!ii) - goto next_line; - if (line[ii - 1] != '\\') - break; - // multi-line object - line[--ii] = '\0'; -//TODO: add xmalloc_fgetline-like iface but with appending to existing str - q = xmalloc_fgetline(parser->fp); - if (q) { - parser->lineno++; - line = xasprintf("%s%s", line, q); - free(q); - } - } - // comments mean EOLs - if (comment) { - q = strchrnul(line, comment); - *q = '\0'; - ii = q - line; - } - // skip leading and trailing delimiters - if (!(flags & PARSE_DONT_TRIM)) { - // skip leading - int n = strspn(line, delims); - if (n) { - ii -= n; - overlapping_strcpy(line, line + n); - } - // cut trailing - if (ii) { - while (strchr(delims, line[--ii])) - continue; - line[++ii] = '\0'; - } - } - // if something still remains -> return it - if (ii) - break; + /* Read one line (handling continuations with backslash) */ + line = bb_get_chunk_with_continuation(parser->fp, &len, &parser->lineno); + if (line == NULL) + return 0; + parser->line = line; - next_line: - // skip empty line - free(line); - } + /* Strip trailing line-feed if any */ + if (len && line[len-1] == '\n') + line[len-1] = '\0'; + + /* Skip token in the start of line? */ + if (flags & PARSE_TRIM) + line += strspn(line, delims + 1); - // non-empty line found, parse and return the number of tokens + if (line[0] == '\0' || line[0] == delims[0]) + goto again; - // store line - parser->line = line = xrealloc(line, ii + 1); - if (flags & PARSE_KEEP_COPY) { + if (flags & PARSE_KEEP_COPY) parser->data = xstrdup(line); - } - // split line to tokens - ntokens--; // now it's max allowed token no - // N.B. non-empty remainder is also a token, - // so if ntokens <= 1, we just return the whole line - // N.B. if PARSE_LAST_IS_GREEDY is set the remainder of the line is stuck to the last token - for (ii = 0; *line && ii <= ntokens; ) { - //bb_info_msg("L[%s]", line); - // get next token - // at the last token and need greedy token -> - if ((flags & PARSE_LAST_IS_GREEDY) && (ii == ntokens)) { - // skip possible delimiters - if (!(flags & PARSE_DONT_REDUCE)) - line += strspn(line, delims); - // don't cut the line - q = line + strlen(line); + /* Tokenize the line */ + for (t = 0; *line && *line != delims[0] && t < ntokens; t++) { + /* 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); } else { - // vanilla token. cut the line at the first delim - q = line + strcspn(line, delims); - if (*q) // watch out: do not step past the line end! - *q++ = '\0'; + /* 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--; + } } - // pin token - if ((flags & (PARSE_DONT_REDUCE|PARSE_DONT_TRIM)) || *line) { - //bb_info_msg("N[%d] T[%s]", ii, line); - tokens[ii++] = line; - // process escapes in token - if (flags & PARSE_ESCAPE) { - char *s = line; - while (*s) { - if (*s == '\\') { - s++; - *line++ = bb_process_escape_sequence((const char **)&s); - } else { - *line++ = *s++; - } + + /* Token not terminated? */ + if (line[0] == delims[0]) + *line = '\0'; + else if (line[0] != '\0') + *(line++) = '\0'; + +#if 0 /* unused so far */ + if (flags & PARSE_ESCAPE) { + const char *from; + char *to; + + from = to = tokens[t]; + while (*from) { + if (*from == '\\') { + from++; + *to++ = bb_process_escape_sequence(&from); + } else { + *to++ = *from++; } - *line = '\0'; } + *to = '\0'; } - line = q; - //bb_info_msg("A[%s]", line); +#endif + + /* Skip possible delimiters */ + if (flags & PARSE_COLLAPSE) + line += strspn(line, delims + 1); } - if (ii < mintokens) { + if (t < mintokens) { bb_error_msg("bad line %u: %d tokens found, %d needed", - parser->lineno, ii, mintokens); + parser->lineno, t, mintokens); if (flags & PARSE_MIN_DIE) xfunc_die(); goto again; } - return ii; + return t; }