parse_config: make test applet easier to enable; fix its code

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2011-06-18 09:23:09 +02:00
parent a1a448347e
commit 3a649363aa
3 changed files with 29 additions and 35 deletions

View File

@ -681,9 +681,6 @@ config EFENCE
endchoice endchoice
### config PARSE
### bool "Uniform config file parser debugging applet: parse"
endmenu endmenu
menu 'Installation Options ("make install" behavior)' menu 'Installation Options ("make install" behavior)'

View File

@ -64,7 +64,6 @@ lib-y += hash_md5_sha.o
lib-y += messages.o lib-y += messages.o
lib-y += mode_string.o lib-y += mode_string.o
lib-y += parse_mode.o lib-y += parse_mode.o
lib-y += parse_config.o
lib-y += perror_msg.o lib-y += perror_msg.o
lib-y += perror_nomsg.o lib-y += perror_nomsg.o
lib-y += perror_nomsg_and_die.o lib-y += perror_nomsg_and_die.o

View File

@ -8,11 +8,27 @@
* Also for use in uClibc (http://uclibc.org/) licensed under LGPLv2.1 or later. * 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:#define parse_trivial_usage
//usage: "[-n MAXTOKENS] [-m MINTOKENS] [-d DELIMS] [-f FLAGS] FILE..." //usage: "[-x] [-n MAXTOKENS] [-m MINTOKENS] [-d DELIMS] [-f FLAGS] FILE..."
//usage:#define parse_full_usage "" //usage:#define parse_full_usage "\n\n"
*/ //usage: " -x Suppress output (for benchmarking)"
#include "libbb.h" #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) int parse_main(int argc UNUSED_PARAM, char **argv)
{ {
const char *delims = "# \t"; const char *delims = "# \t";
char **t;
unsigned flags = PARSE_NORMAL; unsigned flags = PARSE_NORMAL;
int mintokens = 0, ntokens = 128; int mintokens = 0, ntokens = 128;
unsigned noout;
opt_complementary = "-1:n+:m+:f+"; 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; //argc -= optind;
argv += optind; argv += optind;
t = xmalloc(sizeof(t[0]) * ntokens);
while (*argv) { while (*argv) {
int n;
parser_t *p = config_open(*argv); parser_t *p = config_open(*argv);
if (p) { while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) != 0) {
int n; if (!noout) {
char **t = xmalloc(sizeof(char *) * ntokens);
while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) != 0) {
for (int i = 0; i < n; ++i) for (int i = 0; i < n; ++i)
printf("[%s]", t[i]); printf("[%s]", t[i]);
puts(""); puts("");
} }
config_close(p);
} }
config_close(p);
argv++; argv++;
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
#endif #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)) parser_t* FAST_FUNC config_open2(const char *filename, FILE* FAST_FUNC (*fopen_func)(const char *path))
{ {
FILE* fp; FILE* fp;