From 3a649363aa34742b641125f51713493de4d3c7ef Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sat, 18 Jun 2011 09:23:09 +0200 Subject: [PATCH] parse_config: make test applet easier to enable; fix its code Signed-off-by: Denys Vlasenko --- Config.in | 3 --- libbb/Kbuild.src | 1 - libbb/parse_config.c | 60 +++++++++++++++++++++----------------------- 3 files changed, 29 insertions(+), 35 deletions(-) diff --git a/Config.in b/Config.in index 8f4d64274..1e7181261 100644 --- a/Config.in +++ b/Config.in @@ -681,9 +681,6 @@ config EFENCE endchoice -### config PARSE -### bool "Uniform config file parser debugging applet: parse" - endmenu menu 'Installation Options ("make install" behavior)' diff --git a/libbb/Kbuild.src b/libbb/Kbuild.src index 62bee9394..a6e80e692 100644 --- a/libbb/Kbuild.src +++ b/libbb/Kbuild.src @@ -64,7 +64,6 @@ lib-y += hash_md5_sha.o lib-y += messages.o lib-y += mode_string.o lib-y += parse_mode.o -lib-y += parse_config.o lib-y += perror_msg.o lib-y += perror_nomsg.o lib-y += perror_nomsg_and_die.o diff --git a/libbb/parse_config.c b/libbb/parse_config.c index 769ae5103..c0c34f312 100644 --- a/libbb/parse_config.c +++ b/libbb/parse_config.c @@ -8,11 +8,27 @@ * Also for use in uClibc (http://uclibc.org/) licensed under LGPLv2.1 or later. */ -/* +/* 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 + //usage:#define parse_trivial_usage -//usage: "[-n MAXTOKENS] [-m MINTOKENS] [-d DELIMS] [-f FLAGS] FILE..." -//usage:#define parse_full_usage "" -*/ +//usage: "[-x] [-n MAXTOKENS] [-m MINTOKENS] [-d DELIMS] [-f FLAGS] FILE..." +//usage:#define parse_full_usage "\n\n" +//usage: " -x Suppress output (for benchmarking)" #include "libbb.h" @@ -21,52 +37,34 @@ int parse_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int parse_main(int argc UNUSED_PARAM, char **argv) { const char *delims = "# \t"; + char **t; unsigned flags = PARSE_NORMAL; int mintokens = 0, ntokens = 128; + unsigned noout; opt_complementary = "-1:n+:m+:f+"; - getopt32(argv, "n:m:d:f:", &ntokens, &mintokens, &delims, &flags); + noout = 1 & getopt32(argv, "xn:m:d:f:", &ntokens, &mintokens, &delims, &flags); //argc -= optind; argv += optind; + + t = xmalloc(sizeof(t[0]) * ntokens); while (*argv) { + int n; parser_t *p = config_open(*argv); - if (p) { - int n; - char **t = xmalloc(sizeof(char *) * ntokens); - while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) != 0) { + while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) != 0) { + if (!noout) { for (int i = 0; i < n; ++i) printf("[%s]", t[i]); puts(""); } - config_close(p); } + config_close(p); argv++; } return EXIT_SUCCESS; } #endif -/* - -Typical usage: - ------ CUT ----- - char *t[3]; // tokens placeholder - parser_t *p = config_open(filename); - if (p) { - // parse line-by-line - while (config_read(p, t, 3, 0, delimiters, flags)) { // 1..3 tokens - // use tokens - bb_error_msg("TOKENS: [%s][%s][%s]", t[0], t[1], t[2]); - } - ... - // free parser - config_close(p); - } ------ CUT ----- - -*/ - parser_t* FAST_FUNC config_open2(const char *filename, FILE* FAST_FUNC (*fopen_func)(const char *path)) { FILE* fp; -- 2.25.1