From a3dd4d45c46f549489ae39a4dc77c0aa66b9a3e6 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 1 May 2022 03:04:47 -0500 Subject: [PATCH] Add filename and line number to syntax error. --- toys/pending/sh.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/toys/pending/sh.c b/toys/pending/sh.c index 6b12dea5..750cec5c 100644 --- a/toys/pending/sh.c +++ b/toys/pending/sh.c @@ -256,7 +256,7 @@ GLOBALS( long long SECONDS; char *isexec, *wcpat; unsigned options, jobcnt, LINENO; - int hfd, pid, bangpid, varslen, srclvl, recursion; + int hfd, pid, bangpid, srclvl, recursion; // Callable function array struct sh_function { @@ -282,11 +282,11 @@ GLOBALS( long flags; char *str; } *vars; - long varslen, shift; + long varslen, shift, oldlineno; struct sh_function *func; // TODO wire this up struct sh_pipeline *pl; - char *ifs; + char *ifs, *omnom; struct sh_arg arg; struct arg_list *delete; @@ -338,7 +338,10 @@ static const char *redirectors[] = {"<<<", "<<-", "<<", "<&", "<>", "<", ">>", static void syntax_err(char *s) { - error_msg("syntax error: %s", s); + struct sh_fcall *ff = TT.ff; +// TODO: script@line only for script not interactive. + for (ff = TT.ff; ff != TT.ff->prev; ff = ff->next) if (ff->omnom) break; + error_msg("syntax error '%s'@%u: %s", ff->omnom ? : "-c", TT.LINENO, s); toys.exitval = 2; if (!(TT.options&FLAG_i)) xexit(); } @@ -3754,6 +3757,8 @@ int do_source(char *name, FILE *ff) goto end; } + if (name) TT.ff->omnom = name; + // TODO fix/catch NONBLOCK on input? // TODO when DO we reset lineno? (!LINENO means \0 returns 1) // when do we NOT reset lineno? Inherit but preserve perhaps? newline in $()? @@ -4395,7 +4400,10 @@ void source_main(void) call_function(); TT.ff->arg.v = toys.optargs; TT.ff->arg.c = toys.optc; + TT.ff->oldlineno = TT.LINENO; + TT.LINENO = 0; do_source(name, ff); + TT.LINENO = TT.ff->oldlineno; free(dlist_pop(&TT.ff)); --TT.srclvl; } -- 2.39.2