/[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 971 by ph10, Fri Jun 1 16:29:43 2012 UTC revision 975 by ph10, Sat Jun 2 11:03:06 2012 UTC
# Line 251  static option_item optionlist[] = { Line 251  static option_item optionlist[] = {
251    { OP_PATLIST,    'e',      NULL,              "regex(p)=pattern", "specify pattern (may be used more than once)" },    { OP_PATLIST,    'e',      NULL,              "regex(p)=pattern", "specify pattern (may be used more than once)" },
252    { OP_NODATA,     'F',      NULL,              "fixed-strings", "patterns are sets of newline-separated strings" },    { OP_NODATA,     'F',      NULL,              "fixed-strings", "patterns are sets of newline-separated strings" },
253    { OP_STRING,     'f',      &pattern_filename, "file=path",     "read patterns from file" },    { OP_STRING,     'f',      &pattern_filename, "file=path",     "read patterns from file" },
254    { OP_STRING,     N_FILE_LIST, &file_list,     "file-list=path","read files to search from file" },    { OP_STRING,     N_FILE_LIST, &file_list,     "file-list=path","read files to search from file" },
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" },
# Line 1105  else Line 1105  else
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  /* 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  rule as GNU grep, namely, a search for a binary zero byte near the start of the
1109  file. */  file. */
1110    
1111  if (binary_files != BIN_TEXT)  if (binary_files != BIN_TEXT)
1112    {    {
1113    binary =    binary =
1114      memchr(main_buffer, 0, (bufflength > 1024)? 1024 : bufflength) != NULL;      memchr(main_buffer, 0, (bufflength > 1024)? 1024 : bufflength) != NULL;
1115    if (binary && binary_files == BIN_NOMATCH) return 1;    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
# Line 1230  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"      /* 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      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. */      situation we just want to output the file name. No need to scan further. */
1237    
1238      else if (binary)      else if (binary)
1239        {        {
1240        fprintf(stdout, "Binary file %s matches\n", filename);        fprintf(stdout, "Binary file %s matches\n", filename);
1241        return 0;        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 1876  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)    if (op->one_char > 0 && (op->long_name)[0] == 0)
1881      n = 31 - printf("  -%c", op->one_char);      n = 31 - printf("  -%c", op->one_char);
1882    else    else
1883      {      {
1884      if (op->one_char > 0) sprintf(s, "-%c,", op->one_char);      if (op->one_char > 0) sprintf(s, "-%c,", op->one_char);
1885        else strcpy(s, "   ");        else strcpy(s, "   ");
1886      n = 31 - printf("  %s --%s", s, op->long_name);      n = 31 - printf("  %s --%s", s, op->long_name);
1887      }      }
1888    
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);
# Line 2356  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 2367  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 */    /* Handle OP_BINARY_FILES */
2372    
2373    else if (op->type == OP_BINFILES)    else if (op->type == OP_BINFILES)
2374      {      {
2375      if (strcmp(option_data, "binary") == 0)      if (strcmp(option_data, "binary") == 0)
# Line 2380  for (i = 1; i < argc; i++) Line 2380  for (i = 1; i < argc; i++)
2380        binary_files = BIN_TEXT;        binary_files = BIN_TEXT;
2381      else      else
2382        {        {
2383        fprintf(stderr, "pcregrep: unknown value \"%s\" for binary-files\n",        fprintf(stderr, "pcregrep: unknown value \"%s\" for binary-files\n",
2384          option_data);          option_data);
2385        pcregrep_exit(usage(2));        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    
# Line 2755  if (include_dir_pattern != NULL) Line 2755  if (include_dir_pattern != NULL)
2755      goto EXIT2;      goto EXIT2;
2756      }      }
2757    }    }
2758    
2759  /* If a file that contains a list of files to search has been specified, read  /* If a file that contains a list of files to search has been specified, read
2760  it line by line and search the given files. Otherwise, if there are no further  it line by line and search the given files. Otherwise, if there are no further
2761  arguments, do the business on stdin and exit. */  arguments, do the business on stdin and exit. */
# Line 2765  if (file_list != NULL) Line 2765  if (file_list != NULL)
2765    char buffer[PATBUFSIZE];    char buffer[PATBUFSIZE];
2766    FILE *fl;    FILE *fl;
2767    if (strcmp(file_list, "-") == 0) fl = stdin; else    if (strcmp(file_list, "-") == 0) fl = stdin; else
2768      {      {
2769      fl = fopen(file_list, "rb");      fl = fopen(file_list, "rb");
2770      if (fl == NULL)      if (fl == NULL)
2771        {        {
2772        fprintf(stderr, "pcregrep: Failed to open %s: %s\n", file_list,        fprintf(stderr, "pcregrep: Failed to open %s: %s\n", file_list,
2773          strerror(errno));          strerror(errno));
2774        goto EXIT2;        goto EXIT2;
2775        }        }
2776      }      }
2777    while (fgets(buffer, PATBUFSIZE, fl) != NULL)    while (fgets(buffer, PATBUFSIZE, fl) != NULL)
2778      {      {
2779      int frc;      int frc;
2780      char *end = buffer + (int)strlen(buffer);      char *end = buffer + (int)strlen(buffer);
2781      while (end > buffer && isspace(end[-1])) end--;      while (end > buffer && isspace(end[-1])) end--;
2782      *end = 0;      *end = 0;
2783      if (*buffer != 0)      if (*buffer != 0)
2784        {        {
2785        frc = grep_or_recurse(buffer, dee_action == dee_RECURSE, FALSE);        frc = grep_or_recurse(buffer, dee_action == dee_RECURSE, FALSE);
2786        if (frc > 1) rc = frc;        if (frc > 1) rc = frc;
2787          else if (frc == 0 && rc == 1) rc = 0;          else if (frc == 0 && rc == 1) rc = 0;
2788        }        }
2789      }      }
2790    if (fl != stdin) fclose (fl);    if (fl != stdin) fclose (fl);
2791    }    }
2792    
2793  /* Do this only if there was no file list (and no file arguments). */  /* Do this only if there was no file list (and no file arguments). */
2794    
# Line 2804  them as files or directories. Pass in th Line 2804  them as files or directories. Pass in th
2804  at top level - this suppresses the file name if the argument is not a directory  at top level - this suppresses the file name if the argument is not a directory
2805  and filenames are not otherwise forced. */  and filenames are not otherwise forced. */
2806    
2807  only_one_at_top = i == argc - 1 && file_list == NULL;  only_one_at_top = i == argc - 1 && file_list == NULL;
2808    
2809  for (; i < argc; i++)  for (; i < argc; i++)
2810    {    {

Legend:
Removed from v.971  
changed lines
  Added in v.975

  ViewVC Help
Powered by ViewVC 1.1.5