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