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