From 50db1f29bf96c2ae4dbb96763793a9592d99cf02 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sat, 5 Aug 2017 18:20:34 +0200 Subject: [PATCH] sysctl: recognize ";comment" and "#comment" lines function old new delta config_read 639 699 +60 Signed-off-by: Denys Vlasenko --- include/libbb.h | 5 +++++ libbb/parse_config.c | 12 ++++++++++-- procps/sysctl.c | 7 ++++--- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/include/libbb.h b/include/libbb.h index 6077f64c9..51e8f27a5 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1403,6 +1403,11 @@ enum { // keep a copy of current line PARSE_KEEP_COPY = 0x00200000 * ENABLE_FEATURE_CROND_D, PARSE_EOL_COMMENTS = 0x00400000, // comments are recognized even if they aren't the first char + PARSE_ALT_COMMENTS = 0x00800000, // delim[0] and delim[1] are two different allowed comment chars + // (so far, delim[0] will only work as comment char for full-line comment) + // (IOW: it works as if PARSE_EOL_COMMENTS is not set. sysctl applet is okay with this) + PARSE_WS_COMMENTS = 0x01000000, // comments are recognized even if there is whitespace before + // ("line start>#comment" is also comment, not only "line start>#comment") // NORMAL is: // * remove leading and trailing delimiters and collapse // multiple delimiters into one diff --git a/libbb/parse_config.c b/libbb/parse_config.c index da7482c6d..eaf69d97f 100644 --- a/libbb/parse_config.c +++ b/libbb/parse_config.c @@ -161,13 +161,18 @@ mintokens > 0 make config_read() print error message if less than mintokens #undef config_read int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const char *delims) { - char *line; + char *line, *p; int ntokens, mintokens; int t; + char alt_comment_ch; if (!parser) return 0; + alt_comment_ch = '\0'; + if (flags & PARSE_ALT_COMMENTS) + alt_comment_ch = *delims++; + ntokens = (uint8_t)flags; mintokens = (uint8_t)(flags >> 8); @@ -184,7 +189,10 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const if (flags & PARSE_TRIM) line += strspn(line, delims + 1); - if (line[0] == '\0' || line[0] == delims[0]) + p = line; + if (flags & PARSE_WS_COMMENTS) + p = skip_whitespace(p); + if (p[0] == '\0' || p[0] == delims[0] || p[0] == alt_comment_ch) goto again; if (flags & PARSE_KEEP_COPY) { diff --git a/procps/sysctl.c b/procps/sysctl.c index ef1a1b99f..a42a91247 100644 --- a/procps/sysctl.c +++ b/procps/sysctl.c @@ -247,15 +247,16 @@ static int sysctl_handle_preload_file(const char *filename) /* Must do it _after_ config_open(): */ xchdir("/proc/sys"); -//TODO: ';' is comment char too -//TODO: #comment is also comment, not strictly 1st char only parse_flags = 0; parse_flags &= ~PARSE_COLLAPSE; // NO (var==val is not var=val) - treat consecutive delimiters as one parse_flags &= ~PARSE_TRIM; // NO - trim leading and trailing delimiters parse_flags |= PARSE_GREEDY; // YES - last token takes entire remainder of the line parse_flags &= ~PARSE_MIN_DIE; // NO - die if < min tokens found parse_flags &= ~PARSE_EOL_COMMENTS; // NO (only first char) - comments are recognized even if not first char - while (config_read(parser, token, 2, 2, "#=", parse_flags)) { + parse_flags |= PARSE_ALT_COMMENTS;// YES - two comment chars: ';' and '#' + /* #comment is also comment, not strictly 1st char only */ + parse_flags |= PARSE_WS_COMMENTS; // YES - comments are recognized even if there is whitespace before + while (config_read(parser, token, 2, 2, ";#=", parse_flags)) { char *tp; trim(token[1]);