diff options
author | 2003-09-28 07:57:57 +0000 | |
---|---|---|
committer | 2003-09-28 07:57:57 +0000 | |
commit | ac8683415d337cb761901affd6b3bd236d49f204 (patch) | |
tree | 00d844f9f5a8fbc162f1aab313e4a431b2885feb | |
parent | realloc(3) cleanup. (diff) | |
download | wireguard-openbsd-ac8683415d337cb761901affd6b3bd236d49f204.tar.xz wireguard-openbsd-ac8683415d337cb761901affd6b3bd236d49f204.zip |
Better error hanndling:
- skip current line on syntax error
- detect EOF in strings and comments
- report correct line number in above case
- more consistent warning and errors
ok henning@
-rw-r--r-- | usr.bin/bc/bc.y | 26 | ||||
-rw-r--r-- | usr.bin/bc/extern.h | 4 | ||||
-rw-r--r-- | usr.bin/bc/scan.l | 11 |
3 files changed, 22 insertions, 19 deletions
diff --git a/usr.bin/bc/bc.y b/usr.bin/bc/bc.y index bdab4153118..31fa4df9524 100644 --- a/usr.bin/bc/bc.y +++ b/usr.bin/bc/bc.y @@ -1,5 +1,5 @@ %{ -/* $OpenBSD: bc.y,v 1.7 2003/09/28 07:45:55 otto Exp $ */ +/* $OpenBSD: bc.y,v 1.8 2003/09/28 07:57:57 otto Exp $ */ /* * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net> @@ -31,7 +31,7 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: bc.y,v 1.7 2003/09/28 07:45:55 otto Exp $"; +static const char rcsid[] = "$OpenBSD: bc.y,v 1.8 2003/09/28 07:57:57 otto Exp $"; #endif /* not lint */ #include <ctype.h> @@ -60,7 +60,6 @@ struct tree { int yyparse(void); int yywrap(void); -void yyerror(char *); static void grow(void); static ssize_t cs(const char *); @@ -72,7 +71,6 @@ static void free_tree(void); static ssize_t numnode(int); static void add_par(ssize_t); static void add_local(ssize_t); -static void fatal(const char *); static void warning(const char *); static void init(void); static __dead void usage(void); @@ -168,8 +166,9 @@ input_item : semicolon_list NEWLINE putchar('\n'); free_tree(); } - | error + | error NEWLINE { + yyerrok; } ; @@ -244,7 +243,7 @@ statement : expression | RETURN { if (nesting == 0) { - warnx("return must be in a function"); + warning("return must be in a function"); YYERROR; } $$ = node(cs("0"), epilogue, @@ -253,7 +252,7 @@ statement : expression | RETURN LPAR return_expression RPAR { if (nesting == 0) { - warnx("return must be in a function"); + warning("return must be in a function"); YYERROR; } $$ = $3; @@ -695,16 +694,17 @@ add_local(ssize_t n) int yywrap(void) { - lineno = 1; if (optind < sargc) { filename = sargv[optind++]; yyin = fopen(filename, "r"); + lineno = 1; if (yyin == NULL) err(1, "cannot open %s", filename); return 0; } else if (optind == sargc) { optind++; yyin = stdin; + lineno = 1; filename = "stdin"; return 0; } @@ -715,14 +715,14 @@ void yyerror(char *s) { if (isspace(*yytext) || !isprint(*yytext)) - printf("c[%s:%d: %s: ascii char 0x%x unexpected]pc\n", - filename, lineno, s, *yytext); + printf("c[%s: %s:%d: %s: ascii char 0x%x unexpected]pc\n", + __progname, filename, lineno, s, *yytext); else - printf("c[%s:%d: %s: %s unexpected]pc\n", - filename, lineno, s, yytext); + printf("c[%s: %s:%d: %s: %s unexpected]pc\n", + __progname, filename, lineno, s, yytext); } -static void +void fatal(const char *s) { errx(1, "%s:%d: %s", filename, lineno, s); diff --git a/usr.bin/bc/extern.h b/usr.bin/bc/extern.h index 85cae900c40..8d4b0417523 100644 --- a/usr.bin/bc/extern.h +++ b/usr.bin/bc/extern.h @@ -1,4 +1,4 @@ -/* $OpenBSD: extern.h,v 1.2 2003/09/26 07:02:52 deraadt Exp $ */ +/* $OpenBSD: extern.h,v 1.3 2003/09/28 07:57:57 otto Exp $ */ /* * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net> @@ -24,6 +24,8 @@ struct lvalue { }; int yylex(void); +void yyerror(char *); +void fatal(const char *); void abort_line(int); extern int lineno; diff --git a/usr.bin/bc/scan.l b/usr.bin/bc/scan.l index 9075a5eb3df..43245a80b2d 100644 --- a/usr.bin/bc/scan.l +++ b/usr.bin/bc/scan.l @@ -1,5 +1,5 @@ %{ -/* $OpenBSD: scan.l,v 1.4 2003/09/26 19:00:38 deraadt Exp $ */ +/* $OpenBSD: scan.l,v 1.5 2003/09/28 07:57:57 otto Exp $ */ /* * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net> @@ -18,7 +18,7 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: scan.l,v 1.4 2003/09/26 19:00:38 deraadt Exp $"; +static const char rcsid[] = "$OpenBSD: scan.l,v 1.5 2003/09/28 07:57:57 otto Exp $"; #endif /* not lint */ #include <err.h> @@ -50,6 +50,7 @@ DIGIT [0-9A-F] \n lineno++; \* ; [^*\n]+ ; + <<EOF>> fatal("end of file in comment"); } \" BEGIN(string); init_strbuf(); @@ -57,6 +58,7 @@ DIGIT [0-9A-F] [^"\n]+ add_str(yytext); \n add_str("\n"); lineno++; \" BEGIN(INITIAL); yylval.str = strbuf; return STRING; + <<EOF>> fatal("end of file in string"); } {DIGIT}+ { @@ -87,8 +89,7 @@ DIGIT [0-9A-F] \\\n[ \t]* lineno++; [^0-9A-F\.] { if (strcmp(strbuf, ".") == 0) { - warnx("unexpected ascii char 0x%x at line %d", - yytext[0], lineno); + yyerror("syntax error"); BEGIN(INITIAL); REJECT; } @@ -159,7 +160,7 @@ DIGIT [0-9A-F] \n lineno++; return NEWLINE; [ \t] ; -. warnx("unexpected character %c at line %d", yytext[0], lineno); +. yyerror("illegal character"); %% |