diff --git a/src/globals.h b/src/globals.h index a1f48cc..a97b785 100644 --- a/src/globals.h +++ b/src/globals.h @@ -141,4 +141,5 @@ extern int first_player; // Who WAS the first player to go? extern bool game_loaded; // True if game was loaded from disk extern char *game_filename; // Game file filename + #endif /* included_GLOBALS_H */ diff --git a/src/system.h b/src/system.h index f10a322..f1c7bb7 100644 --- a/src/system.h +++ b/src/system.h @@ -50,9 +50,11 @@ #include #include #include +#include #include #include #include +#include #include diff --git a/src/trader.c b/src/trader.c index 164941f..5d2f59a 100644 --- a/src/trader.c +++ b/src/trader.c @@ -52,8 +52,10 @@ int main (int argc, char *argv[]) { init_program_name(argv); - // Initialise the locale (and ignore any errors) - setlocale(LC_ALL, ""); + // Initialise the locale + if (setlocale(LC_ALL, "") == NULL) { + err_exit("could not set locale (check LANG, LC_ALL and LANGUAGE in environment)"); + } // Process command line arguments process_cmdline(argc, argv); @@ -114,12 +116,14 @@ int main (int argc, char *argv[]) } else { wprintw(w2, "0%05o ", c); } + + if (c == 0x1C) { + err_exit("You pressed ^%c!", c + '@'); + } + wrefresh(w2); } - delwin(w2); - delwin(w1); - clear(); refresh(); endwin(); diff --git a/src/utils.c b/src/utils.c index 3269c3c..79112bb 100644 --- a/src/utils.c +++ b/src/utils.c @@ -215,3 +215,76 @@ char *intto_game_filename (const int game_num) return p; } } + + +/*----------------------------------------------------------------------- + Function: err_exit - Print an error and exit + Arguments: format - printf()-like format of error message + ... - printf()-like arguments + Returns: (does not return) + + This function closes all curses windows, prints the name of the program + and the error message to stderr (using format and following arguments + as if passed to printf()) and exits with error code EXIT_FAILURE. The + format supplied does NOT need to supply the program name nor the + trailing end-line character. The format should not be NULL; user- + supplied strings should ALWAYS be printed using "%s" as the format (and + with the user string as a second argument), NOT passed in as the format + itself. +*/ + +void err_exit (const char *format, ...) +{ + va_list args; + + + clear(); + refresh(); + endwin(); + + fprintf(stderr, "%s: ", program_name()); + va_start(args, format); + vfprintf(stderr, format, args); + va_end(args); + fputs("\n", stderr); + + exit(EXIT_FAILURE); +} + + +/*----------------------------------------------------------------------- + Function: errno_exit - Print an error (using errno) and exit + Arguments: format - printf()-like format of error message + ... - printf()-like arguments + Returns: (does not return) + + This function closes all curses windows, prints the name of the + program, the error message (using format and following arguments as if + passed to printf()) and the string corresponding to errno to stderr, + then exits with error code EXIT_FAILURE. The format supplied does NOT + need to supply the program name, any colons nor the trailing end-line + character. The format may be NULL if no intermediate message is + needed. +*/ + +void errno_exit (const char *format, ...) +{ + va_list args; + int saved_errno = errno; + + + clear(); + refresh(); + endwin(); + + fprintf(stderr, "%s: ", program_name()); + if (format != NULL) { + va_start(args, format); + vfprintf(stderr, format, args); + va_end(args); + fputs(": ", stderr); + } + fprintf(stderr, "%s\n", strerror(saved_errno)); + + exit(EXIT_FAILURE); +} diff --git a/src/utils.h b/src/utils.h index b71e12a..cf462d5 100644 --- a/src/utils.h +++ b/src/utils.h @@ -53,5 +53,10 @@ extern const char *data_directory (void); extern char *strto_game_filename (const char *game_num); extern char *intto_game_filename (const int game_num); +extern void err_exit (const char *format, ...) + __attribute__((noreturn, format (printf, 1, 2))); +extern void errno_exit (const char *format, ...) + __attribute__((noreturn, format (printf, 1, 2))); + #endif /* included_UTILS_H */