aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-07-20 17:58:12 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-07-20 17:58:12 +0000
commiteb7512984a26baab2db1913c38a7107f745a70ab (patch)
tree6640c564d8d7865a5218f8224bf69b88c1c6958f
parent9b366f41367f717d8ddd9909fdc710b92f92a16c (diff)
downloadbusybox-w32-eb7512984a26baab2db1913c38a7107f745a70ab.tar.gz
busybox-w32-eb7512984a26baab2db1913c38a7107f745a70ab.tar.bz2
busybox-w32-eb7512984a26baab2db1913c38a7107f745a70ab.zip
udhcp: use libbb for config file parsing (by Vladimir)
function old new delta read_config 313 230 -83
-rw-r--r--networking/udhcp/files.c49
1 files changed, 16 insertions, 33 deletions
diff --git a/networking/udhcp/files.c b/networking/udhcp/files.c
index eaa50b807..fe6bff4ba 100644
--- a/networking/udhcp/files.c
+++ b/networking/udhcp/files.c
@@ -307,53 +307,36 @@ static const struct config_keyword keywords[] = {
307}; 307};
308enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 }; 308enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 };
309 309
310
311/*
312 * Domain names may have 254 chars, and string options can be 254
313 * chars long. However, 80 bytes will be enough for most, and won't
314 * hog up memory. If you have a special application, change it
315 */
316#define READ_CONFIG_BUF_SIZE 80
317
318void read_config(const char *file) 310void read_config(const char *file)
319{ 311{
320 FILE *in; 312 parser_t *parser;
321 char buffer[READ_CONFIG_BUF_SIZE], *token, *line; 313 const struct config_keyword *k;
322 unsigned i, lineno; 314 unsigned i;
315 char *token[2];
323 316
324 for (i = 0; i < KWS_WITH_DEFAULTS; i++) 317 for (i = 0; i < KWS_WITH_DEFAULTS; i++)
325 keywords[i].handler(keywords[i].def, keywords[i].var); 318 keywords[i].handler(keywords[i].def, keywords[i].var);
326 319
327 in = fopen_or_warn(file, "r"); 320 parser = config_open(file);
328 if (!in) 321 if (!parser)
329 return; 322 return;
330 323
331 lineno = 0; 324 while (config_read(parser, token, 2, 0, "# \t", PARSE_LAST_IS_GREEDY)) {
332 while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) { 325 if (!token[1])
333 lineno++; 326 continue;
334 /* *strchrnul(buffer, '\n') = '\0'; - trim() will do it */ 327 for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) {
335 *strchrnul(buffer, '#') = '\0'; 328 if (!strcasecmp(token[0], k->keyword)) {
336 329 if (!k->handler(token[1], k->var)) {
337 token = strtok(buffer, " \t"); 330 bb_error_msg("can't parse line %u in %s",
338 if (!token) continue; 331 parser->lineno, file);
339 line = strtok(NULL, "");
340 if (!line) continue;
341
342 trim(line); /* remove leading/trailing whitespace */
343
344 for (i = 0; i < ARRAY_SIZE(keywords); i++) {
345 if (!strcasecmp(token, keywords[i].keyword)) {
346 if (!keywords[i].handler(line, keywords[i].var)) {
347 bb_error_msg("can't parse line %u in %s at '%s'",
348 lineno, file, line);
349 /* reset back to the default value */ 332 /* reset back to the default value */
350 keywords[i].handler(keywords[i].def, keywords[i].var); 333 k->handler(k->def, k->var);
351 } 334 }
352 break; 335 break;
353 } 336 }
354 } 337 }
355 } 338 }
356 fclose(in); 339 config_close(parser);
357 340
358 server_config.start_ip = ntohl(server_config.start_ip); 341 server_config.start_ip = ntohl(server_config.start_ip);
359 server_config.end_ip = ntohl(server_config.end_ip); 342 server_config.end_ip = ntohl(server_config.end_ip);