From e5f461914c369edfd84a1d4f00a02c106368ff2b Mon Sep 17 00:00:00 2001 From: anon Date: Wed, 15 Nov 2023 14:29:58 +0100 Subject: [PATCH] nicer exits --- source/globals.h | 5 +++++ source/main.cpp | 20 +++++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 source/globals.h diff --git a/source/globals.h b/source/globals.h new file mode 100644 index 0000000..70bda67 --- /dev/null +++ b/source/globals.h @@ -0,0 +1,5 @@ +const static int EXIT_EARLY_SUCCESS = 400; +enum { + UNKNOWN_OPTION = 1, + IO_ERROR = 2, +}; diff --git a/source/main.cpp b/source/main.cpp index 1ce8421..dd054e3 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -58,16 +58,18 @@ char * output_name_from_input_name(const char * const input, const char * const return r; } +static inline void try_fopen(FILE * &file, const char * const path, const char * const mode) { file = fopen(path, mode); if (!file) { fprintf(stderr, "Error opening file '%s'.\n", path); fflush(stderr); - exit(4); + exit(IO_ERROR); } } +static void yylex(FILE * &yyin, FILE * &yyout, int (*yylex_)(void)) { /* --- Preparation --- */ if (output) { @@ -91,13 +93,13 @@ void yylex(FILE * &yyin, FILE * &yyout, int (*yylex_)(void)) { } signed main(int argc, char * * argv) { - switch (parse_round1_arguments(argc - 1, argv + 1)) { - case 1: { - } return 0; - case 2: { - } return 1; - default: { - } break; + { + const int b = parse_round1_arguments(argc - 1, argv + 1); + switch (b) { + case 0: break; + case EXIT_EARLY_SUCCESS: exit(EXIT_SUCCESS); + default: exit(b); + } } for (int n = 1; n < argc; n++) { @@ -146,5 +148,5 @@ signed main(int argc, char * * argv) { } } - return 0; + return EXIT_SUCCESS; }