xref: /openbsd/lib/libcurses/base/lib_freeall.c (revision 73471bf0)
1 /* $OpenBSD: lib_freeall.c,v 1.5 2010/01/12 23:22:05 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright (c) 1998-2007,2008 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                    1996-on                     *
33  ****************************************************************************/
34 
35 #include <curses.priv.h>
36 #include <term_entry.h>
37 #include <tic.h>
38 
39 #if HAVE_NC_FREEALL
40 
41 #if HAVE_LIBDBMALLOC
42 extern int malloc_errfd;	/* FIXME */
43 #endif
44 
45 MODULE_ID("$Id: lib_freeall.c,v 1.5 2010/01/12 23:22:05 nicm Exp $")
46 
47 /*
48  * Free all ncurses data.  This is used for testing only (there's no practical
49  * use for it as an extension).
50  */
51 NCURSES_EXPORT(void)
52 _nc_freeall(void)
53 {
54     WINDOWLIST *p, *q;
55     static va_list empty_va;
56 
57     T((T_CALLED("_nc_freeall()")));
58 #if NO_LEAKS
59     if (SP != 0) {
60 	if (SP->_oldnum_list != 0) {
61 	    FreeAndNull(SP->_oldnum_list);
62 	}
63 	if (SP->_panelHook.destroy != 0) {
64 	    SP->_panelHook.destroy(SP->_panelHook.stdscr_pseudo_panel);
65 	}
66     }
67 #endif
68     if (SP != 0) {
69 	_nc_lock_global(curses);
70 
71 	while (_nc_windows != 0) {
72 	    bool deleted = FALSE;
73 
74 	    /* Delete only windows that're not a parent */
75 	    for (each_window(p)) {
76 		bool found = FALSE;
77 
78 		for (each_window(q)) {
79 		    if ((p != q)
80 			&& (q->win._flags & _SUBWIN)
81 			&& (&(p->win) == q->win._parent)) {
82 			found = TRUE;
83 			break;
84 		    }
85 		}
86 
87 		if (!found) {
88 		    if (delwin(&(p->win)) != ERR)
89 			deleted = TRUE;
90 		    break;
91 		}
92 	    }
93 
94 	    /*
95 	     * Don't continue to loop if the list is trashed.
96 	     */
97 	    if (!deleted)
98 		break;
99 	}
100 	delscreen(SP);
101 	_nc_unlock_global(curses);
102     }
103     if (cur_term != 0)
104 	del_curterm(cur_term);
105 
106     (void) _nc_printf_string(0, empty_va);
107 #ifdef TRACE
108     (void) _nc_trace_buf(-1, 0);
109 #endif
110 #if USE_WIDEC_SUPPORT
111     FreeIfNeeded(_nc_wacs);
112 #endif
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 
126 NCURSES_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     {
135 	static va_list fake;
136 	free(_nc_varargs("?", fake));
137     }
138 #endif
139     fclose(stdout);
140     FreeIfNeeded(last_setbuf);
141     exit(code);
142 }
143 
144 #else
145 NCURSES_EXPORT(void)
146 _nc_freeall(void)
147 {
148 }
149 
150 NCURSES_EXPORT(void)
151 _nc_free_and_exit(int code)
152 {
153     if (SP)
154 	delscreen(SP);
155     if (cur_term != 0)
156 	del_curterm(cur_term);
157     exit(code);
158 }
159 #endif
160