/[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 585 by ph10, Wed Jan 12 17:21:11 2011 UTC revision 636 by ph10, Sun Jul 24 17:43:51 2011 UTC
# Line 296  const char utf8_table4[] = { Line 296  const char utf8_table4[] = {
296    
297    
298  /*************************************************  /*************************************************
299    *         Exit from the program                  *
300    *************************************************/
301    
302    /* If there has been a resource error, give a suitable message.
303    
304    Argument:  the return code
305    Returns:   does not return
306    */
307    
308    static void
309    pcregrep_exit(int rc)
310    {
311    if (resource_error)
312      {
313      fprintf(stderr, "pcregrep: Error %d or %d means that a resource limit "
314        "was exceeded.\n", PCRE_ERROR_MATCHLIMIT, PCRE_ERROR_RECURSIONLIMIT);
315      fprintf(stderr, "pcregrep: Check your regex for nested unlimited loops.\n");
316      }
317    
318    exit(rc);
319    }
320    
321    
322    /*************************************************
323  *            OS-specific functions               *  *            OS-specific functions               *
324  *************************************************/  *************************************************/
325    
# Line 565  return sys_errlist[n]; Line 589  return sys_errlist[n];
589    
590    
591  /*************************************************  /*************************************************
 *         Exit from the program                  *  
 *************************************************/  
   
 /* If there has been a resource error, give a suitable message.  
   
 Argument:  the return code  
 Returns:   does not return  
 */  
   
 static void  
 pcregrep_exit(int rc)  
 {  
 if (resource_error)  
   {  
   fprintf(stderr, "pcregrep: Error %d or %d means that a resource limit "  
     "was exceeded.\n", PCRE_ERROR_MATCHLIMIT, PCRE_ERROR_RECURSIONLIMIT);  
   fprintf(stderr, "pcregrep: Check your regex for nested unlimited loops.\n");  
   }  
   
 exit(rc);  
 }  
   
   
   
 /*************************************************  
592  *            Read one line of input              *  *            Read one line of input              *
593  *************************************************/  *************************************************/
594    
# Line 635  Arguments: Line 634  Arguments:
634    endptr    end of available data    endptr    end of available data
635    lenptr    where to put the length of the eol sequence    lenptr    where to put the length of the eol sequence
636    
637  Returns:    pointer to the last byte of the line  Returns:    pointer to the last byte of the line, including the newline byte(s)
638  */  */
639    
640  static char *  static char *
# Line 936  is used multiple times for the same subj Line 935  is used multiple times for the same subj
935  to find all possible matches.  to find all possible matches.
936    
937  Arguments:  Arguments:
938    matchptr    the start of the subject    matchptr     the start of the subject
939    length      the length of the subject to match    length       the length of the subject to match
940    offsets     the offets vector to fill in    startoffset  where to start matching
941    mrc         address of where to put the result of pcre_exec()    offsets      the offets vector to fill in
942      mrc          address of where to put the result of pcre_exec()
943    
944  Returns:      TRUE if there was a match  Returns:      TRUE if there was a match
945                FALSE if there was no match                FALSE if there was no match
# Line 947  Returns:      TRUE if there was a match Line 947  Returns:      TRUE if there was a match
947  */  */
948    
949  static BOOL  static BOOL
950  match_patterns(char *matchptr, size_t length, int *offsets, int *mrc)  match_patterns(char *matchptr, size_t length, int startoffset, int *offsets,
951      int *mrc)
952  {  {
953  int i;  int i;
954  size_t slen = length;  size_t slen = length;
# Line 959  if (slen > 200) Line 960  if (slen > 200)
960    }    }
961  for (i = 0; i < pattern_count; i++)  for (i = 0; i < pattern_count; i++)
962    {    {
963    *mrc = pcre_exec(pattern_list[i], hints_list[i], matchptr, (int)length, 0,    *mrc = pcre_exec(pattern_list[i], hints_list[i], matchptr, (int)length,
964      PCRE_NOTEMPTY, offsets, OFFSET_SIZE);      startoffset, PCRE_NOTEMPTY, offsets, OFFSET_SIZE);
965    if (*mrc >= 0) return TRUE;    if (*mrc >= 0) return TRUE;
966    if (*mrc == PCRE_ERROR_NOMATCH) continue;    if (*mrc == PCRE_ERROR_NOMATCH) continue;
967    fprintf(stderr, "pcregrep: pcre_exec() gave error %d while matching ", *mrc);    fprintf(stderr, "pcregrep: pcre_exec() gave error %d while matching ", *mrc);
# Line 1078  while (ptr < endptr) Line 1079  while (ptr < endptr)
1079    {    {
1080    int endlinelength;    int endlinelength;
1081    int mrc = 0;    int mrc = 0;
1082      int startoffset = 0;
1083    BOOL match;    BOOL match;
1084    char *matchptr = ptr;    char *matchptr = ptr;
1085    char *t = ptr;    char *t = ptr;
# Line 1154  while (ptr < endptr) Line 1156  while (ptr < endptr)
1156    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
1157    finding subsequent matches when colouring matched lines. */    finding subsequent matches when colouring matched lines. */
1158    
1159    match = match_patterns(matchptr, length, offsets, &mrc);    match = match_patterns(matchptr, length, startoffset, offsets, &mrc);
1160    
1161    /* 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. */
1162    
# Line 1187  while (ptr < endptr) Line 1189  while (ptr < endptr)
1189      captured portion of it, as long as this string is not empty, and the      captured portion of it, as long as this string is not empty, and the
1190      --file-offsets and --line-offsets options output offsets for the matching      --file-offsets and --line-offsets options output offsets for the matching
1191      substring (they both force --only-matching = 0). None of these options      substring (they both force --only-matching = 0). None of these options
1192      prints any context. Afterwards, adjust the start and length, and then jump      prints any context. Afterwards, adjust the start and then jump back to look
1193      back to look for further matches in the same line. If we are in invert      for further matches in the same line. If we are in invert mode, however,
1194      mode, however, nothing is printed and we do not restart - this could still      nothing is printed and we do not restart - this could still be useful
1195      be useful because the return code is set. */      because the return code is set. */
1196    
1197      else if (only_matching >= 0)      else if (only_matching >= 0)
1198        {        {
# Line 1217  while (ptr < endptr) Line 1219  while (ptr < endptr)
1219              }              }
1220            }            }
1221          else if (printname != NULL || number) fprintf(stdout, "\n");          else if (printname != NULL || number) fprintf(stdout, "\n");
         matchptr += offsets[1];  
         length -= offsets[1];  
1222          match = FALSE;          match = FALSE;
1223          if (line_buffered) fflush(stdout);          if (line_buffered) fflush(stdout);
1224          rc = 0;    /* Had some success */          rc = 0;                      /* Had some success */
1225            startoffset = offsets[1];    /* Restart after the match */
1226          goto ONLY_MATCHING_RESTART;          goto ONLY_MATCHING_RESTART;
1227          }          }
1228        }        }
# Line 1317  while (ptr < endptr) Line 1318  while (ptr < endptr)
1318        (invert not set). Because the PCRE_FIRSTLINE option is set, the start of        (invert not set). Because the PCRE_FIRSTLINE option is set, the start of
1319        the match will always be before the first newline sequence. */        the match will always be before the first newline sequence. */
1320    
1321        if (multiline)        if (multiline & !invert)
1322          {          {
1323          int ellength;          char *endmatch = ptr + offsets[1];
1324          char *endmatch = ptr;          t = ptr;
1325          if (!invert)          while (t < endmatch)
1326            {            {
1327            endmatch += offsets[1];            t = end_of_line(t, endptr, &endlinelength);
1328            t = ptr;            if (t < endmatch) linenumber++; else break;
           while (t < endmatch)  
             {  
             t = end_of_line(t, endptr, &ellength);  
             if (t <= endmatch) linenumber++; else break;  
             }  
1329            }            }
1330          endmatch = end_of_line(endmatch, endptr, &ellength);          linelength = t - ptr - endlinelength;
         linelength = endmatch - ptr - ellength;  
1331          }          }
1332    
1333        /*** NOTE: Use only fwrite() to output the data line, so that binary        /*** NOTE: Use only fwrite() to output the data line, so that binary
# Line 1359  while (ptr < endptr) Line 1354  while (ptr < endptr)
1354    
1355        if (do_colour && !invert)        if (do_colour && !invert)
1356          {          {
1357          int last_offset = 0;          int plength;
1358          FWRITE(ptr, 1, offsets[0], stdout);          FWRITE(ptr, 1, offsets[0], stdout);
1359          fprintf(stdout, "%c[%sm", 0x1b, colour_string);          fprintf(stdout, "%c[%sm", 0x1b, colour_string);
1360          FWRITE(ptr + offsets[0], 1, offsets[1] - offsets[0], stdout);          FWRITE(ptr + offsets[0], 1, offsets[1] - offsets[0], stdout);
1361          fprintf(stdout, "%c[00m", 0x1b);          fprintf(stdout, "%c[00m", 0x1b);
1362          for (;;)          for (;;)
1363            {            {
1364            last_offset += offsets[1];            startoffset = offsets[1];
1365            matchptr += offsets[1];            if (startoffset >= linelength + endlinelength ||
1366            length -= offsets[1];                !match_patterns(matchptr, length, startoffset, offsets, &mrc))
1367            if (!match_patterns(matchptr, length, offsets, &mrc)) break;              break;
1368            FWRITE(matchptr, 1, offsets[0], stdout);            FWRITE(matchptr + startoffset, 1, offsets[0] - startoffset, stdout);
1369            fprintf(stdout, "%c[%sm", 0x1b, colour_string);            fprintf(stdout, "%c[%sm", 0x1b, colour_string);
1370            FWRITE(matchptr + offsets[0], 1, offsets[1] - offsets[0], stdout);            FWRITE(matchptr + offsets[0], 1, offsets[1] - offsets[0], stdout);
1371            fprintf(stdout, "%c[00m", 0x1b);            fprintf(stdout, "%c[00m", 0x1b);
1372            }            }
1373          FWRITE(ptr + last_offset, 1,  
1374            (linelength + endlinelength) - last_offset, stdout);          /* In multiline mode, we may have already printed the complete line
1375            and its line-ending characters (if they matched the pattern), so there
1376            may be no more to print. */
1377    
1378            plength = (linelength + endlinelength) - startoffset;
1379            if (plength > 0) FWRITE(ptr + startoffset, 1, plength, stdout);
1380          }          }
1381    
1382        /* Not colouring; no need to search for further matches */        /* Not colouring; no need to search for further matches */

Legend:
Removed from v.585  
changed lines
  Added in v.636

  ViewVC Help
Powered by ViewVC 1.1.5