/[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 117 by ph10, Fri Mar 9 15:59:06 2007 UTC revision 142 by ph10, Fri Mar 30 15:55:18 2007 UTC
# Line 50  POSSIBILITY OF SUCH DAMAGE. Line 50  POSSIBILITY OF SUCH DAMAGE.
50    
51  #include <sys/types.h>  #include <sys/types.h>
52  #include <sys/stat.h>  #include <sys/stat.h>
53  #include <unistd.h>  #ifdef HAVE_UNISTD_H
54    #  include <unistd.h>
55    #endif
56    
57  #include "pcre.h"  #include <pcre.h>
58    
59  #define FALSE 0  #define FALSE 0
60  #define TRUE 1  #define TRUE 1
# Line 119  static char *locale = NULL; Line 121  static char *locale = NULL;
121  static const unsigned char *pcretables = NULL;  static const unsigned char *pcretables = NULL;
122    
123  static int  pattern_count = 0;  static int  pattern_count = 0;
124  static pcre **pattern_list;  static pcre **pattern_list = NULL;
125  static pcre_extra **hints_list;  static pcre_extra **hints_list = NULL;
126    
127  static char *include_pattern = NULL;  static char *include_pattern = NULL;
128  static char *exclude_pattern = NULL;  static char *exclude_pattern = NULL;
# Line 463  return FALSE; Line 465  return FALSE;
465    
466    
467    
468  #if ! HAVE_STRERROR  #ifndef HAVE_STRERROR
469  /*************************************************  /*************************************************
470  *     Provide strerror() for non-ANSI libraries  *  *     Provide strerror() for non-ANSI libraries  *
471  *************************************************/  *************************************************/
# Line 1406  sprintf(buffer, "%s%.*s%s", prefix[proce Line 1408  sprintf(buffer, "%s%.*s%s", prefix[proce
1408    suffix[process_options]);    suffix[process_options]);
1409  pattern_list[pattern_count] =  pattern_list[pattern_count] =
1410    pcre_compile(buffer, options, &error, &errptr, pcretables);    pcre_compile(buffer, options, &error, &errptr, pcretables);
1411  if (pattern_list[pattern_count++] != NULL) return TRUE;  if (pattern_list[pattern_count] != NULL)
1412      {
1413      pattern_count++;
1414      return TRUE;
1415      }
1416    
1417  /* Handle compile errors */  /* Handle compile errors */
1418    
# Line 1488  int i, j; Line 1494  int i, j;
1494  int rc = 1;  int rc = 1;
1495  int pcre_options = 0;  int pcre_options = 0;
1496  int cmd_pattern_count = 0;  int cmd_pattern_count = 0;
1497    int hint_count = 0;
1498  int errptr;  int errptr;
1499  BOOL only_one_at_top;  BOOL only_one_at_top;
1500  char *patterns[MAX_PATTERN_COUNT];  char *patterns[MAX_PATTERN_COUNT];
# Line 1866  hints_list = (pcre_extra **)malloc(MAX_P Line 1873  hints_list = (pcre_extra **)malloc(MAX_P
1873  if (pattern_list == NULL || hints_list == NULL)  if (pattern_list == NULL || hints_list == NULL)
1874    {    {
1875    fprintf(stderr, "pcregrep: malloc failed\n");    fprintf(stderr, "pcregrep: malloc failed\n");
1876    return 2;    goto EXIT2;
1877    }    }
1878    
1879  /* If no patterns were provided by -e, and there is no file provided by -f,  /* If no patterns were provided by -e, and there is no file provided by -f,
# Line 1885  for (j = 0; j < cmd_pattern_count; j++) Line 1892  for (j = 0; j < cmd_pattern_count; j++)
1892    {    {
1893    if (!compile_pattern(patterns[j], pcre_options, NULL,    if (!compile_pattern(patterns[j], pcre_options, NULL,
1894         (j == 0 && cmd_pattern_count == 1)? 0 : j + 1))         (j == 0 && cmd_pattern_count == 1)? 0 : j + 1))
1895      return 2;      goto EXIT2;
1896    }    }
1897    
1898  /* Compile the regular expressions that are provided in a file. */  /* Compile the regular expressions that are provided in a file. */
# Line 1909  if (pattern_filename != NULL) Line 1916  if (pattern_filename != NULL)
1916        {        {
1917        fprintf(stderr, "pcregrep: Failed to open %s: %s\n", pattern_filename,        fprintf(stderr, "pcregrep: Failed to open %s: %s\n", pattern_filename,
1918          strerror(errno));          strerror(errno));
1919        return 2;        goto EXIT2;
1920        }        }
1921      filename = pattern_filename;      filename = pattern_filename;
1922      }      }
# Line 1922  if (pattern_filename != NULL) Line 1929  if (pattern_filename != NULL)
1929      linenumber++;      linenumber++;
1930      if (buffer[0] == 0) continue;   /* Skip blank lines */      if (buffer[0] == 0) continue;   /* Skip blank lines */
1931      if (!compile_pattern(buffer, pcre_options, filename, linenumber))      if (!compile_pattern(buffer, pcre_options, filename, linenumber))
1932        return 2;        goto EXIT2;
1933      }      }
1934    
1935    if (f != stdin) fclose(f);    if (f != stdin) fclose(f);
# Line 1938  for (j = 0; j < pattern_count; j++) Line 1945  for (j = 0; j < pattern_count; j++)
1945      char s[16];      char s[16];
1946      if (pattern_count == 1) s[0] = 0; else sprintf(s, " number %d", j);      if (pattern_count == 1) s[0] = 0; else sprintf(s, " number %d", j);
1947      fprintf(stderr, "pcregrep: Error while studying regex%s: %s\n", s, error);      fprintf(stderr, "pcregrep: Error while studying regex%s: %s\n", s, error);
1948      return 2;      goto EXIT2;
1949      }      }
1950      hint_count++;
1951    }    }
1952    
1953  /* If there are include or exclude patterns, compile them. */  /* If there are include or exclude patterns, compile them. */
# Line 1952  if (exclude_pattern != NULL) Line 1960  if (exclude_pattern != NULL)
1960      {      {
1961      fprintf(stderr, "pcregrep: Error in 'exclude' regex at offset %d: %s\n",      fprintf(stderr, "pcregrep: Error in 'exclude' regex at offset %d: %s\n",
1962        errptr, error);        errptr, error);
1963      return 2;      goto EXIT2;
1964      }      }
1965    }    }
1966    
# Line 1964  if (include_pattern != NULL) Line 1972  if (include_pattern != NULL)
1972      {      {
1973      fprintf(stderr, "pcregrep: Error in 'include' regex at offset %d: %s\n",      fprintf(stderr, "pcregrep: Error in 'include' regex at offset %d: %s\n",
1974        errptr, error);        errptr, error);
1975      return 2;      goto EXIT2;
1976      }      }
1977    }    }
1978    
1979  /* If there are no further arguments, do the business on stdin and exit. */  /* If there are no further arguments, do the business on stdin and exit. */
1980    
1981  if (i >= argc)  if (i >= argc)
1982    return pcregrep(stdin, (filenames > FN_DEFAULT)? stdin_name : NULL);    {
1983      rc = pcregrep(stdin, (filenames > FN_DEFAULT)? stdin_name : NULL);
1984      goto EXIT;
1985      }
1986    
1987  /* Otherwise, work through the remaining arguments as files or directories.  /* Otherwise, work through the remaining arguments as files or directories.
1988  Pass in the fact that there is only one argument at top level - this suppresses  Pass in the fact that there is only one argument at top level - this suppresses
# Line 1988  for (; i < argc; i++) Line 1999  for (; i < argc; i++)
1999      else if (frc == 0 && rc == 1) rc = 0;      else if (frc == 0 && rc == 1) rc = 0;
2000    }    }
2001    
2002    EXIT:
2003    if (pattern_list != NULL)
2004      {
2005      for (i = 0; i < pattern_count; i++) free(pattern_list[i]);
2006      free(pattern_list);
2007      }
2008    if (hints_list != NULL)
2009      {
2010      for (i = 0; i < hint_count; i++) free(hints_list[i]);
2011      free(hints_list);
2012      }
2013  return rc;  return rc;
2014    
2015    EXIT2:
2016    rc = 2;
2017    goto EXIT;
2018  }  }
2019    
2020  /* End of pcregrep */  /* End of pcregrep */

Legend:
Removed from v.117  
changed lines
  Added in v.142

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12