From: Denis Vlasenko Date: Sun, 20 Jul 2008 17:58:12 +0000 (-0000) Subject: udhcp: use libbb for config file parsing (by Vladimir) X-Git-Tag: 1_12_0~138 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=eb7512984a26baab2db1913c38a7107f745a70ab;p=platform%2Fupstream%2Fbusybox.git udhcp: use libbb for config file parsing (by Vladimir) function old new delta read_config 313 230 -83 --- diff --git a/networking/udhcp/files.c b/networking/udhcp/files.c index eaa50b8..fe6bff4 100644 --- a/networking/udhcp/files.c +++ b/networking/udhcp/files.c @@ -307,53 +307,36 @@ static const struct config_keyword keywords[] = { }; enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 }; - -/* - * Domain names may have 254 chars, and string options can be 254 - * chars long. However, 80 bytes will be enough for most, and won't - * hog up memory. If you have a special application, change it - */ -#define READ_CONFIG_BUF_SIZE 80 - void read_config(const char *file) { - FILE *in; - char buffer[READ_CONFIG_BUF_SIZE], *token, *line; - unsigned i, lineno; + parser_t *parser; + const struct config_keyword *k; + unsigned i; + char *token[2]; for (i = 0; i < KWS_WITH_DEFAULTS; i++) keywords[i].handler(keywords[i].def, keywords[i].var); - in = fopen_or_warn(file, "r"); - if (!in) + parser = config_open(file); + if (!parser) return; - lineno = 0; - while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) { - lineno++; - /* *strchrnul(buffer, '\n') = '\0'; - trim() will do it */ - *strchrnul(buffer, '#') = '\0'; - - token = strtok(buffer, " \t"); - if (!token) continue; - line = strtok(NULL, ""); - if (!line) continue; - - trim(line); /* remove leading/trailing whitespace */ - - for (i = 0; i < ARRAY_SIZE(keywords); i++) { - if (!strcasecmp(token, keywords[i].keyword)) { - if (!keywords[i].handler(line, keywords[i].var)) { - bb_error_msg("can't parse line %u in %s at '%s'", - lineno, file, line); + while (config_read(parser, token, 2, 0, "# \t", PARSE_LAST_IS_GREEDY)) { + if (!token[1]) + continue; + for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) { + if (!strcasecmp(token[0], k->keyword)) { + if (!k->handler(token[1], k->var)) { + bb_error_msg("can't parse line %u in %s", + parser->lineno, file); /* reset back to the default value */ - keywords[i].handler(keywords[i].def, keywords[i].var); + k->handler(k->def, k->var); } break; } } } - fclose(in); + config_close(parser); server_config.start_ip = ntohl(server_config.start_ip); server_config.end_ip = ntohl(server_config.end_ip);