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