1 /****************************************************************************
2 * Copyright (c) 1999-2018,2019 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Thomas E. Dickey <dickey@clark.net> 1999-on *
31 ****************************************************************************/
34 * align_ttype.c -- functions for TERMTYPE
36 * _nc_align_termtype()
41 #include <curses.priv.h>
45 MODULE_ID("$Id: alloc_ttype.c,v 1.31 2019/04/27 23:28:31 tom Exp $")
49 * Merge the a/b lists into dst. Both a/b are sorted (see _nc_extend_names()),
50 * so we do not have to worry about order dependencies.
53 merge_names(char **dst, char **a, int na, char **b, int nb)
56 while (na > 0 && nb > 0) {
57 int cmp = strcmp(*a, *b);
76 DEBUG(4, ("merge_names -> %d", n));
81 find_name(char **table, int item, int length, const char *name)
86 for (n = item; n < length; ++n) {
87 if (!strcmp(table[n], name)) {
88 DEBUG(4, ("found name '%s' @%d", name, n));
94 DEBUG(4, ("did not find name '%s'", name));
99 #define EXTEND_NUM(num, ext) \
100 DEBUG(4, ("extending " #num " from %d to %d", \
101 to->num, (unsigned short) (to->num + (ext - to->ext)))); \
102 to->num = (unsigned short) (to->num + (ext - to->ext))
105 realign_data(TERMTYPE2 *to, char **ext_Names,
111 int to_Booleans = to->ext_Booleans;
112 int to_Numbers = to->ext_Numbers;
113 int to_Strings = to->ext_Strings;
116 DEBUG(4, ("realign_data %d/%d/%d vs %d/%d/%d",
124 if (to->ext_Booleans != ext_Booleans) {
126 to2 = to_Booleans + to1;
128 EXTEND_NUM(num_Booleans, ext_Booleans);
129 TYPE_REALLOC(NCURSES_SBOOL, to->num_Booleans, to->Booleans);
130 for (n = to->ext_Booleans - 1,
131 m = ext_Booleans - 1,
132 base = to->num_Booleans - (m + 1); m >= 0; m--) {
133 if (find_name(to->ext_Names, to1, to2, ext_Names[m + from])) {
134 to->Booleans[base + m] = to->Booleans[base + n--];
136 to->Booleans[base + m] = FALSE;
139 to->ext_Booleans = UShort(ext_Booleans);
142 if (to->ext_Numbers != ext_Numbers) {
144 to2 = to_Numbers + to1;
146 EXTEND_NUM(num_Numbers, ext_Numbers);
147 TYPE_REALLOC(NCURSES_INT2, to->num_Numbers, to->Numbers);
148 for (n = to->ext_Numbers - 1,
150 base = to->num_Numbers - (m + 1); m >= 0; m--) {
151 if (find_name(to->ext_Names, to1, to2, ext_Names[m + from])) {
152 to->Numbers[base + m] = to->Numbers[base + n--];
154 to->Numbers[base + m] = ABSENT_NUMERIC;
157 to->ext_Numbers = UShort(ext_Numbers);
160 if (to->ext_Strings != ext_Strings) {
161 to1 = to_Booleans + to_Numbers;
162 to2 = to_Strings + to1;
163 from = ext_Booleans + ext_Numbers;
164 EXTEND_NUM(num_Strings, ext_Strings);
165 TYPE_REALLOC(char *, to->num_Strings, to->Strings);
166 for (n = to->ext_Strings - 1,
168 base = to->num_Strings - (m + 1); m >= 0; m--) {
169 if (find_name(to->ext_Names, to1, to2, ext_Names[m + from])) {
170 to->Strings[base + m] = to->Strings[base + n--];
172 to->Strings[base + m] = ABSENT_STRING;
175 to->ext_Strings = UShort(ext_Strings);
180 * Returns the first index in ext_Names[] for the given token-type
183 _nc_first_ext_name(TERMTYPE2 *tp, int token_type)
187 switch (token_type) {
192 first = tp->ext_Booleans;
195 first = (unsigned) (tp->ext_Booleans + tp->ext_Numbers);
205 * Returns the last index in ext_Names[] for the given token-type
208 _nc_last_ext_name(TERMTYPE2 *tp, int token_type)
212 switch (token_type) {
214 last = tp->ext_Booleans;
217 last = (unsigned) (tp->ext_Booleans + tp->ext_Numbers);
221 last = NUM_EXT_NAMES(tp);
228 * Lookup an entry from extended-names, returning -1 if not found
231 _nc_find_ext_name(TERMTYPE2 *tp, char *name, int token_type)
234 unsigned first = _nc_first_ext_name(tp, token_type);
235 unsigned last = _nc_last_ext_name(tp, token_type);
237 for (j = first; j < last; j++) {
238 if (!strcmp(name, tp->ext_Names[j])) {
246 * Translate an index into ext_Names[] into the corresponding index into data
247 * (e.g., Booleans[]).
250 _nc_ext_data_index(TERMTYPE2 *tp, int n, int token_type)
252 switch (token_type) {
254 n += (tp->num_Booleans - tp->ext_Booleans);
257 n += (tp->num_Numbers - tp->ext_Numbers)
258 - (tp->ext_Booleans);
262 n += (tp->num_Strings - tp->ext_Strings)
263 - (tp->ext_Booleans + tp->ext_Numbers);
269 * Adjust tables to remove (not free) an extended name and its corresponding
273 _nc_del_ext_name(TERMTYPE2 *tp, char *name, int token_type)
277 if ((first = _nc_find_ext_name(tp, name, token_type)) >= 0) {
279 int last = (int) NUM_EXT_NAMES(tp) - 1;
281 for (j = first; j < last; j++) {
282 tp->ext_Names[j] = tp->ext_Names[j + 1];
284 first = _nc_ext_data_index(tp, first, token_type);
285 switch (token_type) {
287 last = tp->num_Booleans - 1;
288 for (j = first; j < last; j++)
289 tp->Booleans[j] = tp->Booleans[j + 1];
294 last = tp->num_Numbers - 1;
295 for (j = first; j < last; j++)
296 tp->Numbers[j] = tp->Numbers[j + 1];
301 last = tp->num_Strings - 1;
302 for (j = first; j < last; j++)
303 tp->Strings[j] = tp->Strings[j + 1];
314 * Adjust tables to insert an extended name, making room for new data. The
315 * index into the corresponding data array is returned.
318 _nc_ins_ext_name(TERMTYPE2 *tp, char *name, int token_type)
320 unsigned first = _nc_first_ext_name(tp, token_type);
321 unsigned last = _nc_last_ext_name(tp, token_type);
322 unsigned total = NUM_EXT_NAMES(tp) + 1;
325 for (j = first; j < last; j++) {
326 int cmp = strcmp(name, tp->ext_Names[j]);
328 /* already present */
329 return _nc_ext_data_index(tp, (int) j, token_type);
335 TYPE_REALLOC(char *, total, tp->ext_Names);
336 for (k = total - 1; k > j; k--)
337 tp->ext_Names[k] = tp->ext_Names[k - 1];
338 tp->ext_Names[j] = name;
339 j = (unsigned) _nc_ext_data_index(tp, (int) j, token_type);
341 switch (token_type) {
345 TYPE_REALLOC(NCURSES_SBOOL, tp->num_Booleans, tp->Booleans);
346 for (k = (unsigned) (tp->num_Booleans - 1); k > j; k--)
347 tp->Booleans[k] = tp->Booleans[k - 1];
352 TYPE_REALLOC(NCURSES_INT2, tp->num_Numbers, tp->Numbers);
353 for (k = (unsigned) (tp->num_Numbers - 1); k > j; k--)
354 tp->Numbers[k] = tp->Numbers[k - 1];
359 TYPE_REALLOC(char *, tp->num_Strings, tp->Strings);
360 for (k = (unsigned) (tp->num_Strings - 1); k > j; k--)
361 tp->Strings[k] = tp->Strings[k - 1];
368 * Look for strings that are marked cancelled, which happen to be the same name
369 * as a boolean or number. We'll get this as a special case when we get a
370 * cancellation of a name that is inherited from another entry.
373 adjust_cancels(TERMTYPE2 *to, TERMTYPE2 *from)
375 int first = to->ext_Booleans + to->ext_Numbers;
376 int last = first + to->ext_Strings;
379 for (j = first; j < last;) {
380 char *name = to->ext_Names[j];
381 int j_str = to->num_Strings - first - to->ext_Strings;
383 if (to->Strings[j + j_str] == CANCELLED_STRING) {
384 if (_nc_find_ext_name(from, to->ext_Names[j], BOOLEAN) >= 0) {
385 if (_nc_del_ext_name(to, name, STRING)
386 || _nc_del_ext_name(to, name, NUMBER)) {
387 k = _nc_ins_ext_name(to, name, BOOLEAN);
388 to->Booleans[k] = FALSE;
392 } else if (_nc_find_ext_name(from, to->ext_Names[j], NUMBER) >= 0) {
393 if (_nc_del_ext_name(to, name, STRING)
394 || _nc_del_ext_name(to, name, BOOLEAN)) {
395 k = _nc_ins_ext_name(to, name, NUMBER);
396 to->Numbers[k] = CANCELLED_NUMERIC;
400 } else if (_nc_find_ext_name(from, to->ext_Names[j], STRING) >= 0) {
401 if (_nc_del_ext_name(to, name, NUMBER)
402 || _nc_del_ext_name(to, name, BOOLEAN)) {
403 k = _nc_ins_ext_name(to, name, STRING);
404 to->Strings[k] = CANCELLED_STRING;
418 _nc_align_termtype(TERMTYPE2 *to, TERMTYPE2 *from)
424 na = to ? ((int) NUM_EXT_NAMES(to)) : 0;
425 nb = from ? ((int) NUM_EXT_NAMES(from)) : 0;
427 DEBUG(2, ("align_termtype to(%d:%s), from(%d:%s)",
428 na, to ? NonNull(to->term_names) : "?",
429 nb, from ? NonNull(from->term_names) : "?"));
431 if (na != 0 || nb != 0) {
432 int ext_Booleans, ext_Numbers, ext_Strings;
433 bool used_ext_Names = FALSE;
435 if ((na == nb) /* check if the arrays are equivalent */
436 &&(to->ext_Booleans == from->ext_Booleans)
437 && (to->ext_Numbers == from->ext_Numbers)
438 && (to->ext_Strings == from->ext_Strings)) {
442 for (n = 0, same = TRUE; n < na; n++) {
443 if (strcmp(to->ext_Names[n], from->ext_Names[n])) {
452 * This is where we pay for having a simple extension representation.
453 * Allocate a new ext_Names array and merge the two ext_Names arrays
454 * into it, updating to's counts for booleans, etc. Fortunately we do
455 * this only for the terminfo compiler (tic) and comparer (infocmp).
457 TYPE_MALLOC(char *, (size_t)(na + nb), ext_Names);
459 if (to->ext_Strings && (from->ext_Booleans + from->ext_Numbers))
460 adjust_cancels(to, from);
462 if (from->ext_Strings && (to->ext_Booleans + to->ext_Numbers))
463 adjust_cancels(from, to);
465 ext_Booleans = merge_names(ext_Names,
470 ext_Numbers = merge_names(ext_Names + ext_Booleans,
475 + from->ext_Booleans,
477 ext_Strings = merge_names(ext_Names + ext_Numbers + ext_Booleans,
487 * Now we must reallocate the Booleans, etc., to allow the data to be
490 if (na != (ext_Booleans + ext_Numbers + ext_Strings)) {
491 realign_data(to, ext_Names, ext_Booleans, ext_Numbers, ext_Strings);
492 FreeIfNeeded(to->ext_Names);
493 to->ext_Names = ext_Names;
494 DEBUG(2, ("realigned %d extended names for '%s' (to)",
495 NUM_EXT_NAMES(to), to->term_names));
496 used_ext_Names = TRUE;
498 if (nb != (ext_Booleans + ext_Numbers + ext_Strings)) {
499 nb = (ext_Booleans + ext_Numbers + ext_Strings);
500 realign_data(from, ext_Names, ext_Booleans, ext_Numbers, ext_Strings);
501 TYPE_REALLOC(char *, (size_t) nb, from->ext_Names);
502 memcpy(from->ext_Names, ext_Names, sizeof(char *) * (size_t) nb);
503 DEBUG(2, ("realigned %d extended names for '%s' (from)",
504 NUM_EXT_NAMES(from), from->term_names));
516 * TERMTYPE and TERMTYPE2 differ only with regard to the values in Numbers.
517 * Use 'mode' to decide which to use.
520 copy_termtype(TERMTYPE2 *dst, const TERMTYPE2 *src, int mode)
522 #if NCURSES_XNAMES || NCURSES_EXT_NUMBERS
525 #if NCURSES_EXT_NUMBERS
530 DEBUG(2, ("copy_termtype"));
531 *dst = *src; /* ...to copy the sizes and string-tables */
533 TYPE_MALLOC(NCURSES_SBOOL, NUM_BOOLEANS(dst), dst->Booleans);
534 TYPE_MALLOC(char *, NUM_STRINGS(dst), dst->Strings);
536 memcpy(dst->Booleans,
538 NUM_BOOLEANS(dst) * sizeof(dst->Booleans[0]));
541 NUM_STRINGS(dst) * sizeof(dst->Strings[0]));
543 #if NCURSES_EXT_NUMBERS
544 if ((mode & dstINT) == 0) {
545 DEBUG(2, ("...convert int ->short"));
546 TYPE_MALLOC(short, NUM_NUMBERS(dst), oldptr);
547 ((TERMTYPE *) dst)->Numbers = oldptr;
549 DEBUG(2, ("...copy without changing size"));
550 TYPE_MALLOC(int, NUM_NUMBERS(dst), newptr);
551 dst->Numbers = newptr;
553 if ((mode == srcINT) && (oldptr != 0)) {
554 DEBUG(2, ("...copy int ->short"));
555 for (i = 0; i < NUM_NUMBERS(dst); ++i) {
556 if (src->Numbers[i] > MAX_OF_TYPE(short)) {
557 oldptr[i] = MAX_OF_TYPE(short);
559 oldptr[i] = (short) src->Numbers[i];
562 } else if ((mode == dstINT) && (newptr != 0)) {
563 DEBUG(2, ("...copy short ->int"));
564 for (i = 0; i < NUM_NUMBERS(dst); ++i) {
565 newptr[i] = ((const short *) (src->Numbers))[i];
568 DEBUG(2, ("...copy %s without change",
574 NUM_NUMBERS(dst) * ((mode & dstINT)
580 TYPE_MALLOC(short, NUM_NUMBERS(dst), dst->Numbers);
583 NUM_NUMBERS(dst) * sizeof(dst->Numbers[0]));
586 /* FIXME: we probably should also copy str_table and ext_str_table,
587 * but tic and infocmp are not written to exploit that (yet).
591 if ((i = NUM_EXT_NAMES(src)) != 0) {
592 TYPE_MALLOC(char *, i, dst->ext_Names);
593 memcpy(dst->ext_Names, src->ext_Names, i * sizeof(char *));
601 * This entrypoint is used by tack.
604 _nc_copy_termtype(TERMTYPE *dst, const TERMTYPE *src)
606 DEBUG(2, ("_nc_copy_termtype..."));
607 copy_termtype((TERMTYPE2 *) dst, (const TERMTYPE2 *) src, 0);
610 #if NCURSES_EXT_NUMBERS
612 _nc_copy_termtype2(TERMTYPE2 *dst, const TERMTYPE2 *src)
614 DEBUG(2, ("_nc_copy_termtype2..."));
615 copy_termtype(dst, src, srcINT | dstINT);
619 * Use this for exporting the internal TERMTYPE2 to the legacy format used via
620 * the CUR macro by applications.
623 _nc_export_termtype2(TERMTYPE *dst, const TERMTYPE2 *src)
625 DEBUG(2, ("_nc_export_termtype2..."));
626 copy_termtype((TERMTYPE2 *) dst, src, srcINT);
628 #endif /* NCURSES_EXT_NUMBERS */