shell: split read builtin from ash
[oweals/busybox.git] / shell / ash.c
index dac73182e4f4d4dad216f755be01910dcdb4f3c3..c7deffd08ea08dcd2ff70ede0ac66d71b94277b0 100644 (file)
@@ -2,18 +2,18 @@
 /*
  * ash shell port for busybox
  *
+ * This code is derived from software contributed to Berkeley by
+ * Kenneth Almquist.
+ *
+ * Original BSD copyright notice is retained at the end of this file.
+ *
  * Copyright (c) 1989, 1991, 1993, 1994
  *      The Regents of the University of California.  All rights reserved.
  *
  * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au>
  * was re-ported from NetBSD and debianized.
  *
- * This code is derived from software contributed to Berkeley by
- * Kenneth Almquist.
- *
  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
- *
- * Original BSD copyright notice is retained at the end of this file.
  */
 
 /*
@@ -34,8 +34,6 @@
 
 #define PROFILE 0
 
-#define IFS_BROKEN
-
 #define JOBS ENABLE_ASH_JOB_CONTROL
 
 #if DEBUG
@@ -50,6 +48,9 @@
 #include <paths.h>
 #include <setjmp.h>
 #include <fnmatch.h>
+
+#include "shell_common.h"
+#include "builtin_read.h"
 #include "math.h"
 #if ENABLE_ASH_RANDOM_SUPPORT
 # include "random.h"
@@ -101,14 +102,17 @@ static const char *const optletters_optnames[] = {
        "b"   "notify",
        "u"   "nounset",
        "\0"  "vi"
+#if ENABLE_ASH_BASH_COMPAT
+       ,"\0"  "pipefail"
+#endif
 #if DEBUG
        ,"\0"  "nolog"
        ,"\0"  "debug"
 #endif
 };
 
-#define optletters(n) optletters_optnames[(n)][0]
-#define optnames(n) (&optletters_optnames[(n)][1])
+#define optletters(n)  optletters_optnames[n][0]
+#define optnames(n)   (optletters_optnames[n] + 1)
 
 enum { NOPTS = ARRAY_SIZE(optletters_optnames) };
 
@@ -178,9 +182,14 @@ struct globals_misc {
 #define bflag optlist[11]
 #define uflag optlist[12]
 #define viflag optlist[13]
+#if ENABLE_ASH_BASH_COMPAT
+# define pipefail optlist[14]
+#else
+# define pipefail 0
+#endif
 #if DEBUG
-#define nolog optlist[14]
-#define debug optlist[15]
+# define nolog optlist[14 + ENABLE_ASH_BASH_COMPAT]
+# define debug optlist[15 + ENABLE_ASH_BASH_COMPAT]
 #endif
 
        /* trap handler commands */
@@ -446,6 +455,7 @@ out2str(const char *p)
 /* ============ Parser structures */
 
 /* control characters in argument strings */
+#define CTL_FIRST CTLESC
 #define CTLESC       ((unsigned char)'\201')    /* escape next character */
 #define CTLVAR       ((unsigned char)'\202')    /* variable defn */
 #define CTLENDVAR    ((unsigned char)'\203')
@@ -455,6 +465,7 @@ out2str(const char *p)
 #define CTLARI       ((unsigned char)'\206')    /* arithmetic expression */
 #define CTLENDARI    ((unsigned char)'\207')
 #define CTLQUOTEMARK ((unsigned char)'\210')
+#define CTL_LAST CTLQUOTEMARK
 
 /* variable substitution byte (follows CTLVAR) */
 #define VSTYPE  0x0f            /* type of variable substitution */
