Print this page
3737 grep does not support -H option
3759 egrep(1) and fgrep(1) -s flag does not hide -c output
Reviewed by: Albert Lee <trisk@nexenta.com>
Reviewed by: Andy Stormont <andyjstormont@gmail.com>

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/egrep/egrep.y
          +++ new/usr/src/cmd/egrep/egrep.y
↓ open down ↓ 24 lines elided ↑ open up ↑
  25   25   * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
  26   26   * Use is subject to license terms.
  27   27   */
  28   28  
  29   29  /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
  30   30  /*        All Rights Reserved   */
  31   31  
  32   32  /*      Copyright (c) 1987, 1988 Microsoft Corporation  */
  33   33  /*        All Rights Reserved   */
  34   34  
  35      -%{
  36      -#pragma ident   "%Z%%M% %I%     %E% SMI"
  37      -%}
       35 +/*
       36 + * Copyright 2013 Damian Bogel. All rights reserved.
       37 + */
  38   38  
  39   39  /*
  40   40   * egrep -- print lines containing (or not containing) a regular expression
  41   41   *
  42   42   *      status returns:
  43   43   *              0 - ok, and some matches
  44   44   *              1 - ok, but no matches
  45   45   *              2 - some error; matches irrelevant
  46   46   */
  47   47  %token CHAR MCHAR DOT MDOT CCL NCCL MCCL NMCCL OR CAT STAR PLUS QUEST
↓ open down ↓ 6 lines elided ↑ open up ↑
  54   54  #include <stdio.h>
  55   55  #include <ctype.h>
  56   56  #include <memory.h>
  57   57  #include <wchar.h>
  58   58  #include <wctype.h>
  59   59  #include <widec.h>
  60   60  #include <stdlib.h>
  61   61  #include <limits.h>
  62   62  #include <locale.h>
  63   63  
       64 +#define STDIN_FILENAME gettext("(standard input)")
       65 +
  64   66  #define BLKSIZE 512     /* size of reported disk blocks */
  65   67  #define EBUFSIZ 8192
  66   68  #define MAXLIN 350
  67   69  #define NCHARS 256
  68   70  #define MAXPOS 4000
  69   71  #define NSTATES 64
  70   72  #define FINAL -1
  71   73  #define RIGHT '\n'      /* serves as record separator and as $ */
  72   74  #define LEFT '\n'       /* beginning of line */
  73   75  int gotofn[NSTATES][NCHARS];
↓ open down ↓ 29 lines elided ↑ open up ↑
 103  105  int compare();
 104  106  void overflo();
 105  107  
 106  108  char reinit = 0;
 107  109  
 108  110  long long lnum;
 109  111  int     bflag;
 110  112  int     cflag;
 111  113  int     eflag;
 112  114  int     fflag;
      115 +int     Hflag;
 113  116  int     hflag;
 114  117  int     iflag;
 115  118  int     lflag;
 116  119  int     nflag;
 117      -int     sflag;
      120 +int     qflag;
 118  121  int     vflag;
 119  122  int     nfile;
 120  123  long long blkno;
 121  124  long long tln;
 122  125  int     nsucc;
 123  126  int     badbotch;
 124  127  extern  char *optarg;
 125  128  extern  int optind;
 126  129  
 127  130  int     f;
