1 /****************************************************************************
2  * Copyright 2019,2020 Thomas E. Dickey                                     *
3  * Copyright 2006-2012,2017 Free Software Foundation, Inc.                  *
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                                                *
32  *     and: Juergen Pfeifer                                                 *
33  ****************************************************************************/
34 
35 #include <curses.priv.h>
36 
37 #include <ctype.h>
38 
39 #include <tic.h>
40 
41 MODULE_ID("$Id: entries.c,v 1.30 2020/02/02 23:34:34 tom Exp $")
42 
43 /****************************************************************************
44  *
45  * Entry queue handling
46  *
47  ****************************************************************************/
48 /*
49  *  The entry list is a doubly linked list with NULLs terminating the lists:
50  *
51  *	  ---------   ---------   ---------
52  *	  |       |   |       |   |       |   offset
53  *        |-------|   |-------|   |-------|
54  *	  |   ----+-->|   ----+-->|  NULL |   next
55  *	  |-------|   |-------|   |-------|
56  *	  |  NULL |<--+----   |<--+----   |   last
57  *	  ---------   ---------   ---------
58  *	      ^                       ^
59  *	      |                       |
60  *	      |                       |
61  *	   _nc_head                _nc_tail
62  */
63 
64 NCURSES_EXPORT_VAR(ENTRY *) _nc_head = 0;
65 NCURSES_EXPORT_VAR(ENTRY *) _nc_tail = 0;
66 
67 static ENTRY *
68 _nc_delink_entry(ENTRY * headp, TERMTYPE2 *tterm)
69 /* delink the allocated storage for the given list entry */
70 {
71     ENTRY *ep, *last;
72 
73     for (last = 0, ep = headp; ep != 0; last = ep, ep = ep->next) {
74 	if (&(ep->tterm) == tterm) {
75 	    if (last != 0) {
76 		last->next = ep->next;
77 	    }
78 	    if (ep->next != 0) {
79 		ep->next->last = last;
80 	    }
81 	    if (ep == _nc_head) {
82 		_nc_head = ep->next;
83 	    }
84 	    if (ep == _nc_tail) {
85 		_nc_tail = last;
86 	    }
87 	    break;
88 	}
89     }
90     return ep;
91 }
92 
93 NCURSES_EXPORT(void)
94 _nc_free_entry(ENTRY * headp, TERMTYPE2 *tterm)
95 /* free the allocated storage consumed by the given list entry */
96 {
97     ENTRY *ep;
98 
99     if ((ep = _nc_delink_entry(headp, tterm)) != 0) {
100 	free(ep);
101     }
102 }
103 
104 NCURSES_EXPORT(void)
105 _nc_free_entries(ENTRY * headp)
106 /* free the allocated storage consumed by list entries */
107 {
108     (void) headp;		/* unused - _nc_head is altered here! */
109 
110     while (_nc_head != 0) {
111 	_nc_free_termtype2(&(_nc_head->tterm));
112     }
113 }
114 
115 NCURSES_EXPORT(void)
116 _nc_leaks_tinfo(void)
117 {
118 #if NO_LEAKS
119     char *s;
120 #endif
121 
122     T((T_CALLED("_nc_free_tinfo()")));
123 #if NO_LEAKS
124     _nc_globals.leak_checking = TRUE;
125     _nc_free_tparm();
126     _nc_tgetent_leaks();
127 
128     if (TerminalOf(CURRENT_SCREEN) != 0) {
129 	del_curterm(TerminalOf(CURRENT_SCREEN));
130     }
131     _nc_forget_prescr();
132 
133     _nc_comp_captab_leaks();
134     _nc_comp_userdefs_leaks();
135     _nc_free_entries(_nc_head);
136     _nc_get_type(0);
137     _nc_first_name(0);
138     _nc_db_iterator_leaks();
139     _nc_keyname_leaks();
140 #if BROKEN_LINKER || USE_REENTRANT
141     _nc_names_leaks();
142     _nc_codes_leaks();
143     FreeIfNeeded(_nc_prescreen.real_acs_map);
144 #endif
145     _nc_comp_error_leaks();
146 
147     if ((s = _nc_home_terminfo()) != 0)
148 	free(s);
149 
150 #ifdef TRACE
151     T((T_RETURN("")));
152     curses_trace(0);
153     _nc_trace_buf(-1, (size_t) 0);
154 #endif
155 
156 #endif /* NO_LEAKS */
157     returnVoid;
158 }
159 
160 #if NO_LEAKS
161 NCURSES_EXPORT(void)
162 _nc_free_tinfo(int code)
163 {
164     _nc_leaks_tinfo();
165     exit(code);
166 }
167 #endif
168 
169 NCURSES_EXPORT(void)
170 exit_terminfo(int code)
171 {
172 #if NO_LEAKS
173     _nc_leaks_tinfo();
174 #endif
175     exit(code);
176 }
177