changeset 272:1db299e868b8

UltraRogue: add the autosave feature. UltraRogue now automatically saves the game when SIGHUP or SIGTERM is received.
author John "Elwin" Edwards
date Sun, 03 Sep 2017 19:58:22 -0400
parents 88bd51f231e7
children 51ddbe7b992d
files urogue/main.c urogue/mdport.c urogue/rogue.h urogue/save.c
diffstat 4 files changed, 62 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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.
--- 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;
+}
--- 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) )
--- 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)
 {