Print this page
3737 grep does not support -H option


  15  * When distributing Covered Code, include this CDDL HEADER in each
  16  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  17  * If applicable, add the following below this CDDL HEADER, with the
  18  * fields enclosed by brackets "[]" replaced with your own identifying
  19  * information: Portions Copyright [yyyy] [name of copyright owner]
  20  *
  21  * CDDL HEADER END
  22  */
  23 %}
  24 /*
  25  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
  26  * Use is subject to license terms.
  27  */
  28 
  29 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T     */
  30 /*        All Rights Reserved   */
  31 
  32 /*      Copyright (c) 1987, 1988 Microsoft Corporation  */
  33 /*        All Rights Reserved   */
  34 
  35 %{
  36 #pragma ident   "%Z%%M% %I%     %E% SMI"
  37 %}
  38 
  39 /*
  40  * egrep -- print lines containing (or not containing) a regular expression
  41  *
  42  *      status returns:
  43  *              0 - ok, and some matches
  44  *              1 - ok, but no matches
  45  *              2 - some error; matches irrelevant
  46  */
  47 %token CHAR MCHAR DOT MDOT CCL NCCL MCCL NMCCL OR CAT STAR PLUS QUEST
  48 %left OR
  49 %left CHAR MCHAR DOT CCL NCCL MCCL NMCCL '('
  50 %left CAT
  51 %left STAR PLUS QUEST
  52 
  53 %{
  54 #include <stdio.h>
  55 #include <ctype.h>
  56 #include <memory.h>
  57 #include <wchar.h>
  58 #include <wctype.h>
  59 #include <widec.h>
  60 #include <stdlib.h>
  61 #include <limits.h>
  62 #include <locale.h>
  63 


  64 #define BLKSIZE 512     /* size of reported disk blocks */
  65 #define EBUFSIZ 8192
  66 #define MAXLIN 350
  67 #define NCHARS 256
  68 #define MAXPOS 4000
  69 #define NSTATES 64
  70 #define FINAL -1
  71 #define RIGHT '\n'      /* serves as record separator and as $ */
  72 #define LEFT '\n'       /* beginning of line */
  73 int gotofn[NSTATES][NCHARS];
  74 int state[NSTATES];
  75 int out[NSTATES];
  76 int line  = 1;
  77 int *name;
  78 int *left;
  79 int *right;
  80 int *parent;
  81 int *foll;
  82 int *positions;
  83 char *chars;


  93 int nstate = 1;
  94 int xstate;
  95 int count;
  96 int icount;
  97 char *input;
  98 
  99 
 100 wchar_t lyylval;
 101 wchar_t nextch();
 102 wchar_t maxmin();
 103 int compare();
 104 void overflo();
 105 
 106 char reinit = 0;
 107 
 108 long long lnum;
 109 int     bflag;
 110 int     cflag;
 111 int     eflag;
 112 int     fflag;

 113 int     hflag;
 114 int     iflag;
 115 int     lflag;
 116 int     nflag;
 117 int     sflag;
 118 int     vflag;
 119 int     nfile;
 120 long long blkno;
 121 long long tln;
 122 int     nsucc;
 123 int     badbotch;
 124 extern  char *optarg;
 125 extern  int optind;
 126 
 127 int     f;
 128 FILE    *expfile;
 129 %}
 130 
 131 %%
 132 s:      t
 133                 { 
 134                   unary(FINAL, $1);
 135                   line--;
 136                 }
 137         ;


 643                                 return;
 644         }
 645 }
 646 
 647 #define USAGE "[ -bchilnsv ] [ -e exp ] [ -f file ] [ strings ] [ file ] ..." 
 648 
 649 int
 650 main(int argc, char **argv)
 651 {
 652         char c;
 653         char nl = '\n';
 654         int errflag = 0;
 655         
 656         (void)setlocale(LC_ALL, "");
 657 
 658 #if !defined(TEXT_DOMAIN)               /* Should be defined by cc -D */
 659         #define TEXT_DOMAIN "SYS_TEST"  /* Use this only if it weren't. */
 660 #endif
 661         (void) textdomain(TEXT_DOMAIN);
 662 
 663         while((c = getopt(argc, argv, "ybcie:f:hlnvs")) != -1)
 664                 switch(c) {
 665 
 666                 case 'b':
 667                         bflag++;
 668                         continue;
 669 
 670                 case 'c':
 671                         cflag++;
 672                         continue;
 673 
 674                 case 'e':
 675                         eflag++;
 676                         input = optarg;
 677                         continue;
 678 
 679                 case 'f':
 680                         fflag++;
 681                         expfile = fopen(optarg, "r");
 682                         if(expfile == NULL) {
 683                                 fprintf(stderr, 
 684                                   gettext("egrep: can't open %s\n"), optarg);
 685                                 exit(2);
 686                         }
 687                         continue;
 688 






 689                 case 'h':
 690                         hflag++;

 691                         continue;
 692 
 693                 case 'y':
 694                 case 'i':
 695                         iflag++;
 696                         continue;
 697 
 698                 case 'l':
 699                         lflag++;

 700                         continue;
 701 
 702                 case 'n':
 703                         nflag++;
 704                         continue;
 705 
 706                 case 's':
 707                         sflag++;

 708                         continue;
 709 
 710                 case 'v':
 711                         vflag++;
 712                         continue;
 713 
 714                 case '?':
 715                         errflag++;
 716                 }
 717         if (errflag || ((argc <= 0) && !fflag && !eflag)) {
 718                 fprintf(stderr, gettext("usage: egrep %s\n"), gettext(USAGE));
 719                 exit(2);
 720         }
 721         if(!eflag && !fflag) {
 722                 input = argv[optind];
 723                 optind++;
 724         }
 725 
 726         argc -= optind;
 727         argv = &argv[optind];


 782         int succ;
 783         char *ptr, *ptrend, *lastptr;
 784         char *buf;
 785         long lBufSiz;
 786         FILE *f;
 787         int nlflag;
 788 
 789         lBufSiz = EBUFSIZ;
 790         if ((buf = malloc (lBufSiz + EBUFSIZ)) == NULL) {
 791                 exit (2); /* out of memory - BAIL */
 792         }
 793 
 794         if (file) {
 795                 if ((f = fopen(file, "r")) == NULL) {
 796                         fprintf(stderr, 
 797                                 gettext("egrep: can't open %s\n"), file);
 798                         badbotch=1;
 799                         return;
 800                 }
 801         } else {
 802                 file = "<stdin>";
 803                 f = stdin;

 804         }
 805         lnum = 1;
 806         tln = 0;
 807         if((count = read(fileno(f), buf, EBUFSIZ)) <= 0) {
 808                 fclose(f);
 809 
 810                 if (cflag) {
 811                         if (nfile>1 && !hflag)
 812                                 fprintf(stdout, "%s:", file);
 813                         fprintf(stdout, "%lld\n", tln);
 814                 }
 815                 return;
 816         }
 817 
 818         blkno = count;
 819         ptr = buf;
 820         for(;;) {
 821                 if((ptrend = memchr(ptr, '\n', buf + count - ptr)) == NULL) {
 822                         /* 
 823                                 move the unused partial record to the head of the buffer 
 824                         */
 825                         if (ptr > buf) {
 826                                 count = buf + count - ptr;
 827                                 memmove (buf, ptr, count);
 828                                 ptr = buf;
 829                         }
 830 
 831                         /*


 885                                         p = lastptr;
 886                                         continue;
 887                                 }
 888                                 succ = !vflag;
 889                                 break;
 890                         }
 891                         c = (unsigned char)*p++;
 892                         if ((t = gotofn[cstat][c]) == 0)
 893                                 cstat = nxtst(cstat, c);
 894                         else
 895                                 cstat = t;
 896                         if(c == RIGHT) {
 897                                 if(out[cstat]) {
 898                                         succ = !vflag;
 899                                         break;
 900                                 }
 901                                 succ = vflag;
 902                                 break;
 903                         }
 904                 }
 905                 if(succ) {
 906                         nsucc = 1;
 907                         if (cflag) tln++;
 908                         else if (sflag)
 909                                 ;       /* ugh */
 910                         else if (lflag) {
 911                                 printf("%s\n", file);
 912                                 fclose(f);
 913                                 return;
 914                         }
 915                         else {
 916                                 if (nfile > 1 && !hflag) 
 917                                         printf(gettext("%s:"), file);


 918                                 if (bflag) {
 919                                         nchars = blkno - (buf + count - ptrend) - 2;
 920                                         if(nlflag)
 921                                                 nchars++;
 922                                         printf("%lld:", nchars/BLKSIZE);
 923                                 }
 924                                 if (nflag) 
 925                                         printf("%lld:", lnum);
 926                                 if(nlflag)
 927                                         nchars = ptrend - ptr + 1;
 928                                 else
 929                                         nchars = ptrend - ptr;
 930                                 fwrite(ptr, (size_t)1, (size_t)nchars, stdout);
 931                         }
 932                 }
 933                 if(!nlflag)
 934                         break;
 935                 ptr = ptrend + 1;
 936                 if(ptr >= buf + count) {
 937                         ptr = buf;
 938                         if((count = read(fileno(f), buf, EBUFSIZ)) <= 0)
 939                                 break;
 940                         blkno += count;
 941                 }
 942                 lnum++;
 943                 if (reinit == 1) 
 944                         clearg();
 945         }
 946         fclose(f);
 947         if (cflag) {
 948                 if (nfile > 1 && !hflag)
 949                         printf(gettext("%s:"), file);
 950                 printf("%lld\n", tln);
 951         }
 952 }
 953 
 954 void
 955 clearg(void)
 956 {
 957         int i, k;
 958         for (i=1; i<=nstate; i++)
 959                 out[i] = 0;
 960         for (i=1; i<=nstate; i++)
 961                 for (k=0; k<NCHARS; k++)
 962                         gotofn[i][k] = 0;
 963         nstate = 1;
 964         nxtpos = inxtpos;
 965         reinit = 0;
 966         count = 0;
 967         for (i=3; i<=line; i++) tmpstat[i] = 0;
 968         if (cstate(line-1)==0) {
 969                 tmpstat[line] = 1;




  15  * When distributing Covered Code, include this CDDL HEADER in each
  16  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  17  * If applicable, add the following below this CDDL HEADER, with the
  18  * fields enclosed by brackets "[]" replaced with your own identifying
  19  * information: Portions Copyright [yyyy] [name of copyright owner]
  20  *
  21  * CDDL HEADER END
  22  */
  23 %}
  24 /*
  25  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
  26  * Use is subject to license terms.
  27  */
  28 
  29 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T     */
  30 /*        All Rights Reserved   */
  31 
  32 /*      Copyright (c) 1987, 1988 Microsoft Corporation  */
  33 /*        All Rights Reserved   */
  34 
  35 /*
  36  * Copyright 2013 Damian Bogel. All rights reserved.
  37  */
  38 
  39 /*
  40  * egrep -- print lines containing (or not containing) a regular expression
  41  *
  42  *      status returns:
  43  *              0 - ok, and some matches
  44  *              1 - ok, but no matches
  45  *              2 - some error; matches irrelevant
  46  */
  47 %token CHAR MCHAR DOT MDOT CCL NCCL MCCL NMCCL OR CAT STAR PLUS QUEST
  48 %left OR
  49 %left CHAR MCHAR DOT CCL NCCL MCCL NMCCL '('
  50 %left CAT
  51 %left STAR PLUS QUEST
  52 
  53 %{
  54 #include <stdio.h>
  55 #include <ctype.h>
  56 #include <memory.h>
  57 #include <wchar.h>
  58 #include <wctype.h>
  59 #include <widec.h>
  60 #include <stdlib.h>
  61 #include <limits.h>
  62 #include <locale.h>
  63 
  64 #define STDIN_FILENAME gettext("(standard input)")
  65 
  66 #define BLKSIZE 512     /* size of reported disk blocks */
  67 #define EBUFSIZ 8192
  68 #define MAXLIN 350
  69 #define NCHARS 256
  70 #define MAXPOS 4000
  71 #define NSTATES 64
  72 #define FINAL -1
  73 #define RIGHT '\n'      /* serves as record separator and as $ */
  74 #define LEFT '\n'       /* beginning of line */
  75 int gotofn[NSTATES][NCHARS];
  76 int state[NSTATES];
  77 int out[NSTATES];
  78 int line  = 1;
  79 int *name;
  80 int *left;
  81 int *right;
  82 int *parent;
  83 int *foll;
  84 int *positions;
  85 char *chars;


  95 int nstate = 1;
  96 int xstate;
  97 int count;
  98 int icount;
  99 char *input;
 100 
 101 
 102 wchar_t lyylval;
 103 wchar_t nextch();
 104 wchar_t maxmin();
 105 int compare();
 106 void overflo();
 107 
 108 char reinit = 0;
 109 
 110 long long lnum;
 111 int     bflag;
 112 int     cflag;
 113 int     eflag;
 114 int     fflag;
 115 int     Hflag;
 116 int     hflag;
 117 int     iflag;
 118 int     lflag;
 119 int     nflag;
 120 int     qflag;
 121 int     vflag;
 122 int     nfile;
 123 long long blkno;
 124 long long tln;
 125 int     nsucc;
 126 int     badbotch;
 127 extern  char *optarg;
 128 extern  int optind;
 129 
 130 int     f;
 131 FILE    *expfile;
 132 %}
 133 
 134 %%
 135 s:      t
 136                 { 
 137                   unary(FINAL, $1);
 138                   line--;
 139                 }
 140         ;


 646                                 return;
 647         }
 648 }
 649 
 650 #define USAGE "[ -bchilnsv ] [ -e exp ] [ -f file ] [ strings ] [ file ] ..." 
 651 
 652 int
 653 main(int argc, char **argv)
 654 {
 655         char c;
 656         char nl = '\n';
 657         int errflag = 0;
 658         
 659         (void)setlocale(LC_ALL, "");
 660 
 661 #if !defined(TEXT_DOMAIN)               /* Should be defined by cc -D */
 662         #define TEXT_DOMAIN "SYS_TEST"  /* Use this only if it weren't. */
 663 #endif
 664         (void) textdomain(TEXT_DOMAIN);
 665 
 666         while((c = getopt(argc, argv, "ybcie:f:Hhlnvs")) != -1)
 667                 switch(c) {
 668 
 669                 case 'b':
 670                         bflag++;
 671                         continue;
 672 
 673                 case 'c':
 674                         cflag++;
 675                         continue;
 676 
 677                 case 'e':
 678                         eflag++;
 679                         input = optarg;
 680                         continue;
 681 
 682                 case 'f':
 683                         fflag++;
 684                         expfile = fopen(optarg, "r");
 685                         if(expfile == NULL) {
 686                                 fprintf(stderr, 
 687                                   gettext("egrep: can't open %s\n"), optarg);
 688                                 exit(2);
 689                         }
 690                         continue;
 691 
 692                 case 'H':
 693                         if (!lflag) /* H is excluded by l as in GNU grep */
 694                                 Hflag++;
 695                         hflag = 0; /* H excludes h */
 696                         continue;
 697 
 698                 case 'h':
 699                         hflag++;
 700                         Hflag = 0; /* h excludes H */
 701                         continue;
 702 
 703                 case 'y':
 704                 case 'i':
 705                         iflag++;
 706                         continue;
 707 
 708                 case 'l':
 709                         lflag++;
 710                         Hflag = 0; /* l excludes H */
 711                         continue;
 712 
 713                 case 'n':
 714                         nflag++;
 715                         continue;
 716 
 717                 case 'q':
 718                 case 's': /* Solaris: legacy option */
 719                         qflag++;
 720                         continue;
 721 
 722                 case 'v':
 723                         vflag++;
 724                         continue;
 725 
 726                 case '?':
 727                         errflag++;
 728                 }
 729         if (errflag || ((argc <= 0) && !fflag && !eflag)) {
 730                 fprintf(stderr, gettext("usage: egrep %s\n"), gettext(USAGE));
 731                 exit(2);
 732         }
 733         if(!eflag && !fflag) {
 734                 input = argv[optind];
 735                 optind++;
 736         }
 737 
 738         argc -= optind;
 739         argv = &argv[optind];


 794         int succ;
 795         char *ptr, *ptrend, *lastptr;
 796         char *buf;
 797         long lBufSiz;
 798         FILE *f;
 799         int nlflag;
 800 
 801         lBufSiz = EBUFSIZ;
 802         if ((buf = malloc (lBufSiz + EBUFSIZ)) == NULL) {
 803                 exit (2); /* out of memory - BAIL */
 804         }
 805 
 806         if (file) {
 807                 if ((f = fopen(file, "r")) == NULL) {
 808                         fprintf(stderr, 
 809                                 gettext("egrep: can't open %s\n"), file);
 810                         badbotch=1;
 811                         return;
 812                 }
 813         } else {

 814                 f = stdin;
 815                 file = STDIN_FILENAME;
 816         }
 817         lnum = 1;
 818         tln = 0;
 819         if((count = read(fileno(f), buf, EBUFSIZ)) <= 0) {
 820                 fclose(f);
 821 
 822                 if (cflag && !qflag) {
 823                         if (Hflag || (nfile > 1 && !hflag))
 824                                 fprintf(stdout, "%s:", file);
 825                         fprintf(stdout, "%lld\n", tln);
 826                 }
 827                 return;
 828         }
 829 
 830         blkno = count;
 831         ptr = buf;
 832         for(;;) {
 833                 if((ptrend = memchr(ptr, '\n', buf + count - ptr)) == NULL) {
 834                         /* 
 835                                 move the unused partial record to the head of the buffer 
 836                         */
 837                         if (ptr > buf) {
 838                                 count = buf + count - ptr;
 839                                 memmove (buf, ptr, count);
 840                                 ptr = buf;
 841                         }
 842 
 843                         /*


 897                                         p = lastptr;
 898                                         continue;
 899                                 }
 900                                 succ = !vflag;
 901                                 break;
 902                         }
 903                         c = (unsigned char)*p++;
 904                         if ((t = gotofn[cstat][c]) == 0)
 905                                 cstat = nxtst(cstat, c);
 906                         else
 907                                 cstat = t;
 908                         if(c == RIGHT) {
 909                                 if(out[cstat]) {
 910                                         succ = !vflag;
 911                                         break;
 912                                 }
 913                                 succ = vflag;
 914                                 break;
 915                         }
 916                 }
 917                 if (succ) {
 918                         nsucc = 1;
 919                         if (lflag || qflag) {
 920                                 if (!qflag)
 921                                         (void) printf("%s\n", file);


 922                                 fclose(f);
 923                                 return;
 924                         }
 925                         if (cflag) {
 926                                 tln++;
 927                         } else {
 928                                 if (Hflag || (nfile > 1 && !hflag))
 929                                         printf("%s:", file);
 930                                 if (bflag) {
 931                                         nchars = blkno - (buf + count - ptrend) - 2;
 932                                         if(nlflag)
 933                                                 nchars++;
 934                                         printf("%lld:", nchars/BLKSIZE);
 935                                 }
 936                                 if (nflag) 
 937                                         printf("%lld:", lnum);
 938                                 if(nlflag)
 939                                         nchars = ptrend - ptr + 1;
 940                                 else
 941                                         nchars = ptrend - ptr;
 942                                 fwrite(ptr, (size_t)1, (size_t)nchars, stdout);
 943                         }
 944                 }
 945                 if(!nlflag)
 946                         break;
 947                 ptr = ptrend + 1;
 948                 if(ptr >= buf + count) {
 949                         ptr = buf;
 950                         if((count = read(fileno(f), buf, EBUFSIZ)) <= 0)
 951                                 break;
 952                         blkno += count;
 953                 }
 954                 lnum++;
 955                 if (reinit == 1) 
 956                         clearg();
 957         }
 958         fclose(f);
 959         if (cflag && !qflag) {
 960                 if (Hflag || (nfile > 1 && !hflag))
 961                         printf("%s:", file);
 962                 printf("%lld\n", tln);
 963         }
 964 }
 965 
 966 void
 967 clearg(void)
 968 {
 969         int i, k;
 970         for (i=1; i<=nstate; i++)
 971                 out[i] = 0;
 972         for (i=1; i<=nstate; i++)
 973                 for (k=0; k<NCHARS; k++)
 974                         gotofn[i][k] = 0;
 975         nstate = 1;
 976         nxtpos = inxtpos;
 977         reinit = 0;
 978         count = 0;
 979         for (i=3; i<=line; i++) tmpstat[i] = 0;
 980         if (cstate(line-1)==0) {
 981                 tmpstat[line] = 1;