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