RPM Community Forums

Mailing List Message of <rpm-cvs>

[CVS] RPM: rpm/rpmio/ rpmgrep.c

From: Jeff Johnson <jbj@rpm5.org>
Date: Sun 10 Feb 2008 - 20:40:10 CET
Message-Id: <20080210194010.86B9234844A@rpm5.org>
  RPM Package Manager, CVS Repository
  http://rpm5.org/cvs/
  ____________________________________________________________________________

  Server: rpm5.org                         Name:   Jeff Johnson
  Root:   /v/rpm/cvs                       Email:  jbj@rpm5.org
  Module: rpm                              Date:   10-Feb-2008 20:40:10
  Branch: HEAD                             Handle: 2008021019401000

  Modified files:
    rpm/rpmio               rpmgrep.c

  Log:
    - rpmgrep: another readbility & style pass, add doxygen markup.

  Summary:
    Revision    Changes     Path
    1.8         +475 -587   rpm/rpmio/rpmgrep.c
  ____________________________________________________________________________

  patch -p0 <<'@@ .'
  Index: rpm/rpmio/rpmgrep.c
  ============================================================================
  $ cvs diff -u -r1.7 -r1.8 rpmgrep.c
  --- rpm/rpmio/rpmgrep.c	10 Feb 2008 18:32:33 -0000	1.7
  +++ rpm/rpmio/rpmgrep.c	10 Feb 2008 19:40:10 -0000	1.8
  @@ -70,33 +70,28 @@
   #define MBUFTHIRD 8192
   #endif
   
  -/* Values for the "filenames" variable, which specifies options for file name
  -output. The order is important; it is assumed that a file name is wanted for
  -all values greater than FN_DEFAULT. */
  -
  +/**
  + * Values for the "filenames" variable, which specifies options for file name
  + * output. The order is important; it is assumed that a file name is wanted for
  + * all values greater than FN_DEFAULT.
  + */
   enum { FN_NONE, FN_DEFAULT, FN_ONLY, FN_NOMATCH_ONLY, FN_FORCE };
   
  -/* File reading styles */
  -
  +/** File reading styles */
   enum { FR_PLAIN, FR_LIBZ, FR_LIBBZ2 };
   
  -/* Actions for the -d and -D options */
  -
  +/** Actions for the -d and -D options */
   enum { dee_READ, dee_SKIP, dee_RECURSE };
   enum { DEE_READ, DEE_SKIP };
   
  -/* Actions for special processing options (flag bits) */
  -
  +/** Actions for special processing options (flag bits) */
   #define PO_WORD_MATCH     0x0001
   #define PO_LINE_MATCH     0x0002
   #define PO_FIXED_STRINGS  0x0004
   
  -/* Line ending types */
  -
  +/** Line ending types */
   enum { EL_LF, EL_CR, EL_CRLF, EL_ANY, EL_ANYCRLF };
   
  -
  -
   /*************************************************
   *               Global variables                 *
   *************************************************/
  @@ -156,19 +151,19 @@
   static BOOL silent = FALSE;
   static BOOL utf8 = FALSE;
   
  -/* Tables for prefixing and suffixing patterns, according to the -w, -x, and -F
  -options. These set the 1, 2, and 4 bits in process_options, respectively. Note
  -that the combination of -w and -x has the same effect as -x on its own, so we
  -can treat them as the same. */
  -
  +/**
  + * Tables for prefixing and suffixing patterns, according to the -w, -x, and -F
  + * options. These set the 1, 2, and 4 bits in process_options, respectively.
  + * Note that the combination of -w and -x has the same effect as -x on its own,
  + * so we can treat them as the same.
  + */
   static const char *prefix[] = {
     "", "\\b", "^(?:", "^(?:", "\\Q", "\\b\\Q", "^(?:\\Q", "^(?:\\Q" };
   
   static const char *suffix[] = {
     "", "\\b", ")$",   ")$",   "\\E", "\\E\\b", "\\E)$",   "\\E)$" };
   
  -/* UTF-8 tables - used only when the newline setting is "any". */
  -
  +/** UTF-8 tables - used only when the newline setting is "any". */
   const int utf8_table3[] = { 0xff, 0x1f, 0x0f, 0x07, 0x03, 0x01};
   
   const char utf8_table4[] = {
  @@ -188,11 +183,6 @@
   /************* Directory scanning in Unix ***********/
   
   #if defined HAVE_SYS_STAT_H && defined HAVE_DIRENT_H && defined HAVE_SYS_TYPES_H
  -#ifdef	DYING
  -#include <sys/types.h>
  -#include <sys/stat.h>
  -#include <dirent.h>
  -#endif
   
   typedef DIR directory_type;
   
  @@ -232,7 +222,6 @@
   
   
   /************* Test for regular file in Unix **********/
  -
   static int
   isregfile(char *filename)
   {
  @@ -244,7 +233,6 @@
   
   
   /************* Test stdout for being a terminal in Unix **********/
  -
   static BOOL
   is_stdout_tty(void)
   {
  @@ -264,8 +252,6 @@
   
   #endif
   
  -
  -
   #ifndef HAVE_STRERROR
   extern int   sys_nerr;
   extern char *sys_errlist[];
  @@ -280,302 +266,284 @@
   #endif /* HAVE_STRERROR */
   
   /*************************************************
  -*             Find end of line                   *
  -*************************************************/
  -
  -/* The length of the endline sequence that is found is set via lenptr. This may
  -be zero at the very end of the file if there is no line-ending sequence there.
  -
  -Arguments:
  -  p         current position in line
  -  endptr    end of available data
  -  lenptr    where to put the length of the eol sequence
  -
  -Returns:    pointer to the last byte of the line
  + * Find end of line.
  + *
  + * The length of the endline sequence that is found is set via lenptr.
  + * This may be zero at the very end of the file if there is no line-ending
  + * sequence there.
  + *
  + * @param p		current position in line
  + * @param endptr	end of available data
  + * @retval *lenptr	length of the eol sequence
  + * @return		pointer to the last byte of the line
   */
  -
   static const char *
   end_of_line(const char *p, const char *endptr, int *lenptr)
   {
       switch(endlinetype) {
  -    default:      /* Just in case */
  +    default:	/* Just in case */
       case EL_LF:
  -      while (p < endptr && *p != '\n') p++;
  -      if (p < endptr) {
  -        *lenptr = 1;
  -        return p + 1;
  -      }
  -      *lenptr = 0;
  -      return endptr;
  -    
  +	while (p < endptr && *p != '\n') p++;
  +	if (p < endptr) {
  +	    *lenptr = 1;
  +	    return p + 1;
  +	}
  +	*lenptr = 0;
  +	return endptr;
  +
       case EL_CR:
  -      while (p < endptr && *p != '\r') p++;
  -      if (p < endptr) {
  -        *lenptr = 1;
  -        return p + 1;
  -      }
  -      *lenptr = 0;
  -      return endptr;
  -    
  +	while (p < endptr && *p != '\r') p++;
  +	if (p < endptr) {
  +	    *lenptr = 1;
  +	    return p + 1;
  +	}
  +	*lenptr = 0;
  +	return endptr;
  +
       case EL_CRLF:
  -      for (;;) {
  -        while (p < endptr && *p != '\r') p++;
  -        if (++p >= endptr) {
  -          *lenptr = 0;
  -          return endptr;
  -        }
  -        if (*p == '\n') {
  -          *lenptr = 2;
  -          return p + 1;
  -        }
  -      }
  -      break;
  -    
  +	for (;;) {
  +	    while (p < endptr && *p != '\r') p++;
  +	    if (++p >= endptr) {
  +		*lenptr = 0;
  +		return endptr;
  +	    }
  +	    if (*p == '\n') {
  +		*lenptr = 2;
  +		return p + 1;
  +	    }
  +	}
  +	break;
  +
       case EL_ANYCRLF:
  -      while (p < endptr) {
  -        int extra = 0;
  -        register int c = *((unsigned char *)p);
  -    
  -        if (utf8 && c >= 0xc0) {
  -          int gcii, gcss;
  -          extra = utf8_table4[c & 0x3f];  /* Number of additional bytes */
  -          gcss = 6*extra;
  -          c = (c & utf8_table3[extra]) << gcss;
  -          for (gcii = 1; gcii <= extra; gcii++) {
  -            gcss -= 6;
  -            c |= (p[gcii] & 0x3f) << gcss;
  -          }
  -        }
  -    
  -        p += 1 + extra;
  -    
  -        switch (c) {
  -        case 0x0a:    /* LF */
  -          *lenptr = 1;
  -          return p;
  -    
  -        case 0x0d:    /* CR */
  -          if (p < endptr && *p == 0x0a) {
  -            *lenptr = 2;
  -            p++;
  -          }
  -          else *lenptr = 1;
  -          return p;
  -    
  -        default:
  -          break;
  -          }
  -      }   /* End of loop for ANYCRLF case */
  -    
  -      *lenptr = 0;  /* Must have hit the end */
  -      return endptr;
  -    
  +	while (p < endptr) {
  +	    int extra = 0;
  +	    register int c = *((unsigned char *)p);
  +
  +	    if (utf8 && c >= 0xc0) {
  +		int gcii, gcss;
  +		extra = utf8_table4[c & 0x3f];  /* No. of additional bytes */
  +		gcss = 6*extra;
  +		c = (c & utf8_table3[extra]) << gcss;
  +		for (gcii = 1; gcii <= extra; gcii++) {
  +		    gcss -= 6;
  +		    c |= (p[gcii] & 0x3f) << gcss;
  +		}
  +	    }
  +
  +	    p += 1 + extra;
  +
  +	    switch (c) {
  +	    case 0x0a:    /* LF */
  +		*lenptr = 1;
  +		return p;
  +
  +	    case 0x0d:    /* CR */
  +		if (p < endptr && *p == 0x0a) {
  +		    *lenptr = 2;
  +		    p++;
  +		}
  +		else *lenptr = 1;
  +		return p;
  +
  +	    default:
  +		break;
  +		}
  +	}   /* End of loop for ANYCRLF case */
  +
  +	*lenptr = 0;  /* Must have hit the end */
  +	return endptr;
  +
       case EL_ANY:
  -      while (p < endptr) {
  -        int extra = 0;
  -        register int c = *((unsigned char *)p);
  -    
  -        if (utf8 && c >= 0xc0) {
  -          int gcii, gcss;
  -          extra = utf8_table4[c & 0x3f];  /* Number of additional bytes */
  -          gcss = 6*extra;
  -          c = (c & utf8_table3[extra]) << gcss;
  -          for (gcii = 1; gcii <= extra; gcii++) {
  -            gcss -= 6;
  -            c |= (p[gcii] & 0x3f) << gcss;
  -          }
  -        }
  -    
  -        p += 1 + extra;
  -    
  -        switch (c) {
  -        case 0x0a:    /* LF */
  -        case 0x0b:    /* VT */
  -        case 0x0c:    /* FF */
  -          *lenptr = 1;
  -          return p;
  -    
  -        case 0x0d:    /* CR */
  -          if (p < endptr && *p == 0x0a) {
  -            *lenptr = 2;
  -            p++;
  -          }
  -          else *lenptr = 1;
  -          return p;
  -    
  -        case 0x85:    /* NEL */
  -          *lenptr = utf8? 2 : 1;
  -          return p;
  -    
  -        case 0x2028:  /* LS */
  -        case 0x2029:  /* PS */
  -          *lenptr = 3;
  -          return p;
  -    
  -        default:
  -          break;
  -        }
  -      }   /* End of loop for ANY case */
  -    
  -      *lenptr = 0;  /* Must have hit the end */
  -      return endptr;
  -    }     /* End of overall switch */
  +	while (p < endptr) {
  +	    int extra = 0;
  +	    register int c = *((unsigned char *)p);
  +
  +	    if (utf8 && c >= 0xc0) {
  +		int gcii, gcss;
  +		extra = utf8_table4[c & 0x3f];  /* No. of additional bytes */
  +		gcss = 6*extra;
  +		c = (c & utf8_table3[extra]) << gcss;
  +		for (gcii = 1; gcii <= extra; gcii++) {
  +		    gcss -= 6;
  +		    c |= (p[gcii] & 0x3f) << gcss;
  +		}
  +	    }
  +
  +	    p += 1 + extra;
  +
  +	    switch (c) {
  +	    case 0x0a:    /* LF */
  +	    case 0x0b:    /* VT */
  +	    case 0x0c:    /* FF */
  +		*lenptr = 1;
  +		return p;
  +
  +	    case 0x0d:    /* CR */
  +		if (p < endptr && *p == 0x0a) {
  +		    *lenptr = 2;
  +		    p++;
  +		}
  +		else *lenptr = 1;
  +		return p;
  +
  +	    case 0x85:    /* NEL */
  +		*lenptr = utf8? 2 : 1;
  +		return p;
  +
  +	    case 0x2028:  /* LS */
  +	    case 0x2029:  /* PS */
  +		*lenptr = 3;
  +		return p;
  +
  +	    default:
  +		break;
  +	    }
  +	}   /* End of loop for ANY case */
  +
  +	*lenptr = 0;  /* Must have hit the end */
  +	return endptr;
  +    }	/* End of overall switch */
   }
   
   /*************************************************
  -*         Find start of previous line            *
  -*************************************************/
  -
  -/* This is called when looking back for before lines to print.
  -
  -Arguments:
  -  p         start of the subsequent line
  -  startptr  start of available data
  -
  -Returns:    pointer to the start of the previous line
  -*/
  -
  + * Find start of previous line
  + *
  + * This is called when looking back for before lines to print.
  + *
  + * @param p		start of the subsequent line
  + * @param startptr	start of available data
  + * @return 		pointer to the start of the previous line
  + */
   static const char *
  -previous_line(const char *p, char *startptr)
  +previous_line(const char *p, const char *startptr)
   {
       switch(endlinetype) {
       default:      /* Just in case */
       case EL_LF:
  -      p--;
  -      while (p > startptr && p[-1] != '\n') p--;
  -      return p;
  -    
  +	p--;
  +	while (p > startptr && p[-1] != '\n') p--;
  +	return p;
  +
       case EL_CR:
  -      p--;
  -      while (p > startptr && p[-1] != '\n') p--;
  -      return p;
  -    
  +	p--;
  +	while (p > startptr && p[-1] != '\n') p--;
  +	return p;
  +
       case EL_CRLF:
  -      for (;;) {
  -        p -= 2;
  -        while (p > startptr && p[-1] != '\n') p--;
  -        if (p <= startptr + 1 || p[-2] == '\r') return p;
  -      }
  -      return p;   /* But control should never get here */
  -    
  +	for (;;) {
  +	    p -= 2;
  +	    while (p > startptr && p[-1] != '\n') p--;
  +	    if (p <= startptr + 1 || p[-2] == '\r') return p;
  +	}
  +	return p;   /* But control should never get here */
  +
       case EL_ANY:
       case EL_ANYCRLF:
  -      if (*(--p) == '\n' && p > startptr && p[-1] == '\r') p--;
  -      if (utf8) while ((*p & 0xc0) == 0x80) p--;
  -    
  -      while (p > startptr) {
  -        register int c;
  -        const char *pp = p - 1;
  -    
  -        if (utf8) {
  -          int extra = 0;
  -          while ((*pp & 0xc0) == 0x80) pp--;
  -          c = *((unsigned char *)pp);
  -          if (c >= 0xc0) {
  -            int gcii, gcss;
  -            extra = utf8_table4[c & 0x3f];  /* Number of additional bytes */
  -            gcss = 6*extra;
  -            c = (c & utf8_table3[extra]) << gcss;
  -            for (gcii = 1; gcii <= extra; gcii++) {
  -              gcss -= 6;
  -              c |= (pp[gcii] & 0x3f) << gcss;
  -            }
  -          }
  -        }
  -        else c = *((unsigned char *)pp);
  -    
  -        if (endlinetype == EL_ANYCRLF) {
  -	  switch (c) {
  -          case 0x0a:    /* LF */
  -          case 0x0d:    /* CR */
  -            return p;
  -    
  -          default:
  -            break;
  -          } 
  -        } else {
  -          switch (c) {
  -          case 0x0a:    /* LF */
  -          case 0x0b:    /* VT */
  -          case 0x0c:    /* FF */
  -          case 0x0d:    /* CR */
  -          case 0x85:    /* NEL */
  -          case 0x2028:  /* LS */
  -          case 0x2029:  /* PS */
  -            return p;
  -    
  -          default:
  -            break;
  -          }
  -        }
  -    
  -        p = pp;  /* Back one character */
  -      }        /* End of loop for ANY case */
  -    
  -      return startptr;  /* Hit start of data */
  -    }     /* End of overall switch */
  -}
  +	if (*(--p) == '\n' && p > startptr && p[-1] == '\r') p--;
  +	if (utf8) while ((*p & 0xc0) == 0x80) p--;
   
  -/*************************************************
  -*       Print the previous "after" lines         *
  -*************************************************/
  +	while (p > startptr) {
  +	    const char *pp = p - 1;
  +	    register int c;
  +
  +	    if (utf8) {
  +		int extra = 0;
  +		while ((*pp & 0xc0) == 0x80) pp--;
  +		c = *((unsigned char *)pp);
  +		if (c >= 0xc0) {
  +		    int gcii, gcss;
  +		    extra = utf8_table4[c & 0x3f]; /* No. of additional bytes */
  +		    gcss = 6*extra;
  +		    c = (c & utf8_table3[extra]) << gcss;
  +		    for (gcii = 1; gcii <= extra; gcii++) {
  +			gcss -= 6;
  +			c |= (pp[gcii] & 0x3f) << gcss;
  +		    }
  +		}
  +	    } else
  +		c = *((unsigned char *)pp);
  +
  +	    if (endlinetype == EL_ANYCRLF) {
  +		switch (c) {
  +		case 0x0a:    /* LF */
  +		case 0x0d:    /* CR */
  +		    return p;
  +
  +		default:
  +		    break;
  +		}
  +	    } else {
  +		switch (c) {
  +		case 0x0a:    /* LF */
  +		case 0x0b:    /* VT */
  +		case 0x0c:    /* FF */
  +		case 0x0d:    /* CR */
  +		case 0x85:    /* NEL */
  +		case 0x2028:  /* LS */
  +		case 0x2029:  /* PS */
  +		    return p;
  +
  +		default:
  +		    break;
  +		}
  +	    }
   
  -/* This is called if we are about to lose said lines because of buffer filling,
  -and at the end of the file. The data in the line is written using fwrite() so
  -that a binary zero does not terminate it.
  -
  -Arguments:
  -  lastmatchnumber   the number of the last matching line, plus one
  -  lastmatchrestart  where we restarted after the last match
  -  endptr            end of available data
  -  printname         filename for printing
  +	    p = pp;  /* Back one character */
  +	}	/* End of loop for ANY case */
   
  -Returns:            nothing
  -*/
  +	return startptr;  /* Hit start of data */
  +    }	/* End of overall switch */
  +}
   
  +/*************************************************
  + * Print the previous "after" lines
  + *
  + * This is called if we are about to lose said lines because of buffer filling,
  + * and at the end of the file. The data in the line is written using fwrite() so
  + * that a binary zero does not terminate it.
  + *
  + * @param lastmatchnumber	the number of the last matching line, plus one
  + * @param lastmatchrestart	where we restarted after the last match
  + * @param endptr		end of available data
  + * @param printname		filename for printing
  + */
   static void do_after_lines(int lastmatchnumber, const char *lastmatchrestart,
  -  char *endptr, char *printname)
  +		char *endptr, char *printname)
   {
       if (after_context > 0 && lastmatchnumber > 0) {
  -      int count = 0;
  -      while (lastmatchrestart < endptr && count++ < after_context) {
  -        int ellength;
  -        const char *pp = lastmatchrestart;
  -        if (printname != NULL) fprintf(stdout, "%s-", printname);
  -        if (number) fprintf(stdout, "%d-", lastmatchnumber++);
  -        pp = end_of_line(pp, endptr, &ellength);
  -        fwrite(lastmatchrestart, 1, pp - lastmatchrestart, stdout);
  -        lastmatchrestart = pp;
  -      }
  -      hyphenpending = TRUE;
  +	int count = 0;
  +	while (lastmatchrestart < endptr && count++ < after_context) {
  +	    const char *pp = lastmatchrestart;
  +	    int ellength;
  +	    if (printname != NULL) fprintf(stdout, "%s-", printname);
  +	    if (number) fprintf(stdout, "%d-", lastmatchnumber++);
  +	    pp = end_of_line(pp, endptr, &ellength);
  +	    fwrite(lastmatchrestart, 1, pp - lastmatchrestart, stdout);
  +	    lastmatchrestart = pp;
  +	}
  +	hyphenpending = TRUE;
       }
   }
   
   /*************************************************
  -*            Grep an individual file             *
  -*************************************************/
  -
  -/* This is called from grep_or_recurse() below. It uses a buffer that is three
  -times the value of MBUFTHIRD. The matching point is never allowed to stray into
  -the top third of the buffer, thus keeping more of the file available for
  -context printing or for multiline scanning. For large files, the pointer will
  -be in the middle third most of the time, so the bottom third is available for
  -"before" context printing.
  -
  -Arguments:
  -  handle       the fopened FILE stream for a normal file
  -               the gzFile pointer when reading is via libz
  -               the BZFILE pointer when reading is via libbz2
  -  frtype       FR_PLAIN, FR_LIBZ, or FR_LIBBZ2
  -  printname    the file name if it is to be printed for each match
  -               or NULL if the file name is not to be printed
  -               it cannot be NULL if filenames[_nomatch]_only is set
  -
  -Returns:       0 if there was at least one match
  -               1 otherwise (no matches)
  -               2 if there is a read error on a .bz2 file
  -*/
  -
  + * Grep an individual file
  + *
  + * This is called from grep_or_recurse() below. It uses a buffer that is three
  + * times the value of MBUFTHIRD. The matching point is never allowed to stray
  + * into the top third of the buffer, thus keeping more of the file available
  + * for context printing or for multiline scanning. For large files, the pointer
  + * will be in the middle third most of the time, so the bottom third is
  + * available for "before" context printing.
  + *
  + * @param  handle	the fopened FILE stream for a normal file
  + *			the gzFile pointer when reading is via libz
  + *			the BZFILE pointer when reading is via libbz2
  + * @param frtype	FR_PLAIN, FR_LIBZ, or FR_LIBBZ2
  + * @param printname	the file name if it is to be printed for each match
  + *			or NULL if the file name is not to be printed
  + *			it cannot be NULL if filenames[_nomatch]_only is set
  + * @return		0: at least one match, 1: no match, 2: read error (bz2)
  + */
   static int
   pcregrep(void *handle, int frtype, char *printname)
   {
  @@ -592,28 +560,27 @@
       size_t bufflength;
       BOOL endhyphenpending = FALSE;
       FILE *in = NULL;                    /* Ensure initialized */
  -    
  +
   #ifdef SUPPORT_LIBZ
       gzFile ingz = NULL;
   #endif
  -    
  +
   #ifdef SUPPORT_LIBBZ2
       BZFILE *inbz2 = NULL;
   #endif
  -    
  -    
  +
  +
       /* Do the first read into the start of the buffer and set up the pointer to end
       of what we have. In the case of libz, a non-zipped .gz file will be read as a
       plain file. However, if a .bz2 file isn't actually bzipped, the first read will
       fail. */
  -    
   #ifdef SUPPORT_LIBZ
       if (frtype == FR_LIBZ) {
         ingz = (gzFile)handle;
         bufflength = gzread (ingz, buffer, 3*MBUFTHIRD);
       } else
   #endif
  -    
  +
   #ifdef SUPPORT_LIBBZ2
       if (frtype == FR_LIBBZ2) {
         inbz2 = (BZFILE *)handle;
  @@ -622,19 +589,18 @@
       }                                    /* without the cast it is unsigned. */
       else
   #endif
  -    
  +
       {
         in = (FILE *)handle;
         bufflength = fread(buffer, 1, 3*MBUFTHIRD, in);
       }
  -    
  +
       endptr = buffer + bufflength;
  -    
  +
       /* Loop while the current pointer is not at the end of the file. For large
       files, endptr will be at the end of the buffer when we are in the middle of the
       file, but ptr will never get there, because as soon as it gets over 2/3 of the
       way, the buffer is shifted left and re-filled. */
  -    
       while (ptr < endptr) {
         int i, endlinelength;
         int mrc = 0;
  @@ -642,27 +608,25 @@
         char *matchptr = ptr;
         const char *t = ptr;
         size_t length, linelength;
  -    
  +
         /* At this point, ptr is at the start of a line. We need to find the length
  -      of the subject string to pass to pcre_exec(). In multiline mode, it is the
  +      of the subject string to pass to mireRegexec(). In multiline mode, it is the
         length remainder of the data in the buffer. Otherwise, it is the length of
         the next line. After matching, we always advance by the length of the next
         line. In multiline mode the PCRE_FIRSTLINE option is used for compiling, so
         that any match is constrained to be in the first line. */
  -    
         t = end_of_line(t, endptr, &endlinelength);
         linelength = t - ptr - endlinelength;
         length = multiline? (size_t)(endptr - ptr) : linelength;
  -    
  +
         /* Extra processing for Jeffrey Friedl's debugging. */
  -    
   #ifdef JFRIEDL_DEBUG
         if (jfriedl_XT || jfriedl_XR) {
             #include <sys/time.h>
             #include <time.h>
             struct timeval start_time, end_time;
             struct timezone dummy;
  -    
  +
             if (jfriedl_XT) {
                 unsigned long newlen = length * jfriedl_XT + strlen(jfriedl_prefix) + strlen(jfriedl_postfix);
                 const char *orig = ptr;
  @@ -680,42 +644,45 @@
                 strcpy(endptr, jfriedl_postfix); endptr += strlen(jfriedl_postfix);
                 length = newlen;
             }
  -    
  +
             if (gettimeofday(&start_time, &dummy) != 0)
                     perror("bad gettimeofday");
  -    
  +
             for (i = 0; i < jfriedl_XR; i++) {
                 miRE mire = pattern_list;
  -              match = (pcre_exec(mire->pcre, mire->hints, ptr, length, 0, 0, offsets, 99) >= 0);
  +              /* XXX save offsets for use by pcre_exec. */
  +              mire->offsets = offsets;
  +              mire->noffsets = 99;
  +              match = (mireRegexec(mire, ptr, length) >= 0);
             }
  -    
  +
             if (gettimeofday(&end_time, &dummy) != 0)
                     perror("bad gettimeofday");
  -    
  +
             double delta = ((end_time.tv_sec + (end_time.tv_usec / 1000000.0))
                             -
                             (start_time.tv_sec + (start_time.tv_usec / 1000000.0)));
  -    
  +
             printf("%s TIMER[%.4f]\n", match ? "MATCH" : "FAIL", delta);
             return 0;
       }
   #endif
  -    
  +
         /* We come back here after a match when the -o option (only_matching) is set,
         in order to find any further matches in the same line. */
  -    
  -      ONLY_MATCHING_RESTART:
  -    
  +ONLY_MATCHING_RESTART:
  +
         /* Run through all the patterns until one matches. Note that we don't include
         the final newline in the subject string. */
  -    
         for (i = 0; i < pattern_count; i++) {
           miRE mire = pattern_list + i;
  -        mrc = pcre_exec(mire->pcre, mire->hints, matchptr, length, 0, 0,
  -          offsets, 99);
  +        /* XXX save offsets for use by pcre_exec. */
  +        mire->offsets = offsets;
  +        mire->noffsets = 99;
  +        mrc = mireRegexec(mire, matchptr, length);
           if (mrc >= 0) { match = TRUE; break; }
           if (mrc != PCRE_ERROR_NOMATCH) {
  -          fprintf(stderr, "pcregrep: pcre_exec() error %d while matching ", mrc);
  +          fprintf(stderr, "pcregrep: mireRegexec() error %d while matching ", mrc);
             if (pattern_count > 1) fprintf(stderr, "pattern number %d to ", i+1);
             fprintf(stderr, "this line:\n");
             fwrite(matchptr, 1, linelength, stderr);  /* In case binary zero included */
  @@ -735,32 +702,27 @@
             break;
           }
         }
  -    
  +
         /* If it's a match or a not-match (as required), do what's wanted. */
  -    
         if (match != invert) {
           BOOL hyphenprinted = FALSE;
  -    
  +
           /* We've failed if we want a file that doesn't have any matches. */
  -    
           if (filenames == FN_NOMATCH_ONLY) return 1;
  -    
  +
           /* Just count if just counting is wanted. */
  -    
           if (count_only) count++;
  -    
  +
           /* If all we want is a file name, there is no need to scan any more lines
           in the file. */
  -    
           else if (filenames == FN_ONLY) {
             fprintf(stdout, "%s\n", printname);
             return 0;
           }
  -    
  +
           /* Likewise, if all we want is a yes/no answer. */
  -    
           else if (quiet) return 0;
  -    
  +
           /* The --only-matching option prints just the substring that matched, and
           the --file-offsets and --line-offsets options output offsets for the
           matching substring (they both force --only-matching). None of these options
  @@ -768,7 +730,6 @@
           back to look for further matches in the same line. If we are in invert
           mode, however, nothing is printed - this could be still useful because the
           return code is set. */
  -    
           else if (only_matching) {
             if (!invert) {
               if (printname != NULL) fprintf(stdout, "%s:", printname);
  @@ -788,29 +749,26 @@
               goto ONLY_MATCHING_RESTART;
             }
           }
  -    
  +
           /* This is the default case when none of the above options is set. We print
           the matching lines(s), possibly preceded and/or followed by other lines of
           context. */
  -    
           else {
             /* See if there is a requirement to print some "after" lines from a
             previous match. We never print any overlaps. */
  -    
             if (after_context > 0 && lastmatchnumber > 0) {
               int ellength;
               int linecount = 0;
               const char *p = lastmatchrestart;
  -    
  +
               while (p < ptr && linecount < after_context) {
                 p = end_of_line(p, ptr, &ellength);
                 linecount++;
               }
  -    
  +
               /* It is important to advance lastmatchrestart during this printing so
               that it interacts correctly with any "before" printing below. Print
               each line's data using fwrite() in case there are binary zeroes. */
  -    
               while (lastmatchrestart < p) {
                 const char *pp = lastmatchrestart;
                 if (printname != NULL) fprintf(stdout, "%s-", printname);
  @@ -821,32 +779,30 @@
               }
               if (lastmatchrestart != ptr) hyphenpending = TRUE;
             }
  -    
  +
             /* If there were non-contiguous lines printed above, insert hyphens. */
  -    
             if (hyphenpending) {
               fprintf(stdout, "--\n");
               hyphenpending = FALSE;
               hyphenprinted = TRUE;
             }
  -    
  +
             /* See if there is a requirement to print some "before" lines for this
             match. Again, don't print overlaps. */
  -    
             if (before_context > 0) {
               int linecount = 0;
               const char *p = ptr;
  -    
  +
               while (p > buffer && (lastmatchnumber == 0 || p > lastmatchrestart) &&
                      linecount < before_context)
               {
                 linecount++;
                 p = previous_line(p, buffer);
               }
  -    
  +
               if (lastmatchnumber > 0 && p > lastmatchrestart && !hyphenprinted)
                 fprintf(stdout, "--\n");
  -    
  +
               while (p < ptr) {
                 int ellength;
                 const char *pp = p;
  @@ -857,22 +813,20 @@
                 p = pp;
               }
             }
  -    
  +
             /* Now print the matching line(s); ensure we set hyphenpending at the end
             of the file if any context lines are being output. */
  -    
             if (after_context > 0 || before_context > 0)
               endhyphenpending = TRUE;
  -    
  +
             if (printname != NULL) fprintf(stdout, "%s:", printname);
             if (number) fprintf(stdout, "%d:", linenumber);
  -    
  +
             /* In multiline mode, we want to print to the end of the line in which
             the end of the matched string is found, so we adjust linelength and the
             line number appropriately, but only when there actually was a match
             (invert not set). Because the PCRE_FIRSTLINE option is set, the start of
             the match will always be before the first newline sequence. */
  -    
             if (multiline) {
               int ellength;
               const char *endmatch = ptr;
  @@ -887,14 +841,12 @@
               endmatch = end_of_line(endmatch, endptr, &ellength);
               linelength = endmatch - ptr - ellength;
             }
  -    
             /*** NOTE: Use only fwrite() to output the data line, so that binary
             zeroes are treated as just another data character. */
  -    
  +
             /* This extra option, for Jeffrey Friedl's debugging requirements,
             replaces the matched string, or a specific captured string if it exists,
             with X. When this happens, colouring is ignored. */
  -    
   #ifdef JFRIEDL_DEBUG
             if (S_arg >= 0 && S_arg < mrc) {
               int first = S_arg * 2;
  @@ -904,9 +856,8 @@
               fwrite(ptr + offsets[last], 1, linelength - offsets[last], stdout);
             } else
   #endif
  -    
  +
             /* We have to split the line(s) up if colouring. */
  -    
             if (do_colour) {
               fwrite(ptr, 1, offsets[0], stdout);
               fprintf(stdout, "%c[%sm", 0x1b, colour_string);
  @@ -917,22 +868,19 @@
             }
             else fwrite(ptr, 1, linelength + endlinelength, stdout);
           }
  -    
  +
           /* End of doing what has to be done for a match */
  -    
           rc = 0;    /* Had some success */
  -    
  +
           /* Remember where the last match happened for after_context. We remember
           where we are about to restart, and that line's number. */
  -    
           lastmatchrestart = ptr + linelength + endlinelength;
           lastmatchnumber = linenumber + 1;
         }
  -    
  +
         /* For a match in multiline inverted mode (which of course did not cause
         anything to be printed), we have to move on to the end of the match before
         proceeding. */
  -    
         if (multiline && invert && match) {
           int ellength;
           const char *endmatch = ptr + offsets[1];
  @@ -944,19 +892,17 @@
           endmatch = end_of_line(endmatch, endptr, &ellength);
           linelength = endmatch - ptr - ellength;
         }
  -    
  +
         /* Advance to after the newline and increment the line number. The file
         offset to the current line is maintained in filepos. */
  -    
         ptr += linelength + endlinelength;
         filepos += linelength + endlinelength;
         linenumber++;
  -    
  +
         /* If we haven't yet reached the end of the file (the buffer is full), and
         the current point is in the top 1/3 of the buffer, slide the buffer down by
         1/3 and refill it. Before we do this, if some unprinted "after" lines are
         about to be lost, print them. */
  -    
         if (bufflength >= sizeof(buffer) && ptr > buffer + 2*MBUFTHIRD) {
           if (after_context > 0 &&
               lastmatchnumber > 0 &&
  @@ -965,81 +911,71 @@
             do_after_lines(lastmatchnumber, lastmatchrestart, endptr, printname);
             lastmatchnumber = 0;
           }
  -    
  +
           /* Now do the shuffle */
  -    
  +
           memmove(buffer, buffer + MBUFTHIRD, 2*MBUFTHIRD);
           ptr -= MBUFTHIRD;
  -    
  +
   #ifdef SUPPORT_LIBZ
           if (frtype == FR_LIBZ)
             bufflength = 2*MBUFTHIRD +
               gzread (ingz, buffer + 2*MBUFTHIRD, MBUFTHIRD);
           else
   #endif
  -    
  +
   #ifdef SUPPORT_LIBBZ2
           if (frtype == FR_LIBBZ2)
             bufflength = 2*MBUFTHIRD +
               BZ2_bzread(inbz2, buffer + 2*MBUFTHIRD, MBUFTHIRD);
           else
   #endif
  -    
  +
           bufflength = 2*MBUFTHIRD + fread(buffer + 2*MBUFTHIRD, 1, MBUFTHIRD, in);
  -    
  +
           endptr = buffer + bufflength;
  -    
  +
           /* Adjust any last match point */
  -    
           if (lastmatchnumber > 0) lastmatchrestart -= MBUFTHIRD;
         }
       }	/* Loop through the whole file */
  -    
  +
       /* End of file; print final "after" lines if wanted; do_after_lines sets
       hyphenpending if it prints something. */
  -    
       if (!only_matching && !count_only) {
         do_after_lines(lastmatchnumber, lastmatchrestart, endptr, printname);
         hyphenpending |= endhyphenpending;
       }
  -    
  +
       /* Print the file name if we are looking for those without matches and there
       were none. If we found a match, we won't have got this far. */
  -    
       if (filenames == FN_NOMATCH_ONLY) {
         fprintf(stdout, "%s\n", printname);
         return 0;
       }
  -    
  +
       /* Print the match count if wanted */
  -    
       if (count_only) {
         if (printname != NULL) fprintf(stdout, "%s:", printname);
         fprintf(stdout, "%d\n", count);
       }
  -    
  +
       return rc;
   }
   
   /*************************************************
  -*     Grep a file or recurse into a directory    *
  -*************************************************/
  -
  -/* Given a path name, if it's a directory, scan all the files if we are
  -recursing; if it's a file, grep it.
  -
  -Arguments:
  -  pathname          the path to investigate
  -  dir_recurse       TRUE if recursing is wanted (-r or -drecurse)
  -  only_one_at_top   TRUE if the path is the only one at toplevel
  -
  -Returns:   0 if there was at least one match
  -           1 if there were no matches
  -           2 there was some kind of error
  -
  -However, file opening failures are suppressed if "silent" is set.
  -*/
  -
  + * Grep a file or recurse into a directory.
  + *
  + * Given a path name, if it's a directory, scan all the files if we are
  + * recursing; if it's a file, grep it.
  + *
  + * @param pathname		the path to investigate
  + * @param dir_recurse		TRUE if recursing is wanted (-r or -drecurse)
  + * @param only_one_at_top	TRUE if the path is the only one at toplevel
  + * @return		0: at least one match, 1: no match, 2: read error (bz2)
  + *
  + * @note file opening failures are suppressed if "silent" is set.
  + */
   static int
   grep_or_recurse(char *pathname, BOOL dir_recurse, BOOL only_one_at_top)
   {
  @@ -1049,76 +985,71 @@
       int pathlen;
       void *handle;
       FILE *in = NULL;           /* Ensure initialized */
  -    
  +
   #ifdef SUPPORT_LIBZ
       gzFile ingz = NULL;
   #endif
  -    
  +
   #ifdef SUPPORT_LIBBZ2
       BZFILE *inbz2 = NULL;
   #endif
  -    
  +
       /* If the file name is "-" we scan stdin */
  -    
       if (strcmp(pathname, "-") == 0) {
         return pcregrep(stdin, FR_PLAIN,
           (filenames > FN_DEFAULT || (filenames == FN_DEFAULT && !only_one_at_top))?
             stdin_name : NULL);
       }
  -    
  +
       /* If the file is a directory, skip if skipping or if we are recursing, scan
       each file within it, subject to any include or exclude patterns that were set.
       The scanning code is localized so it can be made system-specific. */
  -    
       if ((sep = isdirectory(pathname)) != 0) {
         if (dee_action == dee_SKIP) return 1;
         if (dee_action == dee_RECURSE) {
           char buffer[1024];
           char *nextfile;
           directory_type *dir = opendirectory(pathname);
  -    
  +
           if (dir == NULL) {
             if (!silent)
               fprintf(stderr, "pcregrep: Failed to open directory %s: %s\n", pathname,
                 strerror(errno));
             return 2;
           }
  -    
  +
           while ((nextfile = readdirectory(dir)) != NULL) {
             int frc;
             sprintf(buffer, "%.512s%c%.128s", pathname, sep, nextfile);
  -    
  +
             if (excludeMire && mireRegexec(excludeMire, buffer, 0) != PCRE_ERROR_NOMATCH)
               continue;
  -    
  +
             if (includeMire && mireRegexec(includeMire, buffer, 0) == PCRE_ERROR_NOMATCH)
               continue;
  -    
  +
             frc = grep_or_recurse(buffer, dir_recurse, FALSE);
             if (frc > 1) rc = frc;
             else if (frc == 0 && rc == 1) rc = 0;
           }
  -    
  +
           closedirectory(dir);
           return rc;
         }
       }
  -    
  +
       /* If the file is not a directory and not a regular file, skip it if that's
       been requested. */
  -    
       else if (!isregfile(pathname) && DEE_action == DEE_SKIP) return 1;
  -    
  +
       /* Control reaches here if we have a regular file, or if we have a directory
       and recursion or skipping was not requested, or if we have anything else and
       skipping was not requested. The scan proceeds. If this is the first and only
       argument at top level, we don't show the file name, unless we are only showing
       the file name, or the filename was forced (-H). */
  -    
       pathlen = strlen(pathname);
  -    
  +
       /* Open using zlib if it is supported and the file name ends with .gz. */
  -    
   #ifdef SUPPORT_LIBZ
       if (pathlen > 3 && strcmp(pathname + pathlen - 3, ".gz") == 0) {
         ingz = gzopen(pathname, "rb");
  @@ -1132,9 +1063,8 @@
         frtype = FR_LIBZ;
       } else
   #endif
  -    
  +
       /* Otherwise open with bz2lib if it is supported and the name ends with .bz2. */
  -    
   #ifdef SUPPORT_LIBBZ2
       if (pathlen > 4 && strcmp(pathname + pathlen - 4, ".bz2") == 0) {
         inbz2 = BZ2_bzopen(pathname, "rb");
  @@ -1142,45 +1072,40 @@
         frtype = FR_LIBBZ2;
       } else
   #endif
  -    
  +
       /* Otherwise use plain fopen(). The label is so that we can come back here if
       an attempt to read a .bz2 file indicates that it really is a plain file. */
  -    
   #ifdef SUPPORT_LIBBZ2
  -    PLAIN_FILE:
  +PLAIN_FILE:
   #endif
       {
         in = fopen(pathname, "r");
         handle = (void *)in;
         frtype = FR_PLAIN;
       }
  -    
  +
       /* All the opening methods return errno when they fail. */
  -    
       if (handle == NULL) {
         if (!silent)
           fprintf(stderr, "pcregrep: Failed to open %s: %s\n", pathname,
             strerror(errno));
         return 2;
       }
  -    
  +
       /* Now grep the file */
  -    
       rc = pcregrep(handle, frtype, (filenames > FN_DEFAULT ||
         (filenames == FN_DEFAULT && !only_one_at_top))? pathname : NULL);
  -    
  +
       /* Close in an appropriate manner. */
  -    
   #ifdef SUPPORT_LIBZ
       if (frtype == FR_LIBZ)
         gzclose(ingz);
       else
   #endif
  -    
  +
       /* If it is a .bz2 file and the result is 2, it means that the first attempt to
       read failed. If the error indicates that the file isn't in fact bzipped, try
       again as a normal file. */
  -    
   #ifdef SUPPORT_LIBBZ2
       if (frtype == FR_LIBBZ2) {
         if (rc == 2) {
  @@ -1197,30 +1122,26 @@
         BZ2_bzclose(inbz2);
       } else
   #endif
  -    
  -    
  +
       fclose(in);	/* Normal file close */
  -    
  -    
  +
       return rc;	/* Pass back the yield from pcregrep(). */
   }
   
  -/* Structure for options and list of them */
  -
  +/** Structure for options and list of them */
   enum { OP_NODATA, OP_STRING, OP_OP_STRING, OP_NUMBER, OP_OP_NUMBER,
          OP_PATLIST };
   
   typedef struct option_item {
  -  int type;
  -  int one_char;
  -  void *dataptr;
  -  const char *long_name;
  -  const char *help_text;
  +    int type;
  +    int one_char;
  +    void *dataptr;
  +    const char *long_name;
  +    const char *help_text;
   } option_item;
   
   /* Options without a single-letter equivalent get a negative value. This can be
   used to identify them. */
  -
   #define N_COLOUR    (-1)
   #define N_EXCLUDE   (-2)
   #define N_HELP      (-3)
  @@ -1273,10 +1194,12 @@
     { OP_NODATA,    'x',      NULL,              "line-regex(p)", "force patterns to match only whole lines" },
     { OP_NODATA,    0,        NULL,               NULL,            NULL }
   };
  -/*************************************************
  -*                Usage function                  *
  -*************************************************/
   
  +/*************************************************
  + * Usage function.
  + * @param rc			return code
  + * @return			return code
  + */
   static int
   usage(int rc)
   {
  @@ -1292,36 +1215,35 @@
   }
   
   /*************************************************
  -*                Help function                   *
  -*************************************************/
  -
  + * Help function.
  + */
   static void
   help(void)
   {
       option_item *op;
  -    
  +
       printf("Usage: pcregrep [OPTION]... [PATTERN] [FILE1 FILE2 ...]\n");
       printf("Search for PATTERN in each FILE or standard input.\n");
       printf("PATTERN must be present if neither -e nor -f is used.\n");
       printf("\"-\" can be used as a file name to mean STDIN.\n");
  -    
  +
   #ifdef SUPPORT_LIBZ
       printf("Files whose names end in .gz are read using zlib.\n");
   #endif
  -    
  +
   #ifdef SUPPORT_LIBBZ2
       printf("Files whose names end in .bz2 are read using bzlib2.\n");
   #endif
  -    
  +
   #if defined SUPPORT_LIBZ || defined SUPPORT_LIBBZ2
       printf("Other files and the standard input are read as plain files.\n\n");
   #else
       printf("All files are read as plain files, without any interpretation.\n\n");
   #endif
  -    
  +
       printf("Example: pcregrep -i 'hello.*world' menu.h main.c\n\n");
       printf("Options:\n");
  -    
  +
       for (op = optionlist; op->one_char != 0; op++)
         {
         int n;
  @@ -1331,19 +1253,21 @@
         if (n < 1) n = 1;
         printf("%.*s%s\n", n, "                    ", op->help_text);
         }
  -    
  +
       printf("\nWhen reading patterns from a file instead of using a command line option,\n");
       printf("trailing white space is removed and blank lines are ignored.\n");
       printf("There is a maximum of %d patterns.\n", MAX_PATTERN_COUNT);
  -    
  +
       printf("\nWith no FILEs, read standard input. If fewer than two FILEs given, assume -h.\n");
       printf("Exit status is 0 if any matches, 1 if no matches, and 2 if trouble.\n");
   }
   
   /*************************************************
  -*    Handle a single-letter, no data option      *
  -*************************************************/
  -
  + * Handle a single-letter, no data option.
  + * @param letter
  + * @param options
  + * @return
  + */
   static int
   handle_option(int letter, int options)
   {
  @@ -1368,26 +1292,27 @@
       case 'v': invert = TRUE; break;
       case 'w': process_options |= PO_WORD_MATCH; break;
       case 'x': process_options |= PO_LINE_MATCH; break;
  -    
  +
       case 'V':
         fprintf(stderr, "pcregrep version %s\n", pcre_version());
         exit(0);
         break;
  -    
  +
       default:
         fprintf(stderr, "pcregrep: Unknown option -%c\n", letter);
         exit(usage(2));
       }
  -    
  +
       return options;
   }
   
   /*************************************************
  -*          Construct printed ordinal             *
  -*************************************************/
  -
  -/* This turns a number into "1st", "3rd", etc. */
  -
  + * Construct printed ordinal.
  + *
  + * This turns a number into "1st", "3rd", etc.
  + * @param n
  + * @return
  + */
   static char *
   ordin(int n)
   {
  @@ -1405,23 +1330,17 @@
   }
   
   /*************************************************
  -*          Compile a single pattern              *
  -*************************************************/
  -
  -/* When the -F option has been used, this is called for each substring.
  -Otherwise it's called for each supplied pattern.
  -
  -Arguments:
  -  pattern        the pattern string
  -  options        the PCRE options
  -  filename       the file name, or NULL for a command-line pattern
  -  count          0 if this is the only command line pattern, or
  -                 number of the command line pattern, or
  -                 linenumber for a pattern from a file
  -
  -Returns:         TRUE on success, FALSE after an error
  -*/
  -
  + * Compile a single pattern.
  + *
  + * When the -F option has been used, this is called for each substring.
  + * Otherwise it's called for each supplied pattern.
  + *
  + * @param pattern	the pattern string
  + * @param options	the PCRE options
  + * @param filename	the file name, or NULL for a command-line pattern
  + * @param count		pattern index (0 is single pattern)
  + * @return		TRUE on success, FALSE after an error
  + */
   static BOOL
   compile_single_pattern(const char *pattern, int options, char *filename, int count)
   {
  @@ -1429,14 +1348,14 @@
       char buffer[MBUFTHIRD + 16];
       const char *error;
       int errptr;
  -    
  +
       if (pattern_count >= MAX_PATTERN_COUNT) {
         fprintf(stderr, "pcregrep: Too many %spatterns (max %d)\n",
           (filename == NULL)? "command-line " : "", MAX_PATTERN_COUNT);
         return FALSE;
       }
       mire = pattern_list + pattern_count;
  -    
  +
       sprintf(buffer, "%s%.*s%s", prefix[process_options], MBUFTHIRD, pattern,
         suffix[process_options]);
       mire->pcre = pcre_compile(buffer, options, &error, &errptr, pcretables);
  @@ -1444,12 +1363,11 @@
         pattern_count++;
         return TRUE;
       }
  -    
  +
       /* Handle compile errors */
  -    
       errptr -= (int)strlen(prefix[process_options]);
       if (errptr > (int)strlen(pattern)) errptr = (int)strlen(pattern);
  -    
  +
       if (filename == NULL) {
         if (count == 0)
           fprintf(stderr, "pcregrep: Error in command-line regex "
  @@ -1461,28 +1379,22 @@
         fprintf(stderr, "pcregrep: Error in regex in line %d of %s "
           "at offset %d: %s\n", count, filename, errptr, error);
       }
  -    
  +
       return FALSE;
   }
   
   /*************************************************
  -*           Compile one supplied pattern         *
  -*************************************************/
  -
  -/* When the -F option has been used, each string may be a list of strings,
  -separated by line breaks. They will be matched literally.
  -
  -Arguments:
  -  pattern        the pattern string
  -  options        the PCRE options
  -  filename       the file name, or NULL for a command-line pattern
  -  count          0 if this is the only command line pattern, or
  -                 number of the command line pattern, or
  -                 linenumber for a pattern from a file
  -
  -Returns:         TRUE on success, FALSE after an error
  -*/
  -
  + * Compile one supplied pattern.
  + *
  + * When the -F option has been used, each string may be a list of strings,
  + * separated by line breaks. They will be matched literally.
  + *
  + * @param pattern        the pattern string
  + * @param options        the PCRE options
  + * @param filename       the file name, or NULL for a command-line pattern
  + * @param count		pattern index (0 is single pattern)
  + * @return		TRUE on success, FALSE after an error
  + */
   static BOOL
   compile_pattern(const char *pattern, int options, char *filename, int count)
   {
  @@ -1503,15 +1415,11 @@
       else return compile_single_pattern(pattern, options, filename, count);
   }
   
  -/*************************************************
  -*                Main program                    *
  -*************************************************/
  -
   /**
    * Destroy compiled patterns.
    * @param mire		pattern array
    * @param nre		no of patterns in array
  - * @return		NULL always 
  + * @return		NULL always
    */
   /*@-onlytrans@*/	/* XXX miRE array, not refcounted. */
   /*@null@*/
  @@ -1558,8 +1466,10 @@
   }
   /*@=onlytrans@*/
   
  -/* Returns 0 if something matched, 1 if nothing matched, 2 after an error. */
  -
  +/*************************************************
  + * Main program.
  + * @return		0: match found, 1: no match, 2: error.
  + */
   int
   main(int argc, char **argv)
   {
  @@ -1573,11 +1483,10 @@
       const char *locale_from = "--locale";
       const char *error;
       int xx;
  -    
  +
       /* Set the default line ending value from the default in the PCRE library;
       "lf", "cr", "crlf", and "any" are supported. Anything else is treated as "lf".
       */
  -    
       (void)pcre_config(PCRE_CONFIG_NEWLINE, &i);
       switch(i) {
       default:	newline = (char *)"lf";		break;
  @@ -1586,45 +1495,41 @@
       case -1:	newline = (char *)"any";	break;
       case -2:	newline = (char *)"anycrlf";	break;
       }
  -    
  +
       /* Process the options */
  -    
       for (i = 1; i < argc; i++) {
         option_item *op = NULL;
         char *option_data = (char *)"";    /* default to keep compiler happy */
         BOOL longop;
         BOOL longopwasequals = FALSE;
  -    
  +
         if (argv[i][0] != '-') break;
  -    
  +
         /* If we hit an argument that is just "-", it may be a reference to STDIN,
         but only if we have previously had -e or -f to define the patterns. */
  -    
         if (argv[i][1] == 0) {
           if (pattern_filename != NULL || pattern_count > 0) break;
             else exit(usage(2));
         }
  -    
  +
         /* Handle a long name option, or -- to terminate the options */
  -    
         if (argv[i][1] == '-') {
           char *arg = argv[i] + 2;
           char *argequals = strchr(arg, '=');
  -    
  +
           if (*arg == 0) {	/* -- terminates options */
             i++;
             break;                /* out of the options-handling loop */
           }
  -    
  +
           longop = TRUE;
  -    
  +
           /* Some long options have data that follows after =, for example file=name.
           Some options have variations in the long name spelling: specifically, we
           allow "regexp" because GNU grep allows it, though I personally go along
           with Jeffrey Friedl and Larry Wall in preferring "regex" without the "p".
           These options are entered in the table as "regex(p)". No option is in both
           these categories, fortunately. */
  -    
           for (op = optionlist; op->one_char != 0; op++) {
             char *opbra = strchr(op->long_name, '(');
             char *equals = strchr(op->long_name, '=');
  @@ -1654,20 +1559,19 @@
                 break;
             }
           }
  -    
  +
           if (op->one_char == 0) {
             fprintf(stderr, "pcregrep: Unknown option %s\n", argv[i]);
             exit(usage(2));
           }
         }
  -    
  +
         /* Jeffrey Friedl's debugging harness uses these additional options which
         are not in the right form for putting in the option table because they use
         only one hyphen, yet are more than one character long. By putting them
         separately here, they will not get displayed as part of the help() output,
         but I don't think Jeffrey will care about that. */
  -    
  -    #ifdef JFRIEDL_DEBUG
  +#ifdef JFRIEDL_DEBUG
         else if (strcmp(argv[i], "-pre") == 0) {
                 jfriedl_prefix = argv[++i];
                 continue;
  @@ -1681,11 +1585,10 @@
                 sscanf(argv[++i], "%d", &jfriedl_XR);
                 continue;
         }
  -    #endif
  -    
  +#endif
  +
         /* One-char options; many that have no data may be in a single argument; we
         continue till we hit the last one or one that needs data. */
  -    
         else {
           char *s = argv[i] + 1;
           longop = FALSE;
  @@ -1705,21 +1608,19 @@
             pcre_options = handle_option(*s++, pcre_options);
           }
         }
  -    
  +
         /* At this point we should have op pointing to a matched option. If the type
         is NO_DATA, it means that there is no data, and the option might set
         something in the PCRE options. */
  -    
         if (op->type == OP_NODATA) {
           pcre_options = handle_option(op->one_char, pcre_options);
           continue;
         }
  -    
  +
         /* If the option type is OP_OP_STRING or OP_OP_NUMBER, it's an option that
         either has a value or defaults to something. It cannot have data in a
         separate item. At the moment, the only such options are "colo(u)r" and
         Jeffrey Friedl's special -S debugging option. */
  -    
         if (*option_data == 0 &&
             (op->type == OP_OP_STRING || op->type == OP_OP_NUMBER))
         {
  @@ -1727,17 +1628,17 @@
           case N_COLOUR:
             colour_option = (char *)"auto";
             break;
  -    #ifdef JFRIEDL_DEBUG
  +#ifdef JFRIEDL_DEBUG
           case 'S':
             S_arg = 0;
             break;
  -    #endif
  +#endif
           }
           continue;
         }
  -    
  +
         /* Otherwise, find the data string for the option. */
  -    
  +
         if (*option_data == 0) {
           if (i >= argc - 1 || longopwasequals) {
             fprintf(stderr, "pcregrep: Data missing after %s\n", argv[i]);
  @@ -1745,16 +1646,14 @@
           }
           option_data = argv[++i];
         }
  -    
  +
         /* If the option type is OP_PATLIST, it's the -e option, which can be called
         multiple times to create a list of patterns. */
  -    
         if (op->type == OP_PATLIST) {
   	xx = argvAdd(&patterns, option_data);
         }
  -    
  +
         /* Otherwise, deal with single string or numeric data values. */
  -    
         else if (op->type != OP_NUMBER && op->type != OP_OP_NUMBER) {
           *((char **)op->dataptr) = option_data;
         }
  @@ -1777,18 +1676,16 @@
           *((int *)op->dataptr) = n;
         }
       }
  -    
  +
       /* Options have been decoded. If -C was used, its value is used as a default
       for -A and -B. */
  -    
       if (both_context > 0) {
         if (after_context == 0) after_context = both_context;
         if (before_context == 0) before_context = both_context;
       }
  -    
  +
       /* Only one of --only-matching, --file-offsets, or --line-offsets is permitted.
       However, the latter two set the only_matching flag. */
  -    
       if ((only_matching && (file_offsets || line_offsets)) ||
           (file_offsets && line_offsets))
       {
  @@ -1796,25 +1693,23 @@
           "and/or --line-offsets\n");
         exit(usage(2));
       }
  -    
  +
       if (file_offsets || line_offsets) only_matching = TRUE;
  -    
  +
       /* If a locale has not been provided as an option, see if the LC_CTYPE or
       LC_ALL environment variable is set, and if so, use it. */
  -    
       if (locale == NULL) {
         locale = getenv("LC_ALL");
         locale_from = "LCC_ALL";
       }
  -    
  +
       if (locale == NULL) {
         locale = getenv("LC_CTYPE");
         locale_from = "LC_CTYPE";
       }
  -    
  +
       /* If a locale has been provided, set it, and generate the tables the PCRE
       needs. Otherwise, pcretables==NULL, which causes the use of default tables. */
  -    
       if (locale != NULL) {
         if (setlocale(LC_CTYPE, locale) == NULL) {
           fprintf(stderr, "pcregrep: Failed to set locale %s (obtained from %s)\n",
  @@ -1823,9 +1718,8 @@
         }
         pcretables = pcre_maketables();
       }
  -    
  +
       /* Sort out colouring */
  -    
       if (colour_option != NULL && strcmp(colour_option, "never") != 0) {
         if (strcmp(colour_option, "always") == 0) do_colour = TRUE;
         else if (strcmp(colour_option, "auto") == 0) do_colour = is_stdout_tty();
  @@ -1840,9 +1734,8 @@
           if (cs != NULL) colour_string = cs;
         }
       }
  -    
  +
       /* Interpret the newline type; the default settings are Unix-like. */
  -    
       if (strcmp(newline, "cr") == 0 || strcmp(newline, "CR") == 0) {
         pcre_options |= PCRE_NEWLINE_CR;
         endlinetype = EL_CR;
  @@ -1867,9 +1760,8 @@
         fprintf(stderr, "pcregrep: Invalid newline specifier \"%s\"\n", newline);
         return 2;
       }
  -    
  +
       /* Interpret the text values for -d and -D */
  -    
       if (dee_option != NULL) {
         if (strcmp(dee_option, "read") == 0) dee_action = dee_READ;
         else if (strcmp(dee_option, "recurse") == 0) dee_action = dee_RECURSE;
  @@ -1879,7 +1771,7 @@
           return 2;
         }
       }
  -    
  +
       if (DEE_option != NULL) {
         if (strcmp(DEE_option, "read") == 0) DEE_action = DEE_READ;
         else if (strcmp(DEE_option, "skip") == 0) DEE_action = DEE_SKIP;
  @@ -1888,10 +1780,9 @@
           return 2;
         }
       }
  -    
  +
       /* Check the values for Jeffrey Friedl's debugging options. */
  -    
  -    #ifdef JFRIEDL_DEBUG
  +#ifdef JFRIEDL_DEBUG
       if (S_arg > 9) {
         fprintf(stderr, "pcregrep: bad value for -S option\n");
         return 2;
  @@ -1900,11 +1791,11 @@
         if (jfriedl_XT == 0) jfriedl_XT = 1;
         if (jfriedl_XR == 0) jfriedl_XR = 1;
       }
  -    #endif
  -    
  +#endif
  +
       /* Get memory to store the pattern and hints lists. */
       pattern_list = xcalloc(MAX_PATTERN_COUNT, sizeof(*pattern_list));
  -    
  +
       /* If no patterns were provided by -e, and there is no file provided by -f,
       the first argument is the one and only pattern, and it must exist. */
       npatterns = argvCount(patterns);
  @@ -1912,7 +1803,7 @@
         if (i >= argc) return usage(2);
   	xx = argvAdd(&patterns, argv[i++]);
       }
  -    
  +
       /* Compile the patterns that were provided on the command line, either by
       multiple uses of -e or as a single unkeyed pattern. */
       npatterns = argvCount(patterns);
  @@ -1921,14 +1812,14 @@
              (j == 0 && npatterns == 1)? 0 : j + 1))
           goto errorexit;
       }
  -    
  +
       /* Compile the regular expressions that are provided in a file. */
       if (pattern_filename != NULL) {
         int linenumber = 0;
         FILE *f;
         char *filename;
         char buffer[MBUFTHIRD];
  -    
  +
         if (strcmp(pattern_filename, "-") == 0) {
           f = stdin;
           filename = stdin_name;
  @@ -1941,7 +1832,7 @@
           }
           filename = pattern_filename;
         }
  -    
  +
         while (fgets(buffer, MBUFTHIRD, f) != NULL) {
           char *s = buffer + (int)strlen(buffer);
           while (s > buffer && isspace((unsigned char)(s[-1]))) s--;
  @@ -1951,10 +1842,10 @@
           if (!compile_pattern(buffer, pcre_options, filename, linenumber))
             goto errorexit;
         }
  -    
  +
         if (f != stdin) fclose(f);
       }
  -    
  +
       /* Study the regular expressions, as we will be running them many times */
       for (j = 0; j < pattern_count; j++) {
         miRE mire = pattern_list + j;
  @@ -1966,9 +1857,8 @@
           goto errorexit;
         }
       }
  -    
  +
       /* If there are include or exclude patterns, compile them. */
  -    
       if (exclude_pattern != NULL) {
         excludeMire = mireNew(RPMMIRE_PCRE, 0);
         /* XXX save locale tables for use by pcre_compile2. */
  @@ -1980,7 +1870,7 @@
           goto errorexit;
         }
       }
  -    
  +
       if (include_pattern != NULL) {
         includeMire = mireNew(RPMMIRE_PCRE, 0);
         /* XXX save locale tables for use by pcre_compile2. */
  @@ -1992,28 +1882,26 @@
           goto errorexit;
         }
       }
  -    
  +
       /* If there are no further arguments, do the business on stdin and exit. */
  -    
       if (i >= argc) {
         rc = pcregrep(stdin, FR_PLAIN, (filenames > FN_DEFAULT)? stdin_name : NULL);
         goto exit;
       }
  -    
  +
       /* Otherwise, work through the remaining arguments as files or directories.
       Pass in the fact that there is only one argument at top level - this suppresses
       the file name if the argument is not a directory and filenames are not
       otherwise forced. */
  -    
       only_one_at_top = i == argc - 1;   /* Catch initial value of i */
  -    
  +
       for (; i < argc; i++) {
         int frc = grep_or_recurse(argv[i], dee_action == dee_RECURSE,
           only_one_at_top);
         if (frc > 1) rc = frc;
           else if (frc == 0 && rc == 1) rc = 0;
       }
  -    
  +
   exit:
       includeMire = mireFree(includeMire);
       excludeMire = mireFree(excludeMire);
  @@ -2022,7 +1910,7 @@
       patterns = argvFree(patterns);
   
       return rc;
  -    
  +
   errorexit:
       rc = 2;
       goto exit;
  @@ .
Received on Sun Feb 10 20:40:10 2008
Driven by Jeff Johnson and the RPM project team.
Hosted by OpenPKG and Ralf S. Engelschall.
Powered by FreeBSD and OpenPKG.