home · contact · privacy
f79eac7582f25f0d5c7284a47198740b35015344
[plomrogue] / src / rexit.c
1 /* rexit.c */
2
3 #include "rexit.h"
4 #include <stdlib.h> /* for exit(), free(), defines EXIT_SUCESS, EXIT_FAILURE */
5 #include <stdio.h> /* for printf(), perror(), sprintf() */
6 #include <stdint.h> /* for uint8_t */
7 #include <string.h> /* for strlen() */
8 #include <errno.h> /* for errno */
9 #include "main.h" /* for world global */
10 #include "map.h" /* for Map struct ("free(world.map->cells)") */
11 #include "command_db.h" /* for free_command_db() */
12 #include "windows.h" /* for free_winmeta_and_endwin() */
13 #include "map_objects.h" /* for free_map_objects, free_map_object_defs() */
14 #include "misc.h" /* for unload_interface_conf() */
15 #include "map_object_actions.h" /* for free_map_object_actions() */
16
17
18
19 /* The clean-up routine and the flag resource by which it decides what to do. */
20 static uint32_t cleanup_flags = 0x0000;
21 static void cleanup();
22
23
24
25 static void cleanup()
26 {
27     if (cleanup_flags & CLEANUP_LOG)
28     {
29         free(world.log);
30     }
31     if (cleanup_flags & CLEANUP_COMMAND_DB)
32     {
33         free_command_db();
34     }
35     if (cleanup_flags & CLEANUP_MAP)
36     {
37         free(world.map->cells);
38     }
39     if (cleanup_flags & CLEANUP_MAP_OBJECTS)
40     {
41         free_map_objects(world.map_objs);
42     }
43     if (cleanup_flags & CLEANUP_MAP_OBJECT_DEFS)
44     {
45         free_map_object_defs(world.map_obj_defs);
46     }
47     if (cleanup_flags & CLEANUP_MAP_OBJECT_ACTS)
48     {
49         free_map_object_actions(world.map_obj_acts);
50     }
51     if (cleanup_flags & CLEANUP_INTERFACE) /* Only cleaning-up order          */
52     {                                      /* dependency known so far:        */
53         unload_interface_conf();           /* unload_interface_conf() must    */
54     }                                      /* come before                     */
55     if (cleanup_flags & CLEANUP_NCURSES)   /* free_winmeta_and_endwin()       */
56     {                                      /* since it depends on world.wmeta */
57         free_winmeta_and_endwin();         /* for closing all windows.        */
58     }
59 }
60
61
62
63 extern void set_cleanup_flag(enum cleanup_flag flag)
64 {
65     cleanup_flags = cleanup_flags | flag;
66 }
67
68
69
70 extern void exit_game()
71 {
72     cleanup();
73     exit(EXIT_SUCCESS);
74 }
75
76
77
78 extern void exit_err(uint8_t err, char * msg)
79 {
80     if (0 == err)
81     {
82         return;
83     }
84     cleanup();
85     if (NULL == msg)
86     {
87         msg = "Details unknown.";
88     }
89     printf("Aborted PlomRogue due to error. %s\nInternal error code: %d\n",
90            msg, err);
91     if (0 != errno)
92     {
93         perror("errno states");
94     }
95     exit(EXIT_FAILURE);
96 }
97
98
99
100 extern void exit_trouble(uint8_t err, char * parent, char * child)
101 {
102     char * p1 = "Trouble in ";
103     char * p2 = " with ";
104     char * p3 = ".";
105     uint16_t size = strlen(p1) + strlen(parent) + strlen(p2) + strlen(child)
106                     + strlen(p3) + 1;
107     char msg[size];
108     sprintf(msg, "%s%s%s%s%s", p1, parent, p2, child, p3);
109     exit_err(err, msg);
110 }