From 99bb0d15b22786b5421a7ab3fa239e717887ed32 Mon Sep 17 00:00:00 2001 From: Stephen Williams Date: Thu, 26 Nov 2020 16:11:59 -0800 Subject: [PATCH] Report error if command file is not properly terminated. Command file lines must all be terminated by EOL. If the last line is not properly terminated, report an error. --- driver/cflexor.lex | 51 +++++++++++++++++++++++++++++++++++++--------- driver/cfparse.y | 3 +++ driver/globals.h | 3 +++ driver/main.c | 2 +- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/driver/cflexor.lex b/driver/cflexor.lex index 1bf7cec1d..05ceb5dcc 100644 --- a/driver/cflexor.lex +++ b/driver/cflexor.lex @@ -45,6 +45,17 @@ typedef struct t_cmdfile { s_cmdfile cmdfile_stack[MAX_CMDFILE_DEPTH]; int cmdfile_stack_ptr = 0; +# define PROCESS_EOF \ + if (--cmdfile_stack_ptr < 0) { \ + free(current_file); \ + yyterminate(); \ + } else { \ + yy_delete_buffer(YY_CURRENT_BUFFER); \ + yy_switch_to_buffer(cmdfile_stack[cmdfile_stack_ptr].buffer); \ + free(current_file); \ + current_file = cmdfile_stack[cmdfile_stack_ptr].cmdfile; \ + } + %} %x CCOMMENT @@ -59,12 +70,26 @@ int cmdfile_stack_ptr = 0; . { yymore(); } \n { cflloc.first_line += 1; BEGIN(comment_enter); } +<> { + fprintf(stderr, "%s:%d: ERROR: Comment not terminated.\n"< + current_file, cflloc.first_line); + command_file_errors += 1; + BEGIN(0); + PROCESS_EOF } + /* Accept C style comments. */ "/*" { comment_enter = YY_START; BEGIN(CCOMMENT); } . { yymore(); } \n { cflloc.first_line += 1; yymore(); } "*/" { BEGIN(comment_enter); } +<> { + fprintf(stderr, "%s:%d: ERROR: Comment not terminated.\n"< + current_file, cflloc.first_line); + command_file_errors += 1; + BEGIN(0); + PROCESS_EOF } + /* Accept shell type comments. */ ^"#".* { ; } @@ -119,6 +144,13 @@ int cmdfile_stack_ptr = 0; cflloc.first_line += 1; BEGIN(0); } +<> { + BEGIN(0); + fprintf(stderr, "%s:%d: ERROR: Plusargs statement not terminated.\n", + current_file, cflloc.first_line); + command_file_errors += 1; + PROCESS_EOF } + /* Notice the -a flag. */ "-a" { return TOK_Da; } @@ -159,21 +191,19 @@ int cmdfile_stack_ptr = 0; cflval.text = trim_trailing_white(yytext, 0); BEGIN(0); return TOK_STRING; } +<> { + cflval.text = trim_trailing_white(yytext, 0); + BEGIN(0); + fprintf(stderr, "%s:%d: ERROR: File name not terminated.\n", + current_file, cflloc.first_line); + command_file_errors += 1; + PROCESS_EOF } /* Fallback match. */ . { return yytext[0]; } /* At the end of file switch back to the previous buffer. */ -<> { - if (--cmdfile_stack_ptr < 0) { - free(current_file); - yyterminate(); - } else { - yy_delete_buffer(YY_CURRENT_BUFFER); - yy_switch_to_buffer(cmdfile_stack[cmdfile_stack_ptr].buffer); - free(current_file); - current_file = cmdfile_stack[cmdfile_stack_ptr].cmdfile; - } } +<> { PROCESS_EOF } %% @@ -241,6 +271,7 @@ void switch_to_command_file(const char *file) void cfreset(FILE*fd, const char*path) { + command_file_errors = 0; yyin = fd; yyrestart(fd); current_file = strdup(path); diff --git a/driver/cfparse.y b/driver/cfparse.y index 4653ba560..7c6a6fdcc 100644 --- a/driver/cfparse.y +++ b/driver/cfparse.y @@ -268,8 +268,11 @@ skip_arg : TOK_PLUSARG %% +int command_file_errors = 0; + int yyerror(const char*msg) { (void)msg; /* Parameter is not used. */ + command_file_errors += 1; return 0; } diff --git a/driver/globals.h b/driver/globals.h index b71106f73..9823a0fcd 100644 --- a/driver/globals.h +++ b/driver/globals.h @@ -21,6 +21,9 @@ # include + /* Count errors in the command file. */ +extern int command_file_errors; + /* This is the integer-width argument that will be passed to ivl. */ extern unsigned integer_width; diff --git a/driver/main.c b/driver/main.c index 7a8d39c8f..ffb9cde62 100644 --- a/driver/main.c +++ b/driver/main.c @@ -1379,7 +1379,7 @@ int main(int argc, char **argv) cfreset(fp, command_filename); rc = cfparse(); - if (rc != 0) { + if (rc != 0 || command_file_errors > 0) { fprintf(stderr, "%s: parsing failed in base command " "file %s.\n", argv[0], command_filename); return 1;