1 /****************************************************************************
2 * Copyright (c) 1999-2009,2010 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.19 2010/01/23 17:57:43 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));
94 realign_data(TERMTYPE *to, char **ext_Names,
100 int limit = (to->ext_Booleans + to->ext_Numbers + to->ext_Strings);
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--];
111 to->Booleans[base + m] = FALSE;
114 to->ext_Booleans = ext_Booleans;
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,
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--];
125 to->Numbers[base + m] = ABSENT_NUMERIC;
128 to->ext_Numbers = ext_Numbers;
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,
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--];
139 to->Strings[base + m] = ABSENT_STRING;
142 to->ext_Strings = ext_Strings;
147 * Returns the first index in ext_Names[] for the given token-type
150 _nc_first_ext_name(TERMTYPE *tp, int token_type)
154 switch (token_type) {
159 first = tp->ext_Booleans;
162 first = (unsigned) (tp->ext_Booleans + tp->ext_Numbers);
172 * Returns the last index in ext_Names[] for the given token-type
175 _nc_last_ext_name(TERMTYPE *tp, int token_type)
179 switch (token_type) {
181 last = tp->ext_Booleans;
184 last = (unsigned) (tp->ext_Booleans + tp->ext_Numbers);
188 last = NUM_EXT_NAMES(tp);
195 * Lookup an entry from extended-names, returning -1 if not found
198 _nc_find_ext_name(TERMTYPE *tp, char *name, int token_type)
201 unsigned first = _nc_first_ext_name(tp, token_type);
202 unsigned last = _nc_last_ext_name(tp, token_type);
204 for (j = first; j < last; j++) {
205 if (!strcmp(name, tp->ext_Names[j])) {
213 * Translate an index into ext_Names[] into the corresponding index into data
214 * (e.g., Booleans[]).
217 _nc_ext_data_index(TERMTYPE *tp, int n, int token_type)
219 switch (token_type) {
221 n += (tp->num_Booleans - tp->ext_Booleans);
224 n += (tp->num_Numbers - tp->ext_Numbers)
225 - (tp->ext_Booleans);
229 n += (tp->num_Strings - tp->ext_Strings)
230 - (tp->ext_Booleans + tp->ext_Numbers);
236 * Adjust tables to remove (not free) an extended name and its corresponding
240 _nc_del_ext_name(TERMTYPE *tp, char *name, int token_type)
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];
250 first = _nc_ext_data_index(tp, first, token_type);
251 switch (token_type) {
253 last = tp->num_Booleans - 1;
254 for (j = first; j < last; j++)
255 tp->Booleans[j] = tp->Booleans[j + 1];
260 last = tp->num_Numbers - 1;
261 for (j = first; j < last; j++)
262 tp->Numbers[j] = tp->Numbers[j + 1];
267 last = tp->num_Strings - 1;
268 for (j = first; j < last; j++)
269 tp->Strings[j] = tp->Strings[j + 1];
280 * Adjust tables to insert an extended name, making room for new data. The
281 * index into the corresponding data array is returned.
284 _nc_ins_ext_name(TERMTYPE *tp, char *name, int token_type)
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;
291 for (j = first; j < last; j++) {
292 int cmp = strcmp(name, tp->ext_Names[j]);
294 /* already present */
295 return _nc_ext_data_index(tp, (int) j, token_type);
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 = _nc_ext_data_index(tp, (int) j, token_type);
307 switch (token_type) {
311 tp->Booleans = typeRealloc(NCURSES_SBOOL, tp->num_Booleans, tp->Booleans);
312 for (k = tp->num_Booleans - 1; k > j; k--)
313 tp->Booleans[k] = tp->Booleans[k - 1];
318 tp->Numbers = typeRealloc(short, tp->num_Numbers, tp->Numbers);
319 for (k = tp->num_Numbers - 1; k > j; k--)
320 tp->Numbers[k] = tp->Numbers[k - 1];
325 tp->Strings = typeRealloc(char *, tp->num_Strings, tp->Strings);
326 for (k = tp->num_Strings - 1; k > j; k--)
327 tp->Strings[k] = tp->Strings[k - 1];
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.
339 adjust_cancels(TERMTYPE *to, TERMTYPE *from)
341 int first = to->ext_Booleans + to->ext_Numbers;
342 int last = first + to->ext_Strings;
345 for (j = first; j < last;) {
346 char *name = to->ext_Names[j];
347 int j_str = to->num_Strings - first - to->ext_Strings;
349 if (to->Strings[j + j_str] == CANCELLED_STRING) {
350 if ((k = _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;
358 } else if ((k = _nc_find_ext_name(from, to->ext_Names[j],
360 if (_nc_del_ext_name(to, name, STRING)
361 || _nc_del_ext_name(to, name, BOOLEAN)) {
362 k = _nc_ins_ext_name(to, name, NUMBER);
363 to->Numbers[k] = CANCELLED_NUMERIC;
367 } else if ((k = _nc_find_ext_name(from, to->ext_Names[j],
369 if (_nc_del_ext_name(to, name, NUMBER)
370 || _nc_del_ext_name(to, name, BOOLEAN)) {
371 k = _nc_ins_ext_name(to, name, STRING);
372 to->Strings[k] = CANCELLED_STRING;
386 _nc_align_termtype(TERMTYPE *to, TERMTYPE *from)
388 int na = NUM_EXT_NAMES(to);
389 int nb = NUM_EXT_NAMES(from);
393 int ext_Booleans, ext_Numbers, ext_Strings;
394 bool used_ext_Names = FALSE;
396 DEBUG(2, ("align_termtype to(%d:%s), from(%d:%s)", na, to->term_names,
397 nb, from->term_names));
399 if (na != 0 || nb != 0) {
400 if ((na == nb) /* check if the arrays are equivalent */
401 &&(to->ext_Booleans == from->ext_Booleans)
402 && (to->ext_Numbers == from->ext_Numbers)
403 && (to->ext_Strings == from->ext_Strings)) {
404 for (n = 0, same = TRUE; n < na; n++) {
405 if (strcmp(to->ext_Names[n], from->ext_Names[n])) {
414 * This is where we pay for having a simple extension representation.
415 * Allocate a new ext_Names array and merge the two ext_Names arrays
416 * into it, updating to's counts for booleans, etc. Fortunately we do
417 * this only for the terminfo compiler (tic) and comparer (infocmp).
419 ext_Names = typeMalloc(char *, na + nb);
421 if (to->ext_Strings && (from->ext_Booleans + from->ext_Numbers))
422 adjust_cancels(to, from);
424 if (from->ext_Strings && (to->ext_Booleans + to->ext_Numbers))
425 adjust_cancels(from, to);
427 ext_Booleans = merge_names(ext_Names,
432 ext_Numbers = merge_names(ext_Names + ext_Booleans,
437 + from->ext_Booleans,
439 ext_Strings = merge_names(ext_Names + ext_Numbers + ext_Booleans,
449 * Now we must reallocate the Booleans, etc., to allow the data to be
452 if (na != (ext_Booleans + ext_Numbers + ext_Strings)) {
453 realign_data(to, ext_Names, ext_Booleans, ext_Numbers, ext_Strings);
454 FreeIfNeeded(to->ext_Names);
455 to->ext_Names = ext_Names;
456 DEBUG(2, ("realigned %d extended names for '%s' (to)",
457 NUM_EXT_NAMES(to), to->term_names));
458 used_ext_Names = TRUE;
460 if (nb != (ext_Booleans + ext_Numbers + ext_Strings)) {
461 nb = (ext_Booleans + ext_Numbers + ext_Strings);
462 realign_data(from, ext_Names, ext_Booleans, ext_Numbers, ext_Strings);
463 from->ext_Names = typeRealloc(char *, nb, from->ext_Names);
464 memcpy(from->ext_Names, ext_Names, sizeof(char *) * nb);
465 DEBUG(2, ("realigned %d extended names for '%s' (from)",
466 NUM_EXT_NAMES(from), from->term_names));
475 _nc_copy_termtype(TERMTYPE *dst, TERMTYPE *src)
479 *dst = *src; /* ...to copy the sizes and string-tables */
480 dst->Booleans = typeMalloc(NCURSES_SBOOL, NUM_BOOLEANS(dst));
481 dst->Numbers = typeMalloc(short, NUM_NUMBERS(dst));
482 dst->Strings = typeMalloc(char *, NUM_STRINGS(dst));
484 /* FIXME: use memcpy for these and similar loops */
485 for_each_boolean(i, dst)
486 dst->Booleans[i] = src->Booleans[i];
487 for_each_number(i, dst)
488 dst->Numbers[i] = src->Numbers[i];
489 for_each_string(i, dst)
490 dst->Strings[i] = src->Strings[i];
492 /* FIXME: we probably should also copy str_table and ext_str_table,
493 * but tic and infocmp are not written to exploit that (yet).
497 if ((i = NUM_EXT_NAMES(src)) != 0) {
498 dst->ext_Names = typeMalloc(char *, i);
499 memcpy(dst->ext_Names, src->ext_Names, i * sizeof(char *));