xref: /openbsd-src/lib/libcurses/base/lib_freeall.c (revision ed0e49f07449f40f6ae9cd77a8444e966ebf6510)
1 /*	$OpenBSD: lib_freeall.c,v 1.2 1999/05/08 20:29:00 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_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.14 1999/04/03 23:17:06 tom 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 static void free_tries(struct tries *p)
56 {
57 	struct tries *q;
58 
59 	while (p != 0) {
60 		q = p->sibling;
61 		if (p->child != 0)
62 			free_tries(p->child);
63 		free(p);
64 		p = q;
65 	}
66 }
67 
68 /*
69  * Free all ncurses data.  This is used for testing only (there's no practical
70  * use for it as an extension).
71  */
72 void _nc_freeall(void)
73 {
74 	WINDOWLIST *p, *q;
75 
76 #if NO_LEAKS
77 	_nc_free_tparm();
78 #endif
79 	while (_nc_windows != 0) {
80 		/* Delete only windows that're not a parent */
81 		for (p = _nc_windows; p != 0; p = p->next) {
82 			bool found = FALSE;
83 
84 			for (q = _nc_windows; q != 0; q = q->next) {
85 				if ((p != q)
86 				 && (q->win->_flags & _SUBWIN)
87 				 && (p->win == q->win->_parent)) {
88 					found = TRUE;
89 					break;
90 				}
91 			}
92 
93 			if (!found) {
94 				delwin(p->win);
95 				break;
96 			}
97 		}
98 	}
99 
100 	if (SP != 0) {
101 		free_tries (SP->_keytry);
102 		free_tries (SP->_key_ok);
103 	    	free_slk(SP->_slk);
104 		FreeIfNeeded(SP->_color_pairs);
105 		FreeIfNeeded(SP->_color_table);
106 		/* it won't free buffer anyway */
107 /*		_nc_set_buffer(SP->_ofp, FALSE);*/
108 #if !BROKEN_LINKER
109 		FreeAndNull(SP);
110 #endif
111 	}
112 
113 	if (cur_term != 0) {
114 		_nc_free_termtype(&(cur_term->type));
115 		free(cur_term);
116 	}
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 void _nc_free_and_exit(int code)
130 {
131 	_nc_freeall();
132 	exit(code);
133 }
134 #else
135 void _nc_freeall(void) { }
136 #endif
137