hush: fix \<newline> handling
[oweals/busybox.git] / shell / msh.c
index 52a11bc48ae6ff4097591fa6d9b485ceb0f18646..dffacf02adac12befb6f19d093b60b185f532ce5 100644 (file)
@@ -12,7 +12,6 @@
  *
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
-
 #include <sys/times.h>
 #include <setjmp.h>
 
@@ -35,7 +34,6 @@
 # include <assert.h>
 # define bb_dev_null "/dev/null"
 # define DEFAULT_SHELL "/proc/self/exe"
-# define CONFIG_BUSYBOX_EXEC_PATH "/proc/self/exe"
 # define bb_banner "busybox standalone"
 # define ENABLE_FEATURE_SH_STANDALONE 0
 # define bb_msg_memory_exhausted "memory exhausted"
@@ -45,7 +43,7 @@
 # define nonblock_safe_read(fd,buf,count) read(fd,buf,count)
 # define NOT_LONE_DASH(s) ((s)[0] != '-' || (s)[1])
 # define LONE_CHAR(s,c) ((s)[0] == (c) && !(s)[1])
-# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
+# define NORETURN __attribute__ ((__noreturn__))
 static int find_applet_by_name(const char *applet)
 {
        return -1;
@@ -92,17 +90,17 @@ static char *itoa(int n)
 #ifdef MSHDEBUG
 static int mshdbg = MSHDEBUG;
 
-#define DBGPRINTF(x)   if (mshdbg>0) printf x
-#define DBGPRINTF0(x)  if (mshdbg>0) printf x
-#define DBGPRINTF1(x)  if (mshdbg>1) printf x
-#define DBGPRINTF2(x)  if (mshdbg>2) printf x
-#define DBGPRINTF3(x)  if (mshdbg>3) printf x
-#define DBGPRINTF4(x)  if (mshdbg>4) printf x
-#define DBGPRINTF5(x)  if (mshdbg>5) printf x
-#define DBGPRINTF6(x)  if (mshdbg>6) printf x
-#define DBGPRINTF7(x)  if (mshdbg>7) printf x
-#define DBGPRINTF8(x)  if (mshdbg>8) printf x
-#define DBGPRINTF9(x)  if (mshdbg>9) printf x
+#define DBGPRINTF(x)   if (mshdbg > 0) printf x
+#define DBGPRINTF0(x)  if (mshdbg > 0) printf x
+#define DBGPRINTF1(x)  if (mshdbg > 1) printf x
+#define DBGPRINTF2(x)  if (mshdbg > 2) printf x
+#define DBGPRINTF3(x)  if (mshdbg > 3) printf x
+#define DBGPRINTF4(x)  if (mshdbg > 4) printf x
+#define DBGPRINTF5(x)  if (mshdbg > 5) printf x
+#define DBGPRINTF6(x)  if (mshdbg > 6) printf x
+#define DBGPRINTF7(x)  if (mshdbg > 7) printf x
+#define DBGPRINTF8(x)  if (mshdbg > 8) printf x
+#define DBGPRINTF9(x)  if (mshdbg > 9) printf x
 
 static int mshdbg_rc = 0;
 
@@ -124,7 +122,7 @@ static int mshdbg_rc = 0;
 
 #define RCPRINTF(x) ((void)0)
 
-#endif                                                 /* MSHDEBUG */
+#endif  /* MSHDEBUG */
 
 
 #if ENABLE_FEATURE_EDITING_FANCY_PROMPT
@@ -141,13 +139,13 @@ static int mshdbg_rc = 0;
  * shell
  */
 
-#define        LINELIM   2100
-#define        NPUSH     8                             /* limit to input nesting */
+#define LINELIM   2100
+#define NPUSH     8             /* limit to input nesting */
 
 #undef NOFILE
-#define        NOFILE    20                    /* Number of open files */
-#define        NUFILE    10                    /* Number of user-accessible files */
-#define        FDBASE    10                    /* First file usable by Shell */
+#define NOFILE    20            /* Number of open files */
+#define NUFILE    10            /* Number of user-accessible files */
+#define FDBASE    10            /* First file usable by Shell */
 
 /*
  * values returned by wait
@@ -159,7 +157,7 @@ static int mshdbg_rc = 0;
 /*
  * library and system definitions
  */
-typedef void xint;                             /* base type of jmp_buf, for not broken compilers */
+typedef void xint;              /* base type of jmp_buf, for not broken compilers */
 
 /*
  * shell components
@@ -173,8 +171,8 @@ typedef void xint;                          /* base type of jmp_buf, for not broken
  * redirection
  */
 struct ioword {
-       short io_unit;                  /* unit affected */
-       short io_flag;                  /* action (below) */
+       smallint io_flag;               /* action (below) */
+       int io_fd;                      /* fd affected */
        char *io_name;                  /* file name */
 };
 
@@ -186,7 +184,7 @@ struct ioword {
 #define        IODUP    32                     /* >&digit */
 #define        IOCLOSE  64                     /* >&- */
 
-#define        IODEFAULT (-1)                  /* token for default IO unit */
+#define        IODEFAULT (-1)                  /* "default" IO fd */
 
 
 /*
@@ -194,7 +192,7 @@ struct ioword {
  * Might eventually use a union.
  */
 struct op {
-       smallint type;                  /* operation type, see Txxxx below */
+       smallint op_type;               /* operation type, see Txxxx below */
        char **op_words;                /* arguments to a command */
        struct ioword **ioact;          /* IO actions (eg, < > >>) */
        struct op *left;
@@ -588,14 +586,13 @@ static const struct builtincmd builtincmds[] = {
        { NULL      , NULL       },
 };
 
-static struct op *scantree(struct op *);
-static struct op *dowholefile(int, int);
+static struct op *dowholefile(int /*, int*/);
 
 
 /* Globals */
 static char **dolv;
 static int dolc;
-static int exstat;
+static uint8_t exstat;
 static smallint gflg;                   /* (seems to be a parse error indicator) */
 static smallint interactive;            /* Is this an interactive shell */
 static smallint execflg;
@@ -726,7 +723,7 @@ static void print_tree(struct op *head)
                return;
        }
 
-       DBGPRINTF(("NODE: %p,  left %p, right %p\n", head, head->left,
+       DBGPRINTF(("NODE: %p, left %p, right %p\n", head, head->left,
                           head->right));
 
        if (head->left)
@@ -744,7 +741,7 @@ static void print_tree(struct op *head)
 static void prs(const char *s)
 {
        if (*s)
-               write(2, s, strlen(s));
+               xwrite_str(STDERR_FILENO, s);
 }
 
 static void prn(unsigned u)
@@ -781,7 +778,7 @@ static void closeall(void)
 
 
 /* fail but return to process next command */
-static void fail(void) ATTRIBUTE_NORETURN;
+static void fail(void) NORETURN;
 static void fail(void)
 {
        longjmp(failpt, 1);
@@ -789,7 +786,7 @@ static void fail(void)
 }
 
 /* abort shell (or fail in subshell) */
-static void leave(void) ATTRIBUTE_NORETURN;
+static void leave(void) NORETURN;
 static void leave(void)
 {
        DBGPRINTF(("LEAVE: leave called!\n"));
@@ -807,7 +804,8 @@ static void warn(const char *s)
 {
        if (*s) {
                prs(s);
-               exstat = -1;
+               if (!exstat)
+                       exstat = 255;
        }
        prs("\n");
        if (FLAG['e'])
@@ -1268,7 +1266,7 @@ static int newfile(char *s)
                f = open(s, O_RDONLY);
                if (f < 0) {
                        prs(s);
-                       err(": cannot open");
+                       err(": can't open");
                        return 1;
                }
        }
@@ -1278,6 +1276,7 @@ static int newfile(char *s)
 }
 
 
+#ifdef UNUSED
 struct op *scantree(struct op *head)
 {
        struct op *dotnode;
@@ -1302,13 +1301,14 @@ struct op *scantree(struct op *head)
 
        DBGPRINTF5(("SCANTREE: checking node %p\n", head));
 
-       if ((head->type != TDOT) && LONE_CHAR(head->op_words[0], '.')) {
+       if ((head->op_type != TDOT) && LONE_CHAR(head->op_words[0], '.')) {
                DBGPRINTF5(("SCANTREE: dot found in node %p\n", head));
                return head;
        }
 
        return NULL;
 }
+#endif
 
 
 static void onecommand(void)
@@ -1325,9 +1325,9 @@ static void onecommand(void)
        freehere(areanum);
        freearea(areanum);
        garbage();
-       wdlist = 0;
-       iolist = 0;
-       global_env.errpt = 0;
+       wdlist = NULL;
+       iolist = NULL;
+       global_env.errpt = NULL;
        global_env.linep = line;
        yynerrs = 0;
        multiline = 0;
@@ -1448,7 +1448,7 @@ static void next(int f)
        PUSHIO(afile, f, filechar);
 }
 
-static void onintr(int s)                                      /* ANSI C requires a parameter */
+static void onintr(int s UNUSED_PARAM) /* ANSI C requires a parameter */
 {
        signal(SIGINT, onintr);
        intr = 1;
@@ -1543,7 +1543,7 @@ static int gmatch(const char *s, const char *p)
  * shell: syntax (C version)
  */
 
-static void yyerror(const char *s) ATTRIBUTE_NORETURN;
+static void yyerror(const char *s) NORETURN;
 static void yyerror(const char *s)
 {
        yynerrs = 1;
@@ -1556,7 +1556,7 @@ static void yyerror(const char *s)
        fail();
 }
 
-static void zzerr(void) ATTRIBUTE_NORETURN;
+static void zzerr(void) NORETURN;
 static void zzerr(void)
 {
        yyerror("syntax error");
@@ -1593,7 +1593,7 @@ static struct op *pipeline(int cf)
                                zzerr();
                        }
 
-                       if (t->type != TPAREN && t->type != TCOM) {
+                       if (t->op_type != TPAREN && t->op_type != TCOM) {
                                /* shell statement */
                                t = block(TPAREN, t, NOBLOCK, NOWORDS);
                        }
@@ -1627,7 +1627,7 @@ static struct op *andor(void)
                        }
 
                        t = block(c == LOGAND ? TAND : TOR, t, p, NOWORDS);
-               }                                               /* WHILE */
+               }
 
                peeksym = c;
        }
@@ -1723,7 +1723,7 @@ static struct op *simple(void)
                case WORD:
                        if (t == NULL) {
                                t = newtp();
-                               t->type = TCOM;
+                               t->op_type = TCOM;
                        }
                        peeksym = 0;
                        word(yylval.cp);
@@ -1775,7 +1775,7 @@ static struct op *command(int cf)
                        if (iolist == NULL)
                                return NULL;
                        t = newtp();
-                       t->type = TCOM;
+                       t->op_type = TCOM;
                }
                break;
 
@@ -1789,7 +1789,7 @@ static struct op *command(int cf)
 
        case FOR:
                t = newtp();
-               t->type = TFOR;
+               t->op_type = TFOR;
                musthave(WORD, 0);
                startl = 1;
                t->str = yylval.cp;
@@ -1806,7 +1806,7 @@ static struct op *command(int cf)
        case UNTIL:
                multiline++;
                t = newtp();
-               t->type = (c == WHILE ? TWHILE : TUNTIL);
+               t->op_type = (c == WHILE ? TWHILE : TUNTIL);
                t->left = c_list();
                t->right = dogroup(1);
                /* t->op_words = NULL; - newtp() did this */
@@ -1815,7 +1815,7 @@ static struct op *command(int cf)
 
        case CASE:
                t = newtp();
-               t->type = TCASE;
+               t->op_type = TCASE;
                musthave(WORD, 0);
                t->str = yylval.cp;
                startl++;
@@ -1832,7 +1832,7 @@ static struct op *command(int cf)
        case IF:
                multiline++;
                t = newtp();
-               t->type = TIF;
+               t->op_type = TIF;
                t->left = c_list();
                t->right = thenpart();
                musthave(FI, 0);
@@ -1841,7 +1841,7 @@ static struct op *command(int cf)
 
        case DOT:
                t = newtp();
-               t->type = TDOT;
+               t->op_type = TDOT;
 
                musthave(WORD, 0);              /* gets name of file */
                DBGPRINTF7(("COMMAND: DOT clause, yylval.cp is %s\n", yylval.cp));
@@ -1864,11 +1864,11 @@ static struct op *command(int cf)
        return t;
 }
 
