1 /* $OpenBSD: alloc_entry.c,v 1.8 2023/10/17 09:52:09 nicm Exp $ */
2
3 /****************************************************************************
4 * Copyright 2018-2021,2022 Thomas E. Dickey *
5 * Copyright 1998-2013,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: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
34 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
35 * and: Thomas E. Dickey 1996-on *
36 ****************************************************************************/
37
38 /*
39 * alloc_entry.c -- allocation functions for terminfo entries
40 *
41 * _nc_copy_entry()
42 * _nc_init_entry()
43 * _nc_merge_entry()
44 * _nc_save_str()
45 * _nc_wrap_entry()
46 *
47 */
48
49 #include <curses.priv.h>
50
51 #include <tic.h>
52
53 MODULE_ID("$Id: alloc_entry.c,v 1.8 2023/10/17 09:52:09 nicm Exp $")
54
55 #define ABSENT_OFFSET -1
56 #define CANCELLED_OFFSET -2
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)
_nc_init_entry(ENTRY * const tp)62 _nc_init_entry(ENTRY * const tp)
63 /* initialize a terminal type data block */
64 {
65 DEBUG(2, (T_CALLED("_nc_init_entry(tp=%p)"), (void *) tp));
66
67 if (tp == NULL) {
68 #if NO_LEAKS
69 if (stringbuf != NULL) {
70 FreeAndNull(stringbuf);
71 }
72 return;
73 #else
74 _nc_err_abort("_nc_init_entry called without initialization");
75 #endif
76 }
77
78 if (stringbuf == NULL)
79 TYPE_CALLOC(char, (size_t) MAX_ENTRY_SIZE, stringbuf);
80
81 next_free = 0;
82
83 _nc_init_termtype(&(tp->tterm));
84
85 DEBUG(2, (T_RETURN("")));
86 }
87
88 NCURSES_EXPORT(ENTRY *)
_nc_copy_entry(ENTRY * oldp)89 _nc_copy_entry(ENTRY * oldp)
90 {
91 ENTRY *newp;
92
93 DEBUG(2, (T_CALLED("_nc_copy_entry(oldp=%p)"), (void *) oldp));
94
95 newp = typeCalloc(ENTRY, 1);
96 if (newp != NULL) {
97 *newp = *oldp;
98 _nc_copy_termtype2(&(newp->tterm), &(oldp->tterm));
99 }
100
101 DEBUG(2, (T_RETURN("%p"), (void *) newp));
102 return (newp);
103 }
104
105 /* save a copy of string in the string buffer */
106 NCURSES_EXPORT(char *)
_nc_save_str(const char * string)107 _nc_save_str(const char *string)
108 {
109 char *result = 0;
110 size_t old_next_free = next_free;
111
112 if (stringbuf != NULL) {
113 size_t len;
114
115 if (!VALID_STRING(string))
116 string = "";
117 len = strlen(string) + 1;
118
119 if (len == 1 && next_free != 0) {
120 /*
121 * Cheat a little by making an empty string point to the end of the
122 * previous string.
123 */
124 if (next_free < MAX_ENTRY_SIZE) {
125 result = (stringbuf + next_free - 1);
126 }
127 } else if (next_free + len < MAX_ENTRY_SIZE) {
128 _nc_STRCPY(&stringbuf[next_free], string, MAX_ENTRY_SIZE);
129 DEBUG(7, ("Saved string %s", _nc_visbuf(string)));
130 DEBUG(7, ("at location %d", (int) next_free));
131 next_free += len;
132 result = (stringbuf + old_next_free);
133 } else {
134 _nc_warning("Too much data, some is lost: %s", string);
135 }
136 }
137 return result;
138 }
139
140 NCURSES_EXPORT(void)
_nc_wrap_entry(ENTRY * const ep,bool copy_strings)141 _nc_wrap_entry(ENTRY * const ep, bool copy_strings)
142 /* copy the string parts to allocated storage, preserving pointers to it */
143 {
144 int offsets[MAX_ENTRY_SIZE / sizeof(short)];
145 int useoffsets[MAX_USES];
146 unsigned i, n;
147 unsigned nuses;
148 TERMTYPE2 *tp;
149
150 DEBUG(2, (T_CALLED("_nc_wrap_entry(ep=%p, copy_strings=%d)"), (void *)
151 ep, copy_strings));
152 if (ep == NULL || stringbuf == NULL)
153 _nc_err_abort("_nc_wrap_entry called without initialization");
154
155 nuses = ep->nuses;
156 tp = &(ep->tterm);
157 if (copy_strings) {
158 next_free = 0; /* clear static storage */
159
160 /* copy term_names, Strings, uses */
161 tp->term_names = _nc_save_str(tp->term_names);
162 for_each_string(i, tp) {
163 if (tp->Strings[i] != ABSENT_STRING &&
164 tp->Strings[i] != CANCELLED_STRING) {
165 tp->Strings[i] = _nc_save_str(tp->Strings[i]);
166 }
167 }
168
169 for (i = 0; i < nuses; i++) {
170 if (ep->uses[i].name == 0) {
171 ep->uses[i].name = _nc_save_str(ep->uses[i].name);
172 }
173 }
174
175 free(tp->str_table);
176 }
177
178 assert(tp->term_names >= stringbuf);
179 n = (unsigned) (tp->term_names - stringbuf);
180 for_each_string(i, &(ep->tterm)) {
181 if (i < SIZEOF(offsets)) {
182 if (tp->Strings[i] == ABSENT_STRING) {
183 offsets[i] = ABSENT_OFFSET;
184 } else if (tp->Strings[i] == CANCELLED_STRING) {
185 offsets[i] = CANCELLED_OFFSET;
186 } else {
187 offsets[i] = (int) (tp->Strings[i] - stringbuf);
188 }
189 }
190 }
191
192 for (i = 0; i < nuses; i++) {
193 if (ep->uses[i].name == 0)
194 useoffsets[i] = ABSENT_OFFSET;
195 else
196 useoffsets[i] = (int) (ep->uses[i].name - stringbuf);
197 }
198
199 TYPE_MALLOC(char, next_free, tp->str_table);
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 = 0;
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 == 0 || from == 0 || target == 0 || to == 0)
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 (to->Booleans[i] != (NCURSES_SBOOL) CANCELLED_BOOLEAN) {
364 int mergebool = from->Booleans[i];
365
366 if (mergebool == CANCELLED_BOOLEAN)
367 to->Booleans[i] = FALSE;
368 else if (mergebool == TRUE)
369 to->Booleans[i] = (NCURSES_SBOOL) mergebool;
370 }
371 }
372
373 for_each_number(i, from) {
374 if (to->Numbers[i] != CANCELLED_NUMERIC) {
375 int mergenum = from->Numbers[i];
376
377 if (mergenum == CANCELLED_NUMERIC)
378 to->Numbers[i] = ABSENT_NUMERIC;
379 else if (mergenum != ABSENT_NUMERIC)
380 to->Numbers[i] = (NCURSES_INT2) mergenum;
381 }
382 }
383
384 /*
385 * Note: the copies of strings this makes don't have their own
386 * storage. This is OK right now, but will be a problem if we
387 * we ever want to deallocate entries.
388 */
389 for_each_string(i, from) {
390 if (to->Strings[i] != CANCELLED_STRING) {
391 char *mergestring = from->Strings[i];
392
393 if (mergestring == CANCELLED_STRING)
394 to->Strings[i] = ABSENT_STRING;
395 else if (mergestring != ABSENT_STRING)
396 to->Strings[i] = mergestring;
397 }
398 }
399 #if NCURSES_XNAMES
400 /* cleanup */
401 free(copy.Booleans);
402 free(copy.Numbers);
403 free(copy.Strings);
404 free(copy.ext_Names);
405 #endif
406 }
407
408 #if NO_LEAKS
409 NCURSES_EXPORT(void)
_nc_alloc_entry_leaks(void)410 _nc_alloc_entry_leaks(void)
411 {
412 if (stringbuf != NULL) {
413 FreeAndNull(stringbuf);
414 }
415 next_free = 0;
416 }
417 #endif
418