udhcp: use libbb for config file parsing (by Vladimir)
function old new delta read_config 313 230 -83
This commit is contained in:
parent
9b366f4136
commit
eb7512984a
@ -307,53 +307,36 @@ static const struct config_keyword keywords[] = {
|
|||||||
};
|
};
|
||||||
enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 };
|
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)
|
void read_config(const char *file)
|
||||||
{
|
{
|
||||||
FILE *in;
|
parser_t *parser;
|
||||||
char buffer[READ_CONFIG_BUF_SIZE], *token, *line;
|
const struct config_keyword *k;
|
||||||
unsigned i, lineno;
|
unsigned i;
|
||||||
|
char *token[2];
|
||||||
|
|
||||||
for (i = 0; i < KWS_WITH_DEFAULTS; i++)
|
for (i = 0; i < KWS_WITH_DEFAULTS; i++)
|
||||||
keywords[i].handler(keywords[i].def, keywords[i].var);
|
keywords[i].handler(keywords[i].def, keywords[i].var);
|
||||||
|
|
||||||
in = fopen_or_warn(file, "r");
|
parser = config_open(file);
|
||||||
if (!in)
|
if (!parser)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
lineno = 0;
|
while (config_read(parser, token, 2, 0, "# \t", PARSE_LAST_IS_GREEDY)) {
|
||||||
while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
|
if (!token[1])
|
||||||
lineno++;
|
continue;
|
||||||
/* *strchrnul(buffer, '\n') = '\0'; - trim() will do it */
|
for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) {
|
||||||
*strchrnul(buffer, '#') = '\0';
|
if (!strcasecmp(token[0], k->keyword)) {
|
||||||
|
if (!k->handler(token[1], k->var)) {
|
||||||
token = strtok(buffer, " \t");
|
bb_error_msg("can't parse line %u in %s",
|
||||||
if (!token) continue;
|
parser->lineno, file);
|
||||||
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);
|
|
||||||
/* reset back to the default value */
|
/* reset back to the default value */
|
||||||
keywords[i].handler(keywords[i].def, keywords[i].var);
|
k->handler(k->def, k->var);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose(in);
|
config_close(parser);
|
||||||
|
|
||||||
server_config.start_ip = ntohl(server_config.start_ip);
|
server_config.start_ip = ntohl(server_config.start_ip);
|
||||||
server_config.end_ip = ntohl(server_config.end_ip);
|
server_config.end_ip = ntohl(server_config.end_ip);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user