xref: /openbsd/lib/libcurses/tinfo/entries.c (revision c7ef0cfc)
1 /* $OpenBSD: entries.c,v 1.2 2023/10/17 09:52:09 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright 2019-2022,2023 Thomas E. Dickey                                *
5  * Copyright 2006-2012,2017 Free Software Foundation, Inc.                  *
6  *                                                                          *
7  * Permission is hereby granted, free of charge, to any person obtaining a  *
8  * copy of this software and associated documentation files (the            *
9  * "Software"), to deal in the Software without restriction, including      *
10  * without limitation the rights to use, copy, modify, merge, publish,      *
11  * distribute, distribute with modifications, sublicense, and/or sell       *
12  * copies of the Software, and to permit persons to whom the Software is    *
13  * furnished to do so, subject to the following conditions:                 *
14  *                                                                          *
15  * The above copyright notice and this permission notice shall be included  *
16  * in all copies or substantial portions of the Software.                   *
17  *                                                                          *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
21  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
24  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
25  *                                                                          *
26  * Except as contained in this notice, the name(s) of the above copyright   *
27  * holders shall not be used in advertising or otherwise to promote the     *
28  * sale, use or other dealings in this Software without prior written       *
29  * authorization.                                                           *
30  ****************************************************************************/
31 
32 /****************************************************************************
33  *  Author: Thomas E. Dickey                                                *
34  *     and: Juergen Pfeifer                                                 *
35  ****************************************************************************/
36 
37 #include <curses.priv.h>
38 
39 #include <ctype.h>
40 
41 #include <tic.h>
42 
43 MODULE_ID("$Id: entries.c,v 1.2 2023/10/17 09:52:09 nicm Exp $")
44 
45 /****************************************************************************
46  *
47  * Entry queue handling
48  *
49  ****************************************************************************/
50 /*
51  *  The entry list is a doubly linked list with NULLs terminating the lists:
52  *
53  *	  ---------   ---------   ---------
54  *	  |       |   |       |   |       |   offset
55  *        |-------|   |-------|   |-------|
56  *	  |   ----+-->|   ----+-->|  NULL |   next
57  *	  |-------|   |-------|   |-------|
58  *	  |  NULL |<--+----   |<--+----   |   last
59  *	  ---------   ---------   ---------
60  *	      ^                       ^
61  *	      |                       |
62  *	      |                       |
63  *	   _nc_head                _nc_tail
64  */
65 
66 NCURSES_EXPORT_VAR(ENTRY *) _nc_head = 0;
67 NCURSES_EXPORT_VAR(ENTRY *) _nc_tail = 0;
68 
69 static ENTRY *
_nc_delink_entry(ENTRY * headp,const TERMTYPE2 * const tterm)70 _nc_delink_entry(ENTRY * headp, const TERMTYPE2 *const tterm)
71 /* delink the allocated storage for the given list entry */
72 {
73     ENTRY *ep, *last;
74 
75     for (last = 0, ep = headp; ep != 0; last = ep, ep = ep->next) {
76 	if (&(ep->tterm) == tterm) {
77 	    if (last != 0) {
78 		last->next = ep->next;
79 	    }
80 	    if (ep->next != 0) {
81 		ep->next->last = last;
82 	    }
83 	    if (ep == _nc_head) {
84 		_nc_head = ep->next;
85 	    }
86 	    if (ep == _nc_tail) {
87 		_nc_tail = last;
88 	    }
89 	    break;
90 	}
91     }
92     return ep;
93 }
94 
95 NCURSES_EXPORT(void)
_nc_free_entry(ENTRY * headp,TERMTYPE2 * tterm)96 _nc_free_entry(ENTRY * headp, TERMTYPE2 *tterm)
97 /* free the allocated storage consumed by the given list entry */
98 {
99     ENTRY *ep;
100 
101     if ((ep = _nc_delink_entry(headp, tterm)) != 0) {
102 	free(ep);
103     }
104 }
105 
106 NCURSES_EXPORT(void)
_nc_free_entries(ENTRY * headp)107 _nc_free_entries(ENTRY * headp)
108 /* free the allocated storage consumed by list entries */
109 {
110     (void) headp;		/* unused - _nc_head is altered here! */
111 
112     while (_nc_head != 0) {
113 	_nc_free_termtype2(&(_nc_head->tterm));
114     }
115 }
116 
117 NCURSES_EXPORT(void)
_nc_leaks_tinfo(void)118 _nc_leaks_tinfo(void)
119 {
120 #if NO_LEAKS
121     char *s;
122 #endif
123 
124     T((T_CALLED("_nc_leaks_tinfo()")));
125 #if NO_LEAKS
126     _nc_globals.leak_checking = TRUE;
127     _nc_free_tparm(cur_term);
128     _nc_tgetent_leaks();
129 
130 #ifdef USE_PTHREADS
131     /*
132      * Discard any prescreen data which is not used for the current screen.
133      */
134     _nc_lock_global(screen);
135     {
136 	PRESCREEN_LIST *p;
137 	pthread_t id = GetThreadID();
138 	for (p = _nc_prescreen.allocated; p != 0; p = p->next) {
139 	    if (p->id == id && p->sp != CURRENT_SCREEN) {
140 		FreeAndNull(p->sp);
141 	    }
142 	}
143     }
144     _nc_unlock_global(screen);
145 #endif
146     if (TerminalOf(CURRENT_SCREEN) != 0) {
147 	del_curterm(TerminalOf(CURRENT_SCREEN));
148     }
149     _nc_forget_prescr();
150 
151     _nc_comp_captab_leaks();
152     _nc_comp_userdefs_leaks();
153     _nc_free_entries(_nc_head);
154     _nc_get_type(0);
155     _nc_first_name(0);
156     _nc_db_iterator_leaks();
157     _nc_keyname_leaks();
158 #if BROKEN_LINKER || USE_REENTRANT
159     _nc_names_leaks();
160     _nc_codes_leaks();
161     FreeIfNeeded(_nc_prescreen.real_acs_map);
162 #endif
163     _nc_comp_error_leaks();
164 
165     if ((s = _nc_home_terminfo()) != 0)
166 	free(s);
167 
168 #ifdef TRACE
169     T((T_RETURN("")));
170     curses_trace(0);
171     _nc_trace_buf(-1, (size_t) 0);
172 #endif
173 
174 #endif /* NO_LEAKS */
175     returnVoid;
176 }
177 
178 #if NO_LEAKS
179 NCURSES_EXPORT(void)
_nc_free_tinfo(int code)180 _nc_free_tinfo(int code)
181 {
182     T((T_CALLED("_nc_free_tinfo(%d)"), code));
183     _nc_leaks_tinfo();
184     exit(code);
185 }
186 #endif
187 
188 NCURSES_EXPORT(void)
exit_terminfo(int code)189 exit_terminfo(int code)
190 {
191     T((T_CALLED("exit_terminfo(%d)"), code));
192 #if NO_LEAKS
193     _nc_leaks_tinfo();
194 #endif
195     exit(code);
196 }
197