]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/lib_freeall.c
ncurses 4.2
[ncurses.git] / ncurses / 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 #define HAVE_NC_FREEALL 1
34
35 #include <curses.priv.h>
36 #include <term.h>
37
38 #if HAVE_LIBDBMALLOC
39 extern int malloc_errfd;        /* FIXME */
40 #endif
41
42 MODULE_ID("$Id: lib_freeall.c,v 1.11 1998/02/11 12:13:57 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 void _nc_free_termtype(struct termtype *p, int base)
54 {
55         if (p != 0) {
56                 FreeIfNeeded(p->term_names);
57                 FreeIfNeeded(p->str_table);
58                 if (base)
59                         free(p);
60         }
61 }
62
63 static void free_tries(struct tries *p)
64 {
65         struct tries *q;
66
67         while (p != 0) {
68                 q = p->sibling;
69                 if (p->child != 0)
70                         free_tries(p->child);
71                 free(p);
72                 p = q;
73         }
74 }
75
76 /*
77  * Free all ncurses data.  This is used for testing only (there's no practical
78  * use for it as an extension).
79  */
80 void _nc_freeall(void)
81 {
82         WINDOWLIST *p, *q;
83
84 #if NO_LEAKS
85         _nc_free_tparm();
86 #endif
87         while (_nc_windows != 0) {
88                 /* Delete only windows that're not a parent */
89                 for (p = _nc_windows; p != 0; p = p->next) {
90                         bool found = FALSE;
91
92                         for (q = _nc_windows; q != 0; q = q->next) {
93                                 if ((p != q)
94                                  && (q->win->_flags & _SUBWIN)
95                                  && (p->win == q->win->_parent)) {
96                                         found = TRUE;
97                                         break;
98                                 }
99                         }
100
101                         if (!found) {
102                                 delwin(p->win);
103                                 break;
104                         }
105                 }
106         }
107
108         if (SP != 0) {
109                 free_tries (SP->_keytry);
110                 free_tries (SP->_key_ok);
111                 free_slk(SP->_slk);
112                 FreeIfNeeded(SP->_color_pairs);
113                 FreeIfNeeded(SP->_color_table);
114                 _nc_set_buffer(SP->_ofp, FALSE);
115 #if !BROKEN_LINKER
116                 FreeAndNull(SP);
117 #endif
118         }
119
120         if (cur_term != 0) {
121                 _nc_free_termtype(&(cur_term->type), TRUE);
122         }
123
124 #ifdef TRACE
125         (void) _nc_trace_buf(-1, 0);
126 #endif
127 #if HAVE_LIBDBMALLOC
128         malloc_dump(malloc_errfd);
129 #elif HAVE_LIBDMALLOC
130 #elif HAVE_PURIFY
131         purify_all_inuse();
132 #endif
133 }
134
135 void _nc_free_and_exit(int code)
136 {
137         _nc_freeall();
138         exit(code);
139 }