]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/alloc_ttype.c
ncurses 5.9 - patch 20120922
[ncurses.git] / ncurses / tinfo / alloc_ttype.c
1 /****************************************************************************
2  * Copyright (c) 1999-2010,2012 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.24 2012/03/01 01:21:56 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 if (cmp == 0) {
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 length, char *name)
82 {
83     while (length-- > 0) {
84         if (!strcmp(*table++, name)) {
85             DEBUG(4, ("found name '%s'", name));
86             return TRUE;
87         }
88     }
89     DEBUG(4, ("did not find name '%s'", name));
90     return FALSE;
91 }
92
93 #define EXTEND_NUM(num, ext) \
94         to->num = (unsigned short) (to->num + (ext - to->ext))
95
96 static void
97 realign_data(TERMTYPE *to, char **ext_Names,
98              int ext_Booleans,
99              int ext_Numbers,
100              int ext_Strings)
101 {
102     int n, m, base;
103     int limit = (to->ext_Booleans + to->ext_Numbers + to->ext_Strings);
104
105     if (to->ext_Booleans != ext_Booleans) {
106         EXTEND_NUM(num_Booleans, ext_Booleans);
107         to->Booleans = typeRealloc(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--];
113             } else {
114                 to->Booleans[base + m] = FALSE;
115             }
116         }
117         to->ext_Booleans = UShort(ext_Booleans);
118     }
119     if (to->ext_Numbers != ext_Numbers) {
120         EXTEND_NUM(num_Numbers, ext_Numbers);
121         to->Numbers = typeRealloc(short, to->num_Numbers, to->Numbers);
122         for (n = to->ext_Numbers - 1,
123              m = ext_Numbers - 1,
124              base = to->num_Numbers - (m + 1); m >= 0; m--) {
125             if (find_name(to->ext_Names, limit, ext_Names[m + ext_Booleans])) {
126                 to->Numbers[base + m] = to->Numbers[base + n--];
127             } else {
128                 to->Numbers[base + m] = ABSENT_NUMERIC;
129             }
130         }
131         to->ext_Numbers = UShort(ext_Numbers);
132     }
133     if (to->ext_Strings != ext_Strings) {
134         EXTEND_NUM(num_Strings, ext_Strings);
135         to->Strings = typeRealloc(char *, to->num_Strings, to->Strings);
136         for (n = to->ext_Strings - 1,
137              m = ext_Strings - 1,
138              base = to->num_Strings - (m + 1); m >= 0; m--) {
139             if (find_name(to->ext_Names, limit, ext_Names[m + ext_Booleans + ext_Numbers])) {
140                 to->Strings[base + m] = to->Strings[base + n--];
141             } else {
142                 to->Strings[base + m] = ABSENT_STRING;
143             }
144         }
145         to->ext_Strings = UShort(ext_Strings);
146     }
147 }
148
149 /*
150  * Returns the first index in ext_Names[] for the given token-type
151  */
152 static unsigned
153 _nc_first_ext_name(TERMTYPE *tp, int token_type)
154 {
155     unsigned first;
156
157     switch (token_type) {
158     case BOOLEAN:
159         first = 0;
160         break;
161     case NUMBER:
162         first = tp->ext_Booleans;
163         break;
164     case STRING:
165         first = (unsigned) (tp->ext_Booleans + tp->ext_Numbers);
166         break;
167     default:
168         first = 0;
169         break;
170     }
171     return first;
172 }
173
174 /*
175  * Returns the last index in ext_Names[] for the given token-type
176  */
177 static unsigned
178 _nc_last_ext_name(TERMTYPE *tp, int token_type)
179 {
180     unsigned last;
181
182     switch (token_type) {
183     case BOOLEAN:
184         last = tp->ext_Booleans;
185         break;
186     case NUMBER:
187         last = (unsigned) (tp->ext_Booleans + tp->ext_Numbers);
188         break;
189     default:
190     case STRING:
191         last = NUM_EXT_NAMES(tp);
192         break;
193     }
194     return last;
195 }
196
197 /*
198  * Lookup an entry from extended-names, returning -1 if not found
199  */
200 static int
201 _nc_find_ext_name(TERMTYPE *tp, char *name, int token_type)
202 {
203     unsigned j;
204     unsigned first = _nc_first_ext_name(tp, token_type);
205     unsigned last = _nc_last_ext_name(tp, token_type);
206
207     for (j = first; j < last; j++) {
208         if (!strcmp(name, tp->ext_Names[j])) {
209             return (int) j;
210         }
211     }
212     return -1;
213 }
214
215 /*
216  * Translate an index into ext_Names[] into the corresponding index into data
217  * (e.g., Booleans[]).
218  */
219 static int
220 _nc_ext_data_index(TERMTYPE *tp, int n, int token_type)
221 {
222     switch (token_type) {
223     case BOOLEAN:
224         n += (tp->num_Booleans - tp->ext_Booleans);
225         break;
226     case NUMBER:
227         n += (tp->num_Numbers - tp->ext_Numbers)
228             - (tp->ext_Booleans);
229         break;
230     default:
231     case STRING:
232         n += (tp->num_Strings - tp->ext_Strings)
233             - (tp->ext_Booleans + tp->ext_Numbers);
234     }
235     return n;
236 }
237
238 /*
239  * Adjust tables to remove (not free) an extended name and its corresponding
240  * data.
241  */
242 static bool
243 _nc_del_ext_name(TERMTYPE *tp, char *name, int token_type)
244 {
245     int j;
246     int first, last;
247
248     if ((first = _nc_find_ext_name(tp, name, token_type)) >= 0) {
249         last = (int) NUM_EXT_NAMES(tp) - 1;
250         for (j = first; j < last; j++) {
251             tp->ext_Names[j] = tp->ext_Names[j + 1];
252         }
253         first = _nc_ext_data_index(tp, first, token_type);
254         switch (token_type) {
255         case BOOLEAN:
256             last = tp->num_Booleans - 1;
257             for (j = first; j < last; j++)
258                 tp->Booleans[j] = tp->Booleans[j + 1];
259             tp->ext_Booleans--;
260             tp->num_Booleans--;
261             break;
262         case NUMBER:
263             last = tp->num_Numbers - 1;
264             for (j = first; j < last; j++)
265                 tp->Numbers[j] = tp->Numbers[j + 1];
266             tp->ext_Numbers--;
267             tp->num_Numbers--;
268             break;
269         case STRING:
270             last = tp->num_Strings - 1;
271             for (j = first; j < last; j++)
272                 tp->Strings[j] = tp->Strings[j + 1];
273             tp->ext_Strings--;
274             tp->num_Strings--;
275             break;
276         }
277         return TRUE;
278     }
279     return FALSE;
280 }
281
282 /*
283  * Adjust tables to insert an extended name, making room for new data.  The
284  * index into the corresponding data array is returned.
285  */
286 static int
287 _nc_ins_ext_name(TERMTYPE *tp, char *name, int token_type)
288 {
289     unsigned first = _nc_first_ext_name(tp, token_type);
290     unsigned last = _nc_last_ext_name(tp, token_type);
291     unsigned total = NUM_EXT_NAMES(tp) + 1;
292     unsigned j, k;
293
294     for (j = first; j < last; j++) {
295         int cmp = strcmp(name, tp->ext_Names[j]);
296         if (cmp == 0)
297             /* already present */
298             return _nc_ext_data_index(tp, (int) j, token_type);
299         if (cmp < 0) {
300             break;
301         }
302     }
303
304     tp->ext_Names = typeRealloc(char *, total, tp->ext_Names);
305     for (k = total - 1; k > j; k--)
306         tp->ext_Names[k] = tp->ext_Names[k - 1];
307     tp->ext_Names[j] = name;
308     j = (unsigned) _nc_ext_data_index(tp, (int) j, token_type);
309
310     switch (token_type) {
311     case BOOLEAN:
312         tp->ext_Booleans++;
313         tp->num_Booleans++;
314         tp->Booleans = typeRealloc(NCURSES_SBOOL, tp->num_Booleans, tp->Booleans);
315         for (k = (unsigned) (tp->num_Booleans - 1); k > j; k--)
316             tp->Booleans[k] = tp->Booleans[k - 1];
317         break;
318     case NUMBER:
319         tp->ext_Numbers++;
320         tp->num_Numbers++;
321         tp->Numbers = typeRealloc(short, tp->num_Numbers, tp->Numbers);
322         for (k = (unsigned) (tp->num_Numbers - 1); k > j; k--)
323             tp->Numbers[k] = tp->Numbers[k - 1];
324         break;
325     case STRING:
326         tp->ext_Strings++;
327         tp->num_Strings++;
328         tp->Strings = typeRealloc(char *, tp->num_Strings, tp->Strings);
329         for (k = (unsigned) (tp->num_Strings - 1); k > j; k--)
330             tp->Strings[k] = tp->Strings[k - 1];
331         break;
332     }
333     return (int) j;
334 }
335
336 /*
337  * Look for strings that are marked cancelled, which happen to be the same name
338  * as a boolean or number.  We'll get this as a special case when we get a
339  * cancellation of a name that is inherited from another entry.
340  */
341 static void
342 adjust_cancels(TERMTYPE *to, TERMTYPE *from)
343 {
344     int first = to->ext_Booleans + to->ext_Numbers;
345     int last = first + to->ext_Strings;
346     int j, k;
347
348     for (j = first; j < last;) {
349         char *name = to->ext_Names[j];
350         int j_str = to->num_Strings - first - to->ext_Strings;
351
352         if (to->Strings[j + j_str] == CANCELLED_STRING) {
353             if (_nc_find_ext_name(from, to->ext_Names[j], BOOLEAN) >= 0) {
354                 if (_nc_del_ext_name(to, name, STRING)
355                     || _nc_del_ext_name(to, name, NUMBER)) {
356                     k = _nc_ins_ext_name(to, name, BOOLEAN);
357                     to->Booleans[k] = FALSE;
358                 } else {
359                     j++;
360                 }
361             } else if (_nc_find_ext_name(from, to->ext_Names[j], NUMBER) >= 0) {
362                 if (_nc_del_ext_name(to, name, STRING)
363                     || _nc_del_ext_name(to, name, BOOLEAN)) {
364                     k = _nc_ins_ext_name(to, name, NUMBER);
365                     to->Numbers[k] = CANCELLED_NUMERIC;
366                 } else {
367                     j++;
368                 }
369             } else if (_nc_find_ext_name(from, to->ext_Names[j], STRING) >= 0) {
370                 if (_nc_del_ext_name(to, name, NUMBER)
371                     || _nc_del_ext_name(to, name, BOOLEAN)) {
372                     k = _nc_ins_ext_name(to, name, STRING);
373                     to->Strings[k] = CANCELLED_STRING;
374                 } else {
375                     j++;
376                 }
377             } else {
378                 j++;
379             }
380         } else {
381             j++;
382         }
383     }
384 }
385
386 NCURSES_EXPORT(void)
387 _nc_align_termtype(TERMTYPE *to, TERMTYPE *from)
388 {
389     int na = (int) NUM_EXT_NAMES(to);
390     int nb = (int) NUM_EXT_NAMES(from);
391     int n;
392     bool same;
393     char **ext_Names;
394     int ext_Booleans, ext_Numbers, ext_Strings;
395     bool used_ext_Names = FALSE;
396
397     DEBUG(2, ("align_termtype to(%d:%s), from(%d:%s)", na, to->term_names,
398               nb, from->term_names));
399
400     if (na != 0 || nb != 0) {
401         if ((na == nb)          /* check if the arrays are equivalent */
402             &&(to->ext_Booleans == from->ext_Booleans)
403             && (to->ext_Numbers == from->ext_Numbers)
404             && (to->ext_Strings == from->ext_Strings)) {
405             for (n = 0, same = TRUE; n < na; n++) {
406                 if (strcmp(to->ext_Names[n], from->ext_Names[n])) {
407                     same = FALSE;
408                     break;
409                 }
410             }
411             if (same)
412                 return;
413         }
414         /*
415          * This is where we pay for having a simple extension representation. 
416          * Allocate a new ext_Names array and merge the two ext_Names arrays
417          * into it, updating to's counts for booleans, etc.  Fortunately we do
418          * this only for the terminfo compiler (tic) and comparer (infocmp).
419          */
420         ext_Names = typeMalloc(char *, (size_t)(na + nb));
421
422         if (to->ext_Strings && (from->ext_Booleans + from->ext_Numbers))
423             adjust_cancels(to, from);
424
425         if (from->ext_Strings && (to->ext_Booleans + to->ext_Numbers))
426             adjust_cancels(from, to);
427
428         ext_Booleans = merge_names(ext_Names,
429                                    to->ext_Names,
430                                    to->ext_Booleans,
431                                    from->ext_Names,
432                                    from->ext_Booleans);
433         ext_Numbers = merge_names(ext_Names + ext_Booleans,
434                                   to->ext_Names
435                                   + to->ext_Booleans,
436                                   to->ext_Numbers,
437                                   from->ext_Names
438                                   + from->ext_Booleans,
439                                   from->ext_Numbers);
440         ext_Strings = merge_names(ext_Names + ext_Numbers + ext_Booleans,
441                                   to->ext_Names
442                                   + to->ext_Booleans
443                                   + to->ext_Numbers,
444                                   to->ext_Strings,
445                                   from->ext_Names
446                                   + from->ext_Booleans
447                                   + from->ext_Numbers,
448                                   from->ext_Strings);
449         /*
450          * Now we must reallocate the Booleans, etc., to allow the data to be
451          * overlaid.
452          */
453         if (na != (ext_Booleans + ext_Numbers + ext_Strings)) {
454             realign_data(to, ext_Names, ext_Booleans, ext_Numbers, ext_Strings);
455             FreeIfNeeded(to->ext_Names);
456             to->ext_Names = ext_Names;
457             DEBUG(2, ("realigned %d extended names for '%s' (to)",
458                       NUM_EXT_NAMES(to), to->term_names));
459             used_ext_Names = TRUE;
460         }
461         if (nb != (ext_Booleans + ext_Numbers + ext_Strings)) {
462             nb = (ext_Booleans + ext_Numbers + ext_Strings);
463             realign_data(from, ext_Names, ext_Booleans, ext_Numbers, ext_Strings);
464             from->ext_Names = typeRealloc(char *, (size_t) nb, from->ext_Names);
465             memcpy(from->ext_Names, ext_Names, sizeof(char *) * (size_t) nb);
466             DEBUG(2, ("realigned %d extended names for '%s' (from)",
467                       NUM_EXT_NAMES(from), from->term_names));
468         }
469         if (!used_ext_Names)
470             free(ext_Names);
471     }
472 }
473 #endif
474
475 NCURSES_EXPORT(void)
476 _nc_copy_termtype(TERMTYPE *dst, const TERMTYPE *src)
477 {
478     unsigned i;
479
480     *dst = *src;                /* ...to copy the sizes and string-tables */
481     dst->Booleans = typeMalloc(NCURSES_SBOOL, NUM_BOOLEANS(dst));
482     dst->Numbers = typeMalloc(short, NUM_NUMBERS(dst));
483     dst->Strings = typeMalloc(char *, NUM_STRINGS(dst));
484
485     memcpy(dst->Booleans,
486            src->Booleans,
487            NUM_BOOLEANS(dst) * sizeof(dst->Booleans[0]));
488     memcpy(dst->Numbers,
489            src->Numbers,
490            NUM_NUMBERS(dst) * sizeof(dst->Numbers[0]));
491     memcpy(dst->Strings,
492            src->Strings,
493            NUM_STRINGS(dst) * sizeof(dst->Strings[0]));
494
495     /* FIXME: we probably should also copy str_table and ext_str_table,
496      * but tic and infocmp are not written to exploit that (yet).
497      */
498
499 #if NCURSES_XNAMES
500     if ((i = NUM_EXT_NAMES(src)) != 0) {
501         dst->ext_Names = typeMalloc(char *, i);
502         memcpy(dst->ext_Names, src->ext_Names, i * sizeof(char *));
503     } else {
504         dst->ext_Names = 0;
505     }
506 #endif
507
508 }