diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-07-24 23:38:04 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-07-24 23:38:04 +0000 |
commit | 0f99d49ae680e675809428deace3c4fe839d323c (patch) | |
tree | 712afab828ee4fb02493bc60e1a37f1142231263 | |
parent | 22f741484391c3b2fd94881fd41c8c0df9749e95 (diff) | |
download | busybox-w32-0f99d49ae680e675809428deace3c4fe839d323c.tar.gz busybox-w32-0f99d49ae680e675809428deace3c4fe839d323c.tar.bz2 busybox-w32-0f99d49ae680e675809428deace3c4fe839d323c.zip |
*: 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
-rw-r--r-- | include/libbb.h | 1 | ||||
-rw-r--r-- | libbb/parse_config.c | 16 | ||||
-rw-r--r-- | miscutils/fbsplash.c | 86 | ||||
-rw-r--r-- | modutils/modprobe-small.c | 16 | ||||
-rw-r--r-- | networking/libiproute/rt_names.c | 44 |
5 files changed, 57 insertions, 106 deletions
diff --git a/include/libbb.h b/include/libbb.h index c0b731b36..47fcdf08b 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -1006,6 +1006,7 @@ enum { | |||
1006 | PARSE_MIN_DIE = 0x00100000, // die if less tokens found | 1006 | PARSE_MIN_DIE = 0x00100000, // die if less tokens found |
1007 | // keep a copy of current line | 1007 | // keep a copy of current line |
1008 | PARSE_KEEP_COPY = 0x00200000 * ENABLE_DEBUG_CROND_OPTION, | 1008 | PARSE_KEEP_COPY = 0x00200000 * ENABLE_DEBUG_CROND_OPTION, |
1009 | PARSE_ESCAPE = 0x00400000, // process escape sequences in tokens | ||
1009 | }; | 1010 | }; |
1010 | typedef struct parser_t { | 1011 | typedef struct parser_t { |
1011 | FILE *fp; | 1012 | FILE *fp; |
diff --git a/libbb/parse_config.c b/libbb/parse_config.c index d1b29218b..83dc997f6 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 | |||
66 | parser->fp = fopen_func(filename); | 66 | parser->fp = fopen_func(filename); |
67 | if (parser->fp) | 67 | if (parser->fp) |
68 | return parser; | 68 | return parser; |
69 | if (ENABLE_FEATURE_CLEAN_UP) | 69 | free(parser); |
70 | free(parser); | ||
71 | return NULL; | 70 | return NULL; |
72 | } | 71 | } |
73 | 72 | ||
@@ -212,6 +211,19 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const | |||
212 | if ((flags & (PARSE_DONT_REDUCE|PARSE_DONT_TRIM)) || *line) { | 211 | if ((flags & (PARSE_DONT_REDUCE|PARSE_DONT_TRIM)) || *line) { |
213 | //bb_info_msg("N[%d] T[%s]", ii, line); | 212 | //bb_info_msg("N[%d] T[%s]", ii, line); |
214 | tokens[ii++] = line; | 213 | tokens[ii++] = line; |
214 | // process escapes in token | ||
215 | if (flags & PARSE_ESCAPE) { | ||
216 | char *s = line; | ||
217 | while (*s) { | ||
218 | if (*s == '\\') { | ||
219 | s++; | ||
220 | *line++ = bb_process_escape_sequence((const char **)&s); | ||
221 | } else { | ||
222 | *line++ = *s++; | ||
223 | } | ||
224 | } | ||
225 | *line = '\0'; | ||
226 | } | ||
215 | } | 227 | } |
216 | line = q; | 228 | line = q; |
217 | //bb_info_msg("A[%s]", line); | 229 | //bb_info_msg("A[%s]", line); |
diff --git a/miscutils/fbsplash.c b/miscutils/fbsplash.c index 380f09bea..6357f78ea 100644 --- a/miscutils/fbsplash.c +++ b/miscutils/fbsplash.c | |||
@@ -38,13 +38,7 @@ struct globals { | |||
38 | FILE *logfile_fd; // log file | 38 | FILE *logfile_fd; // log file |
39 | #endif | 39 | #endif |
40 | unsigned char *addr; // pointer to framebuffer memory | 40 | unsigned char *addr; // pointer to framebuffer memory |
41 | unsigned nbar_width; // progress bar width | 41 | unsigned ns[7]; // n-parameters |
42 | unsigned nbar_height; // progress bar height | ||
43 | unsigned nbar_posx; // progress bar horizontal position | ||
44 | unsigned nbar_posy; // progress bar vertical position | ||
45 | unsigned char nbar_colr; // progress bar color red component | ||
46 | unsigned char nbar_colg; // progress bar color green component | ||
47 | unsigned char nbar_colb; // progress bar color blue component | ||
48 | const char *image_filename; | 42 | const char *image_filename; |
49 | struct fb_var_screeninfo scr_var; | 43 | struct fb_var_screeninfo scr_var; |
50 | struct fb_fix_screeninfo scr_fix; | 44 | struct fb_fix_screeninfo scr_fix; |
@@ -54,6 +48,13 @@ struct globals { | |||
54 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ | 48 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
55 | } while (0) | 49 | } while (0) |
56 | 50 | ||
51 | #define nbar_width ns[0] // progress bar width | ||
52 | #define nbar_height ns[1] // progress bar height | ||
53 | #define nbar_posx ns[2] // progress bar horizontal position | ||
54 | #define nbar_posy ns[3] // progress bar vertical position | ||
55 | #define nbar_colr ns[4] // progress bar color red component | ||
56 | #define nbar_colg ns[5] // progress bar color green component | ||
57 | #define nbar_colb ns[6] // progress bar color blue component | ||
57 | 58 | ||
58 | #if DEBUG | 59 | #if DEBUG |
59 | #define DEBUG_MESSAGE(strMessage, args...) \ | 60 | #define DEBUG_MESSAGE(strMessage, args...) \ |
@@ -280,77 +281,32 @@ static void fb_drawimage(void) | |||
280 | static void init(const char *cfg_filename) | 281 | static void init(const char *cfg_filename) |
281 | { | 282 | { |
282 | static const char const param_names[] ALIGN1 = | 283 | static const char const param_names[] ALIGN1 = |
283 | "BAR_LEFT\0" "BAR_TOP\0" | ||
284 | "BAR_WIDTH\0" "BAR_HEIGHT\0" | 284 | "BAR_WIDTH\0" "BAR_HEIGHT\0" |
285 | "BAR_LEFT\0" "BAR_TOP\0" | ||
285 | "BAR_R\0" "BAR_G\0" "BAR_B\0" | 286 | "BAR_R\0" "BAR_G\0" "BAR_B\0" |
286 | #if DEBUG | 287 | #if DEBUG |
287 | "DEBUG\0" | 288 | "DEBUG\0" |
288 | #endif | 289 | #endif |
289 | ; | 290 | ; |
290 | 291 | ||
291 | FILE *inifile; | 292 | char *token[2]; |
292 | char *buf; | 293 | parser_t *parser = config_open2(cfg_filename, xfopen_stdin); |
293 | 294 | while (config_read(parser, token, 2, 2, "#=", PARSE_MIN_DIE)) { | |
294 | inifile = xfopen_stdin(cfg_filename); | 295 | unsigned val = xatoi_u(token[1]); |
295 | 296 | int i = index_in_strings(param_names, token[0]); | |
296 | while ((buf = xmalloc_fgetline(inifile)) != NULL) { | 297 | if (i < 0) |
297 | char *value_str; | 298 | bb_error_msg_and_die("syntax error: '%s'", token[0]); |
298 | int val; | 299 | if (i >= 0 && i < 7) |
299 | 300 | G.ns[i] = val; | |
300 | if (*buf == '#') { // it's a comment | ||
301 | free(buf); | ||
302 | continue; | ||
303 | } | ||
304 | |||
305 | value_str = strchr(buf, '='); | ||
306 | if (!value_str) | ||
307 | goto err; | ||
308 | *value_str++ = '\0'; | ||
309 | val = xatoi_u(value_str); | ||
310 | |||
311 | switch (index_in_strings(param_names, buf)) { | ||
312 | case 0: | ||
313 | // progress bar horizontal position | ||
314 | G.nbar_posx = val; | ||
315 | break; | ||
316 | case 1: | ||
317 | // progress bar vertical position | ||
318 | G.nbar_posy = val; | ||
319 | break; | ||
320 | case 2: | ||
321 | // progress bar width | ||
322 | G.nbar_width = val; | ||
323 | break; | ||
324 | case 3: | ||
325 | // progress bar height | ||
326 | G.nbar_height = val; | ||
327 | break; | ||
328 | case 4: | ||
329 | // progress bar color - red component | ||
330 | G.nbar_colr = val; | ||
331 | break; | ||
332 | case 5: | ||
333 | // progress bar color - green component | ||
334 | G.nbar_colg = val; | ||
335 | break; | ||
336 | case 6: | ||
337 | // progress bar color - blue component | ||
338 | G.nbar_colb = val; | ||
339 | break; | ||
340 | #if DEBUG | 301 | #if DEBUG |
341 | case 7: | 302 | if (i == 7) { |
342 | G.bdebug_messages = val; | 303 | G.bdebug_messages = val; |
343 | if (G.bdebug_messages) | 304 | if (G.bdebug_messages) |
344 | G.logfile_fd = xfopen_for_write("/tmp/fbsplash.log"); | 305 | G.logfile_fd = xfopen_for_write("/tmp/fbsplash.log"); |
345 | break; | ||
346 | #endif | ||
347 | err: | ||
348 | default: | ||
349 | bb_error_msg_and_die("syntax error: '%s'", buf); | ||
350 | } | 306 | } |
351 | free(buf); | 307 | #endif |
352 | } | 308 | } |
353 | fclose(inifile); | 309 | config_close(parser); |
354 | } | 310 | } |
355 | 311 | ||
356 | 312 | ||
diff --git a/modutils/modprobe-small.c b/modutils/modprobe-small.c index f75dae8a3..1654cc52d 100644 --- a/modutils/modprobe-small.c +++ b/modutils/modprobe-small.c | |||
@@ -485,23 +485,19 @@ static module_info* find_alias(const char *alias) | |||
485 | } | 485 | } |
486 | 486 | ||
487 | #if ENABLE_FEATURE_MODPROBE_SMALL_CHECK_ALREADY_LOADED | 487 | #if ENABLE_FEATURE_MODPROBE_SMALL_CHECK_ALREADY_LOADED |
488 | // TODO: open only once, invent config_rewind() | ||
488 | static int already_loaded(const char *name) | 489 | static int already_loaded(const char *name) |
489 | { | 490 | { |
490 | int ret = 0; | 491 | int ret = 0; |
491 | int len = strlen(name); | 492 | char *s; |
492 | char *line; | 493 | parser_t *parser = config_open2("/proc/modules", xfopen_for_read); |
493 | FILE* modules; | 494 | while (config_read(parser, &s, 1, 1, "# \t", 0)) { |
494 | 495 | if (strcmp(s, name) == 0) { | |
495 | modules = xfopen_for_read("/proc/modules"); | ||
496 | while ((line = xmalloc_fgets(modules)) != NULL) { | ||
497 | if (strncmp(line, name, len) == 0 && line[len] == ' ') { | ||
498 | free(line); | ||
499 | ret = 1; | 496 | ret = 1; |
500 | break; | 497 | break; |
501 | } | 498 | } |
502 | free(line); | ||
503 | } | 499 | } |
504 | fclose(modules); | 500 | config_close(parser); |
505 | return ret; | 501 | return ret; |
506 | } | 502 | } |
507 | #else | 503 | #else |
diff --git a/networking/libiproute/rt_names.c b/networking/libiproute/rt_names.c index b22df9cb7..1a2d52e86 100644 --- a/networking/libiproute/rt_names.c +++ b/networking/libiproute/rt_names.c | |||
@@ -13,41 +13,27 @@ | |||
13 | #include "libbb.h" | 13 | #include "libbb.h" |
14 | #include "rt_names.h" | 14 | #include "rt_names.h" |
15 | 15 | ||
16 | /* so far all callers have size == 256 */ | ||
17 | #define rtnl_tab_initialize(file, tab, size) rtnl_tab_initialize(file, tab) | ||
18 | #define size 256 | ||
16 | static void rtnl_tab_initialize(const char *file, const char **tab, int size) | 19 | static void rtnl_tab_initialize(const char *file, const char **tab, int size) |
17 | { | 20 | { |
18 | char buf[512]; | 21 | char *token[2]; |
19 | FILE *fp; | 22 | parser_t *parser = config_open2(file, fopen_for_read); |
20 | 23 | if (!parser) | |
21 | fp = fopen_for_read(file); | ||
22 | if (!fp) | ||
23 | return; | 24 | return; |
24 | while (fgets(buf, sizeof(buf), fp)) { | 25 | while (config_read(parser, token, 2, 2, "# \t", 0)) { |
25 | char *p = buf; | 26 | int id = bb_strtou(token[0], NULL, 0); |
26 | int id; | 27 | if (id < 0 || id > size) { |
27 | char namebuf[512]; | 28 | bb_error_msg("database %s is corrupted at line %d", |
28 | 29 | file, parser->lineno); | |
29 | while (*p == ' ' || *p == '\t') | 30 | break; |
30 | p++; | ||
31 | if (*p == '#' || *p == '\n' || *p == 0) | ||
32 | continue; | ||
33 | if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 | ||
34 | && sscanf(p, "0x%x %s #", &id, namebuf) != 2 | ||
35 | && sscanf(p, "%d %s\n", &id, namebuf) != 2 | ||
36 | && sscanf(p, "%d %s #", &id, namebuf) != 2 | ||
37 | ) { | ||
38 | bb_error_msg("database %s is corrupted at %s", | ||
39 | file, p); | ||
40 | return; | ||
41 | } | 31 | } |
42 | 32 | tab[id] = xstrdup(token[1]); | |
43 | if (id < 0 || id > size) | ||
44 | continue; | ||
45 | |||
46 | tab[id] = xstrdup(namebuf); | ||
47 | } | 33 | } |
48 | fclose(fp); | 34 | config_close(parser); |
49 | } | 35 | } |
50 | 36 | #undef size | |
51 | 37 | ||
52 | static const char **rtnl_rtprot_tab; /* [256] */ | 38 | static const char **rtnl_rtprot_tab; /* [256] */ |
53 | 39 | ||