From 0f99d49ae680e675809428deace3c4fe839d323c Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Thu, 24 Jul 2008 23:38:04 +0000 Subject: *: conversion to config parser function old new delta config_read 540 597 +57 config_open2 41 44 +3 rtnl_rtprot_initialize 70 66 -4 rtnl_rttable_initialize 78 73 -5 rtnl_rtscope_initialize 88 83 -5 rtnl_rtrealm_initialize 48 43 -5 rtnl_rtdsfield_initialize 48 43 -5 process_module 566 560 -6 bbunpack 391 383 -8 rtnl_tab_initialize 279 121 -158 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 2/8 up/down: 60/-196) Total: -136 bytes --- include/libbb.h | 1 + libbb/parse_config.c | 16 +++++++- miscutils/fbsplash.c | 86 ++++++++++------------------------------ modutils/modprobe-small.c | 16 +++----- networking/libiproute/rt_names.c | 44 +++++++------------- 5 files changed, 57 insertions(+), 106 deletions(-) diff --git a/include/libbb.h b/include/libbb.h index c0b731b..47fcdf0 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1006,6 +1006,7 @@ enum { PARSE_MIN_DIE = 0x00100000, // die if less tokens found // keep a copy of current line PARSE_KEEP_COPY = 0x00200000 * ENABLE_DEBUG_CROND_OPTION, + PARSE_ESCAPE = 0x00400000, // process escape sequences in tokens }; typedef struct parser_t { FILE *fp; diff --git a/libbb/parse_config.c b/libbb/parse_config.c index d1b2921..83dc997 100644 --- a/libbb/parse_config.c +++ b/libbb/parse_config.c @@ -66,8 +66,7 @@ parser_t* FAST_FUNC config_open2(const char *filename, FILE* FAST_FUNC (*fopen_f parser->fp = fopen_func(filename); if (parser->fp) return parser; - if (ENABLE_FEATURE_CLEAN_UP) - free(parser); + free(parser); return NULL; } @@ -212,6 +211,19 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const if ((flags & (PARSE_DONT_REDUCE|PARSE_DONT_TRIM)) || *line) { //bb_info_msg("N[%d] T[%s]", ii, line); tokens[ii++] = line; + // process escapes in token + if (flags & PARSE_ESCAPE) { + char *s = line; + while (*s) { + if (*s == '\\') { + s++; + *line++ = bb_process_escape_sequence((const char **)&s); + } else { + *line++ = *s++; + } + } + *line = '\0'; + } } line = q; //bb_info_msg("A[%s]", line); diff --git a/miscutils/fbsplash.c b/miscutils/fbsplash.c index 380f09b..6357f78 100644 --- a/miscutils/fbsplash.c +++ b/miscutils/fbsplash.c @@ -38,13 +38,7 @@ struct globals { FILE *logfile_fd; // log file #endif unsigned char *addr; // pointer to framebuffer memory - unsigned nbar_width; // progress bar width - unsigned nbar_height; // progress bar height - unsigned nbar_posx; // progress bar horizontal position - unsigned nbar_posy; // progress bar vertical position - unsigned char nbar_colr; // progress bar color red component - unsigned char nbar_colg; // progress bar color green component - unsigned char nbar_colb; // progress bar color blue component + unsigned ns[7]; // n-parameters const char *image_filename; struct fb_var_screeninfo scr_var; struct fb_fix_screeninfo scr_fix; @@ -54,6 +48,13 @@ struct globals { SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ } while (0) +#define nbar_width ns[0] // progress bar width +#define nbar_height ns[1] // progress bar height +#define nbar_posx ns[2] // progress bar horizontal position +#define nbar_posy ns[3] // progress bar vertical position +#define nbar_colr ns[4] // progress bar color red component +#define nbar_colg ns[5] // progress bar color green component +#define nbar_colb ns[6] // progress bar color blue component #if DEBUG #define DEBUG_MESSAGE(strMessage, args...) \ @@ -280,77 +281,32 @@ static void fb_drawimage(void) static void init(const char *cfg_filename) { static const char const param_names[] ALIGN1 = - "BAR_LEFT\0" "BAR_TOP\0" "BAR_WIDTH\0" "BAR_HEIGHT\0" + "BAR_LEFT\0" "BAR_TOP\0" "BAR_R\0" "BAR_G\0" "BAR_B\0" #if DEBUG "DEBUG\0" #endif ; - FILE *inifile; - char *buf; - - inifile = xfopen_stdin(cfg_filename); - - while ((buf = xmalloc_fgetline(inifile)) != NULL) { - char *value_str; - int val; - - if (*buf == '#') { // it's a comment - free(buf); - continue; - } - - value_str = strchr(buf, '='); - if (!value_str) - goto err; - *value_str++ = '\0'; - val = xatoi_u(value_str); - - switch (index_in_strings(param_names, buf)) { - case 0: - // progress bar horizontal position - G.nbar_posx = val; - break; - case 1: - // progress bar vertical position - G.nbar_posy = val; - break; - case 2: - // progress bar width - G.nbar_width = val; - break; - case 3: - // progress bar height - G.nbar_height = val; - break; - case 4: - // progress bar color - red component - G.nbar_colr = val; - break; - case 5: - // progress bar color - green component - G.nbar_colg = val; - break; - case 6: - // progress bar color - blue component - G.nbar_colb = val; - break; + char *token[2]; + parser_t *parser = config_open2(cfg_filename, xfopen_stdin); + while (config_read(parser, token, 2, 2, "#=", PARSE_MIN_DIE)) { + unsigned val = xatoi_u(token[1]); + int i = index_in_strings(param_names, token[0]); + if (i < 0) + bb_error_msg_and_die("syntax error: '%s'", token[0]); + if (i >= 0 && i < 7) + G.ns[i] = val; #if DEBUG - case 7: + if (i == 7) { G.bdebug_messages = val; if (G.bdebug_messages) G.logfile_fd = xfopen_for_write("/tmp/fbsplash.log"); - break; -#endif - err: - default: - bb_error_msg_and_die("syntax error: '%s'", buf); } - free(buf); +#endif } - fclose(inifile); + config_close(parser); } diff --git a/modutils/modprobe-small.c b/modutils/modprobe-small.c index f75dae8..1654cc5 100644 --- a/modutils/modprobe-small.c +++ b/modutils/modprobe-small.c @@ -485,23 +485,19 @@ static module_info* find_alias(const char *alias) } #if ENABLE_FEATURE_MODPROBE_SMALL_CHECK_ALREADY_LOADED +// TODO: open only once, invent config_rewind() static int already_loaded(const char *name) { int ret = 0; - int len = strlen(name); - char *line; - FILE* modules; - - modules = xfopen_for_read("/proc/modules"); - while ((line = xmalloc_fgets(modules)) != NULL) { - if (strncmp(line, name, len) == 0 && line[len] == ' ') { - free(line); + char *s; + parser_t *parser = config_open2("/proc/modules", xfopen_for_read); + while (config_read(parser, &s, 1, 1, "# \t", 0)) { + if (strcmp(s, name) == 0) { ret = 1; break; } - free(line); } - fclose(modules); + config_close(parser); return ret; } #else diff --git a/networking/libiproute/rt_names.c b/networking/libiproute/rt_names.c index b22df9c..1a2d52e 100644 --- a/networking/libiproute/rt_names.c +++ b/networking/libiproute/rt_names.c @@ -13,41 +13,27 @@ #include "libbb.h" #include "rt_names.h" +/* so far all callers have size == 256 */ +#define rtnl_tab_initialize(file, tab, size) rtnl_tab_initialize(file, tab) +#define size 256 static void rtnl_tab_initialize(const char *file, const char **tab, int size) { - char buf[512]; - FILE *fp; - - fp = fopen_for_read(file); - if (!fp) + char *token[2]; + parser_t *parser = config_open2(file, fopen_for_read); + if (!parser) return; - while (fgets(buf, sizeof(buf), fp)) { - char *p = buf; - int id; - char namebuf[512]; - - while (*p == ' ' || *p == '\t') - p++; - if (*p == '#' || *p == '\n' || *p == 0) - continue; - if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 - && sscanf(p, "0x%x %s #", &id, namebuf) != 2 - && sscanf(p, "%d %s\n", &id, namebuf) != 2 - && sscanf(p, "%d %s #", &id, namebuf) != 2 - ) { - bb_error_msg("database %s is corrupted at %s", - file, p); - return; + while (config_read(parser, token, 2, 2, "# \t", 0)) { + int id = bb_strtou(token[0], NULL, 0); + if (id < 0 || id > size) { + bb_error_msg("database %s is corrupted at line %d", + file, parser->lineno); + break; } - - if (id < 0 || id > size) - continue; - - tab[id] = xstrdup(namebuf); + tab[id] = xstrdup(token[1]); } - fclose(fp); + config_close(parser); } - +#undef size static const char **rtnl_rtprot_tab; /* [256] */ -- cgit v1.1