Add ncurses 5.4 source code.
[dragonfly.git] / contrib / ncurses-5.4 / ncurses / base / lib_freeall.c
1 /****************************************************************************
2  * Copyright (c) 1998-2002,2003 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 <dickey@clark.net> 1996,1997                   *
31  ****************************************************************************/
32
33 #include <curses.priv.h>
34 #include <term_entry.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.26 2003/12/27 18:21:57 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 _nc_freeall(void)
50 {
51     WINDOWLIST *p, *q;
52     char *s;
53
54     T((T_CALLED("_nc_freeall()")));
55 #if NO_LEAKS
56     _nc_free_tparm();
57     FreeAndNull(_nc_oldnums);
58 #endif
59     if (SP != 0) {
60         while (_nc_windows != 0) {
61             /* Delete only windows that're not a parent */
62             for (p = _nc_windows; p != 0; p = p->next) {
63                 bool found = FALSE;
64
65                 for (q = _nc_windows; q != 0; q = q->next) {
66                     if ((p != q)
67                         && (q->win._flags & _SUBWIN)
68                         && (&(p->win) == q->win._parent)) {
69                         found = TRUE;
70                         break;
71                     }
72                 }
73
74                 if (!found) {
75                     delwin(&(p->win));
76                     break;
77                 }
78             }
79         }
80         delscreen(SP);
81     }
82
83     del_curterm(cur_term);
84     _nc_free_entries(_nc_head);
85
86     if ((s = _nc_home_terminfo()) != 0)
87         free(s);
88
89     (void) _nc_printf_string(0, 0);
90 #ifdef TRACE
91     (void) _nc_trace_buf(-1, 0);
92 #endif
93
94 #if HAVE_LIBDBMALLOC
95     malloc_dump(malloc_errfd);
96 #elif HAVE_LIBDMALLOC
97 #elif HAVE_PURIFY
98     purify_all_inuse();
99 #endif
100     returnVoid;
101 }
102
103 NCURSES_EXPORT(void)
104 _nc_free_and_exit(int code)
105 {
106     char *last_setbuf = (SP != 0) ? SP->_setbuf : 0;
107
108     _nc_freeall();
109 #ifdef TRACE
110     trace(0);                   /* close trace file, freeing its setbuf */
111     free(_nc_varargs("?", 0));
112 #endif
113     fclose(stdout);
114     FreeIfNeeded(last_setbuf);
115     exit(code);
116 }
117
118 #else
119 NCURSES_EXPORT(void)
120 _nc_freeall(void)
121 {
122 }
123 #endif