diff options
author | Ron Yorston <rmy@pobox.com> | 2022-05-06 12:31:21 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2022-05-06 12:31:21 +0100 |
commit | ecdc5b3ffabb58e3a9fc3a55a2a44323644995fc (patch) | |
tree | 7a394f48dea0e8e5f47c4ab0eba84d2fb04a00b0 | |
parent | 26ba73098e714459e3294679228a1d54eed14799 (diff) | |
download | busybox-w32-ecdc5b3ffabb58e3a9fc3a55a2a44323644995fc.tar.gz busybox-w32-ecdc5b3ffabb58e3a9fc3a55a2a44323644995fc.tar.bz2 busybox-w32-ecdc5b3ffabb58e3a9fc3a55a2a44323644995fc.zip |
win32: allow preference for applets to be disabled at runtime
The default busybox-w32 configuration enables the PREFER_APPLETS
and SH_STANDALONE features. Sometimes it may be desirable to
override the default preference for applets, for example, if an
applet needs to be replaced by an external program with additional
features.
Add support for the environment variable BB_OVERRIDE_APPLETS.
Its value may be:
- a single dash ('-'): all applets are overridden;
- a space-separated list of names: only the specified applets
are overridden.
-rw-r--r-- | include/libbb.h | 8 | ||||
-rw-r--r-- | libbb/appletlib.c | 46 | ||||
-rw-r--r-- | libbb/lineedit.c | 2 | ||||
-rw-r--r-- | shell/ash.c | 10 |
4 files changed, 61 insertions, 5 deletions
diff --git a/include/libbb.h b/include/libbb.h index 740c25528..482fcce0c 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -1320,6 +1320,14 @@ void run_noexec_applet_and_exit(int a, const char *name, char **argv) NORETURN F | |||
1320 | #ifndef BUILD_INDIVIDUAL | 1320 | #ifndef BUILD_INDIVIDUAL |
1321 | int find_applet_by_name(const char *name) FAST_FUNC; | 1321 | int find_applet_by_name(const char *name) FAST_FUNC; |
1322 | void run_applet_no_and_exit(int a, const char *name, char **argv) NORETURN FAST_FUNC; | 1322 | void run_applet_no_and_exit(int a, const char *name, char **argv) NORETURN FAST_FUNC; |
1323 | int find_preferred_applet_by_name(const char *name) FAST_FUNC; | ||
1324 | int is_applet_preferred(const char *name) FAST_FUNC; | ||
1325 | # if ENABLE_PLATFORM_MINGW32 && \ | ||
1326 | (ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE) | ||
1327 | # define find_applet_by_name(n) find_preferred_applet_by_name(n) | ||
1328 | # else | ||
1329 | # define is_applet_preferred(n) (1) | ||
1330 | # endif | ||
1323 | #endif | 1331 | #endif |
1324 | void show_usage_if_dash_dash_help(int applet_no, char **argv) FAST_FUNC; | 1332 | void show_usage_if_dash_dash_help(int applet_no, char **argv) FAST_FUNC; |
1325 | #if defined(__linux__) | 1333 | #if defined(__linux__) |
diff --git a/libbb/appletlib.c b/libbb/appletlib.c index aa442144a..8d58ce2ea 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c | |||
@@ -76,6 +76,13 @@ static inline int *get_perrno(void) { return &errno; } | |||
76 | static const char packed_scripts[] ALIGN1 = { PACKED_SCRIPTS }; | 76 | static const char packed_scripts[] ALIGN1 = { PACKED_SCRIPTS }; |
77 | #endif | 77 | #endif |
78 | 78 | ||
79 | #if defined(find_applet_by_name) | ||
80 | # undef find_applet_by_name | ||
81 | #endif | ||
82 | #if defined(is_applet_preferred) | ||
83 | # undef is_applet_preferred | ||
84 | #endif | ||
85 | |||
79 | /* "Do not compress usage text if uncompressed text is small | 86 | /* "Do not compress usage text if uncompressed text is small |
80 | * and we don't include bunzip2 code for other reasons" | 87 | * and we don't include bunzip2 code for other reasons" |
81 | * | 88 | * |
@@ -254,6 +261,45 @@ int FAST_FUNC find_applet_by_name(const char *name) | |||
254 | return -1; | 261 | return -1; |
255 | } | 262 | } |
256 | 263 | ||
264 | #if ENABLE_PLATFORM_MINGW32 && NUM_APPLETS > 1 && \ | ||
265 | (ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE) | ||
266 | int FAST_FUNC is_applet_preferred(const char *name) | ||
267 | { | ||
268 | const char *var, *s; | ||
269 | size_t len; | ||
270 | |||
271 | var = getenv("BB_OVERRIDE_APPLETS"); | ||
272 | if (var && *var) { | ||
273 | /* '-' overrides all applets */ | ||
274 | if (var[0] == '-' && var[1] == '\0') | ||
275 | return FALSE; | ||
276 | |||
277 | /* Override applets from a space-separated list */ | ||
278 | len = strlen(name); | ||
279 | s = var - 1; | ||
280 | while (1) { | ||
281 | s = strstr(s + 1, name); | ||
282 | if (!s) | ||
283 | break; | ||
284 | /* neither "name.." nor "xxx,name.."? */ | ||
285 | if (s != var && s[-1] != ' ') | ||
286 | continue; | ||
287 | /* neither "..name" nor "..name,xxx"? */ | ||
288 | if (s[len] != '\0' && s[len] != ' ') | ||
289 | continue; | ||
290 | return FALSE; | ||
291 | } | ||
292 | } | ||
293 | return TRUE; | ||
294 | } | ||
295 | |||
296 | int FAST_FUNC find_preferred_applet_by_name(const char *name) | ||
297 | { | ||
298 | int applet_no = find_applet_by_name(name); | ||
299 | return applet_no >= 0 && is_applet_preferred(name) ? applet_no : -1; | ||
300 | } | ||
301 | #endif | ||
302 | |||
257 | 303 | ||
258 | void lbb_prepare(const char *applet | 304 | void lbb_prepare(const char *applet |
259 | IF_FEATURE_INDIVIDUAL(, char **argv)) | 305 | IF_FEATURE_INDIVIDUAL(, char **argv)) |
diff --git a/libbb/lineedit.c b/libbb/lineedit.c index 778511d16..4f68547eb 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c | |||
@@ -926,7 +926,7 @@ static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type) | |||
926 | # if ENABLE_FEATURE_SH_STANDALONE && NUM_APPLETS != 1 | 926 | # if ENABLE_FEATURE_SH_STANDALONE && NUM_APPLETS != 1 |
927 | const char *p = applet_names; | 927 | const char *p = applet_names; |
928 | while (*p) { | 928 | while (*p) { |
929 | if (strncmp(basecmd, p, baselen) == 0) | 929 | if (strncmp(basecmd, p, baselen) == 0 && is_applet_preferred(p)) |
930 | add_match(xstrdup(p)); | 930 | add_match(xstrdup(p)); |
931 | while (*p++ != '\0') | 931 | while (*p++ != '\0') |
932 | continue; | 932 | continue; |
diff --git a/shell/ash.c b/shell/ash.c index 6c1e58d6f..f42dc49c3 100644 --- a/shell/ash.c +++ b/shell/ash.c | |||
@@ -14961,10 +14961,12 @@ helpcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) | |||
14961 | { | 14961 | { |
14962 | const char *a = applet_names; | 14962 | const char *a = applet_names; |
14963 | while (*a) { | 14963 | while (*a) { |
14964 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a); | 14964 | if (is_applet_preferred(a)) { |
14965 | if (col > 60) { | 14965 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a); |
14966 | out1fmt("\n"); | 14966 | if (col > 60) { |
14967 | col = 0; | 14967 | out1fmt("\n"); |
14968 | col = 0; | ||
14969 | } | ||
14968 | } | 14970 | } |
14969 | while (*a++ != '\0') | 14971 | while (*a++ != '\0') |
14970 | continue; | 14972 | continue; |