[git commit] parse_config: make test applet easier to enable; fix its code

Denys Vlasenko vda.linux at googlemail.com
Sat Jun 18 07:23:09 UTC 2011


commit: http://git.busybox.net/busybox/commit/?id=3a649363aa34742b641125f51713493de4d3c7ef
branch: http://git.busybox.net/busybox/commit/?id=refs/heads/master

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 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 8f4d642..1e71812 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 62bee93..a6e80e6 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 769ae51..c0c34f3 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;
-- 
1.7.3.4



More information about the busybox-cvs mailing list