/[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 378 by ph10, Sun Mar 1 14:13:34 2009 UTC revision 392 by ph10, Tue Mar 17 21:30:30 2009 UTC
# Line 825  if (after_context > 0 && lastmatchnumber Line 825  if (after_context > 0 && lastmatchnumber
825  *   Apply patterns to subject till one matches   *  *   Apply patterns to subject till one matches   *
826  *************************************************/  *************************************************/
827    
828  /* This function is called to run through all patterns, looking for a match. It  /* This function is called to run through all patterns, looking for a match. It
829  is used multiple times for the same subject when colouring is enabled, in order  is used multiple times for the same subject when colouring is enabled, in order
830  to find all possible matches.  to find all possible matches.
831    
832  Arguments:  Arguments:
# Line 834  Arguments: Line 834  Arguments:
834    length      the length of the subject to match    length      the length of the subject to match
835    offsets     the offets vector to fill in    offsets     the offets vector to fill in
836    mrc         address of where to put the result of pcre_exec()    mrc         address of where to put the result of pcre_exec()
837    
838  Returns:      TRUE if there was a match  Returns:      TRUE if there was a match
839                FALSE if there was no match                FALSE if there was no match
840                invert if there was a non-fatal error                invert if there was a non-fatal error
841  */  */
842    
843  static BOOL  static BOOL
844  match_patterns(char *matchptr, size_t length, int *offsets, int *mrc)  match_patterns(char *matchptr, size_t length, int *offsets, int *mrc)
# Line 846  match_patterns(char *matchptr, size_t le Line 846  match_patterns(char *matchptr, size_t le
846  int i;  int i;
847  for (i = 0; i < pattern_count; i++)  for (i = 0; i < pattern_count; i++)
848    {    {
849    *mrc = pcre_exec(pattern_list[i], hints_list[i], matchptr, length, 0, 0,    *mrc = pcre_exec(pattern_list[i], hints_list[i], matchptr, length, 0,
850      offsets, OFFSET_SIZE);      PCRE_NOTEMPTY, offsets, OFFSET_SIZE);
851    if (*mrc >= 0) return TRUE;    if (*mrc >= 0) return TRUE;
852    if (*mrc == PCRE_ERROR_NOMATCH) continue;    if (*mrc == PCRE_ERROR_NOMATCH) continue;
853    fprintf(stderr, "pcregrep: pcre_exec() error %d while matching ", *mrc);    fprintf(stderr, "pcregrep: pcre_exec() error %d while matching ", *mrc);
# Line 992  while (ptr < endptr) Line 992  while (ptr < endptr)
992        #include <time.h>        #include <time.h>
993        struct timeval start_time, end_time;        struct timeval start_time, end_time;
994        struct timezone dummy;        struct timezone dummy;
995        int i;        int i;
996    
997        if (jfriedl_XT)        if (jfriedl_XT)
998        {        {
# Line 1018  while (ptr < endptr) Line 1018  while (ptr < endptr)
1018    
1019    
1020        for (i = 0; i < jfriedl_XR; i++)        for (i = 0; i < jfriedl_XR; i++)
1021            match = (pcre_exec(pattern_list[0], hints_list[0], ptr, length, 0, 0, offsets, OFFSET_SIZE) >= 0);            match = (pcre_exec(pattern_list[0], hints_list[0], ptr, length, 0,
1022                  PCRE_NOTEMPTY, offsets, OFFSET_SIZE) >= 0);
1023    
1024        if (gettimeofday(&end_time, &dummy) != 0)        if (gettimeofday(&end_time, &dummy) != 0)
1025                perror("bad gettimeofday");                perror("bad gettimeofday");
# Line 1037  while (ptr < endptr) Line 1038  while (ptr < endptr)
1038    
1039    ONLY_MATCHING_RESTART:    ONLY_MATCHING_RESTART:
1040    
1041    /* Run through all the patterns until one matches or there is an error other    /* Run through all the patterns until one matches or there is an error other
1042    than NOMATCH. This code is in a subroutine so that it can be re-used for    than NOMATCH. This code is in a subroutine so that it can be re-used for
1043    finding subsequent matches when colouring matched lines. */    finding subsequent matches when colouring matched lines. */
1044    
1045    match = match_patterns(matchptr, length, offsets, &mrc);    match = match_patterns(matchptr, length, offsets, &mrc);
1046    
1047    /* If it's a match or a not-match (as required), do what's wanted. */    /* If it's a match or a not-match (as required), do what's wanted. */
# Line 1095  while (ptr < endptr) Line 1096  while (ptr < endptr)
1096            if (do_colour) fprintf(stdout, "%c[%sm", 0x1b, colour_string);            if (do_colour) fprintf(stdout, "%c[%sm", 0x1b, colour_string);
1097            fwrite(matchptr + offsets[0], 1, offsets[1] - offsets[0], stdout);            fwrite(matchptr + offsets[0], 1, offsets[1] - offsets[0], stdout);
1098            if (do_colour) fprintf(stdout, "%c[00m", 0x1b);            if (do_colour) fprintf(stdout, "%c[00m", 0x1b);
1099            }            }
1100          fprintf(stdout, "\n");          fprintf(stdout, "\n");
1101          matchptr += offsets[1];          matchptr += offsets[1];
1102          length -= offsets[1];          length -= offsets[1];
# Line 1232  while (ptr < endptr) Line 1233  while (ptr < endptr)
1233        else        else
1234  #endif  #endif
1235    
1236        /* We have to split the line(s) up if colouring, and search for further        /* We have to split the line(s) up if colouring, and search for further
1237        matches. */        matches. */
1238    
1239        if (do_colour)        if (do_colour)
1240          {          {
1241          int last_offset = 0;          int last_offset = 0;
1242          fwrite(ptr, 1, offsets[0], stdout);          fwrite(ptr, 1, offsets[0], stdout);
1243          fprintf(stdout, "%c[%sm", 0x1b, colour_string);          fprintf(stdout, "%c[%sm", 0x1b, colour_string);
1244          fwrite(ptr + offsets[0], 1, offsets[1] - offsets[0], stdout);          fwrite(ptr + offsets[0], 1, offsets[1] - offsets[0], stdout);
1245          fprintf(stdout, "%c[00m", 0x1b);          fprintf(stdout, "%c[00m", 0x1b);
1246          for (;;)          for (;;)
1247            {            {
1248            last_offset += offsets[1];            last_offset += offsets[1];
1249            matchptr += offsets[1];            matchptr += offsets[1];
1250            length -= offsets[1];            length -= offsets[1];
1251            if (!match_patterns(matchptr, length, offsets, &mrc)) break;            if (!match_patterns(matchptr, length, offsets, &mrc)) break;
# Line 1256  while (ptr < endptr) Line 1257  while (ptr < endptr)
1257          fwrite(ptr + last_offset, 1, (linelength + endlinelength) - last_offset,          fwrite(ptr + last_offset, 1, (linelength + endlinelength) - last_offset,
1258            stdout);            stdout);
1259          }          }
1260    
1261        /* Not colouring; no need to search for further matches */        /* Not colouring; no need to search for further matches */
1262    
1263        else fwrite(ptr, 1, linelength + endlinelength, stdout);        else fwrite(ptr, 1, linelength + endlinelength, stdout);
1264        }        }
1265    
# Line 1870  const char *error; Line 1871  const char *error;
1871    
1872  /* Set the default line ending value from the default in the PCRE library;  /* Set the default line ending value from the default in the PCRE library;
1873  "lf", "cr", "crlf", and "any" are supported. Anything else is treated as "lf".  "lf", "cr", "crlf", and "any" are supported. Anything else is treated as "lf".
1874  */  Note that the return values from pcre_config(), though derived from the ASCII
1875    codes, are the same in EBCDIC environments, so we must use the actual values
1876    rather than escapes such as as '\r'. */
1877    
1878  (void)pcre_config(PCRE_CONFIG_NEWLINE, &i);  (void)pcre_config(PCRE_CONFIG_NEWLINE, &i);
1879  switch(i)  switch(i)
1880    {    {
1881    default:                 newline = (char *)"lf"; break;    default:               newline = (char *)"lf"; break;
1882    case '\r':               newline = (char *)"cr"; break;    case 13:               newline = (char *)"cr"; break;
1883    case ('\r' << 8) | '\n': newline = (char *)"crlf"; break;    case (13 << 8) | 10:   newline = (char *)"crlf"; break;
1884    case -1:                 newline = (char *)"any"; break;    case -1:               newline = (char *)"any"; break;
1885    case -2:                 newline = (char *)"anycrlf"; break;    case -2:               newline = (char *)"anycrlf"; break;
1886    }    }
1887    
1888  /* Process the options */  /* Process the options */

Legend:
Removed from v.378  
changed lines
  Added in v.392

  ViewVC Help
Powered by ViewVC 1.1.5