1 /* $OpenBSD: comp_parse.c,v 1.3 1999/03/02 06:23:28 millert Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998 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 38 /* 39 * comp_parse.c -- parser driver loop and use handling. 40 * 41 * _nc_read_entry_source(FILE *, literal, bool, bool (*hook)()) 42 * _nc_resolve_uses(void) 43 * _nc_free_entries(void) 44 * 45 * Use this code by calling _nc_read_entry_source() on as many source 46 * files as you like (either terminfo or termcap syntax). If you 47 * want use-resolution, call _nc_resolve_uses(). To free the list 48 * storage, do _nc_free_entries(). 49 * 50 */ 51 52 #include <curses.priv.h> 53 54 #include <ctype.h> 55 56 #include <tic.h> 57 #include <term_entry.h> 58 59 MODULE_ID("$From: comp_parse.c,v 1.34 1999/02/27 22:13:02 tom Exp $") 60 61 static void sanity_check(TERMTYPE *); 62 void (*_nc_check_termtype)(TERMTYPE *) = sanity_check; 63 64 /**************************************************************************** 65 * 66 * Entry queue handling 67 * 68 ****************************************************************************/ 69 /* 70 * The entry list is a doubly linked list with NULLs terminating the lists: 71 * 72 * --------- --------- --------- 73 * | | | | | | offset 74 * |-------| |-------| |-------| 75 * | ----+-->| ----+-->| NULL | next 76 * |-------| |-------| |-------| 77 * | NULL |<--+---- |<--+---- | last 78 * --------- --------- --------- 79 * ^ ^ 80 * | | 81 * | | 82 * _nc_head _nc_tail 83 */ 84 85 ENTRY *_nc_head, *_nc_tail; 86 87 static void enqueue(ENTRY *ep) 88 /* add an entry to the in-core list */ 89 { 90 ENTRY *newp = _nc_copy_entry(ep); 91 92 if (newp == NULL) 93 _nc_err_abort("Out of memory"); 94 95 newp->last = _nc_tail; 96 _nc_tail = newp; 97 98 newp->next = (ENTRY *)NULL; 99 if (newp->last) 100 newp->last->next = newp; 101 } 102 103 void _nc_free_entries(ENTRY *head) 104 /* free the allocated storage consumed by list entries */ 105 { 106 ENTRY *ep, *next; 107 108 for (ep = head; ep; ep = next) 109 { 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) _nc_head = 0; 122 if (ep == _nc_tail) _nc_tail = 0; 123 } 124 } 125 126 bool _nc_entry_match(char *n1, char *n2) 127 /* do any of the aliases in a pair of terminal names match? */ 128 { 129 char *pstart, *qstart, *pend, *qend; 130 char nc1[MAX_NAME_SIZE+1], nc2[MAX_NAME_SIZE+1]; 131 size_t n; 132 133 if (strchr(n1, '|') == NULL) 134 { 135 if ((n = strlcpy(nc1, n1, sizeof(nc1))) > sizeof(nc1) - 2) 136 n = sizeof(nc1) - 2; 137 nc1[n++] = '|'; 138 nc1[n] = '\0'; 139 n1 = nc1; 140 } 141 142 if (strchr(n2, '|') == NULL) 143 { 144 if ((n = strlcpy(nc2, n2, sizeof(nc2))) > sizeof(nc2) - 2) 145 n = sizeof(nc2) - 2; 146 nc2[n++] = '|'; 147 nc2[n] = '\0'; 148 n2 = nc2; 149 } 150 151 for (pstart = n1; (pend = strchr(pstart, '|')); pstart = pend + 1) 152 for (qstart = n2; (qend = strchr(qstart, '|')); qstart = qend + 1) 153 if ((pend-pstart == qend-qstart) 154 && memcmp(pstart, qstart, (size_t)(pend-pstart)) == 0) 155 return(TRUE); 156 157 return(FALSE); 158 } 159 160 /**************************************************************************** 161 * 162 * Entry compiler and resolution logic 163 * 164 ****************************************************************************/ 165 166 void _nc_read_entry_source(FILE *fp, char *buf, 167 int literal, bool silent, 168 bool (*hook)(ENTRY *)) 169 /* slurp all entries in the given file into core */ 170 { 171 ENTRY thisentry; 172 bool oldsuppress = _nc_suppress_warnings; 173 int immediate = 0; 174 175 if (silent) 176 _nc_suppress_warnings = TRUE; /* shut the lexer up, too */ 177 178 memset(&thisentry, 0, sizeof(thisentry)); 179 for (_nc_reset_input(fp, buf); _nc_parse_entry(&thisentry, literal, silent) != ERR; ) 180 { 181 if (!isalnum(thisentry.tterm.term_names[0])) 182 _nc_err_abort("terminal names must start with letter or digit"); 183 184 /* 185 * This can be used for immediate compilation of entries with no 186 * use references to disk, so as to avoid chewing up a lot of 187 * core when the resolution code could fetch entries off disk. 188 */ 189 if (hook != NULLHOOK && (*hook)(&thisentry)) 190 immediate++; 191 else 192 enqueue(&thisentry); 193 } 194 195 if (_nc_tail) 196 { 197 /* set up the head pointer */ 198 for (_nc_head = _nc_tail; _nc_head->last; _nc_head = _nc_head->last) 199 continue; 200 201 DEBUG(1, ("head = %s", _nc_head->tterm.term_names)); 202 DEBUG(1, ("tail = %s", _nc_tail->tterm.term_names)); 203 } 204 #ifdef TRACE 205 else if (!immediate) 206 DEBUG(1, ("no entries parsed")); 207 #endif 208 209 _nc_suppress_warnings = oldsuppress; 210 } 211 212 int _nc_resolve_uses(void) 213 /* try to resolve all use capabilities */ 214 { 215 ENTRY *qp, *rp, *lastread = NULL; 216 bool keepgoing; 217 int i, j, unresolved, total_unresolved, multiples; 218 219 DEBUG(2, ("RESOLUTION BEGINNING")); 220 221 /* 222 * Check for multiple occurrences of the same name. 223 */ 224 multiples = 0; 225 for_entry_list(qp) 226 { 227 int matchcount = 0; 228 229 for_entry_list(rp) 230 if (qp > rp 231 && _nc_entry_match(qp->tterm.term_names, rp->tterm.term_names)) 232 { 233 matchcount++; 234 if (matchcount == 1) 235 { 236 (void) fprintf(stderr, "Name collision between %s", 237 _nc_first_name(qp->tterm.term_names)); 238 multiples++; 239 } 240 if (matchcount >= 1) 241 (void) fprintf(stderr, " %s", _nc_first_name(rp->tterm.term_names)); 242 } 243 if (matchcount >= 1) 244 (void) putc('\n', stderr); 245 } 246 if (multiples > 0) 247 return(FALSE); 248 249 DEBUG(2, ("NO MULTIPLE NAME OCCURRENCES")); 250 251 /* 252 * First resolution stage: replace names in use arrays with entry 253 * pointers. By doing this, we avoid having to do the same name 254 * match once for each time a use entry is itself unresolved. 255 */ 256 total_unresolved = 0; 257 _nc_curr_col = -1; 258 for_entry_list(qp) 259 { 260 unresolved = 0; 261 for (i = 0; i < qp->nuses; i++) 262 { 263 bool foundit; 264 char *child = _nc_first_name(qp->tterm.term_names); 265 char *lookfor = (char *)(qp->uses[i].parent); 266 long lookline = qp->uses[i].line; 267 268 foundit = FALSE; 269 270 _nc_set_type(child); 271 272 /* first, try to resolve from in-core records */ 273 for_entry_list(rp) 274 if (rp != qp 275 && _nc_name_match(rp->tterm.term_names, lookfor, "|")) 276 { 277 DEBUG(2, ("%s: resolving use=%s (in core)", 278 child, lookfor)); 279 280 qp->uses[i].parent = rp; 281 foundit = TRUE; 282 } 283 284 /* if that didn't work, try to merge in a compiled entry */ 285 if (!foundit) 286 { 287 TERMTYPE thisterm; 288 char filename[PATH_MAX]; 289 290 memset(&thisterm, 0, sizeof(thisterm)); 291 if (_nc_read_entry(lookfor, filename, &thisterm) == 1) 292 { 293 DEBUG(2, ("%s: resolving use=%s (compiled)", 294 child, lookfor)); 295 296 rp = typeMalloc(ENTRY,1); 297 if (rp == NULL) 298 _nc_err_abort("Out of memory"); 299 rp->tterm = thisterm; 300 rp->nuses = 0; 301 rp->next = lastread; 302 lastread = rp; 303 304 qp->uses[i].parent = rp; 305 foundit = TRUE; 306 } 307 } 308 309 /* no good, mark this one unresolvable and complain */ 310 if (!foundit) 311 { 312 unresolved++; 313 total_unresolved++; 314 315 _nc_curr_line = lookline; 316 _nc_warning("resolution of use=%s failed", lookfor); 317 qp->uses[i].parent = (ENTRY *)NULL; 318 } 319 } 320 } 321 if (total_unresolved) 322 { 323 /* free entries read in off disk */ 324 _nc_free_entries(lastread); 325 return(FALSE); 326 } 327 328 DEBUG(2, ("NAME RESOLUTION COMPLETED OK")); 329 330 /* 331 * OK, at this point all (char *) references have been successfully 332 * replaced by (ENTRY *) pointers. Time to do the actual merges. 333 */ 334 do { 335 TERMTYPE merged; 336 337 keepgoing = FALSE; 338 339 for_entry_list(qp) 340 { 341 if (qp->nuses > 0) 342 { 343 DEBUG(2, ("%s: attempting merge", _nc_first_name(qp->tterm.term_names))); 344 /* 345 * If any of the use entries we're looking for is 346 * incomplete, punt. We'll catch this entry on a 347 * subsequent pass. 348 */ 349 for (i = 0; i < qp->nuses; i++) 350 if (((ENTRY *)qp->uses[i].parent)->nuses) 351 { 352 DEBUG(2, ("%s: use entry %d unresolved", 353 _nc_first_name(qp->tterm.term_names), i)); 354 goto incomplete; 355 } 356 357 /* 358 * First, make sure there's no garbage in the merge block. 359 * as a side effect, copy into the merged entry the name 360 * field and string table pointer. 361 */ 362 _nc_copy_termtype(&merged, &(qp->tterm)); 363 364 /* 365 * Now merge in each use entry in the proper 366 * (reverse) order. 367 */ 368 for (; qp->nuses; qp->nuses--) 369 _nc_merge_entry(&merged, 370 &((ENTRY *)qp->uses[qp->nuses-1].parent)->tterm); 371 372 /* 373 * Now merge in the original entry. 374 */ 375 _nc_merge_entry(&merged, &qp->tterm); 376 377 /* 378 * Replace the original entry with the merged one. 379 */ 380 FreeIfNeeded(qp->tterm.Booleans); 381 FreeIfNeeded(qp->tterm.Numbers); 382 FreeIfNeeded(qp->tterm.Strings); 383 qp->tterm = merged; 384 385 /* 386 * We know every entry is resolvable because name resolution 387 * didn't bomb. So go back for another pass. 388 */ 389 /* FALLTHRU */ 390 incomplete: 391 keepgoing = TRUE; 392 } 393 } 394 } while 395 (keepgoing); 396 397 DEBUG(2, ("MERGES COMPLETED OK")); 398 399 /* 400 * The exit condition of the loop above is such that all entries 401 * must now be resolved. Now handle cancellations. In a resolved 402 * entry there should be no cancellation markers. 403 */ 404 for_entry_list(qp) 405 { 406 for_each_boolean(j, &(qp->tterm)) 407 if (qp->tterm.Booleans[j] == CANCELLED_BOOLEAN) 408 qp->tterm.Booleans[j] = FALSE; 409 for_each_number(j, &(qp->tterm)) 410 if (qp->tterm.Numbers[j] == CANCELLED_NUMERIC) 411 qp->tterm.Numbers[j] = ABSENT_NUMERIC; 412 for_each_string(j, &(qp->tterm)) 413 if (qp->tterm.Strings[j] == CANCELLED_STRING) 414 qp->tterm.Strings[j] = ABSENT_STRING; 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 (_nc_check_termtype != 0) 427 { 428 _nc_curr_col = -1; 429 for_entry_list(qp) 430 { 431 _nc_curr_line = qp->startline; 432 _nc_set_type(_nc_first_name(qp->tterm.term_names)); 433 _nc_check_termtype(&qp->tterm); 434 } 435 DEBUG(2, ("SANITY CHECK FINISHED")); 436 } 437 438 return(TRUE); 439 } 440 441 /* 442 * This bit of legerdemain turns all the terminfo variable names into 443 * references to locations in the arrays Booleans, Numbers, and Strings --- 444 * precisely what's needed. 445 */ 446 447 #undef CUR 448 #define CUR tp-> 449 450 static void sanity_check(TERMTYPE *tp) 451 { 452 if (!PRESENT(exit_attribute_mode)) 453 { 454 #ifdef __UNUSED__ /* this casts too wide a net */ 455 bool terminal_entry = !strchr(tp->term_names, '+'); 456 if (terminal_entry && 457 (PRESENT(set_attributes) 458 || PRESENT(enter_standout_mode) 459 || PRESENT(enter_underline_mode) 460 || PRESENT(enter_blink_mode) 461 || PRESENT(enter_bold_mode) 462 || PRESENT(enter_dim_mode) 463 || PRESENT(enter_secure_mode) 464 || PRESENT(enter_protected_mode) 465 || PRESENT(enter_reverse_mode))) 466 _nc_warning("no exit_attribute_mode"); 467 #endif /* __UNUSED__ */ 468 PAIRED(enter_standout_mode, exit_standout_mode) 469 PAIRED(enter_underline_mode, exit_underline_mode) 470 } 471 472 /* listed in structure-member order of first argument */ 473 PAIRED(enter_alt_charset_mode, exit_alt_charset_mode) 474 ANDMISSING(enter_alt_charset_mode, acs_chars) 475 ANDMISSING(exit_alt_charset_mode, acs_chars) 476 ANDMISSING(enter_blink_mode, exit_attribute_mode) 477 ANDMISSING(enter_bold_mode, exit_attribute_mode) 478 PAIRED(exit_ca_mode, enter_ca_mode) 479 PAIRED(enter_delete_mode, exit_delete_mode) 480 ANDMISSING(enter_dim_mode, exit_attribute_mode) 481 PAIRED(enter_insert_mode, exit_insert_mode) 482 ANDMISSING(enter_secure_mode, exit_attribute_mode) 483 ANDMISSING(enter_protected_mode, exit_attribute_mode) 484 ANDMISSING(enter_reverse_mode, exit_attribute_mode) 485 PAIRED(from_status_line, to_status_line) 486 PAIRED(meta_off, meta_on) 487 488 PAIRED(prtr_on, prtr_off) 489 PAIRED(save_cursor, restore_cursor) 490 PAIRED(enter_xon_mode, exit_xon_mode) 491 PAIRED(enter_am_mode, exit_am_mode) 492 ANDMISSING(label_off, label_on) 493 PAIRED(display_clock, remove_clock) 494 ANDMISSING(set_color_pair, initialize_pair) 495 } 496