324e7a28274150b3de24b3ac6e108fd48e63b644
[ncurses.git] / ncurses / base / lib_freeall.c
1 /****************************************************************************
2  * Copyright (c) 1998 Free Software Foundation, Inc.                        *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Thomas E. Dickey <dickey@clark.net> 1996,1997                   *
31  ****************************************************************************/
32
33 #include <curses.priv.h>
34 #include <term_entry.h>
35
36 #if HAVE_NC_FREEALL
37
38 #if HAVE_LIBDBMALLOC
39 extern int malloc_errfd;        /* FIXME */
40 #endif
41
42 MODULE_ID("$Id: lib_freeall.c,v 1.15 1999/10/22 21:40:10 tom Exp $")
43
44 static void free_slk(SLK *p)
45 {
46         if (p != 0) {
47                 FreeIfNeeded(p->ent);
48                 FreeIfNeeded(p->buffer);
49                 free(p);
50         }
51 }
52
53 static void free_tries(struct tries *p)
54 {
55         struct tries *q;
56
57         while (p != 0) {
58                 q = p->sibling;
59                 if (p->child != 0)
60                         free_tries(p->child);
61                 free(p);
62                 p = q;
63         }
64 }
65
66 /*
67  * Free all ncurses data.  This is used for testing only (there's no practical
68  * use for it as an extension).
69  */
70 void _nc_freeall(void)
71 {
72         WINDOWLIST *p, *q;
73
74 #if NO_LEAKS
75         _nc_free_tparm();
76 #endif
77         while (_nc_windows != 0) {
78                 /* Delete only windows that're not a parent */
79                 for (p = _nc_windows; p != 0; p = p->next) {
80                         bool found = FALSE;
81
82                         for (q = _nc_windows; q != 0; q = q->next) {
83                                 if ((p != q)
84                                  && (q->win->_flags & _SUBWIN)
85                                  && (p->win == q->win->_parent)) {
86                                         found = TRUE;
87                                         break;
88                                 }
89                         }
90
91                         if (!found) {
92                                 delwin(p->win);
93                                 break;
94                         }
95                 }
96         }
97
98         if (SP != 0) {
99                 free_tries (SP->_keytry);
100                 free_tries (SP->_key_ok);
101                 free_slk(SP->_slk);
102                 FreeIfNeeded(SP->_color_pairs);
103                 FreeIfNeeded(SP->_color_table);
104 #if !BROKEN_LINKER
105                 FreeAndNull(SP);
106 #endif
107         }
108
109         if (cur_term != 0) {
110                 _nc_free_termtype(&(cur_term->type));
111                 free(cur_term);
112         }
113
114 #ifdef TRACE
115         (void) _nc_trace_buf(-1, 0);
116 #endif
117 #if HAVE_LIBDBMALLOC
118         malloc_dump(malloc_errfd);
119 #elif HAVE_LIBDMALLOC
120 #elif HAVE_PURIFY
121         purify_all_inuse();
122 #endif
123 }
124
125 void _nc_free_and_exit(int code)
126 {
127         _nc_freeall();
128         exit(code);
129 }
130 #else
131 void _nc_freeall(void) { }
132 #endif