lib_freeall.c revision 174993
1169695Skan/****************************************************************************
2169695Skan * Copyright (c) 1998-2006,2007 Free Software Foundation, Inc.              *
3169695Skan *                                                                          *
4169695Skan * Permission is hereby granted, free of charge, to any person obtaining a  *
5169695Skan * copy of this software and associated documentation files (the            *
6169695Skan * "Software"), to deal in the Software without restriction, including      *
7169695Skan * without limitation the rights to use, copy, modify, merge, publish,      *
8169695Skan * distribute, distribute with modifications, sublicense, and/or sell       *
9169695Skan * copies of the Software, and to permit persons to whom the Software is    *
10169695Skan * furnished to do so, subject to the following conditions:                 *
11169695Skan *                                                                          *
12169695Skan * The above copyright notice and this permission notice shall be included  *
13169695Skan * in all copies or substantial portions of the Software.                   *
14169695Skan *                                                                          *
15169695Skan * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16169695Skan * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17169695Skan * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18169695Skan * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19169695Skan * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20169695Skan * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21169695Skan * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22169695Skan *                                                                          *
23169695Skan * Except as contained in this notice, the name(s) of the above copyright   *
24169695Skan * holders shall not be used in advertising or otherwise to promote the     *
25169695Skan * sale, use or other dealings in this Software without prior written       *
26169695Skan * authorization.                                                           *
27169695Skan ****************************************************************************/
28169695Skan
29169695Skan/****************************************************************************
30169695Skan *  Author: Thomas E. Dickey                    1996-on                     *
31169695Skan ****************************************************************************/
32169695Skan
33169695Skan#include <curses.priv.h>
34169695Skan#include <term_entry.h>
35169695Skan#include <tic.h>
36169695Skan
37169695Skan#if HAVE_NC_FREEALL
38169695Skan
39169695Skan#if HAVE_LIBDBMALLOC
40169695Skanextern int malloc_errfd;	/* FIXME */
41169695Skan#endif
42169695Skan
43169695SkanMODULE_ID("$Id: lib_freeall.c,v 1.45 2007/12/22 23:29:37 tom Exp $")
44169695Skan
45169695Skan/*
46169695Skan * Free all ncurses data.  This is used for testing only (there's no practical
47169695Skan * use for it as an extension).
48169695Skan */
49169695SkanNCURSES_EXPORT(void)
50169695Skan_nc_freeall(void)
51169695Skan{
52169695Skan    WINDOWLIST *p, *q;
53169695Skan    static va_list empty_va;
54169695Skan
55169695Skan    T((T_CALLED("_nc_freeall()")));
56169695Skan#if NO_LEAKS
57169695Skan    if (SP != 0) {
58169695Skan	if (SP->_oldnum_list != 0) {
59169695Skan	    FreeAndNull(SP->_oldnum_list);
60169695Skan	}
61    }
62#endif
63    if (SP != 0) {
64	_nc_lock_global(windowlist);
65
66	while (_nc_windows != 0) {
67	    bool deleted = FALSE;
68
69	    /* Delete only windows that're not a parent */
70	    for (p = _nc_windows; p != 0; p = p->next) {
71		bool found = FALSE;
72
73		for (q = _nc_windows; q != 0; q = q->next) {
74		    if ((p != q)
75			&& (q->win._flags & _SUBWIN)
76			&& (&(p->win) == q->win._parent)) {
77			found = TRUE;
78			break;
79		    }
80		}
81
82		if (!found) {
83		    if (delwin(&(p->win)) != ERR)
84			deleted = TRUE;
85		    break;
86		}
87	    }
88
89	    /*
90	     * Don't continue to loop if the list is trashed.
91	     */
92	    if (!deleted)
93		break;
94	}
95	delscreen(SP);
96	_nc_unlock_global(windowlist);
97    }
98    if (cur_term != 0)
99	del_curterm(cur_term);
100
101#if USE_WIDEC_SUPPORT
102    FreeIfNeeded(_nc_wacs);
103#endif
104    (void) _nc_printf_string(0, empty_va);
105#ifdef TRACE
106    (void) _nc_trace_buf(-1, 0);
107#endif
108
109#if BROKEN_LINKER || USE_REENTRANT
110    FreeIfNeeded(_nc_prescreen.real_acs_map);
111#endif
112
113    _nc_leaks_tinfo();
114
115#if HAVE_LIBDBMALLOC
116    malloc_dump(malloc_errfd);
117#elif HAVE_LIBDMALLOC
118#elif HAVE_LIBMPATROL
119    __mp_summary();
120#elif HAVE_PURIFY
121    purify_all_inuse();
122#endif
123    returnVoid;
124}
125
126NCURSES_EXPORT(void)
127_nc_free_and_exit(int code)
128{
129    char *last_setbuf = (SP != 0) ? SP->_setbuf : 0;
130
131    _nc_freeall();
132#ifdef TRACE
133    trace(0);			/* close trace file, freeing its setbuf */
134    free(_nc_varargs("?", 0));
135#endif
136    fclose(stdout);
137    FreeIfNeeded(last_setbuf);
138    exit(code);
139}
140
141#else
142NCURSES_EXPORT(void)
143_nc_freeall(void)
144{
145}
146#endif
147