From 6ab99f87b7494b6080914a96f6f438ab20f8c0df Mon Sep 17 00:00:00 2001 From: Jilles Tjoelker Date: Fri, 30 Aug 2013 13:25:15 +0000 Subject: [PATCH] sh: Add a function for the case where one token is required in the parse. --- bin/sh/parser.c | 54 +++++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/bin/sh/parser.c b/bin/sh/parser.c index b23f45581aa1..4bf869e827ef 100644 --- a/bin/sh/parser.c +++ b/bin/sh/parser.c @@ -121,6 +121,7 @@ static int readtoken(void); static int xxreadtoken(void); static int readtoken1(int, const char *, const char *, int); static int noexpand(char *); +static void consumetoken(int); static void synexpect(int) __dead2; static void synerror(const char *) __dead2; static void setprompt(int); @@ -413,8 +414,7 @@ command(void) n1->type = NIF; if ((n1->nif.test = list(0, 0)) == NULL) synexpect(-1); - if (readtoken() != TTHEN) - synexpect(TTHEN); + consumetoken(TTHEN); n1->nif.ifpart = list(0, 0); n2 = n1; while (readtoken() == TELIF) { @@ -423,8 +423,7 @@ command(void) n2->type = NIF; if ((n2->nif.test = list(0, 0)) == NULL) synexpect(-1); - if (readtoken() != TTHEN) - synexpect(TTHEN); + consumetoken(TTHEN); n2->nif.ifpart = list(0, 0); } if (lasttoken == TELSE) @@ -433,27 +432,20 @@ command(void) n2->nif.elsepart = NULL; tokpushback++; } - if (readtoken() != TFI) - synexpect(TFI); + consumetoken(TFI); checkkwd = CHKKWD | CHKALIAS; break; case TWHILE: - case TUNTIL: { - int got; + case TUNTIL: n1 = (union node *)stalloc(sizeof (struct nbinary)); n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL; if ((n1->nbinary.ch1 = list(0, 0)) == NULL) synexpect(-1); - if ((got=readtoken()) != TDO) { -TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : "")); - synexpect(TDO); - } + consumetoken(TDO); n1->nbinary.ch2 = list(0, 0); - if (readtoken() != TDONE) - synexpect(TDONE); + consumetoken(TDONE); checkkwd = CHKKWD | CHKALIAS; break; - } case TFOR: if (readtoken() != TWORD || quoteflag || ! goodname(wordtext)) synerror("Bad for loop variable"); @@ -501,15 +493,13 @@ TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : "")); else synexpect(-1); n1->nfor.body = list(0, 0); - if (readtoken() != t) - synexpect(t); + consumetoken(t); checkkwd = CHKKWD | CHKALIAS; break; case TCASE: n1 = (union node *)stalloc(sizeof (struct ncase)); n1->type = NCASE; - if (readtoken() != TWORD) - synexpect(TWORD); + consumetoken(TWORD); n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg)); n2->type = NARG; n2->narg.text = wordtext; @@ -562,15 +552,13 @@ TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : "")); n1->type = NSUBSHELL; n1->nredir.n = list(0, 0); n1->nredir.redirect = NULL; - if (readtoken() != TRP) - synexpect(TRP); + consumetoken(TRP); checkkwd = CHKKWD | CHKALIAS; is_subshell = 1; break; case TBEGIN: n1 = list(0, 0); - if (readtoken() != TEND) - synexpect(TEND); + consumetoken(TEND); checkkwd = CHKKWD | CHKALIAS; break; /* A simple command must have at least one redirection or word. */ @@ -659,8 +647,7 @@ simplecmd(union node **rpp, union node *redir) } else if (lasttoken == TLP && app == &args->narg.next && rpp == orig_rpp) { /* We have a function */ - if (readtoken() != TRP) - synexpect(TRP); + consumetoken(TRP); funclinno = plinno; /* * - Require plain text. @@ -734,8 +721,7 @@ parsefname(void) { union node *n = redirnode; - if (readtoken() != TWORD) - synexpect(-1); + consumetoken(TWORD); if (n->type == NHERE) { struct heredoc *here = heredoc; struct heredoc *p; @@ -1094,10 +1080,8 @@ parsebackq(char *out, struct nodelist **pbqlist, if (oldstyle) doprompt = saveprompt; - else { - if (readtoken() != TRP) - synexpect(TRP); - } + else + consumetoken(TRP); (*nlpp)->n = n; if (oldstyle) { @@ -1880,6 +1864,14 @@ isassignment(const char *p) } +static void +consumetoken(int token) +{ + if (readtoken() != token) + synexpect(token); +} + + /* * Called when an unexpected token is read during the parse. The argument * is the token that is expected, or -1 if more than one type of token can