From 3994d5a52b8fa70637bffe4173f165267e6169fb Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Mon, 22 Oct 2018 09:12:07 +0100 Subject: Allow shell scripts to be embedded in the binary To assist in the deployment of shell scripts it may be convenient to embed them in the BusyBox binary. This patch adds two configuration options to the shell: - 'Embed scripts in the binary' takes any files in the directory 'embed', concatenates them with null separators, compresses them and embeds them in the binary. - 'Allow the contents of embedded scripts to be listed' makes the shell argument '-L name' list the contents of the named script. Both options are off by default. When scripts are embedded in the binary: - The shell argument '-L' lists the names of the scripts. - Scripts can be run as 'sh name arg...'. - An alias is added for each script, equivalent to "alias name='sh name'". --- shell/embedded_scripts | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100755 shell/embedded_scripts (limited to 'shell/embedded_scripts') diff --git a/shell/embedded_scripts b/shell/embedded_scripts new file mode 100755 index 000000000..0ca89b91e --- /dev/null +++ b/shell/embedded_scripts @@ -0,0 +1,62 @@ +#!/bin/sh + +target="$1" +loc="$2" + +test "$target" || exit 1 +test "$SED" || SED=sed +test "$DD" || DD=dd + +# Some people were bitten by their system lacking a (proper) od +od -v -b /dev/null +if test $? != 0; then + echo 'od tool is not installed or cannot accept "-v -b" options' + exit 1 +fi + +exec >"$target.$$" + +scripts="" +if [ -d "$loc" ] +then + scripts=$(cd $loc; ls * 2>/dev/null) +fi + +n=$(echo $scripts | wc -w) + +if [ $n -ne 0 ] +then + printf 'static const char script_names[] ALIGN1 = ' + for i in $scripts + do + printf '"%s\\0"' $i + done + printf '"\\0";\n' +fi +printf "#define NUM_SCRIPTS $n\n\n" + +if [ $n -ne 0 ] +then + printf '#define UNPACKED_SCRIPTS_LENGTH ' + for i in $scripts + do + cat $loc/$i + printf '\000' + done | wc -c + + printf '#define PACKED_SCRIPTS \\\n' + for i in $scripts + do + cat $loc/$i + printf '\000' + done | bzip2 -1 | $DD bs=2 skip=1 2>/dev/null | od -v -b \ + | grep -v '^ ' \ + | $SED -e 's/^[^ ]*//' \ + -e 's/ //g' \ + -e '/^$/d' \ + -e 's/\(...\)/0\1,/g' \ + -e 's/$/ \\/' + printf '\n' +fi + +mv -- "$target.$$" "$target" -- cgit v1.2.3-55-g6feb