udhcp: use libbb for config file parsing (by Vladimir)
function old new delta read_config 313 230 -83
This commit is contained in:
		@@ -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);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user