lib_freeall.c revision 50276
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
39extern int malloc_errfd;	/* FIXME */
40#endif
41
42MODULE_ID("$Id: lib_freeall.c,v 1.14 1999/04/03 23:17:06 tom Exp $")
43
44static void free_slk(SLK *p)
45{
46	if (p != 0) {
47		FreeIfNeeded(p->ent);
48		FreeIfNeeded(p->buffer);
49		free(p);
50	}
51}
52
53static 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 */
70void _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		/* it won't free buffer anyway */
105/*		_nc_set_buffer(SP->_ofp, FALSE);*/
106#if !BROKEN_LINKER
107		FreeAndNull(SP);
108#endif
109	}
110
111	if (cur_term != 0) {
112		_nc_free_termtype(&(cur_term->type));
113		free(cur_term);
114	}
115
116#ifdef TRACE
117	(void) _nc_trace_buf(-1, 0);
118#endif
119#if HAVE_LIBDBMALLOC
120	malloc_dump(malloc_errfd);
121#elif HAVE_LIBDMALLOC
122#elif HAVE_PURIFY
123	purify_all_inuse();
124#endif
125}
126
127void _nc_free_and_exit(int code)
128{
129	_nc_freeall();
130	exit(code);
131}
132#else
133void _nc_freeall(void) { }
134#endif
135