/[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 90 by nigel, Sat Feb 24 21:41:27 2007 UTC revision 91 by nigel, Sat Feb 24 21:41:34 2007 UTC
# Line 56  POSSIBILITY OF SUCH DAMAGE. Line 56  POSSIBILITY OF SUCH DAMAGE.
56    
57  typedef int BOOL;  typedef int BOOL;
58    
59  #define VERSION "4.2 09-Jan-2006"  #define VERSION "4.3 01-Jun-2006"
60  #define MAX_PATTERN_COUNT 100  #define MAX_PATTERN_COUNT 100
61    
62  #if BUFSIZ > 8192  #if BUFSIZ > 8192
# Line 100  static const char *jfriedl_prefix = ""; Line 100  static const char *jfriedl_prefix = "";
100  static const char *jfriedl_postfix = "";  static const char *jfriedl_postfix = "";
101  #endif  #endif
102    
103    static int  endlinebyte = '\n';     /* Last byte of endline sequence */
104    static int  endlineextra = 0;       /* Extra bytes for endline sequence */
105    
106  static char *colour_string = (char *)"1;31";  static char *colour_string = (char *)"1;31";
107  static char *colour_option = NULL;  static char *colour_option = NULL;
108  static char *dee_option = NULL;  static char *dee_option = NULL;
109  static char *DEE_option = NULL;  static char *DEE_option = NULL;
110    static char *newline = NULL;
111  static char *pattern_filename = NULL;  static char *pattern_filename = NULL;
112  static char *stdin_name = (char *)"(standard input)";  static char *stdin_name = (char *)"(standard input)";
113  static char *locale = NULL;  static char *locale = NULL;
# Line 185  static option_item optionlist[] = { Line 189  static option_item optionlist[] = {
189    { OP_STRING,    N_LABEL,  &stdin_name,       "label=name",    "set name for standard input" },    { OP_STRING,    N_LABEL,  &stdin_name,       "label=name",    "set name for standard input" },
190    { OP_STRING,    N_LOCALE, &locale,           "locale=locale", "use the named locale" },    { OP_STRING,    N_LOCALE, &locale,           "locale=locale", "use the named locale" },
191    { OP_NODATA,    'M',      NULL,              "multiline",     "run in multiline mode" },    { OP_NODATA,    'M',      NULL,              "multiline",     "run in multiline mode" },
192      { OP_STRING,    'N',      &newline,          "newline=type",  "specify newline type (CR, LR, CRLF)" },
193    { OP_NODATA,    'n',      NULL,              "line-number",   "print line number with output lines" },    { OP_NODATA,    'n',      NULL,              "line-number",   "print line number with output lines" },
194    { OP_NODATA,    'o',      NULL,              "only-matching", "show only the part of the line that matched" },    { OP_NODATA,    'o',      NULL,              "only-matching", "show only the part of the line that matched" },
195    { OP_NODATA,    'q',      NULL,              "quiet",         "suppress output, just set return code" },    { OP_NODATA,    'q',      NULL,              "quiet",         "suppress output, just set return code" },
# Line 493  if (after_context > 0 && lastmatchnumber Line 498  if (after_context > 0 && lastmatchnumber
498      char *pp = lastmatchrestart;      char *pp = lastmatchrestart;
499      if (printname != NULL) fprintf(stdout, "%s-", printname);      if (printname != NULL) fprintf(stdout, "%s-", printname);
500      if (number) fprintf(stdout, "%d-", lastmatchnumber++);      if (number) fprintf(stdout, "%d-", lastmatchnumber++);
501      while (*pp != '\n') pp++;      while (*pp != endlinebyte) pp++;
502      fwrite(lastmatchrestart, 1, pp - lastmatchrestart + 1, stdout);      fwrite(lastmatchrestart, 1, pp - lastmatchrestart + (1 + endlineextra),
503          stdout);
504      lastmatchrestart = pp + 1;      lastmatchrestart = pp + 1;
505      }      }
506    hyphenpending = TRUE;    hyphenpending = TRUE;
# Line 566  while (ptr < endptr) Line 572  while (ptr < endptr)
572    that any match is constrained to be in the first line. */    that any match is constrained to be in the first line. */
573    
574    linelength = 0;    linelength = 0;
575    while (t < endptr && *t++ != '\n') linelength++;    while (t < endptr && *t++ != endlinebyte) linelength++;
576    length = multiline? endptr - ptr : linelength;    length = multiline? endptr - ptr : linelength;
577    
578    
# Line 705  while (ptr < endptr) Line 711  while (ptr < endptr)
711    
712          while (p < ptr && linecount < after_context)          while (p < ptr && linecount < after_context)
713            {            {
714            while (*p != '\n') p++;            while (*p != endlinebyte) p++;
715            p++;            p++;
716            linecount++;            linecount++;
717            }            }
# Line 719  while (ptr < endptr) Line 725  while (ptr < endptr)
725            char *pp = lastmatchrestart;            char *pp = lastmatchrestart;
726            if (printname != NULL) fprintf(stdout, "%s-", printname);            if (printname != NULL) fprintf(stdout, "%s-", printname);
727            if (number) fprintf(stdout, "%d-", lastmatchnumber++);            if (number) fprintf(stdout, "%d-", lastmatchnumber++);
728            while (*pp != '\n') pp++;            while (*pp != endlinebyte) pp++;
729            fwrite(lastmatchrestart, 1, pp - lastmatchrestart + 1, stdout);            fwrite(lastmatchrestart, 1, pp - lastmatchrestart +
730                (1 + endlineextra), stdout);
731            lastmatchrestart = pp + 1;            lastmatchrestart = pp + 1;
732            }            }
733          if (lastmatchrestart != ptr) hyphenpending = TRUE;          if (lastmatchrestart != ptr) hyphenpending = TRUE;
# Line 748  while (ptr < endptr) Line 755  while (ptr < endptr)
755            {            {
756            linecount++;            linecount++;
757            p--;            p--;
758            while (p > buffer && p[-1] != '\n') p--;            while (p > buffer && p[-1] != endlinebyte) p--;
759            }            }
760    
761          if (lastmatchnumber > 0 && p > lastmatchrestart && !hyphenprinted)          if (lastmatchnumber > 0 && p > lastmatchrestart && !hyphenprinted)
# Line 759  while (ptr < endptr) Line 766  while (ptr < endptr)
766            char *pp = p;            char *pp = p;
767            if (printname != NULL) fprintf(stdout, "%s-", printname);            if (printname != NULL) fprintf(stdout, "%s-", printname);
768            if (number) fprintf(stdout, "%d-", linenumber - linecount--);            if (number) fprintf(stdout, "%d-", linenumber - linecount--);
769            while (*pp != '\n') pp++;            while (*pp != endlinebyte) pp++;
770            fwrite(p, 1, pp - p + 1, stdout);   /* In case binary zero */            fwrite(p, 1, pp - p + (1 + endlineextra), stdout);
771            p = pp + 1;            p = pp + 1;
772            }            }
773          }          }
# Line 777  while (ptr < endptr) Line 784  while (ptr < endptr)
784        /* 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
785        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
786        line number appropriately. Because the PCRE_FIRSTLINE option is set, the        line number appropriately. Because the PCRE_FIRSTLINE option is set, the
787        start of the match will always be before the first \n character. */        start of the match will always be before the first newline sequence. */
788    
789        if (multiline)        if (multiline)
790          {          {
791          char *endmatch = ptr + offsets[1];          char *endmatch = ptr + offsets[1];
792          t = ptr;          t = ptr;
793          while (t < endmatch) { if (*t++ == '\n') linenumber++; }          while (t < endmatch) { if (*t++ == endlinebyte) linenumber++; }
794          while (endmatch < endptr && *endmatch != '\n') endmatch++;          while (endmatch < endptr && *endmatch != endlinebyte) endmatch++;
795          linelength = endmatch - ptr;          linelength = endmatch - ptr;
796          }          }
797    
# Line 1206  return FALSE; Line 1213  return FALSE;
1213  *************************************************/  *************************************************/
1214    
1215  /* When the -F option has been used, each string may be a list of strings,  /* When the -F option has been used, each string may be a list of strings,
1216  separated by newlines. They will be matched literally.  separated by line breaks. They will be matched literally.
1217    
1218  Arguments:  Arguments:
1219    pattern        the pattern string    pattern        the pattern string
# Line 1227  if ((process_options & PO_FIXED_STRINGS) Line 1234  if ((process_options & PO_FIXED_STRINGS)
1234    char buffer[MBUFTHIRD];    char buffer[MBUFTHIRD];
1235    for(;;)    for(;;)
1236      {      {
1237      char *p = strchr(pattern, '\n');      char *p = strchr(pattern, endlinebyte);
1238      if (p == NULL)      if (p == NULL)
1239        return compile_single_pattern(pattern, options, filename, count);        return compile_single_pattern(pattern, options, filename, count);
1240      sprintf(buffer, "%.*s", p - pattern, pattern);      sprintf(buffer, "%.*s", p - pattern - endlineextra, pattern);
1241      pattern = p + 1;      pattern = p + 1;
1242      if (!compile_single_pattern(buffer, options, filename, count))      if (!compile_single_pattern(buffer, options, filename, count))
1243        return FALSE;        return FALSE;
# Line 1260  char *patterns[MAX_PATTERN_COUNT]; Line 1267  char *patterns[MAX_PATTERN_COUNT];
1267  const char *locale_from = "--locale";  const char *locale_from = "--locale";
1268  const char *error;  const char *error;
1269    
1270    /* Set the default line ending value from the default in the PCRE library. */
1271    
1272    (void)pcre_config(PCRE_CONFIG_NEWLINE, &i);
1273    switch(i)
1274      {
1275      default:                 newline = (char *)"lf"; break;
1276      case '\r':               newline = (char *)"cr"; break;
1277      case ('\r' << 8) | '\n': newline = (char *)"crlf"; break;
1278      }
1279    
1280  /* Process the options */  /* Process the options */
1281    
1282  for (i = 1; i < argc; i++)  for (i = 1; i < argc; i++)
# Line 1543  if (colour_option != NULL && strcmp(colo Line 1560  if (colour_option != NULL && strcmp(colo
1560      }      }
1561    }    }
1562    
1563    /* Interpret the newline type; the default settings are Unix-like. */
1564    
1565    if (strcmp(newline, "cr") == 0 || strcmp(newline, "CR") == 0)
1566      {
1567      pcre_options |= PCRE_NEWLINE_CR;
1568      endlinebyte = '\r';
1569      }
1570    else if (strcmp(newline, "lf") == 0 || strcmp(newline, "LF") == 0)
1571      {
1572      pcre_options |= PCRE_NEWLINE_LF;
1573      }
1574    else if (strcmp(newline, "crlf") == 0 || strcmp(newline, "CRLF") == 0)
1575      {
1576      pcre_options |= PCRE_NEWLINE_CRLF;
1577      endlineextra = 1;
1578      }
1579    else
1580      {
1581      fprintf(stderr, "pcregrep: Invalid newline specifier \"%s\"\n", newline);
1582      return 2;
1583      }
1584    
1585  /* Interpret the text values for -d and -D */  /* Interpret the text values for -d and -D */
1586    
1587  if (dee_option != NULL)  if (dee_option != NULL)

Legend:
Removed from v.90  
changed lines
  Added in v.91

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12