From b81aca4948b18e65252f2629c5fdd446e8e6e818 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sun, 30 Mar 2008 00:58:40 -0700 Subject: [PATCH] Ensure that parse errors cause non-zero exit. Also, avoid creating output file when a parse error occurs --- assembler/src/main.c | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/assembler/src/main.c b/assembler/src/main.c index e2235340..1376ffa7 100644 --- a/assembler/src/main.c +++ b/assembler/src/main.c @@ -30,24 +30,30 @@ #include #include #include +#include #include "gen4asm.h" extern FILE *yyin; +extern int errors; + +char *input_filename = ""; + struct brw_program compiled_program; static const struct option longopts[] = { { NULL, 0, NULL, 0 } }; -void usage(void) +static void usage(void) { fprintf(stderr, "usage: intel-gen4asm [-o outputfile] inputfile\n"); } int main(int argc, char **argv) { + char *output_file = NULL; FILE *output = stdout; struct brw_program_instruction *entry; int err; @@ -56,13 +62,8 @@ int main(int argc, char **argv) while ((o = getopt_long(argc, argv, "o:", longopts, NULL)) != -1) { switch (o) { case 'o': - if (strcmp(optarg, "-") != 0) { - output = fopen(optarg, "w"); - if (output == NULL) { - perror("Couldn't open output file"); - exit(1); - } - } + if (strcmp(optarg, "-") != 0) + output_file = optarg; break; default: usage(); @@ -77,7 +78,8 @@ int main(int argc, char **argv) } if (strcmp(argv[0], "-") != 0) { - yyin = fopen(argv[0], "r"); + input_filename = argv[0]; + yyin = fopen(input_filename, "r"); if (yyin == NULL) { perror("Couldn't open input file"); exit(1); @@ -86,6 +88,16 @@ int main(int argc, char **argv) err = yyparse(); + if (err || errors) + exit (1); + + if (output_file) { + output = fopen(output_file, "w"); + if (output == NULL) { + perror("Couldn't open output file"); + exit(1); + } + } for (entry = compiled_program.first; entry != NULL; entry = entry->next) { @@ -96,5 +108,12 @@ int main(int argc, char **argv) ((int *)(&entry->instruction))[3]); } + fflush (output); + if (ferror (output)) { + perror ("Could not flush output file"); + if (output_file) + unlink (output_file); + err = 1; + } return err; }