-static struct op *dowholefile(int type, int mark)
+static struct op *dowholefile(int type /*, int mark*/)
 {
        struct op *t;
 
-       DBGPRINTF(("DOWHOLEFILE: enter, type=%d, mark=%d\n", type, mark));
+       DBGPRINTF(("DOWHOLEFILE: enter, type=%d\n", type /*, mark*/));
 
        multiline++;
        t = c_list();
@@ -1904,7 +1904,7 @@ static struct op *thenpart(void)
                return NULL;
        }
        t = newtp();
-       /*t->type = 0; - newtp() did this */
+       /*t->op_type = 0; - newtp() did this */
        t->left = c_list();
        if (t->left == NULL)
                zzerr();
@@ -1926,7 +1926,7 @@ static struct op *elsepart(void)
 
        case ELIF:
                t = newtp();
-               t->type = TELIF;
+               t->op_type = TELIF;
                t->left = c_list();
                t->right = thenpart();
                return t;
@@ -1958,7 +1958,7 @@ static struct op *casepart(void)
        DBGPRINTF7(("CASEPART: enter...\n"));
 
        t = newtp();
-       t->type = TPAT;
+       t->op_type = TPAT;
        t->op_words = pattern();
        musthave(')', 0);
        t->left = c_list();
@@ -2027,7 +2027,7 @@ static struct op *block(int type, struct op *t1, struct op *t2, char **wp)
        DBGPRINTF7(("BLOCK: enter, type=%d (%s)\n", type, T_CMD_NAMES[type]));
 
        t = newtp();
