/[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 149 by ph10, Mon Apr 16 15:28:08 2007 UTC revision 279 by ph10, Tue Dec 4 20:01:43 2007 UTC
# Line 38  POSSIBILITY OF SUCH DAMAGE. Line 38  POSSIBILITY OF SUCH DAMAGE.
38  */  */
39    
40  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
41  #  include <config.h>  #include "config.h"
42  #endif  #endif
43    
44  #include <ctype.h>  #include <ctype.h>
# 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    
54  #ifdef HAVE_UNISTD_H  #ifdef HAVE_UNISTD_H
55  #  include <unistd.h>  #include <unistd.h>
56  #endif  #endif
57    
58  #include <pcre.h>  #include "pcre.h"
59    
60  #define FALSE 0  #define FALSE 0
61  #define TRUE 1  #define TRUE 1
# Line 280  for (;;) Line 281  for (;;)
281    if (strcmp(dent->d_name, ".") != 0 && strcmp(dent->d_name, "..") != 0)    if (strcmp(dent->d_name, ".") != 0 && strcmp(dent->d_name, "..") != 0)
282      return dent->d_name;      return dent->d_name;
283    }    }
284  return NULL;   /* Keep compiler happy; never executed */  /* Control never reaches here */
285  }  }
286    
287  static void  static void
# Line 580  switch(endlinetype) Line 581  switch(endlinetype)
581          }          }
582        else *lenptr = 1;        else *lenptr = 1;
583        return p;        return p;
584    
585        default:        default:
586        break;        break;
587        }        }
588      }   /* End of loop for ANYCRLF case */      }   /* End of loop for ANYCRLF case */
589    
590    *lenptr = 0;  /* Must have hit the end */    *lenptr = 0;  /* Must have hit the end */
591    return endptr;    return endptr;
592    
# Line 687  switch(endlinetype) Line 688  switch(endlinetype)
688    return p;   /* But control should never get here */    return p;   /* But control should never get here */
689    
690    case EL_ANY:    case EL_ANY:
691    case EL_ANYCRLF:    case EL_ANYCRLF:
692    if (*(--p) == '\n' && p > startptr && p[-1] == '\r') p--;    if (*(--p) == '\n' && p > startptr && p[-1] == '\r') p--;
693    if (utf8) while ((*p & 0xc0) == 0x80) p--;    if (utf8) while ((*p & 0xc0) == 0x80) p--;
694    
# Line 721  switch(endlinetype) Line 722  switch(endlinetype)
722        case 0x0a:    /* LF */        case 0x0a:    /* LF */
723        case 0x0d:    /* CR */        case 0x0d:    /* CR */
724        return p;        return p;
725    
726        default:        default:
727        break;        break;
728        }        }
729    
730      else switch (c)      else switch (c)
731        {        {
# Line 843  while (ptr < endptr) Line 844  while (ptr < endptr)
844    int i, endlinelength;    int i, endlinelength;
845    int mrc = 0;    int mrc = 0;
846    BOOL match = FALSE;    BOOL match = FALSE;
847      char *matchptr = ptr;
848    char *t = ptr;    char *t = ptr;
849    size_t length, linelength;    size_t length, linelength;
850    
# Line 855  while (ptr < endptr) Line 857  while (ptr < endptr)
857    
858    t = end_of_line(t, endptr, &endlinelength);    t = end_of_line(t, endptr, &endlinelength);
859    linelength = t - ptr - endlinelength;    linelength = t - ptr - endlinelength;
860    length = multiline? endptr - ptr : linelength;    length = multiline? (size_t)(endptr - ptr) : linelength;
861    
862    /* Extra processing for Jeffrey Friedl's debugging. */    /* Extra processing for Jeffrey Friedl's debugging. */
863    
# Line 905  while (ptr < endptr) Line 907  while (ptr < endptr)
907    }    }
908  #endif  #endif
909    
910      /* We come back here after a match when the -o option (only_matching) is set,
911      in order to find any further matches in the same line. */
912    
913      ONLY_MATCHING_RESTART:
914    
915    /* Run through all the patterns until one matches. Note that we don't include    /* Run through all the patterns until one matches. Note that we don't include
916    the final newline in the subject string. */    the final newline in the subject string. */
917    
918    for (i = 0; i < pattern_count; i++)    for (i = 0; i < pattern_count; i++)
919      {      {
920      mrc = pcre_exec(pattern_list[i], hints_list[i], ptr, length, 0, 0,      mrc = pcre_exec(pattern_list[i], hints_list[i], matchptr, length, 0, 0,
921        offsets, 99);        offsets, 99);
922      if (mrc >= 0) { match = TRUE; break; }      if (mrc >= 0) { match = TRUE; break; }
923      if (mrc != PCRE_ERROR_NOMATCH)      if (mrc != PCRE_ERROR_NOMATCH)
# Line 919  while (ptr < endptr) Line 925  while (ptr < endptr)
925        fprintf(stderr, "pcregrep: pcre_exec() error %d while matching ", mrc);        fprintf(stderr, "pcregrep: pcre_exec() error %d while matching ", mrc);
926        if (pattern_count > 1) fprintf(stderr, "pattern number %d to ", i+1);        if (pattern_count > 1) fprintf(stderr, "pattern number %d to ", i+1);
927        fprintf(stderr, "this line:\n");        fprintf(stderr, "this line:\n");
928        fwrite(ptr, 1, linelength, stderr);   /* In case binary zero included */        fwrite(matchptr, 1, linelength, stderr);  /* In case binary zero included */
929        fprintf(stderr, "\n");        fprintf(stderr, "\n");
930        if (error_count == 0 &&        if (error_count == 0 &&
931            (mrc == PCRE_ERROR_MATCHLIMIT || mrc == PCRE_ERROR_RECURSIONLIMIT))            (mrc == PCRE_ERROR_MATCHLIMIT || mrc == PCRE_ERROR_RECURSIONLIMIT))
# Line 966  while (ptr < endptr) Line 972  while (ptr < endptr)
972      else if (quiet) return 0;      else if (quiet) return 0;
973    
974      /* The --only-matching option prints just the substring that matched, and      /* The --only-matching option prints just the substring that matched, and
975      does not pring any context. */      does not print any context. Afterwards, adjust the start and length, and
976        then jump back to look for further matches in the same line. If we are in
977        invert mode, however, nothing is printed - this could be useful still
978        because the return code is set. */
979    
980      else if (only_matching)      else if (only_matching)
981        {        {
982        if (printname != NULL) fprintf(stdout, "%s:", printname);        if (!invert)
983        if (number) fprintf(stdout, "%d:", linenumber);          {
984        fwrite(ptr + offsets[0], 1, offsets[1] - offsets[0], stdout);          if (printname != NULL) fprintf(stdout, "%s:", printname);
985        fprintf(stdout, "\n");          if (number) fprintf(stdout, "%d:", linenumber);
986            fwrite(matchptr + offsets[0], 1, offsets[1] - offsets[0], stdout);
987            fprintf(stdout, "\n");
988            matchptr += offsets[1];
989            length -= offsets[1];
990            match = FALSE;
991            goto ONLY_MATCHING_RESTART;
992            }
993        }        }
994    
995      /* This is the default case when none of the above options is set. We print      /* This is the default case when none of the above options is set. We print
# Line 1063  while (ptr < endptr) Line 1079  while (ptr < endptr)
1079    
1080        /* In multiline mode, we want to print to the end of the line in which        /* In multiline mode, we want to print to the end of the line in which
1081        the end of the matched string is found, so we adjust linelength and the        the end of the matched string is found, so we adjust linelength and the
1082        line number appropriately. Because the PCRE_FIRSTLINE option is set, the        line number appropriately, but only when there actually was a match
1083        start of the match will always be before the first newline sequence. */        (invert not set). Because the PCRE_FIRSTLINE option is set, the start of
1084          the match will always be before the first newline sequence. */
1085    
1086        if (multiline)        if (multiline)
1087          {          {
1088          int ellength;          int ellength;
1089          char *endmatch = ptr + offsets[1];          char *endmatch = ptr;
1090          t = ptr;          if (!invert)
         while (t < endmatch)  
1091            {            {
1092            t = end_of_line(t, endptr, &ellength);            endmatch += offsets[1];
1093            if (t <= endmatch) linenumber++; else break;            t = ptr;
1094              while (t < endmatch)
1095                {
1096                t = end_of_line(t, endptr, &ellength);
1097                if (t <= endmatch) linenumber++; else break;
1098                }
1099            }            }
1100          endmatch = end_of_line(endmatch, endptr, &ellength);          endmatch = end_of_line(endmatch, endptr, &ellength);
1101          linelength = endmatch - ptr - ellength;          linelength = endmatch - ptr - ellength;
# Line 1107  while (ptr < endptr) Line 1128  while (ptr < endptr)
1128          fprintf(stdout, "%c[%sm", 0x1b, colour_string);          fprintf(stdout, "%c[%sm", 0x1b, colour_string);
1129          fwrite(ptr + offsets[0], 1, offsets[1] - offsets[0], stdout);          fwrite(ptr + offsets[0], 1, offsets[1] - offsets[0], stdout);
1130          fprintf(stdout, "%c[00m", 0x1b);          fprintf(stdout, "%c[00m", 0x1b);
1131          fwrite(ptr + offsets[1], 1, linelength - offsets[1], stdout);          fwrite(ptr + offsets[1], 1, (linelength + endlinelength) - offsets[1],
1132              stdout);
1133          }          }
1134        else fwrite(ptr, 1, linelength + endlinelength, stdout);        else fwrite(ptr, 1, linelength + endlinelength, stdout);
1135        }        }
# Line 1123  while (ptr < endptr) Line 1145  while (ptr < endptr)
1145      lastmatchnumber = linenumber + 1;      lastmatchnumber = linenumber + 1;
1146      }      }
1147    
1148      /* For a match in multiline inverted mode (which of course did not cause
1149      anything to be printed), we have to move on to the end of the match before
1150      proceeding. */
1151    
1152      if (multiline && invert && match)
1153        {
1154        int ellength;
1155        char *endmatch = ptr + offsets[1];
1156        t = ptr;
1157        while (t < endmatch)
1158          {
1159          t = end_of_line(t, endptr, &ellength);
1160          if (t <= endmatch) linenumber++; else break;
1161          }
1162        endmatch = end_of_line(endmatch, endptr, &ellength);
1163        linelength = endmatch - ptr - ellength;
1164        }
1165    
1166    /* Advance to after the newline and increment the line number. */    /* Advance to after the newline and increment the line number. */
1167    
1168    ptr += linelength + endlinelength;    ptr += linelength + endlinelength;
# Line 1525  if ((process_options & PO_FIXED_STRINGS) Line 1565  if ((process_options & PO_FIXED_STRINGS)
1565      char *p = end_of_line(pattern, eop, &ellength);      char *p = end_of_line(pattern, eop, &ellength);
1566      if (ellength == 0)      if (ellength == 0)
1567        return compile_single_pattern(pattern, options, filename, count);        return compile_single_pattern(pattern, options, filename, count);
1568      sprintf(buffer, "%.*s", p - pattern - ellength, pattern);      sprintf(buffer, "%.*s", (int)(p - pattern - ellength), pattern);
1569      pattern = p;      pattern = p;
1570      if (!compile_single_pattern(buffer, options, filename, count))      if (!compile_single_pattern(buffer, options, filename, count))
1571        return FALSE;        return FALSE;
# Line 1567  switch(i) Line 1607  switch(i)
1607    case '\r':               newline = (char *)"cr"; break;    case '\r':               newline = (char *)"cr"; break;
1608    case ('\r' << 8) | '\n': newline = (char *)"crlf"; break;    case ('\r' << 8) | '\n': newline = (char *)"crlf"; break;
1609    case -1:                 newline = (char *)"any"; break;    case -1:                 newline = (char *)"any"; break;
1610    case -2:                 newline = (char *)"anycrlf"; break;    case -2:                 newline = (char *)"anycrlf"; break;
1611    }    }
1612    
1613  /* Process the options */  /* Process the options */
# Line 1625  for (i = 1; i < argc; i++) Line 1665  for (i = 1; i < argc; i++)
1665          else                 /* Special case xxx=data */          else                 /* Special case xxx=data */
1666            {            {
1667            int oplen = equals - op->long_name;            int oplen = equals - op->long_name;
1668            int arglen = (argequals == NULL)? strlen(arg) : argequals - arg;            int arglen = (argequals == NULL)? (int)strlen(arg) : argequals - arg;
1669            if (oplen == arglen && strncmp(arg, op->long_name, oplen) == 0)            if (oplen == arglen && strncmp(arg, op->long_name, oplen) == 0)
1670              {              {
1671              option_data = arg + arglen;              option_data = arg + arglen;
# Line 1644  for (i = 1; i < argc; i++) Line 1684  for (i = 1; i < argc; i++)
1684          char buff2[24];          char buff2[24];
1685          int baselen = opbra - op->long_name;          int baselen = opbra - op->long_name;
1686          sprintf(buff1, "%.*s", baselen, op->long_name);          sprintf(buff1, "%.*s", baselen, op->long_name);
1687          sprintf(buff2, "%s%.*s", buff1, strlen(op->long_name) - baselen - 2,          sprintf(buff2, "%s%.*s", buff1,
1688            opbra + 1);            (int)strlen(op->long_name) - baselen - 2, opbra + 1);
1689          if (strcmp(arg, buff1) == 0 || strcmp(arg, buff2) == 0)          if (strcmp(arg, buff1) == 0 || strcmp(arg, buff2) == 0)
1690            break;            break;
1691          }          }

Legend:
Removed from v.149  
changed lines
  Added in v.279

  ViewVC Help
Powered by ViewVC 1.1.5