]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/alloc_ttype.c
ncurses 6.1 - patch 20190728
[ncurses.git] / ncurses / tinfo / alloc_ttype.c
1 /****************************************************************************
2  * Copyright (c) 1999-2018,2019 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Thomas E. Dickey <dickey@clark.net> 1999-on                     *
31  ****************************************************************************/
32
33 /*
34  * align_ttype.c --  functions for TERMTYPE
35  *
36  *      _nc_align_termtype()
37  *      _nc_copy_termtype()
38  *
39  */
40
41 #include <curses.priv.h>
42
43 #include <tic.h>
44
45 MODULE_ID("$Id: alloc_ttype.c,v 1.31 2019/04/27 23:28:31 tom Exp $")
46
47 #if NCURSES_XNAMES
48 /*
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.
51  */
52 static int
53 merge_names(char **dst, char **a, int na, char **b, int nb)
54 {
55     int n = 0;
56     while (na > 0 && nb > 0) {
57         int cmp = strcmp(*a, *b);
58         if (cmp < 0) {
59             dst[n++] = *a++;
60             na--;
61         } else if (cmp > 0) {
62             dst[n++] = *b++;
63             nb--;
64         } else {
65             dst[n++] = *a;
66             a++, b++;
67             na--, nb--;
68         }
69     }
70     while (na-- > 0) {
71         dst[n++] = *a++;
72     }
73     while (nb-- > 0) {
74         dst[n++] = *b++;
75     }
76     DEBUG(4, ("merge_names -> %d", n));
77     return n;
78 }
79
80 static bool
81 find_name(char **table, int item, int length, const char *name)
82 {
83     int n;
84     int result = -1;
85
86     for (n = item; n < length; ++n) {
87         if (!strcmp(table[n], name)) {
88             DEBUG(4, ("found name '%s' @%d", name, n));
89             result = n;
90             break;
91         }
92     }
93     if (result < 0) {
94         DEBUG(4, ("did not find name '%s'", name));
95     }
96     return (result >= 0);
97 }
98
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))
103
104 static void
105 realign_data(TERMTYPE2 *to, char **ext_Names,
106              int ext_Booleans,
107              int ext_Numbers,
108              int ext_Strings)
109 {
110     int n, m, base;
111     int to_Booleans = to->ext_Booleans;
112     int to_Numbers = to->ext_Numbers;
113     int to_Strings = to->ext_Strings;
114     int to1, to2, from;
115
116     DEBUG(4, ("realign_data %d/%d/%d vs %d/%d/%d",
117               ext_Booleans,
118               ext_Numbers,
119               ext_Strings,
120               to->ext_Booleans,
121               to->ext_Numbers,
122               to->ext_Strings));
123
124     if (to->ext_Booleans != ext_Booleans) {
125         to1 = 0;
126         to2 = to_Booleans + to1;
127         from = 0;
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--];
135             } else {
136                 to->Booleans[base + m] = FALSE;
137             }
138         }
139         to->ext_Booleans = UShort(ext_Booleans);
140     }
141
142     if (to->ext_Numbers != ext_Numbers) {
143         to1 = to_Booleans;
144         to2 = to_Numbers + to1;
145         from = ext_Booleans;
146         EXTEND_NUM(num_Numbers, ext_Numbers);
147         TYPE_REALLOC(NCURSES_INT2, to->num_Numbers, to->Numbers);
148         for (n = to->ext_Numbers - 1,
149              m = 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--];
153             } else {
154                 to->Numbers[base + m] = ABSENT_NUMERIC;
155             }
156         }
157         to->ext_Numbers = UShort(ext_Numbers);
158     }
159
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,
167              m = 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--];
171             } else {
172                 to->Strings[base + m] = ABSENT_STRING;
173             }
174         }
175         to->ext_Strings = UShort(ext_Strings);
176     }
177 }
178
179 /*
180  * Returns the first index in ext_Names[] for the given token-type
181  */
182 static unsigned
183 _nc_first_ext_name(TERMTYPE2 *tp, int token_type)
184 {
185     unsigned first;
186
187     switch (token_type) {
188     case BOOLEAN:
189         first = 0;
190         break;
191     case NUMBER:
192         first = tp->ext_Booleans;
193         break;
194     case STRING:
195         first = (unsigned) (tp->ext_Booleans + tp->ext_Numbers);
196         break;
197     default:
198         first = 0;
199         break;
200     }
201     return first;
202 }
203
204 /*
205  * Returns the last index in ext_Names[] for the given token-type
206  */
207 static unsigned
208 _nc_last_ext_name(TERMTYPE2 *tp, int token_type)
209 {
210     unsigned last;
211
212     switch (token_type) {
213     case BOOLEAN:
214         last = tp->ext_Booleans;
215         break;
216     case NUMBER:
217         last = (unsigned) (tp->ext_Booleans + tp->ext_Numbers);
218         break;
219     default:
220     case STRING:
221         last = NUM_EXT_NAMES(tp);
222         break;
223     }
224     return last;
225 }
226
227 /*
228  * Lookup an entry from extended-names, returning -1 if not found
229  */
230 static int
231 _nc_find_ext_name(TERMTYPE2 *tp, char *name, int token_type)
232 {
233     unsigned j;
234     unsigned first = _nc_first_ext_name(tp, token_type);
235     unsigned last = _nc_last_ext_name(tp, token_type);
236
237     for (j = first; j < last; j++) {
238         if (!strcmp(name, tp->ext_Names[j])) {
239             return (int) j;
240         }
241     }
242     return -1;
243 }
244
245 /*
246  * Translate an index into ext_Names[] into the corresponding index into data
247  * (e.g., Booleans[]).
248  */
249 static int
250 _nc_ext_data_index(TERMTYPE2 *tp, int n, int token_type)
251 {
252     switch (token_type) {
253     case BOOLEAN:
254         n += (tp->num_Booleans - tp->ext_Booleans);
255         break;
256     case NUMBER:
257         n += (tp->num_Numbers - tp->ext_Numbers)
258             - (tp->ext_Booleans);
259         break;
260     default:
261     case STRING:
262         n += (tp->num_Strings - tp->ext_Strings)
263             - (tp->ext_Booleans + tp->ext_Numbers);
264     }
265     return n;
266 }
267
268 /*
269  * Adjust tables to remove (not free) an extended name and its corresponding
270  * data.
271  */
272 static bool
273 _nc_del_ext_name(TERMTYPE2 *tp, char *name, int token_type)
274 {
275     int first;
276
277     if ((first = _nc_find_ext_name(tp, name, token_type)) >= 0) {
278         int j;
279         int last = (int) NUM_EXT_NAMES(tp) - 1;
280
281         for (j = first; j < last; j++) {
282             tp->ext_Names[j] = tp->ext_Names[j + 1];
283         }
284         first = _nc_ext_data_index(tp, first, token_type);
285         switch (token_type) {
286         case BOOLEAN:
287             last = tp->num_Booleans - 1;
288             for (j = first; j < last; j++)
289                 tp->Booleans[j] = tp->Booleans[j + 1];
290             tp->ext_Booleans--;
291             tp->num_Booleans--;
292             break;
293         case NUMBER:
294             last = tp->num_Numbers - 1;
295             for (j = first; j < last; j++)
296                 tp->Numbers[j] = tp->Numbers[j + 1];
297             tp->ext_Numbers--;
298             tp->num_Numbers--;
299             break;
300         case STRING:
301             last = tp->num_Strings - 1;
302             for (j = first; j < last; j++)
303                 tp->Strings[j] = tp->Strings[j + 1];
304             tp->ext_Strings--;
305             tp->num_Strings--;
306             break;
307         }
308         return TRUE;
309     }
310     return FALSE;
311 }
312
313 /*
314  * Adjust tables to insert an extended name, making room for new data.  The
315  * index into the corresponding data array is returned.
316  */
317 static int
318 _nc_ins_ext_name(TERMTYPE2 *tp, char *name, int token_type)
319 {
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;
323     unsigned j, k;
324
325     for (j = first; j < last; j++) {
326         int cmp = strcmp(name, tp->ext_Names[j]);
327         if (cmp == 0)
328             /* already present */
329             return _nc_ext_data_index(tp, (int) j, token_type);
330         if (cmp < 0) {
331             break;
332         }
333     }
334
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);
340
341     switch (token_type) {
342     case BOOLEAN:
343         tp->ext_Booleans++;
344         tp->num_Booleans++;
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];
348         break;
349     case NUMBER:
350         tp->ext_Numbers++;
351         tp->num_Numbers++;
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];
355         break;
356     case STRING:
357         tp->ext_Strings++;
358         tp->num_Strings++;
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];
362         break;
363     }
364     return (int) j;
365 }
366
367 /*
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.
371  */
372 static void
373 adjust_cancels(TERMTYPE2 *to, TERMTYPE2 *from)
374 {
375     int first = to->ext_Booleans + to->ext_Numbers;
376     int last = first + to->ext_Strings;
377     int j, k;
378
379     for (j = first; j < last;) {
380         char *name = to->ext_Names[j];
381         int j_str = to->num_Strings - first - to->ext_Strings;
382
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;
389                 } else {
390                     j++;
391                 }
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;
397                 } else {
398                     j++;
399                 }
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;
405                 } else {
406                     j++;
407                 }
408             } else {
409                 j++;
410             }
411         } else {
412             j++;
413         }
414     }
415 }
416
417 NCURSES_EXPORT(void)
418 _nc_align_termtype(TERMTYPE2 *to, TERMTYPE2 *from)
419 {
420     int na;
421     int nb;
422     char **ext_Names;
423
424     na = to ? ((int) NUM_EXT_NAMES(to)) : 0;
425     nb = from ? ((int) NUM_EXT_NAMES(from)) : 0;
426
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) : "?"));
430
431     if (na != 0 || nb != 0) {
432         int ext_Booleans, ext_Numbers, ext_Strings;
433         bool used_ext_Names = FALSE;
434
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)) {
439             int n;
440             bool same;
441
442             for (n = 0, same = TRUE; n < na; n++) {
443                 if (strcmp(to->ext_Names[n], from->ext_Names[n])) {
444                     same = FALSE;
445                     break;
446                 }
447             }
448             if (same)
449                 return;
450         }
451         /*
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).
456          */
457         TYPE_MALLOC(char *, (size_t)(na + nb), ext_Names);
458
459         if (to->ext_Strings && (from->ext_Booleans + from->ext_Numbers))
460             adjust_cancels(to, from);
461
462         if (from->ext_Strings && (to->ext_Booleans + to->ext_Numbers))
463             adjust_cancels(from, to);
464
465         ext_Booleans = merge_names(ext_Names,
466                                    to->ext_Names,
467                                    to->ext_Booleans,
468                                    from->ext_Names,
469                                    from->ext_Booleans);
470         ext_Numbers = merge_names(ext_Names + ext_Booleans,
471                                   to->ext_Names
472                                   + to->ext_Booleans,
473                                   to->ext_Numbers,
474                                   from->ext_Names
475                                   + from->ext_Booleans,
476                                   from->ext_Numbers);
477         ext_Strings = merge_names(ext_Names + ext_Numbers + ext_Booleans,
478                                   to->ext_Names
479                                   + to->ext_Booleans
480                                   + to->ext_Numbers,
481                                   to->ext_Strings,
482                                   from->ext_Names
483                                   + from->ext_Booleans
484                                   + from->ext_Numbers,
485                                   from->ext_Strings);
486         /*
487          * Now we must reallocate the Booleans, etc., to allow the data to be
488          * overlaid.
489          */
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;
497         }
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));
505         }
506         if (!used_ext_Names)
507             free(ext_Names);
508     }
509 }
510 #endif
511
512 #define srcINT 1
513 #define dstINT 2
514
515 /*
516  * TERMTYPE and TERMTYPE2 differ only with regard to the values in Numbers.
517  * Use 'mode' to decide which to use.
518  */
519 static void
520 copy_termtype(TERMTYPE2 *dst, const TERMTYPE2 *src, int mode)
521 {
522 #if NCURSES_XNAMES || NCURSES_EXT_NUMBERS
523     unsigned i;
524 #endif
525 #if NCURSES_EXT_NUMBERS
526     short *oldptr = 0;
527     int *newptr = 0;
528 #endif
529
530     DEBUG(2, ("copy_termtype"));
531     *dst = *src;                /* ...to copy the sizes and string-tables */
532
533     TYPE_MALLOC(NCURSES_SBOOL, NUM_BOOLEANS(dst), dst->Booleans);
534     TYPE_MALLOC(char *, NUM_STRINGS(dst), dst->Strings);
535
536     memcpy(dst->Booleans,
537            src->Booleans,
538            NUM_BOOLEANS(dst) * sizeof(dst->Booleans[0]));
539     memcpy(dst->Strings,
540            src->Strings,
541            NUM_STRINGS(dst) * sizeof(dst->Strings[0]));
542
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;
548     } else {
549         DEBUG(2, ("...copy without changing size"));
550         TYPE_MALLOC(int, NUM_NUMBERS(dst), newptr);
551         dst->Numbers = newptr;
552     }
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);
558             } else {
559                 oldptr[i] = (short) src->Numbers[i];
560             }
561         }
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];
566         }
567     } else {
568         DEBUG(2, ("...copy %s without change",
569                   (mode & dstINT)
570                   ? "int"
571                   : "short"));
572         memcpy(dst->Numbers,
573                src->Numbers,
574                NUM_NUMBERS(dst) * ((mode & dstINT)
575                                    ? sizeof(int)
576                                    : sizeof(short)));
577     }
578 #else
579     (void) mode;
580     TYPE_MALLOC(short, NUM_NUMBERS(dst), dst->Numbers);
581     memcpy(dst->Numbers,
582            src->Numbers,
583            NUM_NUMBERS(dst) * sizeof(dst->Numbers[0]));
584 #endif
585
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).
588      */
589
590 #if NCURSES_XNAMES
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 *));
594     } else {
595         dst->ext_Names = 0;
596     }
597 #endif
598 }
599
600 /*
601  * This entrypoint is used by tack.
602  */
603 NCURSES_EXPORT(void)
604 _nc_copy_termtype(TERMTYPE *dst, const TERMTYPE *src)
605 {
606     DEBUG(2, ("_nc_copy_termtype..."));
607     copy_termtype((TERMTYPE2 *) dst, (const TERMTYPE2 *) src, 0);
608 }
609
610 #if NCURSES_EXT_NUMBERS
611 NCURSES_EXPORT(void)
612 _nc_copy_termtype2(TERMTYPE2 *dst, const TERMTYPE2 *src)
613 {
614     DEBUG(2, ("_nc_copy_termtype2..."));
615     copy_termtype(dst, src, srcINT | dstINT);
616 }
617
618 /*
619  * Use this for exporting the internal TERMTYPE2 to the legacy format used via
620  * the CUR macro by applications.
621  */
622 NCURSES_EXPORT(void)
623 _nc_export_termtype2(TERMTYPE *dst, const TERMTYPE2 *src)
624 {
625     DEBUG(2, ("_nc_export_termtype2..."));
626     copy_termtype((TERMTYPE2 *) dst, src, srcINT);
627 }
628 #endif /* NCURSES_EXT_NUMBERS */