-       t->type = type;
+       t->op_type = type;
        t->left = t1;
        t->right = t2;
        t->op_words = wp;
@@ -2096,7 +2096,7 @@ static struct op *newtp(void)
 static struct op *namelist(struct op *t)
 {
        DBGPRINTF7(("NAMELIST: enter, t=%p, type %s, iolist=%p\n", t,
-                               T_CMD_NAMES[t->type], iolist));
+                               T_CMD_NAMES[t->op_type], iolist));
 
        if (iolist) {
                iolist = addword((char *) NULL, iolist);
@@ -2104,8 +2104,8 @@ static struct op *namelist(struct op *t)
        } else
                t->ioact = NULL;
 
-       if (t->type != TCOM) {
-               if (t->type != TPAREN && t->ioact != NULL) {
+       if (t->op_type != TCOM) {
+               if (t->op_type != TPAREN && t->ioact != NULL) {
                        t = block(TPAREN, t, NOBLOCK, NOWORDS);
                        t->ioact = t->left->ioact;
                        t->left->ioact = NULL;
@@ -2124,7 +2124,7 @@ static char **copyw(void)
        char **wd;
 
        wd = getwords(wdlist);
-       wdlist = 0;
+       wdlist = NULL;
        return wd;
 }
 
@@ -2138,7 +2138,7 @@ static struct ioword **copyio(void)
        struct ioword **iop;
 
        iop = (struct ioword **) getwords(iolist);
-       iolist = 0;
+       iolist = NULL;
        return iop;
 }
 
@@ -2147,7 +2147,7 @@ static struct ioword *io(int u, int f, char *cp)
        struct ioword *iop;
 
        iop = (struct ioword *) tree(sizeof(*iop));
-       iop->io_unit = u;
+       iop->io_fd = u;
        iop->io_flag = f;
        iop->io_name = cp;
        iolist = addword((char *) iop, iolist);
@@ -2194,7 +2194,7 @@ static int yylex(int cf)
                }
                break;
 
-       case '#':                                       /* Comment, skip to next newline or End-of-string */
+       case '#':       /* Comment, skip to next newline or End-of-string */
                while ((c = my_getc(0)) != '\0' && c != '\n')
                        continue;
                unget(c);
@@ -2400,10 +2400,10 @@ static struct op **find1case(struct op *t, const char *w)
                return NULL;
        }
 
