Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/sh PR bin/48498 PR bin/52426



details:   https://anonhg.NetBSD.org/src/rev/5f0fdebdc312
branches:  trunk
changeset: 825652:5f0fdebdc312
user:      kre <kre%NetBSD.org@localhost>
date:      Wed Jul 26 23:09:41 2017 +0000

description:
PR bin/48498   PR bin/52426

Don't ignore unexpected reserved words after ';'
Don't allow any random token type as a case stmt pattern, only a word.
        Those are ancient ash bugs and do not affect correct scripts.

Don't ignore redirects in a case stmt list where the list is nothing but
redirects (if the pattern matches, the redirects should be performed).
        That was introduced when a redirect only case stmt list was allowed
        (older shells had generated a syntax error.)

Random cleanups/refactoring taken from or inspired by the FreeBSD sh
parser ...  use makename() consistently to create a NARG node - we
were using it in a couple of places but most NARG node creation was open
coded.  Introduce consumetoken() (from FreeBSD) to handle the fairly
common case where exactly one token type must come next, and we need to
check that, and skip past it when found (or error) and linebreak() (new)
to handle places where optional \n's are permitted.
Both previously open coded.

Simplify list() by removing its second arg, which was only ever used when
handling the end of a `` (old style command substitution).  Simply move
the code from inside list() to just after its call in the `` case (from
FreeBSD.)

diffstat:

 bin/sh/parser.c |  223 ++++++++++++++++++++++++++++---------------------------
 1 files changed, 112 insertions(+), 111 deletions(-)

diffs (truncated from 489 to 300 lines):

diff -r 4e896373f18b -r 5f0fdebdc312 bin/sh/parser.c
--- a/bin/sh/parser.c   Wed Jul 26 17:50:20 2017 +0000
+++ b/bin/sh/parser.c   Wed Jul 26 23:09:41 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: parser.c,v 1.141 2017/07/03 20:16:44 kre Exp $ */
+/*     $NetBSD: parser.c,v 1.142 2017/07/26 23:09:41 kre Exp $ */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)parser.c   8.7 (Berkeley) 5/16/95";
 #else
-__RCSID("$NetBSD: parser.c,v 1.141 2017/07/03 20:16:44 kre Exp $");
+__RCSID("$NetBSD: parser.c,v 1.142 2017/07/26 23:09:41 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -103,12 +103,12 @@
        .ps_elided_nl = 0,
 };
 
-STATIC union node *list(int, int);
+STATIC union node *list(int);
 STATIC union node *andor(void);
 STATIC union node *pipeline(void);
 STATIC union node *command(void);
 STATIC union node *simplecmd(union node **, union node *);
-STATIC union node *makename(void);
+STATIC union node *makename(int);
 STATIC void parsefname(void);
 STATIC int slurp_heredoc(char *const, const int, const int);
 STATIC void readheredocs(void);
@@ -117,6 +117,8 @@
 STATIC int xxreadtoken(void);
 STATIC int readtoken1(int, char const *, int);
 STATIC int noexpand(char *);
+STATIC void linebreak(void);
+STATIC void consumetoken(int);
 STATIC void synexpect(int, const char *) __dead;
 STATIC void synerror(const char *) __dead;
 STATIC void setprompt(int);
@@ -143,6 +145,7 @@
        parsing++;
 #endif
        tokpushback = 0;
+       checkkwd = 0;
        doprompt = interact;
        if (doprompt)
                setprompt(1);
@@ -162,7 +165,7 @@
        parsing++;
 #endif
        tokpushback++;
-       n = list(1, 0);
+       n = list(1);
 #ifdef DEBUG
        parsing--;
 #endif
@@ -174,12 +177,12 @@
 
 
 STATIC union node *
