ash: revent one place where number() doesn't work
[oweals/busybox.git] / shell / ash.c
index 2fc903b37a581f40c3c39048e1fb458d849ec3f3..bb9464af9a7ec244ac645d7a1e98a42edaa73f76 100644 (file)
  */
 
 /*
- * rewrite arith.y to micro stack based cryptic algorithm by
- * Copyright (c) 2001 Aaron Lehmann <aaronl@vitelus.com>
- *
- * Modified by Paul Mundt <lethal@linux-sh.org> (c) 2004 to support
- * dynamic variables.
- *
- * Modified by Vladimir Oleynik <dzo@simtreas.ru> (c) 2001-2005 to be
- * used in busybox and size optimizations,
- * rewrote arith (see notes to this), added locale support,
- * rewrote dynamic variables.
- */
-
-/*
- * The follow should be set to reflect the type of system you have:
+ * The following should be set to reflect the type of system you have:
  *      JOBS -> 1 if you have Berkeley job control, 0 otherwise.
  *      define SYSV if you are running under System V.
  *      define DEBUG=1 to compile in debugging ('set -o debug' to turn on)
  * a quit signal will generate a core dump.
  */
 #define DEBUG 0
+/* Tweak debug output verbosity here */
+#define DEBUG_TIME 0
+#define DEBUG_PID 1
+#define DEBUG_SIG 1
+
 #define PROFILE 0
 
 #define IFS_BROKEN
@@ -47,9 +39,9 @@
 #define JOBS ENABLE_ASH_JOB_CONTROL
 
 #if DEBUG
-#ifndef _GNU_SOURCE
-#define _GNU_SOURCE
-#endif
+# ifndef _GNU_SOURCE
+#  define _GNU_SOURCE
+# endif
 #endif
 
 #include "busybox.h" /* for applet_names */
 #include <paths.h>
 #include <setjmp.h>
 #include <fnmatch.h>
+#include "math.h"
 
 #if defined SINGLE_APPLET_MAIN
 /* STANDALONE does not make sense, and won't compile */
 #undef CONFIG_FEATURE_SH_STANDALONE
 #undef ENABLE_FEATURE_SH_STANDALONE
-#undef USE_FEATURE_SH_STANDALONE
-#undef SKIP_FEATURE_SH_STANDALONE(...)
+#undef IF_FEATURE_SH_STANDALONE
+#undef IF_NOT_FEATURE_SH_STANDALONE(...)
 #define ENABLE_FEATURE_SH_STANDALONE 0
-#define USE_FEATURE_SH_STANDALONE(...)
-#define SKIP_FEATURE_SH_STANDALONE(...) __VA_ARGS__
+#define IF_FEATURE_SH_STANDALONE(...)
+#define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__
 #endif
 
 #ifndef PIPE_BUF
-#define PIPE_BUF 4096           /* amount of buffering in a pipe */
+# define PIPE_BUF 4096           /* amount of buffering in a pipe */
 #endif
 
 #if defined(__uClinux__)
-#error "Do not even bother, ash will not run on NOMMU machine"
+# error "Do not even bother, ash will not run on NOMMU machine"
 #endif
 
 
 #define CMDTABLESIZE 31         /* should be prime */
 
 
-/* ============ Misc helpers */
-
-#define xbarrier() do { __asm__ __volatile__ ("": : :"memory"); } while (0)
-
-/* C99 says: "char" declaration may be signed or unsigned by default */
-#define signed_char2int(sc) ((int)((signed char)sc))
-
-
 /* ============ Shell options */
 
 static const char *const optletters_optnames[] = {
@@ -127,12 +112,12 @@ enum { NOPTS = ARRAY_SIZE(optletters_optnames) };
 
 static const char homestr[] ALIGN1 = "HOME";
 static const char snlfmt[] ALIGN1 = "%s\n";
-static const char illnum[] ALIGN1 = "Illegal number: %s";
+static const char msg_illnum[] ALIGN1 = "Illegal number: %s";
 
 /*
  * We enclose jmp_buf in a structure so that we can declare pointers to
  * jump locations.  The global variable handler contains the location to
- * jump to when an exception occurs, and the global variable exception
+ * jump to when an exception occurs, and the global variable exception_type
  * contains a code identifying the exception.  To implement nested
  * exception handlers, the user should save the value of handler on entry
  * to an inner scope, set handler to point to a jmploc structure for the
@@ -157,15 +142,11 @@ struct globals_misc {
 
        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 */
+       volatile int suppress_int; /* counter */
+       volatile /*sig_atomic_t*/ smallint pending_int; /* 1 = got SIGINT */
        /* last pending signal */
-       volatile /*sig_atomic_t*/ smallint pendingsig;
-       smallint exception; /* kind of exception (0..5) */
+       volatile /*sig_atomic_t*/ smallint pending_sig;
+       smallint exception_type; /* kind of exception (0..5) */
        /* exceptions */
 #define EXINT 0         /* SIGINT received */
 #define EXERROR 1       /* a generic error */
@@ -210,7 +191,7 @@ struct globals_misc {
 #define S_HARD_IGN 4            /* signal is ignored permenantly */
 
        /* indicates specified signal received */
-       char gotsig[NSIG - 1]; /* offset by 1: "signal" 0 is meaningless */
+       uint8_t gotsig[NSIG - 1]; /* offset by 1: "signal" 0 is meaningless */
        char *trap[NSIG];
 
        /* Rarely referenced stuff */
@@ -231,11 +212,10 @@ extern struct globals_misc *const ash_ptr_to_globals_misc;
 #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 exception_type    (G_misc.exception_type   )
+#define suppress_int      (G_misc.suppress_int     )
+#define pending_int       (G_misc.pending_int      )
+#define pending_sig       (G_misc.pending_sig      )
 #define isloginsh   (G_misc.isloginsh  )
 #define nullstr     (G_misc.nullstr    )
 #define optlist     (G_misc.optlist    )
@@ -254,7 +234,30 @@ extern struct globals_misc *const ash_ptr_to_globals_misc;
 } while (0)
 
 
+/* ============ DEBUG */
+#if DEBUG
+static void trace_printf(const char *fmt, ...);
+static void trace_vprintf(const char *fmt, va_list va);
+# define TRACE(param)    trace_printf param
+# define TRACEV(param)   trace_vprintf param
+# define close(fd) do { \
+       int dfd = (fd); \
+       if (close(dfd) < 0) \
+               bb_error_msg("bug on %d: closing %d(0x%x)", \
+                       __LINE__, dfd, dfd); \
+} while (0)
+#else
+# define TRACE(param)
+# define TRACEV(param)
+#endif
+
+
 /* ============ Utility functions */
+#define xbarrier() do { __asm__ __volatile__ ("": : :"memory"); } while (0)
+
+/* C99 says: "char" declaration may be signed or unsigned by default */
+#define signed_char2int(sc) ((int)(signed char)(sc))
+
 static int isdigit_str9(const char *str)
 {
        int maxlen = 9 + 1; /* max 9 digits: 999999999 */
@@ -272,14 +275,14 @@ static int isdigit_str9(const char *str)
  * more fun than worrying about efficiency and portability. :-))
  */
 #define INT_OFF do { \
-       suppressint++; \
+       suppress_int++; \
        xbarrier(); \
 } while (0)
 
 /*
  * Called to raise an exception.  Since C doesn't include exceptions, we
  * just do a longjmp to the exception handler.  The type of exception is
- * stored in the global variable "exception".
+ * stored in the global variable "exception_type".
  */
 static void raise_exception(int) NORETURN;
 static void
@@ -290,9 +293,15 @@ raise_exception(int e)
                abort();
 #endif
        INT_OFF;
-       exception = e;
+       exception_type = e;
        longjmp(exception_handler->loc, 1);
 }
+#if DEBUG
+#define raise_exception(e) do { \
+       TRACE(("raising exception %d on line %d\n", (e), __LINE__)); \
+       raise_exception(e); \
+} while (0)
+#endif
 
 /*
  * Called from trap.c when a SIGINT is received.  (If the user specifies
@@ -305,97 +314,61 @@ static void raise_interrupt(void) NORETURN;
 static void
 raise_interrupt(void)
 {
-       int i;
+       int ex_type;
 
-       intpending = 0;
+       pending_int = 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() */
+       /* pending_sig = 0; - now done in onsig() */
 
-       i = EXSIG;
+       ex_type = EXSIG;
        if (gotsig[SIGINT - 1] && !trap[SIGINT]) {
                if (!(rootshell && iflag)) {
                        /* Kill ourself with SIGINT */
                        signal(SIGINT, SIG_DFL);
                        raise(SIGINT);
                }
-               i = EXINT;
+               ex_type = EXINT;
        }
-       raise_exception(i);
+       raise_exception(ex_type);
        /* NOTREACHED */
 }
+#if DEBUG
+#define raise_interrupt() do { \
+       TRACE(("raising interrupt on line %d\n", __LINE__)); \
+       raise_interrupt(); \
+} while (0)
+#endif
 
-#if ENABLE_ASH_OPTIMIZE_FOR_SIZE
-static void
+static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void
 int_on(void)
 {
-       if (--suppressint == 0 && intpending) {
+       xbarrier();
+       if (--suppress_int == 0 && pending_int) {
                raise_interrupt();
        }
 }
 #define INT_ON int_on()
-static void
+static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void
 force_int_on(void)
 {
-       suppressint = 0;
-       if (intpending)
+       xbarrier();
+       suppress_int = 0;
+       if (pending_int)
                raise_interrupt();
 }
 #define FORCE_INT_ON force_int_on()
-#else
-#define INT_ON do { \
-       xbarrier(); \
-       if (--suppressint == 0 && intpending) \
-               raise_interrupt(); \
-} while (0)
-#define FORCE_INT_ON do { \
-       xbarrier(); \
-       suppressint = 0; \
-       if (intpending) \
-               raise_interrupt(); \
-} while (0)
-#endif /* ASH_OPTIMIZE_FOR_SIZE */
 
-#define SAVE_INT(v) ((v) = suppressint)
+#define SAVE_INT(v) ((v) = suppress_int)
 
 #define RESTORE_INT(v) do { \
        xbarrier(); \
-       suppressint = (v); \
-       if (suppressint == 0 && intpending) \
+       suppress_int = (v); \
+       if (suppress_int == 0 && pending_int) \
                raise_interrupt(); \
 } while (0)
 
-/*
- * Ignore a signal. Avoids unnecessary system calls.
- */
-static void
-ignoresig(int signo)
-{
-       if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) {
-               signal(signo, SIG_IGN);
-       }
-       sigmode[signo - 1] = S_HARD_IGN;
-}
-
-/*
- * Signal handler. Only one usage site - in setsignal()
- */
-static void
-onsig(int signo)
-{
-       gotsig[signo - 1] = 1;
-       pendingsig = signo;
-
-       if (/* exsig || */ (signo == SIGINT && !trap[SIGINT])) {
-               if (!suppressint) {
-                       pendingsig = 0;
-                       raise_interrupt(); /* does not return */
-               }
-               intpending = 1;
-       }
-}
-
 
 /* ============ Stdout/stderr output */
 
@@ -480,15 +453,15 @@ out2str(const char *p)
 /* ============ Parser structures */
 
 /* control characters in argument strings */
-#define CTLESC '\201'           /* escape next character */
-#define CTLVAR '\202'           /* variable defn */
-#define CTLENDVAR '\203'
-#define CTLBACKQ '\204'
+#define CTLESC       ((unsigned char)'\201')    /* escape next character */
+#define CTLVAR       ((unsigned char)'\202')    /* variable defn */
+#define CTLENDVAR    ((unsigned char)'\203')
+#define CTLBACKQ     ((unsigned char)'\204')
 #define CTLQUOTE 01             /* ored with CTLBACKQ code if in quotes */
 /*      CTLBACKQ | CTLQUOTE == '\205' */
-#define CTLARI  '\206'          /* arithmetic expression */
-#define CTLENDARI '\207'
-#define CTLQUOTEMARK '\210'
+#define CTLARI       ((unsigned char)'\206')    /* arithmetic expression */
+#define CTLENDARI    ((unsigned char)'\207')
+#define CTLQUOTEMARK ((unsigned char)'\210')
 
 /* variable substitution byte (follows CTLVAR) */
 #define VSTYPE  0x0f            /* type of variable substitution */
@@ -659,6 +632,12 @@ union node {
        struct nnot nnot;
 };
 
