# HG changeset patch # User John "Elwin" Edwards # Date 1504483102 14400 # Node ID 1db299e868b8be0be03995f2e3acd271587b4669 # Parent 88bd51f231e7da379636b013103796fa5353ae85 UltraRogue: add the autosave feature. UltraRogue now automatically saves the game when SIGHUP or SIGTERM is received. diff -r 88bd51f231e7 -r 1db299e868b8 urogue/main.c --- a/urogue/main.c Sun Mar 05 20:14:11 2017 -0500 +++ b/urogue/main.c Sun Sep 03 19:58:22 2017 -0400 @@ -197,6 +197,8 @@ food_left = (int) (food_left * scale); + md_onsignal_autosave(); + /* Set up windows */ cw = newwin(LINES, COLS, 0, 0); @@ -272,6 +274,12 @@ exit(100); } +void +fatal_handler(int sig) +{ + fatal(""); +} + /* rnd() Pick a very random number. diff -r 88bd51f231e7 -r 1db299e868b8 urogue/mdport.c --- a/urogue/mdport.c Sun Mar 05 20:14:11 2017 -0500 +++ b/urogue/mdport.c Sun Sep 03 19:58:22 2017 -0400 @@ -1271,3 +1271,43 @@ return(ch & 0x7F); } + +extern void auto_save(int sig); +extern void fatal_handler(int sig); +extern void quit_handler(int sig); + +void +md_onsignal_autosave(void) +{ +#ifdef SIGHUP + signal(SIGHUP, auto_save); +#endif +#ifdef SIGQUIT + signal(SIGQUIT, fatal_handler); +#endif +#ifdef SIGTERM + signal(SIGTERM, auto_save); +#endif +#ifdef SIGINT + signal(SIGINT, quit_handler); +#endif + return; +} + +void +md_ignore_signals(void) +{ +#ifdef SIGHUP + signal(SIGHUP, SIG_IGN); +#endif +#ifdef SIGQUIT + signal(SIGQUIT, SIG_IGN); +#endif +#ifdef SIGTERM + signal(SIGTERM, SIG_IGN); +#endif +#ifdef SIGINT + signal(SIGINT, SIG_IGN); +#endif + return; +} diff -r 88bd51f231e7 -r 1db299e868b8 urogue/rogue.h --- a/urogue/rogue.h Sun Mar 05 20:14:11 2017 -0500 +++ b/urogue/rogue.h Sun Sep 03 19:58:22 2017 -0400 @@ -1813,6 +1813,8 @@ int md_readchar(WINDOW *win); int md_getuid(void); void md_normaluser(void); +void md_onsignal_autosave(void); +void md_ignore_signals(void); #define NOOP(x) (x += 0) #define CCHAR(x) ( (char) (x & A_CHARTEXT) ) diff -r 88bd51f231e7 -r 1db299e868b8 urogue/save.c --- a/urogue/save.c Sun Mar 05 20:14:11 2017 -0500 +++ b/urogue/save.c Sun Sep 03 19:58:22 2017 -0400 @@ -115,6 +115,18 @@ } } +void +auto_save(int sig) +{ + FILE *savef; + + md_ignore_signals(); + if (file_name[0] && (savef = fopen(file_name, "w"))) { + save_file(savef); + } + exit(0); +} + int restore(char *file) {