fix breakage found by randomconfig
[oweals/busybox.git] / shell / ash.c
index 4113ce8e20982f94175cb3c8f06452f1eb01a555..dd20fe33817f162f231581069a69461e12c1690c 100644 (file)
  * a quit signal will generate a core dump.
  */
 #define DEBUG 0
-#define IFS_BROKEN
 #define PROFILE 0
-#if ENABLE_ASH_JOB_CONTROL
-#define JOBS 1
-#else
-#define JOBS 0
-#endif
+
+#define IFS_BROKEN
+
+#define JOBS ENABLE_ASH_JOB_CONTROL
 
 #if DEBUG
+#ifndef _GNU_SOURCE
 #define _GNU_SOURCE
 #endif
-#include "busybox.h" /* for struct bb_applet */
+#endif
+
+#include "busybox.h" /* for applet_names */
 #include <paths.h>
 #include <setjmp.h>
 #include <fnmatch.h>
 #if JOBS || ENABLE_ASH_READ_NCHARS
 #include <termios.h>
 #endif
-extern char **environ;
 
 #if defined(__uClinux__)
 #error "Do not even bother, ash will not run on uClinux"
 #endif
 
 
+/* ============ Hash table sizes. Configurable. */
+
+#define VTABSIZE 39
+#define ATABSIZE 39
+#define CMDTABLESIZE 31         /* should be prime */
+
+
 /* ============ Misc helpers */
 
 #define xbarrier() do { __asm__ __volatile__ ("": : :"memory"); } while (0)
@@ -127,30 +134,10 @@ static char optlist[NOPTS] ALIGN1;
 
 /* ============ Misc data */
 
-static char nullstr[1] ALIGN1;  /* zero length string */
 static const char homestr[] ALIGN1 = "HOME";
 static const char snlfmt[] ALIGN1 = "%s\n";
 static const char illnum[] ALIGN1 = "Illegal number: %s";
 
-static char *minusc;  /* argument to -c option */
-
-/* pid of main shell */
-static int rootpid;
-/* shell level: 0 for the main shell, 1 for its children, and so on */
-static int shlvl;
-#define rootshell (!shlvl)
-/* trap handler commands */
-static char *trap[NSIG];
-static smallint isloginsh;
-/* current value of signal */
-static char sigmode[NSIG - 1];
-/* indicates specified signal received */
-static char gotsig[NSIG - 1];
-static char *arg0; /* value of $0 */
-
-
-/* ============ Interrupts / exceptions */
-
 /*
  * We enclose jmp_buf in a structure so that we can declare pointers to
  * jump locations.  The global variable handler contains the location to
@@ -163,37 +150,90 @@ static char *arg0; /* value of $0 */
 struct jmploc {
        jmp_buf loc;
 };
-static struct jmploc *exception_handler;
-static int exception;
-/* exceptions */
+
+struct globals_misc {
+       /* pid of main shell */
+       int rootpid;
+       /* shell level: 0 for the main shell, 1 for its children, and so on */
+       int shlvl;
+#define rootshell (!shlvl)
+       char *minusc;  /* argument to -c option */
+
+       char *curdir; // = nullstr;     /* current working directory */
+       char *physdir; // = nullstr;    /* physical working directory */
+
+       char *arg0; /* value of $0 */
+
+       struct jmploc *exception_handler;
+
+// disabled by vda: cannot understand how it was supposed to work -
+// cannot fix bugs. That's why you have to explain your non-trivial designs!
+//     /* do we generate EXSIG events */
+//     int exsig; /* counter */
+       volatile int suppressint; /* counter */
+       volatile /*sig_atomic_t*/ smallint intpending; /* 1 = got SIGINT */
+       /* last pending signal */
+       volatile /*sig_atomic_t*/ smallint pendingsig;
+       smallint exception; /* kind of exception (0..5) */
+       /* exceptions */
 #define EXINT 0         /* SIGINT received */
 #define EXERROR 1       /* a generic error */
 #define EXSHELLPROC 2   /* execute a shell procedure */
 #define EXEXEC 3        /* command execution failed */
 #define EXEXIT 4        /* exit the shell */
 #define EXSIG 5         /* trapped signal in wait(1) */
-static volatile int suppressint;
-static volatile sig_atomic_t intpending;
-/* do we generate EXSIG events */
-static int exsig;
-/* last pending signal */
-static volatile sig_atomic_t pendingsig;
-
-/*
- * Sigmode records the current value of the signal handlers for the various
- * modes.  A value of zero means that the current handler is not known.
- * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
- */
 
+       /* trap handler commands */
+       smallint isloginsh;
+       char *trap[NSIG];
+       char nullstr[1];        /* zero length string */
+       /*
+        * Sigmode records the current value of the signal handlers for the various
+        * modes.  A value of zero means that the current handler is not known.
+        * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
+        */
+       char sigmode[NSIG - 1];
 #define S_DFL 1                 /* default signal handling (SIG_DFL) */
 #define S_CATCH 2               /* signal is caught */
 #define S_IGN 3                 /* signal is ignored (SIG_IGN) */
 #define S_HARD_IGN 4            /* signal is ignored permenantly */
 #define S_RESET 5               /* temporary - to reset a hard ignored sig */
 
+       /* indicates specified signal received */
+       char gotsig[NSIG - 1];
+};
+extern struct globals_misc *const ash_ptr_to_globals_misc;
+#define G_misc (*ash_ptr_to_globals_misc)
+#define rootpid   (G_misc.rootpid  )
+#define shlvl     (G_misc.shlvl    )
+#define minusc    (G_misc.minusc   )
+#define curdir    (G_misc.curdir   )
+#define physdir   (G_misc.physdir  )
+#define arg0      (G_misc.arg0     )
+#define exception_handler (G_misc.exception_handler)
+#define exception         (G_misc.exception        )
+#define suppressint       (G_misc.suppressint      )
+#define intpending        (G_misc.intpending       )
+//#define exsig             (G_misc.exsig            )
+#define pendingsig        (G_misc.pendingsig       )
+#define isloginsh (G_misc.isloginsh)
+#define trap      (G_misc.trap     )
+#define nullstr   (G_misc.nullstr  )
+#define sigmode   (G_misc.sigmode  )
+#define gotsig    (G_misc.gotsig   )
+#define INIT_G_misc() do { \
+       (*(struct globals_misc**)&ash_ptr_to_globals_misc) = xzalloc(sizeof(G_misc)); \
+       barrier(); \
+       curdir = nullstr; \
+       physdir = nullstr; \
+} while (0)
+
+
+/* ============ Interrupts / exceptions */
+
 /*
  * These macros allow the user to suspend the handling of interrupt signals
- * over a period of time.  This is similar to SIGHOLD to or sigblock, but
+ * over a period of time.  This is similar to SIGHOLD or to sigblock, but
  * much more efficient and portable.  (But hacking the kernel is so much
  * more fun than worrying about efficiency and portability. :-))
  */
