xref: /freebsd/contrib/ncurses/ncurses/tinfo/entries.c (revision 21817992b3314c908ab50f0bb88d2ee750b9c4ac)
1 /****************************************************************************
2  * Copyright 2019-2022,2023 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.35 2023/05/27 20:13:10 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 *
_nc_delink_entry(ENTRY * headp,const TERMTYPE2 * const tterm)68 _nc_delink_entry(ENTRY * headp, const TERMTYPE2 *const 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)
_nc_free_entry(ENTRY * headp,TERMTYPE2 * tterm)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)
_nc_free_entries(ENTRY * headp)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)
_nc_leaks_tinfo(void)116 _nc_leaks_tinfo(void)
117 {
118 #if NO_LEAKS
119     char *s;
120 #endif
121 
122     T((T_CALLED("_nc_leaks_tinfo()")));
123 #if NO_LEAKS
124     _nc_globals.leak_checking = TRUE;
125     _nc_free_tparm(cur_term);
126     _nc_tgetent_leaks();
127 
128 #ifdef USE_PTHREADS
129     /*
130      * Discard any prescreen data which is not used for the current screen.
131      */
132     _nc_lock_global(screen);
133     {
134 	PRESCREEN_LIST *p;
135 	pthread_t id = GetThreadID();
136 	for (p = _nc_prescreen.allocated; p != 0; p = p->next) {
137 	    if (p->id == id && p->sp != CURRENT_SCREEN) {
138 		FreeAndNull(p->sp);
139 	    }
140 	}
141     }
142     _nc_unlock_global(screen);
143 #endif
144     if (TerminalOf(CURRENT_SCREEN) != 0) {
145 	del_curterm(TerminalOf(CURRENT_SCREEN));
146     }
147     _nc_forget_prescr();
148 
149     _nc_comp_captab_leaks();
150     _nc_comp_userdefs_leaks();
151     _nc_free_entries(_nc_head);
152     _nc_get_type(0);
153     _nc_first_name(0);
154     _nc_db_iterator_leaks();
155     _nc_keyname_leaks();
156 #if BROKEN_LINKER || USE_REENTRANT
157     _nc_names_leaks();
158     _nc_codes_leaks();
159     FreeIfNeeded(_nc_prescreen.real_acs_map);
160 #endif
161     _nc_comp_error_leaks();
162 
163     if ((s = _nc_home_terminfo()) != 0)
164 	free(s);
165 
166 #ifdef TRACE
167     T((T_RETURN("")));
168     curses_trace(0);
169     _nc_trace_buf(-1, (size_t) 0);
170 #endif
171 
172 #endif /* NO_LEAKS */
173     returnVoid;
174 }
175 
176 #if NO_LEAKS
177 NCURSES_EXPORT(void)
_nc_free_tinfo(int code)178 _nc_free_tinfo(int code)
179 {
180     T((T_CALLED("_nc_free_tinfo(%d)"), code));
181     _nc_leaks_tinfo();
182     exit(code);
183 }
184 #endif
185 
186 NCURSES_EXPORT(void)
exit_terminfo(int code)187 exit_terminfo(int code)
188 {
189     T((T_CALLED("exit_terminfo(%d)"), code));
190 #if NO_LEAKS
191     _nc_leaks_tinfo();
192 #endif
193     exit(code);
194 }
195