1 /**************************************************************************** 2 * Copyright (c) 1999-2012,2013 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-on * 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 45 MODULE_ID("$Id: alloc_ttype.c,v 1.27 2013/06/08 16:54:50 tom Exp $") 46 47 #if NCURSES_XNAMES 48 /* 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. 51 */ 52 static int 53 merge_names(char **dst, char **a, int na, char **b, int nb) 54 { 55 int n = 0; 56 while (na > 0 && nb > 0) { 57 int cmp = strcmp(*a, *b); 58 if (cmp < 0) { 59 dst[n++] = *a++; 60 na--; 61 } else if (cmp > 0) { 62 dst[n++] = *b++; 63 nb--; 64 } else if (cmp == 0) { 65 dst[n++] = *a; 66 a++, b++; 67 na--, nb--; 68 } 69 } 70 while (na-- > 0) { 71 dst[n++] = *a++; 72 } 73 while (nb-- > 0) { 74 dst[n++] = *b++; 75 } 76 DEBUG(4, ("merge_names -> %d", n)); 77 return n; 78 } 79 80 static bool 81 find_name(char **table, int length, char *name) 82 { 83 while (length-- > 0) { 84 if (!strcmp(*table++, name)) { 85 DEBUG(4, ("found name '%s'", name)); 86 return TRUE; 87 } 88 } 89 DEBUG(4, ("did not find name '%s'", name)); 90 return FALSE; 91 } 92 93 #define EXTEND_NUM(num, ext) \ 94 to->num = (unsigned short) (to->num + (ext - to->ext)) 95 96 static void 97 realign_data(TERMTYPE *to, char **ext_Names, 98 int ext_Booleans, 99 int ext_Numbers, 100 int ext_Strings) 101 { 102 int n, m, base; 103 int limit = (to->ext_Booleans + to->ext_Numbers + to->ext_Strings); 104 105 if (to->ext_Booleans != ext_Booleans) { 106 EXTEND_NUM(num_Booleans, ext_Booleans); 107 TYPE_REALLOC(NCURSES_SBOOL, to->num_Booleans, to->Booleans); 108 for (n = to->ext_Booleans - 1, 109 m = ext_Booleans - 1, 110 base = to->num_Booleans - (m + 1); m >= 0; m--) { 111 if (find_name(to->ext_Names, limit, ext_Names[m])) { 112 to->Booleans[base + m] = to->Booleans[base + n--]; 113 } else { 114 to->Booleans[base + m] = FALSE; 115 } 116 } 117 to->ext_Booleans = UShort(ext_Booleans); 118 } 119 120 if (to->ext_Numbers != ext_Numbers) { 121 EXTEND_NUM(num_Numbers, ext_Numbers); 122 TYPE_REALLOC(short, to->num_Numbers, to->Numbers); 123 for (n = to->ext_Numbers - 1, 124 m = ext_Numbers - 1, 125 base = to->num_Numbers - (m + 1); m >= 0; m--) { 126 if (find_name(to->ext_Names, limit, ext_Names[m + ext_Booleans])) { 127 to->Numbers[base + m] = to->Numbers[base + n--]; 128 } else { 129 to->Numbers[base + m] = ABSENT_NUMERIC; 130 } 131 } 132 to->ext_Numbers = UShort(ext_Numbers); 133 } 134 if (to->ext_Strings != ext_Strings) { 135 EXTEND_NUM(num_Strings, ext_Strings); 136 TYPE_REALLOC(char *, to->num_Strings, to->Strings); 137 for (n = to->ext_Strings - 1, 138 m = ext_Strings - 1, 139 base = to->num_Strings - (m + 1); m >= 0; m--) { 140 if (find_name(to->ext_Names, limit, ext_Names[m + ext_Booleans + ext_Numbers])) { 141 to->Strings[base + m] = to->Strings[base + n--]; 142 } else { 143 to->Strings[base + m] = ABSENT_STRING; 144 } 145 } 146 to->ext_Strings = UShort(ext_Strings); 147 } 148 } 149 150 /* 151 * Returns the first index in ext_Names[] for the given token-type 152 */ 153 static unsigned 154 _nc_first_ext_name(TERMTYPE *tp, int token_type) 155 { 156 unsigned first; 157 158 switch (token_type) { 159 case BOOLEAN: 160 first = 0; 161 break; 162 case NUMBER: 163 first = tp->ext_Booleans; 164 break; 165 case STRING: 166 first = (unsigned) (tp->ext_Booleans + tp->ext_Numbers); 167 break; 168 default: 169 first = 0; 170 break; 171 } 172 return first; 173 } 174 175 /* 176 * Returns the last index in ext_Names[] for the given token-type 177 */ 178 static unsigned 179 _nc_last_ext_name(TERMTYPE *tp, int token_type) 180 { 181 unsigned last; 182 183 switch (token_type) { 184 case BOOLEAN: 185 last = tp->ext_Booleans; 186 break; 187 case NUMBER: 188 last = (unsigned) (tp->ext_Booleans + tp->ext_Numbers); 189 break; 190 default: 191 case STRING: 192 last = NUM_EXT_NAMES(tp); 193 break; 194 } 195 return last; 196 } 197 198 /* 199 * Lookup an entry from extended-names, returning -1 if not found 200 */ 201 static int 202 _nc_find_ext_name(TERMTYPE *tp, char *name, int token_type) 203 { 204 unsigned j; 205 unsigned first = _nc_first_ext_name(tp, token_type); 206 unsigned last = _nc_last_ext_name(tp, token_type); 207 208 for (j = first; j < last; j++) { 209 if (!strcmp(name, tp->ext_Names[j])) { 210 return (int) j; 211 } 212 } 213 return -1; 214 } 215 216 /* 217 * Translate an index into ext_Names[] into the corresponding index into data 218 * (e.g., Booleans[]). 219 */ 220 static int 221 _nc_ext_data_index(TERMTYPE *tp, int n, int token_type) 222 { 223 switch (token_type) { 224 case BOOLEAN: 225 n += (tp->num_Booleans - tp->ext_Booleans); 226 break; 227 case NUMBER: 228 n += (tp->num_Numbers - tp->ext_Numbers) 229 - (tp->ext_Booleans); 230 break; 231 default: 232 case STRING: 233 n += (tp->num_Strings - tp->ext_Strings) 234 - (tp->ext_Booleans + tp->ext_Numbers); 235 } 236 return n; 237 } 238 239 /* 240 * Adjust tables to remove (not free) an extended name and its corresponding 241 * data. 242 */ 243 static bool 244 _nc_del_ext_name(TERMTYPE *tp, char *name, int token_type) 245 { 246 int j; 247 int first, last; 248 249 if ((first = _nc_find_ext_name(tp, name, token_type)) >= 0) { 250 last = (int) NUM_EXT_NAMES(tp) - 1; 251 for (j = first; j < last; j++) { 252 tp->ext_Names[j] = tp->ext_Names[j + 1]; 253 } 254 first = _nc_ext_data_index(tp, first, token_type); 255 switch (token_type) { 256 case BOOLEAN: 257 last = tp->num_Booleans - 1; 258 for (j = first; j < last; j++) 259 tp->Booleans[j] = tp->Booleans[j + 1]; 260 tp->ext_Booleans--; 261 tp->num_Booleans--; 262 break; 263 case NUMBER: 264 last = tp->num_Numbers - 1; 265 for (j = first; j < last; j++) 266 tp->Numbers[j] = tp->Numbers[j + 1]; 267 tp->ext_Numbers--; 268 tp->num_Numbers--; 269 break; 270 case STRING: 271 last = tp->num_Strings - 1; 272 for (j = first; j < last; j++) 273 tp->Strings[j] = tp->Strings[j + 1]; 274 tp->ext_Strings--; 275 tp->num_Strings--; 276 break; 277 } 278 return TRUE; 279 } 280 return FALSE; 281 } 282 283 /* 284 * Adjust tables to insert an extended name, making room for new data. The 285 * index into the corresponding data array is returned. 286 */ 287 static int 288 _nc_ins_ext_name(TERMTYPE *tp, char *name, int token_type) 289 { 290 unsigned first = _nc_first_ext_name(tp, token_type); 291 unsigned last = _nc_last_ext_name(tp, token_type); 292 unsigned total = NUM_EXT_NAMES(tp) + 1; 293 unsigned j, k; 294 295 for (j = first; j < last; j++) { 296 int cmp = strcmp(name, tp->ext_Names[j]); 297 if (cmp == 0) 298 /* already present */ 299 return _nc_ext_data_index(tp, (int) j, token_type); 300 if (cmp < 0) { 301 break; 302 } 303 } 304 305 TYPE_REALLOC(char *, total, tp->ext_Names); 306 for (k = total - 1; k > j; k--) 307 tp->ext_Names[k] = tp->ext_Names[k - 1]; 308 tp->ext_Names[j] = name; 309 j = (unsigned) _nc_ext_data_index(tp, (int) j, token_type); 310 311 switch (token_type) { 312 case BOOLEAN: 313 tp->ext_Booleans++; 314 tp->num_Booleans++; 315 TYPE_REALLOC(NCURSES_SBOOL, tp->num_Booleans, tp->Booleans); 316 for (k = (unsigned) (tp->num_Booleans - 1); k > j; k--) 317 tp->Booleans[k] = tp->Booleans[k - 1]; 318 break; 319 case NUMBER: 320 tp->ext_Numbers++; 321 tp->num_Numbers++; 322 TYPE_REALLOC(short, tp->num_Numbers, tp->Numbers); 323 for (k = (unsigned) (tp->num_Numbers - 1); k > j; k--) 324 tp->Numbers[k] = tp->Numbers[k - 1]; 325 break; 326 case STRING: 327 tp->ext_Strings++; 328 tp->num_Strings++; 329 TYPE_REALLOC(char *, tp->num_Strings, tp->Strings); 330 for (k = (unsigned) (tp->num_Strings - 1); k > j; k--) 331 tp->Strings[k] = tp->Strings[k - 1]; 332 break; 333 } 334 return (int) j; 335 } 336 337 /* 338 * Look for strings that are marked cancelled, which happen to be the same name 339 * as a boolean or number. We'll get this as a special case when we get a 340 * cancellation of a name that is inherited from another entry. 341 */ 342 static void 343 adjust_cancels(TERMTYPE *to, TERMTYPE *from) 344 { 345 int first = to->ext_Booleans + to->ext_Numbers; 346 int last = first + to->ext_Strings; 347 int j, k; 348 349 for (j = first; j < last;) { 350 char *name = to->ext_Names[j]; 351 int j_str = to->num_Strings - first - to->ext_Strings; 352 353 if (to->Strings[j + j_str] == CANCELLED_STRING) { 354 if (_nc_find_ext_name(from, to->ext_Names[j], BOOLEAN) >= 0) { 355 if (_nc_del_ext_name(to, name, STRING) 356 || _nc_del_ext_name(to, name, NUMBER)) { 357 k = _nc_ins_ext_name(to, name, BOOLEAN); 358 to->Booleans[k] = FALSE; 359 } else { 360 j++; 361 } 362 } else if (_nc_find_ext_name(from, to->ext_Names[j], NUMBER) >= 0) { 363 if (_nc_del_ext_name(to, name, STRING) 364 || _nc_del_ext_name(to, name, BOOLEAN)) { 365 k = _nc_ins_ext_name(to, name, NUMBER); 366 to->Numbers[k] = CANCELLED_NUMERIC; 367 } else { 368 j++; 369 } 370 } else if (_nc_find_ext_name(from, to->ext_Names[j], STRING) >= 0) { 371 if (_nc_del_ext_name(to, name, NUMBER) 372 || _nc_del_ext_name(to, name, BOOLEAN)) { 373 k = _nc_ins_ext_name(to, name, STRING); 374 to->Strings[k] = CANCELLED_STRING; 375 } else { 376 j++; 377 } 378 } else { 379 j++; 380 } 381 } else { 382 j++; 383 } 384 } 385 } 386 387 NCURSES_EXPORT(void) 388 _nc_align_termtype(TERMTYPE *to, TERMTYPE *from) 389 { 390 int na = (int) NUM_EXT_NAMES(to); 391 int nb = (int) NUM_EXT_NAMES(from); 392 int n; 393 bool same; 394 char **ext_Names; 395 int ext_Booleans, ext_Numbers, ext_Strings; 396 bool used_ext_Names = FALSE; 397 398 DEBUG(2, ("align_termtype to(%d:%s), from(%d:%s)", na, to->term_names, 399 nb, from->term_names)); 400 401 if (na != 0 || nb != 0) { 402 if ((na == nb) /* check if the arrays are equivalent */ 403 &&(to->ext_Booleans == from->ext_Booleans) 404 && (to->ext_Numbers == from->ext_Numbers) 405 && (to->ext_Strings == from->ext_Strings)) { 406 for (n = 0, same = TRUE; n < na; n++) { 407 if (strcmp(to->ext_Names[n], from->ext_Names[n])) { 408 same = FALSE; 409 break; 410 } 411 } 412 if (same) 413 return; 414 } 415 /* 416 * This is where we pay for having a simple extension representation. 417 * Allocate a new ext_Names array and merge the two ext_Names arrays 418 * into it, updating to's counts for booleans, etc. Fortunately we do 419 * this only for the terminfo compiler (tic) and comparer (infocmp). 420 */ 421 TYPE_MALLOC(char *, (size_t)(na + nb), ext_Names); 422 423 if (to->ext_Strings && (from->ext_Booleans + from->ext_Numbers)) 424 adjust_cancels(to, from); 425 426 if (from->ext_Strings && (to->ext_Booleans + to->ext_Numbers)) 427 adjust_cancels(from, to); 428 429 ext_Booleans = merge_names(ext_Names, 430 to->ext_Names, 431 to->ext_Booleans, 432 from->ext_Names, 433 from->ext_Booleans); 434 ext_Numbers = merge_names(ext_Names + ext_Booleans, 435 to->ext_Names 436 + to->ext_Booleans, 437 to->ext_Numbers, 438 from->ext_Names 439 + from->ext_Booleans, 440 from->ext_Numbers); 441 ext_Strings = merge_names(ext_Names + ext_Numbers + ext_Booleans, 442 to->ext_Names 443 + to->ext_Booleans 444 + to->ext_Numbers, 445 to->ext_Strings, 446 from->ext_Names 447 + from->ext_Booleans 448 + from->ext_Numbers, 449 from->ext_Strings); 450 /* 451 * Now we must reallocate the Booleans, etc., to allow the data to be 452 * overlaid. 453 */ 454 if (na != (ext_Booleans + ext_Numbers + ext_Strings)) { 455 realign_data(to, ext_Names, ext_Booleans, ext_Numbers, ext_Strings); 456 FreeIfNeeded(to->ext_Names); 457 to->ext_Names = ext_Names; 458 DEBUG(2, ("realigned %d extended names for '%s' (to)", 459 NUM_EXT_NAMES(to), to->term_names)); 460 used_ext_Names = TRUE; 461 } 462 if (nb != (ext_Booleans + ext_Numbers + ext_Strings)) { 463 nb = (ext_Booleans + ext_Numbers + ext_Strings); 464 realign_data(from, ext_Names, ext_Booleans, ext_Numbers, ext_Strings); 465 TYPE_REALLOC(char *, (size_t) nb, from->ext_Names); 466 memcpy(from->ext_Names, ext_Names, sizeof(char *) * (size_t) nb); 467 DEBUG(2, ("realigned %d extended names for '%s' (from)", 468 NUM_EXT_NAMES(from), from->term_names)); 469 } 470 if (!used_ext_Names) 471 free(ext_Names); 472 } 473 } 474 #endif 475 476 NCURSES_EXPORT(void) 477 _nc_copy_termtype(TERMTYPE *dst, const TERMTYPE *src) 478 { 479 #if NCURSES_XNAMES 480 unsigned i; 481 #endif 482 483 *dst = *src; /* ...to copy the sizes and string-tables */ 484 485 TYPE_MALLOC(NCURSES_SBOOL, NUM_BOOLEANS(dst), dst->Booleans); 486 TYPE_MALLOC(short, NUM_NUMBERS(dst), dst->Numbers); 487 TYPE_MALLOC(char *, NUM_STRINGS(dst), dst->Strings); 488 489 memcpy(dst->Booleans, 490 src->Booleans, 491 NUM_BOOLEANS(dst) * sizeof(dst->Booleans[0])); 492 memcpy(dst->Numbers, 493 src->Numbers, 494 NUM_NUMBERS(dst) * sizeof(dst->Numbers[0])); 495 memcpy(dst->Strings, 496 src->Strings, 497 NUM_STRINGS(dst) * sizeof(dst->Strings[0])); 498 499 /* FIXME: we probably should also copy str_table and ext_str_table, 500 * but tic and infocmp are not written to exploit that (yet). 501 */ 502 503 #if NCURSES_XNAMES 504 if ((i = NUM_EXT_NAMES(src)) != 0) { 505 TYPE_MALLOC(char *, i, dst->ext_Names); 506 memcpy(dst->ext_Names, src->ext_Names, i * sizeof(char *)); 507 } else { 508 dst->ext_Names = 0; 509 } 510 #endif 511 } 512