diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-07-16 22:12:18 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-07-16 22:12:18 +0000 |
commit | c01340fe26b76e172805ff641ad9af6bc45cdc91 (patch) | |
tree | 1b3b57ef2fd7441bb91410b89f763056279baddf /libbb/parse_config.c | |
parent | 38e54f1c1384e76adafb4e611a6bd456e4351d42 (diff) | |
download | busybox-w32-c01340fe26b76e172805ff641ad9af6bc45cdc91.tar.gz busybox-w32-c01340fe26b76e172805ff641ad9af6bc45cdc91.tar.bz2 busybox-w32-c01340fe26b76e172805ff641ad9af6bc45cdc91.zip |
update of config file parser from Vladimir
Diffstat (limited to 'libbb/parse_config.c')
-rw-r--r-- | libbb/parse_config.c | 106 |
1 files changed, 7 insertions, 99 deletions
diff --git a/libbb/parse_config.c b/libbb/parse_config.c index 6612db367..e63204b09 100644 --- a/libbb/parse_config.c +++ b/libbb/parse_config.c | |||
@@ -31,101 +31,6 @@ Typical usage: | |||
31 | 31 | ||
32 | */ | 32 | */ |
33 | 33 | ||
34 | #if !PARSER_STDIO_BASED | ||
35 | |||
36 | char* FAST_FUNC config_open(parser_t *parser, const char *filename) | ||
37 | { | ||
38 | // empty file configures nothing! | ||
39 | char *data = xmalloc_open_read_close(filename, NULL); | ||
40 | if (!data) | ||
41 | return data; | ||
42 | |||
43 | // convert 0x5c 0x0a (backslashes at the very end of line) to 0x20 0x20 (spaces) | ||
44 | for (char *s = data; (s = strchr(s, '\\')) != NULL; ++s) | ||
45 | if ('\n' == s[1]) { | ||
46 | s[0] = s[1] = ' '; | ||
47 | } | ||
48 | |||
49 | // init parser | ||
50 | parser->line = parser->data = data; | ||
51 | parser->lineno = 0; | ||
52 | |||
53 | return data; | ||
54 | } | ||
55 | |||
56 | void FAST_FUNC config_close(parser_t *parser) | ||
57 | { | ||
58 | // for now just free config data | ||
59 | free(parser->data); | ||
60 | } | ||
61 | |||
62 | char* FAST_FUNC config_read(parser_t *parser, char **tokens, int ntokens, int mintokens, const char *delims, char comment) | ||
63 | { | ||
64 | char *ret, *line; | ||
65 | int noreduce = (ntokens<0); // do not treat subsequent delimiters as one delimiter | ||
66 | if (ntokens < 0) | ||
67 | ntokens = -ntokens; | ||
68 | ret = line = parser->line; | ||
69 | // nullify tokens | ||
70 | memset(tokens, 0, sizeof(void *) * ntokens); | ||
71 | // now split to lines | ||
72 | while (*line) { | ||
73 | int token_num = 0; | ||
74 | // limit the line | ||
75 | char *ptr = strchrnul(line, '\n'); | ||
76 | *ptr++ = '\0'; | ||
77 | // line number | ||
78 | parser->lineno++; | ||
79 | // comments mean EOLs | ||
80 | if (comment) | ||
81 | *strchrnul(line, comment) = '\0'; | ||
82 | // skip leading delimiters | ||
83 | while (*line && strchr(delims, *line)) | ||
84 | line++; | ||
85 | // skip empty lines | ||
86 | if (*line) { | ||
87 | char *s; | ||
88 | // now split line to tokens | ||
89 | s = line; | ||
90 | while (s) { | ||
91 | char *p; | ||
92 | // get next token | ||
93 | if (token_num+1 >= ntokens) | ||
94 | break; | ||
95 | p = s; | ||
96 | while (*p && !strchr(delims, *p)) | ||
97 | p++; | ||
98 | if (!*p) | ||
99 | break; | ||
100 | *p++ = '\0'; | ||
101 | // pin token | ||
102 | if (noreduce || *s) { | ||
103 | tokens[token_num++] = s; | ||
104 | //bb_error_msg("L[%d] T[%s]", token_num, s); | ||
105 | } | ||
106 | s = p; | ||
107 | } | ||
108 | // non-empty remainder is also a token. So if ntokens == 0, we just return the whole line | ||
109 | if (s && (noreduce || *s)) | ||
110 | tokens[token_num++] = s; | ||
111 | // sanity check: have we got all required tokens? | ||
112 | if (token_num < mintokens) | ||
113 | bb_error_msg_and_die("bad line %u, %d tokens found, %d needed", parser->lineno, token_num, mintokens); | ||
114 | // advance data for the next call | ||
115 | line = ptr; | ||
116 | break; | ||
117 | } | ||
118 | // line didn't contain any token -> try next line | ||
119 | ret = line = ptr; | ||
120 | } | ||
121 | parser->line = line; | ||
122 | |||
123 | // return current line. caller must check *ret to determine whether to continue | ||
124 | return ret; | ||
125 | } | ||
126 | |||
127 | #else // stdio-based | ||
128 | |||
129 | FILE* FAST_FUNC config_open(parser_t *parser, const char *filename) | 34 | FILE* FAST_FUNC config_open(parser_t *parser, const char *filename) |
130 | { | 35 | { |
131 | // empty file configures nothing! | 36 | // empty file configures nothing! |
@@ -142,10 +47,12 @@ FILE* FAST_FUNC config_open(parser_t *parser, const char *filename) | |||
142 | 47 | ||
143 | void FAST_FUNC config_close(parser_t *parser) | 48 | void FAST_FUNC config_close(parser_t *parser) |
144 | { | 49 | { |
50 | free(parser->line); | ||
51 | free(parser->data); | ||
145 | fclose(parser->fp); | 52 | fclose(parser->fp); |
146 | } | 53 | } |
147 | 54 | ||
148 | char* FAST_FUNC config_read(parser_t *parser, char **tokens, int ntokens, int mintokens, const char *delims, char comment) | 55 | int FAST_FUNC config_read(parser_t *parser, char **tokens, int ntokens, int mintokens, const char *delims, char comment) |
149 | { | 56 | { |
150 | char *line, *q; | 57 | char *line, *q; |
151 | int token_num, len; | 58 | int token_num, len; |
@@ -160,6 +67,8 @@ char* FAST_FUNC config_read(parser_t *parser, char **tokens, int ntokens, int mi | |||
160 | // free used line | 67 | // free used line |
161 | free(parser->line); | 68 | free(parser->line); |
162 | parser->line = NULL; | 69 | parser->line = NULL; |
70 | free(parser->data); | ||
71 | parser->data = NULL; | ||
163 | 72 | ||
164 | while (1) { | 73 | while (1) { |
165 | int n; | 74 | int n; |
@@ -211,6 +120,7 @@ char* FAST_FUNC config_read(parser_t *parser, char **tokens, int ntokens, int mi | |||
211 | 120 | ||
212 | // store line | 121 | // store line |
213 | parser->line = line = xrealloc(line, len + 1); | 122 | parser->line = line = xrealloc(line, len + 1); |
123 | parser->data = xstrdup(line); | ||
214 | 124 | ||
215 | // now split line to tokens | 125 | // now split line to tokens |
216 | //TODO: discard consecutive delimiters? | 126 | //TODO: discard consecutive delimiters? |
@@ -241,7 +151,5 @@ char* FAST_FUNC config_read(parser_t *parser, char **tokens, int ntokens, int mi | |||
241 | bb_error_msg_and_die("bad line %u: %d tokens found, %d needed", | 151 | bb_error_msg_and_die("bad line %u: %d tokens found, %d needed", |
242 | parser->lineno, token_num, mintokens); | 152 | parser->lineno, token_num, mintokens); |
243 | 153 | ||
244 | return parser->line; // maybe token_num instead? | 154 | return token_num; |
245 | } | 155 | } |
246 | |||
247 | #endif | ||