diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2011-06-18 09:23:09 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2011-06-18 09:23:09 +0200 |
commit | 3a649363aa34742b641125f51713493de4d3c7ef (patch) | |
tree | ae57a81ca3b0c6aac609afc6fcfde11975b7619c | |
parent | a1a448347e71c9899ad1500cbd8739fd82e1bb91 (diff) | |
download | busybox-w32-3a649363aa34742b641125f51713493de4d3c7ef.tar.gz busybox-w32-3a649363aa34742b641125f51713493de4d3c7ef.tar.bz2 busybox-w32-3a649363aa34742b641125f51713493de4d3c7ef.zip |
parse_config: make test applet easier to enable; fix its code
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | Config.in | 3 | ||||
-rw-r--r-- | libbb/Kbuild.src | 1 | ||||
-rw-r--r-- | libbb/parse_config.c | 60 |
3 files changed, 29 insertions, 35 deletions
@@ -681,9 +681,6 @@ config EFENCE | |||
681 | 681 | ||
682 | endchoice | 682 | endchoice |
683 | 683 | ||
684 | ### config PARSE | ||
685 | ### bool "Uniform config file parser debugging applet: parse" | ||
686 | |||
687 | endmenu | 684 | endmenu |
688 | 685 | ||
689 | menu 'Installation Options ("make install" behavior)' | 686 | 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 | |||
64 | lib-y += messages.o | 64 | lib-y += messages.o |
65 | lib-y += mode_string.o | 65 | lib-y += mode_string.o |
66 | lib-y += parse_mode.o | 66 | lib-y += parse_mode.o |
67 | lib-y += parse_config.o | ||
68 | lib-y += perror_msg.o | 67 | lib-y += perror_msg.o |
69 | lib-y += perror_nomsg.o | 68 | lib-y += perror_nomsg.o |
70 | lib-y += perror_nomsg_and_die.o | 69 | 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 @@ | |||
8 | * Also for use in uClibc (http://uclibc.org/) licensed under LGPLv2.1 or later. | 8 | * Also for use in uClibc (http://uclibc.org/) licensed under LGPLv2.1 or later. |
9 | */ | 9 | */ |
10 | 10 | ||
11 | /* | 11 | /* Uncomment to enable test applet */ |
12 | ////config:config PARSE | ||
13 | ////config: bool "Uniform config file parser debugging applet: parse" | ||
14 | ////config: default n | ||
15 | ////config: help | ||
16 | ////config: Typical usage of parse API: | ||
17 | ////config: char *t[3]; | ||
18 | ////config: parser_t *p = config_open(filename); | ||
19 | ////config: while (config_read(p, t, 3, 0, delimiters, flags)) { // 1..3 tokens | ||
20 | ////config: bb_error_msg("TOKENS: '%s''%s''%s'", t[0], t[1], t[2]); | ||
21 | ////config: } | ||
22 | ////config: config_close(p); | ||
23 | |||
24 | ////applet:IF_PARSE(APPLET(parse, BB_DIR_USR_BIN, BB_SUID_DROP)) | ||
25 | |||
26 | //kbuild:lib-y += parse_config.o | ||
27 | |||
12 | //usage:#define parse_trivial_usage | 28 | //usage:#define parse_trivial_usage |
13 | //usage: "[-n MAXTOKENS] [-m MINTOKENS] [-d DELIMS] [-f FLAGS] FILE..." | 29 | //usage: "[-x] [-n MAXTOKENS] [-m MINTOKENS] [-d DELIMS] [-f FLAGS] FILE..." |
14 | //usage:#define parse_full_usage "" | 30 | //usage:#define parse_full_usage "\n\n" |
15 | */ | 31 | //usage: " -x Suppress output (for benchmarking)" |
16 | 32 | ||
17 | #include "libbb.h" | 33 | #include "libbb.h" |
18 | 34 | ||
@@ -21,52 +37,34 @@ int parse_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | |||
21 | int parse_main(int argc UNUSED_PARAM, char **argv) | 37 | int parse_main(int argc UNUSED_PARAM, char **argv) |
22 | { | 38 | { |
23 | const char *delims = "# \t"; | 39 | const char *delims = "# \t"; |
40 | char **t; | ||
24 | unsigned flags = PARSE_NORMAL; | 41 | unsigned flags = PARSE_NORMAL; |
25 | int mintokens = 0, ntokens = 128; | 42 | int mintokens = 0, ntokens = 128; |
43 | unsigned noout; | ||
26 | 44 | ||
27 | opt_complementary = "-1:n+:m+:f+"; | 45 | opt_complementary = "-1:n+:m+:f+"; |
28 | getopt32(argv, "n:m:d:f:", &ntokens, &mintokens, &delims, &flags); | 46 | noout = 1 & getopt32(argv, "xn:m:d:f:", &ntokens, &mintokens, &delims, &flags); |
29 | //argc -= optind; | 47 | //argc -= optind; |
30 | argv += optind; | 48 | argv += optind; |
49 | |||
50 | t = xmalloc(sizeof(t[0]) * ntokens); | ||
31 | while (*argv) { | 51 | while (*argv) { |
52 | int n; | ||
32 | parser_t *p = config_open(*argv); | 53 | parser_t *p = config_open(*argv); |
33 | if (p) { | 54 | while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) != 0) { |
34 | int n; | 55 | if (!noout) { |
35 | char **t = xmalloc(sizeof(char *) * ntokens); | ||
36 | while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) != 0) { | ||
37 | for (int i = 0; i < n; ++i) | 56 | for (int i = 0; i < n; ++i) |
38 | printf("[%s]", t[i]); | 57 | printf("[%s]", t[i]); |
39 | puts(""); | 58 | puts(""); |
40 | } | 59 | } |
41 | config_close(p); | ||
42 | } | 60 | } |
61 | config_close(p); | ||
43 | argv++; | 62 | argv++; |
44 | } | 63 | } |
45 | return EXIT_SUCCESS; | 64 | return EXIT_SUCCESS; |
46 | } | 65 | } |
47 | #endif | 66 | #endif |
48 | 67 | ||
49 | /* | ||
50 | |||
51 | Typical usage: | ||
52 | |||
53 | ----- CUT ----- | ||
54 | char *t[3]; // tokens placeholder | ||
55 | parser_t *p = config_open(filename); | ||
56 | if (p) { | ||
57 | // parse line-by-line | ||
58 | while (config_read(p, t, 3, 0, delimiters, flags)) { // 1..3 tokens | ||
59 | // use tokens | ||
60 | bb_error_msg("TOKENS: [%s][%s][%s]", t[0], t[1], t[2]); | ||
61 | } | ||
62 | ... | ||
63 | // free parser | ||
64 | config_close(p); | ||
65 | } | ||
66 | ----- CUT ----- | ||
67 | |||
68 | */ | ||
69 | |||
70 | parser_t* FAST_FUNC config_open2(const char *filename, FILE* FAST_FUNC (*fopen_func)(const char *path)) | 68 | parser_t* FAST_FUNC config_open2(const char *filename, FILE* FAST_FUNC (*fopen_func)(const char *path)) |
71 | { | 69 | { |
72 | FILE* fp; | 70 | FILE* fp; |