1 /****************************************************************************
2 * Copyright (c) 1999 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 ****************************************************************************/
35 * align_ttype.c -- functions for TERMTYPE
37 * _nc_align_termtype()
42 #include <curses.priv.h>
45 #include <term_entry.h>
47 MODULE_ID("$Id: alloc_ttype.c,v 1.6 1999/03/01 22:10:44 tom Exp $")
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.
54 static int merge_names(char **dst, char **a, int na, char **b, int nb)
58 int cmp = strcmp(*a, *b);
65 } else if (cmp == 0) {
77 DEBUG(4, ("merge_names -> %d", n));
81 static bool 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));
93 static void realign_data(TERMTYPE *to, char **ext_Names, int ext_Booleans, int ext_Numbers, int ext_Strings)
96 int limit = (to->ext_Booleans + to->ext_Numbers + to->ext_Strings);
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,
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--];
107 to->Booleans[base + m] = FALSE;
110 to->ext_Booleans = ext_Booleans;
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,
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--];
121 to->Numbers[base + m] = ABSENT_NUMERIC;
124 to->ext_Numbers = ext_Numbers;
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,
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--];
135 to->Strings[base + m] = ABSENT_STRING;
138 to->ext_Strings = ext_Strings;
143 * Returns the first index in ext_Names[] for the given token-type
145 static int _nc_first_ext_name(TERMTYPE *tp, int token_type)
149 switch (token_type) {
154 first = tp->ext_Booleans;
157 first = tp->ext_Booleans + tp->ext_Numbers;
167 * Returns the last index in ext_Names[] for the given token-type
169 static int _nc_last_ext_name(TERMTYPE *tp, int token_type)
173 switch (token_type) {
175 last = tp->ext_Booleans;
178 last = tp->ext_Booleans + tp->ext_Numbers;
182 last = NUM_EXT_NAMES(tp);
189 * Lookup an entry from extended-names, returning -1 if not found
191 static int _nc_find_ext_name(TERMTYPE *tp, char *name, int token_type)
194 unsigned first = _nc_first_ext_name(tp, token_type);
195 unsigned last = _nc_last_ext_name(tp, token_type);
197 for (j = first; j < last; j++) {
198 if (!strcmp(name, tp->ext_Names[j])) {
206 * Translate an index into ext_Names[] into the corresponding index into data
207 * (e.g., Booleans[]).
209 static int _nc_ext_data_index(TERMTYPE *tp, int n, int token_type)
211 switch (token_type) {
213 n += (tp->num_Booleans - tp->ext_Booleans);
216 n += (tp->num_Numbers - tp->ext_Numbers)
217 - (tp->ext_Booleans);
221 n += (tp->num_Strings - tp->ext_Strings)
222 - (tp->ext_Booleans + tp->ext_Numbers);
228 * Adjust tables to remove (not free) an extended name and its corresponding
231 static void _nc_del_ext_name(TERMTYPE *tp, char *name, int token_type)
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];
241 first = _nc_ext_data_index(tp, first, token_type);
242 switch (token_type) {
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;
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;
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;
269 * Adjust tables to insert an extended name, making room for new data. The
270 * index into the corresponding data array is returned.
272 static int _nc_ins_ext_name(TERMTYPE *tp, char *name, int token_type)
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;
279 for (j = first; j < last; j++) {
280 int cmp = strcmp(name, tp->ext_Names[j]);
282 /* already present */
283 return _nc_ext_data_index(tp, j, token_type);
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);
295 switch (token_type) {
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];
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];
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];
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.
326 static void adjust_cancels(TERMTYPE *to, TERMTYPE *from)
328 int first = to->ext_Booleans + to->ext_Numbers;
329 int last = first + to->ext_Strings;
332 for (j = first; j < last; ) {
333 char *name = to->ext_Names[j];
334 unsigned j_str = to->num_Strings - first - to->ext_Strings;
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;
352 void _nc_align_termtype(TERMTYPE *to, TERMTYPE *from)
354 int na = NUM_EXT_NAMES(to);
355 int nb = NUM_EXT_NAMES(from);
359 int ext_Booleans, ext_Numbers, ext_Strings;
361 DEBUG(2, ("align_termtype to(%d:%s), from(%d:%s)", na, to->term_names, nb, from->term_names));
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])) {
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).
383 ext_Names = typeMalloc(char *, na+nb);
385 if (to->ext_Strings && (from->ext_Booleans + from->ext_Numbers))
386 adjust_cancels(to, from);
388 if (from->ext_Strings && (to->ext_Booleans + to->ext_Numbers))
389 adjust_cancels(from, to);
391 ext_Booleans = merge_names(ext_Names,
396 ext_Numbers = merge_names(ext_Names + ext_Booleans,
401 + from->ext_Booleans,
403 ext_Strings = merge_names(ext_Names + ext_Numbers + ext_Booleans,
413 * Now we must reallocate the Booleans, etc., to allow the data to be
416 if (na != (ext_Booleans + ext_Numbers + ext_Strings)) {
417 realign_data(to, ext_Names, ext_Booleans, ext_Numbers, ext_Strings);
419 to->ext_Names = ext_Names;
420 DEBUG(2, ("realigned %d extended names for '%s' (to)", NUM_EXT_NAMES(to), to->term_names));
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));
433 void _nc_copy_termtype(TERMTYPE *dst, TERMTYPE *src)
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));
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];
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).
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 *));