embedded_scripts 2.4 KB

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