/[pcre]/code/trunk/pcregrep.c
ViewVC logotype

Diff of /code/trunk/pcregrep.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 944 by ph10, Tue Feb 28 16:49:21 2012 UTC revision 947 by ph10, Sun Mar 4 16:51:13 2012 UTC
# Line 104  enum { DEE_READ, DEE_SKIP }; Line 104  enum { DEE_READ, DEE_SKIP };
104    
105  enum { EL_LF, EL_CR, EL_CRLF, EL_ANY, EL_ANYCRLF };  enum { EL_LF, EL_CR, EL_CRLF, EL_ANY, EL_ANYCRLF };
106    
107    /* Binary file options */
108    
109    enum { BIN_BINARY, BIN_NOMATCH, BIN_TEXT };
110    
111  /* In newer versions of gcc, with FORTIFY_SOURCE set (the default in some  /* In newer versions of gcc, with FORTIFY_SOURCE set (the default in some
112  environments), a warning is issued if the value of fwrite() is ignored.  environments), a warning is issued if the value of fwrite() is ignored.
113  Unfortunately, casting to (void) does not suppress the warning. To get round  Unfortunately, casting to (void) does not suppress the warning. To get round
# Line 160  static pcre *exclude_dir_compiled = NULL Line 164  static pcre *exclude_dir_compiled = NULL
164    
165  static int after_context = 0;  static int after_context = 0;
166  static int before_context = 0;  static int before_context = 0;
167    static int binary_files = BIN_BINARY;
168  static int both_context = 0;  static int both_context = 0;
169  static int bufthird = PCREGREP_BUFSIZE;  static int bufthird = PCREGREP_BUFSIZE;
170  static int bufsize = 3*PCREGREP_BUFSIZE;  static int bufsize = 3*PCREGREP_BUFSIZE;
# Line 197  static BOOL utf8 = FALSE; Line 202  static BOOL utf8 = FALSE;
202  /* Structure for options and list of them */  /* Structure for options and list of them */
203    
204  enum { OP_NODATA, OP_STRING, OP_OP_STRING, OP_NUMBER, OP_LONGNUMBER,  enum { OP_NODATA, OP_STRING, OP_OP_STRING, OP_NUMBER, OP_LONGNUMBER,
205         OP_OP_NUMBER, OP_PATLIST };         OP_OP_NUMBER, OP_PATLIST, OP_BINFILES };
206    
207  typedef struct option_item {  typedef struct option_item {
208    int type;    int type;
# Line 227  used to identify them. */ Line 232  used to identify them. */
232  #define N_BUFSIZE      (-15)  #define N_BUFSIZE      (-15)
233  #define N_NOJIT        (-16)  #define N_NOJIT        (-16)
234  #define N_FILE_LIST    (-17)  #define N_FILE_LIST    (-17)
235    #define N_BINARY_FILES (-18)
236    
237  static option_item optionlist[] = {  static option_item optionlist[] = {
238    { OP_NODATA,     N_NULL,   NULL,              "",              "  terminate options" },    { OP_NODATA,     N_NULL,   NULL,              "",              "terminate options" },
239    { OP_NODATA,     N_HELP,   NULL,              "help",          "display this help and exit" },    { OP_NODATA,     N_HELP,   NULL,              "help",          "display this help and exit" },
240    { OP_NUMBER,     'A',      &after_context,    "after-context=number", "set number of following context lines" },    { OP_NUMBER,     'A',      &after_context,    "after-context=number", "set number of following context lines" },
241      { OP_NODATA,     'a',      NULL,              "text",          "treat binary files as text" },
242    { OP_NUMBER,     'B',      &before_context,   "before-context=number", "set number of prior context lines" },    { OP_NUMBER,     'B',      &before_context,   "before-context=number", "set number of prior context lines" },
243      { OP_BINFILES,   N_BINARY_FILES, NULL,        "binary-files=word", "set treatment of binary files" },
244    { OP_NUMBER,     N_BUFSIZE,&bufthird,         "buffer-size=number", "set processing buffer size parameter" },    { OP_NUMBER,     N_BUFSIZE,&bufthird,         "buffer-size=number", "set processing buffer size parameter" },
245    { OP_OP_STRING,  N_COLOUR, &colour_option,    "color=option",  "matched text color option" },    { OP_OP_STRING,  N_COLOUR, &colour_option,    "color=option",  "matched text color option" },
246    { OP_OP_STRING,  N_COLOUR, &colour_option,    "colour=option", "matched text colour option" },    { OP_OP_STRING,  N_COLOUR, &colour_option,    "colour=option", "matched text colour option" },
# Line 247  static option_item optionlist[] = { Line 255  static option_item optionlist[] = {
255    { OP_NODATA,     N_FOFFSETS, NULL,            "file-offsets",  "output file offsets, not text" },    { OP_NODATA,     N_FOFFSETS, NULL,            "file-offsets",  "output file offsets, not text" },
256    { OP_NODATA,     'H',      NULL,              "with-filename", "force the prefixing filename on output" },    { OP_NODATA,     'H',      NULL,              "with-filename", "force the prefixing filename on output" },
257    { OP_NODATA,     'h',      NULL,              "no-filename",   "suppress the prefixing filename on output" },    { OP_NODATA,     'h',      NULL,              "no-filename",   "suppress the prefixing filename on output" },
258      { OP_NODATA,     'I',      NULL,              "",              "treat binary files as not matching (ignore)" },
259    { OP_NODATA,     'i',      NULL,              "ignore-case",   "ignore case distinctions" },    { OP_NODATA,     'i',      NULL,              "ignore-case",   "ignore case distinctions" },
260  #ifdef SUPPORT_PCREGREP_JIT  #ifdef SUPPORT_PCREGREP_JIT
261    { OP_NODATA,     N_NOJIT,  NULL,              "no-jit",        "do not use just-in-time compiler optimization" },    { OP_NODATA,     N_NOJIT,  NULL,              "no-jit",        "do not use just-in-time compiler optimization" },
# Line 1047  char *lastmatchrestart = NULL; Line 1056  char *lastmatchrestart = NULL;
1056  char *ptr = main_buffer;  char *ptr = main_buffer;
1057  char *endptr;  char *endptr;
1058  size_t bufflength;  size_t bufflength;
1059    BOOL binary = FALSE;
1060  BOOL endhyphenpending = FALSE;  BOOL endhyphenpending = FALSE;
1061  BOOL input_line_buffered = line_buffered;  BOOL input_line_buffered = line_buffered;
1062  FILE *in = NULL;                    /* Ensure initialized */  FILE *in = NULL;                    /* Ensure initialized */
# Line 1094  else Line 1104  else
1104    
1105  endptr = main_buffer + bufflength;  endptr = main_buffer + bufflength;
1106    
1107    /* Unless binary-files=text, see if we have a binary file. This uses the same
1108    rule as GNU grep, namely, a search for a binary zero byte near the start of the
1109    file. */
1110    
1111    if (binary_files != BIN_TEXT)
1112      {
1113      binary =
1114        memchr(main_buffer, 0, (bufflength > 1024)? 1024 : bufflength) != NULL;
1115      if (binary && binary_files == BIN_NOMATCH) return 1;
1116      }
1117    
1118  /* Loop while the current pointer is not at the end of the file. For large  /* Loop while the current pointer is not at the end of the file. For large
1119  files, endptr will be at the end of the buffer when we are in the middle of the  files, endptr will be at the end of the buffer when we are in the middle of the
1120  file, but ptr will never get there, because as soon as it gets over 2/3 of the  file, but ptr will never get there, because as soon as it gets over 2/3 of the
# Line 1209  while (ptr < endptr) Line 1230  while (ptr < endptr)
1230      /* Just count if just counting is wanted. */      /* Just count if just counting is wanted. */
1231    
1232      if (count_only) count++;      if (count_only) count++;
1233    
1234        /* When handling a binary file and binary-files==binary, the "binary"
1235        variable will be set true (it's false in all other cases). In this
1236        situation we just want to output the file name. No need to scan further. */
1237    
1238        else if (binary)
1239          {
1240          fprintf(stdout, "Binary file %s matches\n", filename);
1241          return 0;
1242          }
1243    
1244      /* If all we want is a file name, there is no need to scan any more lines      /* If all we want is a file name, there is no need to scan any more lines
1245      in the file. */      in the file. */
# Line 1845  for (op = optionlist; op->one_char != 0; Line 1876  for (op = optionlist; op->one_char != 0;
1876    contains an underscore. */    contains an underscore. */
1877    
1878    if (strchr(op->long_name, '_') != NULL) continue;    if (strchr(op->long_name, '_') != NULL) continue;
1879    
1880      if (op->one_char > 0 && (op->long_name)[0] == 0)
1881        n = 31 - printf("  -%c", op->one_char);
1882      else
1883        {
1884        if (op->one_char > 0) sprintf(s, "-%c,", op->one_char);
1885          else strcpy(s, "   ");
1886        n = 31 - printf("  %s --%s", s, op->long_name);
1887        }
1888    
   if (op->one_char > 0) sprintf(s, "-%c,", op->one_char); else strcpy(s, "   ");  
   n = 31 - printf("  %s --%s", s, op->long_name);  
1889    if (n < 1) n = 1;    if (n < 1) n = 1;
1890    printf("%.*s%s\n", n, "                     ", op->help_text);    printf("%.*s%s\n", n, "                           ", op->help_text);
1891    }    }
1892    
1893  printf("\nNumbers may be followed by K or M, e.g. --buffer-size=100K.\n");  printf("\nNumbers may be followed by K or M, e.g. --buffer-size=100K.\n");
# Line 1880  switch(letter) Line 1918  switch(letter)
1918    case N_LBUFFER: line_buffered = TRUE; break;    case N_LBUFFER: line_buffered = TRUE; break;
1919    case N_LOFFSETS: line_offsets = number = TRUE; break;    case N_LOFFSETS: line_offsets = number = TRUE; break;
1920    case N_NOJIT: study_options &= ~PCRE_STUDY_JIT_COMPILE; break;    case N_NOJIT: study_options &= ~PCRE_STUDY_JIT_COMPILE; break;
1921      case 'a': binary_files = BIN_TEXT; break;
1922    case 'c': count_only = TRUE; break;    case 'c': count_only = TRUE; break;
1923    case 'F': process_options |= PO_FIXED_STRINGS; break;    case 'F': process_options |= PO_FIXED_STRINGS; break;
1924    case 'H': filenames = FN_FORCE; break;    case 'H': filenames = FN_FORCE; break;
1925      case 'I': binary_files = BIN_NOMATCH; break;
1926    case 'h': filenames = FN_NONE; break;    case 'h': filenames = FN_NONE; break;
1927    case 'i': options |= PCRE_CASELESS; break;    case 'i': options |= PCRE_CASELESS; break;
1928    case 'l': omit_zero_count = TRUE; filenames = FN_MATCH_ONLY; break;    case 'l': omit_zero_count = TRUE; filenames = FN_MATCH_ONLY; break;
# Line 2316  for (i = 1; i < argc; i++) Line 2356  for (i = 1; i < argc; i++)
2356    
2357    /* If the option type is OP_PATLIST, it's the -e option, which can be called    /* If the option type is OP_PATLIST, it's the -e option, which can be called
2358    multiple times to create a list of patterns. */    multiple times to create a list of patterns. */
2359    
2360    if (op->type == OP_PATLIST)    if (op->type == OP_PATLIST)
2361      {      {
2362      if (cmd_pattern_count >= MAX_PATTERN_COUNT)      if (cmd_pattern_count >= MAX_PATTERN_COUNT)
# Line 2327  for (i = 1; i < argc; i++) Line 2367  for (i = 1; i < argc; i++)
2367        }        }
2368      patterns[cmd_pattern_count++] = option_data;      patterns[cmd_pattern_count++] = option_data;
2369      }      }
2370    
2371      /* Handle OP_BINARY_FILES */
2372    
2373      else if (op->type == OP_BINFILES)
2374        {
2375        if (strcmp(option_data, "binary") == 0)
2376          binary_files = BIN_BINARY;
2377        else if (strcmp(option_data, "without-match") == 0)
2378          binary_files = BIN_NOMATCH;
2379        else if (strcmp(option_data, "text") == 0)
2380          binary_files = BIN_TEXT;
2381        else
2382          {
2383          fprintf(stderr, "pcregrep: unknown value \"%s\" for binary-files\n",
2384            option_data);
2385          pcregrep_exit(usage(2));
2386          }
2387        }
2388    
2389    /* Otherwise, deal with single string or numeric data values. */    /* Otherwise, deal with single string or numeric data values. */
2390    

Legend:
Removed from v.944  
changed lines
  Added in v.947

  ViewVC Help
Powered by ViewVC 1.1.5