xref: /freebsd/contrib/ncurses/ncurses/tinfo/alloc_ttype.c (revision 68ad2b0d7af2a3571c4abac9afa712f9b09b721c)
1 /****************************************************************************
2  * Copyright 2018-2024,2025 Thomas E. Dickey                                *
3  * Copyright 1999-2016,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: Thomas E. Dickey <dickey@clark.net> 1999-on                     *
32  ****************************************************************************/
33 
34 /*
35  * align_ttype.c --  functions for TERMTYPE
36  *
37  *	_nc_align_termtype()
38  *	_nc_copy_termtype()
39  *
40  */
41 
42 #include <curses.priv.h>
43 
44 #include <tic.h>
45 
46 MODULE_ID("$Id: alloc_ttype.c,v 1.55 2025/02/16 18:31:37 tom Exp $")
47 
48 #if NCURSES_XNAMES
49 /*
50  * Merge the a/b lists into dst.  Both a/b are sorted (see _nc_extend_names()),
51  * so we do not have to worry about order dependencies.
52  */
53 static int
merge_names(char ** dst,char ** a,int na,char ** b,int nb)54 merge_names(char **dst, char **a, int na, char **b, int nb)
55 {
56     int n = 0;
57     while (na > 0 && nb > 0) {
58 	int cmp = strcmp(*a, *b);
59 	if (cmp < 0) {
60 	    dst[n++] = *a++;
61 	    na--;
62 	} else if (cmp > 0) {
63 	    dst[n++] = *b++;
64 	    nb--;
65 	} else {
66 	    dst[n++] = *a;
67 	    a++, b++;
68 	    na--, nb--;
69 	}
70     }
71     while (na-- > 0) {
72 	dst[n++] = *a++;
73     }
74     while (nb-- > 0) {
75 	dst[n++] = *b++;
76     }
77     DEBUG(4, ("merge_names -> %d", n));
78     return n;
79 }
80 
81 static bool
find_name(char ** table,int item,int length,const char * name)82 find_name(char **table, int item, int length, const char *name)
83 {
84     int n;
85     int result = -1;
86 
87     for (n = item; n < length; ++n) {
88 	if (!strcmp(table[n], name)) {
89 	    DEBUG(4, ("found name '%s' @%d", name, n));
90 	    result = n;
91 	    break;
92 	}
93     }
94     if (result < 0) {
95 	DEBUG(4, ("did not find name '%s'", name));
96     }
97     return (result >= 0);
98 }
99 
100 #define EXTEND_NUM(num, ext) \
101 	DEBUG(4, ("extending " #num " from %d to %d", \
102 	 to->num, (unsigned short) (to->num + (ext - to->ext)))); \
103 	to->num = (unsigned short) (to->num + (ext - to->ext))
104 
105 static void
realign_data(TERMTYPE2 * to,char ** ext_Names,int ext_Booleans,int ext_Numbers,int ext_Strings)106 realign_data(TERMTYPE2 *to, char **ext_Names,
107 	     int ext_Booleans,
108 	     int ext_Numbers,
109 	     int ext_Strings)
110 {
111     int n, m, base;
112     int to_Booleans = to->ext_Booleans;
113     int to_Numbers = to->ext_Numbers;
114     int to_Strings = to->ext_Strings;
115     int to1, to2, from;
116 
117     DEBUG(4, ("realign_data %d/%d/%d vs %d/%d/%d",
118 	      ext_Booleans,
119 	      ext_Numbers,
120 	      ext_Strings,
121 	      to->ext_Booleans,
122 	      to->ext_Numbers,
123 	      to->ext_Strings));
124 
125     if (to->ext_Booleans != ext_Booleans) {
126 	to1 = 0;
127 	to2 = to_Booleans + to1;
128 	from = 0;
129 	EXTEND_NUM(num_Booleans, ext_Booleans);
130 	TYPE_REALLOC(NCURSES_SBOOL, to->num_Booleans, to->Booleans);
131 	for (n = to->ext_Booleans - 1,
132 	     m = ext_Booleans - 1,
133 	     base = to->num_Booleans - (m + 1); m >= 0; m--) {
134 	    if (find_name(to->ext_Names, to1, to2, ext_Names[m + from])) {
135 		to->Booleans[base + m] = to->Booleans[base + n--];
136 	    } else {
137 		to->Booleans[base + m] = FALSE;
138 	    }
139 	}
140 	to->ext_Booleans = UShort(ext_Booleans);
141     }
142 
143     if (to->ext_Numbers != ext_Numbers) {
144 	to1 = to_Booleans;
145 	to2 = to_Numbers + to1;
146 	from = ext_Booleans;
147 	EXTEND_NUM(num_Numbers, ext_Numbers);
148 	TYPE_REALLOC(NCURSES_INT2, to->num_Numbers, to->Numbers);
149 	for (n = to->ext_Numbers - 1,
150 	     m = ext_Numbers - 1,
151 	     base = to->num_Numbers - (m + 1); m >= 0; m--) {
152 	    if (find_name(to->ext_Names, to1, to2, ext_Names[m + from])) {
153 		to->Numbers[base + m] = to->Numbers[base + n--];
154 	    } else {
155 		to->Numbers[base + m] = ABSENT_NUMERIC;
156 	    }
157 	}
158 	to->ext_Numbers = UShort(ext_Numbers);
159     }
160 
161     if (to->ext_Strings != ext_Strings) {
162 	to1 = to_Booleans + to_Numbers;
163 	to2 = to_Strings + to1;
164 	from = ext_Booleans + ext_Numbers;
165 	EXTEND_NUM(num_Strings, ext_Strings);
166 	TYPE_REALLOC(char *, to->num_Strings, to->Strings);
167 	for (n = to->ext_Strings - 1,
168 	     m = ext_Strings - 1,
169 	     base = to->num_Strings - (m + 1); m >= 0; m--) {
170 	    if (find_name(to->ext_Names, to1, to2, ext_Names[m + from])) {
171 		to->Strings[base + m] = to->Strings[base + n--];
172 	    } else {
173 		to->Strings[base + m] = ABSENT_STRING;
174 	    }
175 	}
176 	to->ext_Strings = UShort(ext_Strings);
177     }
178 }
179 
180 /*
181  * Returns the first index in ext_Names[] for the given token-type
182  */
183 static unsigned
_nc_first_ext_name(const TERMTYPE2 * tp,int token_type)184 _nc_first_ext_name(const TERMTYPE2 *tp, int token_type)
185 {
186     unsigned first;
187 
188     switch (token_type) {
189     case BOOLEAN:
190 	first = 0;
191 	break;
192     case NUMBER:
193 	first = tp->ext_Booleans;
194 	break;
195     case STRING:
196 	first = (unsigned) (tp->ext_Booleans + tp->ext_Numbers);
197 	break;
198     default:
199 	first = 0;
200 	break;
201     }
202     return first;
203 }
204 
205 /*
206  * Returns the last index in ext_Names[] for the given token-type
207  */
208 static unsigned
_nc_last_ext_name(const TERMTYPE2 * tp,int token_type)209 _nc_last_ext_name(const TERMTYPE2 *tp, int token_type)
210 {
211     unsigned last;
212 
213     switch (token_type) {
214     case BOOLEAN:
215 	last = tp->ext_Booleans;
216 	break;
217     case NUMBER:
218 	last = (unsigned) (tp->ext_Booleans + tp->ext_Numbers);
219 	break;
220     default:
221     case STRING:
222 	last = NUM_EXT_NAMES(tp);
223 	break;
224     }
225     return last;
226 }
227 
228 /*
229  * Lookup an entry from extended-names, returning -1 if not found
230  */
231 static int
_nc_find_ext_name(const TERMTYPE2 * tp,const char * name,int token_type)232 _nc_find_ext_name(const TERMTYPE2 *tp, const char *name, int token_type)
233 {
234     if (name != NULL) {
235 	unsigned j;
236 	unsigned first = _nc_first_ext_name(tp, token_type);
237 	unsigned last = _nc_last_ext_name(tp, token_type);
238 
239 	for (j = first; j < last; j++) {
240 	    if (!strcmp(name, tp->ext_Names[j])) {
241 		return (int) j;
242 	    }
243 	}
244     }
245     return -1;
246 }
247 
248 /*
249  * Translate an index into ext_Names[] into the corresponding index into data
250  * (e.g., Booleans[]).
251  */
252 static int
_nc_ext_data_index(const TERMTYPE2 * tp,int n,int token_type)253 _nc_ext_data_index(const TERMTYPE2 *tp, int n, int token_type)
254 {
255     switch (token_type) {
256     case BOOLEAN:
257 	n += (tp->num_Booleans - tp->ext_Booleans);
258 	break;
259     case NUMBER:
260 	n += (tp->num_Numbers - tp->ext_Numbers)
261 	    - (tp->ext_Booleans);
262 	break;
263     default:
264     case STRING:
265 	n += (tp->num_Strings - tp->ext_Strings)
266 	    - (tp->ext_Booleans + tp->ext_Numbers);
267     }
268     return n;
269 }
270 
271 /*
272  * Adjust tables to remove (not free) an extended name and its corresponding
273  * data.
274  */
275 static bool
_nc_del_ext_name(TERMTYPE2 * tp,const char * name,int token_type)276 _nc_del_ext_name(TERMTYPE2 *tp, const char *name, int token_type)
277 {
278     int first;
279 
280     if ((first = _nc_find_ext_name(tp, name, token_type)) >= 0) {
281 	int j;
282 	int last = (int) NUM_EXT_NAMES(tp) - 1;
283 
284 	for (j = first; j < last; j++) {
285 	    tp->ext_Names[j] = tp->ext_Names[j + 1];
286 	}
287 	first = _nc_ext_data_index(tp, first, token_type);
288 	switch (token_type) {
289 	case BOOLEAN:
290 	    last = tp->num_Booleans - 1;
291 	    for (j = first; j < last; j++)
292 		tp->Booleans[j] = tp->Booleans[j + 1];
293 	    tp->ext_Booleans--;
294 	    tp->num_Booleans--;
295 	    break;
296 	case NUMBER:
297 	    last = tp->num_Numbers - 1;
298 	    for (j = first; j < last; j++)
299 		tp->Numbers[j] = tp->Numbers[j + 1];
300 	    tp->ext_Numbers--;
301 	    tp->num_Numbers--;
302 	    break;
303 	case STRING:
304 	    last = tp->num_Strings - 1;
305 	    for (j = first; j < last; j++)
306 		tp->Strings[j] = tp->Strings[j + 1];
307 	    tp->ext_Strings--;
308 	    tp->num_Strings--;
309 	    break;
310 	}
311 	return TRUE;
312     }
313     return FALSE;
314 }
315 
316 /*
317  * Adjust tables to insert an extended name, making room for new data.  The
318  * index into the corresponding data array is returned.
319  */
320 static int
_nc_ins_ext_name(TERMTYPE2 * tp,char * name,int token_type)321 _nc_ins_ext_name(TERMTYPE2 *tp, char *name, int token_type)
322 {
323     unsigned first = _nc_first_ext_name(tp, token_type);
324     unsigned last = _nc_last_ext_name(tp, token_type);
325     unsigned total = NUM_EXT_NAMES(tp) + 1;
326     unsigned j, k;
327 
328     for (j = first; j < last; j++) {
329 	int cmp = strcmp(name, tp->ext_Names[j]);
330 	if (cmp == 0)
331 	    /* already present */
332 	    return _nc_ext_data_index(tp, (int) j, token_type);
333 	if (cmp < 0) {
334 	    break;
335 	}
336     }
337 
338     TYPE_REALLOC(char *, total, tp->ext_Names);
339     for (k = total - 1; k > j; k--)
340 	tp->ext_Names[k] = tp->ext_Names[k - 1];
341     tp->ext_Names[j] = name;
342     j = (unsigned) _nc_ext_data_index(tp, (int) j, token_type);
343 
344     switch (token_type) {
345     case BOOLEAN:
346 	tp->ext_Booleans++;
347 	tp->num_Booleans++;
348 	TYPE_REALLOC(NCURSES_SBOOL, tp->num_Booleans, tp->Booleans);
349 	for (k = (unsigned) (tp->num_Booleans - 1); k > j; k--)
350 	    tp->Booleans[k] = tp->Booleans[k - 1];
351 	break;
352     case NUMBER:
353 	tp->ext_Numbers++;
354 	tp->num_Numbers++;
355 	TYPE_REALLOC(NCURSES_INT2, tp->num_Numbers, tp->Numbers);
356 	for (k = (unsigned) (tp->num_Numbers - 1); k > j; k--)
357 	    tp->Numbers[k] = tp->Numbers[k - 1];
358 	break;
359     case STRING:
360 	tp->ext_Strings++;
361 	tp->num_Strings++;
362 	TYPE_REALLOC(char *, tp->num_Strings, tp->Strings);
363 	for (k = (unsigned) (tp->num_Strings - 1); k > j; k--)
364 	    tp->Strings[k] = tp->Strings[k - 1];
365 	break;
366     }
367     return (int) j;
368 }
369 
370 /*
371  * Look for strings that are marked cancelled, which happen to be the same name
372  * as a boolean or number.  We'll get this as a special case when we get a
373  * cancellation of a name that is inherited from another entry.
374  */
375 static void
adjust_cancels(TERMTYPE2 * to,TERMTYPE2 * from)376 adjust_cancels(TERMTYPE2 *to, TERMTYPE2 *from)
377 {
378     int first = to->ext_Booleans + to->ext_Numbers;
379     int last = first + to->ext_Strings;
380     int j, k;
381 
382     DEBUG(3, (T_CALLED("adjust_cancels(%s), from(%s)"),
383 	      NonNull(to->term_names),
384 	      NonNull(from->term_names)));
385     for (j = first; j < last;) {
386 	char *name;
387 	int j_str = to->num_Strings - first - to->ext_Strings;
388 
389 	if ((j + j_str) > NUM_STRINGS(to))
390 	    break;
391 	name = to->ext_Names[j];
392 	if (to->Strings[j + j_str] == CANCELLED_STRING) {
393 	    if (_nc_find_ext_name(from, to->ext_Names[j], BOOLEAN) >= 0) {
394 		if (_nc_del_ext_name(to, name, STRING)
395 		    || _nc_del_ext_name(to, name, NUMBER)) {
396 		    k = _nc_ins_ext_name(to, name, BOOLEAN);
397 		    to->Booleans[k] = FALSE;
398 		} else {
399 		    j++;
400 		}
401 	    } else if (_nc_find_ext_name(from, to->ext_Names[j], NUMBER) >= 0) {
402 		if (_nc_del_ext_name(to, name, STRING)
403 		    || _nc_del_ext_name(to, name, BOOLEAN)) {
404 		    k = _nc_ins_ext_name(to, name, NUMBER);
405 		    to->Numbers[k] = CANCELLED_NUMERIC;
406 		} else {
407 		    j++;
408 		}
409 	    } else if (_nc_find_ext_name(from, to->ext_Names[j], STRING) >= 0) {
410 		if (_nc_del_ext_name(to, name, NUMBER)
411 		    || _nc_del_ext_name(to, name, BOOLEAN)) {
412 		    k = _nc_ins_ext_name(to, name, STRING);
413 		    to->Strings[k] = CANCELLED_STRING;
414 		} else {
415 		    j++;
416 		}
417 	    } else {
418 		j++;
419 	    }
420 	} else {
421 	    j++;
422 	}
423     }
424     DEBUG(3, (T_RETURN("")));
425 }
426 
427 NCURSES_EXPORT(void)
_nc_align_termtype(TERMTYPE2 * to,TERMTYPE2 * from)428 _nc_align_termtype(TERMTYPE2 *to, TERMTYPE2 *from)
429 {
430     int na;
431     int nb;
432     char **ext_Names;
433 
434     na = to ? ((int) NUM_EXT_NAMES(to)) : 0;
435     nb = from ? ((int) NUM_EXT_NAMES(from)) : 0;
436 
437     DEBUG(2, (T_CALLED("_nc_align_termtype to(%d:%s), from(%d:%s)"),
438 	      na, to ? NonNull(to->term_names) : "?",
439 	      nb, from ? NonNull(from->term_names) : "?"));
440 
441     if (to != NULL && from != NULL && (na != 0 || nb != 0)) {
442 	int ext_Booleans, ext_Numbers, ext_Strings;
443 	bool used_ext_Names = FALSE;
444 
445 	if ((na == nb)		/* check if the arrays are equivalent */
446 	    &&(to->ext_Booleans == from->ext_Booleans)
447 	    && (to->ext_Numbers == from->ext_Numbers)
448 	    && (to->ext_Strings == from->ext_Strings)) {
449 	    int n;
450 	    bool same;
451 
452 	    for (n = 0, same = TRUE; n < na; n++) {
453 		if (strcmp(to->ext_Names[n], from->ext_Names[n])) {
454 		    same = FALSE;
455 		    break;
456 		}
457 	    }
458 	    if (same) {
459 		DEBUG(2, (T_RETURN("")));
460 		return;
461 	    }
462 	}
463 	/*
464 	 * This is where we pay for having a simple extension representation.
465 	 * Allocate a new ext_Names array and merge the two ext_Names arrays
466 	 * into it, updating to's counts for booleans, etc.  Fortunately we do
467 	 * this only for the terminfo compiler (tic) and comparer (infocmp).
468 	 */
469 	TYPE_MALLOC(char *, (size_t)(na + nb), ext_Names);
470 
471 	if (to->ext_Strings && (from->ext_Booleans + from->ext_Numbers))
472 	    adjust_cancels(to, from);
473 
474 	if (from->ext_Strings && (to->ext_Booleans + to->ext_Numbers))
475 	    adjust_cancels(from, to);
476 
477 	ext_Booleans = merge_names(ext_Names,
478 				   to->ext_Names,
479 				   to->ext_Booleans,
480 				   from->ext_Names,
481 				   from->ext_Booleans);
482 	ext_Numbers = merge_names(ext_Names + ext_Booleans,
483 				  to->ext_Names
484 				  + to->ext_Booleans,
485 				  to->ext_Numbers,
486 				  from->ext_Names
487 				  + from->ext_Booleans,
488 				  from->ext_Numbers);
489 	ext_Strings = merge_names(ext_Names + ext_Numbers + ext_Booleans,
490 				  to->ext_Names
491 				  + to->ext_Booleans
492 				  + to->ext_Numbers,
493 				  to->ext_Strings,
494 				  from->ext_Names
495 				  + from->ext_Booleans
496 				  + from->ext_Numbers,
497 				  from->ext_Strings);
498 	/*
499 	 * Now we must reallocate the Booleans, etc., to allow the data to be
500 	 * overlaid.
501 	 */
502 	if (na != (ext_Booleans + ext_Numbers + ext_Strings)) {
503 	    realign_data(to, ext_Names, ext_Booleans, ext_Numbers, ext_Strings);
504 	    FreeIfNeeded(to->ext_Names);
505 	    to->ext_Names = ext_Names;
506 	    DEBUG(2, ("realigned %d extended names for '%s' (to)",
507 		      NUM_EXT_NAMES(to), to->term_names));
508 	    used_ext_Names = TRUE;
509 	}
510 	if (nb != (ext_Booleans + ext_Numbers + ext_Strings)) {
511 	    nb = (ext_Booleans + ext_Numbers + ext_Strings);
512 	    realign_data(from, ext_Names, ext_Booleans, ext_Numbers, ext_Strings);
513 	    TYPE_REALLOC(char *, (size_t) nb, from->ext_Names);
514 	    memcpy(from->ext_Names, ext_Names, sizeof(char *) * (size_t) nb);
515 	    DEBUG(2, ("realigned %d extended names for '%s' (from)",
516 		      NUM_EXT_NAMES(from), from->term_names));
517 	}
518 	if (!used_ext_Names)
519 	    free(ext_Names);
520     }
521     DEBUG(2, (T_RETURN("")));
522 }
523 #endif
524 
525 #define srcINT 1
526 #define dstINT 2
527 
528 /*
529  * TERMTYPE and TERMTYPE2 differ only with regard to the values in Numbers.
530  * Use 'mode' to decide which to use.
531  */
532 static void
copy_termtype(TERMTYPE2 * dst,const TERMTYPE2 * src,int mode)533 copy_termtype(TERMTYPE2 *dst, const TERMTYPE2 *src, int mode)
534 {
535     unsigned i;
536     int pass;
537     char *new_table;
538     size_t new_table_size;
539 #if NCURSES_EXT_NUMBERS
540     short *oldptr = NULL;
541     int *newptr = NULL;
542 #endif
543 
544     DEBUG(2, (T_CALLED("copy_termtype(dst=%p, src=%p, mode=%d)"), (void *)
545 	      dst, (const void *) src, mode));
546     *dst = *src;		/* ...to copy the sizes and string-tables */
547 
548     TYPE_MALLOC(NCURSES_SBOOL, NUM_BOOLEANS(dst), dst->Booleans);
549     TYPE_MALLOC(char *, NUM_STRINGS(dst), dst->Strings);
550 
551     memcpy(dst->Booleans,
552 	   src->Booleans,
553 	   NUM_BOOLEANS(dst) * sizeof(dst->Booleans[0]));
554     memcpy(dst->Strings,
555 	   src->Strings,
556 	   NUM_STRINGS(dst) * sizeof(dst->Strings[0]));
557 
558     new_table = NULL;
559     new_table_size = 0;
560     for (pass = 0; pass < 2; ++pass) {
561 	size_t str_size = 0;
562 	if (src->term_names != NULL) {
563 	    if (pass) {
564 		dst->term_names = new_table + str_size;
565 		_nc_STRCPY(dst->term_names + str_size,
566 			   src->term_names,
567 			   new_table_size - str_size);
568 	    }
569 	    str_size += strlen(src->term_names) + 1;
570 	}
571 	for_each_string(i, src) {
572 	    if (VALID_STRING(src->Strings[i])) {
573 		if (pass) {
574 		    _nc_STRCPY(new_table + str_size,
575 			       src->Strings[i],
576 			       new_table_size - str_size);
577 		    dst->Strings[i] = new_table + str_size;
578 		}
579 		str_size += strlen(src->Strings[i]) + 1;
580 	    }
581 	}
582 	if (pass) {
583 	    dst->str_table = new_table;
584 	} else {
585 	    ++str_size;
586 	    if ((new_table = malloc(str_size)) == NULL)
587 		_nc_err_abort(MSG_NO_MEMORY);
588 	    new_table_size = str_size;
589 	}
590     }
591 
592 #if NCURSES_EXT_NUMBERS
593     if ((mode & dstINT) == 0) {
594 	DEBUG(2, ("...convert int ->short"));
595 	TYPE_MALLOC(short, NUM_NUMBERS(dst), oldptr);
596 	((TERMTYPE *) dst)->Numbers = oldptr;
597     } else {
598 	DEBUG(2, ("...copy without changing size"));
599 	TYPE_MALLOC(int, NUM_NUMBERS(dst), newptr);
600 	dst->Numbers = newptr;
601     }
602     if ((mode == srcINT) && (oldptr != NULL)) {
603 	DEBUG(2, ("...copy int ->short"));
604 	for (i = 0; i < NUM_NUMBERS(dst); ++i) {
605 	    if (src->Numbers[i] > MAX_OF_TYPE(short)) {
606 		oldptr[i] = MAX_OF_TYPE(short);
607 	    } else {
608 		oldptr[i] = (short) src->Numbers[i];
609 	    }
610 	}
611     } else if ((mode == dstINT) && (newptr != NULL)) {
612 	DEBUG(2, ("...copy short ->int"));
613 	for (i = 0; i < NUM_NUMBERS(dst); ++i) {
614 	    newptr[i] = ((const short *) (src->Numbers))[i];
615 	}
616     } else {
617 	DEBUG(2, ("...copy %s without change",
618 		  (mode & dstINT)
619 		  ? "int"
620 		  : "short"));
621 	memcpy(dst->Numbers,
622 	       src->Numbers,
623 	       NUM_NUMBERS(dst) * ((mode & dstINT)
624 				   ? sizeof(int)
625 				   : sizeof(short)));
626     }
627 #else
628     (void) mode;
629     TYPE_MALLOC(short, NUM_NUMBERS(dst), dst->Numbers);
630     memcpy(dst->Numbers,
631 	   src->Numbers,
632 	   NUM_NUMBERS(dst) * sizeof(dst->Numbers[0]));
633 #endif
634 
635 #if NCURSES_XNAMES
636     if ((i = NUM_EXT_NAMES(src)) != 0) {
637 	TYPE_MALLOC(char *, i, dst->ext_Names);
638 	memcpy(dst->ext_Names, src->ext_Names, i * sizeof(char *));
639 
640 	new_table = NULL;
641 	new_table_size = 0;
642 	for (pass = 0; pass < 2; ++pass) {
643 	    size_t str_size = 0;
644 	    char *raw_data = src->ext_str_table;
645 	    if (raw_data != NULL) {
646 		for (i = 0; i < src->ext_Strings; ++i) {
647 		    size_t skip = strlen(raw_data) + 1;
648 		    if (skip != 1) {
649 			if (pass) {
650 			    _nc_STRCPY(new_table + str_size,
651 				       raw_data,
652 				       new_table_size - str_size);
653 			}
654 			str_size += skip;
655 			raw_data += skip;
656 		    }
657 		}
658 	    }
659 	    for (i = 0; i < NUM_EXT_NAMES(dst); ++i) {
660 		if (VALID_STRING(src->ext_Names[i])) {
661 		    if (pass) {
662 			_nc_STRCPY(new_table + str_size,
663 				   src->ext_Names[i],
664 				   new_table_size - str_size);
665 			dst->ext_Names[i] = new_table + str_size;
666 		    }
667 		    str_size += strlen(src->ext_Names[i]) + 1;
668 		}
669 	    }
670 	    if (pass) {
671 		dst->ext_str_table = new_table;
672 	    } else {
673 		++str_size;
674 		if ((new_table = calloc(str_size, 1)) == NULL)
675 		    _nc_err_abort(MSG_NO_MEMORY);
676 		new_table_size = str_size;
677 	    }
678 	}
679     } else {
680 	dst->ext_Names = NULL;
681     }
682 #endif
683     (void) new_table_size;
684     DEBUG(2, (T_RETURN("")));
685 }
686 
687 NCURSES_EXPORT(void)
_nc_copy_termtype(TERMTYPE * dst,const TERMTYPE * src)688 _nc_copy_termtype(TERMTYPE *dst, const TERMTYPE *src)
689 {
690     DEBUG(2, (T_CALLED("_nc_copy_termtype(dst=%p, src=%p)"), (void *) dst,
691 	      (const void *) src));
692     copy_termtype((TERMTYPE2 *) dst, (const TERMTYPE2 *) src, 0);
693     DEBUG(2, (T_RETURN("")));
694 }
695 
696 #if NCURSES_EXT_NUMBERS
697 NCURSES_EXPORT(void)
_nc_copy_termtype2(TERMTYPE2 * dst,const TERMTYPE2 * src)698 _nc_copy_termtype2(TERMTYPE2 *dst, const TERMTYPE2 *src)
699 {
700     DEBUG(2, (T_CALLED("_nc_copy_termtype2(dst=%p, src=%p)"), (void *) dst,
701 	      (const void *) src));
702     copy_termtype(dst, src, srcINT | dstINT);
703     DEBUG(2, (T_RETURN("")));
704 }
705 
706 /*
707  * Use this for exporting the internal TERMTYPE2 to the legacy format used via
708  * the CUR macro by applications.
709  */
710 NCURSES_EXPORT(void)
_nc_export_termtype2(TERMTYPE * dst,const TERMTYPE2 * src)711 _nc_export_termtype2(TERMTYPE *dst, const TERMTYPE2 *src)
712 {
713     DEBUG(2, (T_CALLED("_nc_export_termtype2(dst=%p, src=%p)"), (void *)
714 	      dst, (const void *) src));
715     copy_termtype((TERMTYPE2 *) dst, src, srcINT);
716     DEBUG(2, (T_RETURN("")));
717 }
718 #endif /* NCURSES_EXT_NUMBERS */
719