@@ -233,18 +273,17 @@ static void
 raise_interrupt(void)
 {
        int i;
-       sigset_t mask;
 
        intpending = 0;
-       /* Signal is not automatically re-enabled after it is raised,
-        * do it ourself */
-       sigemptyset(&mask);
-       sigprocmask(SIG_SETMASK, &mask, 0);
+       /* Signal is not automatically unmasked after it is raised,
+        * do it ourself - unmask all signals */
+       sigprocmask_allsigs(SIG_UNBLOCK);
        /* pendingsig = 0; - now done in onsig() */
 
        i = EXSIG;
        if (gotsig[SIGINT - 1] && !trap[SIGINT]) {
                if (!(rootshell && iflag)) {
+                       /* Kill ourself with SIGINT */
                        signal(SIGINT, SIG_DFL);
                        raise(SIGINT);
                }
@@ -297,15 +336,6 @@ force_int_on(void)
                        raise_interrupt(); \
        } while (0)
 
-#define EXSIGON \
-       do { \
-               exsig++; \
-               xbarrier(); \
-               if (pendingsig) \
-                       raise_exception(EXSIG); \
-       } while (0)
-/* EXSIG is turned off by evalbltin(). */
-
 /*
  * Ignore a signal. Only one usage site - in forkchild()
  */
@@ -327,10 +357,10 @@ onsig(int signo)
        gotsig[signo - 1] = 1;
        pendingsig = signo;
 
-       if (exsig || (signo == SIGINT && !trap[SIGINT])) {
+       if (/* exsig || */ (signo == SIGINT && !trap[SIGINT])) {
                if (!suppressint) {
                        pendingsig = 0;
-                       raise_interrupt();
+                       raise_interrupt(); /* does not return */
                }
                intpending = 1;
        }
@@ -436,16 +466,21 @@ out2str(const char *p)
 #define VSQUOTE 0x80            /* inside double quotes--suppress splitting */
 
 /* values of VSTYPE field */
-#define VSNORMAL        0x1             /* normal variable:  $var or ${var} */
-#define VSMINUS         0x2             /* ${var-text} */
-#define VSPLUS          0x3             /* ${var+text} */
-#define VSQUESTION      0x4             /* ${var?message} */
-#define VSASSIGN        0x5             /* ${var=text} */
-#define VSTRIMRIGHT     0x6             /* ${var%pattern} */
-#define VSTRIMRIGHTMAX  0x7             /* ${var%%pattern} */
-#define VSTRIMLEFT      0x8             /* ${var#pattern} */
-#define VSTRIMLEFTMAX   0x9             /* ${var##pattern} */
-#define VSLENGTH        0xa             /* ${#var} */
+#define VSNORMAL        0x1     /* normal variable:  $var or ${var} */
+#define VSMINUS         0x2     /* ${var-text} */
+#define VSPLUS          0x3     /* ${var+text} */
+#define VSQUESTION      0x4     /* ${var?message} */
+#define VSASSIGN        0x5     /* ${var=text} */
+#define VSTRIMRIGHT     0x6     /* ${var%pattern} */
+#define VSTRIMRIGHTMAX  0x7     /* ${var%%pattern} */
+#define VSTRIMLEFT      0x8     /* ${var#pattern} */
+#define VSTRIMLEFTMAX   0x9     /* ${var##pattern} */
+#define VSLENGTH        0xa     /* ${#var} */
+#if ENABLE_ASH_BASH_COMPAT
+#define VSSUBSTR        0xc     /* ${var:position:length} */
+#define VSREPLACE       0xd     /* ${var/pattern/replacement} */
+#define VSREPLACEALL    0xe     /* ${var//pattern/replacement} */
+#endif
 
 static const char dolatstr[] ALIGN1 = {
        CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
@@ -977,7 +1012,7 @@ struct parsefile {
 };
 
 static struct parsefile basepf;         /* top level input file */
-static struct parsefile *parsefile = &basepf;  /* current input file */
+static struct parsefile *g_parsefile = &basepf;  /* current input file */
 static int startlinno;                 /* line # where last token started */
 static char *commandname;              /* currently executing command */
 static struct strlist *cmdenviron;     /* environment for builtin command */
@@ -993,7 +1028,7 @@ ash_vmsg(const char *msg, va_list ap)
        if (commandname) {
                if (strcmp(arg0, commandname))
                        fprintf(stderr, "%s: ", commandname);
-               if (!iflag || parsefile->fd)
+               if (!iflag || g_parsefile->fd)
                        fprintf(stderr, "line %d: ", startlinno);
        }
        vfprintf(stderr, msg, ap);
@@ -1090,7 +1125,7 @@ enum {
         * on many machines.  */
        SHELL_SIZE = sizeof(union {int i; char *cp; double d; }) - 1,
        /* Minimum size of a block */
-       MINSIZE  = SHELL_ALIGN(504),
+       MINSIZE = SHELL_ALIGN(504),
 };
 
 struct stack_block {
@@ -1105,16 +1140,38 @@ struct stackmark {
        struct stackmark *marknext;
 };
 
-static struct stack_block stackbase;
-static struct stack_block *stackp = &stackbase;
-static struct stackmark *markp;
-static char *stacknxt = stackbase.space;
-static size_t stacknleft = MINSIZE;
-static char *sstrend = stackbase.space + MINSIZE;
-static int herefd = -1;
 
-#define stackblock() ((void *)stacknxt)
-#define stackblocksize() stacknleft
+struct globals_memstack {
+       struct stack_block *g_stackp; // = &stackbase;
+       struct stackmark *markp;
+       char *g_stacknxt; // = stackbase.space;
+       char *sstrend; // = stackbase.space + MINSIZE;
+       size_t g_stacknleft; // = MINSIZE;
+       int    herefd; // = -1;
+       struct stack_block stackbase;
+};
+extern struct globals_memstack *const ash_ptr_to_globals_memstack;
+#define G_memstack (*ash_ptr_to_globals_memstack)
+#define g_stackp     (G_memstack.g_stackp    )
+#define markp        (G_memstack.markp       )
+#define g_stacknxt   (G_memstack.g_stacknxt  )
+#define sstrend      (G_memstack.sstrend     )
+#define g_stacknleft (G_memstack.g_stacknleft)
+#define herefd       (G_memstack.herefd      )
+#define stackbase    (G_memstack.stackbase   )
+#define INIT_G_memstack() do { \
+       (*(struct globals_memstack**)&ash_ptr_to_globals_memstack) = xzalloc(sizeof(G_memstack)); \
+       barrier(); \
+       g_stackp = &stackbase; \
+       g_stacknxt = stackbase.space; \
+       g_stacknleft = MINSIZE; \
+       sstrend = stackbase.space + MINSIZE; \
+       herefd = -1; \
+} while (0)
+
+#define stackblock()     ((void *)g_stacknxt)
+#define stackblocksize() g_stacknleft
+
 
 static void *
 ckrealloc(void * p, size_t nbytes)
@@ -1131,6 +1188,12 @@ ckmalloc(size_t nbytes)
        return ckrealloc(NULL, nbytes);
 }
 
+static void *
+ckzalloc(size_t nbytes)
+{
+       return memset(ckmalloc(nbytes), 0, nbytes);
+}
+
 /*
  * Make a copy of a string in safe storage.
  */
@@ -1158,7 +1221,7 @@ stalloc(size_t nbytes)
        size_t aligned;
 
        aligned = SHELL_ALIGN(nbytes);
-       if (aligned > stacknleft) {
+       if (aligned > g_stacknleft) {
                size_t len;
                size_t blocksize;
                struct stack_block *sp;
@@ -1171,30 +1234,36 @@ stalloc(size_t nbytes)
                        ash_msg_and_raise_error(bb_msg_memory_exhausted);
                INT_OFF;
                sp = ckmalloc(len);
-               sp->prev = stackp;
-               stacknxt = sp->space;
-               stacknleft = blocksize;
-               sstrend = stacknxt + blocksize;
-               stackp = sp;
+               sp->prev = g_stackp;
+               g_stacknxt = sp->space;
+               g_stacknleft = blocksize;
+               sstrend = g_stacknxt + blocksize;
+               g_stackp = sp;
                INT_ON;
        }
-       p = stacknxt;
-       stacknxt += aligned;
-       stacknleft -= aligned;
+       p = g_stacknxt;
+       g_stacknxt += aligned;
+       g_stacknleft -= aligned;
        return p;
 }
 
+static void *
+stzalloc(size_t nbytes)
+{
+       return memset(stalloc(nbytes), 0, nbytes);
+}
+
 static void
 stunalloc(void *p)
 {
 #if DEBUG
-       if (!p || (stacknxt < (char *)p) || ((char *)p < stackp->space)) {
-               write(2, "stunalloc\n", 10);
+       if (!p || (g_stacknxt < (char *)p) || ((char *)p < g_stackp->space)) {
+               write(STDERR_FILENO, "stunalloc\n", 10);
                abort();
        }
 #endif
-       stacknleft += stacknxt - (char *)p;
-       stacknxt = p;
+       g_stacknleft += g_stacknxt - (char *)p;
+       g_stacknxt = p;
 }
 
 /*
@@ -1210,9 +1279,9 @@ ststrdup(const char *p)
 static void
 setstackmark(struct stackmark *mark)
 {
-       mark->stackp = stackp;
-       mark->stacknxt = stacknxt;
-       mark->stacknleft = stacknleft;
+       mark->stackp = g_stackp;
+       mark->stacknxt = g_stacknxt;
+       mark->stacknleft = g_stacknleft;
        mark->marknext = markp;
        markp = mark;
 }
@@ -1227,13 +1296,13 @@ popstackmark(struct stackmark *mark)
 
        INT_OFF;
        markp = mark->marknext;
-       while (stackp != mark->stackp) {
-               sp = stackp;
-               stackp = sp->prev;
+       while (g_stackp != mark->stackp) {
+               sp = g_stackp;
+               g_stackp = sp->prev;
                free(sp);
        }
-       stacknxt = mark->stacknxt;
-       stacknleft = mark->stacknleft;
+       g_stacknxt = mark->stacknxt;
+       g_stacknleft = mark->stacknleft;
        sstrend = mark->stacknxt + mark->stacknleft;
        INT_ON;
 }
@@ -1252,13 +1321,13 @@ growstackblock(void)
 {
        size_t newlen;
 
-       newlen = stacknleft * 2;
-       if (newlen < stacknleft)
+       newlen = g_stacknleft * 2;
+       if (newlen < g_stacknleft)
                ash_msg_and_raise_error(bb_msg_memory_exhausted);
        if (newlen < 128)
                newlen += 128;
 
-       if (stacknxt == stackp->space && stackp != &stackbase) {
+       if (g_stacknxt == g_stackp->space && g_stackp != &stackbase) {
                struct stack_block *oldstackp;
                struct stackmark *xmark;
                struct stack_block *sp;
@@ -1266,15 +1335,15 @@ growstackblock(void)
                size_t grosslen;
 
                INT_OFF;
-               oldstackp = stackp;
-               sp = stackp;
+               oldstackp = g_stackp;
+               sp = g_stackp;
                prevstackp = sp->prev;
                grosslen = newlen + sizeof(struct stack_block) - MINSIZE;
                sp = ckrealloc(sp, grosslen);
                sp->prev = prevstackp;
-               stackp = sp;
-               stacknxt = sp->space;
-               stacknleft = newlen;
+               g_stackp = sp;
+               g_stacknxt = sp->space;
+               g_stacknleft = newlen;
                sstrend = sp->space + newlen;
 
                /*
@@ -1283,20 +1352,20 @@ growstackblock(void)
                 */
                xmark = markp;
                while (xmark != NULL && xmark->stackp == oldstackp) {
-                       xmark->stackp = stackp;
-                       xmark->stacknxt = stacknxt;
-                       xmark->stacknleft = stacknleft;
+                       xmark->stackp = g_stackp;
+                       xmark->stacknxt = g_stacknxt;
+                       xmark->stacknleft = g_stacknleft;
                        xmark = xmark->marknext;
                }
                INT_ON;
        } else {
-               char *oldspace = stacknxt;
-               int oldlen = stacknleft;
+               char *oldspace = g_stacknxt;
+               size_t oldlen = g_stacknleft;
                char *p = stalloc(newlen);
 
                /* free the space we just allocated */
-               stacknxt = memcpy(p, oldspace, oldlen);
-               stacknleft += newlen;
+               g_stacknxt = memcpy(p, oldspace, oldlen);
+               g_stacknleft += newlen;
        }
 }
 
@@ -1304,8 +1373,8 @@ static void
 grabstackblock(size_t len)
 {
        len = SHELL_ALIGN(len);
-       stacknxt += len;
-       stacknleft -= len;
+       g_stacknxt += len;
+       g_stacknleft -= len;
 }
 
 /*
@@ -1334,7 +1403,7 @@ growstackstr(void)
                return stackblock();
        }
        growstackblock();
-       return stackblock() + len;
+       return (char *)stackblock() + len;
 }
 
 /*
@@ -1343,7 +1412,7 @@ growstackstr(void)
 static char *
 makestrspace(size_t newlen, char *p)
 {
-       size_t len = p - stacknxt;
+       size_t len = p - g_stacknxt;
        size_t size = stackblocksize();
 
        for (;;) {
@@ -1355,14 +1424,14 @@ makestrspace(size_t newlen, char *p)
                        break;
                growstackblock();
        }
-       return stackblock() + len;
+       return (char *)stackblock() + len;
 }
 
 static char *
 stack_nputstr(const char *s, size_t n, char *p)
 {
        p = makestrspace(n, p);
-       p = memcpy(p, s, n) + n;
+       p = (char *)memcpy(p, s, n) + n;
        return p;
 }
 
@@ -1391,19 +1460,19 @@ _STPUTC(int c, char *p)
                if (l > m) \
                        (p) = makestrspace(l, q); \
        } while (0)
-#define USTPUTC(c, p)           (*p++ = (c))
+#define USTPUTC(c, p)           (*(p)++ = (c))
 #define STACKSTRNUL(p) \
        do { \
                if ((p) == sstrend) \
-                       p = growstackstr(); \
-               *p = '\0'; \
+                       (p) = growstackstr(); \
+               *(p) = '\0'; \
        } while (0)
-#define STUNPUTC(p)             (--p)
-#define STTOPC(p)               (p[-1])
-#define STADJUST(amount, p)     (p += (amount))
+#define STUNPUTC(p)             (--(p))
+#define STTOPC(p)               ((p)[-1])
+#define STADJUST(amount, p)     ((p) += (amount))
 
 #define grabstackstr(p)         stalloc((char *)(p) - (char *)stackblock())
-#define ungrabstackstr(s, p)    stunalloc((s))
+#define ungrabstackstr(s, p)    stunalloc(s)
 #define stackstrend()           ((void *)sstrend)
 
 
@@ -1417,7 +1486,7 @@ prefix(const char *string, const char *pfx)
 {
        while (*pfx) {
                if (*pfx++ != *string++)
-                       return 0;
+                       return NULL;
        }
        return (char *) string;
 }
@@ -1467,7 +1536,7 @@ single_quote(const char *s)
                q = p = makestrspace(len + 3, p);
 
                *q++ = '\'';
-               q = memcpy(q, s, len) + len;
+               q = (char *)memcpy(q, s, len) + len;
                *q++ = '\'';
                s += len;
 
@@ -1480,7 +1549,7 @@ single_quote(const char *s)
                q = p = makestrspace(len + 3, p);
 
                *q++ = '"';
-               q = memcpy(q, s, len) + len;
+               q = (char *)memcpy(q, s, len) + len;
                *q++ = '"';
                s += len;
 
@@ -1526,7 +1595,7 @@ nextopt(const char *optstring)
                        return '\0';
        }
        c = *p++;
-       for (q = optstring; *q != c; ) {
+       for (q = optstring; *q != c;) {
                if (*q == '\0')
                        ash_msg_and_raise_error("illegal option -%c", c);
                if (*++q == ':')
@@ -1568,41 +1637,26 @@ static unsigned long rseed;
 
 /* ============ Shell variables */
 
-/* flags */
-#define VEXPORT         0x01    /* variable is exported */
-#define VREADONLY       0x02    /* variable cannot be modified */
-#define VSTRFIXED       0x04    /* variable struct is statically allocated */
-#define VTEXTFIXED      0x08    /* text is statically allocated */
-#define VSTACK          0x10    /* text is allocated on the stack */
-#define VUNSET          0x20    /* the variable is not set */
-#define VNOFUNC         0x40    /* don't call the callback function */
-#define VNOSET          0x80    /* do not set variable - just readonly test */
-#define VNOSAVE         0x100   /* when text is on the heap before setvareq */
-#ifdef DYNAMIC_VAR
-# define VDYNAMIC       0x200   /* dynamic variable */
-#else
-# define VDYNAMIC       0
-#endif
-
-#ifdef IFS_BROKEN
-static const char defifsvar[] ALIGN1 = "IFS= \t\n";
-#define defifs (defifsvar + 4)
-#else
-static const char defifs[] ALIGN1 = " \t\n";
-#endif
+/*
+ * The parsefile structure pointed to by the global variable parsefile
+ * contains information about the current file being read.
+ */
+struct redirtab {
+       struct redirtab *next;
+       int renamed[10];
+       int nullredirs;
+};
 
 struct shparam {
        int nparam;             /* # of positional parameters (without $0) */
-       unsigned char malloc;   /* if parameter list dynamically allocated */
-       char **p;               /* parameter list */
 #if ENABLE_ASH_GETOPTS
        int optind;             /* next parameter to be processed by getopts */
        int optoff;             /* used by getopts */
 #endif
+       unsigned char malloced; /* if parameter list dynamically allocated */
+       char **p;               /* parameter list */
 };
 
-static struct shparam shellparam;      /* $@ current positional parameters */
-
 /*
  * Free the list of positional parameters.
  */
@@ -1611,7 +1665,7 @@ freeparam(volatile struct shparam *param)
 {
        char **ap;
 
-       if (param->malloc) {
+       if (param->malloced) {
                for (ap = param->p; *ap; ap++)
                        free(*ap);
                free(param->p);
@@ -1619,12 +1673,7 @@ freeparam(volatile struct shparam *param)
 }
 
 #if ENABLE_ASH_GETOPTS
-static void
-getoptsreset(const char *value)
-{
-       shellparam.optind = number(value);
-       shellparam.optoff = -1;
-}
+static void getoptsreset(const char *value);
 #endif
 
 struct var {
@@ -1642,7 +1691,31 @@ struct localvar {
        const char *text;               /* saved text */
 };
 
-/* Forward decls for varinit[] */
+/* flags */
+#define VEXPORT         0x01    /* variable is exported */
+#define VREADONLY       0x02    /* variable cannot be modified */
+#define VSTRFIXED       0x04    /* variable struct is statically allocated */
+#define VTEXTFIXED      0x08    /* text is statically allocated */
+#define VSTACK          0x10    /* text is allocated on the stack */
+#define VUNSET          0x20    /* the variable is not set */
+#define VNOFUNC         0x40    /* don't call the callback function */
+#define VNOSET          0x80    /* do not set variable - just readonly test */
+#define VNOSAVE         0x100   /* when text is on the heap before setvareq */
+#ifdef DYNAMIC_VAR
+# define VDYNAMIC       0x200   /* dynamic variable */
+#else
+# define VDYNAMIC       0
+#endif
+
+#ifdef IFS_BROKEN
+static const char defifsvar[] ALIGN1 = "IFS= \t\n";
+#define defifs (defifsvar + 4)
+#else
+static const char defifs[] ALIGN1 = " \t\n";
+#endif
+
+
+/* Need to be before varinit_data[] */
 #if ENABLE_LOCALE_SUPPORT
 static void
 change_lc_all(const char *value)
@@ -1666,51 +1739,87 @@ static void changepath(const char *);
 static void change_random(const char *);
 #endif
 
-static struct var varinit[] = {
+static const struct {
+       int flags;
+       const char *text;
+       void (*func)(const char *);
+} varinit_data[] = {
 #ifdef IFS_BROKEN
-       { NULL, VSTRFIXED|VTEXTFIXED,           defifsvar,      NULL },
+       { VSTRFIXED|VTEXTFIXED       , defifsvar   , NULL            },
 #else
-       { NULL, VSTRFIXED|VTEXTFIXED|VUNSET,    "IFS\0",        NULL },
+       { VSTRFIXED|VTEXTFIXED|VUNSET, "IFS\0"     , NULL            },
 #endif
 #if ENABLE_ASH_MAIL
-       { NULL, VSTRFIXED|VTEXTFIXED|VUNSET,    "MAIL\0",       changemail },
-       { NULL, VSTRFIXED|VTEXTFIXED|VUNSET,    "MAILPATH\0",   changemail },
+       { VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL\0"    , changemail      },
+       { VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH\0", changemail      },
 #endif
-       { NULL, VSTRFIXED|VTEXTFIXED,           bb_PATH_root_path, changepath },
-       { NULL, VSTRFIXED|VTEXTFIXED,           "PS1=$ ",       NULL },
-       { NULL, VSTRFIXED|VTEXTFIXED,           "PS2=> ",       NULL },
-       { NULL, VSTRFIXED|VTEXTFIXED,           "PS4=+ ",       NULL },
+       { VSTRFIXED|VTEXTFIXED       , bb_PATH_root_path, changepath },
+       { VSTRFIXED|VTEXTFIXED       , "PS1=$ "    , NULL            },
+       { VSTRFIXED|VTEXTFIXED       , "PS2=> "    , NULL            },
+       { VSTRFIXED|VTEXTFIXED       , "PS4=+ "    , NULL            },
 #if ENABLE_ASH_GETOPTS
-       { NULL, VSTRFIXED|VTEXTFIXED,           "OPTIND=1",     getoptsreset },
+       { VSTRFIXED|VTEXTFIXED       , "OPTIND=1"  , getoptsreset    },
 #endif
 #if ENABLE_ASH_RANDOM_SUPPORT
-       { NULL, VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "RANDOM\0", change_random },
+       { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "RANDOM\0", change_random },
 #endif
 #if ENABLE_LOCALE_SUPPORT
-       { NULL, VSTRFIXED | VTEXTFIXED | VUNSET, "LC_ALL\0",    change_lc_all },
-       { NULL, VSTRFIXED | VTEXTFIXED | VUNSET, "LC_CTYPE\0",  change_lc_ctype },
+       { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_ALL\0"  , change_lc_all   },
+       { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_CTYPE\0", change_lc_ctype },
 #endif
 #if ENABLE_FEATURE_EDITING_SAVEHISTORY
-       { NULL, VSTRFIXED | VTEXTFIXED | VUNSET, "HISTFILE\0",  NULL },
+       { VSTRFIXED|VTEXTFIXED|VUNSET, "HISTFILE\0", NULL            },
 #endif
 };
 
-#define vifs varinit[0]
+
+struct globals_var {
+       struct shparam shellparam;      /* $@ current positional parameters */
+       struct redirtab *redirlist;
+       int g_nullredirs;
+       int preverrout_fd;   /* save fd2 before print debug if xflag is set. */
+       struct var *vartab[VTABSIZE];
+       struct var varinit[ARRAY_SIZE(varinit_data)];
+};
+extern struct globals_var *const ash_ptr_to_globals_var;
+#define G_var (*ash_ptr_to_globals_var)
+#define shellparam    (G_var.shellparam   )
+#define redirlist     (G_var.redirlist    )
+#define g_nullredirs  (G_var.g_nullredirs )
+#define preverrout_fd (G_var.preverrout_fd)
+#define vartab        (G_var.vartab       )
+#define varinit       (G_var.varinit      )
+#define INIT_G_var() do { \
+       unsigned i; \
+       (*(struct globals_var**)&ash_ptr_to_globals_var) = xzalloc(sizeof(G_var)); \
+       barrier(); \
+       for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \
+               varinit[i].flags = varinit_data[i].flags; \
+               varinit[i].text  = varinit_data[i].text; \
+               varinit[i].func  = varinit_data[i].func; \
+       } \
+} while (0)
+
+#define vifs      varinit[0]
 #if ENABLE_ASH_MAIL
-#define vmail (&vifs)[1]
-#define vmpath (&vmail)[1]
+# define vmail    (&vifs)[1]
+# define vmpath   (&vmail)[1]
+# define vpath    (&vmpath)[1]
 #else
-#define vmpath vifs
+# define vpath    (&vifs)[1]
 #endif
-#define vpath (&vmpath)[1]
-#define vps1 (&vpath)[1]
-#define vps2 (&vps1)[1]
-#define vps4 (&vps2)[1]
-#define voptind (&vps4)[1]
+#define vps1      (&vpath)[1]
+#define vps2      (&vps1)[1]
+#define vps4      (&vps2)[1]
 #if ENABLE_ASH_GETOPTS
-#define vrandom (&voptind)[1]
+# define voptind  (&vps4)[1]
+# if ENABLE_ASH_RANDOM_SUPPORT
+#  define vrandom (&voptind)[1]
+# endif
 #else
-#define vrandom (&vps4)[1]
+# if ENABLE_ASH_RANDOM_SUPPORT
+#  define vrandom (&vps4)[1]
+# endif
 #endif
 
 /*
@@ -1720,37 +1829,32 @@ static struct var varinit[] = {
  */
 #define ifsval()        (vifs.text + 4)
 #define ifsset()        ((vifs.flags & VUNSET) == 0)
-#define mailval()       (vmail.text + 5)
-#define mpathval()      (vmpath.text + 9)
+#if ENABLE_ASH_MAIL
+# define mailval()      (vmail.text + 5)
+# define mpathval()     (vmpath.text + 9)
+# define mpathset()     ((vmpath.flags & VUNSET) == 0)
+#endif
 #define pathval()       (vpath.text + 5)
 #define ps1val()        (vps1.text + 4)
 #define ps2val()        (vps2.text + 4)
 #define ps4val()        (vps4.text + 4)
-#define optindval()     (voptind.text + 7)
-
-#define mpathset()      ((vmpath.flags & VUNSET) == 0)
-
-/*
- * The parsefile structure pointed to by the global variable parsefile
- * contains information about the current file being read.
- */
-struct redirtab {
-       struct redirtab *next;
-       int renamed[10];
-       int nullredirs;
-};
-
-static struct redirtab *redirlist;
-static int nullredirs;
-static int preverrout_fd;   /* save fd2 before print debug if xflag is set. */
-
-#define VTABSIZE 39
+#if ENABLE_ASH_GETOPTS
+# define optindval()    (voptind.text + 7)
+#endif
 
-static struct var *vartab[VTABSIZE];
 
 #define is_name(c)      ((c) == '_' || isalpha((unsigned char)(c)))
 #define is_in_name(c)   ((c) == '_' || isalnum((unsigned char)(c)))
 
+#if ENABLE_ASH_GETOPTS
+static void
+getoptsreset(const char *value)
+{
+       shellparam.optind = number(value);
+       shellparam.optoff = -1;
+}
+#endif
+
 /*
  * Return of a legal variable name (a letter or underscore followed by zero or
  * more letters, underscores, and digits).
@@ -1939,9 +2043,9 @@ setvareq(char *s, int flags)
                if (flags & VNOSET)
                        return;
                /* not found */
-               vp = ckmalloc(sizeof(*vp));
+               vp = ckzalloc(sizeof(*vp));
                vp->next = *vpp;
-               vp->func = NULL;
+               /*vp->func = NULL; - ckzalloc did it */
                *vpp = vp;
        }
        if (!(flags & (VTEXTFIXED|VSTACK|VNOSAVE)))
@@ -1975,10 +2079,10 @@ setvar(const char *name, const char *val, int flags)
        }
        INT_OFF;
        nameeq = ckmalloc(namelen + vallen + 2);
-       p = memcpy(nameeq, name, namelen) + namelen;
+       p = (char *)memcpy(nameeq, name, namelen) + namelen;
        if (val) {
                *p++ = '=';
-               p = memcpy(p, val, vallen) + vallen;
+               p = (char *)memcpy(p, val, vallen) + vallen;
        }
        *p = '\0';
        setvareq(nameeq, flags | VNOSAVE);
@@ -2126,7 +2230,8 @@ padvance(const char **path, const char *name)
        if (*path == NULL)
                return NULL;
        start = *path;
-       for (p = start; *p && *p != ':' && *p != '%'; p++);
+       for (p = start; *p && *p != ':' && *p != '%'; p++)
+               continue;
        len = p - start + strlen(name) + 2;     /* "2" is for '/' and '\0' */
        while (stackblocksize() < len)
                growstackblock();
@@ -2140,7 +2245,8 @@ padvance(const char **path, const char *name)
        pathopt = NULL;
        if (*p == '%') {
                pathopt = ++p;
-               while (*p && *p != ':') p++;
+               while (*p && *p != ':')
+                       p++;
        }
        if (*p == ':')
                *path = p + 1;
@@ -2221,9 +2327,6 @@ setprompt(int whichprompt)
 
 static int docd(const char *, int);
 
-static char *curdir = nullstr;          /* current working directory */
-static char *physdir = nullstr;         /* physical working directory */
-
 static int
 cdopt(void)
 {
@@ -2261,7 +2364,7 @@ updatepwd(const char *dir)
                new = stack_putstr(curdir, new);
        }
        new = makestrspace(strlen(dir) + 2, new);
-       lim = stackblock() + 1;
+       lim = (char *)stackblock() + 1;
        if (*dir != '/') {
                if (new[-1] != '/')
                        USTPUTC('/', new);
@@ -2310,7 +2413,7 @@ updatepwd(const char *dir)
 static char *
 getpwd(void)
 {
-       char *dir = getcwd(0, 0);
+       char *dir = getcwd(NULL, 0); /* huh, using glibc extension? */
        return dir ? dir : nullstr;
 }
 
@@ -2376,7 +2479,7 @@ docd(const char *dest, int flags)
 }
 
 static int
-cdcmd(int argc, char **argv)
+cdcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
 {
        const char *dest;
        const char *path;
@@ -2440,7 +2543,7 @@ cdcmd(int argc, char **argv)
 }
 
 static int
-pwdcmd(int argc, char **argv)
+pwdcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
 {
        int flags;
        const char *dir = curdir;
@@ -2584,10 +2687,10 @@ SIT(int c, int syntax)
        ) {
                return CCTL;
        } else {
-               s = strchr(spec_symbls, c);
-               if (s == NULL || *s == '\0')
+               s = strchrnul(spec_symbls, c);
+               if (*s == '\0')
                        return CWORD;
-               indx = syntax_index_table[(s - spec_symbls)];
+               indx = syntax_index_table[s - spec_symbls];
        }
        return S_I_T[indx][syntax];
 }
@@ -2903,8 +3006,6 @@ static const char syntax_index_table[258] = {
 #define ALIASINUSE 1
 #define ALIASDEAD  2
 
-#define ATABSIZE 39
-
 struct alias {
        struct alias *next;
        char *name;
@@ -2912,7 +3013,12 @@ struct alias {
        int flag;
 };
 
-static struct alias *atab[ATABSIZE];
+
+static struct alias **atab; // [ATABSIZE];
+#define INIT_G_alias() do { \
+       atab = xzalloc(ATABSIZE * sizeof(atab[0])); \
+} while (0)
+
 
 static struct alias **
 __lookupalias(const char *name) {
@@ -2983,11 +3089,11 @@ setalias(const char *name, const char *val)
                ap->flag &= ~ALIASDEAD;
        } else {
                /* not found */
-               ap = ckmalloc(sizeof(struct alias));
+               ap = ckzalloc(sizeof(struct alias));
                ap->name = ckstrdup(name);
                ap->val = ckstrdup(val);
-               ap->flag = 0;
-               ap->next = 0;
+               /*ap->flag = 0; - ckzalloc did it */
+               /*ap->next = NULL;*/
                *app = ap;
        }
        INT_ON;
@@ -3039,19 +3145,20 @@ printalias(const struct alias *ap)
  * TODO - sort output
  */
 static int
-aliascmd(int argc, char **argv)
+aliascmd(int argc ATTRIBUTE_UNUSED, char **argv)
 {
        char *n, *v;
        int ret = 0;
        struct alias *ap;
 
-       if (argc == 1) {
+       if (!argv[1]) {
                int i;
 
-               for (i = 0; i < ATABSIZE; i++)
+               for (i = 0; i < ATABSIZE; i++) {
                        for (ap = atab[i]; ap; ap = ap->next) {
                                printalias(ap);
                        }
+               }
                return 0;
        }
        while ((n = *++argv) != NULL) {
@@ -3073,7 +3180,7 @@ aliascmd(int argc, char **argv)
 }
 
 static int
-unaliascmd(int argc, char **argv)
+unaliascmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
 {
        int i;
 
@@ -3146,15 +3253,18 @@ struct job {
 static pid_t backgndpid;        /* pid of last background process */
 static smallint job_warning;    /* user was warned about stopped jobs (can be 2, 1 or 0). */
 
-static struct job *makejob(union node *, int);
+static struct job *makejob(/*union node *,*/ int);
+#if !JOBS
+#define forkshell(job, node, mode) forkshell(job, mode)
+#endif
 static int forkshell(struct job *, union node *, int);
 static int waitforjob(struct job *);
 
 #if !JOBS
-enum { jobctl = 0 };
+enum { doing_jobctl = 0 };
 #define setjobctl(on) do {} while (0)
 #else
-static smallint jobctl;              /* true if doing job control */
+static smallint doing_jobctl;
 static void setjobctl(int);
 #endif
 
@@ -3170,12 +3280,11 @@ setsignal(int signo)
        struct sigaction act;
 
        t = trap[signo];
+       action = S_IGN;
        if (t == NULL)
                action = S_DFL;
        else if (*t != '\0')
                action = S_CATCH;
-       else
-               action = S_IGN;
        if (rootshell && action == S_DFL) {
                switch (signo) {
                case SIGINT:
@@ -3208,7 +3317,7 @@ setsignal(int signo)
                /*
                 * current setting unknown
                 */
-               if (sigaction(signo, 0, &act) == -1) {
+               if (sigaction(signo, NULL, &act) == -1) {
                        /*
                         * Pretend it worked; maybe we should give a warning
                         * here, but other shells don't. We don't alter
@@ -3216,19 +3325,19 @@ setsignal(int signo)
                         */
                        return;
                }
+               tsig = S_RESET; /* force to be set */
                if (act.sa_handler == SIG_IGN) {
+                       tsig = S_HARD_IGN;
                        if (mflag
                         && (signo == SIGTSTP || signo == SIGTTIN || signo == SIGTTOU)
                        ) {
                                tsig = S_IGN;   /* don't hard ignore these */
-                       } else
-                               tsig = S_HARD_IGN;
-               } else {
-                       tsig = S_RESET; /* force to be set */
+                       }
                }
        }
        if (tsig == S_HARD_IGN || tsig == action)
                return;
+       act.sa_handler = SIG_DFL;
        switch (action) {
        case S_CATCH:
                act.sa_handler = onsig;
@@ -3236,13 +3345,11 @@ setsignal(int signo)
        case S_IGN:
                act.sa_handler = SIG_IGN;
                break;
-       default:
-               act.sa_handler = SIG_DFL;
        }
        *t = action;
        act.sa_flags = 0;
        sigfillset(&act.sa_mask);
-       sigaction(signo, &act, 0);
+       sigaction_set(signo, &act);
 }
 
 /* mode flags for set_curjob */
@@ -3251,8 +3358,8 @@ setsignal(int signo)
 #define CUR_STOPPED 0
 
 /* mode flags for dowait */
-#define DOWAIT_NORMAL 0
-#define DOWAIT_BLOCK 1
+#define DOWAIT_NONBLOCK WNOHANG
+#define DOWAIT_BLOCK    0
 
 #if JOBS
 /* pgrp of shell on invocation */
@@ -3327,6 +3434,9 @@ jobno(const struct job *jp)
 /*
  * Convert a job name to a job structure.
  */
+#if !JOBS
+#define getjob(name, getctl) getjob(name)
+#endif
 static struct job *
 getjob(const char *name, int getctl)
 {
@@ -3368,6 +3478,7 @@ getjob(const char *name, int getctl)
        }
 
        if (is_number(p)) {
+// TODO: number() instead? It does error checking...
                num = atoi(p);
                if (num < njobs) {
                        jp = jobtab + num - 1;
@@ -3451,7 +3562,7 @@ setjobctl(int on)
        int fd;
        int pgrp;
 
-       if (on == jobctl || rootshell == 0)
+       if (on == doing_jobctl || rootshell == 0)
                return;
        if (on) {
                int ofd;
@@ -3461,14 +3572,17 @@ setjobctl(int on)
         * That sometimes helps to acquire controlling tty.
         * Obviously, a workaround for bugs when someone
         * failed to provide a controlling tty to bash! :) */
-                       fd += 3;
-                       while (!isatty(fd) && --fd >= 0)
-                               ;
+                       fd = 2;
+                       while (!isatty(fd))
+                               if (--fd < 0)
+                                       goto out;
                }
                fd = fcntl(fd, F_DUPFD, 10);
-               close(ofd);
+               if (ofd >= 0)
+                       close(ofd);
                if (fd < 0)
                        goto out;
+               /* fd is a tty at this point */
                close_on_exec_on(fd);
                do { /* while we are in the background */
                        pgrp = tcgetpgrp(fd);
@@ -3495,25 +3609,26 @@ setjobctl(int on)
                fd = ttyfd;
                pgrp = initialpgrp;
                /* was xtcsetpgrp, but this can make exiting ash
-                * with pty already deleted loop forever */
+                * loop forever if pty is already deleted */
                tcsetpgrp(fd, pgrp);
                setpgid(0, pgrp);
                setsignal(SIGTSTP);
                setsignal(SIGTTOU);
                setsignal(SIGTTIN);
  close:
-               close(fd);
+               if (fd >= 0)
+                       close(fd);
                fd = -1;
        }
        ttyfd = fd;
-       jobctl = on;
+       doing_jobctl = on;
 }
 
 static int
 killcmd(int argc, char **argv)
 {
+       int i = 1;
        if (argv[1] && strcmp(argv[1], "-l") != 0) {
-               int i = 1;
                do {
                        if (argv[i][0] == '%') {
                                struct job *jp = getjob(argv[i], 0);
@@ -3575,7 +3690,7 @@ restartjob(struct job *jp, int mode)
 }
 
 static int
-fg_bgcmd(int argc, char **argv)
+fg_bgcmd(int argc ATTRIBUTE_UNUSED, char **argv)
 {
        struct job *jp;
        FILE *out;
@@ -3667,24 +3782,21 @@ sprint_status(char *s, int status, int sigonly)
  * and the jobs command may give out of date information.
  */
 static int
-waitproc(int block, int *status)
+waitproc(int wait_flags, int *status)
 {
-       int flags = 0;
-
 #if JOBS
-       if (jobctl)
-               flags |= WUNTRACED;
+       if (doing_jobctl)
+               wait_flags |= WUNTRACED;
 #endif
-       if (block == 0)
-               flags |= WNOHANG;
-       return wait3(status, flags, (struct rusage *)NULL);
+       /* NB: _not_ safe_waitpid, we need to detect EINTR */
+       return waitpid(-1, status, wait_flags);
 }
 
 /*
  * Wait for a process to terminate.
  */
 static int
-dowait(int block, struct job *job)
+dowait(int wait_flags, struct job *job)
 {
        int pid;
        int status;
@@ -3692,11 +3804,17 @@ dowait(int block, struct job *job)
        struct job *thisjob;
        int state;
 
-       TRACE(("dowait(%d) called\n", block));
-       pid = waitproc(block, &status);
-       TRACE(("wait returns pid %d, status=%d\n", pid, status));
-       if (pid <= 0)
+       TRACE(("dowait(%d) called\n", wait_flags));
+       pid = waitproc(wait_flags, &status);
+       TRACE(("wait returns pid=%d, status=%d\n", pid, status));
+       if (pid <= 0) {
+               /* If we were doing blocking wait and (probably) got EINTR,
+                * check for pending sigs received while waiting.
+                * (NB: can be moved into callers if needed) */
+               if (wait_flags == DOWAIT_BLOCK && pendingsig)
+                       raise_exception(EXSIG);
                return pid;
+       }
        INT_OFF;
        thisjob = NULL;
        for (jp = curjob; jp; jp = jp->prev_job) {
@@ -3732,7 +3850,6 @@ dowait(int block, struct job *job)
 #if JOBS
        if (!WIFSTOPPED(status))
 #endif
-
                jobless--;
        goto out;
 
@@ -3762,7 +3879,7 @@ dowait(int block, struct job *job)
                len = sprint_status(s, status, 1);
                if (len) {
                        s[len] = '\n';
-                       s[len + 1] = 0;
+                       s[len + 1] = '\0';
                        out2str(s);
                }
        }
@@ -3848,8 +3965,8 @@ showjobs(FILE *out, int mode)
 
        TRACE(("showjobs(%x) called\n", mode));
 
-       /* If not even one one job changed, there is nothing to do */
-       while (dowait(DOWAIT_NORMAL, NULL) > 0)
+       /* If not even one job changed, there is nothing to do */
+       while (dowait(DOWAIT_NONBLOCK, NULL) > 0)
                continue;
 
        for (jp = curjob; jp; jp = jp->prev_job) {
@@ -3860,7 +3977,7 @@ showjobs(FILE *out, int mode)
 }
 
 static int
-jobscmd(int argc, char **argv)
+jobscmd(int argc ATTRIBUTE_UNUSED, char **argv)
 {
        int mode, m;
 
@@ -3913,13 +4030,16 @@ getstatus(struct job *job)
 }
 
 static int
-waitcmd(int argc, char **argv)
+waitcmd(int argc ATTRIBUTE_UNUSED, char **argv)
 {
        struct job *job;
        int retval;
        struct job *jp;
 
-       EXSIGON;
+//     exsig++;
+//     xbarrier();
+       if (pendingsig)
+               raise_exception(EXSIG);
 
        nextopt(nullstr);
        retval = 0;
@@ -3930,16 +4050,14 @@ waitcmd(int argc, char **argv)
                for (;;) {
                        jp = curjob;
                        while (1) {
-                               if (!jp) {
-                                       /* no running procs */
-                                       goto out;
-                               }
+                               if (!jp) /* no running procs */
+                                       goto ret;
                                if (jp->state == JOBRUNNING)
                                        break;
                                jp->waited = 1;
                                jp = jp->prev_job;
                        }
-                       dowait(DOWAIT_BLOCK, 0);
+                       dowait(DOWAIT_BLOCK, NULL);
                }
        }
 
@@ -3948,27 +4066,25 @@ waitcmd(int argc, char **argv)
                if (**argv != '%') {
                        pid_t pid = number(*argv);
                        job = curjob;
-                       goto start;
-                       do {
+                       while (1) {
+                               if (!job)
+                                       goto repeat;
                                if (job->ps[job->nprocs - 1].pid == pid)
                                        break;
                                job = job->prev_job;
- start:
-                               if (!job)
-                                       goto repeat;
-                       } while (1);
+                       }
                } else
                        job = getjob(*argv, 0);
                /* loop until process terminated or stopped */
                while (job->state == JOBRUNNING)
-                       dowait(DOWAIT_BLOCK, 0);
+                       dowait(DOWAIT_BLOCK, NULL);
                job->waited = 1;
                retval = getstatus(job);
  repeat:
                ;
        } while (*++argv);
 
out:
ret:
        return retval;
 }
 
@@ -4020,7 +4136,7 @@ growjobtab(void)
  * Called with interrupts off.
  */
 static struct job *
-makejob(union node *node, int nprocs)
+makejob(/*union node *node,*/ int nprocs)
 {
        int i;
        struct job *jp;
@@ -4035,7 +4151,7 @@ makejob(union node *node, int nprocs)
                if (jp->state != JOBDONE || !jp->waited)
                        continue;
 #if JOBS
-               if (jobctl)
+               if (doing_jobctl)
                        continue;
 #endif
                freejob(jp);
@@ -4045,7 +4161,7 @@ makejob(union node *node, int nprocs)
 #if JOBS
        /* jp->jobctl is a bitfield.
         * "jp->jobctl |= jobctl" likely to give awful code */
-       if (jobctl)
+       if (doing_jobctl)
                jp->jobctl = 1;
 #endif
        jp->prev_job = curjob;
@@ -4055,7 +4171,7 @@ makejob(union node *node, int nprocs)
        if (nprocs > 1) {
                jp->ps = ckmalloc(nprocs * sizeof(struct procstat));
        }
-       TRACE(("makejob(0x%lx, %d) returns %%%d\n", (long)node, nprocs,
+       TRACE(("makejob(%d) returns %%%d\n", nprocs,
                                jobno(jp)));
        return jp;
 }
@@ -4070,20 +4186,22 @@ static char *cmdnextc;
 static void
 cmdputs(const char *s)
 {
+       static const char vstype[VSTYPE + 1][3] = {
+               "", "}", "-", "+", "?", "=",
+               "%", "%%", "#", "##"
+               USE_ASH_BASH_COMPAT(, ":", "/", "//")
+       };
+
        const char *p, *str;
        char c, cc[2] = " ";
        char *nextc;
        int subtype = 0;
        int quoted = 0;
-       static const char vstype[VSTYPE + 1][4] = {
-               "", "}", "-", "+", "?", "=",
-               "%", "%%", "#", "##"
-       };
 
        nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc);
        p = s;
        while ((c = *p++) != 0) {
-               str = 0;
+               str = NULL;
                switch (c) {
                case CTLESC:
                        c = *p++;
@@ -4367,7 +4485,7 @@ clear_traps(void)
        char **tp;
 
        for (tp = trap; tp < &trap[NSIG]; tp++) {
-               if (*tp && **tp) {      /* trap not NULL or SIG_IGN */
+               if (*tp && **tp) {      /* trap not NULL or "" (SIG_IGN) */
                        INT_OFF;
                        free(*tp);
                        *tp = NULL;
@@ -4383,7 +4501,7 @@ static void closescript(void);
 
 /* Called after fork(), in child */
 static void
-forkchild(struct job *jp, union node *n, int mode)
+forkchild(struct job *jp, /*union node *n,*/ int mode)
 {
        int oldlvl;
 
@@ -4395,7 +4513,7 @@ forkchild(struct job *jp, union node *n, int mode)
        clear_traps();
 #if JOBS
        /* do job control only in root shell */
-       jobctl = 0;
+       doing_jobctl = 0;
        if (mode != FORK_NOJOB && jp->jobctl && !oldlvl) {
                pid_t pgrp;
 
@@ -4431,12 +4549,16 @@ forkchild(struct job *jp, union node *n, int mode)
 }
 
 /* Called after fork(), in parent */
+#if !JOBS
+#define forkparent(jp, n, mode, pid) forkparent(jp, mode, pid)
+#endif
 static void
 forkparent(struct job *jp, union node *n, int mode, pid_t pid)
 {
        TRACE(("In parent shell: child = %d\n", pid));
        if (!jp) {
-               while (jobless && dowait(DOWAIT_NORMAL, 0) > 0);
+               while (jobless && dowait(DOWAIT_NONBLOCK, NULL) > 0)
+                       continue;
                jobless++;
                return;
        }
@@ -4462,7 +4584,7 @@ forkparent(struct job *jp, union node *n, int mode, pid_t pid)
                ps->status = -1;
                ps->cmd = nullstr;
 #if JOBS
-               if (jobctl && n)
+               if (doing_jobctl && n)
                        ps->cmd = commandtext(n);
 #endif
        }
@@ -4482,7 +4604,7 @@ forkshell(struct job *jp, union node *n, int mode)
                ash_msg_and_raise_error("cannot fork");
        }
        if (pid == 0)
-               forkchild(jp, n, mode);
+               forkchild(jp, /*n,*/ mode);
        else
                forkparent(jp, n, mode, pid);
        return pid;
@@ -4529,8 +4651,8 @@ waitforjob(struct job *jp)
                 * intuit from the subprocess exit status whether a SIGINT
                 * occurred, and if so interrupt ourselves.  Yuck.  - mycroft
                 */
-               if (jp->sigint)
-                       raise(SIGINT);
+               if (jp->sigint) /* TODO: do the same with all signals */
+                       raise(SIGINT); /* ... by raise(jp->sig) instead? */
        }
        if (jp->state == JOBDONE)
 #endif
@@ -4667,7 +4789,7 @@ openhere(union node *redir)
                        full_write(pip[1], redir->nhere.doc->narg.text, len);
                else
                        expandhere(redir->nhere.doc, pip[1]);
-               _exit(0);
+               _exit(EXIT_SUCCESS);
        }
  out:
        close(pip[1]);
@@ -4793,7 +4915,7 @@ redirect(union node *redir, int flags)
        int fd;
        int newfd;
 
-       nullredirs++;
+       g_nullredirs++;
        if (!redir) {
                return;
        }
@@ -4803,10 +4925,10 @@ redirect(union node *redir, int flags)
                sv = ckmalloc(sizeof(*sv));
                sv->next = redirlist;
                redirlist = sv;
-               sv->nullredirs = nullredirs - 1;
+               sv->nullredirs = g_nullredirs - 1;
                for (i = 0; i < 10; i++)
                        sv->renamed[i] = EMPTY;
-               nullredirs = 0;
+               g_nullredirs = 0;
        }
        n = redir;
        do {
@@ -4857,7 +4979,7 @@ popredir(int drop)
        struct redirtab *rp;
        int i;
 
-       if (--nullredirs >= 0)
+       if (--g_nullredirs >= 0)
                return;
        INT_OFF;
        rp = redirlist;
@@ -4876,7 +4998,7 @@ popredir(int drop)
                }
        }
        redirlist = rp->next;
-       nullredirs = rp->nullredirs;
+       g_nullredirs = rp->nullredirs;
        free(rp);
        INT_ON;
 }
@@ -4892,7 +5014,7 @@ static void
 clearredir(int drop)
 {
        for (;;) {
-               nullredirs = 0;
+               g_nullredirs = 0;
                if (!redirlist)
                        break;
                popredir(drop);
@@ -5035,7 +5157,7 @@ _rmescapes(char *str, int flag)
                }
                q = r;
                if (len > 0) {
-                       q = memcpy(q, str, len) + len;
+                       q = (char *)memcpy(q, str, len) + len;
                }
        }
        inquotes = (flag & RMESCAPE_QUOTED) ^ RMESCAPE_QUOTED;
@@ -5130,8 +5252,8 @@ recordregion(int start, int end, int nulonly)
                ifsp = &ifsfirst;
        } else {
                INT_OFF;
-               ifsp = ckmalloc(sizeof(*ifsp));
-               ifsp->next = NULL;
+               ifsp = ckzalloc(sizeof(*ifsp));
+               /*ifsp->next = NULL; - ckzalloc did it */
                ifslastp->next = ifsp;
                INT_ON;
        }
@@ -5237,13 +5359,13 @@ exptilde(char *startp, char *p, int flag)
  */
 struct backcmd {                /* result of evalbackcmd */
        int fd;                 /* file descriptor to read from */
-       char *buf;              /* buffer */
        int nleft;              /* number of chars in buffer */
+       char *buf;              /* buffer */
        struct job *jp;         /* job structure for command */
 };
 
 /* These forward decls are needed to use "eval" code for backticks handling: */
-static int back_exitstatus; /* exit status of backquoted command */
+static smalluint back_exitstatus; /* exit status of backquoted command */
 #define EV_EXIT 01              /* exit after evaluating tree */
 static void evaltree(union node *, int);
 
@@ -5269,7 +5391,7 @@ evalbackcmd(union node *n, struct backcmd *result)
 
                if (pipe(pip) < 0)
                        ash_msg_and_raise_error("pipe call failed");
-               jp = makejob(n, 1);
+               jp = makejob(/*n,*/ 1);
                if (forkshell(jp, n, FORK_NOJOB) == 0) {
                        FORCE_INT_ON;
                        close(pip[0]);
@@ -5304,7 +5426,7 @@ expbackq(union node *cmd, int quoted, int quotes)
        char *p;
        char *dest;
        int startloc;
-       int syntax = quoted? DQSYNTAX : BASESYNTAX;
+       int syntax = quoted ? DQSYNTAX : BASESYNTAX;
        struct stackmark smark;
 
        INT_OFF;
@@ -5324,7 +5446,7 @@ expbackq(union node *cmd, int quoted, int quotes)
  read:
                if (in.fd < 0)
                        break;
-               i = safe_read(in.fd, buf, sizeof(buf));
+               i = nonblock_safe_read(in.fd, buf, sizeof(buf));
                TRACE(("expbackq: read returns %d\n", i));
                if (i <= 0)
                        break;
@@ -5415,15 +5537,19 @@ expari(int quotes)
 #endif
 
 /* argstr needs it */
-static char *evalvar(char *p, int flag);
+static char *evalvar(char *p, int flag, struct strlist *var_str_list);
 
 /*
  * Perform variable and command substitution.  If EXP_FULL is set, output CTLESC
  * characters to allow for further processing.  Otherwise treat
  * $@ like $* since no splitting will be performed.
+ *
+ * var_str_list (can be NULL) is a list of "VAR=val" strings which take precedence
+ * over shell varables. Needed for "A=a B=$A; echo $B" case - we use it
+ * for correct expansion of "B=$A" word.
  */
 static void
-argstr(char *p, int flag)
+argstr(char *p, int flag, struct strlist *var_str_list)
 {
        static const char spclchars[] ALIGN1 = {
                '=',
@@ -5525,7 +5651,7 @@ argstr(char *p, int flag)
                                        p[5] == CTLQUOTEMARK
                                ))
                        ) {
-                               p = evalvar(p + 1, flag) + 1;
+                               p = evalvar(p + 1, flag, /* var_str_list: */ NULL) + 1;
                                goto start;
                        }
                        inquotes = !inquotes;
@@ -5541,7 +5667,7 @@ argstr(char *p, int flag)
                        length++;
                        goto addquote;
                case CTLVAR:
-                       p = evalvar(p, flag);
+                       p = evalvar(p, flag, var_str_list);
                        goto start;
                case CTLBACKQ:
                        c = 0;
@@ -5562,26 +5688,67 @@ argstr(char *p, int flag)
 }
 
 static char *
-scanleft(char *startp, char *rmesc, char *rmescend, char *str, int quotes,
+scanleft(char *startp, char *rmesc, char *rmescend ATTRIBUTE_UNUSED, char *str, int quotes,
        int zero)
 {
-       char *loc;
-       char *loc2;
+// This commented out code was added by James Simmons <jsimmons@infradead.org>
+// as part of a larger change when he added support for ${var/a/b}.
+// However, it broke # and % operators:
+//
+//var=ababcdcd
+//                 ok       bad
+//echo ${var#ab}   abcdcd   abcdcd
+//echo ${var##ab}  abcdcd   abcdcd
+//echo ${var#a*b}  abcdcd   ababcdcd  (!)
+//echo ${var##a*b} cdcd     cdcd
+//echo ${var#?}    babcdcd  ababcdcd  (!)
+//echo ${var##?}   babcdcd  babcdcd
+//echo ${var#*}    ababcdcd babcdcd   (!)
+//echo ${var##*}
+//echo ${var%cd}   ababcd   ababcd
+//echo ${var%%cd}  ababcd   abab      (!)
+//echo ${var%c*d}  ababcd   ababcd
+//echo ${var%%c*d} abab     ababcdcd  (!)
+//echo ${var%?}    ababcdc  ababcdc
+//echo ${var%%?}   ababcdc  ababcdcd  (!)
+//echo ${var%*}    ababcdcd ababcdcd
+//echo ${var%%*}
+//
+// Commenting it back out helped. Remove it completely if it really
+// is not needed.
+
+       char *loc, *loc2; //, *full;
        char c;
 
        loc = startp;
        loc2 = rmesc;
        do {
-               int match;
+               int match; // = strlen(str);
                const char *s = loc2;
+
                c = *loc2;
                if (zero) {
                        *loc2 = '\0';
                        s = rmesc;
                }
-               match = pmatch(str, s);
+               match = pmatch(str, s); // this line was deleted
+
+//             // chop off end if its '*'
+//             full = strrchr(str, '*');
+//             if (full && full != str)
+//                     match--;
+//
+//             // If str starts with '*' replace with s.
+//             if ((*str == '*') && strlen(s) >= match) {
+//                     full = xstrdup(s);
+//                     strncpy(full+strlen(s)-match+1, str+1, match-1);
+//             } else
+//                     full = xstrndup(str, match);
+//             match = strncmp(s, full, strlen(full));
+//             free(full);
+//
                *loc2 = c;
-               if (match)
+               if (match) // if (!match)
                        return loc;
                if (quotes && *loc == CTLESC)
                        loc++;
@@ -5644,24 +5811,59 @@ varunset(const char *end, const char *var, const char *umsg, int varflags)
        ash_msg_and_raise_error("%.*s: %s%s", end - var - 1, var, msg, tail);
 }
 
+#if ENABLE_ASH_BASH_COMPAT
+static char *
+parse_sub_pattern(char *arg, int inquotes)
+{
+       char *idx, *repl = NULL;
+       unsigned char c;
+
+       idx = arg;
+       while (1) {
+               c = *arg;
+               if (!c)
+                       break;
+               if (c == '/') {
+                       /* Only the first '/' seen is our separator */
+                       if (!repl) {
+                               repl = idx + 1;
+                               c = '\0';
+                       }
+               }
+               *idx++ = c;
+               if (!inquotes && c == '\\' && arg[1] == '\\')
+                       arg++; /* skip both \\, not just first one */
+               arg++;
+       }
+       *idx = c; /* NUL */
+
+       return repl;
+}
+#endif /* ENABLE_ASH_BASH_COMPAT */
+
 static const char *
-subevalvar(char *p, char *str, int strloc, int subtype, int startloc, int varflags, int quotes)
+subevalvar(char *p, char *str, int strloc, int subtype,
+               int startloc, int varflags, int quotes, struct strlist *var_str_list)
 {
+       struct nodelist *saveargbackq = argbackq;
        char *startp;
        char *loc;
-       int saveherefd = herefd;
-       struct nodelist *saveargbackq = argbackq;
-       int amount;
        char *rmesc, *rmescend;
+       USE_ASH_BASH_COMPAT(char *repl = NULL;)
+       USE_ASH_BASH_COMPAT(char null = '\0';)
+       USE_ASH_BASH_COMPAT(int pos, len, orig_len;)
+       int saveherefd = herefd;
+       int amount, workloc, resetloc;
        int zero;
-       char *(*scan)(char *, char *, char *, char *, int , int);
+       char *(*scan)(char*, char*, char*, char*, int, int);
 
        herefd = -1;
-       argstr(p, subtype != VSASSIGN && subtype != VSQUESTION ? EXP_CASE : 0);
+       argstr(p, (subtype != VSASSIGN && subtype != VSQUESTION) ? EXP_CASE : 0,
+                       var_str_list);
        STPUTC('\0', expdest);
        herefd = saveherefd;
        argbackq = saveargbackq;
-       startp = stackblock() + startloc;
+       startp = (char *)stackblock() + startloc;
 
        switch (subtype) {
        case VSASSIGN:
@@ -5670,30 +5872,176 @@ subevalvar(char *p, char *str, int strloc, int subtype, int startloc, int varfla
                STADJUST(amount, expdest);
                return startp;
 
+#if ENABLE_ASH_BASH_COMPAT
+       case VSSUBSTR:
+               loc = str = stackblock() + strloc;
+// TODO: number() instead? It does error checking...
+               pos = atoi(loc);
+               len = str - startp - 1;
+
+               /* *loc != '\0', guaranteed by parser */
+               if (quotes) {
+                       char *ptr;
+
+                       /* We must adjust the length by the number of escapes we find. */
+                       for (ptr = startp; ptr < (str - 1); ptr++) {
+                               if(*ptr == CTLESC) {
+                                       len--;
+                                       ptr++;
+                               }
+                       }
+               }
+               orig_len = len;
+
+               if (*loc++ == ':') {
+// TODO: number() instead? It does error checking...
+                       len = atoi(loc);
+               } else {
+                       len = orig_len;
+                       while (*loc && *loc != ':')
+                               loc++;
+                       if (*loc++ == ':')
+// TODO: number() instead? It does error checking...
+                               len = atoi(loc);
+               }
+               if (pos >= orig_len) {
+                       pos = 0;
+                       len = 0;
+               }
+               if (len > (orig_len - pos))
+                       len = orig_len - pos;
+
+               for (str = startp; pos; str++, pos--) {
+                       if (quotes && *str == CTLESC)
+                               str++;
+               }
+               for (loc = startp; len; len--) {
+                       if (quotes && *str == CTLESC)
+                               *loc++ = *str++;
+                       *loc++ = *str++;
+               }
+               *loc = '\0';
+               amount = loc - expdest;
+               STADJUST(amount, expdest);
+               return loc;
+#endif
+
        case VSQUESTION:
                varunset(p, str, startp, varflags);
                /* NOTREACHED */
        }
+       resetloc = expdest - (char *)stackblock();
 
-       subtype -= VSTRIMRIGHT;
-#if DEBUG
-       if (subtype < 0 || subtype > 3)
-               abort();
-#endif
+       /* We'll comeback here if we grow the stack while handling
+        * a VSREPLACE or VSREPLACEALL, since our pointers into the
+        * stack will need rebasing, and we'll need to remove our work
+        * areas each time
+        */
+ USE_ASH_BASH_COMPAT(restart:)
+
+       amount = expdest - ((char *)stackblock() + resetloc);
+       STADJUST(-amount, expdest);
+       startp = (char *)stackblock() + startloc;
 
        rmesc = startp;
-       rmescend = stackblock() + strloc;
+       rmescend = (char *)stackblock() + strloc;
        if (quotes) {
                rmesc = _rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW);
                if (rmesc != startp) {
                        rmescend = expdest;
-                       startp = stackblock() + startloc;
+                       startp = (char *)stackblock() + startloc;
                }
        }
        rmescend--;
-       str = stackblock() + strloc;
+       str = (char *)stackblock() + strloc;
        preglob(str, varflags & VSQUOTE, 0);
+       workloc = expdest - (char *)stackblock();
+
+#if ENABLE_ASH_BASH_COMPAT
+       if (subtype == VSREPLACE || subtype == VSREPLACEALL) {
+               char *idx, *end, *restart_detect;
+
+               if(!repl) {
+                       repl = parse_sub_pattern(str, varflags & VSQUOTE);
+                       if (!repl)
+                               repl = &null;
+               }
+
+               /* If there's no pattern to match, return the expansion unmolested */
+               if (*str == '\0')
+                       return 0;
+
+               len = 0;
+               idx = startp;
+               end = str - 1;
+               while (idx < end) {
+                       loc = scanright(idx, rmesc, rmescend, str, quotes, 1);
+                       if (!loc) {
+                               /* No match, advance */
+                               restart_detect = stackblock();
+                               STPUTC(*idx, expdest);
+                               if (quotes && *idx == CTLESC) {
+                                       idx++;
+                                       len++;
+                                       STPUTC(*idx, expdest);
+                               }
+                               if (stackblock() != restart_detect)
+                                       goto restart;
+                               idx++;
+                               len++;
+                               rmesc++;
+                               continue;
+                       }
+
+                       if (subtype == VSREPLACEALL) {
+                               while (idx < loc) {
+                                       if (quotes && *idx == CTLESC)
+                                               idx++;
+                                       idx++;
+                                       rmesc++;
+                               }
+                       } else
+                               idx = loc;
+
+                       for (loc = repl; *loc; loc++) {
+                               restart_detect = stackblock();
+                               STPUTC(*loc, expdest);
+                               if (stackblock() != restart_detect)
+                                       goto restart;
+                               len++;
+                       }
+
+                       if (subtype == VSREPLACE) {
+                               while (*idx) {
+                                       restart_detect = stackblock();
+                                       STPUTC(*idx, expdest);
+                                       if (stackblock() != restart_detect)
+                                               goto restart;
+                                       len++;
+                                       idx++;
+                               }
+                               break;
+                       }
+               }
 
+               /* We've put the replaced text into a buffer at workloc, now
+                * move it to the right place and adjust the stack.
+                */
+               startp = stackblock() + startloc;
+               STPUTC('\0', expdest);
+               memmove(startp, stackblock() + workloc, len);
+               startp[len++] = '\0';
+               amount = expdest - ((char *)stackblock() + startloc + len - 1);
+               STADJUST(-amount, expdest);
+               return startp;
+       }
+#endif /* ENABLE_ASH_BASH_COMPAT */
+
+       subtype -= VSTRIMRIGHT;
+#if DEBUG
+       if (subtype < 0 || subtype > 7)
+               abort();
+#endif
        /* zero = subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX */
        zero = subtype >> 1;
        /* VSTRIMLEFT/VSTRIMRIGHTMAX -> scanleft */
@@ -5716,7 +6064,7 @@ subevalvar(char *p, char *str, int strloc, int subtype, int startloc, int varfla
  * Add the value of a specialized variable to the stack string.
  */
 static ssize_t
-varvalue(char *name, int varflags, int flags)
+varvalue(char *name, int varflags, int flags, struct strlist *var_str_list)
 {
        int num;
        char *p;
@@ -5807,12 +6155,38 @@ varvalue(char *name, int varflags, int flags)
        case '7':
        case '8':
        case '9':
+// TODO: number() instead? It does error checking...
                num = atoi(name);
                if (num < 0 || num > shellparam.nparam)
                        return -1;
                p = num ? shellparam.p[num - 1] : arg0;
                goto value;
        default:
+               /* NB: name has form "VAR=..." */
+
+               /* "A=a B=$A" case: var_str_list is a list of "A=a" strings
+                * which should be considered before we check variables. */
+               if (var_str_list) {
+                       unsigned name_len = (strchrnul(name, '=') - name) + 1;
+                       p = NULL;
+                       do {
+                               char *str, *eq;
+                               str = var_str_list->text;
+                               eq = strchr(str, '=');
+                               if (!eq) /* stop at first non-assignment */
+                                       break;
+                               eq++;
+                               if (name_len == (unsigned)(eq - str)
+                                && strncmp(str, name, name_len) == 0) {
+                                       p = eq;
+                                       /* goto value; - WRONG! */
+                                       /* think "A=1 A=2 B=$A" */
+                               }
+                               var_str_list = var_str_list->next;
+                       } while (var_str_list);
+                       if (p)
+                               goto value;
+               }
                p = lookupvar(name);
  value:
                if (!p)
@@ -5834,20 +6208,17 @@ varvalue(char *name, int varflags, int flags)
  * input string.
  */
 static char *
-evalvar(char *p, int flag)
+evalvar(char *p, int flag, struct strlist *var_str_list)
 {
-       int subtype;
-       int varflags;
+       char varflags;
+       char subtype;
+       char quoted;
+       char easy;
        char *var;
        int patloc;
-       int c;
        int startloc;
        ssize_t varlen;
-       int easy;
-       int quotes;
-       int quoted;
 
-       quotes = flag & (EXP_FULL | EXP_CASE);
        varflags = *p++;
        subtype = varflags & VSTYPE;
        quoted = varflags & VSQUOTE;
@@ -5857,7 +6228,7 @@ evalvar(char *p, int flag)
        p = strchr(p, '=') + 1;
 
  again:
-       varlen = varvalue(var, varflags, flag);
+       varlen = varvalue(var, varflags, flag, var_str_list);
        if (varflags & VSNUL)
                varlen--;
 
@@ -5871,7 +6242,8 @@ evalvar(char *p, int flag)
                if (varlen < 0) {
                        argstr(
                                p, flag | EXP_TILDE |
-                                       (quoted ?  EXP_QWORD : EXP_WORD)
+                                       (quoted ?  EXP_QWORD : EXP_WORD),
+                               var_str_list
                        );
                        goto end;
                }
@@ -5882,7 +6254,11 @@ evalvar(char *p, int flag)
 
        if (subtype == VSASSIGN || subtype == VSQUESTION) {
                if (varlen < 0) {
-                       if (subevalvar(p, var, 0, subtype, startloc, varflags, 0)) {
+                       if (subevalvar(p, var, /* strloc: */ 0,
+                                       subtype, startloc, varflags,
+                                       /* quotes: */ 0,
+                                       var_str_list)
+                       ) {
                                varflags &= ~VSNUL;
                                /*
                                 * Remove any recorded regions beyond
@@ -5907,10 +6283,8 @@ evalvar(char *p, int flag)
        }
 
        if (subtype == VSNORMAL) {
-               if (!easy)
-                       goto end;
- record:
-               recordregion(startloc, expdest - (char *)stackblock(), quoted);
+               if (easy)
+                       goto record;
                goto end;
        }
 
@@ -5920,6 +6294,11 @@ evalvar(char *p, int flag)
        case VSTRIMLEFTMAX:
        case VSTRIMRIGHT:
        case VSTRIMRIGHTMAX:
+#if ENABLE_ASH_BASH_COMPAT
+       case VSSUBSTR:
+       case VSREPLACE:
+       case VSREPLACEALL:
+#endif
                break;
        default:
                abort();
@@ -5933,8 +6312,11 @@ evalvar(char *p, int flag)
                 */
                STPUTC('\0', expdest);
                patloc = expdest - (char *)stackblock();
-               if (subevalvar(p, NULL, patloc, subtype,
-                               startloc, varflags, quotes) == 0) {
+               if (0 == subevalvar(p, /* str: */ NULL, patloc, subtype,
+                               startloc, varflags,
+                               /* quotes: */ flag & (EXP_FULL | EXP_CASE),
+                               var_str_list)
+               ) {
                        int amount = expdest - (
                                (char *)stackblock() + patloc - 1
                        );
@@ -5942,14 +6324,15 @@ evalvar(char *p, int flag)
                }
                /* Remove any recorded regions beyond start of variable */
                removerecordregions(startloc);
-               goto record;
+ record:
+               recordregion(startloc, expdest - (char *)stackblock(), quoted);
        }
 
  end:
        if (subtype != VSNORMAL) {      /* skip to end of alternative */
                int nesting = 1;
                for (;;) {
-                       c = *p++;
+                       char c = *p++;
                        if (c == CTLESC)
                                p++;
                        else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
@@ -6012,7 +6395,7 @@ ifsbreakup(char *string, struct arglist *arglist)
                                        continue;
                                }
                                *q = '\0';
-                               sp = stalloc(sizeof(*sp));
+                               sp = stzalloc(sizeof(*sp));
                                sp->text = start;
                                *arglist->lastp = sp;
                                arglist->lastp = &sp->next;
@@ -6025,10 +6408,11 @@ ifsbreakup(char *string, struct arglist *arglist)
                                                q = p;
                                                if (*p == CTLESC)
                                                        p++;
-                                               if (strchr(ifs, *p) == NULL ) {
+                                               if (strchr(ifs, *p) == NULL) {
                                                        p = q;
                                                        break;
-                                               } else if (strchr(defifs, *p) == NULL) {
+                                               }
+                                               if (strchr(defifs, *p) == NULL) {
                                                        if (ifsspc) {
                                                                p++;
                                                                ifsspc = 0;
@@ -6052,7 +6436,7 @@ ifsbreakup(char *string, struct arglist *arglist)
                return;
 
  add:
-       sp = stalloc(sizeof(*sp));
+       sp = stzalloc(sizeof(*sp));
        sp->text = start;
        *arglist->lastp = sp;
        arglist->lastp = &sp->next;
@@ -6084,7 +6468,7 @@ addfname(const char *name)
 {
        struct strlist *sp;
 
-       sp = stalloc(sizeof(*sp));
+       sp = stzalloc(sizeof(*sp));
        sp->text = ststrdup(name);
        *exparg.lastp = sp;
        exparg.lastp = &sp->next;
@@ -6184,7 +6568,7 @@ expmeta(char *enddir, char *name)
                p++;
        if (*p == '.')
                matchdot++;
-       while (! intpending && (dp = readdir(dirp)) != NULL) {
+       while (!intpending && (dp = readdir(dirp)) != NULL) {
                if (dp->d_name[0] == '.' && ! matchdot)
                        continue;
                if (pmatch(start, dp->d_name)) {
@@ -6216,7 +6600,7 @@ msort(struct strlist *list, int len)
                return list;
        half = len >> 1;
        p = list;
-       for (n = half; --n >= 0; ) {
+       for (n = half; --n >= 0;) {
                q = p;
                p = p->next;
        }
@@ -6269,7 +6653,7 @@ expsort(struct strlist *str)
 }
 
 static void
-expandmeta(struct strlist *str, int flag)
+expandmeta(struct strlist *str /*, int flag*/)
 {
        static const char metachars[] ALIGN1 = {
                '*', '?', '[', 0
@@ -6334,7 +6718,8 @@ expandarg(union node *arg, struct arglist *arglist, int flag)
        STARTSTACKSTR(expdest);
        ifsfirst.next = NULL;
        ifslastp = NULL;
-       argstr(arg->narg.text, flag);
+       argstr(arg->narg.text, flag,
+                       /* var_str_list: */ arglist ? arglist->list : NULL);
        p = _STPUTC('\0', expdest);
        expdest = p - 1;
        if (arglist == NULL) {
@@ -6349,11 +6734,11 @@ expandarg(union node *arg, struct arglist *arglist, int flag)
                ifsbreakup(p, &exparg);
                *exparg.lastp = NULL;
                exparg.lastp = &exparg.list;
-               expandmeta(exparg.list, flag);
+               expandmeta(exparg.list /*, flag*/);
        } else {
                if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
                        rmescapes(p);
-               sp = stalloc(sizeof(*sp));
+               sp = stzalloc(sizeof(*sp));
                sp->text = p;
                *exparg.lastp = sp;
                exparg.lastp = &sp->next;
@@ -6400,7 +6785,8 @@ casematch(union node *pattern, char *val)
        argbackq = pattern->narg.backquote;
        STARTSTACKSTR(expdest);
        ifslastp = NULL;
-       argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
+       argstr(pattern->narg.text, EXP_TILDE | EXP_CASE,
+                       /* var_str_list: */ NULL);
        STACKSTRNUL(expdest);
        result = patmatch(stackblock(), val);
        popstackmark(&smark);
@@ -6416,13 +6802,19 @@ struct builtincmd {
        /* unsigned flags; */
 };
 #define IS_BUILTIN_SPECIAL(b) ((b)->name[0] & 1)
+/* "regular" builtins always take precedence over commands,
+ * regardless of PATH=....%builtin... position */
 #define IS_BUILTIN_REGULAR(b) ((b)->name[0] & 2)
-#define IS_BUILTIN_ASSIGN(b) ((b)->name[0] & 4)
+#define IS_BUILTIN_ASSIGN(b)  ((b)->name[0] & 4)
 
 struct cmdentry {
-       int cmdtype;
+       smallint cmdtype;       /* CMDxxx */
        union param {
                int index;
+               /* index >= 0 for commands without path (slashes) */
+               /* (TODO: what exactly does the value mean? PATH position?) */
+               /* index == -1 for commands with slashes */
+               /* index == (-2 - applet_no) for NOFORK applets */
                const struct builtincmd *cmd;
                struct funcnode *func;
        } u;
@@ -6454,38 +6846,35 @@ static void find_command(char *, struct cmdentry *, int, const char *);
  * would make the command name "hash" a misnomer.
  */
 
-#define CMDTABLESIZE 31         /* should be prime */
-#define ARB 1                   /* actual size determined at run time */
-
 struct tblentry {
        struct tblentry *next;  /* next entry in hash chain */
        union param param;      /* definition of builtin function */
-       short cmdtype;          /* index identifying command */
+       smallint cmdtype;       /* CMDxxx */
        char rehash;            /* if set, cd done since entry created */
-       char cmdname[ARB];      /* name of command */
+       char cmdname[1];        /* name of command */
 };
 
-static struct tblentry *cmdtable[CMDTABLESIZE];
-static int builtinloc = -1;             /* index in path of %builtin, or -1 */
+static struct tblentry **cmdtable;
+#define INIT_G_cmdtable() do { \
+       cmdtable = xzalloc(CMDTABLESIZE * sizeof(cmdtable[0])); \
+} while (0)
+
+static int builtinloc = -1;     /* index in path of %builtin, or -1 */
+
 
 static void
-tryexec(char *cmd, char **argv, char **envp)
+tryexec(USE_FEATURE_SH_STANDALONE(int applet_no,) char *cmd, char **argv, char **envp)
 {
        int repeated = 0;
 
 #if ENABLE_FEATURE_SH_STANDALONE
-       if (strchr(cmd, '/') == NULL) {
-               const struct bb_applet *a;
-
-               a = find_applet_by_name(cmd);
-               if (a) {
-                       if (a->noexec)
-                               run_appletstruct_and_exit(a, argv);
-                       /* re-exec ourselves with the new arguments */
-                       execve(bb_busybox_exec_path, argv, envp);
-                       /* If they called chroot or otherwise made the binary no longer
-                        * executable, fall through */
-               }
+       if (applet_no >= 0) {
+               if (APPLET_IS_NOEXEC(applet_no))
+                       run_applet_no_and_exit(applet_no, argv);
+               /* re-exec ourselves with the new arguments */
+               execve(bb_busybox_exec_path, argv, envp);
+               /* If they called chroot or otherwise made the binary no longer
+                * executable, fall through */
        }
 #endif
 
@@ -6497,22 +6886,25 @@ tryexec(char *cmd, char **argv, char **envp)
 #else
        execve(cmd, argv, envp);
 #endif
-       if (repeated++) {
+       if (repeated) {
                free(argv);
-       } else if (errno == ENOEXEC) {
+               return;
+       }
+       if (errno == ENOEXEC) {
                char **ap;
                char **new;
 
                for (ap = argv; *ap; ap++)
-                       ;
-               ap = new = ckmalloc((ap - argv + 2) * sizeof(char *));
+                       continue;
+               ap = new = ckmalloc((ap - argv + 2) * sizeof(ap[0]));
                ap[1] = cmd;
                ap[0] = cmd = (char *)DEFAULT_SHELL;
                ap += 2;
                argv++;
-               while ((*ap++ = *argv++))
-                       ;
+               while ((*ap++ = *argv++) != NULL)
+                       continue;
                argv = new;
+               repeated++;
                goto repeat;
        }
 }
@@ -6521,7 +6913,6 @@ tryexec(char *cmd, char **argv, char **envp)
  * Exec a program.  Never returns.  If you change this routine, you may
  * have to change the find_command routine as well.
  */
-#define environment() listvars(VEXPORT, VUNSET, 0)
 static void shellexec(char **, const char *, int) ATTRIBUTE_NORETURN;
 static void
 shellexec(char **argv, const char *path, int idx)
@@ -6530,21 +6921,24 @@ shellexec(char **argv, const char *path, int idx)
        int e;
        char **envp;
        int exerrno;
+#if ENABLE_FEATURE_SH_STANDALONE
+       int applet_no = -1;
+#endif
 
        clearredir(1);
-       envp = environment();
-       if (strchr(argv[0], '/')
+       envp = listvars(VEXPORT, VUNSET, 0);
+       if (strchr(argv[0], '/') != NULL
 #if ENABLE_FEATURE_SH_STANDALONE
-        || find_applet_by_name(argv[0])
+        || (applet_no = find_applet_by_name(argv[0])) >= 0
 #endif
        ) {
-               tryexec(argv[0], argv, envp);
+               tryexec(USE_FEATURE_SH_STANDALONE(applet_no,) argv[0], argv, envp);
                e = errno;
        } else {
                e = ENOENT;
                while ((cmdname = padvance(&path, argv[0])) != NULL) {
                        if (--idx < 0 && pathopt == NULL) {
-                               tryexec(cmdname, argv, envp);
+                               tryexec(USE_FEATURE_SH_STANDALONE(-1,) cmdname, argv, envp);
                                if (errno != ENOENT && errno != ENOTDIR)
                                        e = errno;
                        }
@@ -6566,7 +6960,7 @@ shellexec(char **argv, const char *path, int idx)
        }
        exitstatus = exerrno;
        TRACE(("shellexec failed for %s, errno %d, suppressint %d\n",
-               argv[0], e, suppressint ));
+               argv[0], e, suppressint));
        ash_msg_and_raise(EXEXEC, "%s: %s", argv[0], errmsg(e, "not found"));
        /* NOTREACHED */
 }
@@ -6648,9 +7042,11 @@ cmdlookup(const char *name, int add)
                pp = &cmdp->next;
        }
        if (add && cmdp == NULL) {
-               cmdp = *pp = ckmalloc(sizeof(struct tblentry) - ARB
-                                       + strlen(name) + 1);
-               cmdp->next = NULL;
+               cmdp = *pp = ckzalloc(sizeof(struct tblentry)
+                               + strlen(name)
+                               /* + 1 - already done because
+                                * tblentry::cmdname is char[1] */);
+               /*cmdp->next = NULL; - ckzalloc did it */
                cmdp->cmdtype = CMDUNKNOWN;
                strcpy(cmdp->cmdname, name);
        }
@@ -6694,7 +7090,7 @@ addcmdentry(char *name, struct cmdentry *entry)
 }
 
 static int
-hashcmd(int argc, char **argv)
+hashcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
 {
        struct tblentry **pp;
        struct tblentry *cmdp;
@@ -6702,10 +7098,11 @@ hashcmd(int argc, char **argv)
        struct cmdentry entry;
        char *name;
 
-       while ((c = nextopt("r")) != '\0') {
+       if (nextopt("r") != '\0') {
                clearcmdentry(0);
                return 0;
        }
+
        if (*argptr == NULL) {
                for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) {
                        for (cmdp = *pp; cmdp; cmdp = cmdp->next) {
@@ -6715,13 +7112,16 @@ hashcmd(int argc, char **argv)
                }
                return 0;
        }
+
        c = 0;
        while ((name = *argptr) != NULL) {
                cmdp = cmdlookup(name, 0);
                if (cmdp != NULL
                 && (cmdp->cmdtype == CMDNORMAL
-                    || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)))
+                    || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0))
+               ) {
                        delete_cmd_entry();
+               }
                find_command(name, &entry, DO_ERR, pathval());
                if (entry.cmdtype == CMDUNKNOWN)
                        c = 1;
@@ -6742,12 +7142,13 @@ hashcd(void)
 
        for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) {
                for (cmdp = *pp; cmdp; cmdp = cmdp->next) {
-                       if (cmdp->cmdtype == CMDNORMAL || (
-                               cmdp->cmdtype == CMDBUILTIN &&
-                               !(IS_BUILTIN_REGULAR(cmdp->param.cmd)) &&
-                               builtinloc > 0
-                       ))
+                       if (cmdp->cmdtype == CMDNORMAL
+                        || (cmdp->cmdtype == CMDBUILTIN
+                            && !IS_BUILTIN_REGULAR(cmdp->param.cmd)
+                            && builtinloc > 0)
+                       ) {
                                cmdp->rehash = 1;
+                       }
                }
        }
 }
@@ -6759,15 +7160,14 @@ hashcd(void)
  * Called with interrupts off.
  */
 static void
-changepath(const char *newval)
+changepath(const char *new)
 {
-       const char *old, *new;
-       int idx;
+       const char *old;
        int firstchange;
+       int idx;
        int idx_bltin;
 
        old = pathval();
-       new = newval;
        firstchange = 9999;     /* assume no change */
        idx = 0;
        idx_bltin = -1;
@@ -6783,9 +7183,8 @@ changepath(const char *newval)
                        break;
                if (*new == '%' && idx_bltin < 0 && prefix(new + 1, "builtin"))
                        idx_bltin = idx;
-               if (*new == ':') {
+               if (*new == ':')
                        idx++;
-               }
                new++, old++;
        }
        if (builtinloc < 0 && idx_bltin >= 0)
@@ -6825,6 +7224,7 @@ changepath(const char *newval)
 #define TWHILE 26
 #define TBEGIN 27
 #define TEND 28
+typedef smallint token_id_t;
 
 /* first char is indicating which tokens mark the end of a list */
 static const char *const tokname_array[] = {
@@ -6943,7 +7343,7 @@ describe_command(char *command, int describe_command_verbose)
        case CMDNORMAL: {
                int j = entry.u.index;
                char *p;
-               if (j == -1) {
+               if (j < 0) {
                        p = command;
                } else {
                        do {
@@ -6992,7 +7392,7 @@ describe_command(char *command, int describe_command_verbose)
 }
 
 static int
-typecmd(int argc, char **argv)
+typecmd(int argc ATTRIBUTE_UNUSED, char **argv)
 {
        int i = 1;
        int err = 0;
@@ -7003,7 +7403,7 @@ typecmd(int argc, char **argv)
                i++;
                verbose = 0;
        }
-       while (i < argc) {
+       while (argv[i]) {
                err |= describe_command(argv[i++], verbose);
        }
        return err;
@@ -7011,7 +7411,7 @@ typecmd(int argc, char **argv)
 
 #if ENABLE_ASH_CMDCMD
 static int
-commandcmd(int argc, char **argv)
+commandcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
 {
        int c;
        enum {
@@ -7331,6 +7731,7 @@ static int evalskip;            /* set if we are skipping commands */
 #define SKIPEVAL       (1 << 4)
 static int skipcount;           /* number of levels to skip */
 static int funcnest;            /* depth of function calls */
+static int loopnest;            /* current loop nesting level */
 
 /* forward decl way out to parsing code - dotrap needs it */
 static int evalstring(char *s, int mask);
@@ -7346,7 +7747,7 @@ dotrap(void)
        char *q;
        int i;
        int savestatus;
-       int skip = 0;
+       int skip;
 
        savestatus = exitstatus;
        pendingsig = 0;
@@ -7363,10 +7764,10 @@ dotrap(void)
                skip = evalstring(p, SKIPEVAL);
                exitstatus = savestatus;
                if (skip)
-                       break;
+                       return skip;
        }
 
-       return skip;
+       return 0;
 }
 
 /* forward declarations - evaluation is fairly recursive business... */
@@ -7502,8 +7903,6 @@ static
 #endif
 void evaltreenr(union node *, int) __attribute__ ((alias("evaltree"),__noreturn__));
 
-static int loopnest;            /* current loop nesting level */
-
 static void
 evalloop(union node *n, int flags)
 {
@@ -7549,6 +7948,7 @@ evalfor(union node *n, int flags)
        struct stackmark smark;
 
        setstackmark(&smark);
+       arglist.list = NULL;
        arglist.lastp = &arglist.list;
        for (argp = n->nfor.args; argp; argp = argp->narg.next) {
                expandarg(argp, &arglist, EXP_FULL | EXP_TILDE | EXP_RECORD);
@@ -7588,6 +7988,7 @@ evalcase(union node *n, int flags)
        struct stackmark smark;
 
        setstackmark(&smark);
+       arglist.list = NULL;
        arglist.lastp = &arglist.list;
        expandarg(n->ncase.expr, &arglist, EXP_TILDE);
        exitstatus = 0;
@@ -7619,7 +8020,7 @@ evalsubshell(union node *n, int flags)
        if (!backgnd && flags & EV_EXIT && !trap[0])
                goto nofork;
        INT_OFF;
-       jp = makejob(n, 1);
+       jp = makejob(/*n,*/ 1);
        if (forkshell(jp, n, backgnd) == 0) {
                INT_ON;
                flags |= EV_EXIT;
@@ -7649,7 +8050,7 @@ expredir(union node *n)
        for (redir = n; redir; redir = redir->nfile.next) {
                struct arglist fn;
 
-               memset(&fn, 0, sizeof(fn));
+               fn.list = NULL;
                fn.lastp = &fn.list;
                switch (redir->type) {
                case NFROMTO:
@@ -7694,7 +8095,7 @@ evalpipe(union node *n, int flags)
                pipelen++;
        flags |= EV_EXIT;
        INT_OFF;
-       jp = makejob(n, pipelen);
+       jp = makejob(/*n,*/ pipelen);
        prevfd = -1;
        for (lp = n->npipe.cmdlist; lp; lp = lp->next) {
                prehash(lp->n);
@@ -7739,7 +8140,7 @@ evalpipe(union node *n, int flags)
 static void
 setinteractive(int on)
 {
-       static int is_interactive;
+       static smallint is_interactive;
 
        if (++on == is_interactive)
                return;
@@ -7765,15 +8166,6 @@ setinteractive(int on)
 #endif
 }
 
-#if ENABLE_FEATURE_EDITING_VI
-#define setvimode(on) do { \
-       if (on) line_input_state->flags |= VI_MODE; \
-       else line_input_state->flags &= ~VI_MODE; \
-} while (0)
-#else
-#define setvimode(on) viflag = 0   /* forcibly keep the option off */
-#endif
-
 static void
 optschanged(void)
 {
@@ -7782,7 +8174,14 @@ optschanged(void)
 #endif
        setinteractive(iflag);
        setjobctl(mflag);
-       setvimode(viflag);
+#if ENABLE_FEATURE_EDITING_VI
+       if (viflag)
+               line_input_state->flags |= VI_MODE;
+       else
+               line_input_state->flags &= ~VI_MODE;
+#else
+       viflag = 0; /* forcibly keep the option off */
+#endif
 }
 
 static struct localvar *localvars;
@@ -7838,7 +8237,7 @@ evalfun(struct funcnode *func, int argc, char **argv, int flags)
        savehandler = exception_handler;
        exception_handler = &jmploc;
        localvars = NULL;
-       shellparam.malloc = 0;
+       shellparam.malloced = 0;
        func->count++;
        funcnest++;
        INT_ON;
@@ -7849,7 +8248,7 @@ evalfun(struct funcnode *func, int argc, char **argv, int flags)
        shellparam.optoff = -1;
 #endif
        evaltree(&func->n, flags & EV_TESTED);
-funcdone:
+ funcdone:
        INT_OFF;
        funcnest--;
        freefunc(func);
@@ -7912,7 +8311,7 @@ mklocal(char *name)
        struct var *vp;
 
        INT_OFF;
-       lvp = ckmalloc(sizeof(struct localvar));
+       lvp = ckzalloc(sizeof(struct localvar));
        if (LONE_DASH(name)) {
                char *p;
                p = ckmalloc(sizeof(optlist));
@@ -7949,7 +8348,7 @@ mklocal(char *name)
  * The "local" command.
  */
 static int
-localcmd(int argc, char **argv)
+localcmd(int argc ATTRIBUTE_UNUSED, char **argv)
 {
        char *name;
 
@@ -7961,21 +8360,21 @@ localcmd(int argc, char **argv)
 }
 
 static int
-falsecmd(int argc, char **argv)
+falsecmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
 {
        return 1;
 }
 
 static int
-truecmd(int argc, char **argv)
+truecmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
 {
        return 0;
 }
 
 static int
-execcmd(int argc, char **argv)
+execcmd(int argc ATTRIBUTE_UNUSED, char **argv)
 {
-       if (argc > 1) {
+       if (argv[1]) {
                iflag = 0;              /* exit on error */
                mflag = 0;
                optschanged();
@@ -7988,7 +8387,7 @@ execcmd(int argc, char **argv)
  * The return command.
  */
 static int
-returncmd(int argc, char **argv)
+returncmd(int argc ATTRIBUTE_UNUSED, char **argv)
 {
        /*
         * If called outside a function, do what ksh does;
@@ -8002,19 +8401,13 @@ returncmd(int argc, char **argv)
 static int breakcmd(int, char **);
 static int dotcmd(int, char **);
 static int evalcmd(int, char **);
-#if ENABLE_ASH_BUILTIN_ECHO
-static int echocmd(int, char **);
-#endif
-#if ENABLE_ASH_BUILTIN_TEST
-static int testcmd(int, char **);
-#endif
 static int exitcmd(int, char **);
 static int exportcmd(int, char **);
 #if ENABLE_ASH_GETOPTS
 static int getoptscmd(int, char **);
 #endif
 #if !ENABLE_FEATURE_SH_EXTRA_QUIET
-static int helpcmd(int argc, char **argv);
+static int helpcmd(int, char **);
 #endif
 #if ENABLE_ASH_MATH_SUPPORT
 static int letcmd(int, char **);
@@ -8037,13 +8430,31 @@ static int ulimitcmd(int, char **);
 #define BUILTIN_REG_ASSG        "6"
 #define BUILTIN_SPEC_REG_ASSG   "7"
 
-/* make sure to keep these in proper order since it is searched via bsearch() */
+/* We do not handle [[ expr ]] bashism bash-compatibly,
+ * we make it a synonym of [ expr ].
+ * Basically, word splitting and pathname expansion should NOT be performed
+ * Examples:
+ * no word splitting:     a="a b"; [[ $a = "a b" ]]; echo $? should print "0"
+ * no pathname expansion: [[ /bin/m* = "/bin/m*" ]]; echo $? should print "0"
+ * Additional operators:
+ * || and && should work as -o and -a
+ * =~ regexp match
+ * Apart from the above, [[ expr ]] should work as [ expr ]
+ */
+
+#define echocmd   echo_main
+#define printfcmd printf_main
+#define testcmd   test_main
+
+/* Keep these in proper order since it is searched via bsearch() */
 static const struct builtincmd builtintab[] = {
        { BUILTIN_SPEC_REG      ".", dotcmd },
        { BUILTIN_SPEC_REG      ":", truecmd },
 #if ENABLE_ASH_BUILTIN_TEST
-       { BUILTIN_REGULAR       "[", testcmd },
-       { BUILTIN_REGULAR       "[[", testcmd },
+       { BUILTIN_REGULAR       "[", testcmd },
+#if ENABLE_ASH_BASH_COMPAT
+       { BUILTIN_REGULAR       "[[", testcmd },
+#endif
 #endif
 #if ENABLE_ASH_ALIAS
        { BUILTIN_REG_ASSG      "alias", aliascmd },
@@ -8084,6 +8495,9 @@ static const struct builtincmd builtintab[] = {
        { BUILTIN_NOSPEC        "let", letcmd },
 #endif
        { BUILTIN_ASSIGN        "local", localcmd },
+#if ENABLE_ASH_BUILTIN_PRINTF
+       { BUILTIN_REGULAR       "printf", printfcmd },
+#endif
        { BUILTIN_NOSPEC        "pwd", pwdcmd },
        { BUILTIN_REGULAR       "read", readcmd },
        { BUILTIN_SPEC_REG_ASSG "readonly", exportcmd },
@@ -8092,7 +8506,7 @@ static const struct builtincmd builtintab[] = {
        { BUILTIN_SPEC_REG      "shift", shiftcmd },
        { BUILTIN_SPEC_REG      "source", dotcmd },
 #if ENABLE_ASH_BUILTIN_TEST
-       { BUILTIN_REGULAR       "test", testcmd },
+       { BUILTIN_REGULAR       "test", testcmd },
 #endif
        { BUILTIN_SPEC_REG      "times", timescmd },
        { BUILTIN_SPEC_REG      "trap", trapcmd },
@@ -8107,17 +8521,25 @@ static const struct builtincmd builtintab[] = {
        { BUILTIN_REGULAR       "wait", waitcmd },
 };
 
-
-#define COMMANDCMD (builtintab + 5 + \
-       2 * ENABLE_ASH_BUILTIN_TEST + \
-       ENABLE_ASH_ALIAS + \
-       ENABLE_ASH_JOB_CONTROL)
-#define EXECCMD (builtintab + 7 + \
-       2 * ENABLE_ASH_BUILTIN_TEST + \
-       ENABLE_ASH_ALIAS + \
-       ENABLE_ASH_JOB_CONTROL + \
-       ENABLE_ASH_CMDCMD + \
-       ENABLE_ASH_BUILTIN_ECHO)
+/* Should match the above table! */
+#define COMMANDCMD (builtintab + \
+       2 + \
+       1 * ENABLE_ASH_BUILTIN_TEST + \
+       1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \
+       1 * ENABLE_ASH_ALIAS + \
+       1 * ENABLE_ASH_JOB_CONTROL + \
+       3)
+#define EXECCMD (builtintab + \
+       2 + \
+       1 * ENABLE_ASH_BUILTIN_TEST + \
+       1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \
+       1 * ENABLE_ASH_ALIAS + \
+       1 * ENABLE_ASH_JOB_CONTROL + \
+       3 + \
+       1 * ENABLE_ASH_CMDCMD + \
+       1 + \
+       ENABLE_ASH_BUILTIN_ECHO + \
+       1)
 
 /*
  * Search the table of builtin commands.
@@ -8137,7 +8559,6 @@ find_builtin(const char *name)
 /*
  * Execute a simple command.
  */
-static int back_exitstatus; /* exit status of backquoted command */
 static int
 isassignment(const char *p)
 {
@@ -8147,7 +8568,7 @@ isassignment(const char *p)
        return *q == '=';
 }
 static int
-bltincmd(int argc, char **argv)
+bltincmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
 {
        /* Preserve exitstatus of a previous possible redirection
         * as POSIX mandates */
@@ -8156,8 +8577,8 @@ bltincmd(int argc, char **argv)
 static void
 evalcommand(union node *cmd, int flags)
 {
-       static const struct builtincmd bltin = {
-               "\0\0", bltincmd
+       static const struct builtincmd null_bltin = {
+               "\0\0", bltincmd /* why three NULs? */
        };
        struct stackmark smark;
        union node *argp;
@@ -8183,7 +8604,7 @@ evalcommand(union node *cmd, int flags)
        back_exitstatus = 0;
 
        cmdentry.cmdtype = CMDBUILTIN;
-       cmdentry.u.cmd = &bltin;
+       cmdentry.u.cmd = &null_bltin;
        varlist.lastp = &varlist.list;
        *varlist.lastp = NULL;
        arglist.lastp = &arglist.list;
@@ -8259,7 +8680,7 @@ evalcommand(union node *cmd, int flags)
                        }
                        sp = arglist.list;
                }
-               full_write(preverrout_fd, "\n", 1);
+               safe_write(preverrout_fd, "\n", 1);
        }
 
        cmd_is_exec = 0;
@@ -8314,10 +8735,23 @@ evalcommand(union node *cmd, int flags)
        /* Execute the command. */
        switch (cmdentry.cmdtype) {
        default:
+#if ENABLE_FEATURE_SH_NOFORK
+       {
+               /* find_command() encodes applet_no as (-2 - applet_no) */
+               int applet_no = (- cmdentry.u.index - 2);
+               if (applet_no >= 0 && APPLET_IS_NOFORK(applet_no)) {
+                       listsetvar(varlist.list, VEXPORT|VSTACK);
+                       /* run <applet>_main() */
+                       exitstatus = run_nofork_applet(applet_no, argv);
+                       break;
+               }
+       }
+#endif
+
                /* Fork off a child process if necessary. */
                if (!(flags & EV_EXIT) || trap[0]) {
                        INT_OFF;
-                       jp = makejob(cmd, 1);
+                       jp = makejob(/*cmd,*/ 1);
                        if (forkshell(jp, cmd, FORK_FG) != 0) {
                                exitstatus = waitforjob(jp);
                                INT_ON;
@@ -8343,22 +8777,15 @@ evalcommand(union node *cmd, int flags)
                }
                if (evalbltin(cmdentry.u.cmd, argc, argv)) {
                        int exit_status;
-                       int i, j;
-
-                       i = exception;
+                       int i = exception;
                        if (i == EXEXIT)
                                goto raise;
-
                        exit_status = 2;
-                       j = 0;
                        if (i == EXINT)
-                               j = SIGINT;
+                               exit_status = 128 + SIGINT;
                        if (i == EXSIG)
-                               j = pendingsig;
-                       if (j)
-                               exit_status = j + 128;
+                               exit_status = 128 + pendingsig;
                        exitstatus = exit_status;
-
                        if (i == EXINT || spclbltin > 0) {
  raise:
                                longjmp(exception_handler->loc, 1);
@@ -8408,7 +8835,7 @@ evalbltin(const struct builtincmd *cmd, int argc, char **argv)
        exitstatus |= ferror(stdout);
        clearerr(stdout);
        commandname = savecmdname;
-       exsig = 0;
+//     exsig = 0;
        exception_handler = savehandler;
 
        return i;
@@ -8454,9 +8881,9 @@ prehash(union node *n)
  * in the standard shell so we don't make it one here.
  */
 static int
-breakcmd(int argc, char **argv)
+breakcmd(int argc ATTRIBUTE_UNUSED, char **argv)
 {
-       int n = argc > 1 ? number(argv[1]) : 1;
+       int n = argv[1] ? number(argv[1]) : 1;
 
        if (n <= 0)
                ash_msg_and_raise_error(illnum, argv[1]);
@@ -8489,7 +8916,7 @@ static int parselleft;                  /* copy of parsefile->lleft */
 /* next character in input buffer */
 static char *parsenextc;                /* copy of parsefile->nextc */
 
-static int checkkwd;
+static smallint checkkwd;
 /* values of checkkwd variable */
 #define CHKALIAS        0x1
 #define CHKKWD          0x2
@@ -8498,7 +8925,7 @@ static int checkkwd;
 static void
 popstring(void)
 {
-       struct strpush *sp = parsefile->strpush;
+       struct strpush *sp = g_parsefile->strpush;
 
        INT_OFF;
 #if ENABLE_ASH_ALIAS
@@ -8518,8 +8945,8 @@ popstring(void)
        parsenextc = sp->prevstring;
        parsenleft = sp->prevnleft;
 /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/
-       parsefile->strpush = sp->prev;
-       if (sp != &(parsefile->basestrpush))
+       g_parsefile->strpush = sp->prev;
+       if (sp != &(g_parsefile->basestrpush))
                free(sp);
        INT_ON;
 }
@@ -8528,13 +8955,13 @@ static int
 preadfd(void)
 {
        int nr;
-       char *buf =  parsefile->buf;
+       char *buf =  g_parsefile->buf;
        parsenextc = buf;
 
- retry:
 #if ENABLE_FEATURE_EDITING
-       if (!iflag || parsefile->fd)
-               nr = safe_read(parsefile->fd, buf, BUFSIZ - 1);
+ retry:
+       if (!iflag || g_parsefile->fd)
+               nr = nonblock_safe_read(g_parsefile->fd, buf, BUFSIZ - 1);
        else {
 #if ENABLE_FEATURE_TAB_COMPLETION
                line_input_state->path_lookup = pathval();
@@ -8551,19 +8978,21 @@ preadfd(void)
                        goto retry;
                }
                if (nr < 0 && errno == 0) {
-                       /* Ctrl+D presend */
+                       /* Ctrl+D pressed */
                        nr = 0;
                }
        }
 #else
-       nr = safe_read(parsefile->fd, buf, BUFSIZ - 1);
+       nr = nonblock_safe_read(g_parsefile->fd, buf, BUFSIZ - 1);
 #endif
 
+#if 0
+/* nonblock_safe_read() handles this problem */
        if (nr < 0) {
                if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
                        int flags = fcntl(0, F_GETFL);
-                       if (flags >= 0 && flags & O_NONBLOCK) {
-                               flags &=O_NONBLOCK;
+                       if (flags >= 0 && (flags & O_NONBLOCK)) {
+                               flags &= ~O_NONBLOCK;
                                if (fcntl(0, F_SETFL, flags) >= 0) {
                                        out2str("sh: turning off NDELAY mode\n");
                                        goto retry;
@@ -8571,6 +9000,7 @@ preadfd(void)
                        }
                }
        }
+#endif
        return nr;
 }
 
@@ -8590,9 +9020,9 @@ preadbuffer(void)
        int more;
        char savec;
 
-       while (parsefile->strpush) {
+       while (g_parsefile->strpush) {
 #if ENABLE_ASH_ALIAS
-               if (parsenleft == -1 && parsefile->strpush->ap &&
+               if (parsenleft == -1 && g_parsefile->strpush->ap &&
                        parsenextc[-1] != ' ' && parsenextc[-1] != '\t') {
                        return PEOA;
                }
@@ -8601,7 +9031,7 @@ preadbuffer(void)
                if (--parsenleft >= 0)
                        return signed_char2int(*parsenextc++);
        }
-       if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
+       if (parsenleft == EOF_NLEFT || g_parsefile->buf == NULL)
                return PEOF;
        flush_stdout_stderr();
 
@@ -8730,8 +9160,11 @@ pungetc(void)
  * Push a string back onto the input at this current parsefile level.
  * We handle aliases this way.
  */
+#if !ENABLE_ASH_ALIAS
+#define pushstring(s, ap) pushstring(s)
+#endif
 static void
-pushstring(char *s, void *ap)
+pushstring(char *s, struct alias *ap)
 {
        struct strpush *sp;
        size_t len;
@@ -8739,18 +9172,18 @@ pushstring(char *s, void *ap)
        len = strlen(s);
        INT_OFF;
 /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/
-       if (parsefile->strpush) {
-               sp = ckmalloc(sizeof(struct strpush));
-               sp->prev = parsefile->strpush;
-               parsefile->strpush = sp;
+       if (g_parsefile->strpush) {
+               sp = ckzalloc(sizeof(struct strpush));
+               sp->prev = g_parsefile->strpush;
+               g_parsefile->strpush = sp;
        } else
-               sp = parsefile->strpush = &(parsefile->basestrpush);
+               sp = g_parsefile->strpush = &(g_parsefile->basestrpush);
        sp->prevstring = parsenextc;
        sp->prevnleft = parsenleft;
 #if ENABLE_ASH_ALIAS
-       sp->ap = (struct alias *)ap;
+       sp->ap = ap;
        if (ap) {
-               ((struct alias *)ap)->flag |= ALIASINUSE;
+               ap->flag |= ALIASINUSE;
                sp->string = s;
        }
 #endif
@@ -8768,22 +9201,22 @@ pushfile(void)
 {
        struct parsefile *pf;
 
-       parsefile->nleft = parsenleft;
-       parsefile->lleft = parselleft;
-       parsefile->nextc = parsenextc;
-       parsefile->linno = plinno;
-       pf = ckmalloc(sizeof(*pf));
-       pf->prev = parsefile;
+       g_parsefile->nleft = parsenleft;
+       g_parsefile->lleft = parselleft;
+       g_parsefile->nextc = parsenextc;
+       g_parsefile->linno = plinno;
+       pf = ckzalloc(sizeof(*pf));
+       pf->prev = g_parsefile;
        pf->fd = -1;
-       pf->strpush = NULL;
-       pf->basestrpush.prev = NULL;
-       parsefile = pf;
+       /*pf->strpush = NULL; - ckzalloc did it */
+       /*pf->basestrpush.prev = NULL;*/
+       g_parsefile = pf;
 }
 
 static void
 popfile(void)
 {
-       struct parsefile *pf = parsefile;
+       struct parsefile *pf = g_parsefile;
 
        INT_OFF;
        if (pf->fd >= 0)
@@ -8791,12 +9224,12 @@ popfile(void)
        free(pf->buf);
        while (pf->strpush)
                popstring();
-       parsefile = pf->prev;
+       g_parsefile = pf->prev;
        free(pf);
-       parsenleft = parsefile->nleft;
-       parselleft = parsefile->lleft;
-       parsenextc = parsefile->nextc;
-       plinno = parsefile->linno;
+       parsenleft = g_parsefile->nleft;
+       parselleft = g_parsefile->lleft;
+       parsenextc = g_parsefile->nextc;
+       plinno = g_parsefile->linno;
        INT_ON;
 }
 
@@ -8806,7 +9239,7 @@ popfile(void)
 static void
 popallfiles(void)
 {
-       while (parsefile != &basepf)
+       while (g_parsefile != &basepf)
                popfile();
 }
 
@@ -8818,9 +9251,9 @@ static void
 closescript(void)
 {
        popallfiles();
-       if (parsefile->fd > 0) {
-               close(parsefile->fd);
-               parsefile->fd = 0;
+       if (g_parsefile->fd > 0) {
+               close(g_parsefile->fd);
+               g_parsefile->fd = 0;
        }
 }
 
@@ -8834,11 +9267,11 @@ setinputfd(int fd, int push)
        close_on_exec_on(fd);
        if (push) {
                pushfile();
-               parsefile->buf = 0;
+               g_parsefile->buf = 0;
        }
-       parsefile->fd = fd;
-       if (parsefile->buf == NULL)
-               parsefile->buf = ckmalloc(IBUFSIZ);
+       g_parsefile->fd = fd;
+       if (g_parsefile->buf == NULL)
+               g_parsefile->buf = ckmalloc(IBUFSIZ);
        parselleft = parsenleft = 0;
        plinno = 1;
 }
@@ -8883,7 +9316,7 @@ setinputstring(char *string)
        pushfile();
        parsenextc = string;
        parsenleft = strlen(string);
-       parsefile->buf = NULL;
+       g_parsefile->buf = NULL;
        plinno = 1;
        INT_ON;
 }
@@ -8927,7 +9360,8 @@ chkmail(void)
                        break;
                if (*p == '\0')
                        continue;
-               for (q = p; *q; q++);
+               for (q = p; *q; q++)
+                       continue;
 #if DEBUG
                if (q[-1] != '/')
                        abort();
@@ -8950,7 +9384,7 @@ chkmail(void)
 }
 
 static void
-changemail(const char *val)
+changemail(const char *val ATTRIBUTE_UNUSED)
 {
        mail_var_path_changed = 1;
 }
@@ -8970,14 +9404,15 @@ setparam(char **argv)
        char **ap;
        int nparam;
 
-       for (nparam = 0; argv[nparam]; nparam++);
+       for (nparam = 0; argv[nparam]; nparam++)
+               continue;
        ap = newparam = ckmalloc((nparam + 1) * sizeof(*ap));
        while (*argv) {
                *ap++ = ckstrdup(*argv++);
        }
        *ap = NULL;
        freeparam(&shellparam);
-       shellparam.malloc = 1;
+       shellparam.malloced = 1;
        shellparam.nparam = nparam;
        shellparam.p = newparam;
 #if ENABLE_ASH_GETOPTS
@@ -8989,9 +9424,26 @@ setparam(char **argv)
 /*
  * Process shell options.  The global variable argptr contains a pointer
  * to the argument list; we advance it past the options.
+ *
+ * SUSv3 section 2.8.1 "Consequences of Shell Errors" says:
+ * For a non-interactive shell, an error condition encountered
+ * by a special built-in ... shall cause the shell to write a diagnostic message
+ * to standard error and exit as shown in the following table:
+ * Error                                           Special Built-In
+ * ...
+ * Utility syntax error (option or operand error)  Shall exit
+ * ...
+ * However, in bug 1142 (http://busybox.net/bugs/view.php?id=1142)
+ * we see that bash does not do that (set "finishes" with error code 1 instead,
+ * and shell continues), and people rely on this behavior!
+ * Testcase:
+ * set -o barfoo 2>/dev/null
+ * echo $?
+ *
+ * Oh well. Let's mimic that.
  */
-static void
-minus_o(char *name, int val)
+static int
+plus_minus_o(char *name, int val)
 {
        int i;
 
@@ -8999,15 +9451,20 @@ minus_o(char *name, int val)
                for (i = 0; i < NOPTS; i++) {
                        if (strcmp(name, optnames(i)) == 0) {
                                optlist[i] = val;
-                               return;
+                               return 0;
                        }
                }
-               ash_msg_and_raise_error("illegal option -o %s", name);
+               ash_msg("illegal option %co %s", val ? '-' : '+', name);
+               return 1;
        }
-       out1str("Current option settings\n");
-       for (i = 0; i < NOPTS; i++)
-               out1fmt("%-16s%s\n", optnames(i),
-                               optlist[i] ? "on" : "off");
+       for (i = 0; i < NOPTS; i++) {
+               if (val) {
+                       out1fmt("%-16s%s\n", optnames(i), optlist[i] ? "on" : "off");
+               } else {
+                       out1fmt("set %co %s\n", optlist[i] ? '-' : '+', optnames(i));
+               }
+       }
+       return 0;
 }
 static void
 setoption(int flag, int val)
@@ -9020,10 +9477,10 @@ setoption(int flag, int val)
                        return;
                }
        }
-       ash_msg_and_raise_error("illegal option -%c", flag);
+       ash_msg_and_raise_error("illegal option %c%c", val ? '-' : '+', flag);
        /* NOTREACHED */
 }
-static void
+static int
 options(int cmdline)
 {
        char *p;
@@ -9058,7 +9515,10 @@ options(int cmdline)
                        if (c == 'c' && cmdline) {
                                minusc = p;     /* command is after shell args */
                        } else if (c == 'o') {
-                               minus_o(*argptr, val);
+                               if (plus_minus_o(*argptr, val)) {
+                                       /* it already printed err message */
+                                       return 1; /* error */
+                               }
                                if (*argptr)
                                        argptr++;
                        } else if (cmdline && (c == 'l')) { /* -l or +l == --login */
@@ -9073,30 +9533,32 @@ options(int cmdline)
                        }
                }
        }
+       return 0;
 }
 
 /*
  * The shift builtin command.
  */
 static int
-shiftcmd(int argc, char **argv)
+shiftcmd(int argc ATTRIBUTE_UNUSED, char **argv)
 {
        int n;
        char **ap1, **ap2;
 
        n = 1;
-       if (argc > 1)
+       if (argv[1])
                n = number(argv[1]);
        if (n > shellparam.nparam)
-               ash_msg_and_raise_error("can't shift that many");
+               n = shellparam.nparam;
        INT_OFF;
        shellparam.nparam -= n;
        for (ap1 = shellparam.p; --n >= 0; ap1++) {
-               if (shellparam.malloc)
+               if (shellparam.malloced)
                        free(*ap1);
        }
        ap2 = shellparam.p;
-       while ((*ap2++ = *ap1++) != NULL);
+       while ((*ap2++ = *ap1++) != NULL)
+               continue;
 #if ENABLE_ASH_GETOPTS
        shellparam.optind = 1;
        shellparam.optoff = -1;
@@ -9140,18 +9602,23 @@ showvars(const char *sep_prefix, int on, int off)
  * The set command builtin.
  */
 static int
-setcmd(int argc, char **argv)
+setcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
 {
-       if (argc == 1)
+       int retval;
+
+       if (!argv[1])
                return showvars(nullstr, 0, VUNSET);
        INT_OFF;
-       options(0);
-       optschanged();
-       if (*argptr != NULL) {
-               setparam(argptr);
+       retval = 1;
+       if (!options(0)) { /* if no parse error... */
+               retval = 0;
+               optschanged();
+               if (*argptr != NULL) {
+                       setparam(argptr);
+               }
        }
        INT_ON;
-       return 0;
+       return retval;
 }
 
 #if ENABLE_ASH_RANDOM_SUPPORT
@@ -9190,7 +9657,7 @@ getopts(char *optstr, char *optvar, char **optfirst, int *param_optind, int *opt
                return 1;
        optnext = optfirst + *param_optind - 1;
 
-       if (*param_optind <= 1 || *optoff < 0 || strlen(optnext[-1]) < *optoff)
+       if (*param_optind <= 1 || *optoff < 0 || (int)strlen(optnext[-1]) < *optoff)
                p = NULL;
        else
                p = optnext[-1] + *optoff;
@@ -9209,7 +9676,7 @@ getopts(char *optstr, char *optvar, char **optfirst, int *param_optind, int *opt
        }
 
        c = *p++;
-       for (q = optstr; *q != c; ) {
+       for (q = optstr; *q != c;) {
                if (*q == '\0') {
                        if (optstr[0] == ':') {
                                s[0] = c;
@@ -9299,20 +9766,28 @@ getoptscmd(int argc, char **argv)
 
 /* ============ Shell parser */
 
-/*
- * NEOF is returned by parsecmd when it encounters an end of file.  It
- * must be distinct from NULL, so we use the address of a variable that
- * happens to be handy.
- */
+struct heredoc {
+       struct heredoc *next;   /* next here document in list */
+       union node *here;       /* redirection node */
+       char *eofmark;          /* string indicating end of input */
+       smallint striptabs;     /* if set, strip leading tabs */
+};
+
 static smallint tokpushback;           /* last token pushed back */
-#define NEOF ((union node *)&tokpushback)
 static smallint parsebackquote;        /* nonzero if we are inside backquotes */
-static int lasttoken;                  /* last token read */
+static smallint quoteflag;             /* set if (part of) last token was quoted */
+static token_id_t lasttoken;           /* last token read (integer id Txxx) */
+static struct heredoc *heredoclist;    /* list of here documents to read */
 static char *wordtext;                 /* text of last word returned by readtoken */
 static struct nodelist *backquotelist;
 static union node *redirnode;
 static struct heredoc *heredoc;
-static smallint quoteflag;             /* set if (part of) last token was quoted */
+/*
+ * NEOF is returned by parsecmd when it encounters an end of file.  It
+ * must be distinct from NULL, so we use the address of a variable that
+ * happens to be handy.
+ */
+#define NEOF ((union node *)&tokpushback)
 
 static void raise_error_syntax(const char *) ATTRIBUTE_NORETURN;
 static void
@@ -9343,15 +9818,6 @@ raise_error_unexpected_syntax(int token)
 
 #define EOFMARKLEN 79
 
-struct heredoc {
-       struct heredoc *next;   /* next here document in list */
-       union node *here;               /* redirection node */
-       char *eofmark;          /* string indicating end of input */
-       int striptabs;          /* if set, strip leading tabs */
-};
-
-static struct heredoc *heredoclist;    /* list of here documents to read */
-
 /* parsing is heavily cross-recursive, need these forward decls */
 static union node *andor(void);
 static union node *pipeline(void);
@@ -9378,9 +9844,9 @@ list(int nlflag)
                                n2->npipe.backgnd = 1;
                        } else {
                                if (n2->type != NREDIR) {
-                                       n3 = stalloc(sizeof(struct nredir));
+                                       n3 = stzalloc(sizeof(struct nredir));
                                        n3->nredir.n = n2;
-                                       n3->nredir.redirect = NULL;
+                                       /*n3->nredir.redirect = NULL; - stzalloc did it */
                                        n2 = n3;
                                }
                                n2->type = NBACKGND;
@@ -9389,7 +9855,7 @@ list(int nlflag)
                if (n1 == NULL) {
                        n1 = n2;
                } else {
-                       n3 = stalloc(sizeof(struct nbinary));
+                       n3 = stzalloc(sizeof(struct nbinary));
                        n3->type = NSEMI;
                        n3->nbinary.ch1 = n1;
                        n3->nbinary.ch2 = n2;
@@ -9446,7 +9912,7 @@ andor(void)
                }
                checkkwd = CHKNL | CHKKWD | CHKALIAS;
                n2 = pipeline();
-               n3 = stalloc(sizeof(struct nbinary));
+               n3 = stzalloc(sizeof(struct nbinary));
                n3->type = t;
                n3->nbinary.ch1 = n1;
                n3->nbinary.ch2 = n2;
@@ -9470,15 +9936,15 @@ pipeline(void)
                tokpushback = 1;
        n1 = parse_command();
        if (readtoken() == TPIPE) {
-               pipenode = stalloc(sizeof(struct npipe));
+               pipenode = stzalloc(sizeof(struct npipe));
                pipenode->type = NPIPE;
-               pipenode->npipe.backgnd = 0;
-               lp = stalloc(sizeof(struct nodelist));
+               /*pipenode->npipe.backgnd = 0; - stzalloc did it */
+               lp = stzalloc(sizeof(struct nodelist));
                pipenode->npipe.cmdlist = lp;
                lp->n = n1;
                do {
                        prev = lp;
-                       lp = stalloc(sizeof(struct nodelist));
+                       lp = stzalloc(sizeof(struct nodelist));
                        checkkwd = CHKNL | CHKKWD | CHKALIAS;
                        lp->n = parse_command();
                        prev->next = lp;
@@ -9488,7 +9954,7 @@ pipeline(void)
        }
        tokpushback = 1;
        if (negate) {
-               n2 = stalloc(sizeof(struct nnot));
+               n2 = stzalloc(sizeof(struct nnot));
                n2->type = NNOT;
                n2->nnot.com = n1;
                return n2;
@@ -9501,9 +9967,9 @@ makename(void)
 {
        union node *n;
 
-       n = stalloc(sizeof(struct narg));
+       n = stzalloc(sizeof(struct narg));
        n->type = NARG;
-       n->narg.next = NULL;
+       /*n->narg.next = NULL; - stzalloc did it */
        n->narg.text = wordtext;
        n->narg.backquote = backquotelist;
        return n;
@@ -9572,7 +10038,8 @@ parsefname(void)
                if (heredoclist == NULL)
                        heredoclist = here;
                else {
-                       for (p = heredoclist; p->next; p = p->next);
+                       for (p = heredoclist; p->next; p = p->next)
+                               continue;
                        p->next = here;
                }
        } else if (n->type == NTOFD || n->type == NFROMFD) {
@@ -9590,6 +10057,9 @@ simplecmd(void)
        union node *vars, **vpp;
        union node **rpp, *redir;
        int savecheckkwd;
+#if ENABLE_ASH_BASH_COMPAT
+       smallint double_brackets_flag = 0;
+#endif
 
        args = NULL;
        app = &args;
@@ -9600,12 +10070,30 @@ simplecmd(void)
 
        savecheckkwd = CHKALIAS;
        for (;;) {
+               int t;
                checkkwd = savecheckkwd;
-               switch (readtoken()) {
+               t = readtoken();
+               switch (t) {
+#if ENABLE_ASH_BASH_COMPAT
+               case TAND: /* "&&" */
+               case TOR: /* "||" */
+                       if (!double_brackets_flag) {
+                               tokpushback = 1;
+                               goto out;
+                       }
+                       wordtext = (char *) (t == TAND ? "-a" : "-o");
+#endif
                case TWORD:
-                       n = stalloc(sizeof(struct narg));
+                       n = stzalloc(sizeof(struct narg));
                        n->type = NARG;
+                       /*n->narg.next = NULL; - stzalloc did it */
                        n->narg.text = wordtext;
+#if ENABLE_ASH_BASH_COMPAT
+                       if (strcmp("[[", wordtext) == 0)
+                               double_brackets_flag = 1;
+                       else if (strcmp("]]", wordtext) == 0)
+                               double_brackets_flag = 0;
+#endif
                        n->narg.backquote = backquotelist;
                        if (savecheckkwd && isassignment(wordtext)) {
                                *vpp = n;
@@ -9652,7 +10140,7 @@ simplecmd(void)
        *app = NULL;
        *vpp = NULL;
        *rpp = NULL;
-       n = stalloc(sizeof(struct ncmd));
+       n = stzalloc(sizeof(struct ncmd));
        n->type = NCMD;
        n->ncmd.args = args;
        n->ncmd.assign = vars;
@@ -9678,7 +10166,7 @@ parse_command(void)
                raise_error_unexpected_syntax(-1);
                /* NOTREACHED */
        case TIF:
-               n1 = stalloc(sizeof(struct nif));
+               n1 = stzalloc(sizeof(struct nif));
                n1->type = NIF;
                n1->nif.test = list(0);
                if (readtoken() != TTHEN)
@@ -9686,7 +10174,7 @@ parse_command(void)
                n1->nif.ifpart = list(0);
                n2 = n1;
                while (readtoken() == TELIF) {
-                       n2->nif.elsepart = stalloc(sizeof(struct nif));
+                       n2->nif.elsepart = stzalloc(sizeof(struct nif));
                        n2 = n2->nif.elsepart;
                        n2->type = NIF;
                        n2->nif.test = list(0);
@@ -9705,7 +10193,7 @@ parse_command(void)
        case TWHILE:
        case TUNTIL: {
                int got;
-               n1 = stalloc(sizeof(struct nbinary));
+               n1 = stzalloc(sizeof(struct nbinary));
                n1->type = (lasttoken == TWHILE) ? NWHILE : NUNTIL;
                n1->nbinary.ch1 = list(0);
                got = readtoken();
@@ -9721,15 +10209,16 @@ parse_command(void)
        case TFOR:
                if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
                        raise_error_syntax("Bad for loop variable");
-               n1 = stalloc(sizeof(struct nfor));
+               n1 = stzalloc(sizeof(struct nfor));
                n1->type = NFOR;
                n1->nfor.var = wordtext;
                checkkwd = CHKKWD | CHKALIAS;
                if (readtoken() == TIN) {
                        app = &ap;
                        while (readtoken() == TWORD) {
-                               n2 = stalloc(sizeof(struct narg));
+                               n2 = stzalloc(sizeof(struct narg));
                                n2->type = NARG;
+                               /*n2->narg.next = NULL; - stzalloc did it */
                                n2->narg.text = wordtext;
                                n2->narg.backquote = backquotelist;
                                *app = n2;
@@ -9740,11 +10229,11 @@ parse_command(void)
                        if (lasttoken != TNL && lasttoken != TSEMI)
                                raise_error_unexpected_syntax(-1);
                } else {
-                       n2 = stalloc(sizeof(struct narg));
+                       n2 = stzalloc(sizeof(struct narg));
                        n2->type = NARG;
+                       /*n2->narg.next = NULL; - stzalloc did it */
                        n2->narg.text = (char *)dolatstr;
-                       n2->narg.backquote = NULL;
-                       n2->narg.next = NULL;
+                       /*n2->narg.backquote = NULL;*/
                        n1->nfor.args = n2;
                        /*
                         * Newline or semicolon here is optional (but note
@@ -9760,15 +10249,15 @@ parse_command(void)
                t = TDONE;
                break;
        case TCASE:
-               n1 = stalloc(sizeof(struct ncase));
+               n1 = stzalloc(sizeof(struct ncase));
                n1->type = NCASE;
                if (readtoken() != TWORD)
                        raise_error_unexpected_syntax(TWORD);
-               n1->ncase.expr = n2 = stalloc(sizeof(struct narg));
+               n1->ncase.expr = n2 = stzalloc(sizeof(struct narg));
                n2->type = NARG;
+               /*n2->narg.next = NULL; - stzalloc did it */
                n2->narg.text = wordtext;
                n2->narg.backquote = backquotelist;
-               n2->narg.next = NULL;
                do {
                        checkkwd = CHKKWD | CHKALIAS;
                } while (readtoken() == TNL);
@@ -9781,12 +10270,13 @@ parse_command(void)
                while (t != TESAC) {
                        if (lasttoken == TLP)
                                readtoken();
-                       *cpp = cp = stalloc(sizeof(struct nclist));
+                       *cpp = cp = stzalloc(sizeof(struct nclist));
                        cp->type = NCLIST;
                        app = &cp->nclist.pattern;
                        for (;;) {
-                               *app = ap = stalloc(sizeof(struct narg));
+                               *app = ap = stzalloc(sizeof(struct narg));
                                ap->type = NARG;
+                               /*ap->narg.next = NULL; - stzalloc did it */
                                ap->narg.text = wordtext;
                                ap->narg.backquote = backquotelist;
                                if (readtoken() != TPIPE)
@@ -9794,7 +10284,7 @@ parse_command(void)
                                app = &ap->narg.next;
                                readtoken();
                        }
-                       ap->narg.next = NULL;
+                       //ap->narg.next = NULL;
                        if (lasttoken != TRP)
                                raise_error_unexpected_syntax(TRP);
                        cp->nclist.body = list(2);
@@ -9812,10 +10302,10 @@ parse_command(void)
                *cpp = NULL;
                goto redir;
        case TLP:
-               n1 = stalloc(sizeof(struct nredir));
+               n1 = stzalloc(sizeof(struct nredir));
                n1->type = NSUBSHELL;
                n1->nredir.n = list(0);
-               n1->nredir.redirect = NULL;
+               /*n1->nredir.redirect = NULL; - stzalloc did it */
                t = TRP;
                break;
        case TBEGIN:
@@ -9844,7 +10334,7 @@ parse_command(void)
        *rpp = NULL;
        if (redir) {
                if (n1->type != NSUBSHELL) {
-                       n2 = stalloc(sizeof(struct nredir));
+                       n2 = stzalloc(sizeof(struct nredir));
                        n2->type = NREDIR;
                        n2->nredir.n = n1;
                        n1 = n2;
@@ -9854,6 +10344,52 @@ parse_command(void)
        return n1;
 }
 
+#if ENABLE_ASH_BASH_COMPAT
+static int decode_dollar_squote(void)
+{
+       static const char C_escapes[] ALIGN1 = "nrbtfav""x\\01234567";
+       int c, cnt;
+       char *p;
+       char buf[4];
+
+       c = pgetc();
+       p = strchr(C_escapes, c);
+       if (p) {
+               buf[0] = c;
+               p = buf;
+               cnt = 3;
+               if ((unsigned char)(c - '0') <= 7) { /* \ooo */
+                       do {
+                               c = pgetc();
+                               *++p = c;
+                       } while ((unsigned char)(c - '0') <= 7 && --cnt);
+                       pungetc();
+               } else if (c == 'x') { /* \xHH */
+                       do {
+                               c = pgetc();
+                               *++p = c;
+                       } while (isxdigit(c) && --cnt);
+                       pungetc();
+                       if (cnt == 3) { /* \x but next char is "bad" */
+                               c = 'x';
+                               goto unrecognized;
+                       }
+               } else { /* simple seq like \\ or \t */
+                       p++;
+               }
+               *p = '\0';
+               p = buf;
+               c = bb_process_escape_sequence((void*)&p);
+       } else { /* unrecognized "\z": print both chars unless ' or " */
+               if (c != '\'' && c != '"') {
+ unrecognized:
+                       c |= 0x100; /* "please encode \, then me" */
+               }
+       }
+       return c;
+}
+#endif
+
 /*
  * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
  * is not NULL, read a here document.  In the latter case, eofmark is the
@@ -9865,14 +10401,12 @@ parse_command(void)
  * using goto's to implement the subroutine linkage.  The following macros
  * will run code that appears at the end of readtoken1.
  */
-
 #define CHECKEND()      {goto checkend; checkend_return:;}
 #define PARSEREDIR()    {goto parseredir; parseredir_return:;}
 #define PARSESUB()      {goto parsesub; parsesub_return:;}
 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
 #define PARSEARITH()    {goto parsearith; parsearith_return:;}
-
 static int
 readtoken1(int firstc, int syntax, char *eofmark, int striptabs)
 {
@@ -9894,6 +10428,8 @@ readtoken1(int firstc, int syntax, char *eofmark, int striptabs)
        int parenlevel;      /* levels of parens in arithmetic */
        int dqvarnest;       /* levels of variables expansion within double quotes */
 
+       USE_ASH_BASH_COMPAT(smallint bash_dollar_squote = 0;)
+
 #if __GNUC__
        /* Avoid longjmp clobbering */
        (void) &out;
@@ -9944,6 +10480,15 @@ readtoken1(int firstc, int syntax, char *eofmark, int striptabs)
                        case CCTL:
                                if (eofmark == NULL || dblquote)
                                        USTPUTC(CTLESC, out);
+#if ENABLE_ASH_BASH_COMPAT
+                               if (c == '\\' && bash_dollar_squote) {
+                                       c = decode_dollar_squote();
+                                       if (c & 0x100) {
+                                               USTPUTC('\\', out);
+                                               c = (unsigned char)c;
+                                       }
+                               }
+#endif
                                USTPUTC(c, out);
                                break;
                        case CBACK:     /* backslash */
@@ -9962,11 +10507,9 @@ readtoken1(int firstc, int syntax, char *eofmark, int striptabs)
                                                USTPUTC('\\', out);
                                        }
 #endif
-                                       if (dblquote &&
-                                               c != '\\' && c != '`' &&
-                                               c != '$' && (
-                                                       c != '"' ||
-                                                       eofmark != NULL)
+                                       if (dblquote && c != '\\'
+                                        && c != '`' && c != '$'
+                                        && (c != '"' || eofmark != NULL)
                                        ) {
                                                USTPUTC(CTLESC, out);
                                                USTPUTC('\\', out);
@@ -9989,6 +10532,7 @@ readtoken1(int firstc, int syntax, char *eofmark, int striptabs)
                                dblquote = 1;
                                goto quotemark;
                        case CENDQUOTE:
+                               USE_ASH_BASH_COMPAT(bash_dollar_squote = 0;)
                                if (eofmark != NULL && arinest == 0
                                 && varnest == 0
                                ) {
@@ -10061,7 +10605,7 @@ readtoken1(int firstc, int syntax, char *eofmark, int striptabs)
 
                        }
                        c = pgetc_macro();
-               }
+               } /* for(;;) */
        }
  endword:
 #if ENABLE_ASH_MATH_SUPPORT
@@ -10082,12 +10626,13 @@ readtoken1(int firstc, int syntax, char *eofmark, int striptabs)
                if ((c == '>' || c == '<')
                 && quotef == 0
                 && len <= 2
-                && (*out == '\0' || isdigit(*out))) {
+                && (*out == '\0' || isdigit(*out))
+               ) {
                        PARSEREDIR();
-                       return lasttoken = TREDIR;
-               } else {
-                       pungetc();
+                       lasttoken = TREDIR;
+                       return lasttoken;
                }
+               pungetc();
        }
        quoteflag = quotef;
        backquotelist = bqlist;
@@ -10119,7 +10664,8 @@ checkend: {
                                char *p, *q;
 
                                p = line;
-                               for (q = eofmark + 1; *q && *p == *q; p++, q++);
+                               for (q = eofmark + 1; *q && *p == *q; p++, q++)
+                                       continue;
                                if (*p == '\n' && *q == '\0') {
                                        c = PEOF;
                                        plinno++;
@@ -10142,7 +10688,7 @@ parseredir: {
        char fd = *out;
        union node *np;
 
-       np = stalloc(sizeof(struct nfile));
+       np = stzalloc(sizeof(struct nfile));
        if (c == '>') {
                np->nfile.fd = 1;
                c = pgetc();
@@ -10157,22 +10703,22 @@ parseredir: {
                        pungetc();
                }
        } else {        /* c == '<' */
-               np->nfile.fd = 0;
+               /*np->nfile.fd = 0; - stzalloc did it */
                c = pgetc();
                switch (c) {
                case '<':
                        if (sizeof(struct nfile) != sizeof(struct nhere)) {
-                               np = stalloc(sizeof(struct nhere));
-                               np->nfile.fd = 0;
+                               np = stzalloc(sizeof(struct nhere));
+                               /*np->nfile.fd = 0; - stzalloc did it */
                        }
                        np->type = NHERE;
-                       heredoc = stalloc(sizeof(struct heredoc));
+                       heredoc = stzalloc(sizeof(struct heredoc));
                        heredoc->here = np;
                        c = pgetc();
                        if (c == '-') {
                                heredoc->striptabs = 1;
                        } else {
-                               heredoc->striptabs = 0;
+                               /*heredoc->striptabs = 0; - stzalloc did it */
                                pungetc();
                        }
                        break;
@@ -10205,8 +10751,8 @@ parseredir: {
 /* is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise
  * (assuming ascii char codes, as the original implementation did) */
 #define is_special(c) \
-       ((((unsigned int)c) - 33 < 32) \
-                       && ((0xc1ff920dUL >> (((unsigned int)c) - 33)) & 1))
+       (((unsigned)(c) - 33 < 32) \
+                       && ((0xc1ff920dU >> ((unsigned)(c) - 33)) & 1))
 parsesub: {
        int subtype;
        int typeloc;
@@ -10215,18 +10761,22 @@ parsesub: {
        static const char types[] ALIGN1 = "}-+?=";
 
        c = pgetc();
-       if (
-               c <= PEOA_OR_PEOF  ||
-               (c != '(' && c != '{' && !is_name(c) && !is_special(c))
+       if (c <= PEOA_OR_PEOF
+        || (c != '(' && c != '{' && !is_name(c) && !is_special(c))
        ) {
-               USTPUTC('$', out);
+#if ENABLE_ASH_BASH_COMPAT
+               if (c == '\'')
+                       bash_dollar_squote = 1;
+               else
+#endif
+                       USTPUTC('$', out);
                pungetc();
        } else if (c == '(') {  /* $(command) or $((arith)) */
                if (pgetc() == '(') {
 #if ENABLE_ASH_MATH_SUPPORT
                        PARSEARITH();
 #else
-                       raise_error_syntax("We unsupport $((arith))");
+                       raise_error_syntax("you disabled math support for $((arith)) syntax");
 #endif
                } else {
                        pungetc();
@@ -10269,8 +10819,15 @@ parsesub: {
                if (subtype == 0) {
                        switch (c) {
                        case ':':
-                               flags = VSNUL;
                                c = pgetc();
+#if ENABLE_ASH_BASH_COMPAT
+                               if (c == ':' || c == '$' || isdigit(c)) {
+                                       pungetc();
+                                       subtype = VSSUBSTR;
+                                       break;
+                               }
+#endif
+                               flags = VSNUL;
                                /*FALLTHROUGH*/
                        default:
                                p = strchr(types, c);
@@ -10279,18 +10836,26 @@ parsesub: {
                                subtype = p - types + VSNORMAL;
                                break;
                        case '%':
-                       case '#':
-                               {
-                                       int cc = c;
-                                       subtype = c == '#' ? VSTRIMLEFT :
-                                                            VSTRIMRIGHT;
-                                       c = pgetc();
-                                       if (c == cc)
-                                               subtype++;
-                                       else
-                                               pungetc();
-                                       break;
-                               }
+                       case '#': {
+                               int cc = c;
+                               subtype = c == '#' ? VSTRIMLEFT : VSTRIMRIGHT;
+                               c = pgetc();
+                               if (c == cc)
+                                       subtype++;
+                               else
+                                       pungetc();
+                               break;
+                       }
+#if ENABLE_ASH_BASH_COMPAT
+                       case '/':
+                               subtype = VSREPLACE;
+                               c = pgetc();
+                               if (c == '/')
+                                       subtype++; /* VSREPLACEALL */
+                               else
+                                       pungetc();
+                               break;
+#endif
                        }
                } else {
                        pungetc();
@@ -10414,8 +10979,8 @@ parsebackq: {
        nlpp = &bqlist;
        while (*nlpp)
                nlpp = &(*nlpp)->next;
-       *nlpp = stalloc(sizeof(**nlpp));
-       (*nlpp)->next = NULL;
+       *nlpp = stzalloc(sizeof(**nlpp));
+       /* (*nlpp)->next = NULL; - stzalloc did it */
        parsebackquote = oldstyle;
 
        if (oldstyle) {
@@ -10545,7 +11110,8 @@ xxreadtoken(void)
 #endif
                ) {
                        if (c == '#') {
-                               while ((c = pgetc()) != '\n' && c != PEOF);
+                               while ((c = pgetc()) != '\n' && c != PEOF)
+                                       continue;
                                pungetc();
                        } else if (c == '\\') {
                                if (pgetc() != '\n') {
@@ -10571,7 +11137,7 @@ xxreadtoken(void)
                                                return readtoken1(c, BASESYNTAX, (char *) NULL, 0);
                                        }
 
-                                       if (p - xxreadtoken_chars >= xxreadtoken_singles) {
+                                       if ((size_t)(p - xxreadtoken_chars) >= xxreadtoken_singles) {
                                                if (pgetc() == *p) {    /* double occurrence? */
                                                        p += xxreadtoken_doubles + 1;
                                                } else {
@@ -10579,7 +11145,8 @@ xxreadtoken(void)
                                                }
                                        }
                                }
-                               return lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars];
+                               lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars];
+                               return lasttoken;
                        }
                }
        } /* for */
@@ -10608,7 +11175,8 @@ xxreadtoken(void)
 #endif
                        continue;
                case '#':
-                       while ((c = pgetc()) != '\n' && c != PEOF);
+                       while ((c = pgetc()) != '\n' && c != PEOF)
+                               continue;
                        pungetc();
                        continue;
                case '\\':
@@ -10763,7 +11331,7 @@ parseheredoc(void)
        union node *n;
 
        here = heredoclist;
-       heredoclist = 0;
+       heredoclist = NULL;
 
        while (here) {
                if (needprompt) {
@@ -10771,9 +11339,9 @@ parseheredoc(void)
                }
                readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
                                here->eofmark, here->striptabs);
-               n = stalloc(sizeof(struct narg));
+               n = stzalloc(sizeof(struct narg));
                n->narg.type = NARG;
-               n->narg.next = NULL;
+               /*n->narg.next = NULL; - stzalloc did it */
                n->narg.text = wordtext;
                n->narg.backquote = backquotelist;
                here->here->nhere.doc = n;
@@ -10838,20 +11406,19 @@ evalstring(char *s, int mask)
  * The eval command.
  */
 static int
-evalcmd(int argc, char **argv)
+evalcmd(int argc ATTRIBUTE_UNUSED, char **argv)
 {
        char *p;
        char *concat;
-       char **ap;
 
-       if (argc > 1) {
+       if (argv[1]) {
                p = argv[1];
-               if (argc > 2) {
+               argv += 2;
+               if (argv[0]) {
                        STARTSTACKSTR(concat);
-                       ap = argv + 2;
                        for (;;) {
                                concat = stack_putstr(p, concat);
-                               p = *ap++;
+                               p = *argv++;
                                if (p == NULL)
                                        break;
                                STPUTC(' ', concat);
@@ -10883,7 +11450,7 @@ cmdloop(int top)
 
                setstackmark(&smark);
 #if JOBS
-               if (jobctl)
+               if (doing_jobctl)
                        showjobs(stderr, SHOW_CHANGED);
 #endif
                inter = 0;
@@ -10962,16 +11529,15 @@ dotcmd(int argc, char **argv)
        for (sp = cmdenviron; sp; sp = sp->next)
                setvareq(ckstrdup(sp->text), VSTRFIXED | VTEXTFIXED);
 
-       if (argc >= 2) {        /* That's what SVR2 does */
-               char *fullname;
-
-               fullname = find_dot_file(argv[1]);
-
-               if (argc > 2) {
+       if (argv[1]) {        /* That's what SVR2 does */
+               char *fullname = find_dot_file(argv[1]);
+               argv += 2;
+               argc -= 2;
+               if (argc) { /* argc > 0, argv[0] != NULL */
                        saveparam = shellparam;
-                       shellparam.malloc = 0;
-                       shellparam.nparam = argc - 2;
-                       shellparam.p = argv + 2;
+                       shellparam.malloced = 0;
+                       shellparam.nparam = argc;
+                       shellparam.p = argv;
                };
 
                setinputfile(fullname, INPUT_PUSH_FILE);
@@ -10979,7 +11545,7 @@ dotcmd(int argc, char **argv)
                cmdloop(0);
                popfile();
 
-               if (argc > 2) {
+               if (argc) {
                        freeparam(&shellparam);
                        shellparam = saveparam;
                };
@@ -10989,32 +11555,16 @@ dotcmd(int argc, char **argv)
 }
 
 static int
-exitcmd(int argc, char **argv)
+exitcmd(int argc ATTRIBUTE_UNUSED, char **argv)
 {
        if (stoppedjobs())
                return 0;
-       if (argc > 1)
+       if (argv[1])
                exitstatus = number(argv[1]);
        raise_exception(EXEXIT);
        /* NOTREACHED */
 }
 
-#if ENABLE_ASH_BUILTIN_ECHO
-static int
-echocmd(int argc, char **argv)
-{
-       return bb_echo(argc, argv);
-}
-#endif
-
-#if ENABLE_ASH_BUILTIN_TEST
-static int
-testcmd(int argc, char **argv)
-{
-       return test_main(argc, argv);
-}
-#endif
-
 /*
  * Read a file containing shell functions.
  */
@@ -11113,10 +11663,13 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path)
        }
 
 #if ENABLE_FEATURE_SH_STANDALONE
-       if (find_applet_by_name(name)) {
-               entry->cmdtype = CMDNORMAL;
-               entry->u.index = -1;
-               return;
+       {
+               int applet_no = find_applet_by_name(name);
+               if (applet_no >= 0) {
+                       entry->cmdtype = CMDNORMAL;
+                       entry->u.index = -2 - applet_no;
+                       return;
+               }
        }
 #endif
 
@@ -11142,11 +11695,10 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path)
                                if (bcmd)
                                        goto builtin_success;
                                continue;
-                       } else if (!(act & DO_NOFUNC)
-                        && prefix(pathopt, "func")) {
-                               /* handled below */
-                       } else {
-                               /* ignore unimplemented options */
+                       }
+                       if ((act & DO_NOFUNC)
+                        || !prefix(pathopt, "func")
+                       ) {     /* ignore unimplemented options */
                                continue;
                        }
                }
@@ -11227,7 +11779,7 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path)
  * The trap builtin.
  */
 static int
-trapcmd(int argc, char **argv)
+trapcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
 {
        char *action;
        char **ap;
@@ -11280,9 +11832,10 @@ trapcmd(int argc, char **argv)
  * Lists available builtins
  */
 static int
-helpcmd(int argc, char **argv)
+helpcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
 {
-       int col, i;
+       unsigned col;
+       unsigned i;
 
        out1fmt("\nBuilt-in commands:\n-------------------\n");
        for (col = 0, i = 0; i < ARRAY_SIZE(builtintab); i++) {
@@ -11294,11 +11847,15 @@ helpcmd(int argc, char **argv)
                }
        }
 #if ENABLE_FEATURE_SH_STANDALONE
-       for (i = 0; i < NUM_APPLETS; i++) {
-               col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), applets[i].name);
-               if (col > 60) {
-                       out1fmt("\n");
-                       col = 0;
+       {
+               const char *a = applet_names;
+               while (*a) {
+                       col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a);
+                       if (col > 60) {
+                               out1fmt("\n");
+                               col = 0;
+                       }
+                       a += strlen(a) + 1;
                }
        }
 #endif
@@ -11311,7 +11868,7 @@ helpcmd(int argc, char **argv)
  * The export and readonly commands.
  */
 static int
-exportcmd(int argc, char **argv)
+exportcmd(int argc ATTRIBUTE_UNUSED, char **argv)
 {
        struct var *vp;
        char *name;
@@ -11362,7 +11919,7 @@ unsetfunc(const char *name)
  * with the same name.
  */
 static int
-unsetcmd(int argc, char **argv)
+unsetcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
 {
        char **ap;
        int i;
@@ -11400,7 +11957,7 @@ static const unsigned char timescmd_str[] ALIGN1 = {
 };
 
 static int
-timescmd(int ac, char **av)
+timescmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
 {
        long clk_tck, s, t;
        const unsigned char *p;
@@ -11452,17 +12009,16 @@ dash_arith(const char *s)
  *  Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru>
  */
 static int
-letcmd(int argc, char **argv)
+letcmd(int argc ATTRIBUTE_UNUSED, char **argv)
 {
-       char **ap;
-       arith_t i = 0;
+       arith_t i;
 
-       ap = argv + 1;
-       if (!*ap)
+       argv++;
+       if (!*argv)
                ash_msg_and_raise_error("expression expected");
-       for (ap = argv + 1; *ap; ap++) {
-               i = dash_arith(*ap);
-       }
+       do {
+               i = dash_arith(*argv);
+       } while (*++argv);
 
        return !i;
 }
@@ -11481,14 +12037,24 @@ typedef enum __rlimit_resource rlim_t;
 #endif
 
 /*
- * The read builtin.  The -e option causes backslashes to escape the
- * following character.
- *
+ * The read builtin. Options:
+ *      -r              Do not interpret '\' specially
+ *      -s              Turn off echo (tty only)
+ *      -n NCHARS       Read NCHARS max
+ *      -p PROMPT       Display PROMPT on stderr (if input is from tty)
+ *      -t SECONDS      Timeout after SECONDS (tty or pipe only)
+ *      -u FD           Read from given FD instead of fd 0
  * This uses unbuffered input, which may be avoidable in some cases.
+ * TODO: bash also has:
+ *      -a ARRAY        Read into array[0],[1],etc
+ *      -d DELIM        End on DELIM char, not newline
+ *      -e              Use line editing (tty only)
  */
 static int
-readcmd(int argc, char **argv)
+readcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
 {
+       static const char *const arg_REPLY[] = { "REPLY", NULL };
+
        char **ap;
        int backslash;
        char c;
@@ -11499,31 +12065,23 @@ readcmd(int argc, char **argv)
        int startword;
        int status;
        int i;
+       int fd = 0;
 #if ENABLE_ASH_READ_NCHARS
-       int n_flag = 0;
-       int nchars = 0;
+       int nchars = 0; /* if != 0, -n is in effect */
        int silent = 0;
        struct termios tty, old_tty;
 #endif
 #if ENABLE_ASH_READ_TIMEOUT
-       fd_set set;
-       struct timeval ts;
-
-       ts.tv_sec = ts.tv_usec = 0;
+       unsigned end_ms = 0;
+       unsigned timeout = 0;
 #endif
 
        rflag = 0;
        prompt = NULL;
-#if ENABLE_ASH_READ_NCHARS && ENABLE_ASH_READ_TIMEOUT
-       while ((i = nextopt("p:rt:n:s")) != '\0')
-#elif ENABLE_ASH_READ_NCHARS
-       while ((i = nextopt("p:rn:s")) != '\0')
-#elif ENABLE_ASH_READ_TIMEOUT
-       while ((i = nextopt("p:rt:")) != '\0')
-#else
-       while ((i = nextopt("p:r")) != '\0')
-#endif
-       {
+       while ((i = nextopt("p:u:r"
+               USE_ASH_READ_TIMEOUT("t:")
+               USE_ASH_READ_NCHARS("n:s")
+       )) != '\0') {
                switch (i) {
                case 'p':
                        prompt = optionarg;
@@ -11533,7 +12091,7 @@ readcmd(int argc, char **argv)
                        nchars = bb_strtou(optionarg, NULL, 10);
                        if (nchars < 0 || errno)
                                ash_msg_and_raise_error("invalid count");
-                       n_flag = nchars; /* just a flag "nchars is nonzero" */
+                       /* nchars == 0: off (bash 3.2 does this too) */
                        break;
                case 's':
                        silent = 1;
@@ -11541,6 +12099,11 @@ readcmd(int argc, char **argv)
 #endif
 #if ENABLE_ASH_READ_TIMEOUT
                case 't':
+                       timeout = bb_strtou(optionarg, NULL, 10);
+                       if (errno || timeout > UINT_MAX / 2048)
+                               ash_msg_and_raise_error("invalid timeout");
+                       timeout *= 1000;
+#if 0 /* even bash have no -t N.NNN support */
                        ts.tv_sec = bb_strtou(optionarg, &p, 10);
                        ts.tv_usec = 0;
                        /* EINVAL means number is ok, but not terminated by NUL */
@@ -11564,65 +12127,73 @@ readcmd(int argc, char **argv)
                        if (!(ts.tv_sec | ts.tv_usec)) { /* both are 0? */
                                ash_msg_and_raise_error("invalid timeout");
                        }
+#endif /* if 0 */
                        break;
 #endif
                case 'r':
                        rflag = 1;
                        break;
+               case 'u':
+                       fd = bb_strtou(optionarg, NULL, 10);
+                       if (fd < 0 || errno)
+                               ash_msg_and_raise_error("invalid file descriptor");
+                       break;
                default:
                        break;
                }
        }
-       if (prompt && isatty(0)) {
+       if (prompt && isatty(fd)) {
                out2str(prompt);
        }
        ap = argptr;
        if (*ap == NULL)
-               ash_msg_and_raise_error("arg count");
+               ap = (char**)arg_REPLY;
        ifs = bltinlookup("IFS");
        if (ifs == NULL)
                ifs = defifs;
 #if ENABLE_ASH_READ_NCHARS
-       if (n_flag || silent) {
-               if (tcgetattr(0, &tty) != 0) {
-                       /* Not a tty */
-                       n_flag = 0;
-                       silent = 0;
-               } else {
-                       old_tty = tty;
-                       if (n_flag) {
-                               tty.c_lflag &= ~ICANON;
-                               tty.c_cc[VMIN] = nchars < 256 ? nchars : 255;
-                       }
-                       if (silent) {
-                               tty.c_lflag &= ~(ECHO | ECHOK | ECHONL);
-                       }
-                       tcsetattr(0, TCSANOW, &tty);
+       tcgetattr(fd, &tty);
+       old_tty = tty;
+       if (nchars || silent) {
+               if (nchars) {
+                       tty.c_lflag &= ~ICANON;
+                       tty.c_cc[VMIN] = nchars < 256 ? nchars : 255;
                }
-       }
-#endif
-#if ENABLE_ASH_READ_TIMEOUT
-       if (ts.tv_sec || ts.tv_usec) {
-               FD_ZERO(&set);
-               FD_SET(0, &set);
-
-               /* poll-based wait produces bigger code, using select */
-               i = select(1, &set, NULL, NULL, &ts);
-               if (!i) { /* timed out! */
-#if ENABLE_ASH_READ_NCHARS
-                       if (n_flag)
-                               tcsetattr(0, TCSANOW, &old_tty);
-#endif
-                       return 1;
+               if (silent) {
+                       tty.c_lflag &= ~(ECHO | ECHOK | ECHONL);
                }
+               /* if tcgetattr failed, tcsetattr will fail too.
+                * Ignoring, it's harmless. */
+               tcsetattr(fd, TCSANOW, &tty);
        }
 #endif
+
        status = 0;
        startword = 1;
        backslash = 0;
+#if ENABLE_ASH_READ_TIMEOUT
+       if (timeout) /* NB: ensuring end_ms is nonzero */
+               end_ms = ((unsigned)(monotonic_us() / 1000) + timeout) | 1;
+#endif
        STARTSTACKSTR(p);
        do {
-               if (read(0, &c, 1) != 1) {
+#if ENABLE_ASH_READ_TIMEOUT
+               if (end_ms) {
+                       struct pollfd pfd[1];
+                       pfd[0].fd = fd;
+                       pfd[0].events = POLLIN;
+                       timeout = end_ms - (unsigned)(monotonic_us() / 1000);
+                       if ((int)timeout <= 0 /* already late? */
+                        || safe_poll(pfd, 1, timeout) != 1 /* no? wait... */
+                       ) { /* timed out! */
+#if ENABLE_ASH_READ_NCHARS
+                               tcsetattr(fd, TCSANOW, &old_tty);
+#endif
+                               return 1;
+                       }
+               }
+#endif
+               if (nonblock_safe_read(fd, &c, 1) != 1) {
                        status = 1;
                        break;
                }
@@ -11657,14 +12228,13 @@ readcmd(int argc, char **argv)
        }
 /* end of do {} while: */
 #if ENABLE_ASH_READ_NCHARS
-       while (!n_flag || --nchars);
+       while (--nchars);
 #else
        while (1);
 #endif
 
 #if ENABLE_ASH_READ_NCHARS
-       if (n_flag || silent)
-               tcsetattr(0, TCSANOW, &old_tty);
+       tcsetattr(fd, TCSANOW, &old_tty);
 #endif
 
        STACKSTRNUL(p);
@@ -11678,7 +12248,7 @@ readcmd(int argc, char **argv)
 }
 
 static int
-umaskcmd(int argc, char **argv)
+umaskcmd(int argc ATTRIBUTE_UNUSED, char **argv)
 {
        static const char permuser[3] ALIGN1 = "ugo";
        static const char permmode[3] ALIGN1 = "rwx";
@@ -11853,7 +12423,7 @@ printlim(enum limtype how, const struct rlimit *limit,
 }
 
 static int
-ulimitcmd(int argc, char **argv)
+ulimitcmd(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
 {
        int c;
        rlim_t val = 0;
@@ -11929,6 +12499,7 @@ ulimitcmd(int argc, char **argv)
 
                        while ((c = *p++) >= '0' && c <= '9') {
                                val = (val * 10) + (long)(c - '0');
+                               // val is actually 'unsigned long int' and can't get < 0
                                if (val < (rlim_t) 0)
                                        break;
                        }
@@ -12172,7 +12743,7 @@ is_right_associativity(operator prec)
                || prec == PREC(TOK_CONDITIONAL));
 }
 
-typedef struct ARITCH_VAR_NUM {
+typedef struct {
        arith_t val;
        arith_t contidional_second_val;
        char contidional_second_val_initialized;
@@ -12180,9 +12751,9 @@ typedef struct ARITCH_VAR_NUM {
                           else is variable name */
 } v_n_t;
 
-typedef struct CHK_VAR_RECURSIVE_LOOPED {
+typedef struct chk_var_recursive_looped_t {
        const char *var;
-       struct CHK_VAR_RECURSIVE_LOOPED *next;
+       struct chk_var_recursive_looped_t *next;
 } chk_var_recursive_looped_t;
 
 static chk_var_recursive_looped_t *prev_chk_var_recursive;
@@ -12418,7 +12989,7 @@ static const char op_tokens[] ALIGN1 = {
        0
 };
 /* ptr to ")" */
-#define endexpression &op_tokens[sizeof(op_tokens)-7]
+#define endexpression (&op_tokens[sizeof(op_tokens)-7])
 
 static arith_t
 arith(const char *expr, int *perrcode)
@@ -12426,21 +12997,19 @@ arith(const char *expr, int *perrcode)
        char arithval; /* Current character under analysis */
        operator lasttok, op;
        operator prec;
-
+       operator *stack, *stackptr;
        const char *p = endexpression;
        int errcode;
-
-       size_t datasizes = strlen(expr) + 2;
+       v_n_t *numstack, *numstackptr;
+       unsigned datasizes = strlen(expr) + 2;
 
        /* Stack of integers */
        /* The proof that there can be no more than strlen(startbuf)/2+1 integers
         * in any given correct or incorrect expression is left as an exercise to
         * the reader. */
-       v_n_t *numstack = alloca(((datasizes)/2)*sizeof(v_n_t)),
-                               *numstackptr = numstack;
+       numstackptr = numstack = alloca((datasizes / 2) * sizeof(numstack[0]));
        /* Stack of operator tokens */
-       operator *stack = alloca((datasizes) * sizeof(operator)),
-                               *stackptr = stack;
+       stackptr = stack = alloca(datasizes * sizeof(stack[0]));
 
        *stackptr++ = lasttok = TOK_LPAREN;     /* start off with a left paren */
        *perrcode = errcode = 0;
@@ -12469,7 +13038,8 @@ arith(const char *expr, int *perrcode)
                        if (numstackptr != numstack+1) {
                                /* ... but if there isn't, it's bad */
  err:
-                               return (*perrcode = -1);
+                               *perrcode = -1;
+                               return *perrcode;
                        }
                        if (numstack->var) {
                                /* expression is $((var)) only, lookup now */
@@ -12687,7 +13257,7 @@ init(void)
  * Process the shell command line arguments.
  */
 static void
-procargs(int argc, char **argv)
+procargs(char **argv)
 {
        int i;
        const char *xminusc;
@@ -12695,12 +13265,15 @@ procargs(int argc, char **argv)
 
        xargv = argv;
        arg0 = xargv[0];
-       if (argc > 0)
+       /* if (xargv[0]) - mmm, this is always true! */
                xargv++;
        for (i = 0; i < NOPTS; i++)
                optlist[i] = 2;
        argptr = xargv;
-       options(1);
+       if (options(1)) {
+               /* it already printed err message */
+               raise_exception(EXERROR);
+       }
        xargv = argptr;
        xminusc = minusc;
        if (*xargv == NULL) {
@@ -12735,7 +13308,7 @@ procargs(int argc, char **argv)
        shellparam.optind = 1;
        shellparam.optoff = -1;
 #endif
-       /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
+       /* assert(shellparam.malloced == 0 && shellparam.nparam == 0); */
        while (*xargv) {
                shellparam.nparam++;
                xargv++;
@@ -12792,13 +13365,22 @@ extern int etext();
  * is used to figure out how far we had gotten.
  */
 int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int ash_main(int argc, char **argv)
+int ash_main(int argc ATTRIBUTE_UNUSED, char **argv)
 {
        char *shinit;
        volatile int state;
        struct jmploc jmploc;
        struct stackmark smark;
 
+       /* Initialize global data */
+       INIT_G_misc();
+       INIT_G_memstack();
+       INIT_G_var();
+#if ENABLE_ASH_ALIAS
+       INIT_G_alias();
+#endif
+       INIT_G_cmdtable();
+
 #if PROFILE
        monitor(4, etext, profile_buf, sizeof(profile_buf), 50);
 #endif
@@ -12846,7 +13428,8 @@ int ash_main(int argc, char **argv)
 #endif
        init();
        setstackmark(&smark);
-       procargs(argc, argv);
+       procargs(argv);
+
 #if ENABLE_FEATURE_EDITING_SAVEHISTORY
        if (iflag) {
                const char *hp = lookupvar("HISTFILE");
@@ -12890,7 +13473,7 @@ int ash_main(int argc, char **argv)
 
        if (sflag || minusc == NULL) {
 #if ENABLE_FEATURE_EDITING_SAVEHISTORY
-               if ( iflag ) {
+               if (iflag) {
                        const char *hp = lookupvar("HISTFILE");
 
                        if (hp != NULL)