1 /****************************************************************************
2  * Copyright 2018-2019,2020 Thomas E. Dickey                                *
3  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 
30 /****************************************************************************
31  *  Author: Thomas E. Dickey                    1996-on                     *
32  ****************************************************************************/
33 
34 #include <curses.priv.h>
35 #include <tic.h>
36 
37 #if HAVE_NC_FREEALL
38 
39 #if HAVE_LIBDBMALLOC
40 extern int malloc_errfd;	/* FIXME */
41 #endif
42 
43 MODULE_ID("$Id: lib_freeall.c,v 1.72 2020/02/02 23:34:34 tom Exp $")
44 
45 /*
46  * Free all ncurses data.  This is used for testing only (there's no practical
47  * use for it as an extension).
48  */
49 NCURSES_EXPORT(void)
50 NCURSES_SP_NAME(_nc_freeall) (NCURSES_SP_DCL0)
51 {
52     static va_list empty_va;
53 
54     T((T_CALLED("_nc_freeall()")));
55 #if NO_LEAKS
56     _nc_globals.leak_checking = TRUE;
57     if (SP_PARM != 0) {
58 	if (SP_PARM->_oldnum_list != 0) {
59 	    FreeAndNull(SP_PARM->_oldnum_list);
60 	}
61 	if (SP_PARM->_panelHook.destroy != 0) {
62 	    SP_PARM->_panelHook.destroy(SP_PARM->_panelHook.stdscr_pseudo_panel);
63 	}
64 #if NCURSES_EXT_COLORS
65 	_nc_new_pair_leaks(SP_PARM);
66 #endif
67     }
68 #endif
69     if (SP_PARM != 0) {
70 	_nc_lock_global(curses);
71 
72 	while (WindowList(SP_PARM) != 0) {
73 	    WINDOWLIST *p, *q;
74 	    bool deleted = FALSE;
75 
76 	    /* Delete only windows that're not a parent */
77 	    for (each_window(SP_PARM, p)) {
78 		WINDOW *p_win = &(p->win);
79 		bool found = FALSE;
80 
81 #ifndef USE_SP_WINDOWLIST
82 		if (p->screen != SP_PARM)
83 		    continue;
84 #endif
85 
86 		for (each_window(SP_PARM, q)) {
87 		    WINDOW *q_win = &(q->win);
88 
89 #ifndef USE_SP_WINDOWLIST
90 		    if (q->screen != SP_PARM)
91 			continue;
92 #endif
93 
94 		    if ((p != q)
95 			&& (q_win->_flags & _SUBWIN)
96 			&& (p_win == q_win->_parent)) {
97 			found = TRUE;
98 			break;
99 		    }
100 		}
101 
102 		if (!found) {
103 		    if (delwin(p_win) != ERR)
104 			deleted = TRUE;
105 		    break;
106 		}
107 	    }
108 
109 	    /*
110 	     * Don't continue to loop if the list is trashed.
111 	     */
112 	    if (!deleted)
113 		break;
114 	}
115 	delscreen(SP_PARM);
116 	_nc_unlock_global(curses);
117     }
118 
119     (void) _nc_printf_string(0, empty_va);
120 #ifdef TRACE
121     (void) _nc_trace_buf(-1, (size_t) 0);
122 #endif
123 #if USE_WIDEC_SUPPORT
124     FreeIfNeeded(_nc_wacs);
125 #endif
126     _nc_leaks_tinfo();
127 
128 #if HAVE_LIBDBMALLOC
129     malloc_dump(malloc_errfd);
130 #elif HAVE_LIBDMALLOC
131 #elif HAVE_LIBMPATROL
132     __mp_summary();
133 #elif HAVE_PURIFY
134     purify_all_inuse();
135 #endif
136     returnVoid;
137 }
138 
139 #if NCURSES_SP_FUNCS
140 NCURSES_EXPORT(void)
141 _nc_freeall(void)
142 {
143     NCURSES_SP_NAME(_nc_freeall) (CURRENT_SCREEN);
144 }
145 #endif
146 
147 NCURSES_EXPORT(void)
148 NCURSES_SP_NAME(_nc_free_and_exit) (NCURSES_SP_DCLx int code)
149 {
150     T((T_CALLED("_nc_free_and_exit(%d)"), code));
151     NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
152     NCURSES_SP_NAME(_nc_freeall) (NCURSES_SP_ARG);
153 #ifdef TRACE
154     curses_trace(0);		/* close trace file, freeing its setbuf */
155     {
156 	static va_list fake;
157 	free(_nc_varargs("?", fake));
158     }
159 #endif
160     exit(code);
161 }
162 
163 #else /* !HAVE_NC_FREEALL */
164 NCURSES_EXPORT(void)
165 _nc_freeall(void)
166 {
167 }
168 
169 NCURSES_EXPORT(void)
170 NCURSES_SP_NAME(_nc_free_and_exit) (NCURSES_SP_DCLx int code)
171 {
172     if (SP_PARM) {
173 	delscreen(SP_PARM);
174 	if (SP_PARM->_term)
175 	    NCURSES_SP_NAME(del_curterm) (NCURSES_SP_ARGx SP_PARM->_term);
176     }
177     exit(code);
178 }
179 #endif /* HAVE_NC_FREEALL */
180 
181 #if NCURSES_SP_FUNCS
182 NCURSES_EXPORT(void)
183 _nc_free_and_exit(int code)
184 {
185     NCURSES_SP_NAME(_nc_free_and_exit) (CURRENT_SCREEN, code);
186 }
187 #endif
188 
189 NCURSES_EXPORT(void)
190 exit_curses(int code)
191 {
192 #if NO_LEAKS
193 #if NCURSES_SP_FUNCS
194     NCURSES_SP_NAME(_nc_free_and_exit) (CURRENT_SCREEN, code);
195 #else
196     _nc_free_and_exit(code);	/* deprecated... */
197 #endif
198 #endif
199     exit(code);
200 }
201