aboutsummaryrefslogtreecommitdiff
path: root/libbb/appletlib.c
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2018-11-28 10:28:18 +0000
committerRon Yorston <rmy@pobox.com>2018-11-28 10:28:18 +0000
commit2a69a2200a141c1504b662eca64b802cdab71b12 (patch)
treeeab0cc01852db237a26052a83c8f582ed92b7cd9 /libbb/appletlib.c
parent97ca1f4b955f486cd26461cb09185335483d2921 (diff)
parent572dfb8e78323b9837f7c5e3369ee233a440b8f2 (diff)
downloadbusybox-w32-2a69a2200a141c1504b662eca64b802cdab71b12.tar.gz
busybox-w32-2a69a2200a141c1504b662eca64b802cdab71b12.tar.bz2
busybox-w32-2a69a2200a141c1504b662eca64b802cdab71b12.zip
Merge branch 'busybox' into merge
Diffstat (limited to 'libbb/appletlib.c')
-rw-r--r--libbb/appletlib.c125
1 files changed, 86 insertions, 39 deletions
diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index add609b78..3cbb5699c 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -59,6 +59,17 @@
59 59
60#include "usage_compressed.h" 60#include "usage_compressed.h"
61 61
62#if ENABLE_FEATURE_SH_EMBEDDED_SCRIPTS
63# define DEFINE_SCRIPT_DATA 1
64# include "embedded_scripts.h"
65#else
66# define NUM_SCRIPTS 0
67#endif
68#if NUM_SCRIPTS > 0
69# define BB_ARCHIVE_PUBLIC
70# include "bb_archive.h"
71static const char packed_scripts[] ALIGN1 = { PACKED_SCRIPTS };
72#endif
62 73
63/* "Do not compress usage text if uncompressed text is small 74/* "Do not compress usage text if uncompressed text is small
64 * and we don't include bunzip2 code for other reasons" 75 * and we don't include bunzip2 code for other reasons"
@@ -105,36 +116,10 @@ static const char usage_messages[] ALIGN1 = UNPACKED_USAGE;
105#if ENABLE_FEATURE_COMPRESS_USAGE 116#if ENABLE_FEATURE_COMPRESS_USAGE
106 117
107static const char packed_usage[] ALIGN1 = { PACKED_USAGE }; 118static const char packed_usage[] ALIGN1 = { PACKED_USAGE };
108#define BB_ARCHIVE_PUBLIC 119# define BB_ARCHIVE_PUBLIC
109# include "bb_archive.h" 120# include "bb_archive.h"
110static const char *unpack_usage_messages(void) 121# define unpack_usage_messages() \
111{ 122 unpack_bz2_data(packed_usage, sizeof(packed_usage), sizeof(UNPACKED_USAGE))
112 char *outbuf = NULL;
113 bunzip_data *bd;
114 int i;
115 jmp_buf jmpbuf;
116
117 /* Setup for I/O error handling via longjmp */
118 i = setjmp(jmpbuf);
119 if (i == 0) {
120 i = start_bunzip(&jmpbuf,
121 &bd,
122 /* src_fd: */ -1,
123 /* inbuf: */ packed_usage,
124 /* len: */ sizeof(packed_usage)
125 );
126 }
127 /* read_bunzip can longjmp and end up here with i != 0
128 * on read data errors! Not trivial */
129 if (i == 0) {
130 /* Cannot use xmalloc: will leak bd in NOFORK case! */
131 outbuf = malloc_or_warn(sizeof(UNPACKED_USAGE));
132 if (outbuf)
133 read_bunzip(bd, outbuf, sizeof(UNPACKED_USAGE));
134 }
135 dealloc_bunzip(bd);
136 return outbuf;
137}
138# define dealloc_usage_messages(s) free(s) 123# define dealloc_usage_messages(s) free(s)
139 124
140#else 125#else
@@ -152,21 +137,23 @@ void FAST_FUNC bb_show_usage(void)
152 /* Imagine that this applet is "true". Dont suck in printf! */ 137 /* Imagine that this applet is "true". Dont suck in printf! */
153 const char *usage_string = unpack_usage_messages(); 138 const char *usage_string = unpack_usage_messages();
154 139
155 if (*usage_string == '\b') { 140 if (usage_string) {
156 full_write2_str("No help available.\n\n"); 141 if (*usage_string == '\b') {
157 } else { 142 full_write2_str("No help available.\n\n");
158 full_write2_str("Usage: "SINGLE_APPLET_STR" "); 143 } else {
159 full_write2_str(usage_string); 144 full_write2_str("Usage: "SINGLE_APPLET_STR" ");
160 full_write2_str("\n\n"); 145 full_write2_str(usage_string);
146 full_write2_str("\n\n");
147 }
148 if (ENABLE_FEATURE_CLEAN_UP)
149 dealloc_usage_messages((char*)usage_string);
161 } 150 }
162 if (ENABLE_FEATURE_CLEAN_UP)
163 dealloc_usage_messages((char*)usage_string);
164#else 151#else
165 const char *p; 152 const char *p;
166 const char *usage_string = p = unpack_usage_messages(); 153 const char *usage_string = p = unpack_usage_messages();
167 int ap = find_applet_by_name(applet_name); 154 int ap = find_applet_by_name(applet_name);
168 155
169 if (ap < 0) /* never happens, paranoia */ 156 if (ap < 0 || usage_string == NULL)
170 xfunc_die(); 157 xfunc_die();
171 while (ap) { 158 while (ap) {
172 while (*p++) continue; 159 while (*p++) continue;
@@ -808,6 +795,50 @@ static void install_links(const char *busybox UNUSED_PARAM,
808 795
809static void run_applet_and_exit(const char *name, char **argv) NORETURN; 796static void run_applet_and_exit(const char *name, char **argv) NORETURN;
810 797
798# if NUM_SCRIPTS > 0
799static int find_script_by_name(const char *name)
800{
801 int i;
802 int applet = find_applet_by_name(name);
803
804 if (applet >= 0) {
805 for (i = 0; i < NUM_SCRIPTS; ++i)
806 if (applet_numbers[i] == applet)
807 return i;
808 }
809 return -1;
810}
811
812int scripted_main(int argc UNUSED_PARAM, char **argv)
813{
814 int script = find_script_by_name(applet_name);
815 if (script >= 0)
816#if ENABLE_ASH || ENABLE_SH_IS_ASH || ENABLE_BASH_IS_ASH
817 exit(ash_main(-script - 1, argv));
818#elif ENABLE_HUSH || ENABLE_SH_IS_HUSH || ENABLE_BASH_IS_HUSH
819 exit(hush_main(-script - 1, argv));
820#else
821 return 1;
822#endif
823 return 0;
824}
825
826char* FAST_FUNC
827get_script_content(unsigned n)
828{
829 char *t = unpack_bz2_data(packed_scripts, sizeof(packed_scripts),
830 UNPACKED_SCRIPTS_LENGTH);
831 if (t) {
832 while (n != 0) {
833 while (*t++ != '\0')
834 continue;
835 n--;
836 }
837 }
838 return t;
839}
840# endif /* NUM_SCRIPTS > 0 */
841
811# if ENABLE_BUSYBOX 842# if ENABLE_BUSYBOX
812# if ENABLE_FEATURE_SH_STANDALONE && ENABLE_FEATURE_TAB_COMPLETION 843# if ENABLE_FEATURE_SH_STANDALONE && ENABLE_FEATURE_TAB_COMPLETION
813 /* 844 /*
@@ -850,6 +881,9 @@ int busybox_main(int argc UNUSED_PARAM, char **argv)
850 "\n" 881 "\n"
851 "Usage: busybox [function [arguments]...]\n" 882 "Usage: busybox [function [arguments]...]\n"
852 " or: busybox --list"IF_FULL_LIST_OPTION("[-full]")"\n" 883 " or: busybox --list"IF_FULL_LIST_OPTION("[-full]")"\n"
884# if ENABLE_FEATURE_SHOW_SCRIPT && NUM_SCRIPTS > 0
885 " or: busybox --show SCRIPT\n"
886# endif
853 IF_FEATURE_INSTALLER( 887 IF_FEATURE_INSTALLER(
854 " or: busybox --install "IF_NOT_PLATFORM_MINGW32("[-s] ")"[DIR]\n" 888 " or: busybox --install "IF_NOT_PLATFORM_MINGW32("[-s] ")"[DIR]\n"
855 ) 889 )
@@ -877,9 +911,9 @@ int busybox_main(int argc UNUSED_PARAM, char **argv)
877 "Currently defined functions:\n" 911 "Currently defined functions:\n"
878 ); 912 );
879 col = 0; 913 col = 0;
880 a = applet_names;
881 /* prevent last comma to be in the very last pos */ 914 /* prevent last comma to be in the very last pos */
882 output_width--; 915 output_width--;
916 a = applet_names;
883 while (*a) { 917 while (*a) {
884 int len2 = strlen(a) + 2; 918 int len2 = strlen(a) + 2;
885 if (col >= (int)output_width - len2) { 919 if (col >= (int)output_width - len2) {
@@ -900,6 +934,19 @@ int busybox_main(int argc UNUSED_PARAM, char **argv)
900 return 0; 934 return 0;
901 } 935 }
902 936
937# if ENABLE_FEATURE_SHOW_SCRIPT && NUM_SCRIPTS > 0
938 if (strcmp(argv[1], "--show") == 0) {
939 int n;
940 if (!argv[2])
941 bb_error_msg_and_die(bb_msg_requires_arg, "--show");
942 n = find_script_by_name(argv[2]);
943 if (n < 0)
944 bb_error_msg_and_die("script '%s' not found", argv[2]);
945 full_write1_str(get_script_content(n));
946 return 0;
947 }
948# endif
949
903 if (is_prefixed_with(argv[1], "--list")) { 950 if (is_prefixed_with(argv[1], "--list")) {
904 unsigned i = 0; 951 unsigned i = 0;
905 const char *a = applet_names; 952 const char *a = applet_names;