xref: /openbsd-src/lib/libcurses/base/lib_freeall.c (revision 92dd1ec0a89df25171bc5d61a3d95ea1a68cef0b)
1 /*	$OpenBSD: lib_freeall.c,v 1.1 1999/01/18 19:09:45 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998 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.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.13 1998/11/12 19:42:42 Alexander.V.Lukyanov Exp $")
45 
46 static void free_slk(SLK *p)
47 {
48 	if (p != 0) {
49 		FreeIfNeeded(p->ent);
50 		FreeIfNeeded(p->buffer);
51 		free(p);
52 	}
53 }
54 
55 void _nc_free_termtype(struct termtype *p, int base)
56 {
57 	if (p != 0) {
58 		FreeIfNeeded(p->term_names);
59 		FreeIfNeeded(p->str_table);
60 		if (base)
61 			free(p);
62 	}
63 }
64 
65 static void free_tries(struct tries *p)
66 {
67 	struct tries *q;
68 
69 	while (p != 0) {
70 		q = p->sibling;
71 		if (p->child != 0)
72 			free_tries(p->child);
73 		free(p);
74 		p = q;
75 	}
76 }
77 
78 /*
79  * Free all ncurses data.  This is used for testing only (there's no practical
80  * use for it as an extension).
81  */
82 void _nc_freeall(void)
83 {
84 	WINDOWLIST *p, *q;
85 
86 #if NO_LEAKS
87 	_nc_free_tparm();
88 #endif
89 	while (_nc_windows != 0) {
90 		/* Delete only windows that're not a parent */
91 		for (p = _nc_windows; p != 0; p = p->next) {
92 			bool found = FALSE;
93 
94 			for (q = _nc_windows; q != 0; q = q->next) {
95 				if ((p != q)
96 				 && (q->win->_flags & _SUBWIN)
97 				 && (p->win == q->win->_parent)) {
98 					found = TRUE;
99 					break;
100 				}
101 			}
102 
103 			if (!found) {
104 				delwin(p->win);
105 				break;
106 			}
107 		}
108 	}
109 
110 	if (SP != 0) {
111 		free_tries (SP->_keytry);
112 		free_tries (SP->_key_ok);
113 	    	free_slk(SP->_slk);
114 		FreeIfNeeded(SP->_color_pairs);
115 		FreeIfNeeded(SP->_color_table);
116 		/* it won't free buffer anyway */
117 /*		_nc_set_buffer(SP->_ofp, FALSE);*/
118 #if !BROKEN_LINKER
119 		FreeAndNull(SP);
120 #endif
121 	}
122 
123 	if (cur_term != 0) {
124 		_nc_free_termtype(&(cur_term->type), TRUE);
125 	}
126 
127 #ifdef TRACE
128 	(void) _nc_trace_buf(-1, 0);
129 #endif
130 #if HAVE_LIBDBMALLOC
131 	malloc_dump(malloc_errfd);
132 #elif HAVE_LIBDMALLOC
133 #elif HAVE_PURIFY
134 	purify_all_inuse();
135 #endif
136 }
137 
138 void _nc_free_and_exit(int code)
139 {
140 	_nc_freeall();
141 	exit(code);
142 }
143 #else
144 void _nc_freeall(void) { }
145 #endif
146