+/*
+ * NODE_EOF is returned by parsecmd when it encounters an end of file.
+ * It must be distinct from NULL.
+ */
+#define NODE_EOF ((union node *) -1L)
+
 struct nodelist {
        struct nodelist *next;
        union node *n;
@@ -693,6 +672,12 @@ trace_printf(const char *fmt, ...)
 
        if (debug != 1)
                return;
+       if (DEBUG_TIME)
+               fprintf(tracefile, "%u ", (int) time(NULL));
+       if (DEBUG_PID)
+               fprintf(tracefile, "[%u] ", (int) getpid());
+       if (DEBUG_SIG)
+               fprintf(tracefile, "pending s:%d i:%d(supp:%d) ", pending_sig, pending_int, suppress_int);
        va_start(va, fmt);
        vfprintf(tracefile, fmt, va);
        va_end(va);
@@ -703,6 +688,12 @@ trace_vprintf(const char *fmt, va_list va)
 {
        if (debug != 1)
                return;
+       if (DEBUG_TIME)
+               fprintf(tracefile, "%u ", (int) time(NULL));
+       if (DEBUG_PID)
+               fprintf(tracefile, "[%u] ", (int) getpid());
+       if (DEBUG_SIG)
+               fprintf(tracefile, "pending s:%d i:%d(supp:%d) ", pending_sig, pending_int, suppress_int);
        vfprintf(tracefile, fmt, va);
 }
 
@@ -961,6 +952,12 @@ shtree(union node *n, int ind, char *pfx, FILE *fp)
                return;
 
        indent(ind, pfx, fp);
+
+       if (n == NODE_EOF) {
+               fputs("<EOF>", fp);
+               return;
+       }
+
        switch (n->type) {
        case NSEMI:
                s = "; ";
@@ -983,7 +980,7 @@ shtree(union node *n, int ind, char *pfx, FILE *fp)
                break;
        case NPIPE:
                for (lp = n->npipe.cmdlist; lp; lp = lp->next) {
-                       shcmd(lp->n, fp);
+                       shtree(lp->n, 0, NULL, fp);
                        if (lp->next)
                                fputs(" | ", fp);
                }
@@ -1004,17 +1001,9 @@ static void
 showtree(union node *n)
 {
        trace_puts("showtree called\n");
-       shtree(n, 1, NULL, stdout);
+       shtree(n, 1, NULL, stderr);
 }
 
-#define TRACE(param)    trace_printf param
-#define TRACEV(param)   trace_vprintf param
-
-#else
-
-#define TRACE(param)
-#define TRACEV(param)
-
 #endif /* DEBUG */
 
 
@@ -1113,6 +1102,14 @@ ash_msg_and_raise_error(const char *msg, ...)
        va_end(ap);
 }
 
+static void raise_error_syntax(const char *) NORETURN;
+static void
+raise_error_syntax(const char *msg)
+{
+       ash_msg_and_raise_error("syntax error: %s", msg);
+       /* NOTREACHED */
+}
+
 static void ash_msg_and_raise(int, const char *, ...) NORETURN;
 static void
 ash_msg_and_raise(int cond, const char *msg, ...)
@@ -1551,7 +1548,7 @@ static int
 number(const char *s)
 {
        if (!is_number(s))
-               ash_msg_and_raise_error(illnum, s);
+               ash_msg_and_raise_error(msg_illnum, s);
        return atoi(s);
 }
 
@@ -1693,14 +1690,14 @@ freeparam(volatile struct shparam *param)
 }
 
 #if ENABLE_ASH_GETOPTS
-static void getoptsreset(const char *value);
+static void FAST_FUNC getoptsreset(const char *value);
 #endif
 
 struct var {
        struct var *next;               /* next entry in hash list */
        int flags;                      /* flags are defined above */
        const char *text;               /* name=value */
-       void (*func)(const char *);     /* function to be called when  */
+       void (*func)(const char *) FAST_FUNC; /* function to be called when  */
                                        /* the variable gets set/unset */
 };
 
@@ -1737,13 +1734,13 @@ static const char defifs[] ALIGN1 = " \t\n";
 
 /* Need to be before varinit_data[] */
 #if ENABLE_LOCALE_SUPPORT
-static void
+static void FAST_FUNC
 change_lc_all(const char *value)
 {
        if (value && *value != '\0')
                setlocale(LC_ALL, value);
 }
