1 /****************************************************************************
2 * Copyright (c) 1999-2017,2018 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.30 2018/04/14 19:24:54 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);
64 } else if (cmp == 0) {
76 DEBUG(4, ("merge_names -> %d", n));
81 find_name(char **table, int length, char *name)
83 while (length-- > 0) {
84 if (!strcmp(*table++, name)) {
85 DEBUG(4, ("found name '%s'", name));
89 DEBUG(4, ("did not find name '%s'", name));
93 #define EXTEND_NUM(num, ext) \
94 to->num = (unsigned short) (to->num + (ext - to->ext))
97 realign_data(TERMTYPE2 *to, char **ext_Names,
103 int limit = (to->ext_Booleans + to->ext_Numbers + to->ext_Strings);
105 if (to->ext_Booleans != ext_Booleans) {
106 EXTEND_NUM(num_Booleans, ext_Booleans);
107 TYPE_REALLOC(NCURSES_SBOOL, to->num_Booleans, to->Booleans);
108 for (n = to->ext_Booleans - 1,
109 m = ext_Booleans - 1,
110 base = to->num_Booleans - (m + 1); m >= 0; m--) {
111 if (find_name(to->ext_Names, limit, ext_Names[m])) {
112 to->Booleans[base + m] = to->Booleans[base + n--];
114 to->Booleans[base + m] = FALSE;
117 to->ext_Booleans = UShort(ext_Booleans);
120 if (to->ext_Numbers != ext_Numbers) {
121 EXTEND_NUM(num_Numbers, ext_Numbers);
122 TYPE_REALLOC(NCURSES_INT2, to->num_Numbers, to->Numbers);
123 for (n = to->ext_Numbers - 1,
125 base = to->num_Numbers - (m + 1); m >= 0; m--) {
126 if (find_name(to->ext_Names, limit, ext_Names[m + ext_Booleans])) {
127 to->Numbers[base + m] = to->Numbers[base + n--];
129 to->Numbers[base + m] = ABSENT_NUMERIC;
132 to->ext_Numbers = UShort(ext_Numbers);
134 if (to->ext_Strings != ext_Strings) {
135 EXTEND_NUM(num_Strings, ext_Strings);
136 TYPE_REALLOC(char *, to->num_Strings, to->Strings);
137 for (n = to->ext_Strings - 1,
139 base = to->num_Strings - (m + 1); m >= 0; m--) {
140 if (find_name(to->ext_Names, limit, ext_Names[m + ext_Booleans + ext_Numbers])) {
141 to->Strings[base + m] = to->Strings[base + n--];
143 to->Strings[base + m] = ABSENT_STRING;
146 to->ext_Strings = UShort(ext_Strings);
151 * Returns the first index in ext_Names[] for the given token-type
154 _nc_first_ext_name(TERMTYPE2 *tp, int token_type)
158 switch (token_type) {
163 first = tp->ext_Booleans;
166 first = (unsigned) (tp->ext_Booleans + tp->ext_Numbers);
176 * Returns the last index in ext_Names[] for the given token-type
179 _nc_last_ext_name(TERMTYPE2 *tp, int token_type)
183 switch (token_type) {
185 last = tp->ext_Booleans;
188 last = (unsigned) (tp->ext_Booleans + tp->ext_Numbers);
192 last = NUM_EXT_NAMES(tp);
199 * Lookup an entry from extended-names, returning -1 if not found
202 _nc_find_ext_name(TERMTYPE2 *tp, char *name, int token_type)
205 unsigned first = _nc_first_ext_name(tp, token_type);
206 unsigned last = _nc_last_ext_name(tp, token_type);
208 for (j = first; j < last; j++) {
209 if (!strcmp(name, tp->ext_Names[j])) {
217 * Translate an index into ext_Names[] into the corresponding index into data
218 * (e.g., Booleans[]).
221 _nc_ext_data_index(TERMTYPE2 *tp, int n, int token_type)
223 switch (token_type) {
225 n += (tp->num_Booleans - tp->ext_Booleans);
228 n += (tp->num_Numbers - tp->ext_Numbers)
229 - (tp->ext_Booleans);
233 n += (tp->num_Strings - tp->ext_Strings)
234 - (tp->ext_Booleans + tp->ext_Numbers);
240 * Adjust tables to remove (not free) an extended name and its corresponding
244 _nc_del_ext_name(TERMTYPE2 *tp, char *name, int token_type)
248 if ((first = _nc_find_ext_name(tp, name, token_type)) >= 0) {
250 int last = (int) NUM_EXT_NAMES(tp) - 1;
252 for (j = first; j < last; j++) {
253 tp->ext_Names[j] = tp->ext_Names[j + 1];
255 first = _nc_ext_data_index(tp, first, token_type);
256 switch (token_type) {
258 last = tp->num_Booleans - 1;
259 for (j = first; j < last; j++)
260 tp->Booleans[j] = tp->Booleans[j + 1];
265 last = tp->num_Numbers - 1;
266 for (j = first; j < last; j++)
267 tp->Numbers[j] = tp->Numbers[j + 1];
272 last = tp->num_Strings - 1;
273 for (j = first; j < last; j++)
274 tp->Strings[j] = tp->Strings[j + 1];
285 * Adjust tables to insert an extended name, making room for new data. The
286 * index into the corresponding data array is returned.
289 _nc_ins_ext_name(TERMTYPE2 *tp, char *name, int token_type)
291 unsigned first = _nc_first_ext_name(tp, token_type);
292 unsigned last = _nc_last_ext_name(tp, token_type);
293 unsigned total = NUM_EXT_NAMES(tp) + 1;
296 for (j = first; j < last; j++) {
297 int cmp = strcmp(name, tp->ext_Names[j]);
299 /* already present */
300 return _nc_ext_data_index(tp, (int) j, token_type);
306 TYPE_REALLOC(char *, total, tp->ext_Names);
307 for (k = total - 1; k > j; k--)
308 tp->ext_Names[k] = tp->ext_Names[k - 1];
309 tp->ext_Names[j] = name;
310 j = (unsigned) _nc_ext_data_index(tp, (int) j, token_type);
312 switch (token_type) {
316 TYPE_REALLOC(NCURSES_SBOOL, tp->num_Booleans, tp->Booleans);
317 for (k = (unsigned) (tp->num_Booleans - 1); k > j; k--)
318 tp->Booleans[k] = tp->Booleans[k - 1];
323 TYPE_REALLOC(NCURSES_INT2, tp->num_Numbers, tp->Numbers);
324 for (k = (unsigned) (tp->num_Numbers - 1); k > j; k--)
325 tp->Numbers[k] = tp->Numbers[k - 1];
330 TYPE_REALLOC(char *, tp->num_Strings, tp->Strings);
331 for (k = (unsigned) (tp->num_Strings - 1); k > j; k--)
332 tp->Strings[k] = tp->Strings[k - 1];
339 * Look for strings that are marked cancelled, which happen to be the same name
340 * as a boolean or number. We'll get this as a special case when we get a
341 * cancellation of a name that is inherited from another entry.
344 adjust_cancels(TERMTYPE2 *to, TERMTYPE2 *from)
346 int first = to->ext_Booleans + to->ext_Numbers;
347 int last = first + to->ext_Strings;
350 for (j = first; j < last;) {
351 char *name = to->ext_Names[j];
352 int j_str = to->num_Strings - first - to->ext_Strings;
354 if (to->Strings[j + j_str] == CANCELLED_STRING) {
355 if (_nc_find_ext_name(from, to->ext_Names[j], BOOLEAN) >= 0) {
356 if (_nc_del_ext_name(to, name, STRING)
357 || _nc_del_ext_name(to, name, NUMBER)) {
358 k = _nc_ins_ext_name(to, name, BOOLEAN);
359 to->Booleans[k] = FALSE;
363 } else if (_nc_find_ext_name(from, to->ext_Names[j], NUMBER) >= 0) {
364 if (_nc_del_ext_name(to, name, STRING)
365 || _nc_del_ext_name(to, name, BOOLEAN)) {
366 k = _nc_ins_ext_name(to, name, NUMBER);
367 to->Numbers[k] = CANCELLED_NUMERIC;
371 } else if (_nc_find_ext_name(from, to->ext_Names[j], STRING) >= 0) {
372 if (_nc_del_ext_name(to, name, NUMBER)
373 || _nc_del_ext_name(to, name, BOOLEAN)) {
374 k = _nc_ins_ext_name(to, name, STRING);
375 to->Strings[k] = CANCELLED_STRING;
389 _nc_align_termtype(TERMTYPE2 *to, TERMTYPE2 *from)
395 na = to ? ((int) NUM_EXT_NAMES(to)) : 0;
396 nb = from ? ((int) NUM_EXT_NAMES(from)) : 0;
398 DEBUG(2, ("align_termtype to(%d:%s), from(%d:%s)",
399 na, to ? NonNull(to->term_names) : "?",
400 nb, from ? NonNull(from->term_names) : "?"));
402 if (na != 0 || nb != 0) {
403 int ext_Booleans, ext_Numbers, ext_Strings;
404 bool used_ext_Names = FALSE;
406 if ((na == nb) /* check if the arrays are equivalent */
407 &&(to->ext_Booleans == from->ext_Booleans)
408 && (to->ext_Numbers == from->ext_Numbers)
409 && (to->ext_Strings == from->ext_Strings)) {
413 for (n = 0, same = TRUE; n < na; n++) {
414 if (strcmp(to->ext_Names[n], from->ext_Names[n])) {
423 * This is where we pay for having a simple extension representation.
424 * Allocate a new ext_Names array and merge the two ext_Names arrays
425 * into it, updating to's counts for booleans, etc. Fortunately we do
426 * this only for the terminfo compiler (tic) and comparer (infocmp).
428 TYPE_MALLOC(char *, (size_t)(na + nb), ext_Names);
430 if (to->ext_Strings && (from->ext_Booleans + from->ext_Numbers))
431 adjust_cancels(to, from);
433 if (from->ext_Strings && (to->ext_Booleans + to->ext_Numbers))
434 adjust_cancels(from, to);
436 ext_Booleans = merge_names(ext_Names,
441 ext_Numbers = merge_names(ext_Names + ext_Booleans,
446 + from->ext_Booleans,
448 ext_Strings = merge_names(ext_Names + ext_Numbers + ext_Booleans,
458 * Now we must reallocate the Booleans, etc., to allow the data to be
461 if (na != (ext_Booleans + ext_Numbers + ext_Strings)) {
462 realign_data(to, ext_Names, ext_Booleans, ext_Numbers, ext_Strings);
463 FreeIfNeeded(to->ext_Names);
464 to->ext_Names = ext_Names;
465 DEBUG(2, ("realigned %d extended names for '%s' (to)",
466 NUM_EXT_NAMES(to), to->term_names));
467 used_ext_Names = TRUE;
469 if (nb != (ext_Booleans + ext_Numbers + ext_Strings)) {
470 nb = (ext_Booleans + ext_Numbers + ext_Strings);
471 realign_data(from, ext_Names, ext_Booleans, ext_Numbers, ext_Strings);
472 TYPE_REALLOC(char *, (size_t) nb, from->ext_Names);
473 memcpy(from->ext_Names, ext_Names, sizeof(char *) * (size_t) nb);
474 DEBUG(2, ("realigned %d extended names for '%s' (from)",
475 NUM_EXT_NAMES(from), from->term_names));
487 * TERMTYPE and TERMTYPE2 differ only with regard to the values in Numbers.
488 * Use 'mode' to decide which to use.
491 copy_termtype(TERMTYPE2 *dst, const TERMTYPE2 *src, int mode)
493 #if NCURSES_XNAMES || NCURSES_EXT_NUMBERS
496 #if NCURSES_EXT_NUMBERS
501 DEBUG(2, ("copy_termtype"));
502 *dst = *src; /* ...to copy the sizes and string-tables */
504 TYPE_MALLOC(NCURSES_SBOOL, NUM_BOOLEANS(dst), dst->Booleans);
505 TYPE_MALLOC(char *, NUM_STRINGS(dst), dst->Strings);
507 memcpy(dst->Booleans,
509 NUM_BOOLEANS(dst) * sizeof(dst->Booleans[0]));
512 NUM_STRINGS(dst) * sizeof(dst->Strings[0]));
514 #if NCURSES_EXT_NUMBERS
515 if ((mode & dstINT) == 0) {
516 DEBUG(2, ("...convert int ->short"));
517 TYPE_MALLOC(short, NUM_NUMBERS(dst), oldptr);
518 ((TERMTYPE *) dst)->Numbers = oldptr;
520 DEBUG(2, ("...copy without changing size"));
521 TYPE_MALLOC(int, NUM_NUMBERS(dst), newptr);
522 dst->Numbers = newptr;
524 if ((mode == srcINT) && (oldptr != 0)) {
525 DEBUG(2, ("...copy int ->short"));
526 for (i = 0; i < NUM_NUMBERS(dst); ++i) {
527 if (src->Numbers[i] > MAX_OF_TYPE(short)) {
528 oldptr[i] = MAX_OF_TYPE(short);
530 oldptr[i] = (short) src->Numbers[i];
533 } else if ((mode == dstINT) && (newptr != 0)) {
534 DEBUG(2, ("...copy short ->int"));
535 for (i = 0; i < NUM_NUMBERS(dst); ++i) {
536 newptr[i] = ((const short *) (src->Numbers))[i];
539 DEBUG(2, ("...copy %s without change",
545 NUM_NUMBERS(dst) * ((mode & dstINT)
551 TYPE_MALLOC(short, NUM_NUMBERS(dst), dst->Numbers);
554 NUM_NUMBERS(dst) * sizeof(dst->Numbers[0]));
557 /* FIXME: we probably should also copy str_table and ext_str_table,
558 * but tic and infocmp are not written to exploit that (yet).
562 if ((i = NUM_EXT_NAMES(src)) != 0) {
563 TYPE_MALLOC(char *, i, dst->ext_Names);
564 memcpy(dst->ext_Names, src->ext_Names, i * sizeof(char *));
572 * This entrypoint is used by tack.
575 _nc_copy_termtype(TERMTYPE *dst, const TERMTYPE *src)
577 DEBUG(2, ("_nc_copy_termtype..."));
578 copy_termtype((TERMTYPE2 *) dst, (const TERMTYPE2 *) src, 0);
581 #if NCURSES_EXT_NUMBERS
583 _nc_copy_termtype2(TERMTYPE2 *dst, const TERMTYPE2 *src)
585 DEBUG(2, ("_nc_copy_termtype2..."));
586 copy_termtype(dst, src, srcINT | dstINT);
590 * Use this for exporting the internal TERMTYPE2 to the legacy format used via
591 * the CUR macro by applications.
594 _nc_export_termtype2(TERMTYPE *dst, const TERMTYPE2 *src)
596 DEBUG(2, ("_nc_export_termtype2..."));
597 copy_termtype((TERMTYPE2 *) dst, src, srcINT);
599 #endif /* NCURSES_EXT_NUMBERS */