1 /****************************************************************************
2 * Copyright 2018-2021,2022 Thomas E. Dickey *
3 * Copyright 1998-2013,2017 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
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 ****************************************************************************/
37 * alloc_entry.c -- allocation functions for terminfo entries
47 #include <curses.priv.h>
51 MODULE_ID("$Id: alloc_entry.c,v 1.76 2022/09/17 21:45:44 tom Exp $")
53 #define ABSENT_OFFSET -1
54 #define CANCELLED_OFFSET -2
56 static char *stringbuf; /* buffer for string capabilities */
57 static size_t next_free; /* next free character in stringbuf */
60 _nc_init_entry(ENTRY * const tp)
61 /* initialize a terminal type data block */
63 DEBUG(2, (T_CALLED("_nc_init_entry(tp=%p)"), (void *) tp));
67 if (stringbuf != NULL) {
68 FreeAndNull(stringbuf);
72 _nc_err_abort("_nc_init_entry called without initialization");
76 if (stringbuf == NULL)
77 TYPE_CALLOC(char, (size_t) MAX_ENTRY_SIZE, stringbuf);
81 _nc_init_termtype(&(tp->tterm));
83 DEBUG(2, (T_RETURN("")));
86 NCURSES_EXPORT(ENTRY *)
87 _nc_copy_entry(ENTRY * oldp)
91 DEBUG(2, (T_CALLED("_nc_copy_entry(oldp=%p)"), (void *) oldp));
93 newp = typeCalloc(ENTRY, 1);
96 _nc_copy_termtype2(&(newp->tterm), &(oldp->tterm));
99 DEBUG(2, (T_RETURN("%p"), (void *) newp));
103 /* save a copy of string in the string buffer */
104 NCURSES_EXPORT(char *)
105 _nc_save_str(const char *string)
108 size_t old_next_free = next_free;
110 if (stringbuf != NULL) {
113 if (!VALID_STRING(string))
115 len = strlen(string) + 1;
117 if (len == 1 && next_free != 0) {
119 * Cheat a little by making an empty string point to the end of the
122 if (next_free < MAX_ENTRY_SIZE) {
123 result = (stringbuf + next_free - 1);
125 } else if (next_free + len < MAX_ENTRY_SIZE) {
126 _nc_STRCPY(&stringbuf[next_free], string, MAX_ENTRY_SIZE);
127 DEBUG(7, ("Saved string %s", _nc_visbuf(string)));
128 DEBUG(7, ("at location %d", (int) next_free));
130 result = (stringbuf + old_next_free);
132 _nc_warning("Too much data, some is lost: %s", string);
139 _nc_wrap_entry(ENTRY * const ep, bool copy_strings)
140 /* copy the string parts to allocated storage, preserving pointers to it */
142 int offsets[MAX_ENTRY_SIZE / sizeof(short)];
143 int useoffsets[MAX_USES];
148 DEBUG(2, (T_CALLED("_nc_wrap_entry(ep=%p, copy_strings=%d)"), (void *)
150 if (ep == NULL || stringbuf == NULL)
151 _nc_err_abort("_nc_wrap_entry called without initialization");
156 next_free = 0; /* clear static storage */
158 /* copy term_names, Strings, uses */
159 tp->term_names = _nc_save_str(tp->term_names);
160 for_each_string(i, tp) {
161 if (tp->Strings[i] != ABSENT_STRING &&
162 tp->Strings[i] != CANCELLED_STRING) {
163 tp->Strings[i] = _nc_save_str(tp->Strings[i]);
167 for (i = 0; i < nuses; i++) {
168 if (ep->uses[i].name == 0) {
169 ep->uses[i].name = _nc_save_str(ep->uses[i].name);
176 assert(tp->term_names >= stringbuf);
177 n = (unsigned) (tp->term_names - stringbuf);
178 for_each_string(i, &(ep->tterm)) {
179 if (i < SIZEOF(offsets)) {
180 if (tp->Strings[i] == ABSENT_STRING) {
181 offsets[i] = ABSENT_OFFSET;
182 } else if (tp->Strings[i] == CANCELLED_STRING) {
183 offsets[i] = CANCELLED_OFFSET;
185 offsets[i] = (int) (tp->Strings[i] - stringbuf);
190 for (i = 0; i < nuses; i++) {
191 if (ep->uses[i].name == 0)
192 useoffsets[i] = ABSENT_OFFSET;
194 useoffsets[i] = (int) (ep->uses[i].name - stringbuf);
197 TYPE_MALLOC(char, next_free, tp->str_table);
198 (void) memcpy(tp->str_table, stringbuf, next_free);
200 tp->term_names = tp->str_table + n;
201 for_each_string(i, &(ep->tterm)) {
202 if (i < SIZEOF(offsets)) {
203 if (offsets[i] == ABSENT_OFFSET) {
204 tp->Strings[i] = ABSENT_STRING;
205 } else if (offsets[i] == CANCELLED_OFFSET) {
206 tp->Strings[i] = CANCELLED_STRING;
208 tp->Strings[i] = tp->str_table + offsets[i];
215 if ((n = (unsigned) NUM_EXT_NAMES(tp)) != 0) {
216 if (n < SIZEOF(offsets)) {
219 for (i = 0; i < n; i++) {
220 length += strlen(tp->ext_Names[i]) + 1;
221 offsets[i] = (int) (tp->ext_Names[i] - stringbuf);
223 TYPE_MALLOC(char, length, tp->ext_str_table);
224 for (i = 0, offset = 0; i < n; i++) {
225 tp->ext_Names[i] = tp->ext_str_table + offset;
226 _nc_STRCPY(tp->ext_Names[i],
227 stringbuf + offsets[i],
229 offset += strlen(tp->ext_Names[i]) + 1;
236 for (i = 0; i < nuses; i++) {
237 if (useoffsets[i] == ABSENT_OFFSET) {
238 ep->uses[i].name = 0;
240 ep->uses[i].name = strdup(tp->str_table + useoffsets[i]);
243 DEBUG(2, (T_RETURN("")));
247 _nc_merge_entry(ENTRY * const target, ENTRY * const source)
248 /* merge capabilities from `from' entry into `to' entry */
250 TERMTYPE2 *to = &(target->tterm);
251 TERMTYPE2 *from = &(source->tterm);
255 char *str_table = NULL;
259 if (source == 0 || from == 0 || target == 0 || to == 0)
263 _nc_copy_termtype2(©, from);
265 _nc_align_termtype(to, from);
266 str_size += strlen(to->term_names) + 1;
267 for_each_string(i, from) {
268 if (VALID_STRING(from->Strings[i]))
269 str_size += strlen(from->Strings[i]) + 1;
271 for_each_string(i, to) {
272 if (VALID_STRING(to->Strings[i]))
273 str_size += strlen(to->Strings[i]) + 1;
275 /* allocate a string-table large enough for both source/target, and
276 * copy all of the strings into that table. In the merge, we will
277 * select from the original source/target lists to construct a new
282 if ((str_table = malloc(str_size)) == NULL)
283 _nc_err_abort(MSG_NO_MEMORY);
284 str_copied = str_table;
285 strcpy(str_copied, to->term_names);
286 to->term_names = str_copied;
287 str_copied += strlen(str_copied) + 1;
288 for_each_string(i, from) {
289 if (VALID_STRING(from->Strings[i])) {
290 strcpy(str_copied, from->Strings[i]);
291 from->Strings[i] = str_copied;
292 str_copied += strlen(str_copied) + 1;
295 for_each_string(i, to) {
296 if (VALID_STRING(to->Strings[i])) {
297 strcpy(str_copied, to->Strings[i]);
298 to->Strings[i] = str_copied;
299 str_copied += strlen(str_copied) + 1;
303 to->str_table = str_table;
304 free(from->str_table);
307 for_each_boolean(i, from) {
308 if (to->Booleans[i] != (NCURSES_SBOOL) CANCELLED_BOOLEAN) {
309 int mergebool = from->Booleans[i];
311 if (mergebool == CANCELLED_BOOLEAN)
312 to->Booleans[i] = FALSE;
313 else if (mergebool == TRUE)
314 to->Booleans[i] = (NCURSES_SBOOL) mergebool;
318 for_each_number(i, from) {
319 if (to->Numbers[i] != CANCELLED_NUMERIC) {
320 int mergenum = from->Numbers[i];
322 if (mergenum == CANCELLED_NUMERIC)
323 to->Numbers[i] = ABSENT_NUMERIC;
324 else if (mergenum != ABSENT_NUMERIC)
325 to->Numbers[i] = (NCURSES_INT2) mergenum;
330 * Note: the copies of strings this makes don't have their own
331 * storage. This is OK right now, but will be a problem if we
332 * we ever want to deallocate entries.
334 for_each_string(i, from) {
335 if (to->Strings[i] != CANCELLED_STRING) {
336 char *mergestring = from->Strings[i];
338 if (mergestring == CANCELLED_STRING)
339 to->Strings[i] = ABSENT_STRING;
340 else if (mergestring != ABSENT_STRING)
341 to->Strings[i] = mergestring;
345 /* Discard the data allocated in _nc_copy_termtype2, but do not use
346 * _nc_free_termtype2 because that frees the string-table (which is
347 * not allocated by _nc_copy_termtype2).
352 free(copy.ext_Names);
358 _nc_alloc_entry_leaks(void)
360 if (stringbuf != NULL) {
361 FreeAndNull(stringbuf);