aboutsummaryrefslogtreecommitdiff
path: root/libbb/appletlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'libbb/appletlib.c')
-rw-r--r--libbb/appletlib.c83
1 files changed, 82 insertions, 1 deletions
diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index 319bcc263..08720082e 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -50,6 +50,16 @@
50 50
51#include "usage_compressed.h" 51#include "usage_compressed.h"
52 52
53#if ENABLE_ASH_EMBEDDED_SCRIPTS
54# define DEFINE_script_names 1
55# include "embedded_scripts.h"
56#else
57# define NUM_SCRIPTS 0
58#endif
59#if NUM_SCRIPTS > 0
60# include "bb_archive.h"
61static const char packed_scripts[] ALIGN1 = { PACKED_SCRIPTS };
62#endif
53 63
54/* "Do not compress usage text if uncompressed text is small 64/* "Do not compress usage text if uncompressed text is small
55 * and we don't include bunzip2 code for other reasons" 65 * and we don't include bunzip2 code for other reasons"
@@ -953,7 +963,71 @@ void FAST_FUNC run_applet_no_and_exit(int applet_no, const char *name, char **ar
953} 963}
954# endif /* NUM_APPLETS > 0 */ 964# endif /* NUM_APPLETS > 0 */
955 965
956# if ENABLE_BUSYBOX || NUM_APPLETS > 0 966# if NUM_SCRIPTS > 0
967static char *
968unpack_scripts(void)
969{
970 char *outbuf = NULL;
971 bunzip_data *bd;
972 int i;
973 jmp_buf jmpbuf;
974
975 /* Setup for I/O error handling via longjmp */
976 i = setjmp(jmpbuf);
977 if (i == 0) {
978 i = start_bunzip(&jmpbuf,
979 &bd,
980 /* src_fd: */ -1,
981 /* inbuf: */ packed_scripts,
982 /* len: */ sizeof(packed_scripts)
983 );
984 }
985 /* read_bunzip can longjmp and end up here with i != 0
986 * on read data errors! Not trivial */
987 if (i == 0) {
988 outbuf = xmalloc(UNPACKED_SCRIPTS_LENGTH);
989 read_bunzip(bd, outbuf, UNPACKED_SCRIPTS_LENGTH);
990 }
991 dealloc_bunzip(bd);
992 return outbuf;
993}
994
995/*
996 * In standalone shell mode we sometimes want the index of the script
997 * and sometimes the index offset by NUM_APPLETS.
998 */
999static int
1000find_script_by_name(const char *arg)
1001{
1002 const char *s = script_names;
1003 int i = 0;
1004
1005 while (*s) {
1006 if (strcmp(arg, s) == 0)
1007 return i;
1008 i++;
1009 while (*s++ != '\0')
1010 continue;
1011 }
1012 return -1;
1013}
1014
1015char* FAST_FUNC
1016get_script_content(unsigned n)
1017{
1018 char *t = unpack_scripts();
1019 if (t) {
1020 while (n != 0) {
1021 while (*t++ != '\0')
1022 continue;
1023 n--;
1024 }
1025 }
1026 return t;
1027}
1028# endif /* NUM_SCRIPTS > 0 */
1029
1030# if ENABLE_BUSYBOX || NUM_APPLETS > 0 || NUM_SCRIPTS > 0
957static NORETURN void run_applet_and_exit(const char *name, char **argv) 1031static NORETURN void run_applet_and_exit(const char *name, char **argv)
958{ 1032{
959# if ENABLE_BUSYBOX 1033# if ENABLE_BUSYBOX
@@ -968,6 +1042,13 @@ static NORETURN void run_applet_and_exit(const char *name, char **argv)
968 run_applet_no_and_exit(applet, name, argv); 1042 run_applet_no_and_exit(applet, name, argv);
969 } 1043 }
970# endif 1044# endif
1045# if NUM_SCRIPTS > 0
1046 {
1047 int script = find_script_by_name(name);
1048 if (script >= 0)
1049 exit(ash_main(-script - 1, argv));
1050 }
1051# endif
971 1052
972 /*bb_error_msg_and_die("applet not found"); - links in printf */ 1053 /*bb_error_msg_and_die("applet not found"); - links in printf */
973 full_write2_str(applet_name); 1054 full_write2_str(applet_name);