diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-07-20 13:01:56 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-07-20 13:01:56 +0000 |
commit | 4a717e0c19098111d7fc41f260e01258556ddfbb (patch) | |
tree | d65386c0c0a0c2d80d712847221551ebecee33e3 | |
parent | dcb3fcb042612bfbb311a488379c65024bafd52b (diff) | |
download | busybox-w32-4a717e0c19098111d7fc41f260e01258556ddfbb.tar.gz busybox-w32-4a717e0c19098111d7fc41f260e01258556ddfbb.tar.bz2 busybox-w32-4a717e0c19098111d7fc41f260e01258556ddfbb.zip |
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
-rw-r--r-- | libbb/parse_config.c | 17 | ||||
-rw-r--r-- | procps/sysctl.c | 60 |
2 files changed, 27 insertions, 50 deletions
diff --git a/libbb/parse_config.c b/libbb/parse_config.c index 8d7c97eb1..68caa2c37 100644 --- a/libbb/parse_config.c +++ b/libbb/parse_config.c | |||
@@ -25,7 +25,7 @@ int parse_main(int argc UNUSED_PARAM, char **argv) | |||
25 | if (p) { | 25 | if (p) { |
26 | int n; | 26 | int n; |
27 | char **t = xmalloc(sizeof(char *) * ntokens); | 27 | char **t = xmalloc(sizeof(char *) * ntokens); |
28 | while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) > 0) { | 28 | while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) != 0) { |
29 | for (int i = 0; i < n; ++i) | 29 | for (int i = 0; i < n; ++i) |
30 | printf("[%s]", t[i]); | 30 | printf("[%s]", t[i]); |
31 | puts(""); | 31 | puts(""); |
@@ -114,10 +114,8 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const | |||
114 | int ntokens = flags & 0xFF; | 114 | int ntokens = flags & 0xFF; |
115 | int mintokens = (flags & 0xFF00) >> 8; | 115 | int mintokens = (flags & 0xFF00) >> 8; |
116 | 116 | ||
117 | /* | ||
118 | // N.B. this could only be used in read-in-one-go version, or when tokens use xstrdup(). TODO | 117 | // N.B. this could only be used in read-in-one-go version, or when tokens use xstrdup(). TODO |
119 | if (!parser->lineno || !(flags & PARSE_DONT_NULL)) | 118 | //if (!parser->lineno || !(flags & PARSE_DONT_NULL)) |
120 | */ | ||
121 | memset(tokens, 0, sizeof(tokens[0]) * ntokens); | 119 | memset(tokens, 0, sizeof(tokens[0]) * ntokens); |
122 | config_free_data(parser); | 120 | config_free_data(parser); |
123 | 121 | ||
@@ -171,7 +169,7 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const | |||
171 | break; | 169 | break; |
172 | 170 | ||
173 | next_line: | 171 | next_line: |
174 | /* skip empty line */ | 172 | // skip empty line |
175 | free(line); | 173 | free(line); |
176 | } | 174 | } |
177 | 175 | ||
@@ -183,9 +181,9 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const | |||
183 | parser->data = xstrdup(line); | 181 | parser->data = xstrdup(line); |
184 | } | 182 | } |
185 | 183 | ||
186 | /* now split line to tokens */ | 184 | // split line to tokens |
187 | ntokens--; // now it's max allowed token no | 185 | ntokens--; // now it's max allowed token no |
188 | // N.B, non-empty remainder is also a token, | 186 | // N.B. non-empty remainder is also a token, |
189 | // so if ntokens <= 1, we just return the whole line | 187 | // so if ntokens <= 1, we just return the whole line |
190 | // N.B. if PARSE_LAST_IS_GREEDY is set the remainder of the line is stuck to the last token | 188 | // N.B. if PARSE_LAST_IS_GREEDY is set the remainder of the line is stuck to the last token |
191 | for (ii = 0; *line && ii <= ntokens; ) { | 189 | for (ii = 0; *line && ii <= ntokens; ) { |
@@ -193,7 +191,10 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const | |||
193 | // get next token | 191 | // get next token |
194 | // at the last token and need greedy token -> | 192 | // at the last token and need greedy token -> |
195 | if ((flags & PARSE_LAST_IS_GREEDY) && (ii == ntokens)) { | 193 | if ((flags & PARSE_LAST_IS_GREEDY) && (ii == ntokens)) { |
196 | // ... don't cut the line | 194 | // skip possible delimiters |
195 | if (!(flags & PARSE_DONT_REDUCE)) | ||
196 | line += strspn(line, delims); | ||
197 | // don't cut the line | ||
197 | q = line + strlen(line); | 198 | q = line + strlen(line); |
198 | } else { | 199 | } else { |
199 | // vanilla token. cut the line at the first delim | 200 | // vanilla token. cut the line at the first delim |
diff --git a/procps/sysctl.c b/procps/sysctl.c index 3607a2364..6e582b0f9 100644 --- a/procps/sysctl.c +++ b/procps/sysctl.c | |||
@@ -88,56 +88,32 @@ int sysctl_main(int argc UNUSED_PARAM, char **argv) | |||
88 | * preload the sysctl's from a conf file | 88 | * preload the sysctl's from a conf file |
89 | * - we parse the file and then reform it (strip out whitespace) | 89 | * - we parse the file and then reform it (strip out whitespace) |
90 | */ | 90 | */ |
91 | #define PRELOAD_BUF 256 | ||
92 | 91 | ||
93 | static int sysctl_preload_file_and_exit(const char *filename) | 92 | static int sysctl_preload_file_and_exit(const char *filename) |
94 | { | 93 | { |
95 | int lineno; | 94 | char *token[2]; |
96 | char oneline[PRELOAD_BUF]; | 95 | parser_t *parser; |
97 | char buffer[PRELOAD_BUF]; | ||
98 | char *name, *value; | ||
99 | FILE *fp; | ||
100 | 96 | ||
101 | fp = xfopen(filename, "r"); | 97 | parser = config_open(filename); |
102 | 98 | if (!parser) | |
103 | lineno = 0; | 99 | return 1; |
104 | while (fgets(oneline, sizeof(oneline) - 1, fp)) { | ||
105 | lineno++; | ||
106 | trim(oneline); | ||
107 | if (oneline[0] == '#' || oneline[0] == ';') | ||
108 | continue; | ||
109 | if (!oneline[0] || !oneline[1]) | ||
110 | continue; | ||
111 | |||
112 | name = strtok(oneline, "="); | ||
113 | if (!name) { | ||
114 | bb_error_msg(WARN_BAD_LINE, filename, lineno); | ||
115 | continue; | ||
116 | } | ||
117 | trim(name); | ||
118 | if (!*name) { | ||
119 | bb_error_msg(WARN_BAD_LINE, filename, lineno); | ||
120 | continue; | ||
121 | } | ||
122 | 100 | ||
123 | value = strtok(NULL, "\n\r"); | 101 | while (config_read(parser, token, 2, 0, "# \t=", PARSE_LAST_IS_GREEDY)) { // TODO: ';' is comment char too |
124 | if (!value) { | 102 | if (!token[1]) { |
125 | bb_error_msg(WARN_BAD_LINE, filename, lineno); | 103 | bb_error_msg(WARN_BAD_LINE, filename, parser->lineno); |
126 | continue; | 104 | } else { |
127 | } | 105 | #if 0 |
128 | while (*value == ' ' || *value == '\t') | 106 | char *s = xasprintf("%s=%s", token[0], token[1]); |
129 | value++; | 107 | sysctl_write_setting(s); |
130 | if (!*value) { | 108 | free(s); |
131 | bb_error_msg(WARN_BAD_LINE, filename, lineno); | 109 | #else // PLAY_WITH_FIRE for -4 bytes? |
132 | continue; | 110 | sprintf(parser->line, "%s=%s", token[0], token[1]); // must have room by definition |
111 | sysctl_write_setting(parser->line); | ||
112 | #endif | ||
133 | } | 113 | } |
134 | |||
135 | /* safe because sizeof(oneline) == sizeof(buffer) */ | ||
136 | sprintf(buffer, "%s=%s", name, value); | ||
137 | sysctl_write_setting(buffer); | ||
138 | } | 114 | } |
139 | if (ENABLE_FEATURE_CLEAN_UP) | 115 | if (ENABLE_FEATURE_CLEAN_UP) |
140 | fclose(fp); | 116 | config_close(parser); |
141 | return 0; | 117 | return 0; |
142 | } /* end sysctl_preload_file_and_exit() */ | 118 | } /* end sysctl_preload_file_and_exit() */ |
143 | 119 | ||