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