[PATCH 2/2] busybox: add '--show name' option to display script content

Ron Yorston rmy at pobox.com
Sun Nov 18 15:02:59 UTC 2018


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
run_applet_and_exit                                  728     820     +92
.rodata                                           168634  168690     +56
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 148/0)             Total: 148 bytes

Signed-off-by: Ron Yorston <rmy at pobox.com>
---
 libbb/appletlib.c | 107 +++++++++++++++++++++++++++++-----------------
 1 file changed, 67 insertions(+), 40 deletions(-)

diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index 9e17b84ac..34867ada0 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -61,6 +61,12 @@
 #endif
 
 #define SEPARATE_CUSTOM_SCRIPTS (NUM_CUSTOM_SCRIPTS > 0 || HAS_PROFILE)
+#define SHOW_SCRIPT_CONTENT     (NUM_SCRIPTS > 0 || HAS_PROFILE)
+#if SHOW_SCRIPT_CONTENT
+# define IF_SHOW_SCRIPT_CONTENT(...) __VA_ARGS__
+#else
+# define IF_SHOW_SCRIPT_CONTENT(...)
+#endif
 
 #if NUM_SCRIPTS > 0 || HAS_PROFILE
 # include "bb_archive.h"
@@ -762,6 +768,46 @@ 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 || HAS_PROFILE
+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 + HAS_PROFILE;
+	}
+	return -1;
+}
+
+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 || HAS_PROFILE */
+
+#if NUM_SCRIPTS > 0
+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;
+}
+# endif /* NUM_SCRIPTS > 0 */
+
 # if ENABLE_BUSYBOX
 #  if SEPARATE_CUSTOM_SCRIPTS
 static int is_custom(int applet_no)
@@ -814,6 +860,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_SHOW_SCRIPT_CONTENT(
+			"   or: busybox --show name\n"
+			)
 			IF_FEATURE_INSTALLER(
 			"   or: busybox --install [-s] [DIR]\n"
 			)
@@ -906,6 +955,24 @@ int busybox_main(int argc UNUSED_PARAM, char **argv)
 		return 0;
 	}
 
+	if (SHOW_SCRIPT_CONTENT && strcmp(argv[1], "--show") == 0) {
+		if (argv[2] != NULL) {
+			int n = (HAS_PROFILE && strcmp(argv[2], ".profile") == 0) ? 0 :
+						find_script_by_name(argv[2]);
+			if (n >= 0) {
+				char *script = get_script_content(n);
+				if (script != NULL) {
+					full_write1_str(script);
+				}
+			}
+			else {
+				bb_error_msg_and_die("script '%s' not found", argv[2]);
+			}
+			return 0;
+		}
+		bb_error_msg_and_die(bb_msg_requires_arg, "--show");
+	}
+
 	if (ENABLE_FEATURE_INSTALLER && strcmp(argv[1], "--install") == 0) {
 		int use_symbolic_links;
 		const char *busybox;
@@ -989,46 +1056,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 + HAS_PROFILE;
-	}
-	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;
-}
-# endif /* NUM_SCRIPTS > 0 */
-
-# if NUM_SCRIPTS > 0 || HAS_PROFILE
-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 || HAS_PROFILE */
-
 # if ENABLE_BUSYBOX || NUM_APPLETS > 0
 static NORETURN void run_applet_and_exit(const char *name, char **argv)
 {
-- 
2.19.1



More information about the busybox mailing list