]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_freeall.c
8a13b407f699c285510f18f7f147daa08f30f52c
[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.65 2017/03/04 00:12:23 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                 for (each_window(SP_PARM, q)) {
80                     WINDOW *q_win = &(q->win);
81                     if ((p != q)
82                         && (q_win->_flags & _SUBWIN)
83                         && (p_win == q_win->_parent)) {
84                         found = TRUE;
85                         break;
86                     }
87                 }
88
89                 if (!found) {
90                     if (delwin(p_win) != ERR)
91                         deleted = TRUE;
92                     break;
93                 }
94             }
95
96             /*
97              * Don't continue to loop if the list is trashed.
98              */
99             if (!deleted)
100                 break;
101         }
102         delscreen(SP_PARM);
103         _nc_unlock_global(curses);
104     }
105
106     (void) _nc_printf_string(0, empty_va);
107 #ifdef TRACE
108     (void) _nc_trace_buf(-1, (size_t) 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 #if NCURSES_SP_FUNCS
127 NCURSES_EXPORT(void)
128 _nc_freeall(void)
129 {
130     NCURSES_SP_NAME(_nc_freeall) (CURRENT_SCREEN);
131 }
132 #endif
133
134 NCURSES_EXPORT(void)
135 NCURSES_SP_NAME(_nc_free_and_exit) (NCURSES_SP_DCLx int code)
136 {
137     NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
138     NCURSES_SP_NAME(_nc_freeall) (NCURSES_SP_ARG);
139 #ifdef TRACE
140     trace(0);                   /* close trace file, freeing its setbuf */
141     {
142         static va_list fake;
143         free(_nc_varargs("?", fake));
144     }
145 #endif
146     exit(code);
147 }
148
149 #else
150 NCURSES_EXPORT(void)
151 _nc_freeall(void)
152 {
153 }
154
155 NCURSES_EXPORT(void)
156 NCURSES_SP_NAME(_nc_free_and_exit) (NCURSES_SP_DCLx int code)
157 {
158     if (SP_PARM) {
159         delscreen(SP_PARM);
160         if (SP_PARM->_term)
161             NCURSES_SP_NAME(del_curterm) (NCURSES_SP_ARGx SP_PARM->_term);
162     }
163     exit(code);
164 }
165 #endif
166
167 #if NCURSES_SP_FUNCS
168 NCURSES_EXPORT(void)
169 _nc_free_and_exit(int code)
170 {
171     NCURSES_SP_NAME(_nc_free_and_exit) (CURRENT_SCREEN, code);
172 }
173 #endif