/[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 137 by ph10, Thu Mar 29 13:56:00 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 1866  hints_list = (pcre_extra **)malloc(MAX_P Line 1868  hints_list = (pcre_extra **)malloc(MAX_P
1868  if (pattern_list == NULL || hints_list == NULL)  if (pattern_list == NULL || hints_list == NULL)
1869    {    {
1870    fprintf(stderr, "pcregrep: malloc failed\n");    fprintf(stderr, "pcregrep: malloc failed\n");
1871    return 2;    goto EXIT2;
1872    }    }
1873    
1874  /* 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 1887  for (j = 0; j < cmd_pattern_count; j++)
1887    {    {
1888    if (!compile_pattern(patterns[j], pcre_options, NULL,    if (!compile_pattern(patterns[j], pcre_options, NULL,
1889         (j == 0 && cmd_pattern_count == 1)? 0 : j + 1))         (j == 0 && cmd_pattern_count == 1)? 0 : j + 1))
1890      return 2;      goto EXIT2;
1891    }    }
1892    
1893  /* 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 1911  if (pattern_filename != NULL)
1911        {        {
1912        fprintf(stderr, "pcregrep: Failed to open %s: %s\n", pattern_filename,        fprintf(stderr, "pcregrep: Failed to open %s: %s\n", pattern_filename,
1913          strerror(errno));          strerror(errno));
1914        return 2;        goto EXIT2;
1915        }        }
1916      filename = pattern_filename;      filename = pattern_filename;
1917      }      }
# Line 1922  if (pattern_filename != NULL) Line 1924  if (pattern_filename != NULL)
1924      linenumber++;      linenumber++;
1925      if (buffer[0] == 0) continue;   /* Skip blank lines */      if (buffer[0] == 0) continue;   /* Skip blank lines */
1926      if (!compile_pattern(buffer, pcre_options, filename, linenumber))      if (!compile_pattern(buffer, pcre_options, filename, linenumber))
1927        return 2;        goto EXIT2;
1928      }      }
1929    
1930    if (f != stdin) fclose(f);    if (f != stdin) fclose(f);
# Line 1938  for (j = 0; j < pattern_count; j++) Line 1940  for (j = 0; j < pattern_count; j++)
1940      char s[16];      char s[16];
1941      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);
1942      fprintf(stderr, "pcregrep: Error while studying regex%s: %s\n", s, error);      fprintf(stderr, "pcregrep: Error while studying regex%s: %s\n", s, error);
1943      return 2;      goto EXIT2;
1944      }      }
1945    }    }
1946    
# Line 1952  if (exclude_pattern != NULL) Line 1954  if (exclude_pattern != NULL)
1954      {      {
1955      fprintf(stderr, "pcregrep: Error in 'exclude' regex at offset %d: %s\n",      fprintf(stderr, "pcregrep: Error in 'exclude' regex at offset %d: %s\n",
1956        errptr, error);        errptr, error);
1957      return 2;      goto EXIT2;
1958      }      }
1959    }    }
1960    
# Line 1964  if (include_pattern != NULL) Line 1966  if (include_pattern != NULL)
1966      {      {
1967      fprintf(stderr, "pcregrep: Error in 'include' regex at offset %d: %s\n",      fprintf(stderr, "pcregrep: Error in 'include' regex at offset %d: %s\n",
1968        errptr, error);        errptr, error);
1969      return 2;      goto EXIT2;
1970      }      }
1971    }    }
1972    
1973  /* 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. */
1974    
1975  if (i >= argc)  if (i >= argc)
1976    return pcregrep(stdin, (filenames > FN_DEFAULT)? stdin_name : NULL);    {
1977      rc = pcregrep(stdin, (filenames > FN_DEFAULT)? stdin_name : NULL);
1978      goto EXIT;
1979      }
1980    
1981  /* Otherwise, work through the remaining arguments as files or directories.  /* Otherwise, work through the remaining arguments as files or directories.
1982  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 1993  for (; i < argc; i++)
1993      else if (frc == 0 && rc == 1) rc = 0;      else if (frc == 0 && rc == 1) rc = 0;
1994    }    }
1995    
1996    EXIT:
1997    if (pattern_list != NULL)
1998      {
1999      for (i = 0; i < pattern_count; i++) free(pattern_list[i]);
2000      free(pattern_list);
2001      }
2002    if (hints_list != NULL)
2003      {
2004      for (i = 0; i < pattern_count; i++) free(hints_list[i]);
2005      free(hints_list);
2006      }
2007  return rc;  return rc;
2008    
2009    EXIT2:
2010    rc = 2;
2011    goto EXIT;
2012  }  }
2013    
2014  /* End of pcregrep */  /* End of pcregrep */

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

  ViewVC Help
Powered by ViewVC 1.1.5