ncurses 4.1
[ncurses.git] / ncurses / lib_freeall.c
1 /******************************************************************************
2  * Copyright 1996 by Thomas E. Dickey <dickey@clark.net>                      *
3  * All Rights Reserved.                                                       *
4  *                                                                            *
5  * Permission to use, copy, modify, and distribute this software and its      *
6  * documentation for any purpose and without fee is hereby granted, provided  *
7  * that the above copyright notice appear in all copies and that both that    *
8  * copyright notice and this permission notice appear in supporting           *
9  * documentation, and that the name of the above listed copyright holder(s)   *
10  * not be used in advertising or publicity pertaining to distribution of the  *
11  * software without specific, written prior permission. THE ABOVE LISTED      *
12  * COPYRIGHT HOLDER(S) DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,  *
13  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO     *
14  * EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY         *
15  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER       *
16  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF       *
17  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN        *
18  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.                   *
19  ******************************************************************************/
20
21 #define HAVE_NC_FREEALL 1
22
23 #include <curses.priv.h>
24 #include <term.h>
25
26 #if HAVE_LIBDBMALLOC
27 extern int malloc_errfd;        /* FIXME */
28 #endif
29
30 MODULE_ID("$Id: lib_freeall.c,v 1.8 1997/02/15 18:53:43 tom Exp $")
31
32 static void free_slk(SLK *p)
33 {
34         if (p != 0) {
35                 FreeIfNeeded(p->ent);
36                 FreeIfNeeded(p->buffer);
37                 free(p);
38         }
39 }
40
41 void _nc_free_termtype(struct termtype *p, int base)
42 {
43         if (p != 0) {
44                 FreeIfNeeded(p->term_names);
45                 FreeIfNeeded(p->str_table);
46                 if (base)
47                         free(p);
48         }
49 }
50
51 static void free_tries(struct tries *p)
52 {
53         struct tries *q;
54
55         while (p != 0) {
56                 q = p->sibling;
57                 if (p->child != 0)
58                         free_tries(p->child);
59                 free(p);
60                 p = q;
61         }
62 }
63
64 /*
65  * Free all ncurses data.  This is used for testing only (there's no practical
66  * use for it as an extension).
67  */
68 void _nc_freeall(void)
69 {
70         WINDOWLIST *p, *q;
71
72 #if NO_LEAKS
73         _nc_free_tparm();
74 #endif
75         while (_nc_windows != 0) {
76                 /* Delete only windows that're not a parent */
77                 for (p = _nc_windows; p != 0; p = p->next) {
78                         bool found = FALSE;
79
80                         for (q = _nc_windows; q != 0; q = q->next) {
81                                 if ((p != q)
82                                  && (q->win->_flags & _SUBWIN)
83                                  && (p->win == q->win->_parent)) {
84                                         found = TRUE;
85                                         break;
86                                 }
87                         }
88
89                         if (!found) {
90                                 delwin(p->win);
91                                 break;
92                         }
93                 }
94         }
95
96         if (SP != 0) {
97                 free_tries (SP->_keytry);
98                 free_slk(SP->_slk);
99                 FreeIfNeeded(SP->_color_pairs);
100                 FreeIfNeeded(SP->_color_table);
101                 _nc_set_buffer(SP->_ofp, FALSE);
102 #if !BROKEN_LINKER
103                 FreeAndNull(SP);
104 #endif
105         }
106
107         if (cur_term != 0) {
108                 _nc_free_termtype(&(cur_term->type), TRUE);
109         }
110
111 #if HAVE_LIBDBMALLOC
112         malloc_dump(malloc_errfd);
113 #elif HAVE_LIBDMALLOC
114 #elif HAVE_PURIFY
115         purify_all_inuse();
116 #endif
117 }
118
119 void _nc_free_and_exit(int code)
120 {
121         _nc_freeall();
122         exit(code);
123 }