summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston2018-11-18 19:12:26 +0100
committerDenys Vlasenko2018-11-18 19:19:29 +0100
commitd1b2ae2d04eae05d76ad3c1a07e9092c7d46c773 (patch)
tree76e46c6c5e905b49cc8efcd5aa6ce0ce193a257a
parent3778898f97a64e7b42b53194af7f3b93cc9c07a3 (diff)
downloadbusybox-d1b2ae2d04eae05d76ad3c1a07e9092c7d46c773.zip
busybox-d1b2ae2d04eae05d76ad3c1a07e9092c7d46c773.tar.gz
busybox: add '--show SCRIPT' option to display scripts
Add an option to allow the content of embedded scripts to be displayed. This includes applet scripts, custom scripts and the .profile script. function old new delta busybox_main 624 701 +77 find_script_by_name - 24 +24 scripted_main 41 35 -6 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 1/1 up/down: 101/-6) Total: 95 bytes Signed-off-by: Ron Yorston <rmy@pobox.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--Config.in5
-rw-r--r--libbb/appletlib.c92
2 files changed, 59 insertions, 38 deletions
diff --git a/Config.in b/Config.in
index ae21f52..01680af 100644
--- a/Config.in
+++ b/Config.in
@@ -178,6 +178,11 @@ config BUSYBOX
Running "busybox APPLET [ARGS...]" will still work, of course.
+config FEATURE_SHOW_SCRIPT
+ bool "Support --show SCRIPT"
+ default y
+ depends on BUSYBOX
+
config FEATURE_INSTALLER
bool "Support --install [-s] to install applet links at runtime"
default y
diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index a0ebaca..a79a37e 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -756,6 +756,44 @@ static void install_links(const char *busybox UNUSED_PARAM,
static void run_applet_and_exit(const char *name, char **argv) NORETURN;
+# if NUM_SCRIPTS > 0
+static int find_script_by_name(const char *name)
+{
+ int i;
+ int applet = find_applet_by_name(name);
+
+ if (applet >= 0) {
+ for (i = 0; i < NUM_SCRIPTS; ++i)
+ if (applet_numbers[i] == applet)
+ return i;
+ }
+ return -1;
+}
+
+int scripted_main(int argc UNUSED_PARAM, char **argv)
+{
+ int script = find_script_by_name(applet_name);
+ if (script >= 0)
+ exit(ash_main(-script - 1, argv));
+ return 0;
+}
+
+char* FAST_FUNC
+get_script_content(unsigned n)
+{
+ char *t = unpack_bz2_data(packed_scripts, sizeof(packed_scripts),
+ UNPACKED_SCRIPTS_LENGTH);
+ if (t) {
+ while (n != 0) {
+ while (*t++ != '\0')
+ continue;
+ n--;
+ }
+ }
+ return t;
+}
+# endif /* NUM_SCRIPTS > 0 */
+
# if ENABLE_BUSYBOX
# if ENABLE_FEATURE_SH_STANDALONE && ENABLE_FEATURE_TAB_COMPLETION
/*
@@ -793,6 +831,9 @@ int busybox_main(int argc UNUSED_PARAM, char **argv)
"\n"
"Usage: busybox [function [arguments]...]\n"
" or: busybox --list"IF_FEATURE_INSTALLER("[-full]")"\n"
+# if ENABLE_FEATURE_SHOW_SCRIPT && NUM_SCRIPTS > 0
+ " or: busybox --show SCRIPT\n"
+# endif
IF_FEATURE_INSTALLER(
" or: busybox --install [-s] [DIR]\n"
)
@@ -838,6 +879,19 @@ int busybox_main(int argc UNUSED_PARAM, char **argv)
return 0;
}
+# if ENABLE_FEATURE_SHOW_SCRIPT && NUM_SCRIPTS > 0
+ if (strcmp(argv[1], "--show") == 0) {
+ int n;
+ if (!argv[2])
+ bb_error_msg_and_die(bb_msg_requires_arg, "--show");
+ n = find_script_by_name(argv[2]);
+ if (n < 0)
+ bb_error_msg_and_die("script '%s' not found", argv[2]);
+ full_write1_str(get_script_content(n));
+ return 0;
+ }
+# endif
+
if (is_prefixed_with(argv[1], "--list")) {
unsigned i = 0;
const char *a = applet_names;
@@ -939,44 +993,6 @@ void FAST_FUNC run_applet_no_and_exit(int applet_no, const char *name, char **ar
}
# endif /* NUM_APPLETS > 0 */
-# if NUM_SCRIPTS > 0
-static int find_script_by_name(const char *name)
-{
- int i;
- int applet = find_applet_by_name(name);
-
- if (applet >= 0) {
- for (i = 0; i < NUM_SCRIPTS; ++i)
- if (applet_numbers[i] == applet)
- return i;
- }
- return -1;
-}
-
-int scripted_main(int argc UNUSED_PARAM, char **argv)
-{
- int script = find_script_by_name(applet_name);
- if (script >= 0)
- exit(ash_main(-script - 1, argv));
- return 0;
-}
-
-char* FAST_FUNC
-get_script_content(unsigned n)
-{
- char *t = unpack_bz2_data(packed_scripts, sizeof(packed_scripts),
- UNPACKED_SCRIPTS_LENGTH);
- if (t) {
- while (n != 0) {
- while (*t++ != '\0')
- continue;
- n--;
- }
- }
- return t;
-}
-# endif /* NUM_SCRIPTS > 0 */
-
# if ENABLE_BUSYBOX || NUM_APPLETS > 0
static NORETURN void run_applet_and_exit(const char *name, char **argv)
{