-       DBGPRINTF3(("FIND1CASE: enter, t->type=%d (%s)\n", t->type,
-                               T_CMD_NAMES[t->type]));
+       DBGPRINTF3(("FIND1CASE: enter, t->op_type=%d (%s)\n", t->op_type,
+                               T_CMD_NAMES[t->op_type]));
 
-       if (t->type == TLIST) {
+       if (t->op_type == TLIST) {
                tp = find1case(t->left, w);
                if (tp != NULL) {
                        DBGPRINTF3(("FIND1CASE: found one to the left, returning tp=%p\n", tp));
@@ -2458,18 +2458,18 @@ static int execute(struct op *t, int *pin, int *pout, int no_fork)
                return 0;
        }
 
-       DBGPRINTF(("EXECUTE: t=%p, t->type=%d (%s), t->op_words is %s\n", t,
-                          t->type, T_CMD_NAMES[t->type],
+       DBGPRINTF(("EXECUTE: t=%p, t->op_type=%d (%s), t->op_words is %s\n", t,
+                          t->op_type, T_CMD_NAMES[t->op_type],
                           ((t->op_words == NULL) ? "NULL" : t->op_words[0])));
 
        rv = 0;
        a = areanum++;
        wp2 = t->op_words;
        wp = (wp2 != NULL)
-               ? eval(wp2, t->type == TCOM ? DOALL : DOALL & ~DOKEY)
+               ? eval(wp2, t->op_type == TCOM ? DOALL : DOALL & ~DOKEY)
                : NULL;
 
-       switch (t->type) {
+       switch (t->op_type) {
        case TDOT:
                DBGPRINTF3(("EXECUTE: TDOT\n"));
 
@@ -2477,7 +2477,7 @@ static int execute(struct op *t, int *pin, int *pout, int no_fork)
 
                newfile(evalstr(t->op_words[0], DOALL));
 
-               t->left = dowholefile(TLIST, 0);
+               t->left = dowholefile(TLIST /*, 0*/);
                t->right = NULL;
 
                outtree = outtree_save;
@@ -2552,7 +2552,7 @@ static int execute(struct op *t, int *pin, int *pout, int no_fork)
        case TAND:
                rv = execute(t->left, pin, pout, /* no_fork: */ 0);
                t1 = t->right;
-               if (t1 != NULL && (rv == 0) == (t->type == TAND))
+               if (t1 != NULL && (rv == 0) == (t->op_type == TAND))
                        rv = execute(t1, pin, pout, /* no_fork: */ 0);
                break;
 
@@ -2571,6 +2571,10 @@ static int execute(struct op *t, int *pin, int *pout, int no_fork)
                while (setjmp(bc.brkpt))
                        if (isbreak)
                                goto broken;
+               /* Restore areanum value. It may be incremented by execute()
+                * below, and then "continue" may jump back to setjmp above */
+               areanum = a + 1;
+               freearea(areanum + 1);
                brkset(&bc);
                for (t1 = t->left; i-- && *wp != NULL;) {
                        setval(vp, *wp++);
@@ -2584,9 +2588,13 @@ static int execute(struct op *t, int *pin, int *pout, int no_fork)
                while (setjmp(bc.brkpt))
                        if (isbreak)
                                goto broken;
+               /* Restore areanum value. It may be incremented by execute()
+                * below, and then "continue" may jump back to setjmp above */
+               areanum = a + 1;
+               freearea(areanum + 1);
                brkset(&bc);
                t1 = t->left;
-               while ((execute(t1, pin, pout, /* no_fork: */ 0) == 0) == (t->type == TWHILE))
+               while ((execute(t1, pin, pout, /* no_fork: */ 0) == 0) == (t->op_type == TWHILE))
                        rv = execute(t->right, pin, pout, /* no_fork: */ 0);
                brklist = brklist->nextlev;
                break;
@@ -2707,7 +2715,7 @@ static int forkexec(struct op *t, int *pin, int *pout, int no_fork, char **wp)
                        ((t->op_words == NULL) ? "NULL" : t->op_words[0])));
        owp = wp;
        resetsig = 0;
-       if (t->type == TCOM) {
+       if (t->op_type == TCOM) {
                while (*wp++ != NULL)
                        continue;
                cp = *wp;
@@ -2740,7 +2748,7 @@ static int forkexec(struct op *t, int *pin, int *pout, int no_fork, char **wp)
        // longjmps away (at "Run builtin" below), leaving t->op_words clobbered!
        // See http://bugs.busybox.net/view.php?id=846.
        // Now we do not touch t->op_words, but separately pass wp as param list
-       // to builtins 
+       // to builtins
        DBGPRINTF(("FORKEXEC: bltin %p, no_fork %d, owp %p\n", bltin,
                        no_fork, owp));
        /* Don't fork if it is a lone builtin (not in pipe)
@@ -2760,7 +2768,7 @@ static int forkexec(struct op *t, int *pin, int *pout, int no_fork, char **wp)
                DBGPRINTF3(("FORKEXEC: calling vfork()...\n"));
                newpid = vfork();
                if (newpid == -1) {
-                       DBGPRINTF(("FORKEXEC: ERROR, cannot vfork()!\n"));
+                       DBGPRINTF(("FORKEXEC: ERROR, can't vfork()!\n"));
                        return -1;
                }
 
@@ -2810,7 +2818,7 @@ static int forkexec(struct op *t, int *pin, int *pout, int no_fork, char **wp)
        if (iopp) {
                if (bltin && bltin != doexec) {
                        prs(bltin_name);
-                       err(": cannot redirect shell command");
+                       err(": can't redirect shell command");
                        if (forked)
                                _exit(-1);
                        return -1;
@@ -2830,7 +2838,7 @@ static int forkexec(struct op *t, int *pin, int *pout, int no_fork, char **wp)
                        /* Builtin in pipe: disallowed */
                        /* TODO: allow "exec"? */
                        prs(bltin_name);
-                       err(": cannot run builtin as part of pipe");
+                       err(": can't run builtin as part of pipe");
                        if (forked)
                                _exit(-1);
                        return -1;
@@ -2851,10 +2859,10 @@ static int forkexec(struct op *t, int *pin, int *pout, int no_fork, char **wp)
                signal(SIGQUIT, SIG_DFL);
        }
 
-       if (t->type == TPAREN)
+       if (t->op_type == TPAREN)
                _exit(execute(t->left, NOPIPE, NOPIPE, /* no_fork: */ 1));
        if (wp[0] == NULL)
-               _exit(0);
+               _exit(EXIT_SUCCESS);
 
        cp = rexecve(wp[0], wp, makenv(0, NULL));
        prs(wp[0]);
@@ -2883,13 +2891,13 @@ static int iosetup(struct ioword *iop, int pipein, int pipeout)
        DBGPRINTF(("IOSETUP: iop %p, pipein %i, pipeout %i\n", iop,
                           pipein, pipeout));
 
-       if (iop->io_unit == IODEFAULT)  /* take default */
-               iop->io_unit = iop->io_flag & (IOREAD | IOHERE) ? 0 : 1;
+       if (iop->io_fd == IODEFAULT)    /* take default */
+               iop->io_fd = iop->io_flag & (IOREAD | IOHERE) ? 0 : 1;
 
-       if (pipein && iop->io_unit == 0)
+       if (pipein && iop->io_fd == 0)
                return 0;
 
-       if (pipeout && iop->io_unit == 1)
+       if (pipeout && iop->io_fd == 1)
                return 0;
 
        msg = iop->io_flag & (IOREAD | IOHERE) ? "open" : "create";
@@ -2935,24 +2943,21 @@ static int iosetup(struct ioword *iop, int pipein, int pipeout)
                break;
 
        case IODUP:
-               u = dup2(*cp - '0', iop->io_unit);
+               u = dup2(*cp - '0', iop->io_fd);
                break;
 
        case IOCLOSE:
-               close(iop->io_unit);
+               close(iop->io_fd);
                return 0;
        }
 
        if (u < 0) {
                prs(cp);
-               prs(": cannot ");
+               prs(": can't ");
                warn(msg);
                return 1;
        }
-       if (u != iop->io_unit) {
-               dup2(u, iop->io_unit);
-               close(u);
-       }
+       xmove_fd(u, iop->io_fd);
        return 0;
 }
 
@@ -3009,7 +3014,7 @@ static int waitfor(int lastpid, int canintr)
                                        prs(" - core dumped");
                                if (rv >= ARRAY_SIZE(signame) || signame[rv])
                                        prs("\n");
-                               rv = -1;
+                               rv |= 0x80;
                        } else
                                rv = WAITVAL(s);
                }
@@ -3073,8 +3078,6 @@ static const char *rexecve(char *c, char **v, char **envp)
                if (tp != global_env.linep)
                        *tp++ = '/';
                strcpy(tp, c);
-               //for (i = 0; (*tp++ = c[i++]) != '\0';)
-               //      continue;
 
                DBGPRINTF3(("REXECVE: global_env.linep is %s\n", global_env.linep));
 
@@ -3082,10 +3085,13 @@ static const char *rexecve(char *c, char **v, char **envp)
 
                switch (errno) {
                case ENOEXEC:
+                       /* File is executable but file format isnt recognized */
+                       /* Run it as a shell script */
+                       /* (execve above didnt do it itself, unlike execvp) */
                        *v = global_env.linep;
                        v--;
                        tp = *v;
-                       *v = global_env.linep;
+                       *v = (char*)DEFAULT_SHELL;
                        execve(DEFAULT_SHELL, v, envp);
                        *v = tp;
                        return "no shell";
@@ -3097,7 +3103,12 @@ static const char *rexecve(char *c, char **v, char **envp)
                        return "argument list too long";
                }
        }
-       return errno == ENOENT ? "not found" : "cannot execute";
+       if (errno == ENOENT) {
+               exstat = 127; /* standards require this */
+               return "not found";
+       }
+       exstat = 126; /* mimic bash */
+       return "can't execute";
 }
 
 /*
@@ -3130,8 +3141,8 @@ static int run(struct ioarg *argp, int (*f) (struct ioarg *))
 
        errpt = ev;
        if (newenv(setjmp(errpt)) == 0) {
-               wdlist = 0;
-               iolist = 0;
+               wdlist = NULL;
+               iolist = NULL;
                pushio(argp, f);
                global_env.iobase = global_env.iop;
                yynerrs = 0;
@@ -3158,13 +3169,14 @@ static int run(struct ioarg *argp, int (*f) (struct ioarg *))
  * built-in commands: doX
  */
 
