1 /****************************************************************************
2 * Copyright 2018-2024,2025 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.83 2025/02/16 17:57:12 tom Exp $")
52
53 #define ABSENT_OFFSET -1
54 #define CANCELLED_OFFSET -2
55
56 static char *stringbuf; /* buffer for string capabilities */
57 static size_t next_free; /* next free character in stringbuf */
58
59 NCURSES_EXPORT(void)
_nc_init_entry(ENTRY * const tp)60 _nc_init_entry(ENTRY * const tp)
61 /* initialize a terminal type data block */
62 {
63 DEBUG(2, (T_CALLED("_nc_init_entry(tp=%p)"), (void *) tp));
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_CALLOC(char, (size_t) MAX_ENTRY_SIZE, stringbuf);
78
79 next_free = 0;
80
81 _nc_init_termtype(&(tp->tterm));
82
83 DEBUG(2, (T_RETURN("")));
84 }
85
86 NCURSES_EXPORT(ENTRY *)
_nc_copy_entry(ENTRY * oldp)87 _nc_copy_entry(ENTRY * oldp)
88 {
89 ENTRY *newp;
90
91 DEBUG(2, (T_CALLED("_nc_copy_entry(oldp=%p)"), (void *) oldp));
92
93 newp = typeCalloc(ENTRY, 1);
94 if (newp != NULL) {
95 *newp = *oldp;
96 _nc_copy_termtype2(&(newp->tterm), &(oldp->tterm));
97 }
98
99 DEBUG(2, (T_RETURN("%p"), (void *) newp));
100 return (newp);
101 }
102
103 /* save a copy of string in the string buffer */
104 NCURSES_EXPORT(char *)
_nc_save_str(const char * string)105 _nc_save_str(const char *string)
106 {
107 char *result = NULL;
108 size_t old_next_free = next_free;
109
110 if (stringbuf != NULL) {
111 size_t len;
112
113 if (!VALID_STRING(string))
114 string = "";
115 len = strlen(string) + 1;
116
117 if (len == 1 && next_free != 0) {
118 /*
119 * Cheat a little by making an empty string point to the end of the
120 * previous string.
121 */
122 if (next_free < MAX_ENTRY_SIZE) {
123 result = (stringbuf + next_free - 1);
124 }
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));
129 next_free += len;
130 result = (stringbuf + old_next_free);
131 } else {
132 _nc_warning("Too much data, some is lost: %s", string);
133 }
134 }
135 return result;
136 }
137
138 NCURSES_EXPORT(void)
_nc_wrap_entry(ENTRY * const ep,bool copy_strings)139 _nc_wrap_entry(ENTRY * const ep, bool copy_strings)
140 /* copy the string parts to allocated storage, preserving pointers to it */
141 {
142 int offsets[MAX_ENTRY_SIZE / sizeof(short)];
143 int useoffsets[HARD_MAX_USES];
144 unsigned i, n;
145 unsigned nuses;
146 TERMTYPE2 *tp;
147
148 DEBUG(2, (T_CALLED("_nc_wrap_entry(ep=%p, copy_strings=%d)"), (void *)
149 ep, copy_strings));
150 if (ep == NULL || stringbuf == NULL)
151 _nc_err_abort("_nc_wrap_entry called without initialization");
152
153 nuses = ep->nuses;
154 tp = &(ep->tterm);
155 if (copy_strings) {
156 next_free = 0; /* clear static storage */
157
158 /* copy term_names, Strings, uses */
159 tp->term_names = _nc_save_str(tp->term_names);
160 for_each_string(i, tp) {
161 if (VALID_STRING(tp->Strings[i])) {
162 tp->Strings[i] = _nc_save_str(tp->Strings[i]);
163 }
164 }
165
166 for (i = 0; i < nuses; i++) {
167 if (ep->uses[i].name == NULL) {
168 ep->uses[i].name = _nc_save_str(ep->uses[i].name);
169 }
170 }
171
172 free(tp->str_table);
173 }
174
175 assert(tp->term_names >= stringbuf);
176 n = (unsigned) (tp->term_names - stringbuf);
177 for_each_string(i, &(ep->tterm)) {
178 if (i < SIZEOF(offsets)) {
179 if (tp->Strings[i] == ABSENT_STRING) {
180 offsets[i] = ABSENT_OFFSET;
181 } else if (tp->Strings[i] == CANCELLED_STRING) {
182 offsets[i] = CANCELLED_OFFSET;
183 } else {
184 offsets[i] = (int) (tp->Strings[i] - stringbuf);
185 }
186 }
187 }
188
189 for (i = 0; i < nuses; i++) {
190 if (ep->uses[i].name == NULL)
191 useoffsets[i] = ABSENT_OFFSET;
192 else
193 useoffsets[i] = (int) (ep->uses[i].name - stringbuf);
194 }
195
196 TYPE_MALLOC(char, next_free, tp->str_table);
197 if (stringbuf == NULL)
198 (void) memset(tp->str_table, 0, next_free);
199 else
200 (void) memcpy(tp->str_table, stringbuf, next_free);
201
202 tp->term_names = tp->str_table + n;
203 for_each_string(i, &(ep->tterm)) {
204 if (i < SIZEOF(offsets)) {
205 if (offsets[i] == ABSENT_OFFSET) {
206 tp->Strings[i] = ABSENT_STRING;
207 } else if (offsets[i] == CANCELLED_OFFSET) {
208 tp->Strings[i] = CANCELLED_STRING;
209 } else {
210 tp->Strings[i] = tp->str_table + offsets[i];
211 }
212 }
213 }
214
215 #if NCURSES_XNAMES
216 if (!copy_strings) {
217 if ((n = (unsigned) NUM_EXT_NAMES(tp)) != 0) {
218 if (n < SIZEOF(offsets)) {
219 size_t length = 0;
220 size_t offset;
221 for (i = 0; i < n; i++) {
222 length += strlen(tp->ext_Names[i]) + 1;
223 offsets[i] = (int) (tp->ext_Names[i] - stringbuf);
224 }
225 TYPE_MALLOC(char, length, tp->ext_str_table);
226 for (i = 0, offset = 0; i < n; i++) {
227 tp->ext_Names[i] = tp->ext_str_table + offset;
228 _nc_STRCPY(tp->ext_Names[i],
229 stringbuf + offsets[i],
230 length - offset);
231 offset += strlen(tp->ext_Names[i]) + 1;
232 }
233 }
234 }
235 }
236 #endif
237
238 for (i = 0; i < nuses; i++) {
239 if (useoffsets[i] == ABSENT_OFFSET) {
240 ep->uses[i].name = NULL;
241 } else {
242 ep->uses[i].name = strdup(tp->str_table + useoffsets[i]);
243 }
244 }
245 DEBUG(2, (T_RETURN("")));
246 }
247
248 NCURSES_EXPORT(void)
_nc_merge_entry(ENTRY * const target,ENTRY * const source)249 _nc_merge_entry(ENTRY * const target, ENTRY * const source)
250 /* merge capabilities from `from' entry into `to' entry */
251 {
252 TERMTYPE2 *to = &(target->tterm);
253 TERMTYPE2 *from = &(source->tterm);
254 #if NCURSES_XNAMES
255 TERMTYPE2 copy;
256 size_t str_size, copy_size;
257 char *str_table;
258 #endif
259 unsigned i;
260
261 if (source == NULL || from == NULL || target == NULL || to == NULL)
262 return;
263
264 #if NCURSES_XNAMES
265 _nc_copy_termtype2(©, from);
266 from = ©
267 _nc_align_termtype(to, from);
268 /*
269 * compute the maximum size of the string-table.
270 */
271 str_size = strlen(to->term_names) + 1;
272 for_each_string(i, from) {
273 if (VALID_STRING(from->Strings[i]))
274 str_size += strlen(from->Strings[i]) + 1;
275 }
276 for_each_string(i, to) {
277 if (VALID_STRING(to->Strings[i]))
278 str_size += strlen(to->Strings[i]) + 1;
279 }
280 /* allocate a string-table large enough for both source/target, and
281 * copy all of the strings into that table. In the merge, we will
282 * select from the original source/target lists to construct a new
283 * target list.
284 */
285 if (str_size != 0) {
286 char *str_copied;
287 if ((str_table = malloc(str_size)) == NULL)
288 _nc_err_abort(MSG_NO_MEMORY);
289 str_copied = str_table;
290 _nc_STRCPY(str_copied, to->term_names, str_size);
291 to->term_names = str_copied;
292 copy_size = strlen(str_copied) + 1;
293 str_copied += copy_size;
294 str_size -= copy_size;
295 for_each_string(i, from) {
296 if (VALID_STRING(from->Strings[i])) {
297 _nc_STRCPY(str_copied, from->Strings[i], str_size);
298 from->Strings[i] = str_copied;
299 copy_size = strlen(str_copied) + 1;
300 str_copied += copy_size;
301 str_size -= copy_size;
302 }
303 }
304 for_each_string(i, to) {
305 if (VALID_STRING(to->Strings[i])) {
306 _nc_STRCPY(str_copied, to->Strings[i], str_size);
307 to->Strings[i] = str_copied;
308 copy_size = strlen(str_copied) + 1;
309 str_copied += copy_size;
310 str_size -= copy_size;
311 }
312 }
313 free(to->str_table);
314 to->str_table = str_table;
315 free(from->str_table);
316 }
317 /*
318 * Do the same for the extended-strings (i.e., lists of capabilities).
319 */
320 str_size = 0;
321 for (i = 0; i < NUM_EXT_NAMES(from); ++i) {
322 if (VALID_STRING(from->ext_Names[i]))
323 str_size += strlen(from->ext_Names[i]) + 1;
324 }
325 for (i = 0; i < NUM_EXT_NAMES(to); ++i) {
326 if (VALID_STRING(to->ext_Names[i]))
327 str_size += strlen(to->ext_Names[i]) + 1;
328 }
329 /* allocate a string-table large enough for both source/target, and
330 * copy all of the strings into that table. In the merge, we will
331 * select from the original source/target lists to construct a new
332 * target list.
333 */
334 if (str_size != 0) {
335 char *str_copied;
336 if ((str_table = malloc(str_size)) == NULL)
337 _nc_err_abort(MSG_NO_MEMORY);
338 str_copied = str_table;
339 for (i = 0; i < NUM_EXT_NAMES(from); ++i) {
340 if (VALID_STRING(from->ext_Names[i])) {
341 _nc_STRCPY(str_copied, from->ext_Names[i], str_size);
342 from->ext_Names[i] = str_copied;
343 copy_size = strlen(str_copied) + 1;
344 str_copied += copy_size;
345 str_size -= copy_size;
346 }
347 }
348 for (i = 0; i < NUM_EXT_NAMES(to); ++i) {
349 if (VALID_STRING(to->ext_Names[i])) {
350 _nc_STRCPY(str_copied, to->ext_Names[i], str_size);
351 to->ext_Names[i] = str_copied;
352 copy_size = strlen(str_copied) + 1;
353 str_copied += copy_size;
354 str_size -= copy_size;
355 }
356 }
357 free(to->ext_str_table);
358 to->ext_str_table = str_table;
359 free(from->ext_str_table);
360 }
361 #endif
362 for_each_boolean(i, from) {
363 if (i >= NUM_BOOLEANS(to))
364 break;
365 if (to->Booleans[i] != (NCURSES_SBOOL) CANCELLED_BOOLEAN) {
366 int mergebool = from->Booleans[i];
367
368 if (mergebool == CANCELLED_BOOLEAN)
369 to->Booleans[i] = FALSE;
370 else if (mergebool == TRUE)
371 to->Booleans[i] = (NCURSES_SBOOL) mergebool;
372 }
373 }
374
375 for_each_number(i, from) {
376 if (i >= NUM_NUMBERS(to))
377 break;
378 if (to->Numbers[i] != CANCELLED_NUMERIC) {
379 int mergenum = from->Numbers[i];
380
381 if (mergenum == CANCELLED_NUMERIC)
382 to->Numbers[i] = ABSENT_NUMERIC;
383 else if (mergenum != ABSENT_NUMERIC)
384 to->Numbers[i] = (NCURSES_INT2) mergenum;
385 }
386 }
387
388 /*
389 * Note: the copies of strings this makes don't have their own
390 * storage. This is OK right now, but will be a problem if we
391 * we ever want to deallocate entries.
392 */
393 for_each_string(i, from) {
394 if (i >= NUM_STRINGS(to))
395 break;
396 if (to->Strings[i] != CANCELLED_STRING) {
397 char *mergestring = from->Strings[i];
398
399 if (mergestring == CANCELLED_STRING)
400 to->Strings[i] = ABSENT_STRING;
401 else if (mergestring != ABSENT_STRING)
402 to->Strings[i] = mergestring;
403 }
404 }
405 #if NCURSES_XNAMES
406 /* cleanup */
407 free(copy.Booleans);
408 free(copy.Numbers);
409 free(copy.Strings);
410 free(copy.ext_Names);
411 #endif
412 }
413
414 #if NO_LEAKS
415 NCURSES_EXPORT(void)
_nc_alloc_entry_leaks(void)416 _nc_alloc_entry_leaks(void)
417 {
418 if (stringbuf != NULL) {
419 FreeAndNull(stringbuf);
420 }
421 next_free = 0;
422 }
423 #endif
424