libbb: fixes to config_read() by maintainer

sysctl: use config_read()

function                                             old     new   delta
sysctl_main                                          121     232    +111
config_read                                          478     502     +24
parse_main                                           239     241      +2
sysctl_preload_file_and_exit                         234       -    -234
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 3/0 up/down: 137/-234)          Total: -97 bytes
This commit is contained in:
Denis Vlasenko 2008-07-20 13:01:56 +00:00
parent dcb3fcb042
commit 4a717e0c19
2 changed files with 27 additions and 50 deletions

View File

@ -25,7 +25,7 @@ int parse_main(int argc UNUSED_PARAM, char **argv)
if (p) { if (p) {
int n; int n;
char **t = xmalloc(sizeof(char *) * ntokens); char **t = xmalloc(sizeof(char *) * ntokens);
while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) > 0) { while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) != 0) {
for (int i = 0; i < n; ++i) for (int i = 0; i < n; ++i)
printf("[%s]", t[i]); printf("[%s]", t[i]);
puts(""); puts("");
@ -114,10 +114,8 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const
int ntokens = flags & 0xFF; int ntokens = flags & 0xFF;
int mintokens = (flags & 0xFF00) >> 8; int mintokens = (flags & 0xFF00) >> 8;
/*
// N.B. this could only be used in read-in-one-go version, or when tokens use xstrdup(). TODO // N.B. this could only be used in read-in-one-go version, or when tokens use xstrdup(). TODO
if (!parser->lineno || !(flags & PARSE_DONT_NULL)) //if (!parser->lineno || !(flags & PARSE_DONT_NULL))
*/
memset(tokens, 0, sizeof(tokens[0]) * ntokens); memset(tokens, 0, sizeof(tokens[0]) * ntokens);
config_free_data(parser); config_free_data(parser);
@ -171,7 +169,7 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const
break; break;
next_line: next_line:
/* skip empty line */ // skip empty line
free(line); free(line);
} }
@ -183,9 +181,9 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const
parser->data = xstrdup(line); parser->data = xstrdup(line);
} }
/* now split line to tokens */ // split line to tokens
ntokens--; // now it's max allowed token no ntokens--; // now it's max allowed token no
// N.B, non-empty remainder is also a token, // N.B. non-empty remainder is also a token,
// so if ntokens <= 1, we just return the whole line // so if ntokens <= 1, we just return the whole line
// N.B. if PARSE_LAST_IS_GREEDY is set the remainder of the line is stuck to the last token // N.B. if PARSE_LAST_IS_GREEDY is set the remainder of the line is stuck to the last token
for (ii = 0; *line && ii <= ntokens; ) { for (ii = 0; *line && ii <= ntokens; ) {
@ -193,7 +191,10 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const
// get next token // get next token
// at the last token and need greedy token -> // at the last token and need greedy token ->
if ((flags & PARSE_LAST_IS_GREEDY) && (ii == ntokens)) { if ((flags & PARSE_LAST_IS_GREEDY) && (ii == ntokens)) {
// ... don't cut the line // skip possible delimiters
if (!(flags & PARSE_DONT_REDUCE))
line += strspn(line, delims);
// don't cut the line
q = line + strlen(line); q = line + strlen(line);
} else { } else {
// vanilla token. cut the line at the first delim // vanilla token. cut the line at the first delim

View File

@ -88,56 +88,32 @@ int sysctl_main(int argc UNUSED_PARAM, char **argv)
* preload the sysctl's from a conf file * preload the sysctl's from a conf file
* - we parse the file and then reform it (strip out whitespace) * - we parse the file and then reform it (strip out whitespace)
*/ */
#define PRELOAD_BUF 256
static int sysctl_preload_file_and_exit(const char *filename) static int sysctl_preload_file_and_exit(const char *filename)
{ {
int lineno; char *token[2];
char oneline[PRELOAD_BUF]; parser_t *parser;
char buffer[PRELOAD_BUF];
char *name, *value;
FILE *fp;
fp = xfopen(filename, "r"); parser = config_open(filename);
if (!parser)
return 1;
lineno = 0; while (config_read(parser, token, 2, 0, "# \t=", PARSE_LAST_IS_GREEDY)) { // TODO: ';' is comment char too
while (fgets(oneline, sizeof(oneline) - 1, fp)) { if (!token[1]) {
lineno++; bb_error_msg(WARN_BAD_LINE, filename, parser->lineno);
trim(oneline); } else {
if (oneline[0] == '#' || oneline[0] == ';') #if 0
continue; char *s = xasprintf("%s=%s", token[0], token[1]);
if (!oneline[0] || !oneline[1]) sysctl_write_setting(s);
continue; free(s);
#else // PLAY_WITH_FIRE for -4 bytes?
name = strtok(oneline, "="); sprintf(parser->line, "%s=%s", token[0], token[1]); // must have room by definition
if (!name) { sysctl_write_setting(parser->line);
bb_error_msg(WARN_BAD_LINE, filename, lineno); #endif
continue;
} }
trim(name);
if (!*name) {
bb_error_msg(WARN_BAD_LINE, filename, lineno);
continue;
}
value = strtok(NULL, "\n\r");
if (!value) {
bb_error_msg(WARN_BAD_LINE, filename, lineno);
continue;
}
while (*value == ' ' || *value == '\t')
value++;
if (!*value) {
bb_error_msg(WARN_BAD_LINE, filename, lineno);
continue;
}
/* safe because sizeof(oneline) == sizeof(buffer) */
sprintf(buffer, "%s=%s", name, value);
sysctl_write_setting(buffer);
} }
if (ENABLE_FEATURE_CLEAN_UP) if (ENABLE_FEATURE_CLEAN_UP)
fclose(fp); config_close(parser);
return 0; return 0;
} /* end sysctl_preload_file_and_exit() */ } /* end sysctl_preload_file_and_exit() */