-static int dohelp(struct op *t, char **args)
+static int dohelp(struct op *t UNUSED_PARAM, char **args UNUSED_PARAM)
 {
        int col;
        const struct builtincmd *x;
 
-       puts("\nBuilt-in commands:\n"
-            "-------------------");
+       printf("\n"
+               "Built-in commands:\n"
+               "------------------\n");
 
        col = 0;
        x = builtincmds;
@@ -3194,12 +3206,12 @@ static int dohelp(struct op *t, char **args)
        return EXIT_SUCCESS;
 }
 
-static int dolabel(struct op *t, char **args)
+static int dolabel(struct op *t UNUSED_PARAM, char **args UNUSED_PARAM)
 {
        return 0;
 }
 
-static int dochdir(struct op *t, char **args)
+static int dochdir(struct op *t UNUSED_PARAM, char **args)
 {
        const char *cp, *er;
 
@@ -3220,7 +3232,7 @@ static int dochdir(struct op *t, char **args)
        return 1;
 }
 
-static int doshift(struct op *t, char **args)
+static int doshift(struct op *t UNUSED_PARAM, char **args)
 {
        int n;
 
@@ -3239,7 +3251,7 @@ static int doshift(struct op *t, char **args)
 /*
  * execute login and newgrp directly
  */
-static int dologin(struct op *t, char **args)
+static int dologin(struct op *t UNUSED_PARAM, char **args)
 {
        const char *cp;
 
@@ -3254,7 +3266,7 @@ static int dologin(struct op *t, char **args)
        return 1;
 }
 
-static int doumask(struct op *t, char **args)
+static int doumask(struct op *t UNUSED_PARAM, char **args)
 {
        int i;
        char *cp;
@@ -3304,7 +3316,7 @@ static int doexec(struct op *t, char **args)
        return 1;
 }
 
-static int dodot(struct op *t, char **args)
+static int dodot(struct op *t UNUSED_PARAM, char **args)
 {
        int i;
        const char *sp;
@@ -3358,7 +3370,7 @@ static int dodot(struct op *t, char **args)
        return -1;
 }
 
-static int dowait(struct op *t, char **args)
+static int dowait(struct op *t UNUSED_PARAM, char **args)
 {
        int i;
        char *cp;
@@ -3374,7 +3386,7 @@ static int dowait(struct op *t, char **args)
        return 0;
 }
 
-static int doread(struct op *t, char **args)
+static int doread(struct op *t UNUSED_PARAM, char **args)
 {
        char *cp, **wp;
        int nb = 0;
@@ -3386,7 +3398,7 @@ static int doread(struct op *t, char **args)
        }
        for (wp = args + 1; *wp; wp++) {
                for (cp = global_env.linep; !nl && cp < elinep - 1; cp++) {
-                       nb = nonblock_safe_read(0, cp, sizeof(*cp));
+                       nb = nonblock_safe_read(STDIN_FILENO, cp, sizeof(*cp));
                        if (nb != sizeof(*cp))
                                break;
                        nl = (*cp == '\n');
@@ -3401,12 +3413,12 @@ static int doread(struct op *t, char **args)
        return nb <= 0;
 }
 
-static int doeval(struct op *t, char **args)
+static int doeval(struct op *t UNUSED_PARAM, char **args)
 {
        return RUN(awordlist, args + 1, wdchar);
 }
 
-static int dotrap(struct op *t, char **args)
+static int dotrap(struct op *t UNUSED_PARAM, char **args)
 {
        int n, i;
        int resetsig;
@@ -3487,12 +3499,12 @@ static int getn(char *as)
        return n * m;
 }
 
-static int dobreak(struct op *t, char **args)
+static int dobreak(struct op *t UNUSED_PARAM, char **args)
 {
        return brkcontin(args[1], 1);
 }
 
-static int docontinue(struct op *t, char **args)
+static int docontinue(struct op *t UNUSED_PARAM, char **args)
 {
        return brkcontin(args[1], 0);
 }
@@ -3520,7 +3532,7 @@ static int brkcontin(char *cp, int val)
        /* NOTREACHED */
 }
 
-static int doexit(struct op *t, char **args)
+static int doexit(struct op *t UNUSED_PARAM, char **args)
 {
        char *cp;
 
@@ -3536,13 +3548,13 @@ static int doexit(struct op *t, char **args)
        return 0;
 }
 
-static int doexport(struct op *t, char **args)
+static int doexport(struct op *t UNUSED_PARAM, char **args)
 {
        rdexp(args + 1, export, EXPORT);
        return 0;
 }
 
-static int doreadonly(struct op *t, char **args)
+static int doreadonly(struct op *t UNUSED_PARAM, char **args)
 {
        rdexp(args + 1, ronly, RONLY);
        return 0;
@@ -3578,7 +3590,7 @@ static void badid(char *s)
        err(": bad identifier");
 }
 
-static int doset(struct op *t, char **args)
+static int doset(struct op *t UNUSED_PARAM, char **args)
 {
        struct var *vp;
        char *cp;
@@ -3587,7 +3599,7 @@ static int doset(struct op *t, char **args)
        cp = args[1];
        if (cp == NULL) {
                for (vp = vlist; vp; vp = vp->next)
-                       varput(vp->name, 1);
+                       varput(vp->name, STDOUT_FILENO);
                return 0;
        }
        if (*cp == '-') {
@@ -3626,8 +3638,8 @@ static int doset(struct op *t, char **args)
 static void varput(char *s, int out)
 {
        if (isalnum(*s) || *s == '_') {
-               write(out, s, strlen(s));
-               write(out, "\n", 1);
+               xwrite_str(out, s);
+               xwrite(out, "\n", 1);
        }
 }
 
@@ -3653,7 +3665,7 @@ static void times_fmt(char *buf, clock_t val, unsigned clk_tck)
 #endif
 }
 
-static int dotimes(struct op *t, char **args)
+static int dotimes(struct op *t UNUSED_PARAM, char **args UNUSED_PARAM)
 {
        struct tms buf;
        unsigned clk_tck = sysconf(_SC_CLK_TCK);
@@ -3983,7 +3995,7 @@ static int dollar(int quoted)
                switch (c) {
                case '=':
                        if (isdigit(*s)) {
-                               err("cannot use ${...=...} with $n");
+                               err("can't use ${...=...} with $n");
                                gflg = 1;
                                break;
                        }
@@ -4209,7 +4221,7 @@ static int grave(int quoted)
        prs(argument_list[0]);
        prs(": ");
        err(cp);
-       _exit(1);
+       _exit(EXIT_FAILURE);
 }
 
 
@@ -4311,9 +4323,7 @@ static void globname(char *we, char *pp)
        dname[NAME_MAX] = '\0';
        while ((de = readdir(dirp)) != NULL) {
                /* XXX Hmmm... What this could be? (abial) */
-               /*
-                  if (ent[j].d_ino == 0)
-                     continue;
+               /* if (ent[j].d_ino == 0) continue;
                 */
                strncpy(dname, de->d_name, NAME_MAX);
                if (dname[0] == '.')
@@ -4486,7 +4496,7 @@ static int readc(void)
                                if (global_env.iop == iostack)
                                        ioecho(c);
                                global_env.iop->prev = c;
-                               return global_env.iop->prev;
+                               return c;
                        }
                        if (global_env.iop->task == XIO && global_env.iop->prev != '\n') {
                                global_env.iop->prev = 0;
@@ -4498,7 +4508,7 @@ static int readc(void)
                if (global_env.iop->task == XIO) {
                        if (multiline) {
                                global_env.iop->prev = 0;
-                               return global_env.iop->prev;
+                               return 0;
                        }
                        if (interactive && global_env.iop == iostack + 1) {
 #if ENABLE_FEATURE_EDITING
@@ -4524,7 +4534,7 @@ static int readc(void)
 static void ioecho(char c)
 {
        if (FLAG['v'])
-               write(2, &c, sizeof c);
+               write(STDERR_FILENO, &c, sizeof c);
 }
 
 static void pushio(struct ioarg *argp, int (*fn) (struct ioarg *))
@@ -4584,7 +4594,7 @@ static void pushio(struct ioarg *argp, int (*fn) (struct ioarg *))
        if (fn == filechar || fn == linechar)
                global_env.iop->task = XIO;
        else if (fn == (int (*)(struct ioarg *)) gravechar
-        || fn == (int (*)(struct ioarg *)) qgravechar)
+             || fn == (int (*)(struct ioarg *)) qgravechar)
                global_env.iop->task = XGRAVE;
        else
                global_env.iop->task = XOTHER;
@@ -4604,16 +4614,16 @@ static struct io *setbase(struct io *ip)
  */
 
 /*
- * Produce the characters of a string, then a newline, then EOF.
+ * Produce the characters of a string, then a newline, then NUL.
  */
 static int nlchar(struct ioarg *ap)
 {
-       int c;
+       char c;
 
        if (ap->aword == NULL)
-               return 0;
+               return '\0';
        c = *ap->aword++;
-       if (c == 0) {
+       if (c == '\0') {
                ap->aword = NULL;
                return '\n';
        }
@@ -4735,7 +4745,7 @@ static int filechar(struct ioarg *ap)
                while (size == 0 || position >= size) {
                        size = read_line_input(current_prompt, filechar_cmdbuf, BUFSIZ, line_input_state);
                        if (size < 0) /* Error/EOF */
-                               exit(0);
+                               exit(EXIT_SUCCESS);
                        position = 0;
                        /* if Ctrl-C, size == 0 and loop will repeat */
                }
@@ -4820,7 +4830,7 @@ static int linechar(struct ioarg *ap)
 }
 
 /*
- * remap fd into Shell's fd space
+ * Remap fd into shell's fd space
  */
 static int remap(int fd)
 {
@@ -5057,7 +5067,7 @@ static void freehere(int area)
        DBGPRINTF6(("FREEHERE: enter, area=%d\n", area));
 
        hl = NULL;
-       for (h = acthere; h != NULL; h = h->h_next)
+       for (h = acthere; h != NULL; h = h->h_next) {
                if (getarea((char *) h) >= area) {
                        if (h->h_iop->io_name != NULL)
                                unlink(h->h_iop->io_name);
@@ -5065,8 +5075,10 @@ static void freehere(int area)
                                acthere = h->h_next;
                        else
                                hl->h_next = h->h_next;
-               } else
+               } else {
                        hl = h;
+               }
+       }
 }
 
 
@@ -5204,7 +5216,7 @@ int msh_main(int argc, char **argv)
 
 /* Shell is non-interactive, activate printf-based debug */
 #ifdef MSHDEBUG
-                       mshdbg = (int) (((char) (mshdbg_var->value[0])) - '0');
+                       mshdbg = mshdbg_var->value[0] - '0';
                        if (mshdbg < 0)
                                mshdbg = 0;
 #endif
@@ -5212,7 +5224,7 @@ int msh_main(int argc, char **argv)
 
                        name = *++argv;
                        if (newfile(name))
-                               exit(1);                /* Exit on error */
+                               exit(EXIT_FAILURE);  /* Exit on error */
                }
        }