diff options
author | Ron Yorston <rmy@pobox.com> | 2018-11-18 19:12:26 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-11-18 19:19:29 +0100 |
commit | d1b2ae2d04eae05d76ad3c1a07e9092c7d46c773 (patch) | |
tree | 76e46c6c5e905b49cc8efcd5aa6ce0ce193a257a | |
parent | 3778898f97a64e7b42b53194af7f3b93cc9c07a3 (diff) | |
download | busybox-w32-d1b2ae2d04eae05d76ad3c1a07e9092c7d46c773.tar.gz busybox-w32-d1b2ae2d04eae05d76ad3c1a07e9092c7d46c773.tar.bz2 busybox-w32-d1b2ae2d04eae05d76ad3c1a07e9092c7d46c773.zip |
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.in | 5 | ||||
-rw-r--r-- | libbb/appletlib.c | 92 |
2 files changed, 59 insertions, 38 deletions
@@ -178,6 +178,11 @@ config BUSYBOX | |||
178 | 178 | ||
179 | Running "busybox APPLET [ARGS...]" will still work, of course. | 179 | Running "busybox APPLET [ARGS...]" will still work, of course. |
180 | 180 | ||
181 | config FEATURE_SHOW_SCRIPT | ||
182 | bool "Support --show SCRIPT" | ||
183 | default y | ||
184 | depends on BUSYBOX | ||
185 | |||
181 | config FEATURE_INSTALLER | 186 | config FEATURE_INSTALLER |
182 | bool "Support --install [-s] to install applet links at runtime" | 187 | bool "Support --install [-s] to install applet links at runtime" |
183 | default y | 188 | default y |
diff --git a/libbb/appletlib.c b/libbb/appletlib.c index a0ebaca29..a79a37efb 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c | |||
@@ -756,6 +756,44 @@ static void install_links(const char *busybox UNUSED_PARAM, | |||
756 | 756 | ||
757 | static void run_applet_and_exit(const char *name, char **argv) NORETURN; | 757 | static void run_applet_and_exit(const char *name, char **argv) NORETURN; |
758 | 758 | ||
759 | # if NUM_SCRIPTS > 0 | ||
760 | static int find_script_by_name(const char *name) | ||
761 | { | ||
762 | int i; | ||
763 | int applet = find_applet_by_name(name); | ||
764 | |||
765 | if (applet >= 0) { | ||
766 | for (i = 0; i < NUM_SCRIPTS; ++i) | ||
767 | if (applet_numbers[i] == applet) | ||
768 | return i; | ||
769 | } | ||
770 | return -1; | ||
771 | } | ||
772 | |||
773 | int scripted_main(int argc UNUSED_PARAM, char **argv) | ||
774 | { | ||
775 | int script = find_script_by_name(applet_name); | ||
776 | if (script >= 0) | ||
777 | exit(ash_main(-script - 1, argv)); | ||
778 | return 0; | ||
779 | } | ||
780 | |||
781 | char* FAST_FUNC | ||
782 | get_script_content(unsigned n) | ||
783 | { | ||
784 | char *t = unpack_bz2_data(packed_scripts, sizeof(packed_scripts), | ||
785 | UNPACKED_SCRIPTS_LENGTH); | ||
786 | if (t) { | ||
787 | while (n != 0) { | ||
788 | while (*t++ != '\0') | ||
789 | continue; | ||
790 | n--; | ||
791 | } | ||
792 | } | ||
793 | return t; | ||
794 | } | ||
795 | # endif /* NUM_SCRIPTS > 0 */ | ||
796 | |||
759 | # if ENABLE_BUSYBOX | 797 | # if ENABLE_BUSYBOX |
760 | # if ENABLE_FEATURE_SH_STANDALONE && ENABLE_FEATURE_TAB_COMPLETION | 798 | # if ENABLE_FEATURE_SH_STANDALONE && ENABLE_FEATURE_TAB_COMPLETION |
761 | /* | 799 | /* |
@@ -793,6 +831,9 @@ int busybox_main(int argc UNUSED_PARAM, char **argv) | |||
793 | "\n" | 831 | "\n" |
794 | "Usage: busybox [function [arguments]...]\n" | 832 | "Usage: busybox [function [arguments]...]\n" |
795 | " or: busybox --list"IF_FEATURE_INSTALLER("[-full]")"\n" | 833 | " or: busybox --list"IF_FEATURE_INSTALLER("[-full]")"\n" |
834 | # if ENABLE_FEATURE_SHOW_SCRIPT && NUM_SCRIPTS > 0 | ||
835 | " or: busybox --show SCRIPT\n" | ||
836 | # endif | ||
796 | IF_FEATURE_INSTALLER( | 837 | IF_FEATURE_INSTALLER( |
797 | " or: busybox --install [-s] [DIR]\n" | 838 | " or: busybox --install [-s] [DIR]\n" |
798 | ) | 839 | ) |
@@ -838,6 +879,19 @@ int busybox_main(int argc UNUSED_PARAM, char **argv) | |||
838 | return 0; | 879 | return 0; |
839 | } | 880 | } |
840 | 881 | ||
882 | # if ENABLE_FEATURE_SHOW_SCRIPT && NUM_SCRIPTS > 0 | ||
883 | if (strcmp(argv[1], "--show") == 0) { | ||
884 | int n; | ||
885 | if (!argv[2]) | ||
886 | bb_error_msg_and_die(bb_msg_requires_arg, "--show"); | ||
887 | n = find_script_by_name(argv[2]); | ||
888 | if (n < 0) | ||
889 | bb_error_msg_and_die("script '%s' not found", argv[2]); | ||
890 | full_write1_str(get_script_content(n)); | ||
891 | return 0; | ||
892 | } | ||
893 | # endif | ||
894 | |||
841 | if (is_prefixed_with(argv[1], "--list")) { | 895 | if (is_prefixed_with(argv[1], "--list")) { |
842 | unsigned i = 0; | 896 | unsigned i = 0; |
843 | const char *a = applet_names; | 897 | 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 | |||
939 | } | 993 | } |
940 | # endif /* NUM_APPLETS > 0 */ | 994 | # endif /* NUM_APPLETS > 0 */ |
941 | 995 | ||
942 | # if NUM_SCRIPTS > 0 | ||
943 | static int find_script_by_name(const char *name) | ||
944 | { | ||
945 | int i; | ||
946 | int applet = find_applet_by_name(name); | ||
947 | |||
948 | if (applet >= 0) { | ||
949 | for (i = 0; i < NUM_SCRIPTS; ++i) | ||
950 | if (applet_numbers[i] == applet) | ||
951 | return i; | ||
952 | } | ||
953 | return -1; | ||
954 | } | ||
955 | |||
956 | int scripted_main(int argc UNUSED_PARAM, char **argv) | ||
957 | { | ||
958 | int script = find_script_by_name(applet_name); | ||
959 | if (script >= 0) | ||
960 | exit(ash_main(-script - 1, argv)); | ||
961 | return 0; | ||
962 | } | ||
963 | |||
964 | char* FAST_FUNC | ||
965 | get_script_content(unsigned n) | ||
966 | { | ||
967 | char *t = unpack_bz2_data(packed_scripts, sizeof(packed_scripts), | ||
968 | UNPACKED_SCRIPTS_LENGTH); | ||
969 | if (t) { | ||
970 | while (n != 0) { | ||
971 | while (*t++ != '\0') | ||
972 | continue; | ||
973 | n--; | ||
974 | } | ||
975 | } | ||
976 | return t; | ||
977 | } | ||
978 | # endif /* NUM_SCRIPTS > 0 */ | ||
979 | |||
980 | # if ENABLE_BUSYBOX || NUM_APPLETS > 0 | 996 | # if ENABLE_BUSYBOX || NUM_APPLETS > 0 |
981 | static NORETURN void run_applet_and_exit(const char *name, char **argv) | 997 | static NORETURN void run_applet_and_exit(const char *name, char **argv) |
982 | { | 998 | { |