↓ open down ↓ 509 lines elided ↑ open up ↑
 637  640                                  else follow(p);
 638  641                                  return;
 639  642                  case FINAL:     if (tmpstat[line] != 1) {
 640  643                                          tmpstat[line] = 1;
 641  644                                          count++;
 642  645                                  }
 643  646                                  return;
 644  647          }
 645  648  }
 646  649  
 647      -#define USAGE "[ -bchilnsv ] [ -e exp ] [ -f file ] [ strings ] [ file ] ..." 
      650 +#define USAGE "[ -bchHilnsqv ] [ -e exp ] [ -f file ] [ strings ] [ file ] ..." 
 648  651  
 649  652  int
 650  653  main(int argc, char **argv)
 651  654  {
 652  655          char c;
 653  656          char nl = '\n';
 654  657          int errflag = 0;
 655  658          
 656  659          (void)setlocale(LC_ALL, "");
 657  660  
 658  661  #if !defined(TEXT_DOMAIN)               /* Should be defined by cc -D */
 659  662          #define TEXT_DOMAIN "SYS_TEST"  /* Use this only if it weren't. */
 660  663  #endif
 661  664          (void) textdomain(TEXT_DOMAIN);
 662  665  
 663      -        while((c = getopt(argc, argv, "ybcie:f:hlnvs")) != -1)
      666 +        while((c = getopt(argc, argv, "ybcie:f:Hhlnvsq")) != -1)
 664  667                  switch(c) {
 665  668  
 666  669                  case 'b':
 667  670                          bflag++;
 668  671                          continue;
 669  672  
 670  673                  case 'c':
 671  674                          cflag++;
 672  675                          continue;
 673  676  
↓ open down ↓ 5 lines elided ↑ open up ↑
 679  682                  case 'f':
 680  683                          fflag++;
 681  684                          expfile = fopen(optarg, "r");
 682  685                          if(expfile == NULL) {
 683  686                                  fprintf(stderr, 
 684  687                                    gettext("egrep: can't open %s\n"), optarg);
 685  688                                  exit(2);
 686  689                          }
 687  690                          continue;
 688  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 +
 689  698                  case 'h':
 690  699                          hflag++;
      700 +                        Hflag = 0; /* h excludes H */
 691  701                          continue;
 692  702  
 693  703                  case 'y':
 694  704                  case 'i':
 695  705                          iflag++;
 696  706                          continue;
 697  707  
 698  708                  case 'l':
 699  709                          lflag++;
      710 +                        Hflag = 0; /* l excludes H */
 700  711                          continue;
 701  712  
 702  713                  case 'n':
 703  714                          nflag++;
 704  715                          continue;
 705  716  
 706      -                case 's':
 707      -                        sflag++;
      717 +                case 'q':
      718 +                case 's': /* Solaris: legacy option */
      719 +                        qflag++;
 708  720                          continue;
 709  721  
 710  722                  case 'v':
 711  723                          vflag++;
 712  724                          continue;
 713  725  
 714  726                  case '?':
 715  727                          errflag++;
 716  728                  }
 717  729          if (errflag || ((argc <= 0) && !fflag && !eflag)) {
↓ open down ↓ 74 lines elided ↑ open up ↑
 792  804          }
 793  805  
 794  806          if (file) {
 795  807                  if ((f = fopen(file, "r")) == NULL) {
 796  808                          fprintf(stderr, 
 797  809                                  gettext("egrep: can't open %s\n"), file);
 798  810                          badbotch=1;
 799  811                          return;
 800  812                  }
 801  813          } else {
 802      -                file = "<stdin>";
 803  814                  f = stdin;
      815 +                file = STDIN_FILENAME;
 804  816          }
 805  817          lnum = 1;
 806  818          tln = 0;
 807  819          if((count = read(fileno(f), buf, EBUFSIZ)) <= 0) {
 808  820                  fclose(f);
 809  821  
 810      -                if (cflag) {
 811      -                        if (nfile>1 && !hflag)
      822 +                if (cflag && !qflag) {
      823 +                        if (Hflag || (nfile > 1 && !hflag))
 812  824                                  fprintf(stdout, "%s:", file);
 813  825                          fprintf(stdout, "%lld\n", tln);
 814  826                  }
 815  827                  return;
 816  828          }
 817  829  
 818  830          blkno = count;
 819  831          ptr = buf;
 820  832          for(;;) {
 821  833                  if((ptrend = memchr(ptr, '\n', buf + count - ptr)) == NULL) {
↓ open down ↓ 73 lines elided ↑ open up ↑
 895  907                                  cstat = t;
 896  908                          if(c == RIGHT) {
 897  909                                  if(out[cstat]) {
 898  910                                          succ = !vflag;
 899  911                                          break;
 900  912                                  }
 901  913                                  succ = vflag;
 902  914                                  break;
 903  915                          }
 904  916                  }
 905      -                if(succ) {
      917 +                if (succ) {
 906  918                          nsucc = 1;
 907      -                        if (cflag) tln++;
 908      -                        else if (sflag)
 909      -                                ;       /* ugh */
 910      -                        else if (lflag) {
 911      -                                printf("%s\n", file);
      919 +                        if (lflag || qflag) {
      920 +                                if (!qflag)
      921 +                                        (void) printf("%s\n", file);
 912  922                                  fclose(f);
 913  923                                  return;
 914  924                          }
 915      -                        else {
 916      -                                if (nfile > 1 && !hflag) 
 917      -                                        printf(gettext("%s:"), file);
      925 +                        if (cflag) {
      926 +                                tln++;
      927 +                        } else {
      928 +                                if (Hflag || (nfile > 1 && !hflag))
      929 +                                        printf("%s:", file);
 918  930                                  if (bflag) {
 919  931                                          nchars = blkno - (buf + count - ptrend) - 2;
 920  932                                          if(nlflag)
 921  933                                                  nchars++;
 922  934                                          printf("%lld:", nchars/BLKSIZE);
 923  935                                  }
 924  936                                  if (nflag) 
 925  937                                          printf("%lld:", lnum);
 926  938                                  if(nlflag)
 927  939                                          nchars = ptrend - ptr + 1;
↓ open down ↓ 9 lines elided ↑ open up ↑
 937  949                          ptr = buf;
 938  950                          if((count = read(fileno(f), buf, EBUFSIZ)) <= 0)
 939  951                                  break;
 940  952                          blkno += count;
 941  953                  }
 942  954                  lnum++;
 943  955                  if (reinit == 1) 
 944  956                          clearg();
 945  957          }
 946  958          fclose(f);
 947      -        if (cflag) {
 948      -                if (nfile > 1 && !hflag)
 949      -                        printf(gettext("%s:"), file);
      959 +        if (cflag && !qflag) {
      960 +                if (Hflag || (nfile > 1 && !hflag))
      961 +                        printf("%s:", file);
 950  962                  printf("%lld\n", tln);
 951  963          }
 952  964  }
 953  965  
 954  966  void
 955  967  clearg(void)
 956  968  {
 957  969          int i, k;
 958  970          for (i=1; i<=nstate; i++)
 959  971                  out[i] = 0;
↓ open down ↓ 347 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX