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