]> git.mdlowis.com Git - projs/tide.git/commitdiff
Fixed cleanup handling
authorMichael D. Lowis <mike@mdlowis.com>
Fri, 1 Jan 2016 20:56:27 +0000 (15:56 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Fri, 1 Jan 2016 20:56:27 +0000 (15:56 -0500)
source/main.c
source/util.h

index bbfc224cc1ee6bb9cc2493bbab8ea63864fb3819..7d7070ba5722231fa4e01df0c3ad5f854417fcee 100644 (file)
@@ -1,5 +1,7 @@
 /* Includes
  *****************************************************************************/
+static void cleanup(void);
+#define CLEANUP_HOOK cleanup
 #include <util.h>
 #include <ncurses.h>
 
@@ -27,7 +29,6 @@ int Cursor_X = 0, Cursor_Y = 0;
 /* Declarations
  *****************************************************************************/
 static void setup(void);
-static void cleanup(void);
 static void load(char* fname);
 static void edit(void);
 
@@ -121,6 +122,7 @@ static void edit(void)
  *****************************************************************************/
 int main(int argc, char** argv)
 {
+    atexit(cleanup);
     setup();
     if (argc > 1) {
         load(argv[1]);
@@ -128,6 +130,5 @@ int main(int argc, char** argv)
     } else {
         die("no filename provided");
     }
-    cleanup();
     return EXIT_SUCCESS;
 }
index 49f81c94d925d9714b95b2b853108d9045d10afd..d32bcef94e57025a4e4c10c7c0179a515c23195a 100644 (file)
@@ -7,7 +7,7 @@
 #include <stdbool.h>
 #include <errno.h>
 
-/* Usefule Standard Functions */
+/* Useful Standard Functions */
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 /* Generic Death Function */
 static void die(const char* msgfmt, ...)
 {
-   va_list args;
-   va_start(args, msgfmt);
-   fprintf(stderr, "Error: ");
-   vfprintf(stderr, msgfmt, args);
-   fprintf(stderr, "\n");
-   va_end(args);
-   exit(EXIT_FAILURE);
+    va_list args;
+    va_start(args, msgfmt);
+    #ifdef CLEANUP_HOOK
+    CLEANUP_HOOK();
+    #endif
+    fprintf(stderr, "Error: ");
+    vfprintf(stderr, msgfmt, args);
+    fprintf(stderr, "\n");
+    va_end(args);
+    exit(EXIT_FAILURE);
 }
 
 /* Signal Handling */
@@ -88,7 +91,7 @@ static char* efreadline(FILE* input)
         free(str);
         return NULL;
     }
-    while(true) {
+    while (true) {
         char ch = fgetc(input);
         if (ch == EOF) break;
         str[index++] = ch;