@@ -1727,13 +1738,6 @@ struct localvar {
 # define VDYNAMIC       0
 #endif
 
-#ifdef IFS_BROKEN
-static const char defifsvar[] ALIGN1 = "IFS= \t\n";
-#define defifs (defifsvar + 4)
-#else
-static const char defifs[] ALIGN1 = " \t\n";
-#endif
-
 
 /* Need to be before varinit_data[] */
 #if ENABLE_LOCALE_SUPPORT
@@ -1764,7 +1768,7 @@ static const struct {
        const char *text;
        void (*func)(const char *) FAST_FUNC;
 } varinit_data[] = {
-#ifdef IFS_BROKEN
+#if IFS_BROKEN
        { VSTRFIXED|VTEXTFIXED       , defifsvar   , NULL            },
 #else
        { VSTRFIXED|VTEXTFIXED|VUNSET, "IFS\0"     , NULL            },
@@ -2602,75 +2606,72 @@ pwdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 #define CSPCL    13             /* these terminate a word */
 #define CIGN     14             /* character should be ignored */
 
+#define PEOF     256
 #if ENABLE_ASH_ALIAS
-# define PEOA         256
-# define PEOF         257
-#else
-# define PEOF         256
+# define PEOA    257
 #endif
 
-/* number syntax index */
-#define BASESYNTAX 0    /* not in quotes */
-#define DQSYNTAX   1    /* in double quotes */
-#define SQSYNTAX   2    /* in single quotes */
-#define ARISYNTAX  3    /* in arithmetic */
-#define PSSYNTAX   4    /* prompt */
-
-#if ENABLE_ASH_OPTIMIZE_FOR_SIZE
-# define USE_SIT_FUNCTION
-#endif
+#define USE_SIT_FUNCTION ENABLE_ASH_OPTIMIZE_FOR_SIZE
 
 #if ENABLE_SH_MATH_SUPPORT
-static const uint8_t S_I_T[][4] = {
-# if ENABLE_ASH_ALIAS
-       { CSPCL, CIGN, CIGN, CIGN },            /* 0, PEOA */
-# endif
-       { CSPCL, CWORD, CWORD, CWORD },         /* 1, ' ' */
-       { CNL, CNL, CNL, CNL },                 /* 2, \n */
-       { CWORD, CCTL, CCTL, CWORD },           /* 3, !*-/:=?[]~ */
-       { CDQUOTE, CENDQUOTE, CWORD, CWORD },   /* 4, '"' */
-       { CVAR, CVAR, CWORD, CVAR },            /* 5, $ */
-       { CSQUOTE, CWORD, CENDQUOTE, CWORD },   /* 6, "'" */
-       { CSPCL, CWORD, CWORD, CLP },           /* 7, ( */
-       { CSPCL, CWORD, CWORD, CRP },           /* 8, ) */
-       { CBACK, CBACK, CCTL, CBACK },          /* 9, \ */
-       { CBQUOTE, CBQUOTE, CWORD, CBQUOTE },   /* 10, ` */
-       { CENDVAR, CENDVAR, CWORD, CENDVAR },   /* 11, } */
-# ifndef USE_SIT_FUNCTION
-       { CENDFILE, CENDFILE, CENDFILE, CENDFILE }, /* 12, PEOF */
-       { CWORD, CWORD, CWORD, CWORD },         /* 13, 0-9A-Za-z */
-       { CCTL, CCTL, CCTL, CCTL }              /* 14, CTLESC ... */
-# endif
-};
+# define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8) | (d << 12))
 #else
-static const uint8_t S_I_T[][3] = {
-# if ENABLE_ASH_ALIAS
-       { CSPCL, CIGN, CIGN },                  /* 0, PEOA */
-# endif
-       { CSPCL, CWORD, CWORD },                /* 1, ' ' */
-       { CNL, CNL, CNL },                      /* 2, \n */
-       { CWORD, CCTL, CCTL },                  /* 3, !*-/:=?[]~ */
-       { CDQUOTE, CENDQUOTE, CWORD },          /* 4, '"' */
-       { CVAR, CVAR, CWORD },                  /* 5, $ */
-       { CSQUOTE, CWORD, CENDQUOTE },          /* 6, "'" */
-       { CSPCL, CWORD, CWORD },                /* 7, ( */
-       { CSPCL, CWORD, CWORD },                /* 8, ) */
-       { CBACK, CBACK, CCTL },                 /* 9, \ */
-       { CBQUOTE, CBQUOTE, CWORD },            /* 10, ` */
-       { CENDVAR, CENDVAR, CWORD },            /* 11, } */
-# ifndef USE_SIT_FUNCTION
-       { CENDFILE, CENDFILE, CENDFILE },       /* 12, PEOF */
-       { CWORD, CWORD, CWORD },                /* 13, 0-9A-Za-z */
-       { CCTL, CCTL, CCTL }                    /* 14, CTLESC ... */
-# endif
+# define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8))
+#endif
+static const uint16_t S_I_T[] = {
+#if ENABLE_ASH_ALIAS
+       SIT_ITEM(CSPCL   , CIGN     , CIGN , CIGN   ),    /* 0, PEOA */
+#endif
+       SIT_ITEM(CSPCL   , CWORD    , CWORD, CWORD  ),    /* 1, ' ' */
+       SIT_ITEM(CNL     , CNL      , CNL  , CNL    ),    /* 2, \n */
+       SIT_ITEM(CWORD   , CCTL     , CCTL , CWORD  ),    /* 3, !*-/:=?[]~ */
+       SIT_ITEM(CDQUOTE , CENDQUOTE, CWORD, CWORD  ),    /* 4, '"' */
+       SIT_ITEM(CVAR    , CVAR     , CWORD, CVAR   ),    /* 5, $ */
+       SIT_ITEM(CSQUOTE , CWORD    , CENDQUOTE, CWORD),  /* 6, "'" */
+       SIT_ITEM(CSPCL   , CWORD    , CWORD, CLP    ),    /* 7, ( */
+       SIT_ITEM(CSPCL   , CWORD    , CWORD, CRP    ),    /* 8, ) */
+       SIT_ITEM(CBACK   , CBACK    , CCTL , CBACK  ),    /* 9, \ */
+       SIT_ITEM(CBQUOTE , CBQUOTE  , CWORD, CBQUOTE),    /* 10, ` */
+       SIT_ITEM(CENDVAR , CENDVAR  , CWORD, CENDVAR),    /* 11, } */
+#if !USE_SIT_FUNCTION
+       SIT_ITEM(CENDFILE, CENDFILE , CENDFILE, CENDFILE),/* 12, PEOF */
+       SIT_ITEM(CWORD   , CWORD    , CWORD, CWORD  ),    /* 13, 0-9A-Za-z */
+       SIT_ITEM(CCTL    , CCTL     , CCTL , CCTL   )     /* 14, CTLESC ... */
+#endif
+#undef SIT_ITEM
+};
+/* Constants below must match table above */
+enum {
+#if ENABLE_ASH_ALIAS
+       CSPCL_CIGN_CIGN_CIGN               , /*  0 */
+#endif
+       CSPCL_CWORD_CWORD_CWORD            , /*  1 */
+       CNL_CNL_CNL_CNL                    , /*  2 */
+       CWORD_CCTL_CCTL_CWORD              , /*  3 */
+       CDQUOTE_CENDQUOTE_CWORD_CWORD      , /*  4 */
+       CVAR_CVAR_CWORD_CVAR               , /*  5 */
+       CSQUOTE_CWORD_CENDQUOTE_CWORD      , /*  6 */
+       CSPCL_CWORD_CWORD_CLP              , /*  7 */
+       CSPCL_CWORD_CWORD_CRP              , /*  8 */
+       CBACK_CBACK_CCTL_CBACK             , /*  9 */
+       CBQUOTE_CBQUOTE_CWORD_CBQUOTE      , /* 10 */
+       CENDVAR_CENDVAR_CWORD_CENDVAR      , /* 11 */
+       CENDFILE_CENDFILE_CENDFILE_CENDFILE, /* 12 */
+       CWORD_CWORD_CWORD_CWORD            , /* 13 */
+       CCTL_CCTL_CCTL_CCTL                , /* 14 */
 };
-#endif /* SH_MATH_SUPPORT */
 
 /* c in SIT(c, syntax) must be an *unsigned char* or PEOA or PEOF,
  * caller must ensure proper cast on it if c is *char_ptr!
  */
+/* Values for syntax param */
+#define BASESYNTAX 0    /* not in quotes */
+#define DQSYNTAX   1    /* in double quotes */
+#define SQSYNTAX   2    /* in single quotes */
+#define ARISYNTAX  3    /* in arithmetic */
+#define PSSYNTAX   4    /* prompt. never passed to SIT() */
 
-#ifdef USE_SIT_FUNCTION
+#if USE_SIT_FUNCTION
 
 static int
 SIT(int c, int syntax)
@@ -2694,65 +2695,32 @@ SIT(int c, int syntax)
        const char *s;
        int indx;
 
-       if (c == PEOF) {         /* 2^8+2 */
+       if (c == PEOF)
                return CENDFILE;
-       }
 # if ENABLE_ASH_ALIAS
-       if (c == PEOA) {         /* 2^8+1 */
+       if (c == PEOA)
                indx = 0;
-       else
+       else
 # endif
        {
-               if ((unsigned char)c >= CTLESC
-                && (unsigned char)c <= CTLQUOTEMARK
+               /* Cast is purely for paranoia here,
+                * just in case someone passed signed char to us */
+               if ((unsigned char)c >= CTL_FIRST
+                && (unsigned char)c <= CTL_LAST
                ) {
                        return CCTL;
                }
                s = strchrnul(spec_symbls, c);
-               if (*s == '\0') {
+               if (*s == '\0')
                        return CWORD;
-               }
                indx = syntax_index_table[s - spec_symbls];
        }
-       return S_I_T[indx][syntax];
+       return (S_I_T[indx] >> (syntax*4)) & 0xf;
 }
 
 #else   /* !USE_SIT_FUNCTION */
 
-# if ENABLE_ASH_ALIAS
-#  define CSPCL_CIGN_CIGN_CIGN                 0
-#  define CSPCL_CWORD_CWORD_CWORD              1
-#  define CNL_CNL_CNL_CNL                      2
-#  define CWORD_CCTL_CCTL_CWORD                3
-#  define CDQUOTE_CENDQUOTE_CWORD_CWORD        4
-#  define CVAR_CVAR_CWORD_CVAR                 5
-#  define CSQUOTE_CWORD_CENDQUOTE_CWORD        6
-#  define CSPCL_CWORD_CWORD_CLP                7
-#  define CSPCL_CWORD_CWORD_CRP                8
-#  define CBACK_CBACK_CCTL_CBACK               9
-#  define CBQUOTE_CBQUOTE_CWORD_CBQUOTE       10
-#  define CENDVAR_CENDVAR_CWORD_CENDVAR       11
-#  define CENDFILE_CENDFILE_CENDFILE_CENDFILE 12
-#  define CWORD_CWORD_CWORD_CWORD             13
-#  define CCTL_CCTL_CCTL_CCTL                 14
-# else
-#  define CSPCL_CWORD_CWORD_CWORD              0
-#  define CNL_CNL_CNL_CNL                      1
-#  define CWORD_CCTL_CCTL_CWORD                2
-#  define CDQUOTE_CENDQUOTE_CWORD_CWORD        3
-#  define CVAR_CVAR_CWORD_CVAR                 4
-#  define CSQUOTE_CWORD_CENDQUOTE_CWORD        5
-#  define CSPCL_CWORD_CWORD_CLP                6
-#  define CSPCL_CWORD_CWORD_CRP                7
-#  define CBACK_CBACK_CCTL_CBACK               8
-#  define CBQUOTE_CBQUOTE_CWORD_CBQUOTE        9
-#  define CENDVAR_CENDVAR_CWORD_CENDVAR       10
-#  define CENDFILE_CENDFILE_CENDFILE_CENDFILE 11
-#  define CWORD_CWORD_CWORD_CWORD             12
-#  define CCTL_CCTL_CCTL_CCTL                 13
-# endif
-
-static const uint8_t syntax_index_table[258] = {
+static const uint8_t syntax_index_table[] = {
        /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */
        /*   0      */ CWORD_CWORD_CWORD_CWORD,
        /*   1      */ CWORD_CWORD_CWORD_CWORD,
@@ -3010,15 +2978,15 @@ static const uint8_t syntax_index_table[258] = {
        /* 253      */ CWORD_CWORD_CWORD_CWORD,
        /* 254      */ CWORD_CWORD_CWORD_CWORD,
        /* 255      */ CWORD_CWORD_CWORD_CWORD,
+       /* PEOF */     CENDFILE_CENDFILE_CENDFILE_CENDFILE,
 # if ENABLE_ASH_ALIAS
        /* PEOA */     CSPCL_CIGN_CIGN_CIGN,
 # endif
-       /* PEOF */     CENDFILE_CENDFILE_CENDFILE_CENDFILE,
 };
 
-# define SIT(c, syntax) (S_I_T[syntax_index_table[c]][syntax])
+# define SIT(c, syntax) ((S_I_T[syntax_index_table[c]] >> ((syntax)*4)) & 0xf)
 
-#endif  /* USE_SIT_FUNCTION */
+#endif  /* !USE_SIT_FUNCTION */
 
 
 /* ============ Alias handling */
@@ -3228,8 +3196,8 @@ unaliascmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 /* ============ jobs.c */
 
 /* Mode argument to forkshell.  Don't change FORK_FG or FORK_BG. */
-#define FORK_FG 0
-#define FORK_BG 1
+#define FORK_FG    0
+#define FORK_BG    1
 #define FORK_NOJOB 2
 
 /* mode flags for showjob(s) */
@@ -3244,9 +3212,9 @@ unaliascmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  * array of pids.
  */
 struct procstat {
-       pid_t   pid;            /* process id */
-       int     status;         /* last process status from wait() */
-       char    *cmd;           /* text of command being run */
+       pid_t   ps_pid;         /* process id */
+       int     ps_status;      /* last process status from wait() */
+       char    *ps_cmd;        /* text of command being run */
 };
 
 struct job {
@@ -3552,7 +3520,7 @@ getjob(const char *name, int getctl)
 
        found = NULL;
        while (jp) {
-               if (match(jp->ps[0].cmd, p)) {
+               if (match(jp->ps[0].ps_cmd, p)) {
                        if (found)
                                goto err;
                        found = jp;
@@ -3586,8 +3554,8 @@ freejob(struct job *jp)
 
        INT_OFF;
        for (i = jp->nprocs, ps = jp->ps; --i >= 0; ps++) {
-               if (ps->cmd != nullstr)
-                       free(ps->cmd);
+               if (ps->ps_cmd != nullstr)
+                       free(ps->ps_cmd);
        }
        if (jp->ps != &jp->ps0)
                free(jp->ps);
@@ -3689,7 +3657,7 @@ killcmd(int argc, char **argv)
                do {
                        if (argv[i][0] == '%') {
                                struct job *jp = getjob(argv[i], 0);
-                               unsigned pid = jp->ps[0].pid;
+                               unsigned pid = jp->ps[0].ps_pid;
                                /* Enough space for ' -NNN<nul>' */
                                argv[i] = alloca(sizeof(int)*3 + 3);
                                /* kill_main has matching code to expect
@@ -3703,15 +3671,15 @@ killcmd(int argc, char **argv)
 }
 
 static void
-showpipe(struct job *jp, FILE *out)
+showpipe(struct job *jp /*, FILE *out*/)
 {
-       struct procstat *sp;
-       struct procstat *spend;
+       struct procstat *ps;
+       struct procstat *psend;
 
-       spend = jp->ps + jp->nprocs;
-       for (sp = jp->ps + 1; sp < spend; sp++)
-               fprintf(out, " | %s", sp->cmd);
-       outcslow('\n', out);
+       psend = jp->ps + jp->nprocs;
+       for (ps = jp->ps + 1; ps < psend; ps++)
+               printf(" | %s", ps->ps_cmd);
+       outcslow('\n', stdout);
        flush_stdout_stderr();
 }
 
@@ -3728,15 +3696,15 @@ restartjob(struct job *jp, int mode)
        if (jp->state == JOBDONE)
                goto out;
        jp->state = JOBRUNNING;
-       pgid = jp->ps->pid;
+       pgid = jp->ps[0].ps_pid;
        if (mode == FORK_FG)
                xtcsetpgrp(ttyfd, pgid);
        killpg(pgid, SIGCONT);
        ps = jp->ps;
        i = jp->nprocs;
        do {
-               if (WIFSTOPPED(ps->status)) {
-                       ps->status = -1;
+               if (WIFSTOPPED(ps->ps_status)) {
+                       ps->ps_status = -1;
                }
                ps++;
        } while (--i);
@@ -3750,22 +3718,20 @@ static int FAST_FUNC
 fg_bgcmd(int argc UNUSED_PARAM, char **argv)
 {
        struct job *jp;
-       FILE *out;
        int mode;
        int retval;
 
        mode = (**argv == 'f') ? FORK_FG : FORK_BG;
        nextopt(nullstr);
        argv = argptr;
-       out = stdout;
        do {
                jp = getjob(*argv, 1);
                if (mode == FORK_BG) {
                        set_curjob(jp, CUR_RUNNING);
-                       fprintf(out, "[%d] ", jobno(jp));
+                       printf("[%d] ", jobno(jp));
                }
-               outstr(jp->ps->cmd, out);
-               showpipe(jp, out);
+               out1str(jp->ps[0].ps_cmd);
+               showpipe(jp /*, stdout*/);
                retval = restartjob(jp, mode);
        } while (*argv && *++argv);
        return retval;
@@ -3824,8 +3790,9 @@ dowait(int wait_flags, struct job *job)
        /* Do a wait system call. If job control is compiled in, we accept
         * stopped processes. wait_flags may have WNOHANG, preventing blocking.
         * NB: _not_ safe_waitpid, we need to detect EINTR */
-       pid = waitpid(-1, &status,
-                       (doing_jobctl ? (wait_flags | WUNTRACED) : wait_flags));
+       if (doing_jobctl)
+               wait_flags |= WUNTRACED;
+       pid = waitpid(-1, &status, wait_flags);
        TRACE(("wait returns pid=%d, status=0x%x, errno=%d(%s)\n",
                                pid, status, errno, strerror(errno)));
        if (pid <= 0)
@@ -3834,32 +3801,32 @@ dowait(int wait_flags, struct job *job)
        INT_OFF;
        thisjob = NULL;
        for (jp = curjob; jp; jp = jp->prev_job) {
-               struct procstat *sp;
-               struct procstat *spend;
+               struct procstat *ps;
+               struct procstat *psend;
                if (jp->state == JOBDONE)
                        continue;
                state = JOBDONE;
-               spend = jp->ps + jp->nprocs;
-               sp = jp->ps;
+               ps = jp->ps;
+               psend = ps + jp->nprocs;
                do {
-                       if (sp->pid == pid) {
+                       if (ps->ps_pid == pid) {
                                TRACE(("Job %d: changing status of proc %d "
                                        "from 0x%x to 0x%x\n",
-                                       jobno(jp), pid, sp->status, status));
-                               sp->status = status;
+                                       jobno(jp), pid, ps->ps_status, status));
+                               ps->ps_status = status;
                                thisjob = jp;
                        }
-                       if (sp->status == -1)
+                       if (ps->ps_status == -1)
                                state = JOBRUNNING;
 #if JOBS
                        if (state == JOBRUNNING)
                                continue;
-                       if (WIFSTOPPED(sp->status)) {
-                               jp->stopstatus = sp->status;
+                       if (WIFSTOPPED(ps->ps_status)) {
+                               jp->stopstatus = ps->ps_status;
                                state = JOBSTOPPED;
                        }
 #endif
-               } while (++sp < spend);
+               } while (++ps < psend);
                if (thisjob)
                        goto gotjob;
        }
@@ -3925,7 +3892,7 @@ showjob(FILE *out, struct job *jp, int mode)
 
        if (mode & SHOW_ONLY_PGID) { /* jobs -p */
                /* just output process (group) id of pipeline */
-               fprintf(out, "%d\n", ps->pid);
+               fprintf(out, "%d\n", ps->ps_pid);
                return;
        }
 
@@ -3938,7 +3905,7 @@ showjob(FILE *out, struct job *jp, int mode)
                s[col - 3] = '-';
 
        if (mode & SHOW_PIDS)
-               col += fmtstr(s + col, 16, "%d ", ps->pid);
+               col += fmtstr(s + col, 16, "%d ", ps->ps_pid);
 
        psend = ps + jp->nprocs;
 
@@ -3946,7 +3913,7 @@ showjob(FILE *out, struct job *jp, int mode)
                strcpy(s + col, "Running");
                col += sizeof("Running") - 1;
        } else {
-               int status = psend[-1].status;
+               int status = psend[-1].ps_status;
                if (jp->state == JOBSTOPPED)
                        status = jp->stopstatus;
                col += sprint_status(s + col, status, 0);
@@ -3962,20 +3929,20 @@ showjob(FILE *out, struct job *jp, int mode)
         * making it impossible to know 1st process status.
         */
        goto start;
-       while (1) {
+       do {
                /* for each process */
                s[0] = '\0';
                col = 33;
                if (mode & SHOW_PIDS)
-                       col = fmtstr(s, 48, "\n%*c%d ", indent_col, ' ', ps->pid) - 1;
+                       col = fmtstr(s, 48, "\n%*c%d ", indent_col, ' ', ps->ps_pid) - 1;
  start:
-               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;
-       }
+               fprintf(out, "%s%*c%s%s",
+                               s,
+                               33 - col >= 0 ? 33 - col : 0, ' ',
+                               ps == jp->ps ? "" : "| ",
+                               ps->ps_cmd
+               );
+       } while (++ps != psend);
        outcslow('\n', out);
 
        jp->changed = 0;
@@ -4026,20 +3993,31 @@ jobscmd(int argc UNUSED_PARAM, char **argv)
                do
                        showjob(stdout, getjob(*argv, 0), mode);
                while (*++argv);
-       } else
+       } else {
                showjobs(stdout, mode);
+       }
 
        return 0;
 }
 #endif /* JOBS */
 
+/* Called only on finished or stopped jobs (no members are running) */
 static int
 getstatus(struct job *job)
 {
        int status;
        int retval;
+       struct procstat *ps;
+
+       /* Fetch last member's status */
+       ps = job->ps + job->nprocs - 1;
+       status = ps->ps_status;
+       if (pipefail) {
+               /* "set -o pipefail" mode: use last _nonzero_ status */
+               while (status == 0 && --ps >= job->ps)
+                       status = ps->ps_status;
+       }
 
-       status = job->ps[job->nprocs - 1].status;
        retval = WEXITSTATUS(status);
        if (!WIFEXITED(status)) {
 #if JOBS
@@ -4106,7 +4084,7 @@ waitcmd(int argc UNUSED_PARAM, char **argv)
                        while (1) {
                                if (!job)
                                        goto repeat;
-                               if (job->ps[job->nprocs - 1].pid == pid)
+                               if (job->ps[job->nprocs - 1].ps_pid == pid)
                                        break;
                                job = job->prev_job;
                        }
@@ -4229,15 +4207,16 @@ cmdputs(const char *s)
        };
 
        const char *p, *str;
-       char cc[2] = " ";
+       char cc[2];
        char *nextc;
        unsigned char c;
        unsigned char subtype = 0;
        int quoted = 0;
 
+       cc[1] = '\0';
        nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc);
        p = s;
-       while ((c = *p++) != 0) {
+       while ((c = *p++) != '\0') {
                str = NULL;
                switch (c) {
                case CTLESC:
@@ -4308,7 +4287,8 @@ cmdputs(const char *s)
                while ((c = *str++) != '\0') {
                        USTPUTC(c, nextc);
                }
-       }
+       } /* while *p++ not NUL */
+
        if (quoted & 1) {
                USTPUTC('"', nextc);
        }
@@ -4610,7 +4590,7 @@ forkchild(struct job *jp, union node *n, int mode)
                if (jp->nprocs == 0)
                        pgrp = getpid();
                else
-                       pgrp = jp->ps[0].pid;
+                       pgrp = jp->ps[0].ps_pid;
                /* this can fail because we are doing it in the parent also */
                setpgid(0, pgrp);
                if (mode == FORK_FG)
@@ -4682,7 +4662,7 @@ forkparent(struct job *jp, union node *n, int mode, pid_t pid)
                if (jp->nprocs == 0)
                        pgrp = pid;
                else
-                       pgrp = jp->ps[0].pid;
+                       pgrp = jp->ps[0].ps_pid;
                /* This can fail because we are doing it in the child also */
                setpgid(pid, pgrp);
        }
@@ -4693,12 +4673,12 @@ forkparent(struct job *jp, union node *n, int mode, pid_t pid)
        }
        if (jp) {
                struct procstat *ps = &jp->ps[jp->nprocs++];
-               ps->pid = pid;
-               ps->status = -1;
-               ps->cmd = nullstr;
+               ps->ps_pid = pid;
+               ps->ps_status = -1;
+               ps->ps_cmd = nullstr;
 #if JOBS
                if (doing_jobctl && n)
-                       ps->cmd = commandtext(n);
+                       ps->ps_cmd = commandtext(n);
 #endif
        }
 }
@@ -5125,7 +5105,7 @@ redirect(union node *redir, int flags)
                do {
                        sv_pos++;
 #if ENABLE_ASH_BASH_COMPAT
-                       if (redir->nfile.type == NTO2)
+                       if (tmp->nfile.type == NTO2)
                                sv_pos++;
 #endif
                        tmp = tmp->nfile.next;
@@ -6383,22 +6363,16 @@ subevalvar(char *p, char *str, int strloc, int subtype,
 static NOINLINE ssize_t
 varvalue(char *name, int varflags, int flags, struct strlist *var_str_list)
 {
-       int num;
        const char *p;
+       int num;
        int i;
-       int sep = 0;
        int sepq = 0;
        ssize_t len = 0;
-       char **ap;
-       int syntax;
-       int quoted = varflags & VSQUOTE;
        int subtype = varflags & VSTYPE;
        int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR);
+       int quoted = varflags & VSQUOTE;
+       int syntax = quoted ? DQSYNTAX : BASESYNTAX;
 
-       if (quoted && (flags & EXP_FULL))
-               sep = 1 << CHAR_BIT;
-
-       syntax = quoted ? DQSYNTAX : BASESYNTAX;
        switch (*name) {
        case '$':
                num = rootpid;
@@ -6431,13 +6405,20 @@ varvalue(char *name, int varflags, int flags, struct strlist *var_str_list)
                        raise_error_syntax("bad substitution");
 #endif
                break;
-       case '@':
-               if (sep)
+       case '@': {
+               char **ap;
+               int sep;
+
+               if (quoted && (flags & EXP_FULL)) {
+                       /* note: this is not meant as PEOF value */
+                       sep = 1 << CHAR_BIT;
                        goto param;
+               }
                /* fall through */
        case '*':
                sep = ifsset() ? (unsigned char)(ifsval()[0]) : ' ';
-               if (quotes && (SIT(sep, syntax) == CCTL || SIT(sep, syntax) == CBACK))
+               i = SIT(sep, syntax);
+               if (quotes && (i == CCTL || i == CBACK))
                        sepq = 1;
  param:
                ap = shellparam.p;
@@ -6462,11 +6443,14 @@ varvalue(char *name, int varflags, int flags, struct strlist *var_str_list)
                                q = expdest;
                                if (sepq)
                                        STPUTC(CTLESC, q);
+                               /* note: may put NUL despite sep != 0
+                                * (see sep = 1 << CHAR_BIT above) */
                                STPUTC(sep, q);
                                expdest = q;
                        }
                }
                return len;
+       } /* case '@' and '*' */
        case '0':
        case '1':
        case '2':
@@ -6498,7 +6482,8 @@ varvalue(char *name, int varflags, int flags, struct strlist *var_str_list)
                                        break;
                                eq++;
                                if (name_len == (unsigned)(eq - str)
-                                && strncmp(str, name, name_len) == 0) {
+                                && strncmp(str, name, name_len) == 0
+                               ) {
                                        p = eq;
                                        /* goto value; - WRONG! */
                                        /* think "A=1 A=2 B=$A" */
@@ -7712,7 +7697,7 @@ describe_command(char *command, int describe_command_verbose)
                return 127;
        }
  out:
-       outstr("\n", stdout);
+       out1str("\n");
        return 0;
 }
 
@@ -9575,17 +9560,14 @@ pgetc(void)
 }
 
 #if ENABLE_ASH_OPTIMIZE_FOR_SIZE
-#define pgetc_fast() pgetc()
+# define pgetc_fast() pgetc()
 #else
-#define pgetc_fast() pgetc_as_macro()
+# define pgetc_fast() pgetc_as_macro()
 #endif
 
-/*
- * Same as pgetc(), but ignores PEOA.
- */
 #if ENABLE_ASH_ALIAS
 static int
-pgetc2(void)
+pgetc_without_PEOA(void)
 {
        int c;
        do {
@@ -9598,7 +9580,7 @@ pgetc2(void)
        return c;
 }
 #else
-#define pgetc2() pgetc()
+# define pgetc_without_PEOA() pgetc()
 #endif
 
 /*
@@ -9612,7 +9594,7 @@ pfgets(char *line, int len)
        int c;
 
        while (--nleft > 0) {
-               c = pgetc2();
+               c = pgetc_without_PEOA();
                if (c == PEOF) {
                        if (p == line)
                                return NULL;
@@ -10437,7 +10419,7 @@ noexpand(const char *text)
                        continue;
                if (c == CTLESC)
                        text++;
-               else if (SIT((signed char)c, BASESYNTAX) == CCTL)
+               else if (SIT(c, BASESYNTAX) == CCTL)
                        return 0;
        }
        return 1;
@@ -10922,7 +10904,7 @@ readtoken1(int c, int syntax, char *eofmark, int striptabs)
                                USTPUTC(c, out);
                                break;
                        case CBACK:     /* backslash */
-                               c = pgetc2();
+                               c = pgetc_without_PEOA();
                                if (c == PEOF) {
                                        USTPUTC(CTLESC, out);
                                        USTPUTC('\\', out);
@@ -11036,9 +11018,7 @@ readtoken1(int c, int syntax, char *eofmark, int striptabs)
 #endif
                                        goto endword;   /* exit outer loop */
                                }
-#if ENABLE_ASH_ALIAS
-                               if (c != PEOA)
-#endif
+                               IF_ASH_ALIAS(if (c != PEOA))
                                        USTPUTC(c, out);
 
                        }
@@ -11090,13 +11070,12 @@ readtoken1(int c, int syntax, char *eofmark, int striptabs)
 checkend: {
        if (eofmark) {
 #if ENABLE_ASH_ALIAS
-               if (c == PEOA) {
-                       c = pgetc2();
-               }
+               if (c == PEOA)
+                       c = pgetc_without_PEOA();
 #endif
                if (striptabs) {
                        while (c == '\t') {
-                               c = pgetc2();
+                               c = pgetc_without_PEOA();
                        }
                }
                if (c == *eofmark) {
@@ -11398,17 +11377,17 @@ parsebackq: {
                                        continue;
                                }
                                if (pc != '\\' && pc != '`' && pc != '$'
-                                && (!dblquote || pc != '"'))
+                                && (!dblquote || pc != '"')
+                               ) {
                                        STPUTC('\\', pout);
+                               }
                                if (pc <= 255 /* not PEOA or PEOF */) {
                                        break;
                                }
                                /* fall through */
 
                        case PEOF:
-#if ENABLE_ASH_ALIAS
-                       case PEOA:
-#endif
+                       IF_ASH_ALIAS(case PEOA:)
                                startlinno = g_parsefile->linno;
                                raise_error_syntax("EOF in backquote substitution");
 
@@ -11625,9 +11604,7 @@ xxreadtoken(void)
                c = pgetc_fast();
                switch (c) {
                case ' ': case '\t':
-#if ENABLE_ASH_ALIAS
-               case PEOA:
-#endif
+               IF_ASH_ALIAS(case PEOA:)
                        continue;
                case '#':
                        while ((c = pgetc()) != '\n' && c != PEOF)
@@ -11889,8 +11866,9 @@ evalcmd(int argc UNUSED_PARAM, char **argv)
 }
 
 /*
- * Read and execute commands.  "Top" is nonzero for the top level command
- * loop; it turns on prompting if the shell is interactive.
+ * Read and execute commands.
+ * "Top" is nonzero for the top level command loop;
+ * it turns on prompting if the shell is interactive.
  */
 static int
 cmdloop(int top)
@@ -12515,211 +12493,53 @@ typedef enum __rlimit_resource rlim_t;
 static int FAST_FUNC
 readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 {
-       static const char *const arg_REPLY[] = { "REPLY", NULL };
-
-       char **ap;
-       int backslash;
-       char c;
-       int rflag;
-       char *prompt;
-       const char *ifs;
-       char *p;
-       int startword;
-       int status;
+       char *opt_n = NULL;
+       char *opt_p = NULL;
+       char *opt_t = NULL;
+       char *opt_u = NULL;
+       int read_flags = 0;
+       const char *r;
        int i;
-       int fd = 0;
-#if ENABLE_ASH_READ_NCHARS
-       int nchars = 0; /* if != 0, -n is in effect */
-       int silent = 0;
-       struct termios tty, old_tty;
-#endif
-#if ENABLE_ASH_READ_TIMEOUT
-       unsigned end_ms = 0;
-       unsigned timeout = 0;
-#endif
-
-       rflag = 0;
-       prompt = NULL;
-       while ((i = nextopt("p:u:r"
-               IF_ASH_READ_TIMEOUT("t:")
-               IF_ASH_READ_NCHARS("n:s")
-       )) != '\0') {
+
+       while ((i = nextopt("p:u:rt:n:s")) != '\0') {
                switch (i) {
                case 'p':
-                       prompt = optionarg;
+                       opt_p = optionarg;
                        break;
-#if ENABLE_ASH_READ_NCHARS
                case 'n':
-                       nchars = bb_strtou(optionarg, NULL, 10);
-                       if (nchars < 0 || errno)
-                               ash_msg_and_raise_error("invalid count");
-                       /* nchars == 0: off (bash 3.2 does this too) */
+                       opt_n = optionarg;
                        break;
                case 's':
-                       silent = 1;
+                       read_flags |= BUILTIN_READ_SILENT;
                        break;
-#endif
-#if ENABLE_ASH_READ_TIMEOUT
                case 't':
-                       timeout = bb_strtou(optionarg, NULL, 10);
-                       if (errno || timeout > UINT_MAX / 2048)
-                               ash_msg_and_raise_error("invalid timeout");
-                       timeout *= 1000;
-#if 0 /* even bash have no -t N.NNN support */
-                       ts.tv_sec = bb_strtou(optionarg, &p, 10);
-                       ts.tv_usec = 0;
-                       /* EINVAL means number is ok, but not terminated by NUL */
-                       if (*p == '.' && errno == EINVAL) {
-                               char *p2;
-                               if (*++p) {
-                                       int scale;
-                                       ts.tv_usec = bb_strtou(p, &p2, 10);
-                                       if (errno)
-                                               ash_msg_and_raise_error("invalid timeout");
-                                       scale = p2 - p;
-                                       /* normalize to usec */
-                                       if (scale > 6)
-                                               ash_msg_and_raise_error("invalid timeout");
-                                       while (scale++ < 6)
-                                               ts.tv_usec *= 10;
-                               }
-                       } else if (ts.tv_sec < 0 || errno) {
-                               ash_msg_and_raise_error("invalid timeout");
-                       }
-                       if (!(ts.tv_sec | ts.tv_usec)) { /* both are 0? */
-                               ash_msg_and_raise_error("invalid timeout");
-                       }
-#endif /* if 0 */
+                       opt_t = optionarg;
                        break;
-#endif
                case 'r':
-                       rflag = 1;
+                       read_flags |= BUILTIN_READ_RAW;
                        break;
                case 'u':
-                       fd = bb_strtou(optionarg, NULL, 10);
-                       if (fd < 0 || errno)
-                               ash_msg_and_raise_error("invalid file descriptor");
+                       opt_u = optionarg;
                        break;
                default:
                        break;
                }
        }
-       if (prompt && isatty(fd)) {
-               out2str(prompt);
-       }
-       ap = argptr;
-       if (*ap == NULL)
-               ap = (char**)arg_REPLY;
-       ifs = bltinlookup("IFS");
-       if (ifs == NULL)
-               ifs = defifs;
-#if ENABLE_ASH_READ_NCHARS
-       tcgetattr(fd, &tty);
-       old_tty = tty;
-       if (nchars || silent) {
-               if (nchars) {
-                       tty.c_lflag &= ~ICANON;
-                       tty.c_cc[VMIN] = nchars < 256 ? nchars : 255;
-               }
-               if (silent) {
-                       tty.c_lflag &= ~(ECHO | ECHOK | ECHONL);
-               }
-               /* if tcgetattr failed, tcsetattr will fail too.
-                * Ignoring, it's harmless. */
-               tcsetattr(fd, TCSANOW, &tty);
-       }
-#endif
 
-       status = 0;
-       startword = 2;
-       backslash = 0;
-#if ENABLE_ASH_READ_TIMEOUT
-       if (timeout) /* NB: ensuring end_ms is nonzero */
-               end_ms = ((unsigned)(monotonic_us() / 1000) + timeout) | 1;
-#endif
-       STARTSTACKSTR(p);
-       do {
-               const char *is_ifs;
-
-#if ENABLE_ASH_READ_TIMEOUT
-               if (end_ms) {
-                       struct pollfd pfd[1];
-                       pfd[0].fd = fd;
-                       pfd[0].events = POLLIN;
-                       timeout = end_ms - (unsigned)(monotonic_us() / 1000);
-                       if ((int)timeout <= 0 /* already late? */
-                        || safe_poll(pfd, 1, timeout) != 1 /* no? wait... */
-                       ) { /* timed out! */
-#if ENABLE_ASH_READ_NCHARS
-                               tcsetattr(fd, TCSANOW, &old_tty);
-#endif
-                               return 1;
-                       }
-               }
-#endif
-               if (nonblock_safe_read(fd, &c, 1) != 1) {
-                       status = 1;
-                       break;
-               }
-               if (c == '\0')
-                       continue;
-               if (backslash) {
-                       backslash = 0;
-                       if (c != '\n')
-                               goto put;
-                       continue;
-               }
-               if (!rflag && c == '\\') {
-                       backslash = 1;
-                       continue;
-               }
-               if (c == '\n')
-                       break;
-               /* $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 && is_ifs) {
-                       const char *beg;
-                       STACKSTRNUL(p);
-                       beg = stackblock();
-                       setvar(*ap, beg, 0);
-                       ap++;
-                       /* can we skip one non-space ifs char? (2: yes) */
-                       startword = isspace(c) ? 2 : 1;
-                       STARTSTACKSTR(p);
-                       continue;
-               }
- put:
-               STPUTC(c, p);
-       }
-/* end of do {} while: */
-#if ENABLE_ASH_READ_NCHARS
-       while (--nchars);
-#else
-       while (1);
-#endif
+       r = builtin_read(setvar,
+               argptr,
+               bltinlookup("IFS"), /* can be NULL */
+               read_flags,
+               opt_n,
+               opt_p,
+               opt_t,
+               opt_u
+       );
 
-#if ENABLE_ASH_READ_NCHARS
-       tcsetattr(fd, TCSANOW, &old_tty);
-#endif
+       if ((uintptr_t)r > 1)
+               ash_msg_and_raise_error(r);
 
-       STACKSTRNUL(p);
-       /* Remove trailing space ifs chars */
-       while ((char *)stackblock() <= --p && isspace(*p) && strchr(ifs, *p) != NULL)
-               *p = '\0';
-       setvar(*ap, stackblock(), 0);
-       while (*++ap != NULL)
-               setvar(*ap, nullstr, 0);
-       return status;
+       return (uintptr_t)r;
 }
 
 static int FAST_FUNC
@@ -13053,6 +12873,10 @@ init(void)
 
        /* from trap.c: */
        signal(SIGCHLD, SIG_DFL);
+       /* bash re-enables SIGHUP which is SIG_IGNed on entry.
+        * Try: "trap '' HUP; bash; echo RET" and type "kill -HUP $$"
+        */
+        signal(SIGHUP, SIG_DFL);
 
        /* from var.c: */
        {