1 /****************************************************************************
2 * Copyright 2018-2022,2023 Thomas E. Dickey *
3 * Copyright 1999-2016,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: Thomas E. Dickey <dickey@clark.net> 1999-on *
32 ****************************************************************************/
35 * align_ttype.c -- functions for TERMTYPE
37 * _nc_align_termtype()
42 #include <curses.priv.h>
46 MODULE_ID("$Id: alloc_ttype.c,v 1.51 2023/09/09 23:15:53 tom Exp $")
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.
54 merge_names(char **dst, char **a, int na, char **b, int nb)
57 while (na > 0 && nb > 0) {
58 int cmp = strcmp(*a, *b);
77 DEBUG(4, ("merge_names -> %d", n));
82 find_name(char **table, int item, int length, const char *name)
87 for (n = item; n < length; ++n) {
88 if (!strcmp(table[n], name)) {
89 DEBUG(4, ("found name '%s' @%d", name, n));
95 DEBUG(4, ("did not find name '%s'", name));
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))
106 realign_data(TERMTYPE2 *to, char **ext_Names,
112 int to_Booleans = to->ext_Booleans;
113 int to_Numbers = to->ext_Numbers;
114 int to_Strings = to->ext_Strings;
117 DEBUG(4, ("realign_data %d/%d/%d vs %d/%d/%d",
125 if (to->ext_Booleans != ext_Booleans) {
127 to2 = to_Booleans + to1;
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--];
137 to->Booleans[base + m] = FALSE;
140 to->ext_Booleans = UShort(ext_Booleans);
143 if (to->ext_Numbers != ext_Numbers) {
145 to2 = to_Numbers + to1;
147 EXTEND_NUM(num_Numbers, ext_Numbers);
148 TYPE_REALLOC(NCURSES_INT2, to->num_Numbers, to->Numbers);
149 for (n = to->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--];
155 to->Numbers[base + m] = ABSENT_NUMERIC;
158 to->ext_Numbers = UShort(ext_Numbers);
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,
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--];
173 to->Strings[base + m] = ABSENT_STRING;
176 to->ext_Strings = UShort(ext_Strings);
181 * Returns the first index in ext_Names[] for the given token-type
184 _nc_first_ext_name(TERMTYPE2 *tp, int token_type)
188 switch (token_type) {
193 first = tp->ext_Booleans;
196 first = (unsigned) (tp->ext_Booleans + tp->ext_Numbers);
206 * Returns the last index in ext_Names[] for the given token-type
209 _nc_last_ext_name(TERMTYPE2 *tp, int token_type)
213 switch (token_type) {
215 last = tp->ext_Booleans;
218 last = (unsigned) (tp->ext_Booleans + tp->ext_Numbers);
222 last = NUM_EXT_NAMES(tp);
229 * Lookup an entry from extended-names, returning -1 if not found
232 _nc_find_ext_name(TERMTYPE2 *tp, char *name, int token_type)
235 unsigned first = _nc_first_ext_name(tp, token_type);
236 unsigned last = _nc_last_ext_name(tp, token_type);
238 for (j = first; j < last; j++) {
239 if (!strcmp(name, tp->ext_Names[j])) {
247 * Translate an index into ext_Names[] into the corresponding index into data
248 * (e.g., Booleans[]).
251 _nc_ext_data_index(TERMTYPE2 *tp, int n, int token_type)
253 switch (token_type) {
255 n += (tp->num_Booleans - tp->ext_Booleans);
258 n += (tp->num_Numbers - tp->ext_Numbers)
259 - (tp->ext_Booleans);
263 n += (tp->num_Strings - tp->ext_Strings)
264 - (tp->ext_Booleans + tp->ext_Numbers);
270 * Adjust tables to remove (not free) an extended name and its corresponding
274 _nc_del_ext_name(TERMTYPE2 *tp, char *name, int token_type)
278 if ((first = _nc_find_ext_name(tp, name, token_type)) >= 0) {
280 int last = (int) NUM_EXT_NAMES(tp) - 1;
282 for (j = first; j < last; j++) {
283 tp->ext_Names[j] = tp->ext_Names[j + 1];
285 first = _nc_ext_data_index(tp, first, token_type);
286 switch (token_type) {
288 last = tp->num_Booleans - 1;
289 for (j = first; j < last; j++)
290 tp->Booleans[j] = tp->Booleans[j + 1];
295 last = tp->num_Numbers - 1;
296 for (j = first; j < last; j++)
297 tp->Numbers[j] = tp->Numbers[j + 1];
302 last = tp->num_Strings - 1;
303 for (j = first; j < last; j++)
304 tp->Strings[j] = tp->Strings[j + 1];
315 * Adjust tables to insert an extended name, making room for new data. The
316 * index into the corresponding data array is returned.
319 _nc_ins_ext_name(TERMTYPE2 *tp, char *name, int token_type)
321 unsigned first = _nc_first_ext_name(tp, token_type);
322 unsigned last = _nc_last_ext_name(tp, token_type);
323 unsigned total = NUM_EXT_NAMES(tp) + 1;
326 for (j = first; j < last; j++) {
327 int cmp = strcmp(name, tp->ext_Names[j]);
329 /* already present */
330 return _nc_ext_data_index(tp, (int) j, token_type);
336 TYPE_REALLOC(char *, total, tp->ext_Names);
337 for (k = total - 1; k > j; k--)
338 tp->ext_Names[k] = tp->ext_Names[k - 1];
339 tp->ext_Names[j] = name;
340 j = (unsigned) _nc_ext_data_index(tp, (int) j, token_type);
342 switch (token_type) {
346 TYPE_REALLOC(NCURSES_SBOOL, tp->num_Booleans, tp->Booleans);
347 for (k = (unsigned) (tp->num_Booleans - 1); k > j; k--)
348 tp->Booleans[k] = tp->Booleans[k - 1];
353 TYPE_REALLOC(NCURSES_INT2, tp->num_Numbers, tp->Numbers);
354 for (k = (unsigned) (tp->num_Numbers - 1); k > j; k--)
355 tp->Numbers[k] = tp->Numbers[k - 1];
360 TYPE_REALLOC(char *, tp->num_Strings, tp->Strings);
361 for (k = (unsigned) (tp->num_Strings - 1); k > j; k--)
362 tp->Strings[k] = tp->Strings[k - 1];
369 * Look for strings that are marked cancelled, which happen to be the same name
370 * as a boolean or number. We'll get this as a special case when we get a
371 * cancellation of a name that is inherited from another entry.
374 adjust_cancels(TERMTYPE2 *to, TERMTYPE2 *from)
376 int first = to->ext_Booleans + to->ext_Numbers;
377 int last = first + to->ext_Strings;
380 DEBUG(3, (T_CALLED("adjust_cancels(%s), from(%s)"),
381 NonNull(to->term_names),
382 NonNull(from->term_names)));
383 for (j = first; j < last;) {
384 char *name = to->ext_Names[j];
385 int j_str = to->num_Strings - first - to->ext_Strings;
387 if (to->Strings[j + j_str] == CANCELLED_STRING) {
388 if (_nc_find_ext_name(from, to->ext_Names[j], BOOLEAN) >= 0) {
389 if (_nc_del_ext_name(to, name, STRING)
390 || _nc_del_ext_name(to, name, NUMBER)) {
391 k = _nc_ins_ext_name(to, name, BOOLEAN);
392 to->Booleans[k] = FALSE;
396 } else if (_nc_find_ext_name(from, to->ext_Names[j], NUMBER) >= 0) {
397 if (_nc_del_ext_name(to, name, STRING)
398 || _nc_del_ext_name(to, name, BOOLEAN)) {
399 k = _nc_ins_ext_name(to, name, NUMBER);
400 to->Numbers[k] = CANCELLED_NUMERIC;
404 } else if (_nc_find_ext_name(from, to->ext_Names[j], STRING) >= 0) {
405 if (_nc_del_ext_name(to, name, NUMBER)
406 || _nc_del_ext_name(to, name, BOOLEAN)) {
407 k = _nc_ins_ext_name(to, name, STRING);
408 to->Strings[k] = CANCELLED_STRING;
419 DEBUG(3, (T_RETURN("")));
423 _nc_align_termtype(TERMTYPE2 *to, TERMTYPE2 *from)
429 na = to ? ((int) NUM_EXT_NAMES(to)) : 0;
430 nb = from ? ((int) NUM_EXT_NAMES(from)) : 0;
432 DEBUG(2, (T_CALLED("_nc_align_termtype to(%d:%s), from(%d:%s)"),
433 na, to ? NonNull(to->term_names) : "?",
434 nb, from ? NonNull(from->term_names) : "?"));
436 if (to != NULL && from != NULL && (na != 0 || nb != 0)) {
437 int ext_Booleans, ext_Numbers, ext_Strings;
438 bool used_ext_Names = FALSE;
440 if ((na == nb) /* check if the arrays are equivalent */
441 &&(to->ext_Booleans == from->ext_Booleans)
442 && (to->ext_Numbers == from->ext_Numbers)
443 && (to->ext_Strings == from->ext_Strings)) {
447 for (n = 0, same = TRUE; n < na; n++) {
448 if (strcmp(to->ext_Names[n], from->ext_Names[n])) {
454 DEBUG(2, (T_RETURN("")));
459 * This is where we pay for having a simple extension representation.
460 * Allocate a new ext_Names array and merge the two ext_Names arrays
461 * into it, updating to's counts for booleans, etc. Fortunately we do
462 * this only for the terminfo compiler (tic) and comparer (infocmp).
464 TYPE_MALLOC(char *, (size_t)(na + nb), ext_Names);
466 if (to->ext_Strings && (from->ext_Booleans + from->ext_Numbers))
467 adjust_cancels(to, from);
469 if (from->ext_Strings && (to->ext_Booleans + to->ext_Numbers))
470 adjust_cancels(from, to);
472 ext_Booleans = merge_names(ext_Names,
477 ext_Numbers = merge_names(ext_Names + ext_Booleans,
482 + from->ext_Booleans,
484 ext_Strings = merge_names(ext_Names + ext_Numbers + ext_Booleans,
494 * Now we must reallocate the Booleans, etc., to allow the data to be
497 if (na != (ext_Booleans + ext_Numbers + ext_Strings)) {
498 realign_data(to, ext_Names, ext_Booleans, ext_Numbers, ext_Strings);
499 FreeIfNeeded(to->ext_Names);
500 to->ext_Names = ext_Names;
501 DEBUG(2, ("realigned %d extended names for '%s' (to)",
502 NUM_EXT_NAMES(to), to->term_names));
503 used_ext_Names = TRUE;
505 if (nb != (ext_Booleans + ext_Numbers + ext_Strings)) {
506 nb = (ext_Booleans + ext_Numbers + ext_Strings);
507 realign_data(from, ext_Names, ext_Booleans, ext_Numbers, ext_Strings);
508 TYPE_REALLOC(char *, (size_t) nb, from->ext_Names);
509 memcpy(from->ext_Names, ext_Names, sizeof(char *) * (size_t) nb);
510 DEBUG(2, ("realigned %d extended names for '%s' (from)",
511 NUM_EXT_NAMES(from), from->term_names));
516 DEBUG(2, (T_RETURN("")));
524 * TERMTYPE and TERMTYPE2 differ only with regard to the values in Numbers.
525 * Use 'mode' to decide which to use.
528 copy_termtype(TERMTYPE2 *dst, const TERMTYPE2 *src, int mode)
533 size_t new_table_size;
534 #if NCURSES_EXT_NUMBERS
539 DEBUG(2, (T_CALLED("copy_termtype(dst=%p, src=%p, mode=%d)"), (void *)
540 dst, (const void *) src, mode));
541 *dst = *src; /* ...to copy the sizes and string-tables */
543 TYPE_MALLOC(NCURSES_SBOOL, NUM_BOOLEANS(dst), dst->Booleans);
544 TYPE_MALLOC(char *, NUM_STRINGS(dst), dst->Strings);
546 memcpy(dst->Booleans,
548 NUM_BOOLEANS(dst) * sizeof(dst->Booleans[0]));
551 NUM_STRINGS(dst) * sizeof(dst->Strings[0]));
555 for (pass = 0; pass < 2; ++pass) {
557 if (src->term_names != NULL) {
559 dst->term_names = new_table + str_size;
560 _nc_STRCPY(dst->term_names + str_size,
562 new_table_size - str_size);
564 str_size += strlen(src->term_names) + 1;
566 for_each_string(i, src) {
567 if (VALID_STRING(src->Strings[i])) {
569 _nc_STRCPY(new_table + str_size,
571 new_table_size - str_size);
572 dst->Strings[i] = new_table + str_size;
574 str_size += strlen(src->Strings[i]) + 1;
578 dst->str_table = new_table;
581 if ((new_table = malloc(str_size)) == NULL)
582 _nc_err_abort(MSG_NO_MEMORY);
583 new_table_size = str_size;
587 #if NCURSES_EXT_NUMBERS
588 if ((mode & dstINT) == 0) {
589 DEBUG(2, ("...convert int ->short"));
590 TYPE_MALLOC(short, NUM_NUMBERS(dst), oldptr);
591 ((TERMTYPE *) dst)->Numbers = oldptr;
593 DEBUG(2, ("...copy without changing size"));
594 TYPE_MALLOC(int, NUM_NUMBERS(dst), newptr);
595 dst->Numbers = newptr;
597 if ((mode == srcINT) && (oldptr != 0)) {
598 DEBUG(2, ("...copy int ->short"));
599 for (i = 0; i < NUM_NUMBERS(dst); ++i) {
600 if (src->Numbers[i] > MAX_OF_TYPE(short)) {
601 oldptr[i] = MAX_OF_TYPE(short);
603 oldptr[i] = (short) src->Numbers[i];
606 } else if ((mode == dstINT) && (newptr != 0)) {
607 DEBUG(2, ("...copy short ->int"));
608 for (i = 0; i < NUM_NUMBERS(dst); ++i) {
609 newptr[i] = ((const short *) (src->Numbers))[i];
612 DEBUG(2, ("...copy %s without change",
618 NUM_NUMBERS(dst) * ((mode & dstINT)
624 TYPE_MALLOC(short, NUM_NUMBERS(dst), dst->Numbers);
627 NUM_NUMBERS(dst) * sizeof(dst->Numbers[0]));
631 if ((i = NUM_EXT_NAMES(src)) != 0) {
632 TYPE_MALLOC(char *, i, dst->ext_Names);
633 memcpy(dst->ext_Names, src->ext_Names, i * sizeof(char *));
637 for (pass = 0; pass < 2; ++pass) {
639 char *raw_data = src->ext_str_table;
640 if (raw_data != NULL) {
641 for (i = 0; i < src->ext_Strings; ++i) {
642 size_t skip = strlen(raw_data) + 1;
645 _nc_STRCPY(new_table + str_size,
647 new_table_size - str_size);
654 for (i = 0; i < NUM_EXT_NAMES(dst); ++i) {
655 if (VALID_STRING(src->ext_Names[i])) {
657 _nc_STRCPY(new_table + str_size,
659 new_table_size - str_size);
660 dst->ext_Names[i] = new_table + str_size;
662 str_size += strlen(src->ext_Names[i]) + 1;
666 dst->ext_str_table = new_table;
669 if ((new_table = calloc(str_size, 1)) == NULL)
670 _nc_err_abort(MSG_NO_MEMORY);
671 new_table_size = str_size;
678 (void) new_table_size;
679 DEBUG(2, (T_RETURN("")));
683 _nc_copy_termtype(TERMTYPE *dst, const TERMTYPE *src)
685 DEBUG(2, (T_CALLED("_nc_copy_termtype(dst=%p, src=%p)"), (void *) dst,
686 (const void *) src));
687 copy_termtype((TERMTYPE2 *) dst, (const TERMTYPE2 *) src, 0);
688 DEBUG(2, (T_RETURN("")));
691 #if NCURSES_EXT_NUMBERS
693 _nc_copy_termtype2(TERMTYPE2 *dst, const TERMTYPE2 *src)
695 DEBUG(2, (T_CALLED("_nc_copy_termtype2(dst=%p, src=%p)"), (void *) dst,
696 (const void *) src));
697 copy_termtype(dst, src, srcINT | dstINT);
698 DEBUG(2, (T_RETURN("")));
702 * Use this for exporting the internal TERMTYPE2 to the legacy format used via
703 * the CUR macro by applications.
706 _nc_export_termtype2(TERMTYPE *dst, const TERMTYPE2 *src)
708 DEBUG(2, (T_CALLED("_nc_export_termtype2(dst=%p, src=%p)"), (void *)
709 dst, (const void *) src));
710 copy_termtype((TERMTYPE2 *) dst, src, srcINT);
711 DEBUG(2, (T_RETURN("")));
713 #endif /* NCURSES_EXT_NUMBERS */