1 /****************************************************************************
2 * Copyright (c) 1999,2000 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 *
31 ****************************************************************************/
34 * align_ttype.c -- functions for TERMTYPE
36 * _nc_align_termtype()
41 #include <curses.priv.h>
44 #include <term_entry.h>
46 MODULE_ID("$Id: alloc_ttype.c,v 1.10 2000/08/12 21:56:24 tom Exp $")
50 * Merge the a/b lists into dst. Both a/b are sorted (see _nc_extend_names()),
51 * so we do not have to worry about order dependencies.
54 merge_names(char **dst, char **a, int na, char **b, int nb)
57 while (na > 0 && nb > 0) {
58 int cmp = strcmp(*a, *b);
65 } else if (cmp == 0) {
77 DEBUG(4, ("merge_names -> %d", n));
82 find_name(char **table, int length, char *name)
84 while (length-- > 0) {
85 if (!strcmp(*table++, name)) {
86 DEBUG(4, ("found name '%s'", name));
90 DEBUG(4, ("did not find name '%s'", name));
95 realign_data(TERMTYPE * to, char **ext_Names, int ext_Booleans, int
96 ext_Numbers, int ext_Strings)
99 int limit = (to->ext_Booleans + to->ext_Numbers + to->ext_Strings);
101 if (to->ext_Booleans != ext_Booleans) {
102 to->num_Booleans += (ext_Booleans - to->ext_Booleans);
103 to->Booleans = typeRealloc(char, to->num_Booleans, to->Booleans);
104 for (n = to->ext_Booleans - 1,
105 m = ext_Booleans - 1,
106 base = to->num_Booleans - (m + 1); m >= 0; m--) {
107 if (find_name(to->ext_Names, limit, ext_Names[m])) {
108 to->Booleans[base + m] = to->Booleans[base + n--];
110 to->Booleans[base + m] = FALSE;
113 to->ext_Booleans = ext_Booleans;
115 if (to->ext_Numbers != ext_Numbers) {
116 to->num_Numbers += (ext_Numbers - to->ext_Numbers);
117 to->Numbers = typeRealloc(short, to->num_Numbers, to->Numbers);
118 for (n = to->ext_Numbers - 1,
120 base = to->num_Numbers - (m + 1); m >= 0; m--) {
121 if (find_name(to->ext_Names, limit, ext_Names[m + ext_Booleans])) {
122 to->Numbers[base + m] = to->Numbers[base + n--];
124 to->Numbers[base + m] = ABSENT_NUMERIC;
127 to->ext_Numbers = ext_Numbers;
129 if (to->ext_Strings != ext_Strings) {
130 to->num_Strings += (ext_Strings - to->ext_Strings);
131 to->Strings = typeRealloc(char *, to->num_Strings, to->Strings);
132 for (n = to->ext_Strings - 1,
134 base = to->num_Strings - (m + 1); m >= 0; m--) {
135 if (find_name(to->ext_Names, limit, ext_Names[m + ext_Booleans + ext_Numbers])) {
136 to->Strings[base + m] = to->Strings[base + n--];
138 to->Strings[base + m] = ABSENT_STRING;
141 to->ext_Strings = ext_Strings;
146 * Returns the first index in ext_Names[] for the given token-type
149 _nc_first_ext_name(TERMTYPE * tp, int token_type)
153 switch (token_type) {
158 first = tp->ext_Booleans;
161 first = tp->ext_Booleans + tp->ext_Numbers;
171 * Returns the last index in ext_Names[] for the given token-type
174 _nc_last_ext_name(TERMTYPE * tp, int token_type)
178 switch (token_type) {
180 last = tp->ext_Booleans;
183 last = tp->ext_Booleans + tp->ext_Numbers;
187 last = NUM_EXT_NAMES(tp);
194 * Lookup an entry from extended-names, returning -1 if not found
197 _nc_find_ext_name(TERMTYPE * tp, char *name, int token_type)
200 unsigned first = _nc_first_ext_name(tp, token_type);
201 unsigned last = _nc_last_ext_name(tp, token_type);
203 for (j = first; j < last; j++) {
204 if (!strcmp(name, tp->ext_Names[j])) {
212 * Translate an index into ext_Names[] into the corresponding index into data
213 * (e.g., Booleans[]).
216 _nc_ext_data_index(TERMTYPE * tp, int n, int token_type)
218 switch (token_type) {
220 n += (tp->num_Booleans - tp->ext_Booleans);
223 n += (tp->num_Numbers - tp->ext_Numbers)
224 - (tp->ext_Booleans);
228 n += (tp->num_Strings - tp->ext_Strings)
229 - (tp->ext_Booleans + tp->ext_Numbers);
235 * Adjust tables to remove (not free) an extended name and its corresponding
239 _nc_del_ext_name(TERMTYPE * tp, char *name, int token_type)
244 if ((first = _nc_find_ext_name(tp, name, token_type)) >= 0) {
245 last = NUM_EXT_NAMES(tp) - 1;
246 for (j = first; j < last; j++) {
247 tp->ext_Names[j] = tp->ext_Names[j + 1];
249 first = _nc_ext_data_index(tp, first, token_type);
250 switch (token_type) {
252 last = tp->num_Booleans - 1;
253 for (j = first; j < last; j++)
254 tp->Booleans[j] = tp->Booleans[j + 1];
255 tp->ext_Booleans -= 1;
256 tp->num_Booleans -= 1;
259 last = tp->num_Numbers - 1;
260 for (j = first; j < last; j++)
261 tp->Numbers[j] = tp->Numbers[j + 1];
262 tp->ext_Numbers -= 1;
263 tp->num_Numbers -= 1;
266 last = tp->num_Strings - 1;
267 for (j = first; j < last; j++)
268 tp->Strings[j] = tp->Strings[j + 1];
269 tp->ext_Strings -= 1;
270 tp->num_Strings -= 1;
279 * Adjust tables to insert an extended name, making room for new data. The
280 * index into the corresponding data array is returned.
283 _nc_ins_ext_name(TERMTYPE * tp, char *name, int token_type)
285 unsigned first = _nc_first_ext_name(tp, token_type);
286 unsigned last = _nc_last_ext_name(tp, token_type);
287 unsigned total = NUM_EXT_NAMES(tp) + 1;
290 for (j = first; j < last; j++) {
291 int cmp = strcmp(name, tp->ext_Names[j]);
293 /* already present */
294 return _nc_ext_data_index(tp, j, token_type);
300 tp->ext_Names = typeRealloc(char *, total, tp->ext_Names);
301 for (k = total - 1; k > j; k--)
302 tp->ext_Names[k] = tp->ext_Names[k - 1];
303 tp->ext_Names[j] = name;
304 j = _nc_ext_data_index(tp, j, token_type);
306 switch (token_type) {
308 tp->ext_Booleans += 1;
309 tp->num_Booleans += 1;
310 tp->Booleans = typeRealloc(char, tp->num_Booleans, tp->Booleans);
311 for (k = tp->num_Booleans - 1; k > j; k--)
312 tp->Booleans[k] = tp->Booleans[k - 1];
315 tp->ext_Numbers += 1;
316 tp->num_Numbers += 1;
317 tp->Numbers = typeRealloc(short, tp->num_Numbers, tp->Numbers);
318 for (k = tp->num_Numbers - 1; k > j; k--)
319 tp->Numbers[k] = tp->Numbers[k - 1];
322 tp->ext_Strings += 1;
323 tp->num_Strings += 1;
324 tp->Strings = typeRealloc(char *, tp->num_Strings, tp->Strings);
325 for (k = tp->num_Strings - 1; k > j; k--)
326 tp->Strings[k] = tp->Strings[k - 1];
333 * Look for strings that are marked cancelled, which happen to be the same name
334 * as a boolean or number. We'll get this as a special case when we get a
335 * cancellation of a name that is inherited from another entry.
338 adjust_cancels(TERMTYPE * to, TERMTYPE * from)
340 int first = to->ext_Booleans + to->ext_Numbers;
341 int last = first + to->ext_Strings;
344 for (j = first; j < last;) {
345 char *name = to->ext_Names[j];
346 unsigned j_str = to->num_Strings - first - to->ext_Strings;
348 if (to->Strings[j + j_str] == CANCELLED_STRING) {
349 if ((k = _nc_find_ext_name(from, to->ext_Names[j], BOOLEAN)) >= 0) {
350 if (_nc_del_ext_name(to, name, STRING)
351 || _nc_del_ext_name(to, name, NUMBER)) {
352 k = _nc_ins_ext_name(to, name, BOOLEAN);
353 to->Booleans[k] = FALSE;
357 } else if ((k = _nc_find_ext_name(from, to->ext_Names[j],
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;
374 _nc_align_termtype(TERMTYPE * to, TERMTYPE * from)
376 int na = NUM_EXT_NAMES(to);
377 int nb = NUM_EXT_NAMES(from);
381 int ext_Booleans, ext_Numbers, ext_Strings;
383 DEBUG(2, ("align_termtype to(%d:%s), from(%d:%s)", na, to->term_names,
384 nb, from->term_names));
386 if (na != 0 || nb != 0) {
387 if ((na == nb) /* check if the arrays are equivalent */
388 &&(to->ext_Booleans == from->ext_Booleans)
389 && (to->ext_Numbers == from->ext_Numbers)
390 && (to->ext_Strings == from->ext_Strings)) {
391 for (n = 0, same = TRUE; n < na; n++) {
392 if (strcmp(to->ext_Names[n], from->ext_Names[n])) {
401 * This is where we pay for having a simple extension representation.
402 * Allocate a new ext_Names array and merge the two ext_Names arrays
403 * into it, updating to's counts for booleans, etc. Fortunately we do
404 * this only for the terminfo compiler (tic) and comparer (infocmp).
406 ext_Names = typeMalloc(char *, na + nb);
408 if (to->ext_Strings && (from->ext_Booleans + from->ext_Numbers))
409 adjust_cancels(to, from);
411 if (from->ext_Strings && (to->ext_Booleans + to->ext_Numbers))
412 adjust_cancels(from, to);
414 ext_Booleans = merge_names(ext_Names,
419 ext_Numbers = merge_names(ext_Names + ext_Booleans,
424 + from->ext_Booleans,
426 ext_Strings = merge_names(ext_Names + ext_Numbers + ext_Booleans,
436 * Now we must reallocate the Booleans, etc., to allow the data to be
439 if (na != (ext_Booleans + ext_Numbers + ext_Strings)) {
440 realign_data(to, ext_Names, ext_Booleans, ext_Numbers, ext_Strings);
441 FreeIfNeeded(to->ext_Names);
442 to->ext_Names = ext_Names;
443 DEBUG(2, ("realigned %d extended names for '%s' (to)",
444 NUM_EXT_NAMES(to), to->term_names));
446 if (nb != (ext_Booleans + ext_Numbers + ext_Strings)) {
447 nb = (ext_Booleans + ext_Numbers + ext_Strings);
448 realign_data(from, ext_Names, ext_Booleans, ext_Numbers, ext_Strings);
449 from->ext_Names = typeRealloc(char *, nb, from->ext_Names);
450 memcpy(from->ext_Names, ext_Names, sizeof(char *) * nb);
451 DEBUG(2, ("realigned %d extended names for '%s' (from)",
452 NUM_EXT_NAMES(from), from->term_names));
459 _nc_copy_termtype(TERMTYPE * dst, TERMTYPE * src)
463 *dst = *src; /* ...to copy the sizes and string-tables */
464 dst->Booleans = typeMalloc(char, NUM_BOOLEANS(dst));
465 dst->Numbers = typeMalloc(short, NUM_NUMBERS(dst));
466 dst->Strings = typeMalloc(char *, NUM_STRINGS(dst));
468 /* FIXME: use memcpy for these and similar loops */
469 for_each_boolean(i, dst)
470 dst->Booleans[i] = src->Booleans[i];
471 for_each_number(i, dst)
472 dst->Numbers[i] = src->Numbers[i];
473 for_each_string(i, dst)
474 dst->Strings[i] = src->Strings[i];
476 /* FIXME: we probably should also copy str_table and ext_str_table,
477 * but tic and infocmp are not written to exploit that (yet).
481 if ((i = NUM_EXT_NAMES(src)) != 0) {
482 dst->ext_Names = typeMalloc(char *, i);
483 memcpy(dst->ext_Names, src->ext_Names, i * sizeof(char *));