]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/alloc_entry.c
0bc93942ca42a627e96467fc852b1273a708fd3a
[ncurses.git] / ncurses / tinfo / alloc_entry.c
1 /****************************************************************************
2  * Copyright 2018-2021,2022 Thomas E. Dickey                                *
3  * Copyright 1998-2013,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: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
32  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
33  *     and: Thomas E. Dickey                        1996-on                 *
34  ****************************************************************************/
35
36 /*
37  * alloc_entry.c -- allocation functions for terminfo entries
38  *
39  *      _nc_copy_entry()
40  *      _nc_init_entry()
41  *      _nc_merge_entry()
42  *      _nc_save_str()
43  *      _nc_wrap_entry()
44  *
45  */
46
47 #include <curses.priv.h>
48
49 #include <tic.h>
50
51 MODULE_ID("$Id: alloc_entry.c,v 1.68 2022/02/26 22:19:31 tom Exp $")
52
53 #define ABSENT_OFFSET    -1
54 #define CANCELLED_OFFSET -2
55
56 #define MAX_STRTAB      4096    /* documented maximum entry size */
57
58 static char *stringbuf;         /* buffer for string capabilities */
59 static size_t next_free;        /* next free character in stringbuf */
60
61 NCURSES_EXPORT(void)
62 _nc_init_entry(ENTRY * const tp)
63 /* initialize a terminal type data block */
64 {
65     if (tp == NULL) {
66 #if NO_LEAKS
67         if (stringbuf != NULL) {
68             FreeAndNull(stringbuf);
69         }
70         return;
71 #else
72         _nc_err_abort("_nc_init_entry called without initialization");
73 #endif
74     }
75
76     if (stringbuf == NULL)
77         TYPE_MALLOC(char, (size_t) MAX_STRTAB, stringbuf);
78
79     next_free = 0;
80
81     _nc_init_termtype(&(tp->tterm));
82 }
83
84 NCURSES_EXPORT(ENTRY *)
85 _nc_copy_entry(ENTRY * oldp)
86 {
87     ENTRY *newp = typeCalloc(ENTRY, 1);
88
89     if (newp != NULL) {
90         *newp = *oldp;
91         _nc_copy_termtype2(&(newp->tterm), &(oldp->tterm));
92     }
93     return newp;
94 }
95
96 /* save a copy of string in the string buffer */
97 NCURSES_EXPORT(char *)
98 _nc_save_str(const char *string)
99 {
100     char *result = 0;
101     size_t old_next_free = next_free;
102     size_t len;
103
104     if (stringbuf != NULL) {
105         if (!VALID_STRING(string))
106             string = "";
107         len = strlen(string) + 1;
108
109         if (len == 1 && next_free != 0) {
110             /*
111              * Cheat a little by making an empty string point to the end of the
112              * previous string.
113              */
114             if (next_free < MAX_STRTAB) {
115                 result = (stringbuf + next_free - 1);
116             }
117         } else if (next_free + len < MAX_STRTAB) {
118             _nc_STRCPY(&stringbuf[next_free], string, MAX_STRTAB);
119             DEBUG(7, ("Saved string %s", _nc_visbuf(string)));
120             DEBUG(7, ("at location %d", (int) next_free));
121             next_free += len;
122             result = (stringbuf + old_next_free);
123         } else {
124             _nc_warning("Too much data, some is lost: %s", string);
125         }
126     }
127     return result;
128 }
129
130 NCURSES_EXPORT(void)
131 _nc_wrap_entry(ENTRY * const ep, bool copy_strings)
132 /* copy the string parts to allocated storage, preserving pointers to it */
133 {
134     int offsets[MAX_ENTRY_SIZE / sizeof(short)];
135     int useoffsets[MAX_USES];
136     unsigned i, n;
137     unsigned nuses;
138     TERMTYPE2 *tp;
139
140     if (ep == NULL || stringbuf == NULL)
141         _nc_err_abort("_nc_wrap_entry called without initialization");
142
143     nuses = ep->nuses;
144     tp = &(ep->tterm);
145     if (copy_strings) {
146         next_free = 0;          /* clear static storage */
147
148         /* copy term_names, Strings, uses */
149         tp->term_names = _nc_save_str(tp->term_names);
150         for_each_string(i, tp) {
151             if (tp->Strings[i] != ABSENT_STRING &&
152                 tp->Strings[i] != CANCELLED_STRING) {
153                 tp->Strings[i] = _nc_save_str(tp->Strings[i]);
154             }
155         }
156
157         for (i = 0; i < nuses; i++) {
158             if (ep->uses[i].name == 0) {
159                 ep->uses[i].name = _nc_save_str(ep->uses[i].name);
160             }
161         }
162
163         free(tp->str_table);
164     }
165
166     assert(tp->term_names >= stringbuf);
167     n = (unsigned) (tp->term_names - stringbuf);
168     for_each_string(i, &(ep->tterm)) {
169         if (i < SIZEOF(offsets)) {
170             if (tp->Strings[i] == ABSENT_STRING) {
171                 offsets[i] = ABSENT_OFFSET;
172             } else if (tp->Strings[i] == CANCELLED_STRING) {
173                 offsets[i] = CANCELLED_OFFSET;
174             } else {
175                 offsets[i] = (int) (tp->Strings[i] - stringbuf);
176             }
177         }
178     }
179
180     for (i = 0; i < nuses; i++) {
181         if (ep->uses[i].name == 0)
182             useoffsets[i] = ABSENT_OFFSET;
183         else
184             useoffsets[i] = (int) (ep->uses[i].name - stringbuf);
185     }
186
187     TYPE_MALLOC(char, next_free, tp->str_table);
188     (void) memcpy(tp->str_table, stringbuf, next_free);
189
190     tp->term_names = tp->str_table + n;
191     for_each_string(i, &(ep->tterm)) {
192         if (i < SIZEOF(offsets)) {
193             if (offsets[i] == ABSENT_OFFSET) {
194                 tp->Strings[i] = ABSENT_STRING;
195             } else if (offsets[i] == CANCELLED_OFFSET) {
196                 tp->Strings[i] = CANCELLED_STRING;
197             } else {
198                 tp->Strings[i] = tp->str_table + offsets[i];
199             }
200         }
201     }
202
203 #if NCURSES_XNAMES
204     if (!copy_strings) {
205         if ((n = (unsigned) NUM_EXT_NAMES(tp)) != 0) {
206             if (n < SIZEOF(offsets)) {
207                 size_t length = 0;
208                 size_t offset;
209                 for (i = 0; i < n; i++) {
210                     length += strlen(tp->ext_Names[i]) + 1;
211                     offsets[i] = (int) (tp->ext_Names[i] - stringbuf);
212                 }
213                 TYPE_MALLOC(char, length, tp->ext_str_table);
214                 for (i = 0, offset = 0; i < n; i++) {
215                     tp->ext_Names[i] = tp->ext_str_table + offset;
216                     _nc_STRCPY(tp->ext_Names[i],
217                                stringbuf + offsets[i],
218                                length - offset);
219                     offset += strlen(tp->ext_Names[i]) + 1;
220                 }
221             }
222         }
223     }
224 #endif
225
226     for (i = 0; i < nuses; i++) {
227         if (useoffsets[i] == ABSENT_OFFSET)
228             ep->uses[i].name = 0;
229         else
230             ep->uses[i].name = (tp->str_table + useoffsets[i]);
231     }
232 }
233
234 NCURSES_EXPORT(void)
235 _nc_merge_entry(ENTRY * const target, ENTRY * const source)
236 /* merge capabilities from `from' entry into `to' entry */
237 {
238     TERMTYPE2 *to = &(target->tterm);
239     TERMTYPE2 *from = &(source->tterm);
240 #if NCURSES_XNAMES
241     TERMTYPE2 copy;
242 #endif
243     unsigned i;
244
245     if (source == 0 || from == 0 || target == 0 || to == 0)
246         return;
247
248 #if NCURSES_XNAMES
249     _nc_copy_termtype2(&copy, from);
250     from = &copy;
251     _nc_align_termtype(to, from);
252 #endif
253     for_each_boolean(i, from) {
254         if (to->Booleans[i] != (NCURSES_SBOOL) CANCELLED_BOOLEAN) {
255             int mergebool = from->Booleans[i];
256
257             if (mergebool == CANCELLED_BOOLEAN)
258                 to->Booleans[i] = FALSE;
259             else if (mergebool == TRUE)
260                 to->Booleans[i] = (NCURSES_SBOOL) mergebool;
261         }
262     }
263
264     for_each_number(i, from) {
265         if (to->Numbers[i] != CANCELLED_NUMERIC) {
266             int mergenum = from->Numbers[i];
267
268             if (mergenum == CANCELLED_NUMERIC)
269                 to->Numbers[i] = ABSENT_NUMERIC;
270             else if (mergenum != ABSENT_NUMERIC)
271                 to->Numbers[i] = (NCURSES_INT2) mergenum;
272         }
273     }
274
275     /*
276      * Note: the copies of strings this makes don't have their own
277      * storage.  This is OK right now, but will be a problem if we
278      * we ever want to deallocate entries.
279      */
280     for_each_string(i, from) {
281         if (to->Strings[i] != CANCELLED_STRING) {
282             char *mergestring = from->Strings[i];
283
284             if (mergestring == CANCELLED_STRING)
285                 to->Strings[i] = ABSENT_STRING;
286             else if (mergestring != ABSENT_STRING)
287                 to->Strings[i] = mergestring;
288         }
289     }
290 #if NCURSES_XNAMES
291     /* Discard the data allocated in _nc_copy_termtype2, but do not use
292      * _nc_free_termtype2 because that frees the string-table (which is
293      * not allocated by _nc_copy_termtype2).
294      */
295     free(copy.Booleans);
296     free(copy.Numbers);
297     free(copy.Strings);
298     free(copy.ext_Names);
299 #endif
300 }
301
302 #if NO_LEAKS
303 NCURSES_EXPORT(void)
304 _nc_alloc_entry_leaks(void)
305 {
306     if (stringbuf != NULL) {
307         FreeAndNull(stringbuf);
308     }
309     next_free = 0;
310 }
311 #endif