bc: convert to "G trick" - this returns bc to zero bss increase
[oweals/busybox.git] / scripts / embedded_scripts
1 #!/bin/sh
2
3 . ./.config || exit 1
4
5 target="$1"
6 custom_loc="$2"
7 applet_loc="$3"
8
9 test "$target" || exit 1
10 test "$SED" || SED=sed
11 test "$DD" || DD=dd
12
13 if [ x"$CONFIG_FEATURE_SH_EMBEDDED_SCRIPTS" != x"y" ]
14 then
15         printf '#define NUM_SCRIPTS 0\n' >"$target"
16         exit 0
17 fi
18
19 # Some people were bitten by their system lacking a (proper) od
20 od -v -b </dev/null >/dev/null
21 if test $? != 0; then
22         echo 'od tool is not installed or cannot accept "-v -b" options'
23         exit 1
24 fi
25
26 custom_scripts=""
27 if [ -d "$custom_loc" ]
28 then
29         custom_scripts=$(cd $custom_loc; ls * 2>/dev/null)
30 fi
31 all_scripts=$($srctree/applets/busybox.mkscripts)
32
33 # all_scripts includes applet scripts and custom scripts, sort them out
34 applet_scripts=""
35 for i in $all_scripts
36 do
37     found=0
38         for j in $custom_scripts
39         do
40                 if [ "$i" = "$j" ]
41                 then
42                         found=1
43                         break;
44                 fi
45         done
46         if [ $found -eq 0 ]
47         then
48                 # anything that isn't a custom script is an applet script
49                 applet_scripts="$applet_scripts $i"
50         fi
51 done
52
53 # we know the custom scripts are present but applet scripts might have
54 # become detached from their configuration
55 for i in $applet_scripts
56 do
57         #if [ ! -f "$applet_loc/$i" -a ! -f "$custom_loc/$i" ]
58         if [ ! -f "$applet_loc/$i" ]
59         then
60                 echo "missing applet script $i"
61                 exit 1
62         fi
63 done
64
65 n=$(echo $custom_scripts $applet_scripts | wc -w)
66 nall=$(echo $all_scripts | wc -w)
67
68 if [ $n -ne $nall ]
69 then
70         echo "script mismatch $n != $nall"
71         exit 1
72 fi
73
74 concatenate_scripts() {
75         for i in $custom_scripts
76         do
77                 cat $custom_loc/$i
78                 printf '\000'
79         done
80         for i in $applet_scripts
81         do
82                 cat $applet_loc/$i
83                 printf '\000'
84         done
85 }
86
87 exec >"$target.$$"
88
89 if [ $n -ne 0 ]
90 then
91         printf '#ifdef DEFINE_SCRIPT_DATA\n'
92         printf 'const uint16_t applet_numbers[] = {\n'
93         for i in $custom_scripts $applet_scripts
94         do
95                 # TODO support applets with names including invalid characters
96                 printf '\tAPPLET_NO_%s,\n' $i
97         done
98         printf '};\n'
99         printf '#else\n'
100         printf 'extern const uint16_t applet_numbers[];\n'
101         printf '#endif\n'
102 fi
103
104 printf "\n"
105 printf '#define NUM_SCRIPTS %d\n' $n
106 printf "\n"
107
108 if [ $n -ne 0 ]
109 then
110         printf '#define UNPACKED_SCRIPTS_LENGTH '
111         concatenate_scripts | wc -c
112
113         printf '#define PACKED_SCRIPTS \\\n'
114         concatenate_scripts | bzip2 -1 | $DD bs=2 skip=1 2>/dev/null | \
115         od -v -b \
116         | grep -v '^ ' \
117         | $SED -e 's/^[^ ]*//' \
118                 -e 's/ //g' \
119                 -e '/^$/d' \
120                 -e 's/\(...\)/0\1,/g' \
121                 -e 's/$/ \\/'
122         printf '\n'
123 fi
124
125 mv -- "$target.$$" "$target"