From 5bb39bb1995cb12d8da76b1d482df9be1acc2eb4 Mon Sep 17 00:00:00 2001 From: Harald van Dijk Date: Tue, 15 Mar 2011 15:52:05 +0800 Subject: [SHELL] Improve LINENO support This patch improves LINENO support by storing line numbers in the parse tree, for commands as well as for function definitions. It makes LINENO behaves properly when calling functions, and has the added benefit of improved line numbers in error messages when the last-parsed command is not the last-executed one. It removes the earlier LINENO support, and instead sets LINENO from evaltree when a command is executed Signed-off-by: Harald van Dijk Signed-off-by: Herbert Xu --- src/parser.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) (limited to 'src/parser.c') diff --git a/src/parser.c b/src/parser.c index be20ff7..0bfd620 100644 --- a/src/parser.c +++ b/src/parser.c @@ -93,7 +93,6 @@ struct nodelist *backquotelist; union node *redirnode; struct heredoc *heredoc; int quoteflag; /* set if (part of) last token was quoted */ -int startlinno; /* line # where last token started */ STATIC union node *list(int); @@ -304,10 +303,13 @@ command(void) union node *redir, **rpp; union node **rpp2; int t; + int savelinno; redir = NULL; rpp2 = &redir; + savelinno = plinno; + switch (readtoken()) { default: synexpect(-1); @@ -356,6 +358,7 @@ TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : "")); synerror("Bad for loop variable"); n1 = (union node *)stalloc(sizeof (struct nfor)); n1->type = NFOR; + n1->nfor.linno = savelinno; n1->nfor.var = wordtext; checkkwd = CHKNL | CHKKWD | CHKALIAS; if (readtoken() == TIN) { @@ -395,6 +398,7 @@ TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : "")); case TCASE: n1 = (union node *)stalloc(sizeof (struct ncase)); n1->type = NCASE; + n1->ncase.linno = savelinno; if (readtoken() != TWORD) synexpect(TWORD); n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg)); @@ -445,6 +449,7 @@ next_case: case TLP: n1 = (union node *)stalloc(sizeof (struct nredir)); n1->type = NSUBSHELL; + n1->nredir.linno = savelinno; n1->nredir.n = list(0); n1->nredir.redirect = NULL; t = TRP; @@ -477,6 +482,7 @@ redir: if (n1->type != NSUBSHELL) { n2 = (union node *)stalloc(sizeof (struct nredir)); n2->type = NREDIR; + n2->nredir.linno = savelinno; n2->nredir.n = n1; n1 = n2; } @@ -494,6 +500,7 @@ simplecmd(void) { union node *vars, **vpp; union node **rpp, *redir; int savecheckkwd; + int savelinno; args = NULL; app = &args; @@ -503,6 +510,7 @@ simplecmd(void) { rpp = &redir; savecheckkwd = CHKALIAS; + savelinno = plinno; for (;;) { checkkwd = savecheckkwd; switch (readtoken()) { @@ -546,7 +554,9 @@ simplecmd(void) { synerror("Bad function name"); n->type = NDEFUN; checkkwd = CHKNL | CHKKWD | CHKALIAS; - n->narg.next = command(); + n->ndefun.text = n->narg.text; + n->ndefun.linno = plinno; + n->ndefun.body = command(); return n; } /* fall through */ @@ -561,6 +571,7 @@ out: *rpp = NULL; n = (union node *)stalloc(sizeof (struct ncmd)); n->type = NCMD; + n->ncmd.linno = savelinno; n->ncmd.args = args; n->ncmd.assign = vars; n->ncmd.redirect = redir; @@ -738,8 +749,6 @@ out: * quoted. * If the token is TREDIR, then we set redirnode to a structure containing * the redirection. - * In all cases, the variable startlinno is set to the number of the line - * on which the token starts. * * [Change comment: here documents and internal procedures] * [Readtoken shouldn't have any arguments. Perhaps we should make the @@ -763,7 +772,6 @@ xxreadtoken(void) if (needprompt) { setprompt(2); } - startlinno = plinno; for (;;) { /* until token or start of word found */ c = pgetc_macro(); switch (c) { @@ -776,7 +784,7 @@ xxreadtoken(void) continue; case '\\': if (pgetc() == '\n') { - startlinno = lineno_inc(); + plinno++; if (doprompt) setprompt(2); continue; @@ -784,7 +792,7 @@ xxreadtoken(void) pungetc(); goto breakloop; case '\n': - lineno_inc(); + plinno++; needprompt = doprompt; RETURN(TNL); case PEOF: @@ -855,7 +863,6 @@ readtoken1(int firstc, char const *syntax, char *eofmark, int striptabs) /* syntax before arithmetic */ char const *uninitialized_var(prevsyntax); - startlinno = plinno; dblquote = 0; if (syntax == DQSYNTAX) dblquote = 1; @@ -886,7 +893,7 @@ readtoken1(int firstc, char const *syntax, char *eofmark, int striptabs) if (syntax == BASESYNTAX) goto endword; /* exit outer loop */ USTPUTC(c, out); - lineno_inc(); + plinno++; if (doprompt) setprompt(2); c = pgetc(); @@ -907,6 +914,7 @@ readtoken1(int firstc, char const *syntax, char *eofmark, int striptabs) USTPUTC('\\', out); pungetc(); } else if (c == '\n') { + plinno++; if (doprompt) setprompt(2); } else { @@ -1008,7 +1016,6 @@ endword: if (syntax != BASESYNTAX && eofmark == NULL) synerror("Unterminated quoted string"); if (varnest != 0) { - startlinno = plinno; /* { */ synerror("Missing '}'"); } @@ -1065,7 +1072,7 @@ checkend: { if (c == '\n' || c == PEOF) { c = PEOF; - lineno_inc(); + plinno++; needprompt = doprompt; } else { int len; @@ -1315,7 +1322,7 @@ parsebackq: { case '\\': if ((pc = pgetc()) == '\n') { - lineno_inc(); + plinno++; if (doprompt) setprompt(2); /* @@ -1336,11 +1343,10 @@ parsebackq: { case PEOF: case PEOA: - startlinno = plinno; synerror("EOF in backquote substitution"); case '\n': - lineno_inc(); + plinno++; needprompt = doprompt; break; @@ -1472,6 +1478,7 @@ synexpect(int token) STATIC void synerror(const char *msg) { + errlinno = plinno; sh_error("Syntax error: %s", msg); /* NOTREACHED */ } -- cgit 1.4.1