*: conversion to config parser
authorDenis Vlasenko <vda.linux@googlemail.com>
Thu, 24 Jul 2008 23:38:04 +0000 (23:38 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Thu, 24 Jul 2008 23:38:04 +0000 (23:38 -0000)
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
libbb/parse_config.c
miscutils/fbsplash.c
modutils/modprobe-small.c
networking/libiproute/rt_names.c

index c0b731b36e708b541765ecb61e6ac6036f84dab5..47fcdf08b9d9f738c326178c661047d5cd38966e 100644 (file)
@@ -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;
index d1b29218bb4ff1b70e8ea0796fdf287ca71fffa6..83dc997f6fffe7b5793677e4323ac67ddf307ea7 100644 (file)
@@ -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);
index 380f09beab3370568ccf00785875f6ded75d61a5..6357f78ea8ed2db4ab27957239f68c5f88bf1c84 100644 (file)
@@ -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);
 }
 
 
index f75dae8a3bc4c7190fe3ae35e60a95227939992c..1654cc52d7feda98105c81f2610bd005575dcbca 100644 (file)
@@ -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
index b22df9cb73725fdac6988f80a3defd888d41e508..1a2d52e86161007f04ac213e1acff5bc89aa9d73 100644 (file)
 #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] */