summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2015-10-13 17:16:43 +0200
committerTobias Klauser <tklauser@distanz.ch>2015-10-13 17:22:08 +0200
commit895377c6e96ec8ac853568eb275043741a7621cd (patch)
tree91a6d5ce3b11ec555d427cdb6d784bfcd78e80e6
parentdaac1198c011aa122cf52004629dd77b4e99ae7d (diff)
trafgen: Don't panic() on parser errors
If the C preprocessor is used to parse the packet description, a temporary file is created which is not deleted if an error occurs during parsing in compile_packets(). Instead, don't panic() on errors and only print a message, and only die() once we cleaned up after us. Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
-rw-r--r--trafgen_conf.h2
-rw-r--r--trafgen_parser.y27
2 files changed, 19 insertions, 10 deletions
diff --git a/trafgen_conf.h b/trafgen_conf.h
index 3922d15..01d249d 100644
--- a/trafgen_conf.h
+++ b/trafgen_conf.h
@@ -45,7 +45,7 @@ struct packet_dyn {
size_t slen;
};
-extern int compile_packets(char *file, int verbose, int cpu, bool invoke_cpp);
+extern void compile_packets(char *file, int verbose, int cpu, bool invoke_cpp);
extern void cleanup_packets(void);
#endif /* TRAFGEN_CONF */
diff --git a/trafgen_parser.y b/trafgen_parser.y
index f934baa..126a819 100644
--- a/trafgen_parser.y
+++ b/trafgen_parser.y
@@ -592,9 +592,10 @@ void cleanup_packets(void)
free(packet_dyn);
}
-int compile_packets(char *file, int verbose, int cpu, bool invoke_cpp)
+void compile_packets(char *file, int verbose, int cpu, bool invoke_cpp)
{
char tmp_file[128];
+ int ret = -1;
memset(tmp_file, 0, sizeof(tmp_file));
our_cpu = cpu;
@@ -608,8 +609,10 @@ int compile_packets(char *file, int verbose, int cpu, bool invoke_cpp)
slprintf(tmp_file, sizeof(tmp_file), "%s/.tmp-%u-%s", dir, rand(), base);
slprintf(cmd, sizeof(cmd), "cpp -I" ETCDIRE_STRING " %s > %s",
file, tmp_file);
- if (system(cmd) != 0)
- panic("Failed to invoke C preprocessor!\n");
+ if (system(cmd) != 0) {
+ fprintf(stderr, "Failed to invoke C preprocessor!\n");
+ goto err;
+ }
file = tmp_file;
xfree(a);
@@ -620,24 +623,30 @@ int compile_packets(char *file, int verbose, int cpu, bool invoke_cpp)
yyin = stdin;
else
yyin = fopen(file, "r");
- if (!yyin)
- panic("Cannot open %s: %s!\n", file, strerror(errno));
+ if (!yyin) {
+ fprintf(stderr, "Cannot open %s: %s!\n", file, strerror(errno));
+ goto err;
+ }
realloc_packet();
- yyparse();
+ if (yyparse() != 0)
+ goto err;
finalize_packet();
if (our_cpu == 0 && verbose)
dump_conf();
+ ret = 0;
+err:
fclose(yyin);
+
if (invoke_cpp)
unlink(tmp_file);
-
- return 0;
+ if (ret)
+ die();
}
void yyerror(const char *err)
{
- panic("Syntax error at line %d, char '%s': %s\n", yylineno, yytext, err);
+ fprintf(stderr, "Syntax error at line %d, char '%s': %s\n", yylineno, yytext, err);
}