diff options
author | Ron Yorston <rmy@pobox.com> | 2024-02-02 11:43:15 +0000 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2024-02-02 11:43:15 +0000 |
commit | 8dee37d53a47b8f96ba609d65d85438ef4e14fea (patch) | |
tree | 7fbf1e4c0994651703457364ba86dd7a4fc3450e | |
parent | e960b0d69d3f954d50e814a6bc4d6e206bde7f66 (diff) | |
download | busybox-w32-8dee37d53a47b8f96ba609d65d85438ef4e14fea.tar.gz busybox-w32-8dee37d53a47b8f96ba609d65d85438ef4e14fea.tar.bz2 busybox-w32-8dee37d53a47b8f96ba609d65d85438ef4e14fea.zip |
win32: rearrange applet override handling
- Rename some functions to be more meaningful.
- Adjust conditional compilation to clarify which code is required
for 'standalone shell' and 'exec prefers applets' settings.
This shouldn't result in any change to the behaviour or size of
default builds.
-rw-r--r-- | include/libbb.h | 8 | ||||
-rw-r--r-- | libbb/appletlib.c | 53 | ||||
-rw-r--r-- | libbb/lineedit.c | 3 | ||||
-rw-r--r-- | shell/ash.c | 10 |
4 files changed, 38 insertions, 36 deletions
diff --git a/include/libbb.h b/include/libbb.h index 2f882aa9d..76a107521 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -1330,12 +1330,12 @@ int find_applet_by_name(const char *name) FAST_FUNC; | |||
1330 | void run_applet_no_and_exit(int a, const char *name, char **argv) NORETURN FAST_FUNC; | 1330 | void run_applet_no_and_exit(int a, const char *name, char **argv) NORETURN FAST_FUNC; |
1331 | # if ENABLE_PLATFORM_MINGW32 | 1331 | # if ENABLE_PLATFORM_MINGW32 |
1332 | # if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE | 1332 | # if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE |
1333 | int is_applet_preferred(const char *name, const char *path) FAST_FUNC; | 1333 | int prefer_applet(const char *name, const char *path) FAST_FUNC; |
1334 | int find_applet_by_name_with_path(const char *name, const char *path) FAST_FUNC; | 1334 | int find_applet_by_name_for_sh(const char *name, const char *path) FAST_FUNC; |
1335 | # endif | 1335 | # endif |
1336 | # else | 1336 | # else |
1337 | # define is_applet_preferred(n, p) (1) | 1337 | # define prefer_applet(n, p) (1) |
1338 | # define find_applet_by_name_with_path(n, p) find_applet_by_name(n) | 1338 | # define find_applet_by_name_for_sh(n, p) find_applet_by_name(n) |
1339 | # endif | 1339 | # endif |
1340 | #endif | 1340 | #endif |
1341 | void show_usage_if_dash_dash_help(int applet_no, char **argv) FAST_FUNC; | 1341 | void show_usage_if_dash_dash_help(int applet_no, char **argv) FAST_FUNC; |
diff --git a/libbb/appletlib.c b/libbb/appletlib.c index 5b42a9091..1232d97c9 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c | |||
@@ -101,10 +101,10 @@ static const char packed_scripts[] ALIGN1 = { PACKED_SCRIPTS }; | |||
101 | #endif | 101 | #endif |
102 | 102 | ||
103 | #if ENABLE_PLATFORM_MINGW32 && NUM_APPLETS > 1 && \ | 103 | #if ENABLE_PLATFORM_MINGW32 && NUM_APPLETS > 1 && \ |
104 | (ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE) | 104 | ENABLE_FEATURE_SH_STANDALONE |
105 | static int really_find_applet_by_name(const char *name); | 105 | static int find_applet_by_name_internal(const char *name); |
106 | #else | 106 | #else |
107 | #define really_find_applet_by_name(n) find_applet_by_name(n) | 107 | # define find_applet_by_name_internal(n) find_applet_by_name(n) |
108 | #endif | 108 | #endif |
109 | 109 | ||
110 | unsigned FAST_FUNC string_array_len(char **argv) | 110 | unsigned FAST_FUNC string_array_len(char **argv) |
@@ -162,7 +162,7 @@ void FAST_FUNC bb_show_usage(void) | |||
162 | #else | 162 | #else |
163 | const char *p; | 163 | const char *p; |
164 | const char *usage_string = p = unpack_usage_messages(); | 164 | const char *usage_string = p = unpack_usage_messages(); |
165 | int ap = really_find_applet_by_name(applet_name); | 165 | int ap = find_applet_by_name_internal(applet_name); |
166 | 166 | ||
167 | if (ap < 0 || usage_string == NULL) | 167 | if (ap < 0 || usage_string == NULL) |
168 | xfunc_die(); | 168 | xfunc_die(); |
@@ -196,8 +196,8 @@ void FAST_FUNC bb_show_usage(void) | |||
196 | } | 196 | } |
197 | 197 | ||
198 | #if ENABLE_PLATFORM_MINGW32 && NUM_APPLETS > 1 && \ | 198 | #if ENABLE_PLATFORM_MINGW32 && NUM_APPLETS > 1 && \ |
199 | (ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE) | 199 | ENABLE_FEATURE_SH_STANDALONE |
200 | static int really_find_applet_by_name(const char *name) | 200 | static int find_applet_by_name_internal(const char *name) |
201 | #else | 201 | #else |
202 | int FAST_FUNC find_applet_by_name(const char *name) | 202 | int FAST_FUNC find_applet_by_name(const char *name) |
203 | #endif | 203 | #endif |
@@ -265,19 +265,21 @@ int FAST_FUNC find_applet_by_name(const char *name) | |||
265 | return -1; | 265 | return -1; |
266 | } | 266 | } |
267 | 267 | ||
268 | #if ENABLE_PLATFORM_MINGW32 && NUM_APPLETS > 1 && \ | 268 | #if ENABLE_PLATFORM_MINGW32 && NUM_APPLETS > 1 |
269 | (ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE) | 269 | # if ENABLE_FEATURE_SH_STANDALONE |
270 | int FAST_FUNC find_applet_by_name_with_path(const char *name, const char *path) | 270 | int FAST_FUNC find_applet_by_name_for_sh(const char *name, const char *path) |
271 | { | 271 | { |
272 | int applet_no = really_find_applet_by_name(name); | 272 | int applet_no = find_applet_by_name_internal(name); |
273 | return applet_no >= 0 && is_applet_preferred(name, path) ? applet_no : -1; | 273 | return applet_no >= 0 && prefer_applet(name, path) ? applet_no : -1; |
274 | } | 274 | } |
275 | 275 | ||
276 | int FAST_FUNC find_applet_by_name(const char *name) | 276 | int FAST_FUNC find_applet_by_name(const char *name) |
277 | { | 277 | { |
278 | return find_applet_by_name_with_path(name, NULL); | 278 | return find_applet_by_name_for_sh(name, NULL); |
279 | } | 279 | } |
280 | # endif | ||
280 | 281 | ||
282 | # if ENABLE_FEATURE_SH_STANDALONE || ENABLE_FEATURE_PREFER_APPLETS | ||
281 | static int external_exists(const char *name, const char *path) | 283 | static int external_exists(const char *name, const char *path) |
282 | { | 284 | { |
283 | char *path0, *path1, *ret; | 285 | char *path0, *path1, *ret; |
@@ -289,34 +291,34 @@ static int external_exists(const char *name, const char *path) | |||
289 | return ret != NULL; | 291 | return ret != NULL; |
290 | } | 292 | } |
291 | 293 | ||
292 | static int is_applet_preferred_by_var(const char *name, const char *path, | 294 | static int prefer_applet_internal(const char *name, const char *path, |
293 | const char *var) | 295 | const char *override) |
294 | { | 296 | { |
295 | const char *s, *sep; | 297 | const char *s, *sep; |
296 | size_t len; | 298 | size_t len; |
297 | 299 | ||
298 | if (var && *var) { | 300 | if (override && *override) { |
299 | /* '-' disables all applets */ | 301 | /* '-' disables all applets */ |
300 | if (var[0] == '-' && var[1] == '\0') | 302 | if (override[0] == '-' && override[1] == '\0') |
301 | return FALSE; | 303 | return FALSE; |
302 | 304 | ||
303 | /* '+' each applet is overridden if an external command exists */ | 305 | /* '+' each applet is overridden if an external command exists */ |
304 | if (var[0] == '+' && var[1] == '\0') | 306 | if (override[0] == '+' && override[1] == '\0') |
305 | return !external_exists(name, path); | 307 | return !external_exists(name, path); |
306 | 308 | ||
307 | /* Handle applets from a list separated by spaces, commas or | 309 | /* Handle applets from a list separated by spaces, commas or |
308 | * semicolons. Applets before the first semicolon are disabled. | 310 | * semicolons. Applets before the first semicolon are disabled. |
309 | * Applets after the first semicolon are overridden if a | 311 | * Applets after the first semicolon are overridden if a |
310 | * corresponding external command exists. */ | 312 | * corresponding external command exists. */ |
311 | sep = strchr(var, ';'); | 313 | sep = strchr(override, ';'); |
312 | len = strlen(name); | 314 | len = strlen(name); |
313 | s = var - 1; | 315 | s = override - 1; |
314 | while (1) { | 316 | while (1) { |
315 | s = strstr(s + 1, name); | 317 | s = strstr(s + 1, name); |
316 | if (!s) | 318 | if (!s) |
317 | break; | 319 | break; |
318 | /* neither "name.." nor "xxx,name.."? */ | 320 | /* neither "name.." nor "xxx,name.."? */ |
319 | if (s != var && !strchr(" ,;", s[-1])) | 321 | if (s != override && !strchr(" ,;", s[-1])) |
320 | continue; | 322 | continue; |
321 | /* neither "..name" nor "..name,xxx"? */ | 323 | /* neither "..name" nor "..name,xxx"? */ |
322 | if (s[len] != '\0' && !strchr(" ,;", s[len])) | 324 | if (s[len] != '\0' && !strchr(" ,;", s[len])) |
@@ -328,15 +330,16 @@ static int is_applet_preferred_by_var(const char *name, const char *path, | |||
328 | return TRUE; | 330 | return TRUE; |
329 | } | 331 | } |
330 | 332 | ||
331 | int FAST_FUNC is_applet_preferred(const char *name, const char *path) | 333 | int FAST_FUNC prefer_applet(const char *name, const char *path) |
332 | { | 334 | { |
333 | int ret; | 335 | int ret; |
334 | 336 | ||
335 | ret = is_applet_preferred_by_var(name, path, getenv(BB_OVERRIDE_APPLETS)); | 337 | ret = prefer_applet_internal(name, path, getenv(BB_OVERRIDE_APPLETS)); |
336 | if (sizeof(CONFIG_OVERRIDE_APPLETS) > 1 && ret) | 338 | if (sizeof(CONFIG_OVERRIDE_APPLETS) > 1 && ret) |
337 | ret = is_applet_preferred_by_var(name, path, CONFIG_OVERRIDE_APPLETS); | 339 | ret = prefer_applet_internal(name, path, CONFIG_OVERRIDE_APPLETS); |
338 | return ret; | 340 | return ret; |
339 | } | 341 | } |
342 | # endif | ||
340 | #endif | 343 | #endif |
341 | 344 | ||
342 | 345 | ||
@@ -1120,7 +1123,7 @@ int busybox_main(int argc UNUSED_PARAM, char **argv) | |||
1120 | /* convert to "<applet> --help" */ | 1123 | /* convert to "<applet> --help" */ |
1121 | applet_name = argv[0] = argv[2]; | 1124 | applet_name = argv[0] = argv[2]; |
1122 | argv[2] = NULL; | 1125 | argv[2] = NULL; |
1123 | if (really_find_applet_by_name(applet_name) >= 0) { | 1126 | if (find_applet_by_name_internal(applet_name) >= 0) { |
1124 | /* Make "--help foo" exit with 0: */ | 1127 | /* Make "--help foo" exit with 0: */ |
1125 | xfunc_error_retval = 0; | 1128 | xfunc_error_retval = 0; |
1126 | bb_show_usage(); | 1129 | bb_show_usage(); |
@@ -1233,7 +1236,7 @@ static NORETURN void run_applet_and_exit(const char *name, char **argv) | |||
1233 | # if NUM_APPLETS > 0 | 1236 | # if NUM_APPLETS > 0 |
1234 | /* find_applet_by_name() search is more expensive, so goes second */ | 1237 | /* find_applet_by_name() search is more expensive, so goes second */ |
1235 | { | 1238 | { |
1236 | int applet = really_find_applet_by_name(name); | 1239 | int applet = find_applet_by_name_internal(name); |
1237 | if (applet >= 0) | 1240 | if (applet >= 0) |
1238 | run_applet_no_and_exit(applet, name, argv); | 1241 | run_applet_no_and_exit(applet, name, argv); |
1239 | } | 1242 | } |
diff --git a/libbb/lineedit.c b/libbb/lineedit.c index 13cbb3229..ee494e013 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c | |||
@@ -979,8 +979,7 @@ static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type) | |||
979 | state->path_lookup : NULL; | 979 | state->path_lookup : NULL; |
980 | # endif | 980 | # endif |
981 | while (*p) { | 981 | while (*p) { |
982 | if (strncmp(basecmd, p, baselen) == 0 && | 982 | if (strncmp(basecmd, p, baselen) == 0 && prefer_applet(p, shpath)) |
983 | is_applet_preferred(p, shpath)) | ||
984 | add_match(xstrdup(p), TRUE); | 983 | add_match(xstrdup(p), TRUE); |
985 | while (*p++ != '\0') | 984 | while (*p++ != '\0') |
986 | continue; | 985 | continue; |
diff --git a/shell/ash.c b/shell/ash.c index 235eb8b7d..5b73c3c66 100644 --- a/shell/ash.c +++ b/shell/ash.c | |||
@@ -9197,7 +9197,7 @@ static void shellexec(char *prog, char **argv, const char *path, int idx) | |||
9197 | if (has_path(prog) | 9197 | if (has_path(prog) |
9198 | #endif | 9198 | #endif |
9199 | #if ENABLE_FEATURE_SH_STANDALONE | 9199 | #if ENABLE_FEATURE_SH_STANDALONE |
9200 | || (applet_no = find_applet_by_name_with_path(prog, path)) >= 0 | 9200 | || (applet_no = find_applet_by_name_for_sh(prog, path)) >= 0 |
9201 | #endif | 9201 | #endif |
9202 | ) { | 9202 | ) { |
9203 | #if ENABLE_PLATFORM_MINGW32 | 9203 | #if ENABLE_PLATFORM_MINGW32 |
@@ -9218,7 +9218,7 @@ static void shellexec(char *prog, char **argv, const char *path, int idx) | |||
9218 | if (unix_path(prog)) { | 9218 | if (unix_path(prog)) { |
9219 | const char *name = bb_basename(prog); | 9219 | const char *name = bb_basename(prog); |
9220 | # if ENABLE_FEATURE_SH_STANDALONE | 9220 | # if ENABLE_FEATURE_SH_STANDALONE |
9221 | if ((applet_no = find_applet_by_name_with_path(name, path)) >= 0) { | 9221 | if ((applet_no = find_applet_by_name_for_sh(name, path)) >= 0) { |
9222 | tryexec(applet_no, name, argv, envp); | 9222 | tryexec(applet_no, name, argv, envp); |
9223 | e = errno; | 9223 | e = errno; |
9224 | } | 9224 | } |
@@ -15055,7 +15055,7 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path) | |||
15055 | name = (char *)bb_basename(name); | 15055 | name = (char *)bb_basename(name); |
15056 | if ( | 15056 | if ( |
15057 | # if ENABLE_FEATURE_SH_STANDALONE | 15057 | # if ENABLE_FEATURE_SH_STANDALONE |
15058 | find_applet_by_name_with_path(name, path) >= 0 || | 15058 | find_applet_by_name_for_sh(name, path) >= 0 || |
15059 | # endif | 15059 | # endif |
15060 | !find_builtin(bb_basename(name)) | 15060 | !find_builtin(bb_basename(name)) |
15061 | ) { | 15061 | ) { |
@@ -15126,7 +15126,7 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path) | |||
15126 | 15126 | ||
15127 | #if ENABLE_FEATURE_SH_STANDALONE | 15127 | #if ENABLE_FEATURE_SH_STANDALONE |
15128 | { | 15128 | { |
15129 | int applet_no = find_applet_by_name_with_path(name, path); | 15129 | int applet_no = find_applet_by_name_for_sh(name, path); |
15130 | if (applet_no >= 0) { | 15130 | if (applet_no >= 0) { |
15131 | entry->cmdtype = CMDNORMAL; | 15131 | entry->cmdtype = CMDNORMAL; |
15132 | entry->u.index = -2 - applet_no; | 15132 | entry->u.index = -2 - applet_no; |
@@ -15379,7 +15379,7 @@ helpcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) | |||
15379 | { | 15379 | { |
15380 | const char *a = applet_names; | 15380 | const char *a = applet_names; |
15381 | while (*a) { | 15381 | while (*a) { |
15382 | if (is_applet_preferred(a, pathval())) { | 15382 | if (prefer_applet(a, pathval())) { |
15383 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a); | 15383 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a); |
15384 | if (col > 60) { | 15384 | if (col > 60) { |
15385 | out1fmt("\n"); | 15385 | out1fmt("\n"); |