ash: recognize embedded scripts in SH_STANDALONE mode
authorRon Yorston <rmy@pobox.com>
Thu, 1 Nov 2018 10:04:47 +0000 (11:04 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Thu, 1 Nov 2018 10:07:26 +0000 (11:07 +0100)
function                                             old     new   delta
find_script_by_name                                    -      51     +51
shellexec                                            254     271     +17
find_command                                         990    1007     +17
evalcommand                                         1653    1661      +8
doCommands                                          2233    2222     -11
run_applet_and_exit                                  128     100     -28
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 6/4 up/down: 104/-52)            Total: 52 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
include/libbb.h
libbb/appletlib.c
shell/ash.c

index affff58748629b0d6adfb4b49e56035130ee866f..a32608ebda2a2487d5e04e0cc8216fe250607f12 100644 (file)
@@ -1322,7 +1322,7 @@ void bb_logenv_override(void) FAST_FUNC;
 #endif
 
 /* Embedded script support */
-//int find_script_by_name(const char *arg IF_FEATURE_SH_STANDALONE(, int offset)) FAST_FUNC;
+int find_script_by_name(const char *name) FAST_FUNC;
 char *get_script_content(unsigned n) FAST_FUNC;
 
 /* Applets which are useful from another applets */
index 4e6d1c3d6851f085e1877f39017bf71534f41880..92d99fbe85e9ca4d24ae2d514ff54de5f70aac5a 100644 (file)
@@ -964,20 +964,20 @@ void FAST_FUNC run_applet_no_and_exit(int applet_no, const char *name, char **ar
 # endif /* NUM_APPLETS > 0 */
 
 # if NUM_SCRIPTS > 0
-static int
-find_script_by_name(const char *arg)
+int FAST_FUNC
+find_script_by_name(const char *name)
 {
        const char *s = script_names;
        int i = 0;
 
        while (*s) {
-               if (strcmp(arg, s) == 0)
+               if (strcmp(name, s) == 0)
                        return i;
                i++;
                while (*s++ != '\0')
                        continue;
        }
-       return -1;
+       return -0x10000; /* make it so that NUM_APPLETS + <error> is still < 0 */
 }
 
 static char *
index 3adb6d0d23f41592a0b9548d5ef9e18ec0389572..58ecf6d2cecd506e1647e21e4fb7f810854268f9 100644 (file)
@@ -8085,6 +8085,9 @@ static void shellexec(char *prog, char **argv, const char *path, int idx)
        if (strchr(prog, '/') != NULL
 #if ENABLE_FEATURE_SH_STANDALONE
         || (applet_no = find_applet_by_name(prog)) >= 0
+# if NUM_SCRIPTS > 0
+        || (applet_no = NUM_APPLETS + find_script_by_name(prog)) >= 0
+# endif
 #endif
        ) {
                tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) prog, argv, envp);
@@ -10186,6 +10189,10 @@ evalcommand(union node *cmd, int flags)
  */
                /* find_command() encodes applet_no as (-2 - applet_no) */
                int applet_no = (- cmdentry.u.index - 2);
+# if NUM_SCRIPTS > 0
+               /* Applets are ok, but not embedded scripts */
+               if (applet_no < NUM_APPLETS)
+# endif
                if (applet_no >= 0 && APPLET_IS_NOFORK(applet_no)) {
                        char **sv_environ;
 
@@ -13368,6 +13375,11 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path)
 #if ENABLE_FEATURE_SH_STANDALONE
        {
                int applet_no = find_applet_by_name(name);
+# if NUM_SCRIPTS > 0
+               if (applet_no < 0)
+                       /* embedded script indices are offset by NUM_APPLETS */
+                       applet_no = NUM_APPLETS + find_script_by_name(name);
+# endif
                if (applet_no >= 0) {
                        entry->cmdtype = CMDNORMAL;
                        entry->u.index = -2 - applet_no;