1 /**************************************************************************** 2 * Copyright (c) 1998,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: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 31 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 32 ****************************************************************************/ 33 34 /* 35 * comp_scan.c --- Lexical scanner for terminfo compiler. 36 * 37 * _nc_reset_input() 38 * _nc_get_token() 39 * _nc_panic_mode() 40 * int _nc_syntax; 41 * int _nc_curr_line; 42 * long _nc_curr_file_pos; 43 * long _nc_comment_start; 44 * long _nc_comment_end; 45 */ 46 47 #include <curses.priv.h> 48 49 #include <ctype.h> 50 #include <term_entry.h> 51 #include <tic.h> 52 53 MODULE_ID("$From: comp_scan.c,v 1.42 2000/04/01 19:08:36 tom Exp $") 54 55 /* 56 * Maximum length of string capability we'll accept before raising an error. 57 * Yes, there is a real capability in /etc/termcap this long, an "is". 58 */ 59 #define MAXCAPLEN 600 60 61 #define iswhite(ch) (ch == ' ' || ch == '\t') 62 63 int _nc_syntax = 0; /* termcap or terminfo? */ 64 long _nc_curr_file_pos = 0; /* file offset of current line */ 65 long _nc_comment_start = 0; /* start of comment range before name */ 66 long _nc_comment_end = 0; /* end of comment range before name */ 67 long _nc_start_line = 0; /* start line of current entry */ 68 69 /***************************************************************************** 70 * 71 * Token-grabbing machinery 72 * 73 *****************************************************************************/ 74 75 static bool first_column; /* See 'next_char()' below */ 76 static char separator; /* capability separator */ 77 static int pushtype; /* type of pushback token */ 78 static char pushname[MAX_NAME_SIZE + 1]; 79 80 static int last_char(void); 81 static int next_char(void); 82 static long stream_pos(void); 83 static bool end_of_stream(void); 84 static void push_back(char c); 85 86 /* Assume we may be looking at a termcap-style continuation */ 87 static inline int 88 eat_escaped_newline(int ch) 89 { 90 if (ch == '\\') 91 while ((ch = next_char()) == '\n' || iswhite(ch)) 92 continue; 93 return ch; 94 } 95 96 /* 97 * int 98 * get_token() 99 * 100 * Scans the input for the next token, storing the specifics in the 101 * global structure 'curr_token' and returning one of the following: 102 * 103 * NAMES A line beginning in column 1. 'name' 104 * will be set to point to everything up to but 105 * not including the first separator on the line. 106 * BOOLEAN An entry consisting of a name followed by 107 * a separator. 'name' will be set to point to 108 * the name of the capability. 109 * NUMBER An entry of the form 110 * name#digits, 111 * 'name' will be set to point to the capability 112 * name and 'valnumber' to the number given. 113 * STRING An entry of the form 114 * name=characters, 115 * 'name' is set to the capability name and 116 * 'valstring' to the string of characters, with 117 * input translations done. 118 * CANCEL An entry of the form 119 * name@, 120 * 'name' is set to the capability name and 121 * 'valnumber' to -1. 122 * EOF The end of the file has been reached. 123 * 124 * A `separator' is either a comma or a semicolon, depending on whether 125 * we are in termcap or terminfo mode. 126 * 127 */ 128 129 int 130 _nc_get_token(void) 131 { 132 static const char terminfo_punct[] = "@%&*!#"; 133 long number; 134 int type; 135 int ch; 136 char *numchk; 137 char numbuf[80]; 138 unsigned found; 139 static char buffer[MAX_ENTRY_SIZE]; 140 char *ptr; 141 int dot_flag = FALSE; 142 long token_start; 143 144 if (pushtype != NO_PUSHBACK) { 145 int retval = pushtype; 146 147 _nc_set_type(pushname); 148 DEBUG(3, ("pushed-back token: `%s', class %d", 149 _nc_curr_token.tk_name, pushtype)); 150 151 pushtype = NO_PUSHBACK; 152 pushname[0] = '\0'; 153 154 /* currtok wasn't altered by _nc_push_token() */ 155 return (retval); 156 } 157 158 if (end_of_stream()) 159 return (EOF); 160 161 start_token: 162 token_start = stream_pos(); 163 while ((ch = next_char()) == '\n' || iswhite(ch)) 164 continue; 165 166 ch = eat_escaped_newline(ch); 167 168 if (ch == EOF) 169 type = EOF; 170 else { 171 /* if this is a termcap entry, skip a leading separator */ 172 if (separator == ':' && ch == ':') 173 ch = next_char(); 174 175 if (ch == '.' 176 #ifdef NCURSES_EXT_FUNCS 177 && !_nc_disable_period 178 #endif 179 ) { 180 dot_flag = TRUE; 181 DEBUG(8, ("dot-flag set")); 182 183 while ((ch = next_char()) == '.' || iswhite(ch)) 184 continue; 185 } 186 187 if (ch == EOF) { 188 type = EOF; 189 goto end_of_token; 190 } 191 192 /* have to make some punctuation chars legal for terminfo */ 193 if (!isalnum(ch) 194 #ifdef NCURSES_EXT_FUNCS 195 && !(ch == '.' && _nc_disable_period) 196 #endif 197 && !strchr(terminfo_punct, (char) ch)) { 198 _nc_warning("Illegal character (expected alphanumeric or %s) - %s", 199 terminfo_punct, unctrl(ch)); 200 _nc_panic_mode(separator); 201 goto start_token; 202 } 203 204 ptr = buffer; 205 *(ptr++) = ch; 206 207 if (first_column) { 208 char *desc; 209 210 _nc_comment_start = token_start; 211 _nc_comment_end = _nc_curr_file_pos; 212 _nc_start_line = _nc_curr_line; 213 214 _nc_syntax = ERR; 215 while ((ch = next_char()) != '\n') { 216 if (ch == EOF) 217 _nc_err_abort("premature EOF"); 218 else if (ch == ':' && last_char() != ',') { 219 _nc_syntax = SYN_TERMCAP; 220 separator = ':'; 221 break; 222 } else if (ch == ',') { 223 _nc_syntax = SYN_TERMINFO; 224 separator = ','; 225 /* 226 * Fall-through here is not an accident. 227 * The idea is that if we see a comma, we 228 * figure this is terminfo unless we 229 * subsequently run into a colon -- but 230 * we don't stop looking for that colon until 231 * hitting a newline. This allows commas to 232 * be embedded in description fields of 233 * either syntax. 234 */ 235 /* FALLTHRU */ 236 } else 237 ch = eat_escaped_newline(ch); 238 239 *ptr++ = ch; 240 } 241 ptr[0] = '\0'; 242 if (_nc_syntax == ERR) { 243 /* 244 * Grrr...what we ought to do here is barf, 245 * complaining that the entry is malformed. 246 * But because a couple of name fields in the 247 * 8.2 termcap file end with |\, we just have 248 * to assume it's termcap syntax. 249 */ 250 _nc_syntax = SYN_TERMCAP; 251 separator = ':'; 252 } else if (_nc_syntax == SYN_TERMINFO) { 253 /* throw away trailing /, *$/ */ 254 for (--ptr; iswhite(*ptr) || *ptr == ','; ptr--) 255 continue; 256 ptr[1] = '\0'; 257 } 258 259 /* 260 * This is the soonest we have the terminal name 261 * fetched. Set up for following warning messages. 262 */ 263 ptr = strchr(buffer, '|'); 264 if (ptr == (char *) NULL) 265 ptr = buffer + strlen(buffer); 266 ch = *ptr; 267 *ptr = '\0'; 268 _nc_set_type(buffer); 269 *ptr = ch; 270 271 /* 272 * Compute the boundary between the aliases and the 273 * description field for syntax-checking purposes. 274 */ 275 desc = strrchr(buffer, '|'); 276 if (desc) { 277 if (*desc == '\0') 278 _nc_warning("empty longname field"); 279 else if (strchr(desc, ' ') == (char *) NULL) 280 _nc_warning("older tic versions may treat the description field as an alias"); 281 } 282 if (!desc) 283 desc = buffer + strlen(buffer); 284 285 /* 286 * Whitespace in a name field other than the long name 287 * can confuse rdist and some termcap tools. Slashes 288 * are a no-no. Other special characters can be 289 * dangerous due to shell expansion. 290 */ 291 for (ptr = buffer; ptr < desc; ptr++) { 292 if (isspace(*ptr)) { 293 _nc_warning("whitespace in name or alias field"); 294 break; 295 } else if (*ptr == '/') { 296 _nc_warning("slashes aren't allowed in names or aliases"); 297 break; 298 } else if (strchr("$[]!*?", *ptr)) { 299 _nc_warning("dubious character `%c' in name or alias field", *ptr); 300 break; 301 } 302 } 303 304 ptr = buffer; 305 306 _nc_curr_token.tk_name = buffer; 307 type = NAMES; 308 } else { 309 while ((ch = next_char()) != EOF) { 310 if (!isalnum(ch)) { 311 if (_nc_syntax == SYN_TERMINFO) { 312 if (ch != '_') 313 break; 314 } else { /* allow ';' for "k;" */ 315 if (ch != ';') 316 break; 317 } 318 } 319 *(ptr++) = ch; 320 } 321 322 *ptr++ = '\0'; 323 switch (ch) { 324 case ',': 325 case ':': 326 if (ch != separator) 327 _nc_err_abort("Separator inconsistent with syntax"); 328 _nc_curr_token.tk_name = buffer; 329 type = BOOLEAN; 330 break; 331 case '@': 332 if ((ch = next_char()) != separator) 333 _nc_warning("Missing separator after `%s', have %s", 334 buffer, unctrl(ch)); 335 _nc_curr_token.tk_name = buffer; 336 type = CANCEL; 337 break; 338 339 case '#': 340 found = 0; 341 while (isalnum(ch = next_char())) { 342 numbuf[found++] = ch; 343 if (found >= sizeof(numbuf) - 1) 344 break; 345 } 346 numbuf[found] = '\0'; 347 number = strtol(numbuf, &numchk, 0); 348 if (numchk == numbuf) 349 _nc_warning("no value given for `%s'", buffer); 350 if ((*numchk != '\0') || (ch != separator)) 351 _nc_warning("Missing separator"); 352 _nc_curr_token.tk_name = buffer; 353 _nc_curr_token.tk_valnumber = number; 354 type = NUMBER; 355 break; 356 357 case '=': 358 ch = _nc_trans_string(ptr, buffer + sizeof(buffer)); 359 if (ch != separator) 360 _nc_warning("Missing separator"); 361 _nc_curr_token.tk_name = buffer; 362 _nc_curr_token.tk_valstring = ptr; 363 type = STRING; 364 break; 365 366 case EOF: 367 type = EOF; 368 break; 369 default: 370 /* just to get rid of the compiler warning */ 371 type = UNDEF; 372 _nc_warning("Illegal character - %s", unctrl(ch)); 373 } 374 } /* end else (first_column == FALSE) */ 375 } /* end else (ch != EOF) */ 376 377 end_of_token: 378 379 #ifdef TRACE 380 if (dot_flag == TRUE) 381 DEBUG(8, ("Commented out ")); 382 383 if (_nc_tracing >= DEBUG_LEVEL(7)) { 384 switch (type) { 385 case BOOLEAN: 386 _tracef("Token: Boolean; name='%s'", 387 _nc_curr_token.tk_name); 388 break; 389 390 case NUMBER: 391 _tracef("Token: Number; name='%s', value=%d", 392 _nc_curr_token.tk_name, 393 _nc_curr_token.tk_valnumber); 394 break; 395 396 case STRING: 397 _tracef("Token: String; name='%s', value=%s", 398 _nc_curr_token.tk_name, 399 _nc_visbuf(_nc_curr_token.tk_valstring)); 400 break; 401 402 case CANCEL: 403 _tracef("Token: Cancel; name='%s'", 404 _nc_curr_token.tk_name); 405 break; 406 407 case NAMES: 408 409 _tracef("Token: Names; value='%s'", 410 _nc_curr_token.tk_name); 411 break; 412 413 case EOF: 414 _tracef("Token: End of file"); 415 break; 416 417 default: 418 _nc_warning("Bad token type"); 419 } 420 } 421 #endif 422 423 if (dot_flag == TRUE) /* if commented out, use the next one */ 424 type = _nc_get_token(); 425 426 DEBUG(3, ("token: `%s', class %d", _nc_curr_token.tk_name, type)); 427 428 return (type); 429 } 430 431 /* 432 * char 433 * trans_string(ptr) 434 * 435 * Reads characters using next_char() until encountering a separator, nl, 436 * or end-of-file. The returned value is the character which caused 437 * reading to stop. The following translations are done on the input: 438 * 439 * ^X goes to ctrl-X (i.e. X & 037) 440 * {\E,\n,\r,\b,\t,\f} go to 441 * {ESCAPE,newline,carriage-return,backspace,tab,formfeed} 442 * {\^,\\} go to {carat,backslash} 443 * \ddd (for ddd = up to three octal digits) goes to the character ddd 444 * 445 * \e == \E 446 * \0 == \200 447 * 448 */ 449 450 char 451 _nc_trans_string(char *ptr, char *last) 452 { 453 int count = 0; 454 int number; 455 int i, c; 456 chtype ch, last_ch = '\0'; 457 bool ignored = FALSE; 458 bool long_warning = FALSE; 459 460 while ((ch = c = next_char()) != (chtype) separator && c != EOF) { 461 if (ptr == (last - 1)) 462 break; 463 if ((_nc_syntax == SYN_TERMCAP) && c == '\n') 464 break; 465 if (ch == '^' && last_ch != '%') { 466 ch = c = next_char(); 467 if (c == EOF) 468 _nc_err_abort("Premature EOF"); 469 470 if (!(is7bits(ch) && isprint(ch))) { 471 _nc_warning("Illegal ^ character - %s", unctrl(ch)); 472 } 473 if (ch == '?') { 474 *(ptr++) = '\177'; 475 } else { 476 if ((ch &= 037) == 0) 477 ch = 128; 478 *(ptr++) = (char) (ch); 479 } 480 } else if (ch == '\\') { 481 ch = c = next_char(); 482 if (c == EOF) 483 _nc_err_abort("Premature EOF"); 484 485 if (ch >= '0' && ch <= '7') { 486 number = ch - '0'; 487 for (i = 0; i < 2; i++) { 488 ch = c = next_char(); 489 if (c == EOF) 490 _nc_err_abort("Premature EOF"); 491 492 if (c < '0' || c > '7') { 493 if (isdigit(c)) { 494 _nc_warning("Non-octal digit `%c' in \\ sequence", c); 495 /* allow the digit; it'll do less harm */ 496 } else { 497 push_back((char) c); 498 break; 499 } 500 } 501 502 number = number * 8 + c - '0'; 503 } 504 505 if (number == 0) 506 number = 0200; 507 *(ptr++) = (char) number; 508 } else { 509 switch (c) { 510 case 'E': 511 case 'e': 512 *(ptr++) = '\033'; 513 break; 514 515 case 'a': 516 *(ptr++) = '\007'; 517 break; 518 519 case 'l': 520 case 'n': 521 *(ptr++) = '\n'; 522 break; 523 524 case 'r': 525 *(ptr++) = '\r'; 526 break; 527 528 case 'b': 529 *(ptr++) = '\010'; 530 break; 531 532 case 's': 533 *(ptr++) = ' '; 534 break; 535 536 case 'f': 537 *(ptr++) = '\014'; 538 break; 539 540 case 't': 541 *(ptr++) = '\t'; 542 break; 543 544 case '\\': 545 *(ptr++) = '\\'; 546 break; 547 548 case '^': 549 *(ptr++) = '^'; 550 break; 551 552 case ',': 553 *(ptr++) = ','; 554 break; 555 556 case ':': 557 *(ptr++) = ':'; 558 break; 559 560 case '\n': 561 continue; 562 563 default: 564 _nc_warning("Illegal character %s in \\ sequence", 565 unctrl(ch)); 566 *(ptr++) = (char) ch; 567 } /* endswitch (ch) */ 568 } /* endelse (ch < '0' || ch > '7') */ 569 } 570 /* end else if (ch == '\\') */ 571 else if (ch == '\n' && (_nc_syntax == SYN_TERMINFO)) { 572 /* newlines embedded in a terminfo string are ignored */ 573 ignored = TRUE; 574 } else { 575 *(ptr++) = (char) ch; 576 } 577 578 if (!ignored) { 579 last_ch = ch; 580 count++; 581 } 582 ignored = FALSE; 583 584 if (count > MAXCAPLEN && !long_warning) { 585 _nc_warning("Very long string found. Missing separator?"); 586 long_warning = TRUE; 587 } 588 } /* end while */ 589 590 *ptr = '\0'; 591 592 return (ch); 593 } 594 595 /* 596 * _nc_push_token() 597 * 598 * Push a token of given type so that it will be reread by the next 599 * get_token() call. 600 */ 601 602 void 603 _nc_push_token(int tokclass) 604 { 605 /* 606 * This implementation is kind of bogus, it will fail if we ever do 607 * more than one pushback at a time between get_token() calls. It 608 * relies on the fact that curr_tok is static storage that nothing 609 * but get_token() touches. 610 */ 611 pushtype = tokclass; 612 _nc_get_type(pushname); 613 614 DEBUG(3, ("pushing token: `%s', class %d", 615 _nc_curr_token.tk_name, pushtype)); 616 } 617 618 /* 619 * Panic mode error recovery - skip everything until a "ch" is found. 620 */ 621 void 622 _nc_panic_mode(char ch) 623 { 624 int c; 625 626 for (;;) { 627 c = next_char(); 628 if (c == ch) 629 return; 630 if (c == EOF) 631 return; 632 } 633 } 634 635 /***************************************************************************** 636 * 637 * Character-stream handling 638 * 639 *****************************************************************************/ 640 641 #define LEXBUFSIZ 1024 642 643 static char *bufptr; /* otherwise, the input buffer pointer */ 644 static char *bufstart; /* start of buffer so we can compute offsets */ 645 static FILE *yyin; /* scanner's input file descriptor */ 646 647 /* 648 * _nc_reset_input() 649 * 650 * Resets the input-reading routines. Used on initialization, 651 * or after a seek has been done. Exactly one argument must be 652 * non-null. 653 */ 654 655 void 656 _nc_reset_input(FILE * fp, char *buf) 657 { 658 pushtype = NO_PUSHBACK; 659 pushname[0] = '\0'; 660 yyin = fp; 661 bufstart = bufptr = buf; 662 _nc_curr_file_pos = 0L; 663 if (fp != 0) 664 _nc_curr_line = 0; 665 _nc_curr_col = 0; 666 } 667 668 /* 669 * int last_char() 670 * 671 * Returns the final nonblank character on the current input buffer 672 */ 673 static int 674 last_char(void) 675 { 676 size_t len = strlen(bufptr); 677 while (len--) { 678 if (!isspace(bufptr[len])) 679 return bufptr[len]; 680 } 681 return 0; 682 } 683 684 /* 685 * int next_char() 686 * 687 * Returns the next character in the input stream. Comments and leading 688 * white space are stripped. 689 * 690 * The global state variable 'firstcolumn' is set TRUE if the character 691 * returned is from the first column of the input line. 692 * 693 * The global variable _nc_curr_line is incremented for each new line. 694 * The global variable _nc_curr_file_pos is set to the file offset of the 695 * beginning of each line. 696 */ 697 698 static int 699 next_char(void) 700 { 701 if (!yyin) { 702 if (*bufptr == '\0') 703 return (EOF); 704 if (*bufptr == '\n') { 705 _nc_curr_line++; 706 _nc_curr_col = 0; 707 } 708 } else if (!bufptr || !*bufptr) { 709 /* 710 * In theory this could be recoded to do its I/O one 711 * character at a time, saving the buffer space. In 712 * practice, this turns out to be quite hard to get 713 * completely right. Try it and see. If you succeed, 714 * don't forget to hack push_back() correspondingly. 715 */ 716 static char line[LEXBUFSIZ]; 717 size_t len; 718 719 do { 720 _nc_curr_file_pos = ftell(yyin); 721 722 if ((bufstart = fgets(line, LEXBUFSIZ, yyin)) != NULL) { 723 _nc_curr_line++; 724 _nc_curr_col = 0; 725 } 726 bufptr = bufstart; 727 } while 728 (bufstart != NULL && line[0] == '#'); 729 730 if (bufstart == NULL) 731 return (EOF); 732 733 while (iswhite(*bufptr)) 734 bufptr++; 735 736 /* 737 * Treat a trailing <cr><lf> the same as a <newline> so we can read 738 * files on OS/2, etc. 739 */ 740 if ((len = strlen(bufptr)) > 1) { 741 if (bufptr[len - 1] == '\n' 742 && bufptr[len - 2] == '\r') { 743 bufptr[len - 2] = '\n'; 744 bufptr[len - 1] = '\0'; 745 } 746 } 747 } 748 749 first_column = (bufptr == bufstart); 750 751 _nc_curr_col++; 752 return (*bufptr++); 753 } 754 755 static void 756 push_back(char c) 757 /* push a character back onto the input stream */ 758 { 759 if (bufptr == bufstart) 760 _nc_syserr_abort("Can't backspace off beginning of line"); 761 *--bufptr = c; 762 } 763 764 static long 765 stream_pos(void) 766 /* return our current character position in the input stream */ 767 { 768 return (yyin ? ftell(yyin) : (bufptr ? bufptr - bufstart : 0)); 769 } 770 771 static bool 772 end_of_stream(void) 773 /* are we at end of input? */ 774 { 775 return ((yyin ? feof(yyin) : (bufptr && *bufptr == '\0')) 776 ? TRUE : FALSE); 777 } 778 779 /* comp_scan.c ends here */ 780