[PATCH 2/3] Add 'busybox --scripts' option to list custom scripts

Ron Yorston rmy at pobox.com
Fri Nov 9 11:50:30 UTC 2018


Instead of listing custom scripts along with applets add an option
to list them separately.  This emphasizes their special status as
not fully supported by BusyBox.

With no custom scripts the size of the binary is unchanged; with an
empty custom script:

function                                             old     new   delta
run_applet_and_exit                                  728     792     +64
find_script_by_name                                   26      75     +49
.rodata                                           168561  168598     +37
script_names                                           -       3      +3
packed_scripts                                       123     122      -1
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 3/1 up/down: 153/-1)            Total: 152 bytes

Signed-off-by: Ron Yorston <rmy at pobox.com>
---
 libbb/appletlib.c        | 53 +++++++++++++++++++++++-----------------
 scripts/embedded_scripts |  6 +++++
 2 files changed, 36 insertions(+), 23 deletions(-)

diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index 46eb4697c..17b1ff2a8 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -57,6 +57,7 @@
 # define NUM_SCRIPTS 0
 # define NUM_CUSTOM_SCRIPTS 0
 # define NUM_APPLET_SCRIPTS 0
+# define IF_CUSTOM_SCRIPTS(...)
 #endif
 #if NUM_SCRIPTS > 0
 # include "bb_archive.h"
@@ -785,6 +786,8 @@ static
 #  endif
 int busybox_main(int argc UNUSED_PARAM, char **argv)
 {
+	int do_custom = 0;
+
 	if (!argv[1]) {
 		/* Called without arguments */
 		const char *a;
@@ -803,6 +806,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_CUSTOM_SCRIPTS(
+			"   or: busybox --scripts\n"
+			)
 			IF_FEATURE_INSTALLER(
 			"   or: busybox --install [-s] [DIR]\n"
 			)
@@ -825,42 +831,43 @@ int busybox_main(int argc UNUSED_PARAM, char **argv)
 			"Currently defined functions:\n"
 		);
 		col = 0;
+		a = applet_names;
 		/* prevent last comma to be in the very last pos */
 		output_width--;
-		a = applet_names;
-		{
-#  if NUM_CUSTOM_SCRIPTS > 0
-			int i;
-			for (i = 0; i < 2; i++, a = script_names)
-#  endif
-			while (*a) {
-				int len2 = strlen(a) + 2;
-				if (col >= (int)output_width - len2) {
-					full_write2_str(",\n");
-					col = 0;
-				}
-				if (col == 0) {
-					col = 6;
-					full_write2_str("\t");
-				} else {
-					full_write2_str(", ");
-				}
-				full_write2_str(a);
-				col += len2;
-				a += len2 - 1;
+		while (*a) {
+			int len2 = strlen(a) + 2;
+			if (col >= (int)output_width - len2) {
+				full_write2_str(",\n");
+				col = 0;
 			}
+			if (col == 0) {
+				col = 6;
+				full_write2_str("\t");
+			} else {
+				full_write2_str(", ");
+			}
+			full_write2_str(a);
+			col += len2;
+			a += len2 - 1;
 		}
 		full_write2_str("\n");
 		return 0;
 	}
 
-	if (is_prefixed_with(argv[1], "--list")) {
+#  if NUM_CUSTOM_SCRIPTS > 0
+	do_custom = strcmp(argv[1], "--scripts") == 0;
+#  endif
+	if (is_prefixed_with(argv[1], "--list") || do_custom) {
 		unsigned i = 0;
+#  if NUM_CUSTOM_SCRIPTS > 0
+		const char *a = do_custom ? script_names : applet_names;
+#  else
 		const char *a = applet_names;
+#  endif
 		dup2(1, 2);
 		while (*a) {
 #  if ENABLE_FEATURE_INSTALLER
-			if (argv[1][6]) /* --list-full? */
+			if (argv[1][6] && !do_custom) /* --list-full? */
 				full_write2_str(install_dir[APPLET_INSTALL_LOC(i)] + 1);
 #  endif
 			full_write2_str(a);
diff --git a/scripts/embedded_scripts b/scripts/embedded_scripts
index 865e1480b..e6e8c3415 100755
--- a/scripts/embedded_scripts
+++ b/scripts/embedded_scripts
@@ -87,6 +87,12 @@ printf "\n"
 printf '#define NUM_SCRIPTS %d\n' $n
 printf '#define NUM_CUSTOM_SCRIPTS %d\n' $num_cscripts
 printf '#define NUM_APPLET_SCRIPTS %d\n' $num_ascripts
+if [ $num_cscripts -ne 0 ]
+then
+   	printf "#define IF_CUSTOM_SCRIPTS(...) __VA_ARGS__\n"
+else
+   	printf "#define IF_CUSTOM_SCRIPTS(...)\n"
+fi
 printf "\n"
 
 if [ $n -ne 0 ]
-- 
2.19.1



More information about the busybox mailing list