Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1 | /**************************************************************************** |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 2 | * Copyright 2018-2020,2021 Thomas E. Dickey * |
| 3 | * Copyright 1998-2016,2017 Free Software Foundation, Inc. * |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 35 | #include <tic.h> |
| 36 | |
| 37 | #if HAVE_NC_FREEALL |
| 38 | |
| 39 | #if HAVE_LIBDBMALLOC |
| 40 | extern int malloc_errfd; /* FIXME */ |
| 41 | #endif |
| 42 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 43 | MODULE_ID("$Id: lib_freeall.c,v 1.76 2021/11/06 21:52:49 tom Exp $") |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 50 | NCURSES_SP_NAME(_nc_freeall) (NCURSES_SP_DCL0) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 51 | { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 52 | static va_list empty_va; |
| 53 | |
| 54 | T((T_CALLED("_nc_freeall()"))); |
| 55 | #if NO_LEAKS |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 56 | _nc_globals.leak_checking = TRUE; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 57 | if (SP_PARM != 0) { |
| 58 | if (SP_PARM->_oldnum_list != 0) { |
| 59 | FreeAndNull(SP_PARM->_oldnum_list); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 60 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 61 | if (SP_PARM->_panelHook.destroy != 0) { |
| 62 | SP_PARM->_panelHook.destroy(SP_PARM->_panelHook.stdscr_pseudo_panel); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 63 | } |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 64 | #if NCURSES_EXT_COLORS |
| 65 | _nc_new_pair_leaks(SP_PARM); |
| 66 | #endif |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 67 | } |
| 68 | #endif |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 69 | if (SP_PARM != 0) { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 70 | _nc_lock_global(curses); |
| 71 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 72 | while (WindowList(SP_PARM) != 0) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 73 | WINDOWLIST *p, *q; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 74 | bool deleted = FALSE; |
| 75 | |
| 76 | /* Delete only windows that're not a parent */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 77 | for (each_window(SP_PARM, p)) { |
| 78 | WINDOW *p_win = &(p->win); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 79 | bool found = FALSE; |
| 80 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 81 | if (IS_PAD(p_win)) |
| 82 | continue; |
| 83 | |
| 84 | #ifndef USE_SP_WINDOWLIST |
| 85 | if (p->screen != SP_PARM) |
| 86 | continue; |
| 87 | #endif |
| 88 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 89 | for (each_window(SP_PARM, q)) { |
| 90 | WINDOW *q_win = &(q->win); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 91 | |
| 92 | #ifndef USE_SP_WINDOWLIST |
| 93 | if (q->screen != SP_PARM) |
| 94 | continue; |
| 95 | #endif |
| 96 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 97 | if ((p != q) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 98 | && IS_SUBWIN(q_win) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 99 | && (p_win == q_win->_parent)) { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 100 | found = TRUE; |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if (!found) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 106 | if (delwin(p_win) != ERR) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 107 | deleted = TRUE; |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Don't continue to loop if the list is trashed. |
| 114 | */ |
| 115 | if (!deleted) |
| 116 | break; |
| 117 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 118 | delscreen(SP_PARM); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 119 | _nc_unlock_global(curses); |
| 120 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 121 | |
| 122 | (void) _nc_printf_string(0, empty_va); |
| 123 | #ifdef TRACE |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 124 | (void) _nc_trace_buf(-1, (size_t) 0); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 125 | #endif |
| 126 | #if USE_WIDEC_SUPPORT |
| 127 | FreeIfNeeded(_nc_wacs); |
| 128 | #endif |
| 129 | _nc_leaks_tinfo(); |
| 130 | |
| 131 | #if HAVE_LIBDBMALLOC |
| 132 | malloc_dump(malloc_errfd); |
| 133 | #elif HAVE_LIBDMALLOC |
| 134 | #elif HAVE_LIBMPATROL |
| 135 | __mp_summary(); |
| 136 | #elif HAVE_PURIFY |
| 137 | purify_all_inuse(); |
| 138 | #endif |
| 139 | returnVoid; |
| 140 | } |
| 141 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 142 | #if NCURSES_SP_FUNCS |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 143 | NCURSES_EXPORT(void) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 144 | _nc_freeall(void) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 145 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 146 | NCURSES_SP_NAME(_nc_freeall) (CURRENT_SCREEN); |
| 147 | } |
| 148 | #endif |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 149 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 150 | NCURSES_EXPORT(void) |
| 151 | NCURSES_SP_NAME(_nc_free_and_exit) (NCURSES_SP_DCLx int code) |
| 152 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 153 | T((T_CALLED("_nc_free_and_exit(%d)"), code)); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 154 | NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG); |
| 155 | NCURSES_SP_NAME(_nc_freeall) (NCURSES_SP_ARG); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 156 | #ifdef TRACE |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 157 | curses_trace(0); /* close trace file, freeing its setbuf */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 158 | { |
| 159 | static va_list fake; |
| 160 | free(_nc_varargs("?", fake)); |
| 161 | } |
| 162 | #endif |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 163 | exit(code); |
| 164 | } |
| 165 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 166 | #else /* !HAVE_NC_FREEALL */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 167 | NCURSES_EXPORT(void) |
| 168 | _nc_freeall(void) |
| 169 | { |
| 170 | } |
| 171 | |
| 172 | NCURSES_EXPORT(void) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 173 | NCURSES_SP_NAME(_nc_free_and_exit) (NCURSES_SP_DCLx int code) |
| 174 | { |
| 175 | if (SP_PARM) { |
| 176 | delscreen(SP_PARM); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 177 | } |
| 178 | exit(code); |
| 179 | } |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 180 | #endif /* HAVE_NC_FREEALL */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 181 | |
| 182 | #if NCURSES_SP_FUNCS |
| 183 | NCURSES_EXPORT(void) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 184 | _nc_free_and_exit(int code) |
| 185 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 186 | NCURSES_SP_NAME(_nc_free_and_exit) (CURRENT_SCREEN, code); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 187 | } |
| 188 | #endif |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 189 | |
| 190 | NCURSES_EXPORT(void) |
| 191 | exit_curses(int code) |
| 192 | { |
| 193 | #if NO_LEAKS |
| 194 | #if NCURSES_SP_FUNCS |
| 195 | NCURSES_SP_NAME(_nc_free_and_exit) (CURRENT_SCREEN, code); |
| 196 | #else |
| 197 | _nc_free_and_exit(code); /* deprecated... */ |
| 198 | #endif |
| 199 | #endif |
| 200 | exit(code); |
| 201 | } |