Print this page
3546 add support for grep -o option

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/grep/grep.c
          +++ new/usr/src/cmd/grep/grep.c
↓ open down ↓ 26 lines elided ↑ open up ↑
  27   27  /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
  28   28  /*        All Rights Reserved   */
  29   29  
  30   30  /*      Copyright (c) 1987, 1988 Microsoft Corporation  */
  31   31  /*        All Rights Reserved   */
  32   32  
  33   33  /* Copyright 2012 Nexenta Systems, Inc.  All rights reserved. */
  34   34  
  35   35  /*
  36   36   * Copyright 2013 Damian Bogel. All rights reserved.
       37 + * Copyright (c) 2013 Andrew Stormont.  All rights reserved.
  37   38   */
  38   39  
  39   40  /*
  40   41   * grep -- print lines matching (or not matching) a pattern
  41   42   *
  42   43   *      status returns:
  43   44   *              0 - ok, and some matches
  44   45   *              1 - ok, but no matches
  45   46   *              2 - some error
  46   47   */
↓ open down ↓ 48 lines elided ↑ open up ↑
  95   96  static int      cflag;
  96   97  static int      rflag;
  97   98  static int      Rflag;
  98   99  static int      vflag;
  99  100  static int      sflag;
 100  101  static int      iflag;
 101  102  static int      wflag;
 102  103  static int      hflag;
 103  104  static int      Hflag;
 104  105  static int      qflag;
      106 +static int      oflag;
 105  107  static int      errflg;
 106  108  static int      nfile;
 107  109  static long long        tln;
 108  110  static int      nsucc;
 109  111  static int      outfn = 0;
 110  112  static int      nlflag;
 111  113  static char     *ptr, *ptrend;
 112  114  static char     *expbuf;
 113  115  
 114  116  static void     execute(const char *, int);
↓ open down ↓ 8 lines elided ↑ open up ↑
 123  125          int     c;
 124  126          char    *arg;
 125  127          extern int      optind;
 126  128  
 127  129          (void) setlocale(LC_ALL, "");
 128  130  #if !defined(TEXT_DOMAIN)       /* Should be defined by cc -D */
 129  131  #define TEXT_DOMAIN "SYS_TEST"  /* Use this only if it weren't */
 130  132  #endif
 131  133          (void) textdomain(TEXT_DOMAIN);
 132  134  
 133      -        while ((c = getopt(argc, argv, "hHqblcnRrsviyw")) != -1)
      135 +        while ((c = getopt(argc, argv, "hHqblcnoRrsviyw")) != -1)
 134  136                  switch (c) {
 135  137                  /* based on options order h or H is set as in GNU grep */
 136  138                  case 'h':
 137  139                          hflag++;
 138  140                          Hflag = 0; /* h excludes H */
 139  141                          break;
 140  142                  case 'H':
 141  143                          if (!lflag) /* H is excluded by l */
 142  144                                  Hflag++;
 143  145                          hflag = 0; /* H excludes h */
↓ open down ↓ 3 lines elided ↑ open up ↑
 147  149                          break;
 148  150                  case 'v':
 149  151                          vflag++;
 150  152                          break;
 151  153                  case 'c':
 152  154                          cflag++;
 153  155                          break;
 154  156                  case 'n':
 155  157                          nflag++;
 156  158                          break;
      159 +                case 'o':
      160 +                        oflag++;
      161 +                        break;
 157  162                  case 'R':
 158  163                          Rflag++;
 159  164                          /* FALLTHROUGH */
 160  165                  case 'r':
 161  166                          rflag++;
 162  167                          break;
 163  168                  case 'b':
 164  169                          bflag++;
 165  170                          break;
 166  171                  case 's':
↓ open down ↓ 8 lines elided ↑ open up ↑
 175  180                          iflag++;
 176  181                          break;
 177  182                  case 'w':
 178  183                          wflag++;
 179  184                          break;
 180  185                  case '?':
 181  186                          errflg++;
 182  187                  }
 183  188  
 184  189          if (errflg || (optind >= argc)) {
 185      -                errmsg("Usage: grep [-c|-l|-q] [-r|-R] -hHbnsviw "
      190 +                errmsg("Usage: grep [-c|-l|-q|-o] [-r|-R] -hHbnsviw "
 186  191                      "pattern file . . .\n",
 187  192                      (char *)NULL);
 188  193                  exit(2);
 189  194          }
 190  195  
 191  196          argv = &argv[optind];
 192  197          argc -= optind;
 193  198          nfile = argc - 1;
 194  199  
 195  200          if (strrchr(*argv, '\n') != NULL)
↓ open down ↓ 210 lines elided ↑ open up ↑
 406  411                                      (unsigned char)*p++);
 407  412                          *lbuf = '\0';
 408  413                          lbuf = linebuf;
 409  414                  } else
 410  415                          /*
 411  416                           * Use record as is
 412  417                           */
 413  418                          lbuf = ptr;
 414  419  
 415  420                  /* lflag only once */
 416      -                if ((step(lbuf, expbuf) ^ vflag) && succeed(file) == 1)
 417      -                        break;
      421 +                if (step(lbuf, expbuf) ^ vflag) {
      422 +                        if (oflag) {
      423 +                                /*
      424 +                                 * Only store the matching bits
      425 +                                 */
      426 +                                ptr = loc1;
      427 +                                ptrend = loc2;
      428 +                        }
      429 +                        if (succeed(file) == 1)
      430 +                                break;
      431 +                }
 418  432  
 419  433                  if (!nlflag)
 420  434                          break;
 421  435  
 422  436                  ptr = next_ptr;
 423  437                  count = next_count;
 424  438                  offset = 0;
 425  439          }
 426  440          (void) close(temp);
 427  441  
↓ open down ↓ 105 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX