/[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 93 by nigel, Sat Feb 24 21:41:42 2007 UTC revision 121 by ph10, Mon Mar 12 12:12:47 2007 UTC
# Line 6  Line 6 
6  its pattern matching. On a Unix or Win32 system it can recurse into  its pattern matching. On a Unix or Win32 system it can recurse into
7  directories.  directories.
8    
9             Copyright (c) 1997-2006 University of Cambridge             Copyright (c) 1997-2007 University of Cambridge
10    
11  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
12  Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
# Line 37  POSSIBILITY OF SUCH DAMAGE. Line 37  POSSIBILITY OF SUCH DAMAGE.
37  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
38  */  */
39    
40    #ifdef HAVE_CONFIG_H
41    #  include <config.h>
42    #endif
43    
44  #include <ctype.h>  #include <ctype.h>
45  #include <locale.h>  #include <locale.h>
46  #include <stdio.h>  #include <stdio.h>
# Line 48  POSSIBILITY OF SUCH DAMAGE. Line 52  POSSIBILITY OF SUCH DAMAGE.
52  #include <sys/stat.h>  #include <sys/stat.h>
53  #include <unistd.h>  #include <unistd.h>
54    
 #include "config.h"  
55  #include "pcre.h"  #include "pcre.h"
56    
57  #define FALSE 0  #define FALSE 0
# Line 56  POSSIBILITY OF SUCH DAMAGE. Line 59  POSSIBILITY OF SUCH DAMAGE.
59    
60  typedef int BOOL;  typedef int BOOL;
61    
 #define VERSION "4.4 29-Nov-2006"  
62  #define MAX_PATTERN_COUNT 100  #define MAX_PATTERN_COUNT 100
63    
64  #if BUFSIZ > 8192  #if BUFSIZ > 8192
# Line 117  static char *locale = NULL; Line 119  static char *locale = NULL;
119  static const unsigned char *pcretables = NULL;  static const unsigned char *pcretables = NULL;
120    
121  static int  pattern_count = 0;  static int  pattern_count = 0;
122  static pcre **pattern_list;  static pcre **pattern_list = NULL;
123  static pcre_extra **hints_list;  static pcre_extra **hints_list = NULL;
124    
125  static char *include_pattern = NULL;  static char *include_pattern = NULL;
126  static char *exclude_pattern = NULL;  static char *exclude_pattern = NULL;
# Line 244  although at present the only ones are fo Line 246  although at present the only ones are fo
246    
247  /************* Directory scanning in Unix ***********/  /************* Directory scanning in Unix ***********/
248    
249  #if IS_UNIX  #if defined HAVE_SYS_STAT_H && defined HAVE_DIRENT_H && defined HAVE_SYS_TYPES_H
250  #include <sys/types.h>  #include <sys/types.h>
251  #include <sys/stat.h>  #include <sys/stat.h>
252  #include <dirent.h>  #include <dirent.h>
# Line 314  Lionel Fourquaux. David Burgess added a Line 316  Lionel Fourquaux. David Burgess added a
316  when it did not exist. */  when it did not exist. */
317    
318    
319  #elif HAVE_WIN32API  #elif HAVE_WINDOWS_H
320    
321  #ifndef STRICT  #ifndef STRICT
322  # define STRICT  # define STRICT
# Line 436  FALSE; Line 438  FALSE;
438  typedef void directory_type;  typedef void directory_type;
439    
440  int isdirectory(char *filename) { return 0; }  int isdirectory(char *filename) { return 0; }
441  directory_type * opendirectory(char *filename) {}  directory_type * opendirectory(char *filename) { return (directory_type*)0;}
442  char *readdirectory(directory_type *dir) {}  char *readdirectory(directory_type *dir) { return (char*)0;}
443  void closedirectory(directory_type *dir) {}  void closedirectory(directory_type *dir) {}
444    
445    
# Line 1328  switch(letter) Line 1330  switch(letter)
1330    case 'x': process_options |= PO_LINE_MATCH; break;    case 'x': process_options |= PO_LINE_MATCH; break;
1331    
1332    case 'V':    case 'V':
1333    fprintf(stderr, "pcregrep version %s using ", VERSION);    fprintf(stderr, "pcregrep version %s\n", pcre_version());
   fprintf(stderr, "PCRE version %s\n", pcre_version());  
1334    exit(0);    exit(0);
1335    break;    break;
1336    
# Line 1865  hints_list = (pcre_extra **)malloc(MAX_P Line 1866  hints_list = (pcre_extra **)malloc(MAX_P
1866  if (pattern_list == NULL || hints_list == NULL)  if (pattern_list == NULL || hints_list == NULL)
1867    {    {
1868    fprintf(stderr, "pcregrep: malloc failed\n");    fprintf(stderr, "pcregrep: malloc failed\n");
1869    return 2;    goto EXIT2;
1870    }    }
1871    
1872  /* 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 1884  for (j = 0; j < cmd_pattern_count; j++) Line 1885  for (j = 0; j < cmd_pattern_count; j++)
1885    {    {
1886    if (!compile_pattern(patterns[j], pcre_options, NULL,    if (!compile_pattern(patterns[j], pcre_options, NULL,
1887         (j == 0 && cmd_pattern_count == 1)? 0 : j + 1))         (j == 0 && cmd_pattern_count == 1)? 0 : j + 1))
1888      return 2;      goto EXIT2;
1889    }    }
1890    
1891  /* Compile the regular expressions that are provided in a file. */  /* Compile the regular expressions that are provided in a file. */
# Line 1908  if (pattern_filename != NULL) Line 1909  if (pattern_filename != NULL)
1909        {        {
1910        fprintf(stderr, "pcregrep: Failed to open %s: %s\n", pattern_filename,        fprintf(stderr, "pcregrep: Failed to open %s: %s\n", pattern_filename,
1911          strerror(errno));          strerror(errno));
1912        return 2;        goto EXIT2;
1913        }        }
1914      filename = pattern_filename;      filename = pattern_filename;
1915      }      }
# Line 1921  if (pattern_filename != NULL) Line 1922  if (pattern_filename != NULL)
1922      linenumber++;      linenumber++;
1923      if (buffer[0] == 0) continue;   /* Skip blank lines */      if (buffer[0] == 0) continue;   /* Skip blank lines */
1924      if (!compile_pattern(buffer, pcre_options, filename, linenumber))      if (!compile_pattern(buffer, pcre_options, filename, linenumber))
1925        return 2;        goto EXIT2;
1926      }      }
1927    
1928    if (f != stdin) fclose(f);    if (f != stdin) fclose(f);
# Line 1937  for (j = 0; j < pattern_count; j++) Line 1938  for (j = 0; j < pattern_count; j++)
1938      char s[16];      char s[16];
1939      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);
1940      fprintf(stderr, "pcregrep: Error while studying regex%s: %s\n", s, error);      fprintf(stderr, "pcregrep: Error while studying regex%s: %s\n", s, error);
1941      return 2;      goto EXIT2;
1942      }      }
1943    }    }
1944    
# Line 1951  if (exclude_pattern != NULL) Line 1952  if (exclude_pattern != NULL)
1952      {      {
1953      fprintf(stderr, "pcregrep: Error in 'exclude' regex at offset %d: %s\n",      fprintf(stderr, "pcregrep: Error in 'exclude' regex at offset %d: %s\n",
1954        errptr, error);        errptr, error);
1955      return 2;      goto EXIT2;
1956      }      }
1957    }    }
1958    
# Line 1963  if (include_pattern != NULL) Line 1964  if (include_pattern != NULL)
1964      {      {
1965      fprintf(stderr, "pcregrep: Error in 'include' regex at offset %d: %s\n",      fprintf(stderr, "pcregrep: Error in 'include' regex at offset %d: %s\n",
1966        errptr, error);        errptr, error);
1967      return 2;      goto EXIT2;
1968      }      }
1969    }    }
1970    
1971  /* 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. */
1972    
1973  if (i >= argc)  if (i >= argc)
1974    return pcregrep(stdin, (filenames > FN_DEFAULT)? stdin_name : NULL);    {
1975      rc = pcregrep(stdin, (filenames > FN_DEFAULT)? stdin_name : NULL);
1976      goto EXIT;
1977      }
1978    
1979  /* Otherwise, work through the remaining arguments as files or directories.  /* Otherwise, work through the remaining arguments as files or directories.
1980  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 1987  for (; i < argc; i++) Line 1991  for (; i < argc; i++)
1991      else if (frc == 0 && rc == 1) rc = 0;      else if (frc == 0 && rc == 1) rc = 0;
1992    }    }
1993    
1994    EXIT:
1995    if (pattern_list != NULL)
1996      {
1997      for (i = 0; i < pattern_count; i++) free(pattern_list[i]);
1998      free(pattern_list);
1999      }
2000    if (hints_list != NULL)
2001      {
2002      for (i = 0; i < pattern_count; i++) free(hints_list[i]);
2003      free(hints_list);
2004      }
2005  return rc;  return rc;
2006    
2007    EXIT2:
2008    rc = 2;
2009    goto EXIT;
2010  }  }
2011    
2012  /* End of pcregrep */  /* End of pcregrep */

Legend:
Removed from v.93  
changed lines
  Added in v.121

  ViewVC Help
Powered by ViewVC 1.1.5