diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-07-15 21:09:30 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-07-15 21:09:30 +0000 |
commit | e559e0a75738044403e9a43f54ccb0ac3091cd9a (patch) | |
tree | 9216b28c5e34aa5fed658d7893201947eec861b2 /util-linux/mdev.c | |
parent | 0ed090e184739b6722a87acab644a8df24be09d4 (diff) | |
download | busybox-w32-e559e0a75738044403e9a43f54ccb0ac3091cd9a.tar.gz busybox-w32-e559e0a75738044403e9a43f54ccb0ac3091cd9a.tar.bz2 busybox-w32-e559e0a75738044403e9a43f54ccb0ac3091cd9a.zip |
libbb: unified config parser (By Vladimir Dronnikov)
mdev: use it
function old new delta
config_read - 400 +400
config_open - 43 +43
config_close - 9 +9
qrealloc 33 36 +3
compare_keys 735 737 +2
xstrtoull_range_sfx 296 295 -1
qgravechar 109 106 -3
get_address 181 178 -3
next_token 928 923 -5
sv_main 1228 1222 -6
find_main 418 406 -12
next_field 32 - -32
make_device 1269 1184 -85
------------------------------------------------------------------------------
(add/remove: 3/1 grow/shrink: 2/7 up/down: 457/-147) Total: 310 bytes
Diffstat (limited to 'util-linux/mdev.c')
-rw-r--r-- | util-linux/mdev.c | 81 |
1 files changed, 25 insertions, 56 deletions
diff --git a/util-linux/mdev.c b/util-linux/mdev.c index dd9522999..c61521cbb 100644 --- a/util-linux/mdev.c +++ b/util-linux/mdev.c | |||
@@ -25,16 +25,6 @@ struct globals { | |||
25 | /* We use additional 64+ bytes in make_device() */ | 25 | /* We use additional 64+ bytes in make_device() */ |
26 | #define SCRATCH_SIZE 80 | 26 | #define SCRATCH_SIZE 80 |
27 | 27 | ||
28 | static char *next_field(char *s) | ||
29 | { | ||
30 | char *end = skip_non_whitespace(s); | ||
31 | s = skip_whitespace(end); | ||
32 | *end = '\0'; | ||
33 | if (*s == '\0') | ||
34 | s = NULL; | ||
35 | return s; | ||
36 | } | ||
37 | |||
38 | /* Builds an alias path. | 28 | /* Builds an alias path. |
39 | * This function potentionally reallocates the alias parameter. | 29 | * This function potentionally reallocates the alias parameter. |
40 | */ | 30 | */ |
@@ -104,33 +94,26 @@ static void make_device(char *path, int delete) | |||
104 | type = S_IFBLK; | 94 | type = S_IFBLK; |
105 | 95 | ||
106 | if (ENABLE_FEATURE_MDEV_CONF) { | 96 | if (ENABLE_FEATURE_MDEV_CONF) { |
107 | FILE *fp; | 97 | parser_t parser; |
108 | char *line, *val, *next; | 98 | char *tokens[5]; |
109 | unsigned lineno = 0; | ||
110 | 99 | ||
111 | /* If we have config file, look up user settings */ | 100 | /* If we have config file, look up user settings */ |
112 | fp = fopen_or_warn("/etc/mdev.conf", "r"); | 101 | if (!config_open(&parser, "/etc/mdev.conf")) |
113 | if (!fp) | ||
114 | goto end_parse; | 102 | goto end_parse; |
115 | 103 | ||
116 | while ((line = xmalloc_fgetline(fp)) != NULL) { | 104 | while (config_read(&parser, tokens, 4, 3, " \t", '#')) { |
117 | regmatch_t off[1+9*ENABLE_FEATURE_MDEV_RENAME_REGEXP]; | 105 | regmatch_t off[1+9*ENABLE_FEATURE_MDEV_RENAME_REGEXP]; |
118 | 106 | char *val; | |
119 | ++lineno; | ||
120 | trim(line); | ||
121 | if (!line[0]) | ||
122 | goto next_line; | ||
123 | 107 | ||
124 | /* Fields: regex uid:gid mode [alias] [cmd] */ | 108 | /* Fields: regex uid:gid mode [alias] [cmd] */ |
125 | 109 | ||
126 | /* 1st field: regex to match this device */ | 110 | /* 1st field: regex to match this device */ |
127 | next = next_field(line); | ||
128 | { | 111 | { |
129 | regex_t match; | 112 | regex_t match; |
130 | int result; | 113 | int result; |
131 | 114 | ||
132 | /* Is this it? */ | 115 | /* Is this it? */ |
133 | xregcomp(&match, line, REG_EXTENDED); | 116 | xregcomp(&match, tokens[0], REG_EXTENDED); |
134 | result = regexec(&match, device_name, ARRAY_SIZE(off), off, 0); | 117 | result = regexec(&match, device_name, ARRAY_SIZE(off), off, 0); |
135 | regfree(&match); | 118 | regfree(&match); |
136 | 119 | ||
@@ -147,7 +130,7 @@ static void make_device(char *path, int delete) | |||
147 | if (result || off[0].rm_so | 130 | if (result || off[0].rm_so |
148 | || ((int)off[0].rm_eo != (int)strlen(device_name)) | 131 | || ((int)off[0].rm_eo != (int)strlen(device_name)) |
149 | ) { | 132 | ) { |
150 | goto next_line; | 133 | continue; |
151 | } | 134 | } |
152 | } | 135 | } |
153 | 136 | ||
@@ -155,15 +138,11 @@ static void make_device(char *path, int delete) | |||
155 | * after parsing the rest of fields */ | 138 | * after parsing the rest of fields */ |
156 | 139 | ||
157 | /* 2nd field: uid:gid - device ownership */ | 140 | /* 2nd field: uid:gid - device ownership */ |
158 | if (!next) /* field must exist */ | ||
159 | bb_error_msg_and_die("bad line %u", lineno); | ||
160 | val = next; | ||
161 | next = next_field(val); | ||
162 | { | 141 | { |
163 | struct passwd *pass; | 142 | struct passwd *pass; |
164 | struct group *grp; | 143 | struct group *grp; |
165 | char *str_uid = val; | 144 | char *str_uid = tokens[1]; |
166 | char *str_gid = strchrnul(val, ':'); | 145 | char *str_gid = strchrnul(str_uid, ':'); |
167 | 146 | ||
168 | if (*str_gid) | 147 | if (*str_gid) |
169 | *str_gid++ = '\0'; | 148 | *str_gid++ = '\0'; |
@@ -182,33 +161,30 @@ static void make_device(char *path, int delete) | |||
182 | } | 161 | } |
183 | 162 | ||
184 | /* 3rd field: mode - device permissions */ | 163 | /* 3rd field: mode - device permissions */ |
185 | if (!next) /* field must exist */ | 164 | mode = strtoul(tokens[2], NULL, 8); |
186 | bb_error_msg_and_die("bad line %u", lineno); | ||
187 | val = next; | ||
188 | next = next_field(val); | ||
189 | mode = strtoul(val, NULL, 8); | ||
190 | 165 | ||
166 | val = tokens[3]; | ||
191 | /* 4th field (opt): >alias */ | 167 | /* 4th field (opt): >alias */ |
192 | #if ENABLE_FEATURE_MDEV_RENAME | 168 | #if ENABLE_FEATURE_MDEV_RENAME |
193 | if (!next) | 169 | if (!val) |
194 | break; | 170 | break; |
195 | if (*next == '>' || *next == '=') { | 171 | aliaslink = *val; |
196 | #if ENABLE_FEATURE_MDEV_RENAME_REGEXP | 172 | if (aliaslink == '>' || aliaslink == '=') { |
197 | char *s, *p; | 173 | char *s, *p; |
198 | unsigned i, n; | 174 | unsigned i, n; |
199 | 175 | char *a = val; | |
200 | aliaslink = *next; | 176 | s = strchr(val, ' '); |
201 | val = next; | 177 | val = (s && s[1]) ? s+1 : NULL; |
202 | next = next_field(val); | 178 | #if ENABLE_FEATURE_MDEV_RENAME_REGEXP |
203 | /* substitute %1..9 with off[1..9], if any */ | 179 | /* substitute %1..9 with off[1..9], if any */ |
204 | n = 0; | 180 | n = 0; |
205 | s = val; | 181 | s = a; |
206 | while (*s) | 182 | while (*s) |
207 | if (*s++ == '%') | 183 | if (*s++ == '%') |
208 | n++; | 184 | n++; |
209 | 185 | ||
210 | p = alias = xzalloc(strlen(val) + n * strlen(device_name)); | 186 | p = alias = xzalloc(strlen(a) + n * strlen(device_name)); |
211 | s = val + 1; | 187 | s = a + 1; |
212 | while (*s) { | 188 | while (*s) { |
213 | *p = *s; | 189 | *p = *s; |
214 | if ('%' == *s) { | 190 | if ('%' == *s) { |
@@ -224,24 +200,20 @@ static void make_device(char *path, int delete) | |||
224 | s++; | 200 | s++; |
225 | } | 201 | } |
226 | #else | 202 | #else |
227 | aliaslink = *next; | 203 | alias = xstrdup(a + 1); |
228 | val = next; | ||
229 | next = next_field(val); | ||
230 | alias = xstrdup(val + 1); | ||
231 | #endif | 204 | #endif |
232 | } | 205 | } |
233 | #endif /* ENABLE_FEATURE_MDEV_RENAME */ | 206 | #endif /* ENABLE_FEATURE_MDEV_RENAME */ |
234 | 207 | ||
235 | /* The rest (opt): command to run */ | 208 | /* The rest (opt): command to run */ |
236 | if (!next) | 209 | if (!val) |
237 | break; | 210 | break; |
238 | val = next; | ||
239 | if (ENABLE_FEATURE_MDEV_EXEC) { | 211 | if (ENABLE_FEATURE_MDEV_EXEC) { |
240 | const char *s = "@$*"; | 212 | const char *s = "@$*"; |
241 | const char *s2 = strchr(s, *val); | 213 | const char *s2 = strchr(s, *val); |
242 | 214 | ||
243 | if (!s2) | 215 | if (!s2) |
244 | bb_error_msg_and_die("bad line %u", lineno); | 216 | bb_error_msg_and_die("bad line %u", parser.lineno); |
245 | 217 | ||
246 | /* Correlate the position in the "@$*" with the delete | 218 | /* Correlate the position in the "@$*" with the delete |
247 | * step so that we get the proper behavior: | 219 | * step so that we get the proper behavior: |
@@ -255,12 +227,9 @@ static void make_device(char *path, int delete) | |||
255 | } | 227 | } |
256 | /* end of field parsing */ | 228 | /* end of field parsing */ |
257 | break; /* we found matching line, stop */ | 229 | break; /* we found matching line, stop */ |
258 | next_line: | ||
259 | free(line); | ||
260 | } /* end of "while line is read from /etc/mdev.conf" */ | 230 | } /* end of "while line is read from /etc/mdev.conf" */ |
261 | 231 | ||
262 | free(line); /* in case we used "break" to get here */ | 232 | config_close(&parser); |
263 | fclose(fp); | ||
264 | } | 233 | } |
265 | end_parse: | 234 | end_parse: |
266 | 235 | ||