/[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 286 by ph10, Mon Dec 17 14:46:11 2007 UTC revision 345 by ph10, Mon Apr 28 15:10:02 2008 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-2007 University of Cambridge             Copyright (c) 1997-2008 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 139  static pcre_extra **hints_list = NULL; Line 139  static pcre_extra **hints_list = NULL;
139    
140  static char *include_pattern = NULL;  static char *include_pattern = NULL;
141  static char *exclude_pattern = NULL;  static char *exclude_pattern = NULL;
142    static char *include_dir_pattern = NULL;
143    static char *exclude_dir_pattern = NULL;
144    
145  static pcre *include_compiled = NULL;  static pcre *include_compiled = NULL;
146  static pcre *exclude_compiled = NULL;  static pcre *exclude_compiled = NULL;
147    static pcre *include_dir_compiled = NULL;
148    static pcre *exclude_dir_compiled = NULL;
149    
150  static int after_context = 0;  static int after_context = 0;
151  static int before_context = 0;  static int before_context = 0;
# Line 181  typedef struct option_item { Line 185  typedef struct option_item {
185  /* Options without a single-letter equivalent get a negative value. This can be  /* Options without a single-letter equivalent get a negative value. This can be
186  used to identify them. */  used to identify them. */
187    
188  #define N_COLOUR    (-1)  #define N_COLOUR       (-1)
189  #define N_EXCLUDE   (-2)  #define N_EXCLUDE      (-2)
190  #define N_HELP      (-3)  #define N_EXCLUDE_DIR  (-3)
191  #define N_INCLUDE   (-4)  #define N_HELP         (-4)
192  #define N_LABEL     (-5)  #define N_INCLUDE      (-5)
193  #define N_LOCALE    (-6)  #define N_INCLUDE_DIR  (-6)
194  #define N_NULL      (-7)  #define N_LABEL        (-7)
195  #define N_LOFFSETS  (-8)  #define N_LOCALE       (-8)
196  #define N_FOFFSETS  (-9)  #define N_NULL         (-9)
197    #define N_LOFFSETS     (-10)
198    #define N_FOFFSETS     (-11)
199    
200  static option_item optionlist[] = {  static option_item optionlist[] = {
201    { OP_NODATA,    N_NULL,   NULL,              "",              "  terminate options" },    { OP_NODATA,    N_NULL,   NULL,              "",              "  terminate options" },
# Line 222  static option_item optionlist[] = { Line 228  static option_item optionlist[] = {
228    { OP_NODATA,    'r',      NULL,              "recursive",     "recursively scan sub-directories" },    { OP_NODATA,    'r',      NULL,              "recursive",     "recursively scan sub-directories" },
229    { OP_STRING,    N_EXCLUDE,&exclude_pattern,  "exclude=pattern","exclude matching files when recursing" },    { OP_STRING,    N_EXCLUDE,&exclude_pattern,  "exclude=pattern","exclude matching files when recursing" },
230    { OP_STRING,    N_INCLUDE,&include_pattern,  "include=pattern","include matching files when recursing" },    { OP_STRING,    N_INCLUDE,&include_pattern,  "include=pattern","include matching files when recursing" },
231      { OP_STRING,    N_EXCLUDE_DIR,&exclude_dir_pattern, "exclude_dir=pattern","exclude matching directories when recursing" },
232      { OP_STRING,    N_INCLUDE_DIR,&include_dir_pattern, "include_dir=pattern","include matching directories when recursing" },
233  #ifdef JFRIEDL_DEBUG  #ifdef JFRIEDL_DEBUG
234    { OP_OP_NUMBER, 'S',      &S_arg,            "jeffS",         "replace matched (sub)string with X" },    { OP_OP_NUMBER, 'S',      &S_arg,            "jeffS",         "replace matched (sub)string with X" },
235  #endif  #endif
# Line 1361  if (strcmp(pathname, "-") == 0) Line 1369  if (strcmp(pathname, "-") == 0)
1369    }    }
1370    
1371  /* If the file is a directory, skip if skipping or if we are recursing, scan  /* If the file is a directory, skip if skipping or if we are recursing, scan
1372  each file within it, subject to any include or exclude patterns that were set.  each file and directory within it, subject to any include or exclude patterns
1373  The scanning code is localized so it can be made system-specific. */  that were set. The scanning code is localized so it can be made
1374    system-specific. */
1375    
1376  if ((sep = isdirectory(pathname)) != 0)  if ((sep = isdirectory(pathname)) != 0)
1377    {    {
# Line 1383  if ((sep = isdirectory(pathname)) != 0) Line 1392  if ((sep = isdirectory(pathname)) != 0)
1392    
1393      while ((nextfile = readdirectory(dir)) != NULL)      while ((nextfile = readdirectory(dir)) != NULL)
1394        {        {
1395        int frc, blen;        int frc, nflen;
1396        sprintf(buffer, "%.512s%c%.128s", pathname, sep, nextfile);        sprintf(buffer, "%.512s%c%.128s", pathname, sep, nextfile);
1397        blen = strlen(buffer);        nflen = strlen(nextfile);
1398    
1399        if (exclude_compiled != NULL &&        if (isdirectory(buffer))
1400            pcre_exec(exclude_compiled, NULL, buffer, blen, 0, 0, NULL, 0) >= 0)          {
1401          continue;          if (exclude_dir_compiled != NULL &&
1402                pcre_exec(exclude_dir_compiled, NULL, nextfile, nflen, 0, 0, NULL, 0) >= 0)
1403        if (include_compiled != NULL &&            continue;
1404            pcre_exec(include_compiled, NULL, buffer, blen, 0, 0, NULL, 0) < 0)  
1405          continue;          if (include_dir_compiled != NULL &&
1406                pcre_exec(include_dir_compiled, NULL, nextfile, nflen, 0, 0, NULL, 0) < 0)
1407              continue;
1408            }
1409          else
1410            {
1411            if (exclude_compiled != NULL &&
1412                pcre_exec(exclude_compiled, NULL, nextfile, nflen, 0, 0, NULL, 0) >= 0)
1413              continue;
1414    
1415            if (include_compiled != NULL &&
1416                pcre_exec(include_compiled, NULL, nextfile, nflen, 0, 0, NULL, 0) < 0)
1417              continue;
1418            }
1419    
1420        frc = grep_or_recurse(buffer, dir_recurse, FALSE);        frc = grep_or_recurse(buffer, dir_recurse, FALSE);
1421        if (frc > 1) rc = frc;        if (frc > 1) rc = frc;
# Line 1579  for (op = optionlist; op->one_char != 0; Line 1601  for (op = optionlist; op->one_char != 0;
1601    int n;    int n;
1602    char s[4];    char s[4];
1603    if (op->one_char > 0) sprintf(s, "-%c,", op->one_char); else strcpy(s, "   ");    if (op->one_char > 0) sprintf(s, "-%c,", op->one_char); else strcpy(s, "   ");
1604    printf("  %s --%s%n", s, op->long_name, &n);    n = 30 - printf("  %s --%s", s, op->long_name);
   n = 30 - n;  
1605    if (n < 1) n = 1;    if (n < 1) n = 1;
1606    printf("%.*s%s\n", n, "                    ", op->help_text);    printf("%.*s%s\n", n, "                    ", op->help_text);
1607    }    }
# Line 2288  if (include_pattern != NULL) Line 2309  if (include_pattern != NULL)
2309        errptr, error);        errptr, error);
2310      goto EXIT2;      goto EXIT2;
2311      }      }
2312      }
2313    
2314    if (exclude_dir_pattern != NULL)
2315      {
2316      exclude_dir_compiled = pcre_compile(exclude_dir_pattern, 0, &error, &errptr,
2317        pcretables);
2318      if (exclude_dir_compiled == NULL)
2319        {
2320        fprintf(stderr, "pcregrep: Error in 'exclude_dir' regex at offset %d: %s\n",
2321          errptr, error);
2322        goto EXIT2;
2323        }
2324      }
2325    
2326    if (include_dir_pattern != NULL)
2327      {
2328      include_dir_compiled = pcre_compile(include_dir_pattern, 0, &error, &errptr,
2329        pcretables);
2330      if (include_dir_compiled == NULL)
2331        {
2332        fprintf(stderr, "pcregrep: Error in 'include_dir' regex at offset %d: %s\n",
2333          errptr, error);
2334        goto EXIT2;
2335        }
2336    }    }
2337    
2338  /* 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. */

Legend:
Removed from v.286  
changed lines
  Added in v.345

  ViewVC Help
Powered by ViewVC 1.1.5