1 2 #define yyerror_gettext(x) yyerror(gettext(x)) 3 # line 2 "egrep.y" 4 /* 5 * CDDL HEADER START 6 * 7 * The contents of this file are subject to the terms of the 8 * Common Development and Distribution License, Version 1.0 only 9 * (the "License"). You may not use this file except in compliance 10 * with the License. 11 * 12 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 13 * or http://www.opensolaris.org/os/licensing. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * 17 * When distributing Covered Code, include this CDDL HEADER in each 18 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 19 * If applicable, add the following below this CDDL HEADER, with the 20 * fields enclosed by brackets "[]" replaced with your own identifying 21 * information: Portions Copyright [yyyy] [name of copyright owner] 22 * 23 * CDDL HEADER END 24 */ 25 # define CHAR 257 26 # define MCHAR 258 27 # define DOT 259 28 # define MDOT 260 29 # define CCL 261 30 # define NCCL 262 31 # define MCCL 263 32 # define NMCCL 264 33 # define OR 265 34 # define CAT 266 35 # define STAR 267 36 # define PLUS 268 37 # define QUEST 269 38 39 # line 54 "egrep.y" 40 #include <stdio.h> 41 #include <ctype.h> 42 #include <memory.h> 43 #include <wchar.h> 44 #include <wctype.h> 45 #include <widec.h> 46 #include <stdlib.h> 47 #include <limits.h> 48 #include <locale.h> 49 50 #define STDIN_FILENAME gettext("(standard input)") 51 52 #define BLKSIZE 512 /* size of reported disk blocks */ 53 #define EBUFSIZ 8192 54 #define MAXLIN 350 55 #define NCHARS 256 56 #define MAXPOS 4000 57 #define NSTATES 64 58 #define FINAL -1 59 #define RIGHT '\n' /* serves as record separator and as $ */ 60 #define LEFT '\n' /* beginning of line */ 61 int gotofn[NSTATES][NCHARS]; 62 int state[NSTATES]; 63 int out[NSTATES]; 64 int line = 1; 65 int *name; 66 int *left; 67 int *right; 68 int *parent; 69 int *foll; 70 int *positions; 71 char *chars; 72 wchar_t *lower; 73 wchar_t *upper; 74 int maxlin, maxclin, maxwclin, maxpos; 75 int nxtpos = 0; 76 int inxtpos; 77 int nxtchar = 0; 78 int *tmpstat; 79 int *initstat; 80 int istat; 81 int nstate = 1; 82 int xstate; 83 int count; 84 int icount; 85 char *input; 86 87 88 wchar_t lyylval; 89 wchar_t nextch(); 90 wchar_t maxmin(); 91 int compare(); 92 void overflo(); 93 94 char reinit = 0; 95 96 long long lnum; 97 int bflag; 98 int cflag; 99 int eflag; 100 int fflag; 101 int Hflag; 102 int hflag; 103 int iflag; 104 int lflag; 105 int nflag; 106 int qflag; 107 int vflag; 108 int nfile; 109 long long blkno; 110 long long tln; 111 int nsucc; 112 int badbotch; 113 extern char *optarg; 114 extern int optind; 115 116 int f; 117 FILE *expfile; 118 119 #include <inttypes.h> 120 121 #ifdef __STDC__ 122 #include <stdlib.h> 123 #include <string.h> 124 #define YYCONST const 125 #else 126 #include <malloc.h> 127 #include <memory.h> 128 #define YYCONST 129 #endif 130 131 #include <values.h> 132 133 #if defined(__cplusplus) || defined(__STDC__) 134 135 #if defined(__cplusplus) && defined(__EXTERN_C__) 136 extern "C" { 137 #endif 138 #ifndef yyerror 139 #if defined(__cplusplus) 140 void yyerror(YYCONST char *); 141 #endif 142 #endif 143 #ifndef yylex 144 int yylex(void); 145 #endif 146 int yyparse(void); 147 #if defined(__cplusplus) && defined(__EXTERN_C__) 148 } 149 #endif 150 151 #endif 152 153 #define yyclearin yychar = -1 154 #define yyerrok yyerrflag = 0 155 extern int yychar; 156 extern int yyerrflag; 157 #ifndef YYSTYPE 158 #define YYSTYPE int 159 #endif 160 YYSTYPE yylval; 161 YYSTYPE yyval; 162 typedef int yytabelem; 163 #ifndef YYMAXDEPTH 164 #define YYMAXDEPTH 150 165 #endif 166 #if YYMAXDEPTH > 0 167 int yy_yys[YYMAXDEPTH], *yys = yy_yys; 168 YYSTYPE yy_yyv[YYMAXDEPTH], *yyv = yy_yyv; 169 #else /* user does initial allocation */ 170 int *yys; 171 YYSTYPE *yyv; 172 #endif 173 static int yymaxdepth = YYMAXDEPTH; 174 # define YYERRCODE 256 175 176 # line 196 "egrep.y" 177 178 void add(int *, int); 179 void clearg(void); 180 void execute(char *); 181 void follow(int); 182 int mgetc(void); 183 void synerror(void); 184 185 186 void 187 yyerror(char *s) 188 { 189 fprintf(stderr, "egrep: %s\n", s); 190 exit(2); 191 } 192 193 int 194 yylex(void) 195 { 196 extern int yylval; 197 int cclcnt, x, ccount, oldccount; 198 wchar_t c, lc; 199 200 c = nextch(); 201 switch(c) { 202 case '^': 203 yylval = LEFT; 204 return(CHAR); 205 case '$': 206 c = RIGHT; 207 goto defchar; 208 case '|': return (OR); 209 case '*': return (STAR); 210 case '+': return (PLUS); 211 case '?': return (QUEST); 212 case '(': return (c); 213 case ')': return (c); 214 case '.': return(DOT); 215 case '\0': return (0); 216 case RIGHT: return (OR); 217 case '[': 218 x = (multibyte ? MCCL : CCL); 219 cclcnt = 0; 220 count = nxtchar++; 221 if ((c = nextch()) == '^') { 222 x = (multibyte ? NMCCL : NCCL); 223 c = nextch(); 224 } 225 lc = 0; 226 do { 227 if (iflag && iswalpha(c)) 228 c = towlower(c); 229 if (c == '\0') synerror(); 230 if (c == '-' && cclcnt > 0 && lc != 0) { 231 if ((c = nextch()) != 0) { 232 if(c == ']') { 233 chars[nxtchar++] = '-'; 234 cclcnt++; 235 break; 236 } 237 if (iflag && iswalpha(c)) 238 c = towlower(c); 239 if (!multibyte || 240 (c & WCHAR_CSMASK) == (lc & WCHAR_CSMASK) && 241 lc < c && 242 !iswcntrl(c) && !iswcntrl(lc)) { 243 if (nxtchar >= maxclin) 244 if (allocchars() == 0) 245 overflo(); 246 chars[nxtchar++] = '-'; 247 cclcnt++; 248 } 249 } 250 } 251 ccount = oldccount = nxtchar; 252 if(ccount + MB_LEN_MAX >= maxclin) 253 if(allocchars() == 0) 254 overflo(); 255 ccount += wctomb(&chars[ccount], c); 256 cclcnt += ccount - oldccount; 257 nxtchar += ccount - oldccount; 258 lc = c; 259 } while ((c = nextch()) != ']'); 260 chars[count] = cclcnt; 261 return(x); 262 263 case '\\': 264 if ((c = nextch()) == '\0') synerror(); 265 defchar: 266 default: 267 if (c <= 0177) { 268 yylval = c; 269 return (CHAR); 270 } else { 271 lyylval = c; 272 return (MCHAR); 273 } 274 } 275 } 276 277 wchar_t 278 nextch(void) 279 { 280 wchar_t lc; 281 char multic[MB_LEN_MAX]; 282 int length, d; 283 if (fflag) { 284 if ((length = _mbftowc(multic, &lc, mgetc, &d)) < 0) 285 synerror(); 286 if(length == 0) 287 lc = '\0'; 288 } 289 else { 290 if((length = mbtowc(&lc, input, MB_LEN_MAX)) == -1) 291 synerror(); 292 if(length == 0) 293 return(0); 294 input += length; 295 } 296 return(lc); 297 } 298 299 int 300 mgetc(void) 301 { 302 return(getc(expfile)); 303 } 304 305 void 306 synerror(void) 307 { 308 fprintf(stderr, gettext("egrep: syntax error\n")); 309 exit(2); 310 } 311 312 int 313 enter(int x) 314 { 315 if(line >= maxlin) 316 if(alloctree() == 0) 317 overflo(); 318 name[line] = x; 319 left[line] = 0; 320 right[line] = 0; 321 return(line++); 322 } 323 324 int 325 cclenter(int x) 326 { 327 int linno; 328 linno = enter(x); 329 right[linno] = count; 330 return (linno); 331 } 332 333 int 334 node(int x, int l, int r) 335 { 336 if(line >= maxlin) 337 if(alloctree() == 0) 338 overflo(); 339 name[line] = x; 340 left[line] = l; 341 right[line] = r; 342 parent[l] = line; 343 parent[r] = line; 344 return(line++); 345 } 346 347 int 348 unary(int x, int d) 349 { 350 if(line >= maxlin) 351 if(alloctree() == 0) 352 overflo(); 353 name[line] = x; 354 left[line] = d; 355 right[line] = 0; 356 parent[d] = line; 357 return(line++); 358 } 359 360 int 361 allocchars(void) 362 { 363 maxclin += MAXLIN; 364 if((chars = realloc(chars, maxclin)) == (char *)0) 365 return 0; 366 return 1; 367 } 368 369 int 370 alloctree(void) 371 { 372 maxlin += MAXLIN; 373 if((name = (int *)realloc(name, maxlin*sizeof(int))) == (int *)0) 374 return 0; 375 if((left = (int *)realloc(left, maxlin*sizeof(int))) == (int *)0) 376 return 0; 377 if((right = (int *)realloc(right, maxlin*sizeof(int))) == (int *)0) 378 return 0; 379 if((parent = (int *)realloc(parent, maxlin*sizeof(int))) == (int *)0) 380 return 0; 381 if((foll = (int *)realloc(foll, maxlin*sizeof(int))) == (int *)0) 382 return 0; 383 if((tmpstat = (int *)realloc(tmpstat, maxlin*sizeof(int))) == (int *)0) 384 return 0; 385 if((initstat = (int *)realloc(initstat, maxlin*sizeof(int))) == (int *)0) 386 return 0; 387 return 1; 388 } 389 390 void 391 overflo(void) 392 { 393 fprintf(stderr, gettext("egrep: regular expression too long\n")); 394 exit(2); 395 } 396 397 void 398 cfoll(int v) 399 { 400 int i; 401 if (left[v] == 0) { 402 count = 0; 403 for (i=1; i<=line; i++) tmpstat[i] = 0; 404 follow(v); 405 add(foll, v); 406 } 407 else if (right[v] == 0) cfoll(left[v]); 408 else { 409 cfoll(left[v]); 410 cfoll(right[v]); 411 } 412 } 413 414 void 415 cgotofn(void) 416 { 417 int i; 418 count = 0; 419 inxtpos = nxtpos; 420 for (i=3; i<=line; i++) tmpstat[i] = 0; 421 if (cstate(line-1)==0) { 422 tmpstat[line] = 1; 423 count++; 424 out[1] = 1; 425 } 426 for (i=3; i<=line; i++) initstat[i] = tmpstat[i]; 427 count--; /*leave out position 1 */ 428 icount = count; 429 tmpstat[1] = 0; 430 add(state, 1); 431 istat = nxtst(1, LEFT); 432 } 433 434 int 435 nxtst(int s, int c) 436 { 437 int i, num, k; 438 int pos, curpos, number, newpos; 439 num = positions[state[s]]; 440 count = icount; 441 for (i=3; i<=line; i++) tmpstat[i] = initstat[i]; 442 pos = state[s] + 1; 443 for (i=0; i<num; i++) { 444 curpos = positions[pos]; 445 k = name[curpos]; 446 if (k >= 0) 447 if ( 448 (k == c) 449 || (k == DOT && dot(c)) 450 || (k == MDOT && mdot(c)) 451 || (k == CCL && dot(c) && member(c, right[curpos], 1)) 452 || (k == NCCL && dot(c) && member(c, right[curpos], 0)) 453 || (k == MCCL && mdot(c) && member(c, right[curpos], 1)) 454 ) { 455 number = positions[foll[curpos]]; 456 newpos = foll[curpos] + 1; 457 for (k=0; k<number; k++) { 458 if (tmpstat[positions[newpos]] != 1) { 459 tmpstat[positions[newpos]] = 1; 460 count++; 461 } 462 newpos++; 463 } 464 } 465 pos++; 466 } 467 if (notin(nstate)) { 468 if (++nstate >= NSTATES) { 469 for (i=1; i<NSTATES; i++) 470 out[i] = 0; 471 for (i=1; i<NSTATES; i++) 472 for (k=0; k<NCHARS; k++) 473 gotofn[i][k] = 0; 474 nstate = 1; 475 nxtpos = inxtpos; 476 reinit = 1; 477 add(state, nstate); 478 if (tmpstat[line] == 1) out[nstate] = 1; 479 return nstate; 480 } 481 add(state, nstate); 482 if (tmpstat[line] == 1) out[nstate] = 1; 483 gotofn[s][c] = nstate; 484 return nstate; 485 } 486 else { 487 gotofn[s][c] = xstate; 488 return xstate; 489 } 490 } 491 492 493 int 494 cstate(int v) 495 { 496 int b; 497 if (left[v] == 0) { 498 if (tmpstat[v] != 1) { 499 tmpstat[v] = 1; 500 count++; 501 } 502 return(1); 503 } 504 else if (right[v] == 0) { 505 if (cstate(left[v]) == 0) return (0); 506 else if (name[v] == PLUS) return (1); 507 else return (0); 508 } 509 else if (name[v] == CAT) { 510 if (cstate(left[v]) == 0 && cstate(right[v]) == 0) return (0); 511 else return (1); 512 } 513 else { /* name[v] == OR */ 514 b = cstate(right[v]); 515 if (cstate(left[v]) == 0 || b == 0) return (0); 516 else return (1); 517 } 518 } 519 520 521 int 522 dot(int c) 523 { 524 if(multibyte && c >= 0200 && (!iscntrl(c) || c == SS2 && eucw2 || c == SS3 && eucw3)) 525 return(0); 526 if(c == RIGHT || c == LEFT) 527 return(0); 528 return(1); 529 } 530 531 int 532 mdot(int c) 533 { 534 if(c >= 0200 && !iscntrl(c)) 535 return(1); 536 return(0); 537 } 538 539 int 540 member(int symb, int set, int torf) 541 { 542 int i, num, pos, c, lc; 543 if(symb == RIGHT || symb == LEFT) 544 return(0); 545 num = chars[set]; 546 pos = set + 1; 547 lc = 0; 548 if(iflag) 549 symb = tolower(symb); 550 for (i=0; i<num; i++) { 551 c = (unsigned char)chars[pos++]; 552 if(c == '-' && lc != 0 && ++i < num) { 553 c = (unsigned char)chars[pos++]; 554 if(lc <= symb && symb <= c) 555 return(torf); 556 } 557 if (symb == c) 558 return (torf); 559 lc = c; 560 } 561 return(!torf); 562 } 563 564 int 565 notin(int n) 566 { 567 int i, j, pos; 568 for (i=1; i<=n; i++) { 569 if (positions[state[i]] == count) { 570 pos = state[i] + 1; 571 for (j=0; j < count; j++) 572 if (tmpstat[positions[pos++]] != 1) goto nxt; 573 xstate = i; 574 return (0); 575 } 576 nxt: ; 577 } 578 return (1); 579 } 580 581 void 582 add(int *array, int n) 583 { 584 int i; 585 if (nxtpos + count >= maxpos) { 586 maxpos += MAXPOS + count; 587 if((positions = (int *)realloc(positions, maxpos *sizeof(int))) == (int *)0) 588 overflo(); 589 } 590 array[n] = nxtpos; 591 positions[nxtpos++] = count; 592 for (i=3; i <= line; i++) { 593 if (tmpstat[i] == 1) { 594 positions[nxtpos++] = i; 595 } 596 } 597 } 598 599 void 600 follow(int v) 601 { 602 int p; 603 if (v == line) return; 604 p = parent[v]; 605 switch(name[p]) { 606 case STAR: 607 case PLUS: cstate(v); 608 follow(p); 609 return; 610 611 case OR: 612 case QUEST: follow(p); 613 return; 614 615 case CAT: if (v == left[p]) { 616 if (cstate(right[p]) == 0) { 617 follow(p); 618 return; 619 } 620 } 621 else follow(p); 622 return; 623 case FINAL: if (tmpstat[line] != 1) { 624 tmpstat[line] = 1; 625 count++; 626 } 627 return; 628 } 629 } 630 631 #define USAGE "[ -bchHilnsqv ] [ -e exp ] [ -f file ] [ strings ] [ file ] ..." 632 633 int 634 main(int argc, char **argv) 635 { 636 char c; 637 char nl = '\n'; 638 int errflag = 0; 639 640 (void)setlocale(LC_ALL, ""); 641 642 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 643 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't. */ 644 #endif 645 (void) textdomain(TEXT_DOMAIN); 646 647 while((c = getopt(argc, argv, "ybcie:f:Hhlnvsq")) != -1) 648 switch(c) { 649 650 case 'b': 651 bflag++; 652 continue; 653 654 case 'c': 655 cflag++; 656 continue; 657 658 case 'e': 659 eflag++; 660 input = optarg; 661 continue; 662 663 case 'f': 664 fflag++; 665 expfile = fopen(optarg, "r"); 666 if(expfile == NULL) { 667 fprintf(stderr, 668 gettext("egrep: can't open %s\n"), optarg); 669 exit(2); 670 } 671 continue; 672 673 case 'H': 674 if (!lflag) /* H is excluded by l as in GNU grep */ 675 Hflag++; 676 hflag = 0; /* H excludes h */ 677 continue; 678 679 case 'h': 680 hflag++; 681 Hflag = 0; /* h excludes H */ 682 continue; 683 684 case 'y': 685 case 'i': 686 iflag++; 687 continue; 688 689 case 'l': 690 lflag++; 691 Hflag = 0; /* l excludes H */ 692 continue; 693 694 case 'n': 695 nflag++; 696 continue; 697 698 case 'q': 699 case 's': /* Solaris: legacy option */ 700 qflag++; 701 continue; 702 703 case 'v': 704 vflag++; 705 continue; 706 707 case '?': 708 errflag++; 709 } 710 if (errflag || ((argc <= 0) && !fflag && !eflag)) { 711 fprintf(stderr, gettext("usage: egrep %s\n"), gettext(USAGE)); 712 exit(2); 713 } 714 if(!eflag && !fflag) { 715 input = argv[optind]; 716 optind++; 717 } 718 719 argc -= optind; 720 argv = &argv[optind]; 721 722 /* allocate initial space for arrays */ 723 if((name = (int *)malloc(MAXLIN*sizeof(int))) == (int *)0) 724 overflo(); 725 if((left = (int *)malloc(MAXLIN*sizeof(int))) == (int *)0) 726 overflo(); 727 if((right = (int *)malloc(MAXLIN*sizeof(int))) == (int *)0) 728 overflo(); 729 if((parent = (int *)malloc(MAXLIN*sizeof(int))) == (int *)0) 730 overflo(); 731 if((foll = (int *)malloc(MAXLIN*sizeof(int))) == (int *)0) 732 overflo(); 733 if((tmpstat = (int *)malloc(MAXLIN*sizeof(int))) == (int *)0) 734 overflo(); 735 if((initstat = (int *)malloc(MAXLIN*sizeof(int))) == (int *)0) 736 overflo(); 737 if((chars = (char *)malloc(MAXLIN)) == (char *)0) 738 overflo(); 739 if((lower = (wchar_t *)malloc(MAXLIN*sizeof(wchar_t))) == (wchar_t *)0) 740 overflo(); 741 if((upper = (wchar_t *)malloc(MAXLIN*sizeof(wchar_t))) == (wchar_t *)0) 742 overflo(); 743 if((positions = (int *)malloc(MAXPOS*sizeof(int))) == (int *)0) 744 overflo(); 745 maxlin = MAXLIN; 746 maxclin = MAXLIN; 747 maxwclin = MAXLIN; 748 maxpos = MAXPOS; 749 750 yyparse(); 751 752 cfoll(line-1); 753 cgotofn(); 754 nfile = argc; 755 if (argc<=0) { 756 execute(0); 757 } 758 else while (--argc >= 0) { 759 if (reinit == 1) clearg(); 760 execute(*argv++); 761 } 762 return (badbotch ? 2 : nsucc==0); 763 } 764 765 void 766 execute(char *file) 767 { 768 char *p; 769 int cstat; 770 wchar_t c; 771 int t; 772 long count; 773 long count1, count2; 774 long nchars; 775 int succ; 776 char *ptr, *ptrend, *lastptr; 777 char *buf; 778 long lBufSiz; 779 FILE *f; 780 int nlflag; 781 782 lBufSiz = EBUFSIZ; 783 if ((buf = malloc (lBufSiz + EBUFSIZ)) == NULL) { 784 exit (2); /* out of memory - BAIL */ 785 } 786 787 if (file) { 788 if ((f = fopen(file, "r")) == NULL) { 789 fprintf(stderr, 790 gettext("egrep: can't open %s\n"), file); 791 badbotch=1; 792 return; 793 } 794 } else { 795 f = stdin; 796 file = STDIN_FILENAME; 797 } 798 lnum = 1; 799 tln = 0; 800 if((count = read(fileno(f), buf, EBUFSIZ)) <= 0) { 801 fclose(f); 802 803 if (cflag && !qflag) { 804 if (Hflag || (nfile > 1 && !hflag)) 805 fprintf(stdout, "%s:", file); 806 fprintf(stdout, "%lld\n", tln); 807 } 808 return; 809 } 810 811 blkno = count; 812 ptr = buf; 813 for(;;) { 814 if((ptrend = memchr(ptr, '\n', buf + count - ptr)) == NULL) { 815 /* 816 move the unused partial record to the head of the buffer 817 */ 818 if (ptr > buf) { 819 count = buf + count - ptr; 820 memmove (buf, ptr, count); 821 ptr = buf; 822 } 823 824 /* 825 Get a bigger buffer if this one is full 826 */ 827 if(count > lBufSiz) { 828 /* 829 expand the buffer 830 */ 831 lBufSiz += EBUFSIZ; 832 if ((buf = realloc (buf, lBufSiz + EBUFSIZ)) == NULL) { 833 exit (2); /* out of memory - BAIL */ 834 } 835 836 ptr = buf; 837 } 838 839 p = buf + count; 840 if((count1 = read(fileno(f), p, EBUFSIZ)) > 0) { 841 count += count1; 842 blkno += count1; 843 continue; 844 } 845 ptrend = ptr + count; 846 nlflag = 0; 847 } else 848 nlflag = 1; 849 *ptrend = '\n'; 850 p = ptr; 851 lastptr = ptr; 852 cstat = istat; 853 succ = 0; 854 for(;;) { 855 if(out[cstat]) { 856 if(multibyte && p > ptr) { 857 wchar_t wchar; 858 int length; 859 char *endptr = p; 860 p = lastptr; 861 while(p < endptr) { 862 length = mbtowc(&wchar, p, MB_LEN_MAX); 863 if(length <= 1) 864 p++; 865 else 866 p += length; 867 } 868 if(p == endptr) { 869 succ = !vflag; 870 break; 871 } 872 cstat = 1; 873 length = mbtowc(&wchar, lastptr, MB_LEN_MAX); 874 if(length <= 1) 875 lastptr++; 876 else 877 lastptr += length; 878 p = lastptr; 879 continue; 880 } 881 succ = !vflag; 882 break; 883 } 884 c = (unsigned char)*p++; 885 if ((t = gotofn[cstat][c]) == 0) 886 cstat = nxtst(cstat, c); 887 else 888 cstat = t; 889 if(c == RIGHT) { 890 if(out[cstat]) { 891 succ = !vflag; 892 break; 893 } 894 succ = vflag; 895 break; 896 } 897 } 898 if (succ) { 899 nsucc = 1; 900 if (lflag || qflag) { 901 if (!qflag) 902 (void) printf("%s\n", file); 903 fclose(f); 904 return; 905 } 906 if (cflag) { 907 tln++; 908 } else { 909 if (Hflag || (nfile > 1 && !hflag)) 910 printf("%s:", file); 911 if (bflag) { 912 nchars = blkno - (buf + count - ptrend) - 2; 913 if(nlflag) 914 nchars++; 915 printf("%lld:", nchars/BLKSIZE); 916 } 917 if (nflag) 918 printf("%lld:", lnum); 919 if(nlflag) 920 nchars = ptrend - ptr + 1; 921 else 922 nchars = ptrend - ptr; 923 fwrite(ptr, (size_t)1, (size_t)nchars, stdout); 924 } 925 } 926 if(!nlflag) 927 break; 928 ptr = ptrend + 1; 929 if(ptr >= buf + count) { 930 ptr = buf; 931 if((count = read(fileno(f), buf, EBUFSIZ)) <= 0) 932 break; 933 blkno += count; 934 } 935 lnum++; 936 if (reinit == 1) 937 clearg(); 938 } 939 fclose(f); 940 if (cflag && !qflag) { 941 if (Hflag || (nfile > 1 && !hflag)) 942 printf("%s:", file); 943 printf("%lld\n", tln); 944 } 945 } 946 947 void 948 clearg(void) 949 { 950 int i, k; 951 for (i=1; i<=nstate; i++) 952 out[i] = 0; 953 for (i=1; i<=nstate; i++) 954 for (k=0; k<NCHARS; k++) 955 gotofn[i][k] = 0; 956 nstate = 1; 957 nxtpos = inxtpos; 958 reinit = 0; 959 count = 0; 960 for (i=3; i<=line; i++) tmpstat[i] = 0; 961 if (cstate(line-1)==0) { 962 tmpstat[line] = 1; 963 count++; 964 out[1] = 1; 965 } 966 for (i=3; i<=line; i++) initstat[i] = tmpstat[i]; 967 count--; /*leave out position 1 */ 968 icount = count; 969 tmpstat[1] = 0; 970 add(state, 1); 971 istat = nxtst(1, LEFT); 972 } 973 974 int 975 mdotenter(void) 976 { 977 int i, x1, x2; 978 x1 = enter(DOT); 979 x2 = enter(MDOT); 980 for(i = 1; i < (int) eucw1; i++) 981 x2 = node(CAT, x2, enter(MDOT)); 982 x1 = node(OR, x1, x2); 983 if(eucw2) { 984 x2 = enter('\216'); 985 for(i = 1; i <= (int) eucw2; i++) 986 x2 = node(CAT, x2, enter(MDOT)); 987 x1 = node(OR, x1, x2); 988 } 989 if(eucw3) { 990 x2 = enter('\217'); 991 for(i = 1; i <= (int) eucw3; i++) 992 x2 = node(CAT, x2, enter(MDOT)); 993 x1 = node(OR, x1, x2); 994 } 995 return(x1); 996 } 997 998 int 999 mchar(wchar_t c) 1000 { 1001 char multichar[MB_LEN_MAX+1]; 1002 char *p; 1003 int x1, lc, length; 1004 1005 length = wctomb(multichar, c); 1006 p = multichar; 1007 *(p + length) = '\0'; 1008 x1 = enter((unsigned char)*p++); 1009 while(lc = (unsigned char)*p++) 1010 x1 = node(CAT, x1, enter(lc)); 1011 return(x1); 1012 } 1013 1014 int 1015 ccl(int type) 1016 { 1017 wchar_t c, lc; 1018 char multic1[MB_LEN_MAX]; 1019 char multic2[MB_LEN_MAX]; 1020 int x1, x2, length, current, last, cclcnt; 1021 x2 = 0; 1022 current = 0; 1023 last = genrange(type); 1024 nxtchar = count + 1; 1025 cclcnt = 0; 1026 /* create usual character class for single byte characters */ 1027 while(current <= last && (isascii(c = lower[current]) || c <= 0377 && iscntrl(c))) { 1028 cclcnt++; 1029 chars[nxtchar++] = c; 1030 if(lower[current] != upper[current]) { 1031 chars[nxtchar++] = '-'; 1032 chars[nxtchar++] = upper[current]; 1033 cclcnt += 2; 1034 } 1035 current++; 1036 } 1037 1038 if(cclcnt) 1039 chars[count] = cclcnt; 1040 else 1041 nxtchar = count; 1042 if(current > 0) 1043 /* single byte part of character class */ 1044 x2 = cclenter(type); 1045 else if(type == NCCL) 1046 /* all single byte characters match */ 1047 x2 = enter(DOT); 1048 while(current <= last) { 1049 if(upper[current] == lower[current]) 1050 x1 = mchar(lower[current]); 1051 else { 1052 length = wctomb(multic1, lower[current]); 1053 wctomb(multic2, upper[current]); 1054 x1 = range((unsigned char *)multic1, 1055 (unsigned char *)multic2, length); 1056 } 1057 if(x2) 1058 x2 = node(OR, x2, x1); 1059 else 1060 x2 = x1; 1061 current++; 1062 } 1063 return x2; 1064 } 1065 1066 int 1067 range(unsigned char *p1, unsigned char *p2, int length) 1068 { 1069 char multic[MB_LEN_MAX+1]; 1070 char *p; 1071 int i, x1, x2; 1072 if(length == 1) 1073 return(classenter(*p1, *p2)); 1074 if(p1[0] == p2[0]) 1075 return(node(CAT, enter(p1[0]), range(p1+1, p2+1, length - 1))); 1076 p = multic; 1077 for(i = 1; i < length; i++) 1078 *p++ = 0377; 1079 x1 = node(CAT, enter(p1[0]), 1080 range(p1+1, (unsigned char *)multic, length - 1)); 1081 if((unsigned char)(p1[0] + 1) < p2[0]) { 1082 x2 = classenter(p1[0] + 1, p2[0] - 1); 1083 for(i = 1; i < length; i++) 1084 x2 = node(CAT, x2, enter(MDOT)); 1085 x1 = node(OR, x1, x2); 1086 } 1087 p = multic; 1088 for(i = 1; i < length; i++) 1089 *p++ = 0200; 1090 x2 = node(CAT, enter(p2[0]), 1091 range((unsigned char *)multic, p2+1, length - 1)); 1092 return node(OR, x1, x2); 1093 } 1094 1095 int 1096 classenter(int x1, int x2) 1097 { 1098 static int max, min; 1099 if(!max) { 1100 int i; 1101 for(i = 0200; i <= 0377; i++) 1102 if(!iscntrl(i)) 1103 break; 1104 min = i; 1105 for(i = 0377; i >= 0200; i--) 1106 if(!iscntrl(i)) 1107 break; 1108 max = i; 1109 } 1110 if(x1 <= min && x2 >= max) 1111 return enter(MDOT); 1112 if(nxtchar + 4 >= maxclin) 1113 if(allocchars() == 0) 1114 overflo(); 1115 count = nxtchar++; 1116 chars[nxtchar++] = x1; 1117 chars[nxtchar++] = '-'; 1118 chars[nxtchar++] = x2; 1119 chars[count] = 3; 1120 return cclenter(MCCL); 1121 } 1122 1123 int 1124 genrange(int type) 1125 { 1126 char *p, *endp; 1127 int current, nel, i, last, length; 1128 wchar_t c, lc; 1129 1130 current = 0; 1131 p = &chars[count+1]; 1132 endp = &chars[count+1] + chars[count]; 1133 lc = 0; 1134 1135 /* convert character class into union of ranges */ 1136 while(p < endp) { 1137 length = mbtowc(&c, p, MB_LEN_MAX); 1138 p += length; 1139 if(c == '-' && lc != 0) { 1140 length = mbtowc(&c, p, MB_LEN_MAX); 1141 upper[current-1] = c; 1142 p += length; 1143 } else { 1144 lower[current] = c; 1145 upper[current++] = c; 1146 } 1147 lc = c; 1148 } 1149 nel = current; 1150 /* sort lower and upper bounds of ranges */ 1151 qsort((char *)lower, nel, sizeof(wchar_t), compare); 1152 qsort((char *)upper, nel, sizeof(wchar_t), compare); 1153 last = current - 1; 1154 current = 0; 1155 /* combine overlapping or adjacent ranges */ 1156 for(i = 0; i < last; i++) 1157 if(upper[i] >= lower[i+1] - 1) 1158 upper[current] = upper[i+1]; 1159 else { 1160 lower[++current] = lower[i+1]; 1161 upper[current] = upper[i+1]; 1162 } 1163 if(type == NCCL) { 1164 /* find complement of character class */ 1165 int j, next; 1166 i = 0; 1167 while(i <= current && isascii(c=lower[i]) || c <= 0377 && iscntrl(c)) 1168 i++; 1169 if(i > current) { 1170 /* match all multibyte characters */ 1171 if(eucw2) { 1172 lower[i] = maxmin(WCHAR_CS2, 0); 1173 upper[i++] = maxmin(WCHAR_CS2, 1); 1174 } 1175 if(eucw3) { 1176 lower[i] = maxmin(WCHAR_CS3, 0); 1177 upper[i++] = maxmin(WCHAR_CS3, 1); 1178 } 1179 lower[i] = maxmin(WCHAR_CS1, 0); 1180 upper[i++] = maxmin(WCHAR_CS1, 1); 1181 return i - 1; 1182 } 1183 next = current + 1; 1184 if(next + current + 2 >= maxwclin) { 1185 maxwclin += MAXLIN + next + current + 2; 1186 if((lower = (wchar_t *)realloc(lower, maxwclin *sizeof(wchar_t))) == (wchar_t *)0 || 1187 (upper = (wchar_t *)realloc(upper, maxwclin * sizeof(wchar_t))) == (wchar_t *)0) 1188 overflo(); 1189 } 1190 if(eucw2 && lower[i] > maxmin(WCHAR_CS2, 0)) { 1191 lower[next] = maxmin(WCHAR_CS2, 0); 1192 if((lower[i] & WCHAR_CSMASK) != WCHAR_CS2) { 1193 upper[next++] = maxmin(WCHAR_CS2, 1); 1194 if((lower[i] & WCHAR_CSMASK) == WCHAR_CS1 && eucw3) { 1195 lower[next] = maxmin(WCHAR_CS3, 0); 1196 upper[next++] = maxmin(WCHAR_CS3, 1); 1197 } 1198 if(lower[i] > maxmin(lower[i] & WCHAR_CSMASK, 0)) { 1199 lower[next] = maxmin(lower[i] & WCHAR_CSMASK, 0); 1200 upper[next++] = lower[i] - 1; 1201 } 1202 } else 1203 upper[next++] = lower[i] - 1; 1204 } else if(lower[i] > maxmin(lower[i] & WCHAR_CSMASK, 0)) { 1205 lower[next] = maxmin(lower[i] & WCHAR_CSMASK, 0); 1206 upper[next++] = lower[i] - 1; 1207 } 1208 for(j = i; j < current; j++) { 1209 if(upper[j] < maxmin(upper[j] & WCHAR_CSMASK, 1)) { 1210 lower[next] = upper[j] + 1; 1211 if((upper[j] & WCHAR_CSMASK) != (lower[j+1] & WCHAR_CSMASK)) { 1212 upper[next++] = maxmin(upper[j] & WCHAR_CSMASK, 1); 1213 if(eucw3 && (upper[j] & WCHAR_CSMASK) == WCHAR_CS2 && (lower[j+1] & WCHAR_CSMASK) == WCHAR_CS1) { 1214 lower[next] = maxmin(WCHAR_CS3, 0); 1215 upper[next++] = maxmin(WCHAR_CS3, 1); 1216 } 1217 if(lower[j+1] > maxmin(lower[j+1] & WCHAR_CSMASK, 0)) { 1218 lower[next] = maxmin(lower[j+1] & WCHAR_CSMASK, 0); 1219 upper[next++] = lower[j+1] - 1; 1220 } 1221 } else 1222 upper[next++] = lower[j+1] - 1; 1223 } else if(lower[j+1] > maxmin(lower[j+1], 0)) { 1224 lower[next] = maxmin(lower[j+1], 0); 1225 upper[next++] = lower[j+1] - 1; 1226 } 1227 } 1228 if(upper[current] < maxmin(upper[current] & WCHAR_CSMASK, 1)) { 1229 lower[next] = upper[current] + 1; 1230 upper[next++] = maxmin(upper[current] & WCHAR_CSMASK, 1); 1231 } 1232 if((upper[current] & WCHAR_CSMASK) != WCHAR_CS1) { 1233 if((upper[current] & WCHAR_CSMASK) == WCHAR_CS2 && eucw3) { 1234 lower[next] = maxmin(WCHAR_CS3, 0); 1235 upper[next++] = maxmin(WCHAR_CS3, 1); 1236 } 1237 lower[next] = maxmin(WCHAR_CS1, 0); 1238 upper[next++] = maxmin(WCHAR_CS1, 1); 1239 } 1240 for(j = current + 1; j < next; j++) { 1241 lower[i] = lower[j]; 1242 upper[i++] = upper[j]; 1243 } 1244 current = i - 1; 1245 } 1246 return(current); 1247 } 1248 1249 int 1250 compare(wchar_t *c, wchar_t *d) 1251 { 1252 if(*c < *d) 1253 return -1; 1254 if(*c == *d) 1255 return 0; 1256 return 1; 1257 } 1258 1259 wchar_t 1260 maxmin(wchar_t c, int flag) 1261 { 1262 static wchar_t minmax1[2], minmax2[2], minmax3[2]; 1263 1264 if(!minmax1[0]) { 1265 /* compute min and max process codes for all code sets */ 1266 int length, i; 1267 char multic[MB_LEN_MAX], minmax[2]; 1268 for(i = 0377; i >= 0200; i--) 1269 if(!iscntrl(i)) 1270 break; 1271 minmax[1] = i; 1272 for(i = 0240; i <= 0377; i++) 1273 if(!iscntrl(i)) 1274 break; 1275 minmax[0] = i; 1276 for(i = 0; i <= 1; i++) { 1277 length = MB_LEN_MAX; 1278 while(length--) 1279 multic[length] = minmax[i]; 1280 mbtowc(&minmax1[i], multic, MB_LEN_MAX); 1281 if(eucw2) { 1282 multic[0] = SS2; 1283 mbtowc(&minmax2[i], multic, MB_LEN_MAX); 1284 } 1285 if(eucw3) { 1286 multic[0] = SS3; 1287 mbtowc(&minmax3[i], multic, MB_LEN_MAX); 1288 } 1289 } 1290 } 1291 switch(c) { 1292 case WCHAR_CS1: return minmax1[flag]; 1293 case WCHAR_CS2: return minmax2[flag]; 1294 case WCHAR_CS3: return minmax3[flag]; 1295 } 1296 1297 /* NOTREACHED */ 1298 return (0); 1299 } 1300 static YYCONST yytabelem yyexca[] ={ 1301 -1, 1, 1302 0, -1, 1303 -2, 0, 1304 -1, 5, 1305 0, 2, 1306 -2, 0, 1307 -1, 16, 1308 0, 5, 1309 -2, 0, 1310 -1, 17, 1311 0, 15, 1312 257, 15, 1313 258, 15, 1314 259, 15, 1315 261, 15, 1316 262, 15, 1317 263, 15, 1318 264, 15, 1319 265, 15, 1320 40, 15, 1321 41, 15, 1322 -2, 0, 1323 -1, 22, 1324 0, 4, 1325 -2, 0, 1326 -1, 23, 1327 0, 14, 1328 265, 14, 1329 41, 14, 1330 -2, 0, 1331 -1, 26, 1332 0, 3, 1333 -2, 0, 1334 }; 1335 # define YYNPROD 21 1336 # define YYLAST 281 1337 static YYCONST yytabelem yyact[]={ 1338 1339 13, 25, 4, 3, 14, 17, 2, 1, 15, 5, 1340 0, 0, 0, 0, 13, 18, 19, 20, 0, 21, 1341 0, 22, 23, 0, 0, 0, 0, 0, 13, 0, 1342 23, 0, 23, 0, 0, 0, 0, 0, 0, 0, 1343 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 1344 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 1345 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1346 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1347 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1348 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1349 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1350 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1351 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1352 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1353 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1354 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1355 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1356 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1357 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1358 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1359 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1360 0, 0, 0, 0, 0, 0, 14, 6, 7, 8, 1361 0, 9, 10, 11, 12, 24, 0, 18, 19, 20, 1362 14, 6, 7, 8, 0, 9, 10, 11, 12, 26, 1363 0, 18, 19, 20, 14, 6, 7, 8, 0, 9, 1364 10, 11, 12, 16, 0, 18, 19, 20, 14, 6, 1365 7, 8, 0, 9, 10, 11, 12, 0, 0, 18, 1366 19, 20, 14, 6, 7, 8, 0, 9, 10, 11, 1367 12 }; 1368 static YYCONST yytabelem yypact[]={ 1369 1370 -263,-10000000,-10000000, 16,-10000000, -12,-10000000,-10000000,-10000000,-10000000, 1371 -10000000,-10000000,-10000000, 16,-10000000, 16, 16, -252,-10000000,-10000000, 1372 -10000000, -40, -26, 2, 16,-10000000, 16 }; 1373 static YYCONST yytabelem yypgo[]={ 1374 1375 0, 7, 6, 3, 5 }; 1376 static YYCONST yytabelem yyr1[]={ 1377 1378 0, 1, 2, 2, 2, 2, 3, 4, 4, 4, 1379 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1380 4 }; 1381 static YYCONST yytabelem yyr2[]={ 1382 1383 0, 3, 5, 9, 7, 7, 1, 3, 3, 3, 1384 3, 3, 3, 3, 7, 5, 5, 5, 5, 7, 1385 2 }; 1386 static YYCONST yytabelem yychk[]={ 1387 1388 -10000000, -1, -2, -3, 265, -4, 257, 258, 259, 261, 1389 262, 263, 264, 40, 256, -3, 265, -4, 267, 268, 1390 269, -4, -4, -4, 265, 41, 265 }; 1391 static YYCONST yytabelem yydef[]={ 1392 1393 6, -2, 1, 0, 6, -2, 7, 8, 9, 10, 1394 11, 12, 13, 0, 20, 0, -2, -2, 16, 17, 1395 18, 0, -2, -2, 0, 19, -2 }; 1396 typedef struct 1397 #ifdef __cplusplus 1398 yytoktype 1399 #endif 1400 { 1401 #ifdef __cplusplus 1402 const 1403 #endif 1404 char *t_name; int t_val; } yytoktype; 1405 #ifndef YYDEBUG 1406 # define YYDEBUG 0 /* don't allow debugging */ 1407 #endif 1408 1409 #if YYDEBUG 1410 1411 yytoktype yytoks[] = 1412 { 1413 "CHAR", 257, 1414 "MCHAR", 258, 1415 "DOT", 259, 1416 "MDOT", 260, 1417 "CCL", 261, 1418 "NCCL", 262, 1419 "MCCL", 263, 1420 "NMCCL", 264, 1421 "OR", 265, 1422 "CAT", 266, 1423 "STAR", 267, 1424 "PLUS", 268, 1425 "QUEST", 269, 1426 "(", 40, 1427 "-unknown-", -1 /* ends search */ 1428 }; 1429 1430 #ifdef __cplusplus 1431 const 1432 #endif 1433 char * yyreds[] = 1434 { 1435 "-no such reduction-", 1436 "s : t", 1437 "t : b r", 1438 "t : OR b r OR", 1439 "t : OR b r", 1440 "t : b r OR", 1441 "b : /* empty */", 1442 "r : CHAR", 1443 "r : MCHAR", 1444 "r : DOT", 1445 "r : CCL", 1446 "r : NCCL", 1447 "r : MCCL", 1448 "r : NMCCL", 1449 "r : r OR r", 1450 "r : r r", 1451 "r : r STAR", 1452 "r : r PLUS", 1453 "r : r QUEST", 1454 "r : '(' r ')'", 1455 "r : error", 1456 }; 1457 #endif /* YYDEBUG */ 1458 # line 1 "/usr/share/lib/ccs/yaccpar" 1459 /* 1460 * CDDL HEADER START 1461 * 1462 * The contents of this file are subject to the terms of the 1463 * Common Development and Distribution License, Version 1.0 only 1464 * (the "License"). You may not use this file except in compliance 1465 * with the License. 1466 * 1467 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 1468 * or http://www.opensolaris.org/os/licensing. 1469 * See the License for the specific language governing permissions 1470 * and limitations under the License. 1471 * 1472 * When distributing Covered Code, include this CDDL HEADER in each 1473 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 1474 * If applicable, add the following below this CDDL HEADER, with the 1475 * fields enclosed by brackets "[]" replaced with your own identifying 1476 * information: Portions Copyright [yyyy] [name of copyright owner] 1477 * 1478 * CDDL HEADER END 1479 */ 1480 /* 1481 * Copyright 1993 Sun Microsystems, Inc. All rights reserved. 1482 * Use is subject to license terms. 1483 */ 1484 1485 /* Copyright (c) 1988 AT&T */ 1486 /* All Rights Reserved */ 1487 1488 #pragma ident "%Z%%M% %I% %E% SMI" 1489 1490 /* 1491 ** Skeleton parser driver for yacc output 1492 */ 1493 1494 /* 1495 ** yacc user known macros and defines 1496 */ 1497 #define YYERROR goto yyerrlab 1498 #define YYACCEPT return(0) 1499 #define YYABORT return(1) 1500 #define YYBACKUP( newtoken, newvalue )\ 1501 {\ 1502 if ( yychar >= 0 || ( yyr2[ yytmp ] >> 1 ) != 1 )\ 1503 {\ 1504 yyerror_gettext( "syntax error - cannot backup" );\ 1505 goto yyerrlab;\ 1506 }\ 1507 yychar = newtoken;\ 1508 yystate = *yyps;\ 1509 yylval = newvalue;\ 1510 goto yynewstate;\ 1511 } 1512 #define YYRECOVERING() (!!yyerrflag) 1513 #define YYNEW(type) malloc(sizeof(type) * yynewmax) 1514 #define YYCOPY(to, from, type) \ 1515 (type *) memcpy(to, (char *) from, yymaxdepth * sizeof (type)) 1516 #define YYENLARGE( from, type) \ 1517 (type *) realloc((char *) from, yynewmax * sizeof(type)) 1518 #ifndef YYDEBUG 1519 # define YYDEBUG 1 /* make debugging available */ 1520 #endif 1521 1522 /* 1523 ** user known globals 1524 */ 1525 int yydebug; /* set to 1 to get debugging */ 1526 1527 /* 1528 ** driver internal defines 1529 */ 1530 #define YYFLAG (-10000000) 1531 1532 /* 1533 ** global variables used by the parser 1534 */ 1535 YYSTYPE *yypv; /* top of value stack */ 1536 int *yyps; /* top of state stack */ 1537 1538 int yystate; /* current state */ 1539 int yytmp; /* extra var (lasts between blocks) */ 1540 1541 int yynerrs; /* number of errors */ 1542 int yyerrflag; /* error recovery flag */ 1543 int yychar; /* current input token number */ 1544 1545 1546 1547 #ifdef YYNMBCHARS 1548 #define YYLEX() yycvtok(yylex()) 1549 /* 1550 ** yycvtok - return a token if i is a wchar_t value that exceeds 255. 1551 ** If i<255, i itself is the token. If i>255 but the neither 1552 ** of the 30th or 31st bit is on, i is already a token. 1553 */ 1554 #if defined(__STDC__) || defined(__cplusplus) 1555 int yycvtok(int i) 1556 #else 1557 int yycvtok(i) int i; 1558 #endif 1559 { 1560 int first = 0; 1561 int last = YYNMBCHARS - 1; 1562 int mid; 1563 wchar_t j; 1564 1565 if(i&0x60000000){/*Must convert to a token. */ 1566 if( yymbchars[last].character < i ){ 1567 return i;/*Giving up*/ 1568 } 1569 while ((last>=first)&&(first>=0)) {/*Binary search loop*/ 1570 mid = (first+last)/2; 1571 j = yymbchars[mid].character; 1572 if( j==i ){/*Found*/ 1573 return yymbchars[mid].tvalue; 1574 }else if( j<i ){ 1575 first = mid + 1; 1576 }else{ 1577 last = mid -1; 1578 } 1579 } 1580 /*No entry in the table.*/ 1581 return i;/* Giving up.*/ 1582 }else{/* i is already a token. */ 1583 return i; 1584 } 1585 } 1586 #else/*!YYNMBCHARS*/ 1587 #define YYLEX() yylex() 1588 #endif/*!YYNMBCHARS*/ 1589 1590 /* 1591 ** yyparse - return 0 if worked, 1 if syntax error not recovered from 1592 */ 1593 #if defined(__STDC__) || defined(__cplusplus) 1594 int yyparse(void) 1595 #else 1596 int yyparse() 1597 #endif 1598 { 1599 register YYSTYPE *yypvt = 0; /* top of value stack for $vars */ 1600 1601 #if defined(__cplusplus) || defined(lint) 1602 /* 1603 hacks to please C++ and lint - goto's inside 1604 switch should never be executed 1605 */ 1606 static int __yaccpar_lint_hack__ = 0; 1607 switch (__yaccpar_lint_hack__) 1608 { 1609 case 1: goto yyerrlab; 1610 case 2: goto yynewstate; 1611 } 1612 #endif 1613 1614 /* 1615 ** Initialize externals - yyparse may be called more than once 1616 */ 1617 yypv = &yyv[-1]; 1618 yyps = &yys[-1]; 1619 yystate = 0; 1620 yytmp = 0; 1621 yynerrs = 0; 1622 yyerrflag = 0; 1623 yychar = -1; 1624 1625 #if YYMAXDEPTH <= 0 1626 if (yymaxdepth <= 0) 1627 { 1628 if ((yymaxdepth = YYEXPAND(0)) <= 0) 1629 { 1630 yyerror("yacc initialization error"); 1631 YYABORT; 1632 } 1633 } 1634 #endif 1635 1636 { 1637 register YYSTYPE *yy_pv; /* top of value stack */ 1638 register int *yy_ps; /* top of state stack */ 1639 register int yy_state; /* current state */ 1640 register int yy_n; /* internal state number info */ 1641 goto yystack; /* moved from 6 lines above to here to please C++ */ 1642 1643 /* 1644 ** get globals into registers. 1645 ** branch to here only if YYBACKUP was called. 1646 */ 1647 yynewstate: 1648 yy_pv = yypv; 1649 yy_ps = yyps; 1650 yy_state = yystate; 1651 goto yy_newstate; 1652 1653 /* 1654 ** get globals into registers. 1655 ** either we just started, or we just finished a reduction 1656 */ 1657 yystack: 1658 yy_pv = yypv; 1659 yy_ps = yyps; 1660 yy_state = yystate; 1661 1662 /* 1663 ** top of for (;;) loop while no reductions done 1664 */ 1665 yy_stack: 1666 /* 1667 ** put a state and value onto the stacks 1668 */ 1669 #if YYDEBUG 1670 /* 1671 ** if debugging, look up token value in list of value vs. 1672 ** name pairs. 0 and negative (-1) are special values. 1673 ** Note: linear search is used since time is not a real 1674 ** consideration while debugging. 1675 */ 1676 if ( yydebug ) 1677 { 1678 register int yy_i; 1679 1680 printf( "State %d, token ", yy_state ); 1681 if ( yychar == 0 ) 1682 printf( "end-of-file\n" ); 1683 else if ( yychar < 0 ) 1684 printf( "-none-\n" ); 1685 else 1686 { 1687 for ( yy_i = 0; yytoks[yy_i].t_val >= 0; 1688 yy_i++ ) 1689 { 1690 if ( yytoks[yy_i].t_val == yychar ) 1691 break; 1692 } 1693 printf( "%s\n", yytoks[yy_i].t_name ); 1694 } 1695 } 1696 #endif /* YYDEBUG */ 1697 if ( ++yy_ps >= &yys[ yymaxdepth ] ) /* room on stack? */ 1698 { 1699 /* 1700 ** reallocate and recover. Note that pointers 1701 ** have to be reset, or bad things will happen 1702 */ 1703 long yyps_index = (yy_ps - yys); 1704 long yypv_index = (yy_pv - yyv); 1705 long yypvt_index = (yypvt - yyv); 1706 int yynewmax; 1707 #ifdef YYEXPAND 1708 yynewmax = YYEXPAND(yymaxdepth); 1709 #else 1710 yynewmax = 2 * yymaxdepth; /* double table size */ 1711 if (yymaxdepth == YYMAXDEPTH) /* first time growth */ 1712 { 1713 char *newyys = (char *)YYNEW(int); 1714 char *newyyv = (char *)YYNEW(YYSTYPE); 1715 if (newyys != 0 && newyyv != 0) 1716 { 1717 yys = YYCOPY(newyys, yys, int); 1718 yyv = YYCOPY(newyyv, yyv, YYSTYPE); 1719 } 1720 else 1721 yynewmax = 0; /* failed */ 1722 } 1723 else /* not first time */ 1724 { 1725 yys = YYENLARGE(yys, int); 1726 yyv = YYENLARGE(yyv, YYSTYPE); 1727 if (yys == 0 || yyv == 0) 1728 yynewmax = 0; /* failed */ 1729 } 1730 #endif 1731 if (yynewmax <= yymaxdepth) /* tables not expanded */ 1732 { 1733 yyerror( "yacc stack overflow" ); 1734 YYABORT; 1735 } 1736 yymaxdepth = yynewmax; 1737 1738 yy_ps = yys + yyps_index; 1739 yy_pv = yyv + yypv_index; 1740 yypvt = yyv + yypvt_index; 1741 } 1742 *yy_ps = yy_state; 1743 *++yy_pv = yyval; 1744 1745 /* 1746 ** we have a new state - find out what to do 1747 */ 1748 yy_newstate: 1749 if ( ( yy_n = yypact[ yy_state ] ) <= YYFLAG ) 1750 goto yydefault; /* simple state */ 1751 #if YYDEBUG 1752 /* 1753 ** if debugging, need to mark whether new token grabbed 1754 */ 1755 yytmp = yychar < 0; 1756 #endif 1757 if ( ( yychar < 0 ) && ( ( yychar = YYLEX() ) < 0 ) ) 1758 yychar = 0; /* reached EOF */ 1759 #if YYDEBUG 1760 if ( yydebug && yytmp ) 1761 { 1762 register int yy_i; 1763 1764 printf( "Received token " ); 1765 if ( yychar == 0 ) 1766 printf( "end-of-file\n" ); 1767 else if ( yychar < 0 ) 1768 printf( "-none-\n" ); 1769 else 1770 { 1771 for ( yy_i = 0; yytoks[yy_i].t_val >= 0; 1772 yy_i++ ) 1773 { 1774 if ( yytoks[yy_i].t_val == yychar ) 1775 break; 1776 } 1777 printf( "%s\n", yytoks[yy_i].t_name ); 1778 } 1779 } 1780 #endif /* YYDEBUG */ 1781 if ( ( ( yy_n += yychar ) < 0 ) || ( yy_n >= YYLAST ) ) 1782 goto yydefault; 1783 if ( yychk[ yy_n = yyact[ yy_n ] ] == yychar ) /*valid shift*/ 1784 { 1785 yychar = -1; 1786 yyval = yylval; 1787 yy_state = yy_n; 1788 if ( yyerrflag > 0 ) 1789 yyerrflag--; 1790 goto yy_stack; 1791 } 1792 1793 yydefault: 1794 if ( ( yy_n = yydef[ yy_state ] ) == -2 ) 1795 { 1796 #if YYDEBUG 1797 yytmp = yychar < 0; 1798 #endif 1799 if ( ( yychar < 0 ) && ( ( yychar = YYLEX() ) < 0 ) ) 1800 yychar = 0; /* reached EOF */ 1801 #if YYDEBUG 1802 if ( yydebug && yytmp ) 1803 { 1804 register int yy_i; 1805 1806 printf( "Received token " ); 1807 if ( yychar == 0 ) 1808 printf( "end-of-file\n" ); 1809 else if ( yychar < 0 ) 1810 printf( "-none-\n" ); 1811 else 1812 { 1813 for ( yy_i = 0; 1814 yytoks[yy_i].t_val >= 0; 1815 yy_i++ ) 1816 { 1817 if ( yytoks[yy_i].t_val 1818 == yychar ) 1819 { 1820 break; 1821 } 1822 } 1823 printf( "%s\n", yytoks[yy_i].t_name ); 1824 } 1825 } 1826 #endif /* YYDEBUG */ 1827 /* 1828 ** look through exception table 1829 */ 1830 { 1831 register YYCONST int *yyxi = yyexca; 1832 1833 while ( ( *yyxi != -1 ) || 1834 ( yyxi[1] != yy_state ) ) 1835 { 1836 yyxi += 2; 1837 } 1838 while ( ( *(yyxi += 2) >= 0 ) && 1839 ( *yyxi != yychar ) ) 1840 ; 1841 if ( ( yy_n = yyxi[1] ) < 0 ) 1842 YYACCEPT; 1843 } 1844 } 1845 1846 /* 1847 ** check for syntax error 1848 */ 1849 if ( yy_n == 0 ) /* have an error */ 1850 { 1851 /* no worry about speed here! */ 1852 switch ( yyerrflag ) 1853 { 1854 case 0: /* new error */ 1855 yyerror_gettext( "syntax error" ); 1856 goto skip_init; 1857 yyerrlab: 1858 /* 1859 ** get globals into registers. 1860 ** we have a user generated syntax type error 1861 */ 1862 yy_pv = yypv; 1863 yy_ps = yyps; 1864 yy_state = yystate; 1865 skip_init: 1866 yynerrs++; 1867 /* FALLTHRU */ 1868 case 1: 1869 case 2: /* incompletely recovered error */ 1870 /* try again... */ 1871 yyerrflag = 3; 1872 /* 1873 ** find state where "error" is a legal 1874 ** shift action 1875 */ 1876 while ( yy_ps >= yys ) 1877 { 1878 yy_n = yypact[ *yy_ps ] + YYERRCODE; 1879 if ( yy_n >= 0 && yy_n < YYLAST && 1880 yychk[yyact[yy_n]] == YYERRCODE) { 1881 /* 1882 ** simulate shift of "error" 1883 */ 1884 yy_state = yyact[ yy_n ]; 1885 goto yy_stack; 1886 } 1887 /* 1888 ** current state has no shift on 1889 ** "error", pop stack 1890 */ 1891 #if YYDEBUG 1892 # define _POP_ "Error recovery pops state %d, uncovers state %d\n" 1893 if ( yydebug ) 1894 printf( _POP_, *yy_ps, 1895 yy_ps[-1] ); 1896 # undef _POP_ 1897 #endif 1898 yy_ps--; 1899 yy_pv--; 1900 } 1901 /* 1902 ** there is no state on stack with "error" as 1903 ** a valid shift. give up. 1904 */ 1905 YYABORT; 1906 case 3: /* no shift yet; eat a token */ 1907 #if YYDEBUG 1908 /* 1909 ** if debugging, look up token in list of 1910 ** pairs. 0 and negative shouldn't occur, 1911 ** but since timing doesn't matter when 1912 ** debugging, it doesn't hurt to leave the 1913 ** tests here. 1914 */ 1915 if ( yydebug ) 1916 { 1917 register int yy_i; 1918 1919 printf( "Error recovery discards " ); 1920 if ( yychar == 0 ) 1921 printf( "token end-of-file\n" ); 1922 else if ( yychar < 0 ) 1923 printf( "token -none-\n" ); 1924 else 1925 { 1926 for ( yy_i = 0; 1927 yytoks[yy_i].t_val >= 0; 1928 yy_i++ ) 1929 { 1930 if ( yytoks[yy_i].t_val 1931 == yychar ) 1932 { 1933 break; 1934 } 1935 } 1936 printf( "token %s\n", 1937 yytoks[yy_i].t_name ); 1938 } 1939 } 1940 #endif /* YYDEBUG */ 1941 if ( yychar == 0 ) /* reached EOF. quit */ 1942 YYABORT; 1943 yychar = -1; 1944 goto yy_newstate; 1945 } 1946 }/* end if ( yy_n == 0 ) */ 1947 /* 1948 ** reduction by production yy_n 1949 ** put stack tops, etc. so things right after switch 1950 */ 1951 #if YYDEBUG 1952 /* 1953 ** if debugging, print the string that is the user's 1954 ** specification of the reduction which is just about 1955 ** to be done. 1956 */ 1957 if ( yydebug ) 1958 printf( "Reduce by (%d) \"%s\"\n", 1959 yy_n, yyreds[ yy_n ] ); 1960 #endif 1961 yytmp = yy_n; /* value to switch over */ 1962 yypvt = yy_pv; /* $vars top of value stack */ 1963 /* 1964 ** Look in goto table for next state 1965 ** Sorry about using yy_state here as temporary 1966 ** register variable, but why not, if it works... 1967 ** If yyr2[ yy_n ] doesn't have the low order bit 1968 ** set, then there is no action to be done for 1969 ** this reduction. So, no saving & unsaving of 1970 ** registers done. The only difference between the 1971 ** code just after the if and the body of the if is 1972 ** the goto yy_stack in the body. This way the test 1973 ** can be made before the choice of what to do is needed. 1974 */ 1975 { 1976 /* length of production doubled with extra bit */ 1977 register int yy_len = yyr2[ yy_n ]; 1978 1979 if ( !( yy_len & 01 ) ) 1980 { 1981 yy_len >>= 1; 1982 yyval = ( yy_pv -= yy_len )[1]; /* $$ = $1 */ 1983 yy_state = yypgo[ yy_n = yyr1[ yy_n ] ] + 1984 *( yy_ps -= yy_len ) + 1; 1985 if ( yy_state >= YYLAST || 1986 yychk[ yy_state = 1987 yyact[ yy_state ] ] != -yy_n ) 1988 { 1989 yy_state = yyact[ yypgo[ yy_n ] ]; 1990 } 1991 goto yy_stack; 1992 } 1993 yy_len >>= 1; 1994 yyval = ( yy_pv -= yy_len )[1]; /* $$ = $1 */ 1995 yy_state = yypgo[ yy_n = yyr1[ yy_n ] ] + 1996 *( yy_ps -= yy_len ) + 1; 1997 if ( yy_state >= YYLAST || 1998 yychk[ yy_state = yyact[ yy_state ] ] != -yy_n ) 1999 { 2000 yy_state = yyact[ yypgo[ yy_n ] ]; 2001 } 2002 } 2003 /* save until reenter driver code */ 2004 yystate = yy_state; 2005 yyps = yy_ps; 2006 yypv = yy_pv; 2007 } 2008 /* 2009 ** code supplied by user is placed in this switch 2010 */ 2011 switch( yytmp ) 2012 { 2013 2014 case 1: 2015 # line 136 "egrep.y" 2016 { 2017 unary(FINAL, yypvt[-0]); 2018 line--; 2019 } break; 2020 case 2: 2021 # line 142 "egrep.y" 2022 { yyval = node(CAT, yypvt[-1], yypvt[-0]); } break; 2023 case 3: 2024 # line 144 "egrep.y" 2025 { yyval = node(CAT, yypvt[-2], yypvt[-1]); } break; 2026 case 4: 2027 # line 146 "egrep.y" 2028 { yyval = node(CAT, yypvt[-1], yypvt[-0]); } break; 2029 case 5: 2030 # line 148 "egrep.y" 2031 { yyval = node(CAT, yypvt[-2], yypvt[-1]); } break; 2032 case 6: 2033 # line 151 "egrep.y" 2034 { /* if(multibyte) 2035 $$ = mdotenter(); 2036 else */ 2037 yyval = enter(DOT); 2038 yyval = unary(STAR, yyval); 2039 } break; 2040 case 7: 2041 # line 159 "egrep.y" 2042 { yyval = iflag && isalpha(yypvt[-0]) ? 2043 node(OR, enter(tolower(yypvt[-0])), enter(toupper(yypvt[-0]))) : enter(yypvt[-0]); } break; 2044 case 8: 2045 # line 162 "egrep.y" 2046 { yyval = (iflag && iswalpha(lyylval)) ? 2047 node(OR, mchar(towlower(lyylval)), mchar(towupper(lyylval))) : 2048 mchar(lyylval); } break; 2049 case 9: 2050 # line 166 "egrep.y" 2051 { if(multibyte) 2052 yyval = mdotenter(); 2053 else 2054 yyval = enter(DOT); 2055 } break; 2056 case 10: 2057 # line 172 "egrep.y" 2058 { yyval = cclenter(CCL); } break; 2059 case 11: 2060 # line 174 "egrep.y" 2061 { yyval = cclenter(NCCL); } break; 2062 case 12: 2063 # line 176 "egrep.y" 2064 { yyval = ccl(CCL); } break; 2065 case 13: 2066 # line 178 "egrep.y" 2067 { yyval = ccl(NCCL); } break; 2068 case 14: 2069 # line 182 "egrep.y" 2070 { yyval = node(OR, yypvt[-2], yypvt[-0]); } break; 2071 case 15: 2072 # line 184 "egrep.y" 2073 { yyval = node(CAT, yypvt[-1], yypvt[-0]); } break; 2074 case 16: 2075 # line 186 "egrep.y" 2076 { yyval = unary(STAR, yypvt[-1]); } break; 2077 case 17: 2078 # line 188 "egrep.y" 2079 { yyval = unary(PLUS, yypvt[-1]); } break; 2080 case 18: 2081 # line 190 "egrep.y" 2082 { yyval = unary(QUEST, yypvt[-1]); } break; 2083 case 19: 2084 # line 192 "egrep.y" 2085 { yyval = yypvt[-1]; } break; 2086 # line 556 "/usr/share/lib/ccs/yaccpar" 2087 } 2088 goto yystack; /* reset registers in driver code */ 2089 } 2090 2091