]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_freeall.c
ncurses 6.0 - patch 20170722
[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.66 2017/03/25 22:59:38 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     if (SP_PARM != 0) {
56         if (SP_PARM->_oldnum_list != 0) {
57             FreeAndNull(SP_PARM->_oldnum_list);
58         }
59         if (SP_PARM->_panelHook.destroy != 0) {
60             SP_PARM->_panelHook.destroy(SP_PARM->_panelHook.stdscr_pseudo_panel);
61         }
62 #if USE_NEW_PAIR
63         _nc_new_pair_leaks(SP_PARM);
64 #endif
65     }
66 #endif
67     if (SP_PARM != 0) {
68         _nc_lock_global(curses);
69
70         while (WindowList(SP_PARM) != 0) {
71             WINDOWLIST *p, *q;
72             bool deleted = FALSE;
73
74             /* Delete only windows that're not a parent */
75             for (each_window(SP_PARM, p)) {
76                 WINDOW *p_win = &(p->win);
77                 bool found = FALSE;
78
79 #ifndef USE_SP_WINDOWLIST
80                 if (p->screen != SP_PARM)
81                     continue;
82 #endif
83
84                 for (each_window(SP_PARM, q)) {
85                     WINDOW *q_win = &(q->win);
86
87 #ifndef USE_SP_WINDOWLIST
88                     if (q->screen != SP_PARM)
89                         continue;
90 #endif
91
92                     if ((p != q)
93                         && (q_win->_flags & _SUBWIN)
94                         && (p_win == q_win->_parent)) {
95                         found = TRUE;
96                         break;
97                     }
98                 }
99
100                 if (!found) {
101                     if (delwin(p_win) != ERR)
102                         deleted = TRUE;
103                     break;
104                 }
105             }
106
107             /*
108              * Don't continue to loop if the list is trashed.
109              */
110             if (!deleted)
111                 break;
112         }
113         delscreen(SP_PARM);
114         _nc_unlock_global(curses);
115     }
116
117     (void) _nc_printf_string(0, empty_va);
118 #ifdef TRACE
119     (void) _nc_trace_buf(-1, (size_t) 0);
120 #endif
121 #if USE_WIDEC_SUPPORT
122     FreeIfNeeded(_nc_wacs);
123 #endif
124     _nc_leaks_tinfo();
125
126 #if HAVE_LIBDBMALLOC
127     malloc_dump(malloc_errfd);
128 #elif HAVE_LIBDMALLOC
129 #elif HAVE_LIBMPATROL
130     __mp_summary();
131 #elif HAVE_PURIFY
132     purify_all_inuse();
133 #endif
134     returnVoid;
135 }
136
137 #if NCURSES_SP_FUNCS
138 NCURSES_EXPORT(void)
139 _nc_freeall(void)
140 {
141     NCURSES_SP_NAME(_nc_freeall) (CURRENT_SCREEN);
142 }
143 #endif
144
145 NCURSES_EXPORT(void)
146 NCURSES_SP_NAME(_nc_free_and_exit) (NCURSES_SP_DCLx int code)
147 {
148     NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
149     NCURSES_SP_NAME(_nc_freeall) (NCURSES_SP_ARG);
150 #ifdef TRACE
151     trace(0);                   /* close trace file, freeing its setbuf */
152     {
153         static va_list fake;
154         free(_nc_varargs("?", fake));
155     }
156 #endif
157     exit(code);
158 }
159
160 #else
161 NCURSES_EXPORT(void)
162 _nc_freeall(void)
163 {
164 }
165
166 NCURSES_EXPORT(void)
167 NCURSES_SP_NAME(_nc_free_and_exit) (NCURSES_SP_DCLx int code)
168 {
169     if (SP_PARM) {
170         delscreen(SP_PARM);
171         if (SP_PARM->_term)
172             NCURSES_SP_NAME(del_curterm) (NCURSES_SP_ARGx SP_PARM->_term);
173     }
174     exit(code);
175 }
176 #endif
177
178 #if NCURSES_SP_FUNCS
179 NCURSES_EXPORT(void)
180 _nc_free_and_exit(int code)
181 {
182     NCURSES_SP_NAME(_nc_free_and_exit) (CURRENT_SCREEN, code);
183 }
184 #endif