ash: builtin: Mark more regular built-ins
[oweals/busybox.git] / libbb / appletlib.c
index 6dfaf1f412bb8bc8b1fbd38281dfa02c2a4a58cf..f842e73cc28d6a8b8d05bc48f620dd6f3d0bdda4 100644 (file)
@@ -50,8 +50,8 @@
 
 #include "usage_compressed.h"
 
-#if ENABLE_ASH_EMBEDDED_SCRIPTS
-# define DEFINE_script_names 1
+#if ENABLE_FEATURE_SH_EMBEDDED_SCRIPTS
+# define DEFINE_SCRIPT_DATA 1
 # include "embedded_scripts.h"
 #else
 # define NUM_SCRIPTS 0
@@ -123,16 +123,16 @@ void FAST_FUNC bb_show_usage(void)
 {
        if (ENABLE_SHOW_USAGE) {
 #ifdef SINGLE_APPLET_STR
-               /* Imagine that this applet is "true". Dont suck in printf! */
+               /* Imagine that this applet is "true". Dont link in printf! */
                const char *usage_string = unpack_usage_messages();
 
                if (usage_string) {
                        if (*usage_string == '\b') {
-                               full_write2_str("No help available.\n\n");
+                               full_write2_str("No help available\n");
                        } else {
                                full_write2_str("Usage: "SINGLE_APPLET_STR" ");
                                full_write2_str(usage_string);
-                               full_write2_str("\n\n");
+                               full_write2_str("\n");
                        }
                        if (ENABLE_FEATURE_CLEAN_UP)
                                dealloc_usage_messages((char*)usage_string);
@@ -149,9 +149,9 @@ void FAST_FUNC bb_show_usage(void)
                        ap--;
                }
                full_write2_str(bb_banner);
-               full_write2_str(" multi-call binary.\n");
+               full_write2_str(" multi-call binary.\n"); /* common string */
                if (*p == '\b')
-                       full_write2_str("\nNo help available.\n\n");
+                       full_write2_str("\nNo help available\n");
                else {
                        full_write2_str("\nUsage: ");
                        full_write2_str(applet_name);
@@ -304,7 +304,7 @@ void lbb_prepare(const char *applet
                IF_FEATURE_INDIVIDUAL(, char **argv))
 {
 #ifdef __GLIBC__
-       (*(int **)&bb_errno) = __errno_location();
+       (*(int **)not_const_pp(&bb_errno)) = __errno_location();
        barrier();
 #endif
        applet_name = applet;
@@ -631,7 +631,7 @@ static void check_suid(int applet_no)
                        /* same group / in group */
                        m >>= 3;
                if (!(m & S_IXOTH)) /* is x bit not set? */
-                       bb_error_msg_and_die("you have no permission to run this applet");
+                       bb_simple_error_msg_and_die("you have no permission to run this applet");
 
                /* We set effective AND saved ids. If saved-id is not set
                 * like we do below, seteuid(0) can still later succeed! */
@@ -643,7 +643,7 @@ static void check_suid(int applet_no)
                        rgid = sct->m_ugid.gid;
                /* else: we will set egid = rgid, thus dropping sgid effect */
                if (setresgid(-1, rgid, rgid))
-                       bb_perror_msg_and_die("setresgid");
+                       bb_simple_perror_msg_and_die("setresgid");
 
                /* Are we directed to change uid
                 * (APPLET = s** USER.GROUP or APPLET = S** USER.GROUP)?
@@ -653,7 +653,7 @@ static void check_suid(int applet_no)
                        uid = sct->m_ugid.uid;
                /* else: we will set euid = ruid, thus dropping suid effect */
                if (setresuid(-1, uid, uid))
-                       bb_perror_msg_and_die("setresuid");
+                       bb_simple_perror_msg_and_die("setresuid");
 
                goto ret;
        }
@@ -663,7 +663,7 @@ static void check_suid(int applet_no)
 
                if (!onetime) {
                        onetime = 1;
-                       bb_error_msg("using fallback suid method");
+                       bb_simple_error_msg("using fallback suid method");
                }
        }
 #   endif
@@ -673,7 +673,7 @@ static void check_suid(int applet_no)
                /* Real uid is not 0. If euid isn't 0 too, suid bit
                 * is most probably not set on our executable */
                if (geteuid())
-                       bb_error_msg_and_die("must be suid to work properly");
+                       bb_simple_error_msg_and_die("must be suid to work properly");
        } else if (APPLET_SUID(applet_no) == BB_SUID_DROP) {
                /*
                 * Drop all privileges.
@@ -756,6 +756,51 @@ static void install_links(const char *busybox UNUSED_PARAM,
 
 static void run_applet_and_exit(const char *name, char **argv) NORETURN;
 
+# if NUM_SCRIPTS > 0
+static int find_script_by_name(const char *name)
+{
+       int i;
+       int applet = find_applet_by_name(name);
+
+       if (applet >= 0) {
+               for (i = 0; i < NUM_SCRIPTS; ++i)
+                       if (applet_numbers[i] == applet)
+                               return i;
+       }
+       return -1;
+}
+
+int scripted_main(int argc UNUSED_PARAM, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int scripted_main(int argc UNUSED_PARAM, char **argv)
+{
+       int script = find_script_by_name(applet_name);
+       if (script >= 0)
+#if ENABLE_ASH || ENABLE_SH_IS_ASH || ENABLE_BASH_IS_ASH
+               exit(ash_main(-script - 1, argv));
+#elif ENABLE_HUSH || ENABLE_SH_IS_HUSH || ENABLE_BASH_IS_HUSH
+               exit(hush_main(-script - 1, argv));
+#else
+               return 1;
+#endif
+       return 0;
+}
+
+char* FAST_FUNC
+get_script_content(unsigned n)
+{
+       char *t = unpack_bz2_data(packed_scripts, sizeof(packed_scripts),
+                                       UNPACKED_SCRIPTS_LENGTH);
+       if (t) {
+               while (n != 0) {
+                       while (*t++ != '\0')
+                               continue;
+                       n--;
+               }
+       }
+       return t;
+}
+# endif /* NUM_SCRIPTS > 0 */
+
 # if ENABLE_BUSYBOX
 #  if ENABLE_FEATURE_SH_STANDALONE && ENABLE_FEATURE_TAB_COMPLETION
     /*
@@ -793,6 +838,9 @@ int busybox_main(int argc UNUSED_PARAM, char **argv)
                        "\n"
                        "Usage: busybox [function [arguments]...]\n"
                        "   or: busybox --list"IF_FEATURE_INSTALLER("[-full]")"\n"
+#  if ENABLE_FEATURE_SHOW_SCRIPT && NUM_SCRIPTS > 0
+                       "   or: busybox --show SCRIPT\n"
+#  endif
                        IF_FEATURE_INSTALLER(
                        "   or: busybox --install [-s] [DIR]\n"
                        )
@@ -818,32 +866,39 @@ int busybox_main(int argc UNUSED_PARAM, char **argv)
                /* prevent last comma to be in the very last pos */
                output_width--;
                a = applet_names;
-               {
-#  if NUM_SCRIPTS > 0
-                       int i;
-                       for (i = 0; i < 2; i++, a = script_names)
-#  endif
-                       while (*a) {
-                               int len2 = strlen(a) + 2;
-                               if (col >= (int)output_width - len2) {
-                                       full_write2_str(",\n");
-                                       col = 0;
-                               }
-                               if (col == 0) {
-                                       col = 6;
-                                       full_write2_str("\t");
-                               } else {
-                                       full_write2_str(", ");
-                               }
-                               full_write2_str(a);
-                               col += len2;
-                               a += len2 - 1;
+               while (*a) {
+                       int len2 = strlen(a) + 2;
+                       if (col >= (int)output_width - len2) {
+                               full_write2_str(",\n");
+                               col = 0;
                        }
+                       if (col == 0) {
+                               col = 6;
+                               full_write2_str("\t");
+                       } else {
+                               full_write2_str(", ");
+                       }
+                       full_write2_str(a);
+                       col += len2;
+                       a += len2 - 1;
                }
                full_write2_str("\n");
                return 0;
        }
 
+#  if ENABLE_FEATURE_SHOW_SCRIPT && NUM_SCRIPTS > 0
+       if (strcmp(argv[1], "--show") == 0) {
+               int n;
+               if (!argv[2])
+                       bb_error_msg_and_die(bb_msg_requires_arg, "--show");
+               n = find_script_by_name(argv[2]);
+               if (n < 0)
+                       bb_error_msg_and_die("script '%s' not found", argv[2]);
+               full_write1_str(get_script_content(n));
+               return 0;
+       }
+#  endif
+
        if (is_prefixed_with(argv[1], "--list")) {
                unsigned i = 0;
                const char *a = applet_names;
@@ -945,40 +1000,7 @@ void FAST_FUNC run_applet_no_and_exit(int applet_no, const char *name, char **ar
 }
 # endif /* NUM_APPLETS > 0 */
 
-# if NUM_SCRIPTS > 0
-int FAST_FUNC
-find_script_by_name(const char *name)
-{
-       const char *s = script_names;
-       int i = 0;
-
-       while (*s) {
-               if (strcmp(name, s) == 0)
-                       return i;
-               i++;
-               while (*s++ != '\0')
-                       continue;
-       }
-       return -0x10000; /* make it so that NUM_APPLETS + <error> is still < 0 */
-}
-
-char* FAST_FUNC
-get_script_content(unsigned n)
-{
-       char *t = unpack_bz2_data(packed_scripts, sizeof(packed_scripts),
-                                       UNPACKED_SCRIPTS_LENGTH);
-       if (t) {
-               while (n != 0) {
-                       while (*t++ != '\0')
-                               continue;
-                       n--;
-               }
-       }
-       return t;
-}
-# endif /* NUM_SCRIPTS > 0 */
-
-# if ENABLE_BUSYBOX || NUM_APPLETS > 0 || NUM_SCRIPTS > 0
+# if ENABLE_BUSYBOX || NUM_APPLETS > 0
 static NORETURN void run_applet_and_exit(const char *name, char **argv)
 {
 #  if ENABLE_BUSYBOX
@@ -993,13 +1015,6 @@ static NORETURN void run_applet_and_exit(const char *name, char **argv)
                        run_applet_no_and_exit(applet, name, argv);
        }
 #  endif
-#  if NUM_SCRIPTS > 0
-       {
-               int script = find_script_by_name(name);
-               if (script >= 0)
-                       exit(ash_main(-script - 1, argv));
-       }
-#  endif
 
        /*bb_error_msg_and_die("applet not found"); - links in printf */
        full_write2_str(applet_name);