-static void
+static void FAST_FUNC
 change_lc_ctype(const char *value)
 {
        if (value && *value != '\0')
@@ -1752,17 +1749,17 @@ change_lc_ctype(const char *value)
 #endif
 #if ENABLE_ASH_MAIL
 static void chkmail(void);
-static void changemail(const char *);
+static void changemail(const char *) FAST_FUNC;
 #endif
-static void changepath(const char *);
+static void changepath(const char *) FAST_FUNC;
 #if ENABLE_ASH_RANDOM_SUPPORT
-static void change_random(const char *);
+static void change_random(const char *) FAST_FUNC;
 #endif
 
 static const struct {
        int flags;
        const char *text;
-       void (*func)(const char *);
+       void (*func)(const char *) FAST_FUNC;
 } varinit_data[] = {
 #ifdef IFS_BROKEN
        { VSTRFIXED|VTEXTFIXED       , defifsvar   , NULL            },
@@ -1868,7 +1865,7 @@ extern struct globals_var *const ash_ptr_to_globals_var;
 #define is_in_name(c)   ((c) == '_' || isalnum((unsigned char)(c)))
 
 #if ENABLE_ASH_GETOPTS
-static void
+static void FAST_FUNC
 getoptsreset(const char *value)
 {
        shellparam.optind = number(value);
@@ -1987,7 +1984,7 @@ findvar(struct var **vpp, const char *name)
 /*
  * Find the value of a variable.  Returns NULL if not set.
  */
-static char *
+static const char *
 lookupvar(const char *name)
 {
        struct var *v;
@@ -2013,7 +2010,7 @@ lookupvar(const char *name)
 /*
  * Search the environment of a builtin command.
  */
-static char *
+static const char *
 bltinlookup(const char *name)
 {
        struct strlist *sp;
@@ -2231,17 +2228,17 @@ listvars(int on, int off, char ***end)
 /* ============ Path search helper
  *
  * The variable path (passed by reference) should be set to the start
- * of the path before the first call; padvance will update
- * this value as it proceeds.  Successive calls to padvance will return
+ * of the path before the first call; path_advance will update
+ * this value as it proceeds.  Successive calls to path_advance will return
  * the possible path expansions in sequence.  If an option (indicated by
  * a percent sign) appears in the path entry then the global variable
  * pathopt will be set to point to it; otherwise pathopt will be set to
  * NULL.
  */
-static const char *pathopt;     /* set by padvance */
+static const char *pathopt;     /* set by path_advance */
 
 static char *
-padvance(const char **path, const char *name)
+path_advance(const char **path, const char *name)
 {
        const char *p;
        char *q;
@@ -2346,8 +2343,6 @@ setprompt(int whichprompt)
 #define CD_PHYSICAL 1
 #define CD_PRINT 2
 
-static int docd(const char *, int);
-
 static int
 cdopt(void)
 {
@@ -2355,7 +2350,7 @@ cdopt(void)
        int i, j;
 
        j = 'L';
-       while ((i = nextopt("LP"))) {
+       while ((i = nextopt("LP")) != '\0') {
                if (i != j) {
                        flags ^= CD_PHYSICAL;
                        j = i;
@@ -2499,7 +2494,7 @@ docd(const char *dest, int flags)
        return err;
 }
 
-static int
+static int FAST_FUNC
 cdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 {
        const char *dest;
@@ -2545,7 +2540,7 @@ cdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
        }
        do {
                c = *path;
-               p = padvance(&path, dest);
+               p = path_advance(&path, dest);
                if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
                        if (c && c != ':')
                                flags |= CD_PRINT;
@@ -2563,7 +2558,7 @@ cdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
        return 0;
 }
 
-static int
+static int FAST_FUNC
 pwdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 {
        int flags;
@@ -2626,7 +2621,7 @@ pwdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 #define USE_SIT_FUNCTION
 #endif
 
-#if ENABLE_ASH_MATH_SUPPORT
+#if ENABLE_SH_MATH_SUPPORT
 static const char S_I_T[][4] = {
 #if ENABLE_ASH_ALIAS
        { CSPCL, CIGN, CIGN, CIGN },            /* 0, PEOA */
@@ -2670,7 +2665,7 @@ static const char S_I_T[][3] = {
        { CCTL, CCTL, CCTL }                    /* 14, CTLESC ... */
 #endif
 };
-#endif /* ASH_MATH_SUPPORT */
+#endif /* SH_MATH_SUPPORT */
 
 #ifdef USE_SIT_FUNCTION
 
@@ -2696,23 +2691,26 @@ SIT(int c, int syntax)
        const char *s;
        int indx;
 
-       if (c == PEOF)          /* 2^8+2 */
+       if (c == PEOF) {         /* 2^8+2 */
                return CENDFILE;
+       }
 #if ENABLE_ASH_ALIAS
-       if (c == PEOA)          /* 2^8+1 */
+       if (c == PEOA) {         /* 2^8+1 */
                indx = 0;
-       else
+       else
 #endif
-
-       if ((unsigned char)c >= (unsigned char)(CTLESC)
-        && (unsigned char)c <= (unsigned char)(CTLQUOTEMARK)
-       ) {
-               return CCTL;
+       {
+               if ((unsigned char)c >= (unsigned char)(CTLESC)
+                && (unsigned char)c <= (unsigned char)(CTLQUOTEMARK)
+               ) {
+                       return CCTL;
+               }
+               s = strchrnul(spec_symbls, c);
+               if (*s == '\0') {
+                       return CWORD;
+               }
+               indx = syntax_index_table[s - spec_symbls];
        }
-       s = strchrnul(spec_symbls, c);
-       if (*s == '\0')
-               return CWORD;
-       indx = syntax_index_table[s - spec_symbls];
        return S_I_T[indx][syntax];
 }
 
@@ -3015,7 +3013,7 @@ static const char syntax_index_table[258] = {
        /* 257   127      */ CWORD_CWORD_CWORD_CWORD,
 };
 
-#define SIT(c, syntax) (S_I_T[(int)syntax_index_table[((int)c)+SYNBASE]][syntax])
+#define SIT(c, syntax) (S_I_T[(int)syntax_index_table[(int)(c) + SYNBASE]][syntax])
 
 #endif  /* USE_SIT_FUNCTION */
 
@@ -3165,7 +3163,7 @@ printalias(const struct alias *ap)
 /*
  * TODO - sort output
  */
-static int
+static int FAST_FUNC
 aliascmd(int argc UNUSED_PARAM, char **argv)
 {
        char *n, *v;
@@ -3200,7 +3198,7 @@ aliascmd(int argc UNUSED_PARAM, char **argv)
        return ret;
 }
 
-static int
+static int FAST_FUNC
 unaliascmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 {
        int i;
@@ -3232,9 +3230,9 @@ unaliascmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 #define FORK_NOJOB 2
 
 /* mode flags for showjob(s) */
-#define SHOW_PGID       0x01    /* only show pgid - for jobs -p */
-#define SHOW_PID        0x04    /* include process pid */
-#define SHOW_CHANGED    0x08    /* only jobs whose state has changed */
+#define SHOW_ONLY_PGID  0x01    /* show only pgid (jobs -p) */
+#define SHOW_PIDS       0x02    /* show individual pids, not just one line per job */
+#define SHOW_CHANGED    0x04    /* only jobs whose state has changed */
 
 /*
  * A job structure contains information about a job.  A job is either a
@@ -3242,7 +3240,6 @@ unaliascmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  * latter case, pidlist will be non-NULL, and will point to a -1 terminated
  * array of pids.
  */
-
 struct procstat {
        pid_t   pid;            /* process id */
        int     status;         /* last process status from wait() */
@@ -3286,6 +3283,39 @@ static smallint doing_jobctl; //references:8
 static void setjobctl(int);
 #endif
 
+/*
+ * Ignore a signal.
+ */
+static void
+ignoresig(int signo)
+{
+       /* Avoid unnecessary system calls. Is it already SIG_IGNed? */
+       if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) {
+               /* No, need to do it */
+               signal(signo, SIG_IGN);
+       }
+       sigmode[signo - 1] = S_HARD_IGN;
+}
+
+/*
+ * Signal handler. Only one usage site - in setsignal()
+ */
+static void
+onsig(int signo)
+{
+       gotsig[signo - 1] = 1;
+
+       if (signo == SIGINT && !trap[SIGINT]) {
+               if (!suppress_int) {
+                       pending_sig = 0;
+                       raise_interrupt(); /* does not return */
+               }
+               pending_int = 1;
+       } else {
+               pending_sig = signo;
+       }
+}
+
 /*
  * Set the signal handler for the specified signal.  The routine figures
  * out what it should be set to.
@@ -3469,7 +3499,7 @@ getjob(const char *name, int getctl)
 {
        struct job *jp;
        struct job *found;
-       const char *err_msg = "No such job: %s";
+       const char *err_msg = "%s: no such job";
        unsigned num;
        int c;
        const char *p;
@@ -3505,7 +3535,6 @@ 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;
@@ -3521,10 +3550,8 @@ getjob(const char *name, int getctl)
                p++;
        }
 
-       found = 0;
-       while (1) {
-               if (!jp)
-                       goto err;
+       found = NULL;
+       while (jp) {
                if (match(jp->ps[0].cmd, p)) {
                        if (found)
                                goto err;
@@ -3533,6 +3560,9 @@ getjob(const char *name, int getctl)
                }
                jp = jp->prev_job;
        }
+       if (!found)
+               goto err;
+       jp = found;
 
  gotit:
 #if JOBS
@@ -3651,7 +3681,7 @@ setjobctl(int on)
        doing_jobctl = on;
 }
 
-static int
+static int FAST_FUNC
 killcmd(int argc, char **argv)
 {
        int i = 1;
@@ -3716,7 +3746,7 @@ restartjob(struct job *jp, int mode)
        return status;
 }
 
-static int
+static int FAST_FUNC
 fg_bgcmd(int argc UNUSED_PARAM, char **argv)
 {
        struct job *jp;
@@ -3796,7 +3826,8 @@ dowait(int wait_flags, struct job *job)
         * NB: _not_ safe_waitpid, we need to detect EINTR */
        pid = waitpid(-1, &status,
                        (doing_jobctl ? (wait_flags | WUNTRACED) : wait_flags));
-       TRACE(("wait returns pid=%d, status=0x%x\n", pid, status));
+       TRACE(("wait returns pid=%d, status=0x%x, errno=%d(%s)\n",
+                               pid, status, errno, strerror(errno)));
        if (pid <= 0)
                return pid;
 
@@ -3875,7 +3906,7 @@ static int
 blocking_wait_with_raise_on_sig(struct job *job)
 {
        pid_t pid = dowait(DOWAIT_BLOCK, job);
-       if (pid <= 0 && pendingsig)
+       if (pid <= 0 && pending_sig)
                raise_exception(EXSIG);
        return pid;
 }
@@ -3892,7 +3923,7 @@ showjob(FILE *out, struct job *jp, int mode)
 
        ps = jp->ps;
 
-       if (mode & SHOW_PGID) {
+       if (mode & SHOW_ONLY_PGID) { /* jobs -p */
                /* just output process (group) id of pipeline */
                fprintf(out, "%d\n", ps->pid);
                return;
@@ -3902,11 +3933,11 @@ showjob(FILE *out, struct job *jp, int mode)
        indent_col = col;
 
        if (jp == curjob)
-               s[col - 2] = '+';
+               s[col - 3] = '+';
        else if (curjob && jp == curjob->prev_job)
-               s[col - 2] = '-';
+               s[col - 3] = '-';
 
-       if (mode & SHOW_PID)
+       if (mode & SHOW_PIDS)
                col += fmtstr(s + col, 16, "%d ", ps->pid);
 
        psend = ps + jp->nprocs;
@@ -3920,25 +3951,32 @@ showjob(FILE *out, struct job *jp, int mode)
                        status = jp->stopstatus;
                col += sprint_status(s + col, status, 0);
        }
+       /* By now, "[JOBID]*  [maybe PID] STATUS" is printed */
 
+       /* This loop either prints "<cmd1> | <cmd2> | <cmd3>" line
+        * or prints several "PID             | <cmdN>" lines,
+        * depending on SHOW_PIDS bit.
+        * We do not print status of individual processes
+        * between PID and <cmdN>. bash does it, but not very well:
+        * first line shows overall job status, not process status,
+        * making it impossible to know 1st process status.
+        */
        goto start;
-
-       do {
+       while (1) {
                /* for each process */
-               col = fmtstr(s, 48, " |\n%*c%d ", indent_col, ' ', ps->pid) - 3;
+               s[0] = '\0';
+               col = 33;
+               if (mode & SHOW_PIDS)
+                       col = fmtstr(s, 48, "\n%*c%d ", indent_col, ' ', ps->pid) - 1;
  start:
-               fprintf(out, "%s%*c%s",
-                       s, 33 - col >= 0 ? 33 - col : 0, ' ', ps->cmd
-               );
-               if (!(mode & SHOW_PID)) {
-                       showpipe(jp, out);
+               fprintf(out, "%s%*c", s, 33 - col >= 0 ? 33 - col : 0, ' ');
+               if (ps != jp->ps)
+                       fprintf(out, "| ");
+               fprintf(out, "%s", ps->cmd);
+               if (++ps == psend)
                        break;
-               }
-               if (++ps == psend) {
-                       outcslow('\n', out);
-                       break;
-               }
-       } while (1);
+       }
+       outcslow('\n', out);
 
        jp->changed = 0;
 
@@ -3957,7 +3995,7 @@ showjobs(FILE *out, int mode)
 {
        struct job *jp;
 
-       TRACE(("showjobs(%x) called\n", mode));
+       TRACE(("showjobs(0x%x) called\n", mode));
 
        /* Handle all finished jobs */
        while (dowait(DOWAIT_NONBLOCK, NULL) > 0)
@@ -3970,23 +4008,23 @@ showjobs(FILE *out, int mode)
        }
 }
 
-static int
+static int FAST_FUNC
 jobscmd(int argc UNUSED_PARAM, char **argv)
 {
        int mode, m;
 
        mode = 0;
-       while ((m = nextopt("lp"))) {
+       while ((m = nextopt("lp")) != '\0') {
                if (m == 'l')
-                       mode = SHOW_PID;
+                       mode |= SHOW_PIDS;
                else
-                       mode = SHOW_PGID;
+                       mode |= SHOW_ONLY_PGID;
        }
 
        argv = argptr;
        if (*argv) {
                do
-                       showjob(stdout, getjob(*argv,0), mode);
+                       showjob(stdout, getjob(*argv, 0), mode);
                while (*++argv);
        } else
                showjobs(stdout, mode);
@@ -4018,21 +4056,19 @@ getstatus(struct job *job)
                }
                retval += 128;
        }
-       TRACE(("getstatus: job %d, nproc %d, status %x, retval %x\n",
+       TRACE(("getstatus: job %d, nproc %d, status 0x%x, retval 0x%x\n",
                jobno(job), job->nprocs, status, retval));
        return retval;
 }
 
-static int
+static int FAST_FUNC
 waitcmd(int argc UNUSED_PARAM, char **argv)
 {
        struct job *job;
        int retval;
        struct job *jp;
 
-//     exsig++;
-//     xbarrier();
-       if (pendingsig)
+       if (pending_sig)
                raise_exception(EXSIG);
 
        nextopt(nullstr);
@@ -4189,7 +4225,7 @@ cmdputs(const char *s)
        static const char vstype[VSTYPE + 1][3] = {
                "", "}", "-", "+", "?", "=",
                "%", "%%", "#", "##"
-               USE_ASH_BASH_COMPAT(, ":", "/", "//")
+               IF_ASH_BASH_COMPAT(, ":", "/", "//")
        };
 
        const char *p, *str;
@@ -4228,7 +4264,7 @@ cmdputs(const char *s)
                case CTLBACKQ+CTLQUOTE:
                        str = "\"$(...)\"";
                        goto dostr;
-#if ENABLE_ASH_MATH_SUPPORT
+#if ENABLE_SH_MATH_SUPPORT
                case CTLARI:
                        str = "$((";
                        goto dostr;
@@ -4268,7 +4304,7 @@ cmdputs(const char *s)
                if (!str)
                        continue;
  dostr:
-               while ((c = *str++)) {
+               while ((c = *str++) != '\0') {
                        USTPUTC(c, nextc);
                }
        }
@@ -4345,11 +4381,12 @@ cmdtxt(union node *n)
                cmdputs("if ");
                cmdtxt(n->nif.test);
                cmdputs("; then ");
-               n = n->nif.ifpart;
                if (n->nif.elsepart) {
-                       cmdtxt(n);
+                       cmdtxt(n->nif.ifpart);
                        cmdputs("; else ");
                        n = n->nif.elsepart;
+               } else {
+                       n = n->nif.ifpart;
                }
                p = "; fi";
                goto dotail;
@@ -4499,8 +4536,11 @@ clear_traps(void)
 static void closescript(void);
 
 /* Called after fork(), in child */
+#if !JOBS
+# define forkchild(jp, n, mode) forkchild(jp, mode)
+#endif
 static void
-forkchild(struct job *jp, /*union node *n,*/ int mode)
+forkchild(struct job *jp, union node *n, int mode)
 {
        int oldlvl;
 
@@ -4540,7 +4580,7 @@ forkchild(struct job *jp, /*union node *n,*/ int mode)
                if (jp->nprocs == 0) {
                        close(0);
                        if (open(bb_dev_null, O_RDONLY) != 0)
-                               ash_msg_and_raise_error("can't open %s", bb_dev_null);
+                               ash_msg_and_raise_error("can't open '%s'", bb_dev_null);
                }
        }
        if (!oldlvl) {
@@ -4556,6 +4596,13 @@ forkchild(struct job *jp, /*union node *n,*/ int mode)
                 * Take care of the second rule: */
                setsignal(SIGQUIT);
        }
+#if JOBS
+       if (n && n->type == NCMD && strcmp(n->ncmd.args->narg.text, "jobs") == 0) {
+               TRACE(("Job hack\n"));
+               freejob(curjob);
+               return;
+       }
+#endif
        for (jp = curjob; jp; jp = jp->prev_job)
                freejob(jp);
        jobless = 0;
@@ -4617,7 +4664,7 @@ forkshell(struct job *jp, union node *n, int mode)
                ash_msg_and_raise_error("can't fork");
        }
        if (pid == 0)
-               forkchild(jp, /*n,*/ mode);
+               forkchild(jp, n, mode);
        else
                forkparent(jp, n, mode, pid);
        return pid;
@@ -5103,7 +5150,9 @@ redirect(union node *redir, int flags)
                if (newfd < 0) {
                        /* NTOFD/NFROMFD: copy redir->ndup.dupfd to fd */
                        if (redir->ndup.dupfd < 0) { /* "fd>&-" */
-                               close(fd);
+                               /* Don't want to trigger debugging */
+                               if (fd != -1)
+                                       close(fd);
                        } else {
                                copyfd(redir->ndup.dupfd, fd | COPYFD_EXACT);
                        }
@@ -5156,7 +5205,7 @@ popredir(int drop, int restore)
                                /*close(fd);*/
                                copyfd(copy, fd | COPYFD_EXACT);
                        }
-                       close(copy);
+                       close(copy & ~COPYFD_RESTORE);
                }
        }
        redirlist = rp->next;
@@ -5199,7 +5248,7 @@ redirectsafe(union node *redir, int flags)
                redirect(redir, flags);
        }
        exception_handler = savehandler;
-       if (err && exception != EXERROR)
+       if (err && exception_type != EXERROR)
                longjmp(exception_handler->loc, 1);
        RESTORE_INT(saveint);
        return err;
@@ -5211,17 +5260,33 @@ redirectsafe(union node *redir, int flags)
  * We have to deal with backquotes, shell variables, and file metacharacters.
  */
 
-#if ENABLE_ASH_MATH_SUPPORT_64
-typedef int64_t arith_t;
-#define arith_t_type long long
-#else
-typedef long arith_t;
-#define arith_t_type long
-#endif
+#if ENABLE_SH_MATH_SUPPORT
+static arith_t
+ash_arith(const char *s)
+{
+       arith_eval_hooks_t math_hooks;
+       arith_t result;
+       int errcode = 0;
+
+       math_hooks.lookupvar = lookupvar;
+       math_hooks.setvar = setvar;
+       math_hooks.endofname = endofname;
+
+       INT_OFF;
+       result = arith(s, &errcode, &math_hooks);
+       if (errcode < 0) {
+               if (errcode == -3)
+                       ash_msg_and_raise_error("exponent less than 0");
+               if (errcode == -2)
+                       ash_msg_and_raise_error("divide by zero");
+               if (errcode == -5)
+                       ash_msg_and_raise_error("expression recursion loop detected");
+               raise_error_syntax(s);
+       }
+       INT_ON;
 
-#if ENABLE_ASH_MATH_SUPPORT
-static arith_t dash_arith(const char *);
-static arith_t arith(const char *expr, int *perrcode);
+       return result;
+}
 #endif
 
 /*
@@ -5281,11 +5346,7 @@ cvtnum(arith_t num)
        int len;
 
        expdest = makestrspace(32, expdest);
-#if ENABLE_ASH_MATH_SUPPORT_64
-       len = fmtstr(expdest, 32, "%lld", (long long) num);
-#else
-       len = fmtstr(expdest, 32, "%ld", num);
-#endif
+       len = fmtstr(expdest, 32, arith_t_fmt, num);
        STADJUST(len, expdest);
        return len;
 }
@@ -5305,7 +5366,7 @@ esclen(const char *start, const char *p)
  * Remove any CTLESC characters from a string.
  */
 static char *
-_rmescapes(char *str, int flag)
+rmescapes(char *str, int flag)
 {
        static const char qchars[] ALIGN1 = { CTLESC, CTLQUOTEMARK, '\0' };
 
@@ -5368,8 +5429,6 @@ _rmescapes(char *str, int flag)
        }
        return r;
 }
-#define rmescapes(p) _rmescapes((p), 0)
-
 #define pmatch(a, b) !fnmatch((a), (b), 0)
 
 /*
@@ -5384,7 +5443,7 @@ preglob(const char *pattern, int quoted, int flag)
        if (quoted) {
                flag |= RMESCAPE_QUOTED;
        }
-       return _rmescapes((char *)pattern, flag);
+       return rmescapes((char *)pattern, flag);
 }
 
 /*
@@ -5395,14 +5454,17 @@ memtodest(const char *p, size_t len, int syntax, int quotes)
 {
        char *q = expdest;
 
-       q = makestrspace(len * 2, q);
+       q = makestrspace(quotes ? len * 2 : len, q);
 
        while (len--) {
                int c = signed_char2int(*p++);
                if (!c)
                        continue;
-               if (quotes && (SIT(c, syntax) == CCTL || SIT(c, syntax) == CBACK))
-                       USTPUTC(CTLESC, q);
+               if (quotes) {
+                       int n = SIT(c, syntax);
+                       if (n == CCTL || n == CBACK)
+                               USTPUTC(CTLESC, q);
+               }
                USTPUTC(c, q);
        }
 
@@ -5545,7 +5607,7 @@ static uint8_t back_exitstatus; /* exit status of backquoted command */
 #define EV_EXIT 01              /* exit after evaluating tree */
 static void evaltree(union node *, int);
 
-static void
+static void FAST_FUNC
 evalbackcmd(union node *n, struct backcmd *result)
 {
        int saveherefd;
@@ -5649,7 +5711,7 @@ expbackq(union node *cmd, int quoted, int quotes)
                stackblock() + startloc));
 }
 
-#if ENABLE_ASH_MATH_SUPPORT
+#if ENABLE_SH_MATH_SUPPORT
 /*
  * Expand arithmetic expression.  Backup to start of expression,
  * evaluate, place result in (backed up) result, adjust string position.
@@ -5702,9 +5764,9 @@ expari(int quotes)
        expdest = p;
 
        if (quotes)
-               rmescapes(p + 2);
+               rmescapes(p + 2, 0);
 
-       len = cvtnum(dash_arith(p + 2));
+       len = cvtnum(ash_arith(p + 2));
 
        if (flag != '"')
                recordregion(begoff, begoff + len, 0);
@@ -5735,14 +5797,14 @@ argstr(char *p, int flag, struct strlist *var_str_list)
                CTLVAR,
                CTLBACKQ,
                CTLBACKQ | CTLQUOTE,
-#if ENABLE_ASH_MATH_SUPPORT
+#if ENABLE_SH_MATH_SUPPORT
                CTLENDARI,
 #endif
                0
        };
        const char *reject = spclchars;
        int c;
-       int quotes = flag & (EXP_FULL | EXP_CASE);      /* do CTLESC */
+       int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); /* do CTLESC */
        int breakall = flag & EXP_WORD;
        int inquotes;
        size_t length;
@@ -5772,7 +5834,7 @@ argstr(char *p, int flag, struct strlist *var_str_list)
                length += strcspn(p + length, reject);
                c = p[length];
                if (c && (!(c & 0x80)
-#if ENABLE_ASH_MATH_SUPPORT
+#if ENABLE_SH_MATH_SUPPORT
                                        || c == CTLENDARI
 #endif
                   )) {
@@ -5850,7 +5912,7 @@ argstr(char *p, int flag, struct strlist *var_str_list)
                        expbackq(argbackq->n, c, quotes);
                        argbackq = argbackq->next;
                        goto start;
-#if ENABLE_ASH_MATH_SUPPORT
+#if ENABLE_SH_MATH_SUPPORT
                case CTLENDARI:
                        p--;
                        expari(quotes);
@@ -6025,9 +6087,9 @@ subevalvar(char *p, char *str, int strloc, int subtype,
        char *startp;
        char *loc;
        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;)
+       IF_ASH_BASH_COMPAT(char *repl = NULL;)
+       IF_ASH_BASH_COMPAT(char null = '\0';)
+       IF_ASH_BASH_COMPAT(int pos, len, orig_len;)
        int saveherefd = herefd;
        int amount, workloc, resetloc;
        int zero;
@@ -6051,15 +6113,15 @@ subevalvar(char *p, char *str, int strloc, int subtype,
 #if ENABLE_ASH_BASH_COMPAT
        case VSSUBSTR:
                loc = str = stackblock() + strloc;
-// TODO: number() instead? It does error checking...
-               pos = atoi(loc);
+               /* Read POS in ${var:POS:LEN} */
+               pos = atoi(loc); /* number(loc) errors out on "1:4" */
                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. */
+                       /* Adjust the length by the number of escapes */
                        for (ptr = startp; ptr < (str - 1); ptr++) {
                                if (*ptr == CTLESC) {
                                        len--;
@@ -6070,15 +6132,22 @@ subevalvar(char *p, char *str, int strloc, int subtype,
                orig_len = len;
 
                if (*loc++ == ':') {
-// TODO: number() instead? It does error checking...
-                       len = atoi(loc);
+                       /* ${var::LEN} */
+                       len = number(loc);
                } else {
+                       /* Skip POS in ${var:POS:LEN} */
                        len = orig_len;
-                       while (*loc && *loc != ':')
+                       while (*loc && *loc != ':') {
+                               /* TODO?
+                                * bash complains on: var=qwe; echo ${var:1a:123}
+                               if (!isdigit(*loc))
+                                       ash_msg_and_raise_error(msg_illnum, str);
+                                */
                                loc++;
-                       if (*loc++ == ':')
-// TODO: number() instead? It does error checking...
-                               len = atoi(loc);
+                       }
+                       if (*loc++ == ':') {
+                               len = number(loc);
+                       }
                }
                if (pos >= orig_len) {
                        pos = 0;
@@ -6113,7 +6182,7 @@ subevalvar(char *p, char *str, int strloc, int subtype,
         * stack will need rebasing, and we'll need to remove our work
         * areas each time
         */
USE_ASH_BASH_COMPAT(restart:)
IF_ASH_BASH_COMPAT(restart:)
 
        amount = expdest - ((char *)stackblock() + resetloc);
        STADJUST(-amount, expdest);
@@ -6122,7 +6191,7 @@ subevalvar(char *p, char *str, int strloc, int subtype,
        rmesc = startp;
        rmescend = (char *)stackblock() + strloc;
        if (quotes) {
-               rmesc = _rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW);
+               rmesc = rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW);
                if (rmesc != startp) {
                        rmescend = expdest;
                        startp = (char *)stackblock() + startloc;
@@ -6244,7 +6313,7 @@ static ssize_t
 varvalue(char *name, int varflags, int flags, struct strlist *var_str_list)
 {
        int num;
-       char *p;
+       const char *p;
        int i;
        int sep = 0;
        int sepq = 0;
@@ -6277,14 +6346,13 @@ varvalue(char *name, int varflags, int flags, struct strlist *var_str_list)
                len = cvtnum(num);
                break;
        case '-':
-               p = makestrspace(NOPTS, expdest);
+               expdest = makestrspace(NOPTS, expdest);
                for (i = NOPTS - 1; i >= 0; i--) {
                        if (optlist[i]) {
-                               USTPUTC(optletters(i), p);
+                               USTPUTC(optletters(i), expdest);
                                len++;
                        }
                }
-               expdest = p;
                break;
        case '@':
                if (sep)
@@ -6298,7 +6366,7 @@ varvalue(char *name, int varflags, int flags, struct strlist *var_str_list)
                ap = shellparam.p;
                if (!ap)
                        return -1;
-               while ((p = *ap++)) {
+               while ((p = *ap++) != NULL) {
                        size_t partlen;
 
                        partlen = strlen(p);
@@ -6332,8 +6400,7 @@ varvalue(char *name, int varflags, int flags, struct strlist *var_str_list)
        case '7':
        case '8':
        case '9':
-// TODO: number() instead? It does error checking...
-               num = atoi(name);
+               num = atoi(name); /* number(name) fails on ${var#str} etc */
                if (num < 0 || num > shellparam.nparam)
                        return -1;
                p = num ? shellparam.p[num - 1] : arg0;
@@ -6745,7 +6812,7 @@ expmeta(char *enddir, char *name)
                p++;
        if (*p == '.')
                matchdot++;
-       while (!intpending && (dp = readdir(dirp)) != NULL) {
+       while (!pending_int && (dp = readdir(dirp)) != NULL) {
                if (dp->d_name[0] == '.' && !matchdot)
                        continue;
                if (pmatch(start, dp->d_name)) {
@@ -6866,7 +6933,7 @@ expandmeta(struct strlist *str /*, int flag*/)
                         */
  nometa:
                        *exparg.lastp = str;
-                       rmescapes(str->text);
+                       rmescapes(str->text, 0);
                        exparg.lastp = &str->next;
                } else {
                        *exparg.lastp = NULL;
@@ -6914,7 +6981,7 @@ expandarg(union node *arg, struct arglist *arglist, int flag)
                expandmeta(exparg.list /*, flag*/);
        } else {
                if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
-                       rmescapes(p);
+                       rmescapes(p, 0);
                sp = stzalloc(sizeof(*sp));
                sp->text = p;
                *exparg.lastp = sp;
@@ -6975,7 +7042,7 @@ casematch(union node *pattern, char *val)
 
 struct builtincmd {
        const char *name;
-       int (*builtin)(int, char **);
+       int (*builtin)(int, char **) FAST_FUNC;
        /* unsigned flags; */
 };
 #define IS_BUILTIN_SPECIAL(b) ((b)->name[0] & 1)
@@ -7040,7 +7107,7 @@ static int builtinloc = -1;     /* index in path of %builtin, or -1 */
 
 
 static void
-tryexec(USE_FEATURE_SH_STANDALONE(int applet_no,) char *cmd, char **argv, char **envp)
+tryexec(IF_FEATURE_SH_STANDALONE(int applet_no,) char *cmd, char **argv, char **envp)
 {
        int repeated = 0;
 
@@ -7112,13 +7179,13 @@ shellexec(char **argv, const char *path, int idx)
         || (applet_no = find_applet_by_name(argv[0])) >= 0
 #endif
        ) {
-               tryexec(USE_FEATURE_SH_STANDALONE(applet_no,) argv[0], argv, envp);
+               tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) argv[0], argv, envp);
                e = errno;
        } else {
                e = ENOENT;
-               while ((cmdname = padvance(&path, argv[0])) != NULL) {
+               while ((cmdname = path_advance(&path, argv[0])) != NULL) {
                        if (--idx < 0 && pathopt == NULL) {
-                               tryexec(USE_FEATURE_SH_STANDALONE(-1,) cmdname, argv, envp);
+                               tryexec(IF_FEATURE_SH_STANDALONE(-1,) cmdname, argv, envp);
                                if (errno != ENOENT && errno != ENOTDIR)
                                        e = errno;
                        }
@@ -7139,8 +7206,8 @@ shellexec(char **argv, const char *path, int idx)
                break;
        }
        exitstatus = exerrno;
-       TRACE(("shellexec failed for %s, errno %d, suppressint %d\n",
-               argv[0], e, suppressint));
+       TRACE(("shellexec failed for %s, errno %d, suppress_int %d\n",
+               argv[0], e, suppress_int));
        ash_msg_and_raise(EXEXEC, "%s: %s", argv[0], errmsg(e, "not found"));
        /* NOTREACHED */
 }
@@ -7155,7 +7222,7 @@ printentry(struct tblentry *cmdp)
        idx = cmdp->param.index;
        path = pathval();
        do {
-               name = padvance(&path, cmdp->cmdname);
+               name = path_advance(&path, cmdp->cmdname);
                stunalloc(name);
        } while (--idx >= 0);
        out1fmt("%s%s\n", name, (cmdp->rehash ? "*" : nullstr));
@@ -7269,7 +7336,7 @@ addcmdentry(char *name, struct cmdentry *entry)
        cmdp->rehash = 0;
 }
 
-static int
+static int FAST_FUNC
 hashcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 {
        struct tblentry **pp;
@@ -7339,7 +7406,7 @@ hashcd(void)
  * pathval() still returns the old value at this point.
  * Called with interrupts off.
  */
-static void
+static void FAST_FUNC
 changepath(const char *new)
 {
        const char *old;
@@ -7527,7 +7594,7 @@ describe_command(char *command, int describe_command_verbose)
                        p = command;
                } else {
                        do {
-                               p = padvance(&path, command);
+                               p = path_advance(&path, command);
                                stunalloc(p);
                        } while (--j >= 0);
                }
@@ -7571,7 +7638,7 @@ describe_command(char *command, int describe_command_verbose)
        return 0;
 }
 
-static int
+static int FAST_FUNC
 typecmd(int argc UNUSED_PARAM, char **argv)
 {
        int i = 1;
@@ -7590,7 +7657,7 @@ typecmd(int argc UNUSED_PARAM, char **argv)
 }
 
 #if ENABLE_ASH_CMDCMD
-static int
+static int FAST_FUNC
 commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 {
        int c;
@@ -7913,51 +7980,64 @@ defun(char *name, union node *func)
        INT_ON;
 }
 
-static int evalskip;            /* set if we are skipping commands */
-/* reasons for skipping commands (see comment on breakcmd routine) */
+/* Reasons for skipping commands (see comment on breakcmd routine) */
 #define SKIPBREAK      (1 << 0)
 #define SKIPCONT       (1 << 1)
 #define SKIPFUNC       (1 << 2)
 #define SKIPFILE       (1 << 3)
 #define SKIPEVAL       (1 << 4)
+static smallint evalskip;       /* set to SKIPxxx if we are skipping commands */
 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 */
+/* Forward decl way out to parsing code - dotrap needs it */
 static int evalstring(char *s, int mask);
 
-/*
- * Called to execute a trap.  Perhaps we should avoid entering new trap
- * handlers while we are executing a trap handler.
+/* Called to execute a trap.
+ * Single callsite - at the end of evaltree().
+ * If we return non-zero, exaltree raises EXEXIT exception.
+ *
+ * Perhaps we should avoid entering new trap handlers
+ * while we are executing a trap handler. [is it a TODO?]
  */
 static int
 dotrap(void)
 {
-       char *p;
-       char *q;
-       int i;
-       int savestatus;
-       int skip;
+       uint8_t *g;
+       int sig;
+       uint8_t savestatus;
 
        savestatus = exitstatus;
-       pendingsig = 0;
+       pending_sig = 0;
        xbarrier();
 
-       for (i = 1, q = gotsig; i < NSIG; i++, q++) {
-               if (!*q)
+       TRACE(("dotrap entered\n"));
+       for (sig = 1, g = gotsig; sig < NSIG; sig++, g++) {
+               int want_exexit;
+               char *t;
+
+               if (*g == 0)
+                       continue;
+               t = trap[sig];
+               /* non-trapped SIGINT is handled separately by raise_interrupt,
+                * don't upset it by resetting gotsig[SIGINT-1] */
+               if (sig == SIGINT && !t)
                        continue;
-               *q = '\0';
 
-               p = trap[i];
-               if (!p)
+               TRACE(("sig %d is active, will run handler '%s'\n", sig, t));
+               *g = 0;
+               if (!t)
                        continue;
-               skip = evalstring(p, SKIPEVAL);
+               want_exexit = evalstring(t, SKIPEVAL);
                exitstatus = savestatus;
-               if (skip)
-                       return skip;
+               if (want_exexit) {
+                       TRACE(("dotrap returns %d\n", want_exexit));
+                       return want_exexit;
+               }
        }
 
+       TRACE(("dotrap returns 0\n"));
        return 0;
 }
 
@@ -7979,28 +8059,34 @@ static void prehash(union node *);
 static void
 evaltree(union node *n, int flags)
 {
-
        struct jmploc *volatile savehandler = exception_handler;
        struct jmploc jmploc;
        int checkexit = 0;
        void (*evalfn)(union node *, int);
        int status;
+       int int_level;
+
+       SAVE_INT(int_level);
 
        if (n == NULL) {
                TRACE(("evaltree(NULL) called\n"));
                goto out1;
        }
-       TRACE(("pid %d, evaltree(%p: %d, %d) called\n",
-                       getpid(), n, n->type, flags));
+       TRACE(("evaltree(%p: %d, %d) called\n", n, n->type, flags));
 
        exception_handler = &jmploc;
        {
                int err = setjmp(jmploc.loc);
                if (err) {
                        /* if it was a signal, check for trap handlers */
-                       if (exception == EXSIG)
+                       if (exception_type == EXSIG) {
+                               TRACE(("exception %d (EXSIG) in evaltree, err=%d\n",
+                                               exception_type, err));
                                goto out;
+                       }
                        /* continue on the way out */
+                       TRACE(("exception %d in evaltree, propagating err=%d\n",
+                                       exception_type, err));
                        exception_handler = savehandler;
                        longjmp(exception_handler->loc, err);
                }
@@ -8083,7 +8169,8 @@ evaltree(union node *n, int flags)
                if (exitstatus == 0) {
                        n = n->nif.ifpart;
                        goto evaln;
-               } else if (n->nif.elsepart) {
+               }
+               if (n->nif.elsepart) {
                        n = n->nif.elsepart;
                        goto evaln;
                }
@@ -8102,13 +8189,16 @@ evaltree(union node *n, int flags)
  out1:
        if (checkexit & exitstatus)
                evalskip |= SKIPEVAL;
-       else if (pendingsig && dotrap())
+       else if (pending_sig && dotrap())
                goto exexit;
 
        if (flags & EV_EXIT) {
  exexit:
                raise_exception(EXEXIT);
        }
+
+       RESTORE_INT(int_level);
+       TRACE(("leaving evaltree (no interrupts)\n"));
 }
 
 #if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3)
@@ -8354,7 +8444,9 @@ evalpipe(union node *n, int flags)
                if (prevfd >= 0)
                        close(prevfd);
                prevfd = pip[0];
-               close(pip[1]);
+               /* Don't want to trigger debugging */
+               if (pip[1] != -1)
+                       close(pip[1]);
        }
        if (n->npipe.pipe_backgnd == 0) {
                exitstatus = waitforjob(jp);
@@ -8428,7 +8520,7 @@ poplocalvars(void)
        while ((lvp = localvars) != NULL) {
                localvars = lvp->next;
                vp = lvp->vp;
-               TRACE(("poplocalvar %s", vp ? vp->text : "-"));
+               TRACE(("poplocalvar %s\n", vp ? vp->text : "-"));
                if (vp == NULL) {       /* $- saved */
                        memcpy(optlist, lvp->text, sizeof(optlist));
                        free((char*)lvp->text);
@@ -8576,7 +8668,7 @@ mklocal(char *name)
 /*
  * The "local" command.
  */
-static int
+static int FAST_FUNC
 localcmd(int argc UNUSED_PARAM, char **argv)
 {
        char *name;
@@ -8588,19 +8680,19 @@ localcmd(int argc UNUSED_PARAM, char **argv)
        return 0;
 }
 
-static int
+static int FAST_FUNC
 falsecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 {
        return 1;
 }
 
-static int
+static int FAST_FUNC
 truecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 {
        return 0;
 }
 
-static int
+static int FAST_FUNC
 execcmd(int argc UNUSED_PARAM, char **argv)
 {
        if (argv[1]) {
@@ -8615,7 +8707,7 @@ execcmd(int argc UNUSED_PARAM, char **argv)
 /*
  * The return command.
  */
-static int
+static int FAST_FUNC
 returncmd(int argc UNUSED_PARAM, char **argv)
 {
        /*
@@ -8627,28 +8719,28 @@ returncmd(int argc UNUSED_PARAM, char **argv)
 }
 
 /* Forward declarations for builtintab[] */
-static int breakcmd(int, char **);
-static int dotcmd(int, char **);
-static int evalcmd(int, char **);
-static int exitcmd(int, char **);
-static int exportcmd(int, char **);
+static int breakcmd(int, char **) FAST_FUNC;
+static int dotcmd(int, char **) FAST_FUNC;
+static int evalcmd(int, char **) FAST_FUNC;
+static int exitcmd(int, char **) FAST_FUNC;
+static int exportcmd(int, char **) FAST_FUNC;
 #if ENABLE_ASH_GETOPTS
-static int getoptscmd(int, char **);
+static int getoptscmd(int, char **) FAST_FUNC;
 #endif
 #if !ENABLE_FEATURE_SH_EXTRA_QUIET
-static int helpcmd(int, char **);
+static int helpcmd(int, char **) FAST_FUNC;
 #endif
-#if ENABLE_ASH_MATH_SUPPORT
-static int letcmd(int, char **);
+#if ENABLE_SH_MATH_SUPPORT
+static int letcmd(int, char **) FAST_FUNC;
 #endif
-static int readcmd(int, char **);
-static int setcmd(int, char **);
-static int shiftcmd(int, char **);
-static int timescmd(int, char **);
-static int trapcmd(int, char **);
-static int umaskcmd(int, char **);
-static int unsetcmd(int, char **);
-static int ulimitcmd(int, char **);
+static int readcmd(int, char **) FAST_FUNC;
+static int setcmd(int, char **) FAST_FUNC;
+static int shiftcmd(int, char **) FAST_FUNC;
+static int timescmd(int, char **) FAST_FUNC;
+static int trapcmd(int, char **) FAST_FUNC;
+static int umaskcmd(int, char **) FAST_FUNC;
+static int unsetcmd(int, char **) FAST_FUNC;
+static int ulimitcmd(int, char **) FAST_FUNC;
 
 #define BUILTIN_NOSPEC          "0"
 #define BUILTIN_SPECIAL         "1"
@@ -8659,21 +8751,16 @@ static int ulimitcmd(int, char **);
 #define BUILTIN_REG_ASSG        "6"
 #define BUILTIN_SPEC_REG_ASSG   "7"
 
-/* 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
+/* Stubs for calling non-FAST_FUNC's */
+#if ENABLE_ASH_BUILTIN_ECHO
+static int FAST_FUNC echocmd(int argc, char **argv)   { return echo_main(argc, argv); }
+#endif
+#if ENABLE_ASH_BUILTIN_PRINTF
+static int FAST_FUNC printfcmd(int argc, char **argv) { return printf_main(argc, argv); }
+#endif
+#if ENABLE_ASH_BUILTIN_TEST
+static int FAST_FUNC testcmd(int argc, char **argv)   { return test_main(argc, argv); }
+#endif
 
 /* Keep these in proper order since it is searched via bsearch() */
 static const struct builtincmd builtintab[] = {
@@ -8720,7 +8807,7 @@ static const struct builtincmd builtintab[] = {
        { BUILTIN_REGULAR       "jobs", jobscmd },
        { BUILTIN_REGULAR       "kill", killcmd },
 #endif
-#if ENABLE_ASH_MATH_SUPPORT
+#if ENABLE_SH_MATH_SUPPORT
        { BUILTIN_NOSPEC        "let", letcmd },
 #endif
        { BUILTIN_ASSIGN        "local", localcmd },
@@ -8796,7 +8883,7 @@ isassignment(const char *p)
                return 0;
        return *q == '=';
 }
-static int
+static int FAST_FUNC
 bltincmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 {
        /* Preserve exitstatus of a previous possible redirection
@@ -8986,6 +9073,7 @@ evalcommand(union node *cmd, int flags)
                        if (forkshell(jp, cmd, FORK_FG) != 0) {
                                exitstatus = waitforjob(jp);
                                INT_ON;
+                               TRACE(("forked child exited with %d\n", exitstatus));
                                break;
                        }
                        FORCE_INT_ON;
@@ -9014,14 +9102,14 @@ evalcommand(union node *cmd, int flags)
 
                if (evalbltin(cmdentry.u.cmd, argc, argv)) {
                        int exit_status;
-                       int i = exception;
+                       int i = exception_type;
                        if (i == EXEXIT)
                                goto raise;
                        exit_status = 2;
                        if (i == EXINT)
                                exit_status = 128 + SIGINT;
                        if (i == EXSIG)
-                               exit_status = 128 + pendingsig;
+                               exit_status = 128 + pending_sig;
                        exitstatus = exit_status;
                        if (i == EXINT || spclbltin > 0) {
  raise:
@@ -9075,7 +9163,6 @@ evalbltin(const struct builtincmd *cmd, int argc, char **argv)
        exitstatus |= ferror(stdout);
        clearerr(stdout);
        commandname = savecmdname;
-//     exsig = 0;
        exception_handler = savehandler;
 
        return i;
@@ -9120,13 +9207,13 @@ prehash(union node *n)
  * be an error to break out of more loops than exist, but it isn't
  * in the standard shell so we don't make it one here.
  */
-static int
+static int FAST_FUNC
 breakcmd(int argc UNUSED_PARAM, char **argv)
 {
        int n = argv[1] ? number(argv[1]) : 1;
 
        if (n <= 0)
-               ash_msg_and_raise_error(illnum, argv[1]);
+               ash_msg_and_raise_error(msg_illnum, argv[1]);
        if (n > loopnest)
                n = loopnest;
        if (n > 0) {
@@ -9284,7 +9371,7 @@ preadfd(void)
  * 2) If an EOF was pushed back (g_parsefile->left_in_line < -BIGNUM)
  *    or we are reading from a string so we can't refill the buffer,
  *    return EOF.
- * 3) If the is more stuff in this buffer, use it else call read to fill it.
+ * 3) If there is more stuff in this buffer, use it else call read to fill it.
  * 4) Process input up to the next newline, deleting nul characters.
  */
 //#define pgetc_debug(...) bb_error_msg(__VA_ARGS__)
@@ -9573,7 +9660,7 @@ setinputfile(const char *fname, int flags)
        if (fd < 0) {
                if (flags & INPUT_NOFILE_OK)
                        goto out;
-               ash_msg_and_raise_error("can't open %s", fname);
+               ash_msg_and_raise_error("can't open '%s'", fname);
        }
        if (fd < 10) {
                fd2 = copyfd(fd, 10);
@@ -9637,7 +9724,7 @@ chkmail(void)
        setstackmark(&smark);
        mpath = mpathset() ? mpathval() : mailval();
        for (mtp = mailtime; mtp < mailtime + MAXMBOXES; mtp++) {
-               p = padvance(&mpath, nullstr);
+               p = path_advance(&mpath, nullstr);
                if (p == NULL)
                        break;
                if (*p == '\0')
@@ -9665,7 +9752,7 @@ chkmail(void)
        popstackmark(&smark);
 }
 
-static void
+static void FAST_FUNC
 changemail(const char *val UNUSED_PARAM)
 {
        mail_var_path_changed = 1;
@@ -9821,7 +9908,7 @@ options(int cmdline)
 /*
  * The shift builtin command.
  */
-static int
+static int FAST_FUNC
 shiftcmd(int argc UNUSED_PARAM, char **argv)
 {
        int n;
@@ -9883,7 +9970,7 @@ showvars(const char *sep_prefix, int on, int off)
 /*
  * The set command builtin.
  */
-static int
+static int FAST_FUNC
 setcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 {
        int retval;
@@ -9904,7 +9991,7 @@ setcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 }
 
 #if ENABLE_ASH_RANDOM_SUPPORT
-static void
+static void FAST_FUNC
 change_random(const char *value)
 {
        /* Galois LFSR parameter */
@@ -10034,7 +10121,7 @@ getopts(char *optstr, char *optvar, char **optfirst, int *param_optind, int *opt
  * be processed in the current argument.  If shellparam.optnext is NULL,
  * then it's the first time getopts has been called.
  */
-static int
+static int FAST_FUNC
 getoptscmd(int argc, char **argv)
 {
        char **optbase;
@@ -10079,20 +10166,6 @@ static char *wordtext;                 /* text of last word returned by readtoke
 static struct nodelist *backquotelist;
 static union node *redirnode;
 static struct heredoc *heredoc;
-/*
- * 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 *) NORETURN;
-static void
-raise_error_syntax(const char *msg)
-{
-       ash_msg_and_raise_error("syntax error: %s", msg);
-       /* NOTREACHED */
-}
 
 /*
  * Called when an unexpected token is read during the parse.  The argument
@@ -10332,7 +10405,7 @@ parsefname(void)
                TRACE(("Here document %d\n", n->type));
                if (!noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
                        raise_error_syntax("illegal eof marker for << redirection");
-               rmescapes(wordtext);
+               rmescapes(wordtext, 0);
                here->eofmark = wordtext;
                here->next = NULL;
                if (heredoclist == NULL)
@@ -10728,7 +10801,7 @@ 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_ASH_BASH_COMPAT(smallint bash_dollar_squote = 0;)
 
 #if __GNUC__
        /* Avoid longjmp clobbering */
@@ -10834,7 +10907,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_ASH_BASH_COMPAT(bash_dollar_squote = 0;)
                                if (eofmark != NULL && arinest == 0
                                 && varnest == 0
                                ) {
@@ -10862,7 +10935,7 @@ readtoken1(int firstc, int syntax, char *eofmark, int striptabs)
                                        USTPUTC(c, out);
                                }
                                break;
-#if ENABLE_ASH_MATH_SUPPORT
+#if ENABLE_SH_MATH_SUPPORT
                        case CLP:       /* '(' in arithmetic */
                                parenlevel++;
                                USTPUTC(c, out);
@@ -10918,7 +10991,7 @@ readtoken1(int firstc, int syntax, char *eofmark, int striptabs)
                } /* for (;;) */
        }
  endword:
-#if ENABLE_ASH_MATH_SUPPORT
+#if ENABLE_SH_MATH_SUPPORT
        if (syntax == ARISYNTAX)
                raise_error_syntax("missing '))'");
 #endif
@@ -10933,7 +11006,7 @@ readtoken1(int firstc, int syntax, char *eofmark, int striptabs)
        len = out - (char *)stackblock();
        out = stackblock();
        if (eofmark == NULL) {
-               if ((c == '>' || c == '<' USE_ASH_BASH_COMPAT( || c == 0x100 + '>'))
+               if ((c == '>' || c == '<' IF_ASH_BASH_COMPAT( || c == 0x100 + '>'))
                 && quotef == 0
                ) {
                        if (isdigit_str9(out)) {
@@ -11095,7 +11168,7 @@ parsesub: {
                pungetc();
        } else if (c == '(') {  /* $(command) or $((arith)) */
                if (pgetc() == '(') {
-#if ENABLE_ASH_MATH_SUPPORT
+#if ENABLE_SH_MATH_SUPPORT
                        PARSEARITH();
 #else
                        raise_error_syntax("you disabled math support for $((arith)) syntax");
@@ -11350,7 +11423,7 @@ parsebackq: {
        goto parsebackq_newreturn;
 }
 
-#if ENABLE_ASH_MATH_SUPPORT
+#if ENABLE_SH_MATH_SUPPORT
 /*
  * Parse an arithmetic expansion (indicate start of one and set state)
  */
@@ -11427,7 +11500,7 @@ xxreadtoken(void)
        startlinno = g_parsefile->linno;
        for (;;) {                      /* until token or start of word found */
                c = pgetc_fast();
-               if (c == ' ' || c == '\t' USE_ASH_ALIAS( || c == PEOA))
+               if (c == ' ' || c == '\t' IF_ASH_ALIAS( || c == PEOA))
                        continue;
 
                if (c == '#') {
@@ -11624,8 +11697,8 @@ peektoken(void)
 }
 
 /*
- * Read and parse a command.  Returns NEOF on end of file.  (NULL is a
- * valid parse tree indicating a blank line.)
+ * Read and parse a command.  Returns NODE_EOF on end of file.
+ * (NULL is a valid parse tree indicating a blank line.)
  */
 static union node *
 parsecmd(int interact)
@@ -11639,7 +11712,7 @@ parsecmd(int interact)
        needprompt = 0;
        t = readtoken();
        if (t == TEOF)
-               return NEOF;
+               return NODE_EOF;
        if (t == TNL)
                return NULL;
        tokpushback = 1;
@@ -11684,7 +11757,8 @@ expandstr(const char *ps)
 {
        union node n;
 
-       /* XXX Fix (char *) cast. */
+       /* XXX Fix (char *) cast. It _is_ a bug. ps is variable's value,
+        * and token processing _can_ alter it (delete NULs etc). */
        setinputstring((char *)ps);
        readtoken1(pgetc(), PSSYNTAX, nullstr, 0);
        popfile();
@@ -11713,7 +11787,7 @@ evalstring(char *s, int mask)
        setstackmark(&smark);
 
        skip = 0;
-       while ((n = parsecmd(0)) != NEOF) {
+       while ((n = parsecmd(0)) != NODE_EOF) {
                evaltree(n, 0);
                popstackmark(&smark);
                skip = evalskip;
@@ -11730,7 +11804,7 @@ evalstring(char *s, int mask)
 /*
  * The eval command.
  */
-static int
+static int FAST_FUNC
 evalcmd(int argc UNUSED_PARAM, char **argv)
 {
        char *p;
@@ -11786,8 +11860,11 @@ cmdloop(int top)
 #endif
                }
                n = parsecmd(inter);
-               /* showtree(n); DEBUG */
-               if (n == NEOF) {
+#if DEBUG
+               if (DEBUG > 2 && debug && (n != NODE_EOF))
+                       showtree(n);
+#endif
+               if (n == NODE_EOF) {
                        if (!top || numeof >= 50)
                                break;
                        if (!stoppedjobs()) {
@@ -11828,7 +11905,16 @@ find_dot_file(char *name)
        if (strchr(name, '/'))
                return name;
 
-       while ((fullname = padvance(&path, name)) != NULL) {
+       /* IIRC standards do not say whether . is to be searched.
+        * And it is even smaller this way, making it unconditional for now:
+        */
+       if (1) { /* ENABLE_ASH_BASH_COMPAT */
+               fullname = name;
+               goto try_cur_dir;
+       }
+
+       while ((fullname = path_advance(&path, name)) != NULL) {
+ try_cur_dir:
                if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
                        /*
                         * Don't bother freeing here, since it will
@@ -11836,7 +11922,8 @@ find_dot_file(char *name)
                         */
                        return fullname;
                }
-               stunalloc(fullname);
+               if (fullname != name)
+                       stunalloc(fullname);
        }
 
        /* not found in the PATH */
@@ -11844,7 +11931,7 @@ find_dot_file(char *name)
        /* NOTREACHED */
 }
 
-static int
+static int FAST_FUNC
 dotcmd(int argc, char **argv)
 {
        struct strlist *sp;
@@ -11879,7 +11966,7 @@ dotcmd(int argc, char **argv)
        return status;
 }
 
-static int
+static int FAST_FUNC
 exitcmd(int argc UNUSED_PARAM, char **argv)
 {
        if (stoppedjobs())
@@ -12010,7 +12097,7 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path)
        e = ENOENT;
        idx = -1;
  loop:
-       while ((fullname = padvance(&path, name)) != NULL) {
+       while ((fullname = path_advance(&path, name)) != NULL) {
                stunalloc(fullname);
                /* NB: code below will still use fullname
                 * despite it being "unallocated" */
@@ -12103,7 +12190,7 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path)
 /*
  * The trap builtin.
  */
-static int
+static int FAST_FUNC
 trapcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 {
        char *action;
@@ -12153,13 +12240,15 @@ trapcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 /*
  * Lists available builtins
  */
-static int
+static int FAST_FUNC
 helpcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 {
        unsigned col;
        unsigned i;
 
-       out1fmt("\nBuilt-in commands:\n-------------------\n");
+       out1fmt(
+               "Built-in commands:\n"
+               "------------------\n");
        for (col = 0, i = 0; i < ARRAY_SIZE(builtintab); i++) {
                col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '),
                                        builtintab[i].name + 1);
@@ -12189,7 +12278,7 @@ helpcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 /*
  * The export and readonly commands.
  */
-static int
+static int FAST_FUNC
 exportcmd(int argc UNUSED_PARAM, char **argv)
 {
        struct var *vp;
@@ -12240,7 +12329,7 @@ unsetfunc(const char *name)
  * variable to allow a function to be unset when there is a readonly variable
  * with the same name.
  */
-static int
+static int FAST_FUNC
 unsetcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 {
        char **ap;
@@ -12278,7 +12367,7 @@ static const unsigned char timescmd_str[] ALIGN1 = {
        0
 };
 
-static int
+static int FAST_FUNC
 timescmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 {
        long clk_tck, s, t;
@@ -12301,36 +12390,14 @@ timescmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
        return 0;
 }
 
-#if ENABLE_ASH_MATH_SUPPORT
-static arith_t
-dash_arith(const char *s)
-{
-       arith_t result;
-       int errcode = 0;
-
-       INT_OFF;
-       result = arith(s, &errcode);
-       if (errcode < 0) {
-               if (errcode == -3)
-                       ash_msg_and_raise_error("exponent less than 0");
-               if (errcode == -2)
-                       ash_msg_and_raise_error("divide by zero");
-               if (errcode == -5)
-                       ash_msg_and_raise_error("expression recursion loop detected");
-               raise_error_syntax(s);
-       }
-       INT_ON;
-
-       return result;
-}
-
+#if ENABLE_SH_MATH_SUPPORT
 /*
- *  The let builtin. partial stolen from GNU Bash, the Bourne Again SHell.
- *  Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
+ * The let builtin. partial stolen from GNU Bash, the Bourne Again SHell.
+ * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
  *
- *  Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru>
+ * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru>
  */
-static int
+static int FAST_FUNC
 letcmd(int argc UNUSED_PARAM, char **argv)
 {
        arith_t i;
@@ -12339,12 +12406,12 @@ letcmd(int argc UNUSED_PARAM, char **argv)
        if (!*argv)
                ash_msg_and_raise_error("expression expected");
        do {
-               i = dash_arith(*argv);
+               i = ash_arith(*argv);
        } while (*++argv);
 
        return !i;
 }
-#endif /* ASH_MATH_SUPPORT */
+#endif /* SH_MATH_SUPPORT */
 
 
 /* ============ miscbltin.c
@@ -12372,7 +12439,7 @@ typedef enum __rlimit_resource rlim_t;
  *      -d DELIM        End on DELIM char, not newline
  *      -e              Use line editing (tty only)
  */
-static int
+static int FAST_FUNC
 readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 {
        static const char *const arg_REPLY[] = { "REPLY", NULL };
@@ -12401,8 +12468,8 @@ readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
        rflag = 0;
        prompt = NULL;
        while ((i = nextopt("p:u:r"
-               USE_ASH_READ_TIMEOUT("t:")
-               USE_ASH_READ_NCHARS("n:s")
+               IF_ASH_READ_TIMEOUT("t:")
+               IF_ASH_READ_NCHARS("n:s")
        )) != '\0') {
                switch (i) {
                case 'p':
@@ -12491,7 +12558,7 @@ readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 #endif
 
        status = 0;
-       startword = 1;
+       startword = 2;
        backslash = 0;
 #if ENABLE_ASH_READ_TIMEOUT
        if (timeout) /* NB: ensuring end_ms is nonzero */
@@ -12499,6 +12566,8 @@ readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 #endif
        STARTSTACKSTR(p);
        do {
+               const char *is_ifs;
+
 #if ENABLE_ASH_READ_TIMEOUT
                if (end_ms) {
                        struct pollfd pfd[1];
@@ -12528,25 +12597,36 @@ readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
                        continue;
                }
                if (!rflag && c == '\\') {
-                       backslash++;
+                       backslash = 1;
                        continue;
                }
                if (c == '\n')
                        break;
-               if (startword && *ifs == ' ' && strchr(ifs, c)) {
-                       continue;
+               /* $IFS splitting */
+/* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05 */
+               is_ifs = strchr(ifs, c);
+               if (startword && is_ifs) {
+                       if (isspace(c))
+                               continue;
+                       /* it is a non-space ifs char */
+                       startword--;
+                       if (startword == 1) /* first one? */
+                               continue; /* yes, it is not next word yet */
                }
                startword = 0;
-               if (ap[1] != NULL && strchr(ifs, c) != NULL) {
+               if (ap[1] != NULL && is_ifs) {
+                       const char *beg;
                        STACKSTRNUL(p);
-                       setvar(*ap, stackblock(), 0);
+                       beg = stackblock();
+                       setvar(*ap, beg, 0);
                        ap++;
-                       startword = 1;
+                       /* can we skip one non-space ifs char? (2: yes) */
+                       startword = isspace(c) ? 2 : 1;
                        STARTSTACKSTR(p);
-               } else {
- put:
-                       STPUTC(c, p);
+                       continue;
                }
+ put:
+               STPUTC(c, p);
        }
 /* end of do {} while: */
 #if ENABLE_ASH_READ_NCHARS
@@ -12560,8 +12640,8 @@ readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 #endif
 
        STACKSTRNUL(p);
-       /* Remove trailing blanks */
-       while ((char *)stackblock() <= --p && strchr(ifs, *p) != NULL)
+       /* Remove trailing space ifs chars */
+       while ((char *)stackblock() <= --p && isspace(*p) && strchr(ifs, *p) != NULL)
                *p = '\0';
        setvar(*ap, stackblock(), 0);
        while (*++ap != NULL)
@@ -12569,7 +12649,7 @@ readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
        return status;
 }
 
-static int
+static int FAST_FUNC
 umaskcmd(int argc UNUSED_PARAM, char **argv)
 {
        static const char permuser[3] ALIGN1 = "ugo";
@@ -12580,6 +12660,8 @@ umaskcmd(int argc UNUSED_PARAM, char **argv)
                S_IROTH, S_IWOTH, S_IXOTH
        };
 
+       /* TODO: use bb_parse_mode() instead */
+
        char *ap;
        mode_t mask;
        int i;
@@ -12622,7 +12704,7 @@ umaskcmd(int argc UNUSED_PARAM, char **argv)
                        mask = 0;
                        do {
                                if (*ap >= '8' || *ap < '0')
-                                       ash_msg_and_raise_error(illnum, argv[1]);
+                                       ash_msg_and_raise_error(msg_illnum, argv[1]);
                                mask = (mask << 3) + (*ap - '0');
                        } while (*++ap != '\0');
                        umask(mask);
@@ -12646,7 +12728,6 @@ umaskcmd(int argc UNUSED_PARAM, char **argv)
  *
  * Public domain.
  */
-
 struct limits {
        uint8_t cmd;          /* RLIMIT_xxx fit into it */
        uint8_t factor_shift; /* shift by to get rlim_{cur,max} values */
@@ -12744,7 +12825,7 @@ printlim(enum limtype how, const struct rlimit *limit,
        }
 }
 
-static int
+static int FAST_FUNC
 ulimitcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 {
        int c;
@@ -12855,654 +12936,6 @@ ulimitcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
        return 0;
 }
 
-
-/* ============ Math support */
-
-#if ENABLE_ASH_MATH_SUPPORT
-
-/* Copyright (c) 2001 Aaron Lehmann <aaronl@vitelus.com>
-
-   Permission is hereby granted, free of charge, to any person obtaining
-   a copy of this software and associated documentation files (the
-   "Software"), to deal in the Software without restriction, including
-   without limitation the rights to use, copy, modify, merge, publish,
-   distribute, sublicense, and/or sell copies of the Software, and to
-   permit persons to whom the Software is furnished to do so, subject to
-   the following conditions:
-
-   The above copyright notice and this permission notice shall be
-   included in all copies or substantial portions of the Software.
-
-   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
-   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-/* This is my infix parser/evaluator. It is optimized for size, intended
- * as a replacement for yacc-based parsers. However, it may well be faster
- * than a comparable parser written in yacc. The supported operators are
- * listed in #defines below. Parens, order of operations, and error handling
- * are supported. This code is thread safe. The exact expression format should
- * be that which POSIX specifies for shells. */
-
-/* The code uses a simple two-stack algorithm. See
- * http://www.onthenet.com.au/~grahamis/int2008/week02/lect02.html
- * for a detailed explanation of the infix-to-postfix algorithm on which
- * this is based (this code differs in that it applies operators immediately
- * to the stack instead of adding them to a queue to end up with an
- * expression). */
-
-/* To use the routine, call it with an expression string and error return
- * pointer */
-
-/*
- * Aug 24, 2001              Manuel Novoa III
- *
- * Reduced the generated code size by about 30% (i386) and fixed several bugs.
- *
- * 1) In arith_apply():
- *    a) Cached values of *numptr and &(numptr[-1]).
- *    b) Removed redundant test for zero denominator.
- *
- * 2) In arith():
- *    a) Eliminated redundant code for processing operator tokens by moving
- *       to a table-based implementation.  Also folded handling of parens
- *       into the table.
- *    b) Combined all 3 loops which called arith_apply to reduce generated
- *       code size at the cost of speed.
- *
- * 3) The following expressions were treated as valid by the original code:
- *       1()  ,    0!  ,    1 ( *3 )   .
- *    These bugs have been fixed by internally enclosing the expression in
- *    parens and then checking that all binary ops and right parens are
- *    preceded by a valid expression (NUM_TOKEN).
- *
- * Note: It may be desirable to replace Aaron's test for whitespace with
- * ctype's isspace() if it is used by another busybox applet or if additional
- * whitespace chars should be considered.  Look below the "#include"s for a
- * precompiler test.
- */
-
-/*
- * Aug 26, 2001              Manuel Novoa III
- *
- * Return 0 for null expressions.  Pointed out by Vladimir Oleynik.
- *
- * Merge in Aaron's comments previously posted to the busybox list,
- * modified slightly to take account of my changes to the code.
- *
- */
-
-/*
- *  (C) 2003 Vladimir Oleynik <dzo@simtreas.ru>
- *
- * - allow access to variable,
- *   used recursive find value indirection (c=2*2; a="c"; $((a+=2)) produce 6)
- * - realize assign syntax (VAR=expr, +=, *= etc)
- * - realize exponentiation (** operator)
- * - realize comma separated - expr, expr
- * - realise ++expr --expr expr++ expr--
- * - realise expr ? expr : expr (but, second expr calculate always)
- * - allow hexadecimal and octal numbers
- * - was restored loses XOR operator
- * - remove one goto label, added three ;-)
- * - protect $((num num)) as true zero expr (Manuel`s error)
- * - always use special isspace(), see comment from bash ;-)
- */
-
-#define arith_isspace(arithval) \
-       (arithval == ' ' || arithval == '\n' || arithval == '\t')
-
-typedef unsigned char operator;
-
-/* An operator's token id is a bit of a bitfield. The lower 5 bits are the
- * precedence, and 3 high bits are an ID unique across operators of that
- * precedence. The ID portion is so that multiple operators can have the
- * same precedence, ensuring that the leftmost one is evaluated first.
- * Consider * and /. */
-
-#define tok_decl(prec,id) (((id)<<5)|(prec))
-#define PREC(op) ((op) & 0x1F)
-
-#define TOK_LPAREN tok_decl(0,0)
-
-#define TOK_COMMA tok_decl(1,0)
-
-#define TOK_ASSIGN tok_decl(2,0)
-#define TOK_AND_ASSIGN tok_decl(2,1)
-#define TOK_OR_ASSIGN tok_decl(2,2)
-#define TOK_XOR_ASSIGN tok_decl(2,3)
-#define TOK_PLUS_ASSIGN tok_decl(2,4)
-#define TOK_MINUS_ASSIGN tok_decl(2,5)
-#define TOK_LSHIFT_ASSIGN tok_decl(2,6)
-#define TOK_RSHIFT_ASSIGN tok_decl(2,7)
-
-#define TOK_MUL_ASSIGN tok_decl(3,0)
-#define TOK_DIV_ASSIGN tok_decl(3,1)
-#define TOK_REM_ASSIGN tok_decl(3,2)
-
-/* all assign is right associativity and precedence eq, but (7+3)<<5 > 256 */
-#define convert_prec_is_assing(prec) do { if (prec == 3) prec = 2; } while (0)
-
-/* conditional is right associativity too */
-#define TOK_CONDITIONAL tok_decl(4,0)
-#define TOK_CONDITIONAL_SEP tok_decl(4,1)
-
-#define TOK_OR tok_decl(5,0)
-
-#define TOK_AND tok_decl(6,0)
-
-#define TOK_BOR tok_decl(7,0)
-
-#define TOK_BXOR tok_decl(8,0)
-
-#define TOK_BAND tok_decl(9,0)
-
-#define TOK_EQ tok_decl(10,0)
-#define TOK_NE tok_decl(10,1)
-
-#define TOK_LT tok_decl(11,0)
-#define TOK_GT tok_decl(11,1)
-#define TOK_GE tok_decl(11,2)
-#define TOK_LE tok_decl(11,3)
-
-#define TOK_LSHIFT tok_decl(12,0)
-#define TOK_RSHIFT tok_decl(12,1)
-
-#define TOK_ADD tok_decl(13,0)
-#define TOK_SUB tok_decl(13,1)
-
-#define TOK_MUL tok_decl(14,0)
-#define TOK_DIV tok_decl(14,1)
-#define TOK_REM tok_decl(14,2)
-
-/* exponent is right associativity */
-#define TOK_EXPONENT tok_decl(15,1)
-
-/* For now unary operators. */
-#define UNARYPREC 16
-#define TOK_BNOT tok_decl(UNARYPREC,0)
-#define TOK_NOT tok_decl(UNARYPREC,1)
-
-#define TOK_UMINUS tok_decl(UNARYPREC+1,0)
-#define TOK_UPLUS tok_decl(UNARYPREC+1,1)
-
-#define PREC_PRE (UNARYPREC+2)
-
-#define TOK_PRE_INC tok_decl(PREC_PRE, 0)
-#define TOK_PRE_DEC tok_decl(PREC_PRE, 1)
-
-#define PREC_POST (UNARYPREC+3)
-
-#define TOK_POST_INC tok_decl(PREC_POST, 0)
-#define TOK_POST_DEC tok_decl(PREC_POST, 1)
-
-#define SPEC_PREC (UNARYPREC+4)
-
-#define TOK_NUM tok_decl(SPEC_PREC, 0)
-#define TOK_RPAREN tok_decl(SPEC_PREC, 1)
-
-#define NUMPTR (*numstackptr)
-
-static int
-tok_have_assign(operator op)
-{
-       operator prec = PREC(op);
-
-       convert_prec_is_assing(prec);
-       return (prec == PREC(TOK_ASSIGN) ||
-                       prec == PREC_PRE || prec == PREC_POST);
-}
-
-static int
-is_right_associativity(operator prec)
-{
-       return (prec == PREC(TOK_ASSIGN) || prec == PREC(TOK_EXPONENT)
-               || prec == PREC(TOK_CONDITIONAL));
-}
-
-typedef struct {
-       arith_t val;
-       arith_t contidional_second_val;
-       char contidional_second_val_initialized;
-       char *var;      /* if NULL then is regular number,
-                          else is variable name */
-} v_n_t;
-
-typedef struct chk_var_recursive_looped_t {
-       const char *var;
-       struct chk_var_recursive_looped_t *next;
-} chk_var_recursive_looped_t;
-
-static chk_var_recursive_looped_t *prev_chk_var_recursive;
-
-static int
-arith_lookup_val(v_n_t *t)
-{
-       if (t->var) {
-               const char * p = lookupvar(t->var);
-
-               if (p) {
-                       int errcode;
-
-                       /* recursive try as expression */
-                       chk_var_recursive_looped_t *cur;
-                       chk_var_recursive_looped_t cur_save;
-
-                       for (cur = prev_chk_var_recursive; cur; cur = cur->next) {
-                               if (strcmp(cur->var, t->var) == 0) {
-                                       /* expression recursion loop detected */
-                                       return -5;
-                               }
-                       }
-                       /* save current lookuped var name */
-                       cur = prev_chk_var_recursive;
-                       cur_save.var = t->var;
-                       cur_save.next = cur;
-                       prev_chk_var_recursive = &cur_save;
-
-                       t->val = arith (p, &errcode);
-                       /* restore previous ptr after recursiving */
-                       prev_chk_var_recursive = cur;
-                       return errcode;
-               }
-               /* allow undefined var as 0 */
-               t->val = 0;
-       }
-       return 0;
-}
-
-/* "applying" a token means performing it on the top elements on the integer
- * stack. For a unary operator it will only change the top element, but a
- * binary operator will pop two arguments and push a result */
-static int
-arith_apply(operator op, v_n_t *numstack, v_n_t **numstackptr)
-{
-       v_n_t *numptr_m1;
-       arith_t numptr_val, rez;
-       int ret_arith_lookup_val;
-
-       /* There is no operator that can work without arguments */
-       if (NUMPTR == numstack) goto err;
-       numptr_m1 = NUMPTR - 1;
-
-       /* check operand is var with noninteger value */
-       ret_arith_lookup_val = arith_lookup_val(numptr_m1);
-       if (ret_arith_lookup_val)
-               return ret_arith_lookup_val;
-
-       rez = numptr_m1->val;
-       if (op == TOK_UMINUS)
-               rez *= -1;
-       else if (op == TOK_NOT)
-               rez = !rez;
-       else if (op == TOK_BNOT)
-               rez = ~rez;
-       else if (op == TOK_POST_INC || op == TOK_PRE_INC)
-               rez++;
-       else if (op == TOK_POST_DEC || op == TOK_PRE_DEC)
-               rez--;
-       else if (op != TOK_UPLUS) {
-               /* Binary operators */
-
-               /* check and binary operators need two arguments */
-               if (numptr_m1 == numstack) goto err;
-
-               /* ... and they pop one */
-               --NUMPTR;
-               numptr_val = rez;
-               if (op == TOK_CONDITIONAL) {
-                       if (!numptr_m1->contidional_second_val_initialized) {
-                               /* protect $((expr1 ? expr2)) without ": expr" */
-                               goto err;
-                       }
-                       rez = numptr_m1->contidional_second_val;
-               } else if (numptr_m1->contidional_second_val_initialized) {
-                       /* protect $((expr1 : expr2)) without "expr ? " */
-                       goto err;
-               }
-               numptr_m1 = NUMPTR - 1;
-               if (op != TOK_ASSIGN) {
-                       /* check operand is var with noninteger value for not '=' */
-                       ret_arith_lookup_val = arith_lookup_val(numptr_m1);
-                       if (ret_arith_lookup_val)
-                               return ret_arith_lookup_val;
-               }
-               if (op == TOK_CONDITIONAL) {
-                       numptr_m1->contidional_second_val = rez;
-               }
-               rez = numptr_m1->val;
-               if (op == TOK_BOR || op == TOK_OR_ASSIGN)
-                       rez |= numptr_val;
-               else if (op == TOK_OR)
-                       rez = numptr_val || rez;
-               else if (op == TOK_BAND || op == TOK_AND_ASSIGN)
-                       rez &= numptr_val;
-               else if (op == TOK_BXOR || op == TOK_XOR_ASSIGN)
-                       rez ^= numptr_val;
-               else if (op == TOK_AND)
-                       rez = rez && numptr_val;
-               else if (op == TOK_EQ)
-                       rez = (rez == numptr_val);
-               else if (op == TOK_NE)
-                       rez = (rez != numptr_val);
-               else if (op == TOK_GE)
-                       rez = (rez >= numptr_val);
-               else if (op == TOK_RSHIFT || op == TOK_RSHIFT_ASSIGN)
-                       rez >>= numptr_val;
-               else if (op == TOK_LSHIFT || op == TOK_LSHIFT_ASSIGN)
-                       rez <<= numptr_val;
-               else if (op == TOK_GT)
-                       rez = (rez > numptr_val);
-               else if (op == TOK_LT)
-                       rez = (rez < numptr_val);
-               else if (op == TOK_LE)
-                       rez = (rez <= numptr_val);
-               else if (op == TOK_MUL || op == TOK_MUL_ASSIGN)
-                       rez *= numptr_val;
-               else if (op == TOK_ADD || op == TOK_PLUS_ASSIGN)
-                       rez += numptr_val;
-               else if (op == TOK_SUB || op == TOK_MINUS_ASSIGN)
-                       rez -= numptr_val;
-               else if (op == TOK_ASSIGN || op == TOK_COMMA)
-                       rez = numptr_val;
-               else if (op == TOK_CONDITIONAL_SEP) {
-                       if (numptr_m1 == numstack) {
-                               /* protect $((expr : expr)) without "expr ? " */
-                               goto err;
-                       }
-                       numptr_m1->contidional_second_val_initialized = op;
-                       numptr_m1->contidional_second_val = numptr_val;
-               } else if (op == TOK_CONDITIONAL) {
-                       rez = rez ?
-                               numptr_val : numptr_m1->contidional_second_val;
-               } else if (op == TOK_EXPONENT) {
-                       if (numptr_val < 0)
-                               return -3;      /* exponent less than 0 */
-                       else {
-                               arith_t c = 1;
-
-                               if (numptr_val)
-                                       while (numptr_val--)
-                                               c *= rez;
-                               rez = c;
-                       }
-               } else if (numptr_val==0)          /* zero divisor check */
-                       return -2;
-               else if (op == TOK_DIV || op == TOK_DIV_ASSIGN)
-                       rez /= numptr_val;
-               else if (op == TOK_REM || op == TOK_REM_ASSIGN)
-                       rez %= numptr_val;
-       }
-       if (tok_have_assign(op)) {
-               char buf[sizeof(arith_t_type)*3 + 2];
-
-               if (numptr_m1->var == NULL) {
-                       /* Hmm, 1=2 ? */
-                       goto err;
-               }
-               /* save to shell variable */
-#if ENABLE_ASH_MATH_SUPPORT_64
-               snprintf(buf, sizeof(buf), "%lld", (arith_t_type) rez);
-#else
-               snprintf(buf, sizeof(buf), "%ld", (arith_t_type) rez);
-#endif
-               setvar(numptr_m1->var, buf, 0);
-               /* after saving, make previous value for v++ or v-- */
-               if (op == TOK_POST_INC)
-                       rez--;
-               else if (op == TOK_POST_DEC)
-                       rez++;
-       }
-       numptr_m1->val = rez;
-       /* protect geting var value, is number now */
-       numptr_m1->var = NULL;
-       return 0;
- err:
-       return -1;
-}
-
-/* longest must be first */
-static const char op_tokens[] ALIGN1 = {
-       '<','<','=',0, TOK_LSHIFT_ASSIGN,
-       '>','>','=',0, TOK_RSHIFT_ASSIGN,
-       '<','<',    0, TOK_LSHIFT,
-       '>','>',    0, TOK_RSHIFT,
-       '|','|',    0, TOK_OR,
-       '&','&',    0, TOK_AND,
-       '!','=',    0, TOK_NE,
-       '<','=',    0, TOK_LE,
-       '>','=',    0, TOK_GE,
-       '=','=',    0, TOK_EQ,
-       '|','=',    0, TOK_OR_ASSIGN,
-       '&','=',    0, TOK_AND_ASSIGN,
-       '*','=',    0, TOK_MUL_ASSIGN,
-       '/','=',    0, TOK_DIV_ASSIGN,
-       '%','=',    0, TOK_REM_ASSIGN,
-       '+','=',    0, TOK_PLUS_ASSIGN,
-       '-','=',    0, TOK_MINUS_ASSIGN,
-       '-','-',    0, TOK_POST_DEC,
-       '^','=',    0, TOK_XOR_ASSIGN,
-       '+','+',    0, TOK_POST_INC,
-       '*','*',    0, TOK_EXPONENT,
-       '!',        0, TOK_NOT,
-       '<',        0, TOK_LT,
-       '>',        0, TOK_GT,
-       '=',        0, TOK_ASSIGN,
-       '|',        0, TOK_BOR,
-       '&',        0, TOK_BAND,
-       '*',        0, TOK_MUL,
-       '/',        0, TOK_DIV,
-       '%',        0, TOK_REM,
-       '+',        0, TOK_ADD,
-       '-',        0, TOK_SUB,
-       '^',        0, TOK_BXOR,
-       /* uniq */
-       '~',        0, TOK_BNOT,
-       ',',        0, TOK_COMMA,
-       '?',        0, TOK_CONDITIONAL,
-       ':',        0, TOK_CONDITIONAL_SEP,
-       ')',        0, TOK_RPAREN,
-       '(',        0, TOK_LPAREN,
-       0
-};
-/* ptr to ")" */
-#define endexpression (&op_tokens[sizeof(op_tokens)-7])
-
-static arith_t
-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;
-       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. */
-       numstackptr = numstack = alloca((datasizes / 2) * sizeof(numstack[0]));
-       /* Stack of operator tokens */
-       stackptr = stack = alloca(datasizes * sizeof(stack[0]));
-
-       *stackptr++ = lasttok = TOK_LPAREN;     /* start off with a left paren */
-       *perrcode = errcode = 0;
-
-       while (1) {
-               arithval = *expr;
-               if (arithval == 0) {
-                       if (p == endexpression) {
-                               /* Null expression. */
-                               return 0;
-                       }
-
-                       /* This is only reached after all tokens have been extracted from the
-                        * input stream. If there are still tokens on the operator stack, they
-                        * are to be applied in order. At the end, there should be a final
-                        * result on the integer stack */
-
-                       if (expr != endexpression + 1) {
-                               /* If we haven't done so already, */
-                               /* append a closing right paren */
-                               expr = endexpression;
-                               /* and let the loop process it. */
-                               continue;
-                       }
-                       /* At this point, we're done with the expression. */
-                       if (numstackptr != numstack+1) {
-                               /* ... but if there isn't, it's bad */
- err:
-                               *perrcode = -1;
-                               return *perrcode;
-                       }
-                       if (numstack->var) {
-                               /* expression is $((var)) only, lookup now */
-                               errcode = arith_lookup_val(numstack);
-                       }
- ret:
-                       *perrcode = errcode;
-                       return numstack->val;
-               }
-
-               /* Continue processing the expression. */
-               if (arith_isspace(arithval)) {
-                       /* Skip whitespace */
-                       goto prologue;
-               }
-               p = endofname(expr);
-               if (p != expr) {
-                       size_t var_name_size = (p-expr) + 1;  /* trailing zero */
-
-                       numstackptr->var = alloca(var_name_size);
-                       safe_strncpy(numstackptr->var, expr, var_name_size);
-                       expr = p;
- num:
-                       numstackptr->contidional_second_val_initialized = 0;
-                       numstackptr++;
-                       lasttok = TOK_NUM;
-                       continue;
-               }
-               if (isdigit(arithval)) {
-                       numstackptr->var = NULL;
-#if ENABLE_ASH_MATH_SUPPORT_64
-                       numstackptr->val = strtoll(expr, (char **) &expr, 0);
-#else
-                       numstackptr->val = strtol(expr, (char **) &expr, 0);
-#endif
-                       goto num;
-               }
-               for (p = op_tokens; ; p++) {
-                       const char *o;
-
-                       if (*p == 0) {
-                               /* strange operator not found */
-                               goto err;
-                       }
-                       for (o = expr; *p && *o == *p; p++)
-                               o++;
-                       if (!*p) {
-                               /* found */
-                               expr = o - 1;
-                               break;
-                       }
-                       /* skip tail uncompared token */
-                       while (*p)
-                               p++;
-                       /* skip zero delim */
-                       p++;
-               }
-               op = p[1];
-
-               /* post grammar: a++ reduce to num */
-               if (lasttok == TOK_POST_INC || lasttok == TOK_POST_DEC)
-                       lasttok = TOK_NUM;
-
-               /* Plus and minus are binary (not unary) _only_ if the last
-                * token was as number, or a right paren (which pretends to be
-                * a number, since it evaluates to one). Think about it.
-                * It makes sense. */
-               if (lasttok != TOK_NUM) {
-                       switch (op) {
-                       case TOK_ADD:
-                               op = TOK_UPLUS;
-                               break;
-                       case TOK_SUB:
-                               op = TOK_UMINUS;
-                               break;
-                       case TOK_POST_INC:
-                               op = TOK_PRE_INC;
-                               break;
-                       case TOK_POST_DEC:
-                               op = TOK_PRE_DEC;
-                               break;
-                       }
-               }
-               /* We don't want a unary operator to cause recursive descent on the
-                * stack, because there can be many in a row and it could cause an
-                * operator to be evaluated before its argument is pushed onto the
-                * integer stack. */
-               /* But for binary operators, "apply" everything on the operator
-                * stack until we find an operator with a lesser priority than the
-                * one we have just extracted. */
-               /* Left paren is given the lowest priority so it will never be
-                * "applied" in this way.
-                * if associativity is right and priority eq, applied also skip
-                */
-               prec = PREC(op);
-               if ((prec > 0 && prec < UNARYPREC) || prec == SPEC_PREC) {
-                       /* not left paren or unary */
-                       if (lasttok != TOK_NUM) {
-                               /* binary op must be preceded by a num */
-                               goto err;
-                       }
-                       while (stackptr != stack) {
-                               if (op == TOK_RPAREN) {
-                                       /* The algorithm employed here is simple: while we don't
-                                        * hit an open paren nor the bottom of the stack, pop
-                                        * tokens and apply them */
-                                       if (stackptr[-1] == TOK_LPAREN) {
-                                               --stackptr;
-                                               /* Any operator directly after a */
-                                               lasttok = TOK_NUM;
-                                               /* close paren should consider itself binary */
-                                               goto prologue;
-                                       }
-                               } else {
-                                       operator prev_prec = PREC(stackptr[-1]);
-
-                                       convert_prec_is_assing(prec);
-                                       convert_prec_is_assing(prev_prec);
-                                       if (prev_prec < prec)
-                                               break;
-                                       /* check right assoc */
-                                       if (prev_prec == prec && is_right_associativity(prec))
-                                               break;
-                               }
-                               errcode = arith_apply(*--stackptr, numstack, &numstackptr);
-                               if (errcode) goto ret;
-                       }
-                       if (op == TOK_RPAREN) {
-                               goto err;
-                       }
-               }
-
-               /* Push this operator to the stack and remember it. */
-               *stackptr++ = lasttok = op;
- prologue:
-               ++expr;
-       } /* while */
-}
-#endif /* ASH_MATH_SUPPORT */
-
-
 /* ============ main() and helpers */
 
 /*
@@ -13519,7 +12952,7 @@ exitshell(void)
        status = exitstatus;
        TRACE(("pid %d, exitshell(%d)\n", getpid(), status));
        if (setjmp(loc.loc)) {
-               if (exception == EXEXIT)
+               if (exception_type == EXEXIT)
 /* dash bug: it just does _exit(exitstatus) here
  * but we have to do setjobctl(0) first!
  * (bug is still not fixed in dash-0.5.3 - if you run dash
@@ -13690,8 +13123,8 @@ extern int etext();
 int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int ash_main(int argc UNUSED_PARAM, char **argv)
 {
-       char *shinit;
-       volatile int state;
+       const char *shinit;
+       volatile smallint state;
        struct jmploc jmploc;
        struct stackmark smark;
 
@@ -13713,21 +13146,20 @@ int ash_main(int argc UNUSED_PARAM, char **argv)
 #endif
        state = 0;
        if (setjmp(jmploc.loc)) {
-               int e;
-               int s;
+               smallint e;
+               smallint s;
 
                reset();
 
-               e = exception;
+               e = exception_type;
                if (e == EXERROR)
                        exitstatus = 2;
                s = state;
                if (e == EXEXIT || s == 0 || iflag == 0 || shlvl)
                        exitshell();
-
-               if (e == EXINT) {
+               if (e == EXINT)
                        outcslow('\n', stderr);
-               }
+
                popstackmark(&smark);
                FORCE_INT_ON; /* enable interrupts */
                if (s == 1)
@@ -13741,7 +13173,7 @@ int ash_main(int argc UNUSED_PARAM, char **argv)
        exception_handler = &jmploc;
 #if DEBUG
        opentrace();
-       trace_puts("Shell args: ");
+       TRACE(("Shell args: "));
        trace_puts_args(argv);
 #endif
        rootpid = getpid();
@@ -13769,7 +13201,7 @@ int ash_main(int argc UNUSED_PARAM, char **argv)
                }
        }
 #endif
-       if (argv[0] && argv[0][0] == '-')
+       if (/* argv[0] && */ argv[0][0] == '-')
                isloginsh = 1;
        if (isloginsh) {
                state = 1;
@@ -13793,15 +13225,21 @@ int ash_main(int argc UNUSED_PARAM, char **argv)
        }
  state3:
        state = 4;
-       if (minusc)
+       if (minusc) {
+               /* evalstring pushes parsefile stack.
+                * Ensure we don't falsely claim that 0 (stdin)
+                * is one of stacked source fds.
+                * Testcase: ash -c 'exec 1>&0' must not complain. */
+               if (!sflag)
+                       g_parsefile->fd = -1;
                evalstring(minusc, 0);
+       }
 
        if (sflag || minusc == NULL) {
 #if ENABLE_FEATURE_EDITING_SAVEHISTORY
                if (iflag) {
                        const char *hp = lookupvar("HISTFILE");
-
-                       if (hp != NULL)
+                       if (hp)
                                line_input_state->hist_file = hp;
                }
 #endif
@@ -13821,14 +13259,6 @@ int ash_main(int argc UNUSED_PARAM, char **argv)
        /* NOTREACHED */
 }
 
-#if DEBUG
-const char *applet_name = "debug stuff usage";
-int main(int argc, char **argv)
-{
-       return ash_main(argc, argv);
-}
-#endif
-
 
 /*-
  * Copyright (c) 1989, 1991, 1993, 1994