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.8 2000/03/25 17:03:11 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 && 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 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 void 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 } 274 } 275 276 /* 277 * Adjust tables to insert an extended name, making room for new data. The 278 * index into the corresponding data array is returned. 279 */ 280 static int 281 _nc_ins_ext_name(TERMTYPE * tp, char *name, int token_type) 282 { 283 unsigned first = _nc_first_ext_name(tp, token_type); 284 unsigned last = _nc_last_ext_name(tp, token_type); 285 unsigned total = NUM_EXT_NAMES(tp) + 1; 286 unsigned j, k; 287 288 for (j = first; j < last; j++) { 289 int cmp = strcmp(name, tp->ext_Names[j]); 290 if (cmp == 0) 291 /* already present */ 292 return _nc_ext_data_index(tp, j, token_type); 293 if (cmp < 0) { 294 break; 295 } 296 } 297 298 tp->ext_Names = typeRealloc(char *, total, tp->ext_Names); 299 for (k = total - 1; k > j; k--) 300 tp->ext_Names[k] = tp->ext_Names[k - 1]; 301 tp->ext_Names[j] = name; 302 j = _nc_ext_data_index(tp, j, token_type); 303 304 switch (token_type) { 305 case BOOLEAN: 306 tp->ext_Booleans += 1; 307 tp->num_Booleans += 1; 308 tp->Booleans = typeRealloc(char, tp->num_Booleans, tp->Booleans); 309 for (k = tp->num_Booleans - 1; k > j; k--) 310 tp->Booleans[k] = tp->Booleans[k - 1]; 311 break; 312 case NUMBER: 313 tp->ext_Numbers += 1; 314 tp->num_Numbers += 1; 315 tp->Numbers = typeRealloc(short, tp->num_Numbers, tp->Numbers); 316 for (k = tp->num_Numbers - 1; k > j; k--) 317 tp->Numbers[k] = tp->Numbers[k - 1]; 318 break; 319 case STRING: 320 tp->ext_Strings += 1; 321 tp->num_Strings += 1; 322 tp->Strings = typeRealloc(char *, tp->num_Strings, tp->Strings); 323 for (k = tp->num_Strings - 1; k > j; k--) 324 tp->Strings[k] = tp->Strings[k - 1]; 325 break; 326 } 327 return j; 328 } 329 330 /* 331 * Look for strings that are marked cancelled, which happen to be the same name 332 * as a boolean or number. We'll get this as a special case when we get a 333 * cancellation of a name that is inherited from another entry. 334 */ 335 static void 336 adjust_cancels(TERMTYPE * to, TERMTYPE * from) 337 { 338 int first = to->ext_Booleans + to->ext_Numbers; 339 int last = first + to->ext_Strings; 340 int j, k; 341 342 for (j = first; j < last;) { 343 char *name = to->ext_Names[j]; 344 unsigned j_str = to->num_Strings - first - to->ext_Strings; 345 346 if (to->Strings[j + j_str] == CANCELLED_STRING) { 347 if ((k = _nc_find_ext_name(from, to->ext_Names[j], BOOLEAN)) >= 0) { 348 _nc_del_ext_name(to, name, STRING); 349 k = _nc_ins_ext_name(to, name, BOOLEAN); 350 to->Booleans[k] = FALSE; 351 } else if ((k = _nc_find_ext_name(from, to->ext_Names[j], 352 NUMBER)) >= 0) { 353 _nc_del_ext_name(to, name, STRING); 354 k = _nc_ins_ext_name(to, name, NUMBER); 355 to->Numbers[k] = CANCELLED_NUMERIC; 356 } 357 } else { 358 j++; 359 } 360 } 361 } 362 363 void 364 _nc_align_termtype(TERMTYPE * to, TERMTYPE * from) 365 { 366 int na = NUM_EXT_NAMES(to); 367 int nb = NUM_EXT_NAMES(from); 368 int n; 369 bool same; 370 char **ext_Names; 371 int ext_Booleans, ext_Numbers, ext_Strings; 372 373 DEBUG(2, ("align_termtype to(%d:%s), from(%d:%s)", na, to->term_names, 374 nb, from->term_names)); 375 376 if (na != 0 || nb != 0) { 377 if ((na == nb) /* check if the arrays are equivalent */ 378 &&(to->ext_Booleans == from->ext_Booleans) 379 && (to->ext_Numbers == from->ext_Numbers) 380 && (to->ext_Strings == from->ext_Strings)) { 381 for (n = 0, same = TRUE; n < na; n++) { 382 if (strcmp(to->ext_Names[n], from->ext_Names[n])) { 383 same = FALSE; 384 break; 385 } 386 } 387 if (same) 388 return; 389 } 390 /* 391 * This is where we pay for having a simple extension representation. 392 * Allocate a new ext_Names array and merge the two ext_Names arrays 393 * into it, updating to's counts for booleans, etc. Fortunately we do 394 * this only for the terminfo compiler (tic) and comparer (infocmp). 395 */ 396 ext_Names = typeMalloc(char *, na + nb); 397 398 if (to->ext_Strings && (from->ext_Booleans + from->ext_Numbers)) 399 adjust_cancels(to, from); 400 401 if (from->ext_Strings && (to->ext_Booleans + to->ext_Numbers)) 402 adjust_cancels(from, to); 403 404 ext_Booleans = merge_names(ext_Names, 405 to->ext_Names, 406 to->ext_Booleans, 407 from->ext_Names, 408 from->ext_Booleans); 409 ext_Numbers = merge_names(ext_Names + ext_Booleans, 410 to->ext_Names 411 + to->ext_Booleans, 412 to->ext_Numbers, 413 from->ext_Names 414 + from->ext_Booleans, 415 from->ext_Numbers); 416 ext_Strings = merge_names(ext_Names + ext_Numbers + ext_Booleans, 417 to->ext_Names 418 + to->ext_Booleans 419 + to->ext_Numbers, 420 to->ext_Strings, 421 from->ext_Names 422 + from->ext_Booleans 423 + from->ext_Numbers, 424 from->ext_Strings); 425 /* 426 * Now we must reallocate the Booleans, etc., to allow the data to be 427 * overlaid. 428 */ 429 if (na != (ext_Booleans + ext_Numbers + ext_Strings)) { 430 realign_data(to, ext_Names, ext_Booleans, ext_Numbers, ext_Strings); 431 FreeIfNeeded(to->ext_Names); 432 to->ext_Names = ext_Names; 433 DEBUG(2, ("realigned %d extended names for '%s' (to)", 434 NUM_EXT_NAMES(to), to->term_names)); 435 } 436 if (nb != (ext_Booleans + ext_Numbers + ext_Strings)) { 437 nb = (ext_Booleans + ext_Numbers + ext_Strings); 438 realign_data(from, ext_Names, ext_Booleans, ext_Numbers, ext_Strings); 439 from->ext_Names = typeRealloc(char *, nb, from->ext_Names); 440 memcpy(from->ext_Names, ext_Names, sizeof(char *) * nb); 441 DEBUG(2, ("realigned %d extended names for '%s' (from)", 442 NUM_EXT_NAMES(from), from->term_names)); 443 } 444 } 445 } 446 #endif 447 448 void 449 _nc_copy_termtype(TERMTYPE * dst, TERMTYPE * src) 450 { 451 int i; 452 453 *dst = *src; /* ...to copy the sizes and string-tables */ 454 dst->Booleans = typeMalloc(char, NUM_BOOLEANS(dst)); 455 dst->Numbers = typeMalloc(short, NUM_NUMBERS(dst)); 456 dst->Strings = typeMalloc(char *, NUM_STRINGS(dst)); 457 458 /* FIXME: use memcpy for these and similar loops */ 459 for_each_boolean(i, dst) 460 dst->Booleans[i] = src->Booleans[i]; 461 for_each_number(i, dst) 462 dst->Numbers[i] = src->Numbers[i]; 463 for_each_string(i, dst) 464 dst->Strings[i] = src->Strings[i]; 465 466 /* FIXME: we probably should also copy str_table and ext_str_table, 467 * but tic and infocmp are not written to exploit that (yet). 468 */ 469 470 #if NCURSES_XNAMES 471 if ((i = NUM_EXT_NAMES(src)) != 0) { 472 dst->ext_Names = typeMalloc(char *, i); 473 memcpy(dst->ext_Names, src->ext_Names, i * sizeof(char *)); 474 } else { 475 dst->ext_Names = 0; 476 } 477 #endif 478 479 } 480