From 432f6040cbde877820f49b69062eb04d80fdb886 Mon Sep 17 00:00:00 2001 From: dyx Date: Fri, 6 May 2022 23:05:54 +0800 Subject: [PATCH] report lineno while meet the error --- Makefile | 14 ++++++++------ tc.c | 6 +++++- test/5.err | 1 + test/5.in | 2 ++ test/6.err | 1 + test/6.in | 4 ++++ version | 1 + 7 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 test/5.err create mode 100644 test/5.in create mode 100644 test/6.err create mode 100644 test/6.in create mode 100644 version diff --git a/Makefile b/Makefile index 56fb29f..d2a0a49 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,5 @@ .PHONY: all install test clean -version = 0.0.0 prefix = /usr/local exec_prefix = $(prefix) bindir = $(exec_prefix)/bin @@ -33,15 +32,18 @@ test: all bname=`basename $${in%%.*}`; \ echo running test $${bname}...; \ out=testenv/$${bname}.out; \ - ans=test/$${bname}.out; \ - <$$in ./shsub >$$out; \ - diff -u $$ans $$out; \ + err=testenv/$${bname}.err; \ + ansout=test/$${bname}.out; \ + anserr=test/$${bname}.err; \ + <$$in ./shsub >$$out 2>$$err; \ + [ -f $$ansout ] && diff -u $$ansout $$out; \ + [ -f $$anserr ] && diff -u $$anserr $$err; \ done; \ echo all tests passed -shsub: shsub.sh usage LICENSE +shsub: shsub.sh usage LICENSE version m4 \ - -D__version__='$(version)' \ + -D__version__="`cat version`" \ -D__license__="`cat LICENSE`" \ -D__usage__="`cat usage`" \ shsub.sh > shsub diff --git a/tc.c b/tc.c index 59e4691..022b570 100644 --- a/tc.c +++ b/tc.c @@ -56,6 +56,7 @@ int cstack[TOKSIZ], cssize; int popc(void); int pushc(int c); +int lineno = 1; char token[TOKSIZ]; enum token gettok(void); int dryread(char *s); @@ -68,6 +69,8 @@ main() puts("set -e"); for (st = SINIT; st != STERM && st != SERR; st = next) { in = gettok(); + if (token[0] == '\n') + lineno++; if (in == ESCOPEN) strcpy(token, "<%"); if (in == ESCCLS) @@ -117,7 +120,8 @@ void entrexp(enum state from, enum token in) void entrerr(enum state from, enum token in) { - fprintf(stderr, "illegal token: %s", token); + fprintf(stderr, "shsub: illegal token %s at line %d\n", + token, lineno); exit(ESYNTAX); } diff --git a/test/5.err b/test/5.err new file mode 100644 index 0000000..0c6cfe0 --- /dev/null +++ b/test/5.err @@ -0,0 +1 @@ +shsub: illegal token %> at line 2 diff --git a/test/5.in b/test/5.in new file mode 100644 index 0000000..b8b215a --- /dev/null +++ b/test/5.in @@ -0,0 +1,2 @@ +<% echo hello world %> +%> this line has an error diff --git a/test/6.err b/test/6.err new file mode 100644 index 0000000..f29ad26 --- /dev/null +++ b/test/6.err @@ -0,0 +1 @@ +shsub: illegal token %> at line 1 diff --git a/test/6.in b/test/6.in new file mode 100644 index 0000000..b5a8057 --- /dev/null +++ b/test/6.in @@ -0,0 +1,4 @@ +%> +<% +<% echo hello world %> +%> this line has an error diff --git a/version b/version new file mode 100644 index 0000000..8acdd82 --- /dev/null +++ b/version @@ -0,0 +1 @@ +0.0.1