mirror of
https://git.zap.org.au/git/trader.git
synced 2025-02-02 15:08:13 -05:00
Add the err_exit() and errno_exit() functions
This commit is contained in:
parent
162062cbfb
commit
6c90801102
@ -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 */
|
||||
|
@ -50,9 +50,11 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdarg.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <locale.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <getopt.h>
|
||||
|
||||
|
14
src/trader.c
14
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();
|
||||
|
73
src/utils.c
73
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);
|
||||
}
|
||||
|
@ -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 */
|
||||
|
Loading…
x
Reference in New Issue
Block a user