1 /* $OpenBSD: comp_parse.c,v 1.10 2001/01/22 18:01:51 millert Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc. * 5 * * 6 * Permission is hereby granted, free of charge, to any person obtaining a * 7 * copy of this software and associated documentation files (the * 8 * "Software"), to deal in the Software without restriction, including * 9 * without limitation the rights to use, copy, modify, merge, publish, * 10 * distribute, distribute with modifications, sublicense, and/or sell * 11 * copies of the Software, and to permit persons to whom the Software is * 12 * furnished to do so, subject to the following conditions: * 13 * * 14 * The above copyright notice and this permission notice shall be included * 15 * in all copies or substantial portions of the Software. * 16 * * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 24 * * 25 * Except as contained in this notice, the name(s) of the above copyright * 26 * holders shall not be used in advertising or otherwise to promote the * 27 * sale, use or other dealings in this Software without prior written * 28 * authorization. * 29 ****************************************************************************/ 30 31 /**************************************************************************** 32 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 33 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 34 ****************************************************************************/ 35 36 /* 37 * comp_parse.c -- parser driver loop and use handling. 38 * 39 * _nc_read_entry_source(FILE *, literal, bool, bool (*hook)()) 40 * _nc_resolve_uses(void) 41 * _nc_free_entries(void) 42 * 43 * Use this code by calling _nc_read_entry_source() on as many source 44 * files as you like (either terminfo or termcap syntax). If you 45 * want use-resolution, call _nc_resolve_uses(). To free the list 46 * storage, do _nc_free_entries(). 47 * 48 */ 49 50 #include <curses.priv.h> 51 52 #include <ctype.h> 53 54 #include <tic.h> 55 #include <term_entry.h> 56 57 MODULE_ID("$From: comp_parse.c,v 1.48 2001/01/15 00:44:51 tom Exp $") 58 59 static void sanity_check(TERMTYPE *); 60 NCURSES_IMPEXP void NCURSES_API(*_nc_check_termtype) (TERMTYPE *) = sanity_check; 61 62 /**************************************************************************** 63 * 64 * Entry queue handling 65 * 66 ****************************************************************************/ 67 /* 68 * The entry list is a doubly linked list with NULLs terminating the lists: 69 * 70 * --------- --------- --------- 71 * | | | | | | offset 72 * |-------| |-------| |-------| 73 * | ----+-->| ----+-->| NULL | next 74 * |-------| |-------| |-------| 75 * | NULL |<--+---- |<--+---- | last 76 * --------- --------- --------- 77 * ^ ^ 78 * | | 79 * | | 80 * _nc_head _nc_tail 81 */ 82 83 NCURSES_EXPORT_VAR(ENTRY *) _nc_head = 0; 84 NCURSES_EXPORT_VAR(ENTRY *) _nc_tail = 0; 85 86 static void 87 enqueue(ENTRY * ep) 88 /* add an entry to the in-core list */ 89 { 90 ENTRY *newp = _nc_copy_entry(ep); 91 92 if (newp == 0) 93 _nc_err_abort("Out of memory"); 94 95 newp->last = _nc_tail; 96 _nc_tail = newp; 97 98 newp->next = 0; 99 if (newp->last) 100 newp->last->next = newp; 101 } 102 103 NCURSES_EXPORT(void) 104 _nc_free_entries(ENTRY * headp) 105 /* free the allocated storage consumed by list entries */ 106 { 107 ENTRY *ep, *next; 108 109 for (ep = headp; ep; ep = next) { 110 /* 111 * This conditional lets us disconnect storage from the list. 112 * To do this, copy an entry out of the list, then null out 113 * the string-table member in the original and any use entries 114 * it references. 115 */ 116 FreeIfNeeded(ep->tterm.str_table); 117 118 next = ep->next; 119 120 free(ep); 121 if (ep == _nc_head) 122 _nc_head = 0; 123 if (ep == _nc_tail) 124 _nc_tail = 0; 125 } 126 } 127 128 static char * 129 force_bar(char *dst, char *src, size_t siz) 130 { 131 if (strchr(src, '|') == 0) { 132 size_t len; 133 134 len = strlcpy(dst, src, siz); 135 if (len >= siz - 2) 136 len = siz - 2;; 137 (void) strcpy(dst + len, "|"); 138 src = dst; 139 } 140 return src; 141 } 142 143 NCURSES_EXPORT(bool) 144 _nc_entry_match(char *n1, char *n2) 145 /* do any of the aliases in a pair of terminal names match? */ 146 { 147 char *pstart, *qstart, *pend, *qend; 148 char nc1[MAX_NAME_SIZE + 2], nc2[MAX_NAME_SIZE + 2]; 149 150 n1 = force_bar(nc1, n1, sizeof(nc1)); 151 n2 = force_bar(nc2, n2, sizeof(nc2)); 152 153 for (pstart = n1; (pend = strchr(pstart, '|')); pstart = pend + 1) 154 for (qstart = n2; (qend = strchr(qstart, '|')); qstart = qend + 1) 155 if ((pend - pstart == qend - qstart) 156 && memcmp(pstart, qstart, (size_t) (pend - pstart)) == 0) 157 return (TRUE); 158 159 return (FALSE); 160 } 161 162 /**************************************************************************** 163 * 164 * Entry compiler and resolution logic 165 * 166 ****************************************************************************/ 167 168 NCURSES_EXPORT(void) 169 _nc_read_entry_source(FILE * fp, char *buf, 170 int literal, bool silent, 171 bool(*hook) (ENTRY *)) 172 /* slurp all entries in the given file into core */ 173 { 174 ENTRY thisentry; 175 bool oldsuppress = _nc_suppress_warnings; 176 int immediate = 0; 177 178 if (silent) 179 _nc_suppress_warnings = TRUE; /* shut the lexer up, too */ 180 181 _nc_reset_input(fp, buf); 182 for (;;) { 183 memset(&thisentry, 0, sizeof(thisentry)); 184 if (_nc_parse_entry(&thisentry, literal, silent) == ERR) 185 break; 186 if (!isalnum(CharOf(thisentry.tterm.term_names[0]))) 187 _nc_err_abort("terminal names must start with letter or digit"); 188 189 /* 190 * This can be used for immediate compilation of entries with no 191 * use references to disk, so as to avoid chewing up a lot of 192 * core when the resolution code could fetch entries off disk. 193 */ 194 if (hook != NULLHOOK && (*hook) (&thisentry)) 195 immediate++; 196 else 197 enqueue(&thisentry); 198 } 199 200 if (_nc_tail) { 201 /* set up the head pointer */ 202 for (_nc_head = _nc_tail; _nc_head->last; _nc_head = _nc_head->last) 203 continue; 204 205 DEBUG(1, ("head = %s", _nc_head->tterm.term_names)); 206 DEBUG(1, ("tail = %s", _nc_tail->tterm.term_names)); 207 } 208 #ifdef TRACE 209 else if (!immediate) 210 DEBUG(1, ("no entries parsed")); 211 #endif 212 213 _nc_suppress_warnings = oldsuppress; 214 } 215 216 NCURSES_EXPORT(int) 217 _nc_resolve_uses(bool fullresolve) 218 /* try to resolve all use capabilities */ 219 { 220 ENTRY *qp, *rp, *lastread = 0; 221 bool keepgoing; 222 int i, j, unresolved, total_unresolved, multiples; 223 224 DEBUG(2, ("RESOLUTION BEGINNING")); 225 226 /* 227 * Check for multiple occurrences of the same name. 228 */ 229 multiples = 0; 230 for_entry_list(qp) { 231 int matchcount = 0; 232 233 for_entry_list(rp) { 234 if (qp > rp 235 && _nc_entry_match(qp->tterm.term_names, rp->tterm.term_names)) { 236 matchcount++; 237 if (matchcount == 1) { 238 (void) fprintf(stderr, "Name collision between %s", 239 _nc_first_name(qp->tterm.term_names)); 240 multiples++; 241 } 242 if (matchcount >= 1) 243 (void) fprintf(stderr, " %s", _nc_first_name(rp->tterm.term_names)); 244 } 245 } 246 if (matchcount >= 1) 247 (void) putc('\n', stderr); 248 } 249 if (multiples > 0) 250 return (FALSE); 251 252 DEBUG(2, ("NO MULTIPLE NAME OCCURRENCES")); 253 254 /* 255 * First resolution stage: compute link pointers corresponding to names. 256 */ 257 total_unresolved = 0; 258 _nc_curr_col = -1; 259 for_entry_list(qp) { 260 unresolved = 0; 261 for (i = 0; i < qp->nuses; i++) { 262 bool foundit; 263 char *child = _nc_first_name(qp->tterm.term_names); 264 char *lookfor = qp->uses[i].name; 265 long lookline = qp->uses[i].line; 266 267 foundit = FALSE; 268 269 _nc_set_type(child); 270 271 /* first, try to resolve from in-core records */ 272 for_entry_list(rp) { 273 if (rp != qp 274 && _nc_name_match(rp->tterm.term_names, lookfor, "|")) { 275 DEBUG(2, ("%s: resolving use=%s (in core)", 276 child, lookfor)); 277 278 qp->uses[i].link = rp; 279 foundit = TRUE; 280 } 281 } 282 283 /* if that didn't work, try to merge in a compiled entry */ 284 if (!foundit) { 285 TERMTYPE thisterm; 286 char filename[PATH_MAX]; 287 288 memset(&thisterm, 0, sizeof(thisterm)); 289 if (_nc_read_entry(lookfor, filename, &thisterm) == 1) { 290 DEBUG(2, ("%s: resolving use=%s (compiled)", 291 child, lookfor)); 292 293 rp = typeMalloc(ENTRY, 1); 294 if (rp == 0) 295 _nc_err_abort("Out of memory"); 296 rp->tterm = thisterm; 297 rp->nuses = 0; 298 rp->next = lastread; 299 lastread = rp; 300 301 qp->uses[i].link = rp; 302 foundit = TRUE; 303 } 304 } 305 306 /* no good, mark this one unresolvable and complain */ 307 if (!foundit) { 308 unresolved++; 309 total_unresolved++; 310 311 _nc_curr_line = lookline; 312 _nc_warning("resolution of use=%s failed", lookfor); 313 qp->uses[i].link = 0; 314 } 315 } 316 } 317 if (total_unresolved) { 318 /* free entries read in off disk */ 319 _nc_free_entries(lastread); 320 return (FALSE); 321 } 322 323 DEBUG(2, ("NAME RESOLUTION COMPLETED OK")); 324 325 /* 326 * OK, at this point all (char *) references in `name' mwmbers 327 * have been successfully converred to (ENTRY *) pointers in 328 * `link' members. Time to do the actual merges. 329 */ 330 if (fullresolve) { 331 do { 332 TERMTYPE merged; 333 334 keepgoing = FALSE; 335 336 for_entry_list(qp) { 337 if (qp->nuses > 0) { 338 DEBUG(2, ("%s: attempting merge", 339 _nc_first_name(qp->tterm.term_names))); 340 /* 341 * If any of the use entries we're looking for is 342 * incomplete, punt. We'll catch this entry on a 343 * subsequent pass. 344 */ 345 for (i = 0; i < qp->nuses; i++) 346 if (qp->uses[i].link->nuses) { 347 DEBUG(2, ("%s: use entry %d unresolved", 348 _nc_first_name(qp->tterm.term_names), i)); 349 goto incomplete; 350 } 351 352 /* 353 * First, make sure there's no garbage in the 354 * merge block. as a side effect, copy into 355 * the merged entry the name field and string 356 * table pointer. 357 */ 358 _nc_copy_termtype(&merged, &(qp->tterm)); 359 360 /* 361 * Now merge in each use entry in the proper 362 * (reverse) order. 363 */ 364 for (; qp->nuses; qp->nuses--) 365 _nc_merge_entry(&merged, 366 &qp->uses[qp->nuses - 1].link->tterm); 367 368 /* 369 * Now merge in the original entry. 370 */ 371 _nc_merge_entry(&merged, &qp->tterm); 372 373 /* 374 * Replace the original entry with the merged one. 375 */ 376 FreeIfNeeded(qp->tterm.Booleans); 377 FreeIfNeeded(qp->tterm.Numbers); 378 FreeIfNeeded(qp->tterm.Strings); 379 qp->tterm = merged; 380 _nc_wrap_entry(qp, TRUE); 381 382 /* 383 * We know every entry is resolvable because name resolution 384 * didn't bomb. So go back for another pass. 385 */ 386 /* FALLTHRU */ 387 incomplete: 388 keepgoing = TRUE; 389 } 390 } 391 } while 392 (keepgoing); 393 394 DEBUG(2, ("MERGES COMPLETED OK")); 395 396 /* 397 * The exit condition of the loop above is such that all entries 398 * must now be resolved. Now handle cancellations. In a resolved 399 * entry there should be no cancellation markers. 400 */ 401 for_entry_list(qp) { 402 for_each_boolean(j, &(qp->tterm)) { 403 if ((int) qp->tterm.Booleans[j] == CANCELLED_BOOLEAN) 404 qp->tterm.Booleans[j] = ABSENT_BOOLEAN; 405 } 406 for_each_number(j, &(qp->tterm)) { 407 if (qp->tterm.Numbers[j] == CANCELLED_NUMERIC) 408 qp->tterm.Numbers[j] = ABSENT_NUMERIC; 409 } 410 for_each_string(j, &(qp->tterm)) { 411 if (qp->tterm.Strings[j] == CANCELLED_STRING) 412 qp->tterm.Strings[j] = ABSENT_STRING; 413 } 414 } 415 } 416 417 /* 418 * We'd like to free entries read in off disk at this point, but can't. 419 * The merge_entry() code doesn't copy the strings in the use entries, 420 * it just aliases them. If this ever changes, do a 421 * free_entries(lastread) here. 422 */ 423 424 DEBUG(2, ("RESOLUTION FINISHED")); 425 426 if (fullresolve) 427 if (_nc_check_termtype != 0) { 428 _nc_curr_col = -1; 429 for_entry_list(qp) { 430 _nc_curr_line = qp->startline; 431 _nc_set_type(_nc_first_name(qp->tterm.term_names)); 432 _nc_check_termtype(&qp->tterm); 433 } 434 DEBUG(2, ("SANITY CHECK FINISHED")); 435 } 436 437 return (TRUE); 438 } 439 440 /* 441 * This bit of legerdemain turns all the terminfo variable names into 442 * references to locations in the arrays Booleans, Numbers, and Strings --- 443 * precisely what's needed. 444 */ 445 446 #undef CUR 447 #define CUR tp-> 448 449 static void 450 sanity_check(TERMTYPE * tp) 451 { 452 if (!PRESENT(exit_attribute_mode)) { 453 #ifdef __UNUSED__ /* this casts too wide a net */ 454 bool terminal_entry = !strchr(tp->term_names, '+'); 455 if (terminal_entry && 456 (PRESENT(set_attributes) 457 || PRESENT(enter_standout_mode) 458 || PRESENT(enter_underline_mode) 459 || PRESENT(enter_blink_mode) 460 || PRESENT(enter_bold_mode) 461 || PRESENT(enter_dim_mode) 462 || PRESENT(enter_secure_mode) 463 || PRESENT(enter_protected_mode) 464 || PRESENT(enter_reverse_mode))) 465 _nc_warning("no exit_attribute_mode"); 466 #endif /* __UNUSED__ */ 467 PAIRED(enter_standout_mode, exit_standout_mode) 468 PAIRED(enter_underline_mode, exit_underline_mode) 469 } 470 471 /* listed in structure-member order of first argument */ 472 PAIRED(enter_alt_charset_mode, exit_alt_charset_mode); 473 ANDMISSING(enter_alt_charset_mode, acs_chars); 474 ANDMISSING(exit_alt_charset_mode, acs_chars); 475 ANDMISSING(enter_blink_mode, exit_attribute_mode); 476 ANDMISSING(enter_bold_mode, exit_attribute_mode); 477 PAIRED(exit_ca_mode, enter_ca_mode); 478 PAIRED(enter_delete_mode, exit_delete_mode); 479 ANDMISSING(enter_dim_mode, exit_attribute_mode); 480 PAIRED(enter_insert_mode, exit_insert_mode); 481 ANDMISSING(enter_secure_mode, exit_attribute_mode); 482 ANDMISSING(enter_protected_mode, exit_attribute_mode); 483 ANDMISSING(enter_reverse_mode, exit_attribute_mode); 484 PAIRED(from_status_line, to_status_line); 485 PAIRED(meta_off, meta_on); 486 487 PAIRED(prtr_on, prtr_off); 488 PAIRED(save_cursor, restore_cursor); 489 PAIRED(enter_xon_mode, exit_xon_mode); 490 PAIRED(enter_am_mode, exit_am_mode); 491 ANDMISSING(label_off, label_on); 492 PAIRED(display_clock, remove_clock); 493 ANDMISSING(set_color_pair, initialize_pair); 494 } 495