150276Speter/****************************************************************************
2178866Srafan * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
350276Speter *                                                                          *
450276Speter * Permission is hereby granted, free of charge, to any person obtaining a  *
550276Speter * copy of this software and associated documentation files (the            *
650276Speter * "Software"), to deal in the Software without restriction, including      *
750276Speter * without limitation the rights to use, copy, modify, merge, publish,      *
850276Speter * distribute, distribute with modifications, sublicense, and/or sell       *
950276Speter * copies of the Software, and to permit persons to whom the Software is    *
1050276Speter * furnished to do so, subject to the following conditions:                 *
1150276Speter *                                                                          *
1250276Speter * The above copyright notice and this permission notice shall be included  *
1350276Speter * in all copies or substantial portions of the Software.                   *
1450276Speter *                                                                          *
1550276Speter * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1650276Speter * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
1750276Speter * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
1850276Speter * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
1950276Speter * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2050276Speter * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2150276Speter * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2250276Speter *                                                                          *
2350276Speter * Except as contained in this notice, the name(s) of the above copyright   *
2450276Speter * holders shall not be used in advertising or otherwise to promote the     *
2550276Speter * sale, use or other dealings in this Software without prior written       *
2650276Speter * authorization.                                                           *
2750276Speter ****************************************************************************/
2850276Speter
2950276Speter/****************************************************************************
30174993Srafan *  Author: Thomas E. Dickey                    1996-on                     *
3150276Speter ****************************************************************************/
3250276Speter
3350276Speter#include <curses.priv.h>
3450276Speter#include <term_entry.h>
35166124Srafan#include <tic.h>
3650276Speter
3750276Speter#if HAVE_NC_FREEALL
3850276Speter
3950276Speter#if HAVE_LIBDBMALLOC
4050276Speterextern int malloc_errfd;	/* FIXME */
4150276Speter#endif
4250276Speter
43184989SrafanMODULE_ID("$Id: lib_freeall.c,v 1.54 2008/09/27 13:09:57 tom Exp $")
4450276Speter
4550276Speter/*
4650276Speter * Free all ncurses data.  This is used for testing only (there's no practical
4750276Speter * use for it as an extension).
4850276Speter */
4976726SpeterNCURSES_EXPORT(void)
5062449Speter_nc_freeall(void)
5150276Speter{
5262449Speter    WINDOWLIST *p, *q;
53166124Srafan    static va_list empty_va;
5450276Speter
55166124Srafan    T((T_CALLED("_nc_freeall()")));
5650276Speter#if NO_LEAKS
57174993Srafan    if (SP != 0) {
58174993Srafan	if (SP->_oldnum_list != 0) {
59174993Srafan	    FreeAndNull(SP->_oldnum_list);
60174993Srafan	}
61184989Srafan	if (SP->_panelHook.destroy != 0) {
62184989Srafan	    SP->_panelHook.destroy(SP->_panelHook.stdscr_pseudo_panel);
63184989Srafan	}
64166124Srafan    }
6550276Speter#endif
6662449Speter    if (SP != 0) {
67184989Srafan	_nc_lock_global(curses);
68174993Srafan
6950276Speter	while (_nc_windows != 0) {
70174993Srafan	    bool deleted = FALSE;
71174993Srafan
7262449Speter	    /* Delete only windows that're not a parent */
73178866Srafan	    for (each_window(p)) {
7462449Speter		bool found = FALSE;
7550276Speter
76178866Srafan		for (each_window(q)) {
7762449Speter		    if ((p != q)
7897049Speter			&& (q->win._flags & _SUBWIN)
7997049Speter			&& (&(p->win) == q->win._parent)) {
8062449Speter			found = TRUE;
8162449Speter			break;
8262449Speter		    }
8362449Speter		}
8450276Speter
8562449Speter		if (!found) {
86174993Srafan		    if (delwin(&(p->win)) != ERR)
87174993Srafan			deleted = TRUE;
8862449Speter		    break;
8950276Speter		}
9062449Speter	    }
91174993Srafan
92174993Srafan	    /*
93174993Srafan	     * Don't continue to loop if the list is trashed.
94174993Srafan	     */
95174993Srafan	    if (!deleted)
96174993Srafan		break;
9750276Speter	}
98166124Srafan	delscreen(SP);
99184989Srafan	_nc_unlock_global(curses);
100166124Srafan    }
101174993Srafan    if (cur_term != 0)
102174993Srafan	del_curterm(cur_term);
103174993Srafan
104166124Srafan    (void) _nc_printf_string(0, empty_va);
10550276Speter#ifdef TRACE
10662449Speter    (void) _nc_trace_buf(-1, 0);
10750276Speter#endif
108184989Srafan#if USE_WIDEC_SUPPORT
109184989Srafan    FreeIfNeeded(_nc_wacs);
110174993Srafan#endif
111174993Srafan    _nc_leaks_tinfo();
112174993Srafan
11350276Speter#if HAVE_LIBDBMALLOC
11462449Speter    malloc_dump(malloc_errfd);
11550276Speter#elif HAVE_LIBDMALLOC
116166124Srafan#elif HAVE_LIBMPATROL
117166124Srafan    __mp_summary();
11850276Speter#elif HAVE_PURIFY
11962449Speter    purify_all_inuse();
12050276Speter#endif
121166124Srafan    returnVoid;
12250276Speter}
12350276Speter
12476726SpeterNCURSES_EXPORT(void)
12562449Speter_nc_free_and_exit(int code)
12650276Speter{
127166124Srafan    char *last_setbuf = (SP != 0) ? SP->_setbuf : 0;
128166124Srafan
12962449Speter    _nc_freeall();
130166124Srafan#ifdef TRACE
131166124Srafan    trace(0);			/* close trace file, freeing its setbuf */
132184989Srafan    {
133184989Srafan	static va_list fake;
134184989Srafan	free(_nc_varargs("?", fake));
135184989Srafan    }
136166124Srafan#endif
137166124Srafan    fclose(stdout);
138166124Srafan    FreeIfNeeded(last_setbuf);
13962449Speter    exit(code);
14050276Speter}
14162449Speter
14250276Speter#else
14376726SpeterNCURSES_EXPORT(void)
14462449Speter_nc_freeall(void)
14562449Speter{
14662449Speter}
147184989Srafan
148184989SrafanNCURSES_EXPORT(void)
149184989Srafan_nc_free_and_exit(int code)
150184989Srafan{
151184989Srafan    if (SP)
152184989Srafan	delscreen(SP);
153184989Srafan    if (cur_term != 0)
154184989Srafan	del_curterm(cur_term);
155184989Srafan    exit(code);
156184989Srafan}
15750276Speter#endif
158