-list(int nlflag, int erflag)
+list(int nlflag)
 {
        union node *n1, *n2, *n3;
        int tok;
 
-       CTRACE(DBG_PARSE, ("list(%d,%d): entered @%d\n",nlflag,erflag,plinno));
+       CTRACE(DBG_PARSE, ("list(%d): entered @%d\n",nlflag,plinno));
 
        checkkwd = 2;
        if (nlflag == 0 && tokendlist[peektoken()])
@@ -189,11 +192,11 @@
                n2 = andor();
                tok = readtoken();
                if (tok == TBACKGND) {
-                       if (n2->type == NCMD || n2->type == NPIPE) {
+                       if (n2->type == NCMD || n2->type == NPIPE)
                                n2->ncmd.backgnd = 1;
-                       } else if (n2->type == NREDIR) {
+                       else if (n2->type == NREDIR)
                                n2->type = NBACKGND;
-                       } else {
+                       else {
                                n3 = stalloc(sizeof(struct nredir));
                                n3->type = NBACKGND;
                                n3->nredir.n = n2;
@@ -201,16 +204,16 @@
                                n2 = n3;
                        }
                }
-               if (n1 == NULL) {
-                       n1 = n2;
-               }
-               else {
+
+               if (n1 != NULL) {
                        n3 = stalloc(sizeof(struct nbinary));
                        n3->type = NSEMI;
                        n3->nbinary.ch1 = n1;
                        n3->nbinary.ch2 = n2;
                        n1 = n3;
-               }
+               } else
+                       n1 = n2;
+
                switch (tok) {
                case TBACKGND:
                case TSEMI:
@@ -221,18 +224,20 @@
                                readheredocs();
                                if (nlflag)
                                        return n1;
-                       } else {
+                       } else if (tok == TEOF && nlflag)
+                               return n1;
+                       else
                                tokpushback++;
-                       }
+
                        checkkwd = 2;
-                       if (tokendlist[peektoken()])
+                       if (!nlflag && tokendlist[peektoken()])
                                return n1;
                        break;
                case TEOF:
                        pungetc();      /* push back EOF on input */
                        return n1;
                default:
-                       if (nlflag || erflag)
+                       if (nlflag)
                                synexpect(-1, 0);
                        tokpushback++;
                        return n1;
@@ -357,69 +362,55 @@
        case TIF:
                n1 = stalloc(sizeof(struct nif));
                n1->type = NIF;
-               n1->nif.test = list(0, 0);
-               if (readtoken() != TTHEN)
-                       synexpect(TTHEN, 0);
-               n1->nif.ifpart = list(0, 0);
+               n1->nif.test = list(0);
+               consumetoken(TTHEN);
+               n1->nif.ifpart = list(0);
                n2 = n1;
                while (readtoken() == TELIF) {
                        n2->nif.elsepart = stalloc(sizeof(struct nif));
                        n2 = n2->nif.elsepart;
                        n2->type = NIF;
-                       n2->nif.test = list(0, 0);
-                       if (readtoken() != TTHEN)
-                               synexpect(TTHEN, 0);
-                       n2->nif.ifpart = list(0, 0);
+                       n2->nif.test = list(0);
+                       consumetoken(TTHEN);
+                       n2->nif.ifpart = list(0);
                }
                if (lasttoken == TELSE)
-                       n2->nif.elsepart = list(0, 0);
+                       n2->nif.elsepart = list(0);
                else {
                        n2->nif.elsepart = NULL;
                        tokpushback++;
                }
-               if (readtoken() != TFI)
-                       synexpect(TFI, 0);
+               consumetoken(TFI);
                checkkwd = 1;
                break;
        case TWHILE:
-       case TUNTIL: {
-               int got;
-
+       case TUNTIL:
                n1 = stalloc(sizeof(struct nbinary));
                n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
-               n1->nbinary.ch1 = list(0, 0);
-               if ((got=readtoken()) != TDO) {
-                       VTRACE(DBG_PARSE, ("expecting DO got %s %s\n",
-                           tokname[got], got == TWORD ? wordtext : ""));
-                       synexpect(TDO, 0);
-               }
-               n1->nbinary.ch2 = list(0, 0);
-               if (readtoken() != TDONE)
-                       synexpect(TDONE, 0);
+               n1->nbinary.ch1 = list(0);
+               consumetoken(TDO);
+               n1->nbinary.ch2 = list(0);
+               consumetoken(TDONE);
                checkkwd = 1;
                break;
-       }
        case TFOR:
                if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
                        synerror("Bad for loop variable");
                n1 = stalloc(sizeof(struct nfor));
                n1->type = NFOR;
                n1->nfor.var = wordtext;
-               if (readtoken()==TWORD && !quoteflag && equal(wordtext,"in")) {
+               linebreak();
+               if (lasttoken==TWORD && !quoteflag && equal(wordtext,"in")) {
                        app = &ap;
                        while (readtoken() == TWORD) {
-                               n2 = stalloc(sizeof(struct narg));
-                               n2->type = NARG;
-                               n2->narg.text = wordtext;
-                               n2->narg.backquote = backquotelist;
-                               n2->narg.lineno = startlinno;
+                               n2 = makename(startlinno);
                                *app = n2;
                                app = &n2->narg.next;
                        }
                        *app = NULL;
                        n1->nfor.args = ap;
                        if (lasttoken != TNL && lasttoken != TSEMI)
-                               synexpect(-1, 0);
+                               synexpect(TSEMI, 0);
                } else {
                        static char argvars[5] = {
                            CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
@@ -445,30 +436,24 @@
                else if (t == TBEGIN)
                        t = TEND;
                else
-                       synexpect(-1, 0);
-               n1->nfor.body = list(0, 0);
-               if (readtoken() != t)
-                       synexpect(t, 0);
+                       synexpect(TDO, 0);
+               n1->nfor.body = list(0);
+               consumetoken(t);
                checkkwd = 1;
                break;
        case TCASE:
                n1 = stalloc(sizeof(struct ncase));
                n1->type = NCASE;
                n1->ncase.lineno = startlinno - elided_nl;
-               if (readtoken() != TWORD)
-                       synexpect(TWORD, 0);
-               n1->ncase.expr = n2 = stalloc(sizeof(struct narg));
-               n2->type = NARG;
-               n2->narg.text = wordtext;
-               n2->narg.backquote = backquotelist;
-               n2->narg.lineno = startlinno;
-               n2->narg.next = NULL;
-               while (readtoken() == TNL);
-               if (lasttoken != TWORD || ! equal(wordtext, "in"))
+               consumetoken(TWORD);
+               n1->ncase.expr = makename(startlinno);
+               linebreak();
+               if (lasttoken != TWORD || !equal(wordtext, "in"))
                        synexpect(-1, "in");
                cpp = &n1->ncase.cases;
                noalias = 1;
-               checkkwd = 2, readtoken();
+               checkkwd = 2;
+               readtoken();
                /*
                 * Both ksh and bash accept 'case x in esac'
                 * so configure scripts started taking advantage of this.
@@ -487,28 +472,25 @@
                 */
                while (lasttoken != TESAC) {
                        *cpp = cp = stalloc(sizeof(struct nclist));
-                       if (lasttoken == TLP)
-                               readtoken();
                        cp->type = NCLIST;
                        app = &cp->nclist.pattern;
+                       if (lasttoken == TLP)
+                               readtoken();
                        for (;;) {
-                               *app = ap = stalloc(sizeof(struct narg));
-                               ap->type = NARG;
-                               ap->narg.lineno = startlinno;
-                               ap->narg.text = wordtext;
-                               ap->narg.backquote = backquotelist;
-                               if (checkkwd = 2, readtoken() != TPIPE)
+                               if (lasttoken < TWORD)
+                                       synexpect(TWORD, 0);
+                               *app = ap = makename(startlinno);
+                               checkkwd = 2;
+                               if (readtoken() != TPIPE)
                                        break;
                                app = &ap->narg.next;
                                readtoken();
                        }
-                       ap->narg.next = NULL;
                        noalias = 0;
-                       if (lasttoken != TRP) {
+                       if (lasttoken != TRP)
                                synexpect(TRP, 0);
-                       }
                        cp->nclist.lineno = startlinno;
-                       cp->nclist.body = list(0, 0);
+                       cp->nclist.body = list(0);
 
                        checkkwd = 2;



Home | Main Index | Thread Index | Old Index