1 /****************************************************************************
2  * Copyright (c) 1999,2000 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  * 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 #include <term_entry.h>
45 
46 MODULE_ID("$Id: alloc_ttype.c,v 1.10 2000/08/12 21:56:24 tom Exp $")
47 
48 #if NCURSES_XNAMES
49 /*
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.
52  */
53 static int
54 merge_names(char **dst, char **a, int na, char **b, int nb)
55 {
56     int n = 0;
57     while (na > 0 && nb > 0) {
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
82 find_name(char **table, int length, char *name)
83 {
84     while (length-- > 0) {
85 	if (!strcmp(*table++, name)) {
86 	    DEBUG(4, ("found name '%s'", name));
87 	    return TRUE;
88 	}
89     }
90     DEBUG(4, ("did not find name '%s'", name));
91     return FALSE;
92 }
93 
94 static void
95 realign_data(TERMTYPE * to, char **ext_Names, int ext_Booleans, int
96 	     ext_Numbers, int ext_Strings)
97 {
98     int n, m, base;
99     int limit = (to->ext_Booleans + to->ext_Numbers + to->ext_Strings);
100 
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--];
109 	    } else {
110 		to->Booleans[base + m] = FALSE;
111 	    }
112 	}
113 	to->ext_Booleans = ext_Booleans;
114     }
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,
119 	     m = 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--];
123 	    } else {
124 		to->Numbers[base + m] = ABSENT_NUMERIC;
125 	    }
126 	}
127 	to->ext_Numbers = ext_Numbers;
128     }
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,
133 	     m = 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--];
137 	    } else {
138 		to->Strings[base + m] = ABSENT_STRING;
139 	    }
140 	}
141 	to->ext_Strings = ext_Strings;
142     }
143 }
144 
145 /*
146  * Returns the first index in ext_Names[] for the given token-type
147  */
148 static int
149 _nc_first_ext_name(TERMTYPE * tp, int token_type)
150 {
151     int first;
152 
153     switch (token_type) {
154     case BOOLEAN:
155 	first = 0;
156 	break;
157     case NUMBER:
158 	first = tp->ext_Booleans;
159 	break;
160     case STRING:
161 	first = tp->ext_Booleans + tp->ext_Numbers;
162 	break;
163     default:
164 	first = 0;
165 	break;
166     }
167     return first;
168 }
169 
170 /*
171  * Returns the last index in ext_Names[] for the given token-type
172  */
173 static int
174 _nc_last_ext_name(TERMTYPE * tp, int token_type)
175 {
176     int last;
177 
178     switch (token_type) {
179     case BOOLEAN:
180 	last = tp->ext_Booleans;
181 	break;
182     case NUMBER:
183 	last = tp->ext_Booleans + tp->ext_Numbers;
184 	break;
185     default:
186     case STRING:
187 	last = NUM_EXT_NAMES(tp);
188 	break;
189     }
190     return last;
191 }
192 
193 /*
194  * Lookup an entry from extended-names, returning -1 if not found
195  */
196 static int
197 _nc_find_ext_name(TERMTYPE * tp, char *name, int token_type)
198 {
199     unsigned j;
200     unsigned first = _nc_first_ext_name(tp, token_type);
201     unsigned last = _nc_last_ext_name(tp, token_type);
202 
203     for (j = first; j < last; j++) {
204 	if (!strcmp(name, tp->ext_Names[j])) {
205 	    return j;
206 	}
207     }
208     return -1;
209 }
210 
211 /*
212  * Translate an index into ext_Names[] into the corresponding index into data
213  * (e.g., Booleans[]).
214  */
215 static int
216 _nc_ext_data_index(TERMTYPE * tp, int n, int token_type)
217 {
218     switch (token_type) {
219     case BOOLEAN:
220 	n += (tp->num_Booleans - tp->ext_Booleans);
221 	break;
222     case NUMBER:
223 	n += (tp->num_Numbers - tp->ext_Numbers)
224 	    - (tp->ext_Booleans);
225 	break;
226     default:
227     case STRING:
228 	n += (tp->num_Strings - tp->ext_Strings)
229 	    - (tp->ext_Booleans + tp->ext_Numbers);
230     }
231     return n;
232 }
233 
234 /*
235  * Adjust tables to remove (not free) an extended name and its corresponding
236  * data.
237  */
238 static bool
239 _nc_del_ext_name(TERMTYPE * tp, char *name, int token_type)
240 {
241     int j;
242     int first, last;
243 
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];
248 	}
249 	first = _nc_ext_data_index(tp, first, token_type);
250 	switch (token_type) {
251 	case BOOLEAN:
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;
257 	    break;
258 	case NUMBER:
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;
264 	    break;
265 	case STRING:
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;
271 	    break;
272 	}
273 	return TRUE;
274     }
275     return FALSE;
276 }
277 
278 /*
279  * Adjust tables to insert an extended name, making room for new data.  The
280  * index into the corresponding data array is returned.
281  */
282 static int
283 _nc_ins_ext_name(TERMTYPE * tp, char *name, int token_type)
284 {
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;
288     unsigned j, k;
289 
290     for (j = first; j < last; j++) {
291 	int cmp = strcmp(name, tp->ext_Names[j]);
292 	if (cmp == 0)
293 	    /* already present */
294 	    return _nc_ext_data_index(tp, j, token_type);
295 	if (cmp < 0) {
296 	    break;
297 	}
298     }
299 
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);
305 
306     switch (token_type) {
307     case BOOLEAN:
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];
313 	break;
314     case NUMBER:
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];
320 	break;
321     case STRING:
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];
327 	break;
328     }
329     return j;
330 }
331 
332 /*
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.
336  */
337 static void
338 adjust_cancels(TERMTYPE * to, TERMTYPE * from)
339 {
340     int first = to->ext_Booleans + to->ext_Numbers;
341     int last = first + to->ext_Strings;
342     int j, k;
343 
344     for (j = first; j < last;) {
345 	char *name = to->ext_Names[j];
346 	unsigned j_str = to->num_Strings - first - to->ext_Strings;
347 
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;
354 		} else {
355 		    j++;
356 		}
357 	    } else if ((k = _nc_find_ext_name(from, to->ext_Names[j],
358 					      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 	    }
367 	} else {
368 	    j++;
369 	}
370     }
371 }
372 
373 void
374 _nc_align_termtype(TERMTYPE * to, TERMTYPE * from)
375 {
376     int na = NUM_EXT_NAMES(to);
377     int nb = NUM_EXT_NAMES(from);
378     int n;
379     bool same;
380     char **ext_Names;
381     int ext_Booleans, ext_Numbers, ext_Strings;
382 
383     DEBUG(2, ("align_termtype to(%d:%s), from(%d:%s)", na, to->term_names,
384 	      nb, from->term_names));
385 
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])) {
393 		    same = FALSE;
394 		    break;
395 		}
396 	    }
397 	    if (same)
398 		return;
399 	}
400 	/*
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).
405 	 */
406 	ext_Names = typeMalloc(char *, na + nb);
407 
408 	if (to->ext_Strings && (from->ext_Booleans + from->ext_Numbers))
409 	    adjust_cancels(to, from);
410 
411 	if (from->ext_Strings && (to->ext_Booleans + to->ext_Numbers))
412 	    adjust_cancels(from, to);
413 
414 	ext_Booleans = merge_names(ext_Names,
415 				   to->ext_Names,
416 				   to->ext_Booleans,
417 				   from->ext_Names,
418 				   from->ext_Booleans);
419 	ext_Numbers = merge_names(ext_Names + ext_Booleans,
420 				  to->ext_Names
421 				  + to->ext_Booleans,
422 				  to->ext_Numbers,
423 				  from->ext_Names
424 				  + from->ext_Booleans,
425 				  from->ext_Numbers);
426 	ext_Strings = merge_names(ext_Names + ext_Numbers + ext_Booleans,
427 				  to->ext_Names
428 				  + to->ext_Booleans
429 				  + to->ext_Numbers,
430 				  to->ext_Strings,
431 				  from->ext_Names
432 				  + from->ext_Booleans
433 				  + from->ext_Numbers,
434 				  from->ext_Strings);
435 	/*
436 	 * Now we must reallocate the Booleans, etc., to allow the data to be
437 	 * overlaid.
438 	 */
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));
445 	}
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));
453 	}
454     }
455 }
456 #endif
457 
458 void
459 _nc_copy_termtype(TERMTYPE * dst, TERMTYPE * src)
460 {
461     int i;
462 
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));
467 
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];
475 
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).
478      */
479 
480 #if NCURSES_XNAMES
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 *));
484     } else {
485 	dst->ext_Names = 0;
486     }
487 #endif
488 
489 }
490