|
|
40d7c1 |
diff --git a/doc/grep.texi b/doc/grep.texi
|
|
|
40d7c1 |
index e3495bb..41f4fa5 100644
|
|
|
40d7c1 |
--- a/doc/grep.texi
|
|
|
40d7c1 |
+++ b/doc/grep.texi
|
|
|
40d7c1 |
@@ -596,13 +596,13 @@ If a file's allocation metadata,
|
|
|
40d7c1 |
or if its data read before a line is selected for output,
|
|
|
40d7c1 |
indicate that the file contains binary data,
|
|
|
40d7c1 |
assume that the file is of type @var{type}.
|
|
|
40d7c1 |
-Non-text bytes indicate binary data; these are either data bytes
|
|
|
40d7c1 |
-improperly encoded for the current locale, or null bytes when the
|
|
|
40d7c1 |
+Non-text bytes indicate binary data; these are either output bytes that are
|
|
|
40d7c1 |
+improperly encoded for the current locale, or null input bytes when the
|
|
|
40d7c1 |
@option{-z} (@option{--null-data}) option is not given (@pxref{Other
|
|
|
40d7c1 |
Options}).
|
|
|
40d7c1 |
|
|
|
40d7c1 |
-By default, @var{type} is @samp{binary},
|
|
|
40d7c1 |
-and @command{grep} normally outputs either
|
|
|
40d7c1 |
+By default, @var{type} is @samp{binary}, and when @command{grep}
|
|
|
40d7c1 |
+discovers that a file is binary it normally outputs either
|
|
|
40d7c1 |
a one-line message saying that a binary file matches,
|
|
|
40d7c1 |
or no message if there is no match.
|
|
|
40d7c1 |
When processing binary data, @command{grep} may treat non-text bytes
|
|
|
40d7c1 |
@@ -611,7 +611,8 @@ not match a null byte, as the null byte might be treated as a line
|
|
|
40d7c1 |
terminator even without the @option{-z} (@option{--null-data}) option.
|
|
|
40d7c1 |
|
|
|
40d7c1 |
If @var{type} is @samp{without-match},
|
|
|
40d7c1 |
-@command{grep} assumes that a binary file does not match;
|
|
|
40d7c1 |
+when @command{grep} discovers that a file is binary
|
|
|
40d7c1 |
+it assumes that the rest of the file does not match;
|
|
|
40d7c1 |
this is equivalent to the @option{-I} option.
|
|
|
40d7c1 |
|
|
|
40d7c1 |
If @var{type} is @samp{text},
|
|
|
40d7c1 |
diff --git a/src/grep.c b/src/grep.c
|
|
|
97a081 |
index 2748fd3..eac540a 100644
|
|
|
40d7c1 |
--- a/src/grep.c
|
|
|
40d7c1 |
+++ b/src/grep.c
|
|
|
40d7c1 |
@@ -377,7 +377,6 @@ bool match_icase;
|
|
|
40d7c1 |
bool match_words;
|
|
|
40d7c1 |
bool match_lines;
|
|
|
40d7c1 |
char eolbyte;
|
|
|
40d7c1 |
-enum textbin input_textbin;
|
|
|
40d7c1 |
|
|
|
40d7c1 |
static char const *matcher;
|
|
|
40d7c1 |
|
|
|
40d7c1 |
@@ -389,6 +388,10 @@ static bool omit_dot_slash;
|
|
|
40d7c1 |
static bool errseen;
|
|
|
40d7c1 |
static bool write_error_seen;
|
|
|
40d7c1 |
|
|
|
40d7c1 |
+/* True if output from the current input file has been suppressed
|
|
|
40d7c1 |
+ because an output line had an encoding error. */
|
|
|
40d7c1 |
+static bool encoding_error_output;
|
|
|
40d7c1 |
+
|
|
|
40d7c1 |
enum directories_type
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
READ_DIRECTORIES = 2,
|
|
|
40d7c1 |
@@ -481,12 +484,6 @@ clean_up_stdout (void)
|
|
|
40d7c1 |
close_stdout ();
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
|
|
|
40d7c1 |
-static bool
|
|
|
40d7c1 |
-textbin_is_binary (enum textbin textbin)
|
|
|
40d7c1 |
-{
|
|
|
40d7c1 |
- return textbin < TEXTBIN_UNKNOWN;
|
|
|
40d7c1 |
-}
|
|
|
40d7c1 |
-
|
|
|
40d7c1 |
/* The high-order bit of a byte. */
|
|
|
40d7c1 |
enum { HIBYTE = 0x80 };
|
|
|
40d7c1 |
|
|
|
40d7c1 |
@@ -551,58 +548,60 @@ skip_easy_bytes (char const *buf)
|
|
|
40d7c1 |
return p;
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
|
|
|
40d7c1 |
-/* Return the text type of data in BUF, of size SIZE.
|
|
|
40d7c1 |
+/* Return true if BUF, of size SIZE, has an encoding error.
|
|
|
40d7c1 |
BUF must be followed by at least sizeof (uword) bytes,
|
|
|
40d7c1 |
- which may be arbitrarily written to or read from. */
|
|
|
40d7c1 |
-static enum textbin
|
|
|
40d7c1 |
-buffer_textbin (char *buf, size_t size)
|
|
|
40d7c1 |
+ the first of which may be modified. */
|
|
|
40d7c1 |
+static bool
|
|
|
40d7c1 |
+buf_has_encoding_errors (char *buf, size_t size)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
- if (eolbyte && memchr (buf, '\0', size))
|
|
|
40d7c1 |
- return TEXTBIN_BINARY;
|
|
|
40d7c1 |
+ if (MB_CUR_MAX <= 1)
|
|
|
40d7c1 |
+ return false;
|
|
|
40d7c1 |
|
|
|
40d7c1 |
- if (1 < MB_CUR_MAX)
|
|
|
40d7c1 |
- {
|
|
|
40d7c1 |
- mbstate_t mbs = { 0 };
|
|
|
40d7c1 |
- size_t clen;
|
|
|
40d7c1 |
- char const *p;
|
|
|
40d7c1 |
+ mbstate_t mbs = { 0 };
|
|
|
40d7c1 |
+ size_t clen;
|
|
|
40d7c1 |
|
|
|
40d7c1 |
- buf[size] = -1;
|
|
|
40d7c1 |
- for (p = buf; (p = skip_easy_bytes (p)) < buf + size; p += clen)
|
|
|
40d7c1 |
- {
|
|
|
40d7c1 |
- clen = mbrlen (p, buf + size - p, &mbs);
|
|
|
40d7c1 |
- if ((size_t) -2 <= clen)
|
|
|
40d7c1 |
- return clen == (size_t) -2 ? TEXTBIN_UNKNOWN : TEXTBIN_BINARY;
|
|
|
40d7c1 |
- }
|
|
|
40d7c1 |
+ buf[size] = -1;
|
|
|
40d7c1 |
+ for (char const *p = buf; (p = skip_easy_bytes (p)) < buf + size; p += clen)
|
|
|
40d7c1 |
+ {
|
|
|
40d7c1 |
+ clen = mbrlen (p, buf + size - p, &mbs);
|
|
|
40d7c1 |
+ if ((size_t) -2 <= clen)
|
|
|
40d7c1 |
+ return true;
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
|
|
|
40d7c1 |
- return TEXTBIN_TEXT;
|
|
|
40d7c1 |
+ return false;
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
|
|
|
40d7c1 |
-/* Return the text type of a file. BUF, of size SIZE, is the initial
|
|
|
40d7c1 |
- buffer read from the file with descriptor FD and status ST.
|
|
|
40d7c1 |
- BUF must be followed by at least sizeof (uword) bytes,
|
|
|
40d7c1 |
+
|
|
|
40d7c1 |
+/* Return true if BUF, of size SIZE, has a null byte.
|
|
|
40d7c1 |
+ BUF must be followed by at least one byte,
|
|
|
40d7c1 |
which may be arbitrarily written to or read from. */
|
|
|
40d7c1 |
-static enum textbin
|
|
|
40d7c1 |
-file_textbin (char *buf, size_t size, int fd, struct stat const *st)
|
|
|
40d7c1 |
+static bool
|
|
|
40d7c1 |
+buf_has_nulls (char *buf, size_t size)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
- enum textbin textbin = buffer_textbin (buf, size);
|
|
|
40d7c1 |
- if (textbin_is_binary (textbin))
|
|
|
40d7c1 |
- return textbin;
|
|
|
40d7c1 |
+ buf[size] = 0;
|
|
|
40d7c1 |
+ return strlen (buf) != size;
|
|
|
40d7c1 |
+}
|
|
|
40d7c1 |
|
|
|
40d7c1 |
+/* Return true if a file is known to contain null bytes.
|
|
|
40d7c1 |
+ SIZE bytes have already been read from the file
|
|
|
40d7c1 |
+ with descriptor FD and status ST. */
|
|
|
40d7c1 |
+static bool
|
|
|
40d7c1 |
+file_must_have_nulls (size_t size, int fd, struct stat const *st)
|
|
|
40d7c1 |
+{
|
|
|
40d7c1 |
if (usable_st_size (st))
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
if (st->st_size <= size)
|
|
|
40d7c1 |
- return textbin == TEXTBIN_UNKNOWN ? TEXTBIN_BINARY : textbin;
|
|
|
40d7c1 |
+ return false;
|
|
|
40d7c1 |
|
|
|
40d7c1 |
/* If the file has holes, it must contain a null byte somewhere. */
|
|
|
40d7c1 |
- if (SEEK_HOLE != SEEK_SET && eolbyte)
|
|
|
40d7c1 |
+ if (SEEK_HOLE != SEEK_SET)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
off_t cur = size;
|
|
|
40d7c1 |
if (O_BINARY || fd == STDIN_FILENO)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
cur = lseek (fd, 0, SEEK_CUR);
|
|
|
40d7c1 |
if (cur < 0)
|
|
|
40d7c1 |
- return TEXTBIN_UNKNOWN;
|
|
|
40d7c1 |
+ return false;
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
|
|
|
40d7c1 |
/* Look for a hole after the current location. */
|
|
|
40d7c1 |
@@ -612,12 +611,12 @@ file_textbin (char *buf, size_t size, int fd, struct stat const *st)
|
|
|
40d7c1 |
if (lseek (fd, cur, SEEK_SET) < 0)
|
|
|
40d7c1 |
suppressible_error (filename, errno);
|
|
|
40d7c1 |
if (hole_start < st->st_size)
|
|
|
40d7c1 |
- return TEXTBIN_BINARY;
|
|
|
40d7c1 |
+ return true;
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
|
|
|
40d7c1 |
- return TEXTBIN_UNKNOWN;
|
|
|
40d7c1 |
+ return false;
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
|
|
|
40d7c1 |
/* Convert STR to a nonnegative integer, storing the result in *OUT.
|
|
|
40d7c1 |
@@ -888,7 +887,7 @@ static char *label = NULL; /* Fake filename for stdin */
|
|
|
40d7c1 |
/* Internal variables to keep track of byte count, context, etc. */
|
|
|
40d7c1 |
static uintmax_t totalcc; /* Total character count before bufbeg. */
|
|
|
40d7c1 |
static char const *lastnl; /* Pointer after last newline counted. */
|
|
|
40d7c1 |
-static char const *lastout; /* Pointer after last character output;
|
|
|
40d7c1 |
+static char *lastout; /* Pointer after last character output;
|
|
|
40d7c1 |
NULL if no character has been output
|
|
|
40d7c1 |
or if it's conceptually before bufbeg. */
|
|
|
40d7c1 |
static intmax_t outleft; /* Maximum number of lines to be output. */
|
|
|
40d7c1 |
@@ -960,10 +959,31 @@ print_offset (uintmax_t pos, int min_width, const char *color)
|
|
|
40d7c1 |
pr_sgr_end_if (color);
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
|
|
|
40d7c1 |
-/* Print a whole line head (filename, line, byte). */
|
|
|
40d7c1 |
-static void
|
|
|
40d7c1 |
-print_line_head (char const *beg, char const *lim, char sep)
|
|
|
40d7c1 |
+/* Print a whole line head (filename, line, byte). The output data
|
|
|
40d7c1 |
+ starts at BEG and contains LEN bytes; it is followed by at least
|
|
|
40d7c1 |
+ sizeof (uword) bytes, the first of which may be temporarily modified.
|
|
|
40d7c1 |
+ The output data comes from what is perhaps a larger input line that
|
|
|
40d7c1 |
+ goes until LIM, where LIM[-1] is an end-of-line byte. Use SEP as
|
|
|
40d7c1 |
+ the separator on output.
|
|
|
40d7c1 |
+
|
|
|
40d7c1 |
+ Return true unless the line was suppressed due to an encoding error. */
|
|
|
40d7c1 |
+
|
|
|
40d7c1 |
+static bool
|
|
|
40d7c1 |
+print_line_head (char *beg, size_t len, char const *lim, char sep)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
+ bool encoding_errors = false;
|
|
|
40d7c1 |
+ if (binary_files != TEXT_BINARY_FILES)
|
|
|
40d7c1 |
+ {
|
|
|
40d7c1 |
+ char ch = beg[len];
|
|
|
40d7c1 |
+ encoding_errors = buf_has_encoding_errors (beg, len);
|
|
|
40d7c1 |
+ beg[len] = ch;
|
|
|
40d7c1 |
+ }
|
|
|
40d7c1 |
+ if (encoding_errors)
|
|
|
40d7c1 |
+ {
|
|
|
40d7c1 |
+ encoding_error_output = done_on_match = out_quiet = true;
|
|
|
40d7c1 |
+ return false;
|
|
|
40d7c1 |
+ }
|
|
|
40d7c1 |
+
|
|
|
40d7c1 |
bool pending_sep = false;
|
|
|
40d7c1 |
|
|
|
40d7c1 |
if (out_file)
|
|
|
40d7c1 |
@@ -1010,22 +1030,27 @@ print_line_head (char const *beg, char const *lim, char sep)
|
|
|
40d7c1 |
|
|
|
40d7c1 |
print_sep (sep);
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
+
|
|
|
40d7c1 |
+ return true;
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
|
|
|
40d7c1 |
-static const char *
|
|
|
40d7c1 |
-print_line_middle (const char *beg, const char *lim,
|
|
|
40d7c1 |
+static char *
|
|
|
40d7c1 |
+print_line_middle (char *beg, char *lim,
|
|
|
40d7c1 |
const char *line_color, const char *match_color)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
size_t match_size;
|
|
|
40d7c1 |
size_t match_offset;
|
|
|
40d7c1 |
- const char *cur = beg;
|
|
|
40d7c1 |
- const char *mid = NULL;
|
|
|
40d7c1 |
-
|
|
|
40d7c1 |
- while (cur < lim
|
|
|
40d7c1 |
- && ((match_offset = execute (beg, lim - beg, &match_size, cur))
|
|
|
40d7c1 |
- != (size_t) -1))
|
|
|
40d7c1 |
+ char *cur = beg;
|
|
|
40d7c1 |
+ char *mid = NULL;
|
|
|
40d7c1 |
+ char *b;
|
|
|
40d7c1 |
+
|
|
|
40d7c1 |
+ for (cur = beg;
|
|
|
40d7c1 |
+ (cur < lim
|
|
|
40d7c1 |
+ && ((match_offset = execute (beg, lim - beg, &match_size, cur))
|
|
|
40d7c1 |
+ != (size_t) -1));
|
|
|
40d7c1 |
+ cur = b + match_size)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
- char const *b = beg + match_offset;
|
|
|
40d7c1 |
+ b = beg + match_offset;
|
|
|
40d7c1 |
|
|
|
40d7c1 |
/* Avoid matching the empty line at the end of the buffer. */
|
|
|
40d7c1 |
if (b == lim)
|
|
|
40d7c1 |
@@ -1045,8 +1070,11 @@ print_line_middle (const char *beg, const char *lim,
|
|
|
40d7c1 |
/* This function is called on a matching line only,
|
|
|
40d7c1 |
but is it selected or rejected/context? */
|
|
|
40d7c1 |
if (only_matching)
|
|
|
40d7c1 |
- print_line_head (b, lim, (out_invert ? SEP_CHAR_REJECTED
|
|
|
40d7c1 |
- : SEP_CHAR_SELECTED));
|
|
|
40d7c1 |
+ {
|
|
|
40d7c1 |
+ char sep = out_invert ? SEP_CHAR_REJECTED : SEP_CHAR_SELECTED;
|
|
|
40d7c1 |
+ if (! print_line_head (b, match_size, lim, sep))
|
|
|
40d7c1 |
+ return NULL;
|
|
|
40d7c1 |
+ }
|
|
|
40d7c1 |
else
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
pr_sgr_start (line_color);
|
|
|
40d7c1 |
@@ -1064,7 +1092,6 @@ print_line_middle (const char *beg, const char *lim,
|
|
|
40d7c1 |
if (only_matching)
|
|
|
40d7c1 |
fputs ("\n", stdout);
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
- cur = b + match_size;
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
|
|
|
40d7c1 |
if (only_matching)
|
|
|
40d7c1 |
@@ -1075,8 +1102,8 @@ print_line_middle (const char *beg, const char *lim,
|
|
|
40d7c1 |
return cur;
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
|
|
|
40d7c1 |
-static const char *
|
|
|
40d7c1 |
-print_line_tail (const char *beg, const char *lim, const char *line_color)
|
|
|
40d7c1 |
+static char *
|
|
|
40d7c1 |
+print_line_tail (char *beg, const char *lim, const char *line_color)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
size_t eol_size;
|
|
|
40d7c1 |
size_t tail_size;
|
|
|
40d7c1 |
@@ -1097,14 +1124,15 @@ print_line_tail (const char *beg, const char *lim, const char *line_color)
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
|
|
|
40d7c1 |
static void
|
|
|
40d7c1 |
-prline (char const *beg, char const *lim, char sep)
|
|
|
40d7c1 |
+prline (char *beg, char *lim, char sep)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
bool matching;
|
|
|
40d7c1 |
const char *line_color;
|
|
|
40d7c1 |
const char *match_color;
|
|
|
40d7c1 |
|
|
|
40d7c1 |
if (!only_matching)
|
|
|
40d7c1 |
- print_line_head (beg, lim, sep);
|
|
|
40d7c1 |
+ if (! print_line_head (beg, lim - beg - 1, lim, sep))
|
|
|
40d7c1 |
+ return;
|
|
|
40d7c1 |
|
|
|
40d7c1 |
matching = (sep == SEP_CHAR_SELECTED) ^ out_invert;
|
|
|
40d7c1 |
|
|
|
40d7c1 |
@@ -1124,7 +1152,11 @@ prline (char const *beg, char const *lim, char sep)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
/* We already know that non-matching lines have no match (to colorize). */
|
|
|
40d7c1 |
if (matching && (only_matching || *match_color))
|
|
|
40d7c1 |
- beg = print_line_middle (beg, lim, line_color, match_color);
|
|
|
40d7c1 |
+ {
|
|
|
40d7c1 |
+ beg = print_line_middle (beg, lim, line_color, match_color);
|
|
|
40d7c1 |
+ if (! beg)
|
|
|
40d7c1 |
+ return;
|
|
|
40d7c1 |
+ }
|
|
|
40d7c1 |
|
|
|
40d7c1 |
if (!only_matching && *line_color)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
@@ -1158,7 +1190,7 @@ prpending (char const *lim)
|
|
|
40d7c1 |
lastout = bufbeg;
|
|
|
40d7c1 |
while (pending > 0 && lastout < lim)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
- char const *nl = memchr (lastout, eolbyte, lim - lastout);
|
|
|
40d7c1 |
+ char *nl = memchr (lastout, eolbyte, lim - lastout);
|
|
|
40d7c1 |
size_t match_size;
|
|
|
40d7c1 |
--pending;
|
|
|
40d7c1 |
if (outleft
|
|
|
40d7c1 |
@@ -1173,7 +1205,7 @@ prpending (char const *lim)
|
|
|
40d7c1 |
|
|
|
40d7c1 |
/* Output the lines between BEG and LIM. Deal with context. */
|
|
|
40d7c1 |
static void
|
|
|
40d7c1 |
-prtext (char const *beg, char const *lim)
|
|
|
40d7c1 |
+prtext (char *beg, char *lim)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
static bool used; /* Avoid printing SEP_STR_GROUP before any output. */
|
|
|
40d7c1 |
char eol = eolbyte;
|
|
|
40d7c1 |
@@ -1181,7 +1213,7 @@ prtext (char const *beg, char const *lim)
|
|
|
40d7c1 |
if (!out_quiet && pending > 0)
|
|
|
40d7c1 |
prpending (beg);
|
|
|
40d7c1 |
|
|
|
40d7c1 |
- char const *p = beg;
|
|
|
40d7c1 |
+ char *p = beg;
|
|
|
40d7c1 |
|
|
|
40d7c1 |
if (!out_quiet)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
@@ -1207,7 +1239,7 @@ prtext (char const *beg, char const *lim)
|
|
|
40d7c1 |
|
|
|
40d7c1 |
while (p < beg)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
- char const *nl = memchr (p, eol, beg - p);
|
|
|
40d7c1 |
+ char *nl = memchr (p, eol, beg - p);
|
|
|
40d7c1 |
nl++;
|
|
|
40d7c1 |
prline (p, nl, SEP_CHAR_REJECTED);
|
|
|
40d7c1 |
p = nl;
|
|
|
40d7c1 |
@@ -1220,7 +1252,7 @@ prtext (char const *beg, char const *lim)
|
|
|
40d7c1 |
/* One or more lines are output. */
|
|
|
40d7c1 |
for (n = 0; p < lim && n < outleft; n++)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
- char const *nl = memchr (p, eol, lim - p);
|
|
|
40d7c1 |
+ char *nl = memchr (p, eol, lim - p);
|
|
|
40d7c1 |
nl++;
|
|
|
40d7c1 |
if (!out_quiet)
|
|
|
40d7c1 |
prline (p, nl, SEP_CHAR_SELECTED);
|
|
|
40d7c1 |
@@ -1267,13 +1299,12 @@ zap_nuls (char *p, char *lim, char eol)
|
|
|
40d7c1 |
between matching lines if OUT_INVERT is true). Return a count of
|
|
|
40d7c1 |
lines printed. Replace all NUL bytes with NUL_ZAPPER as we go. */
|
|
|
40d7c1 |
static intmax_t
|
|
|
40d7c1 |
-grepbuf (char const *beg, char const *lim)
|
|
|
40d7c1 |
+grepbuf (char *beg, char const *lim)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
intmax_t outleft0 = outleft;
|
|
|
40d7c1 |
- char const *p;
|
|
|
40d7c1 |
- char const *endp;
|
|
|
40d7c1 |
+ char *endp;
|
|
|
40d7c1 |
|
|
|
40d7c1 |
- for (p = beg; p < lim; p = endp)
|
|
|
40d7c1 |
+ for (char *p = beg; p < lim; p = endp)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
size_t match_size;
|
|
|
40d7c1 |
size_t match_offset = execute (p, lim - p, &match_size, NULL);
|
|
|
40d7c1 |
@@ -1284,15 +1315,15 @@ grepbuf (char const *beg, char const *lim)
|
|
|
40d7c1 |
match_offset = lim - p;
|
|
|
40d7c1 |
match_size = 0;
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
- char const *b = p + match_offset;
|
|
|
40d7c1 |
+ char *b = p + match_offset;
|
|
|
40d7c1 |
endp = b + match_size;
|
|
|
40d7c1 |
/* Avoid matching the empty line at the end of the buffer. */
|
|
|
40d7c1 |
if (!out_invert && b == lim)
|
|
|
40d7c1 |
break;
|
|
|
40d7c1 |
if (!out_invert || p < b)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
- char const *prbeg = out_invert ? p : b;
|
|
|
40d7c1 |
- char const *prend = out_invert ? b : endp;
|
|
|
40d7c1 |
+ char *prbeg = out_invert ? p : b;
|
|
|
40d7c1 |
+ char *prend = out_invert ? b : endp;
|
|
|
40d7c1 |
prtext (prbeg, prend);
|
|
|
40d7c1 |
if (!outleft || done_on_match)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
@@ -1313,7 +1344,6 @@ static intmax_t
|
|
|
40d7c1 |
grep (int fd, struct stat const *st)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
intmax_t nlines, i;
|
|
|
40d7c1 |
- enum textbin textbin;
|
|
|
40d7c1 |
size_t residue, save;
|
|
|
40d7c1 |
char oldc;
|
|
|
40d7c1 |
char *beg;
|
|
|
40d7c1 |
@@ -1322,6 +1352,7 @@ grep (int fd, struct stat const *st)
|
|
|
40d7c1 |
char nul_zapper = '\0';
|
|
|
40d7c1 |
bool done_on_match_0 = done_on_match;
|
|
|
40d7c1 |
bool out_quiet_0 = out_quiet;
|
|
|
40d7c1 |
+ bool has_nulls = false;
|
|
|
40d7c1 |
|
|
|
40d7c1 |
if (! reset (fd, st))
|
|
|
40d7c1 |
return 0;
|
|
|
40d7c1 |
@@ -1333,6 +1364,7 @@ grep (int fd, struct stat const *st)
|
|
|
40d7c1 |
after_last_match = 0;
|
|
|
40d7c1 |
pending = 0;
|
|
|
40d7c1 |
skip_nuls = skip_empty_lines && !eol;
|
|
|
40d7c1 |
+ encoding_error_output = false;
|
|
|
40d7c1 |
seek_data_failed = false;
|
|
|
40d7c1 |
|
|
|
40d7c1 |
nlines = 0;
|
|
|
40d7c1 |
@@ -1345,26 +1377,20 @@ grep (int fd, struct stat const *st)
|
|
|
40d7c1 |
return 0;
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
|
|
|
40d7c1 |
- if (binary_files == TEXT_BINARY_FILES)
|
|
|
40d7c1 |
- textbin = TEXTBIN_TEXT;
|
|
|
40d7c1 |
- else
|
|
|
40d7c1 |
+ for (bool firsttime = true; ; firsttime = false)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
- textbin = file_textbin (bufbeg, buflim - bufbeg, fd, st);
|
|
|
40d7c1 |
- if (textbin_is_binary (textbin))
|
|
|
40d7c1 |
+ if (!has_nulls && eol && binary_files != TEXT_BINARY_FILES
|
|
|
40d7c1 |
+ && (buf_has_nulls (bufbeg, buflim - bufbeg)
|
|
|
40d7c1 |
+ || (firsttime && file_must_have_nulls (buflim - bufbeg, fd, st))))
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
+ has_nulls = true;
|
|
|
40d7c1 |
if (binary_files == WITHOUT_MATCH_BINARY_FILES)
|
|
|
40d7c1 |
return 0;
|
|
|
40d7c1 |
done_on_match = out_quiet = true;
|
|
|
40d7c1 |
nul_zapper = eol;
|
|
|
40d7c1 |
skip_nuls = skip_empty_lines;
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
- else if (execute != Pexecute)
|
|
|
40d7c1 |
- textbin = TEXTBIN_TEXT;
|
|
|
40d7c1 |
- }
|
|
|
40d7c1 |
|
|
|
40d7c1 |
- for (;;)
|
|
|
40d7c1 |
- {
|
|
|
40d7c1 |
- input_textbin = textbin;
|
|
|
40d7c1 |
lastnl = bufbeg;
|
|
|
40d7c1 |
if (lastout)
|
|
|
40d7c1 |
lastout = bufbeg;
|
|
|
97a081 |
@@ -1415,13 +1441,8 @@ grep (int fd, struct stat const *st)
|
|
|
97a081 |
}
|
|
|
97a081 |
|
|
|
97a081 |
/* Detect whether leading context is adjacent to previous output. */
|
|
|
97a081 |
- if (lastout)
|
|
|
97a081 |
- {
|
|
|
97a081 |
- if (textbin == TEXTBIN_UNKNOWN)
|
|
|
97a081 |
- textbin = TEXTBIN_TEXT;
|
|
|
97a081 |
- if (beg != lastout)
|
|
|
97a081 |
- lastout = 0;
|
|
|
97a081 |
- }
|
|
|
97a081 |
+ if (beg != lastout)
|
|
|
97a081 |
+ lastout = 0;
|
|
|
40d7c1 |
|
|
|
40d7c1 |
/* Handle some details and read more data to scan. */
|
|
|
40d7c1 |
save = residue + lim - beg;
|
|
|
40d7c1 |
@@ -1434,22 +1455,6 @@ grep (int fd, struct stat const *st)
|
|
|
40d7c1 |
suppressible_error (filename, errno);
|
|
|
40d7c1 |
goto finish_grep;
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
-
|
|
|
40d7c1 |
- /* If the file's textbin has not been determined yet, assume
|
|
|
40d7c1 |
- it's binary if the next input buffer suggests so. */
|
|
|
40d7c1 |
- if (textbin == TEXTBIN_UNKNOWN)
|
|
|
40d7c1 |
- {
|
|
|
40d7c1 |
- enum textbin tb = buffer_textbin (bufbeg, buflim - bufbeg);
|
|
|
40d7c1 |
- if (textbin_is_binary (tb))
|
|
|
40d7c1 |
- {
|
|
|
40d7c1 |
- if (binary_files == WITHOUT_MATCH_BINARY_FILES)
|
|
|
40d7c1 |
- return 0;
|
|
|
40d7c1 |
- textbin = tb;
|
|
|
40d7c1 |
- done_on_match = out_quiet = true;
|
|
|
40d7c1 |
- nul_zapper = eol;
|
|
|
40d7c1 |
- skip_nuls = skip_empty_lines;
|
|
|
40d7c1 |
- }
|
|
|
40d7c1 |
- }
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
if (residue)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
@@ -1463,7 +1468,7 @@ grep (int fd, struct stat const *st)
|
|
|
40d7c1 |
finish_grep:
|
|
|
40d7c1 |
done_on_match = done_on_match_0;
|
|
|
40d7c1 |
out_quiet = out_quiet_0;
|
|
|
40d7c1 |
- if (textbin_is_binary (textbin) && !out_quiet && nlines != 0)
|
|
|
40d7c1 |
+ if ((has_nulls || encoding_error_output) && !out_quiet && nlines != 0)
|
|
|
40d7c1 |
printf (_("Binary file %s matches\n"), filename);
|
|
|
40d7c1 |
return nlines;
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
diff --git a/src/grep.h b/src/grep.h
|
|
|
40d7c1 |
index 580eb11..2e4527c 100644
|
|
|
40d7c1 |
--- a/src/grep.h
|
|
|
40d7c1 |
+++ b/src/grep.h
|
|
|
40d7c1 |
@@ -29,22 +29,4 @@ extern bool match_words; /* -w */
|
|
|
40d7c1 |
extern bool match_lines; /* -x */
|
|
|
40d7c1 |
extern char eolbyte; /* -z */
|
|
|
40d7c1 |
|
|
|
40d7c1 |
-/* An enum textbin describes the file's type, inferred from data read
|
|
|
40d7c1 |
- before the first line is selected for output. */
|
|
|
40d7c1 |
-enum textbin
|
|
|
40d7c1 |
- {
|
|
|
40d7c1 |
- /* Binary, as it contains null bytes and the -z option is not in effect,
|
|
|
40d7c1 |
- or it contains encoding errors. */
|
|
|
40d7c1 |
- TEXTBIN_BINARY = -1,
|
|
|
40d7c1 |
-
|
|
|
40d7c1 |
- /* Not known yet. Only text has been seen so far. */
|
|
|
40d7c1 |
- TEXTBIN_UNKNOWN = 0,
|
|
|
40d7c1 |
-
|
|
|
40d7c1 |
- /* Text. */
|
|
|
40d7c1 |
- TEXTBIN_TEXT = 1
|
|
|
40d7c1 |
- };
|
|
|
40d7c1 |
-
|
|
|
40d7c1 |
-/* Input file type. */
|
|
|
40d7c1 |
-extern enum textbin input_textbin;
|
|
|
40d7c1 |
-
|
|
|
40d7c1 |
#endif
|
|
|
40d7c1 |
diff --git a/src/pcresearch.c b/src/pcresearch.c
|
|
|
40d7c1 |
index b1f8310..a446b2c 100644
|
|
|
40d7c1 |
--- a/src/pcresearch.c
|
|
|
40d7c1 |
+++ b/src/pcresearch.c
|
|
|
40d7c1 |
@@ -194,32 +194,13 @@ Pexecute (char const *buf, size_t size, size_t *match_size,
|
|
|
40d7c1 |
error. */
|
|
|
40d7c1 |
char const *subject = buf;
|
|
|
40d7c1 |
|
|
|
40d7c1 |
- /* If the input type is unknown, the caller is still testing the
|
|
|
40d7c1 |
- input, which means the current buffer cannot contain encoding
|
|
|
40d7c1 |
- errors and a multiline search is typically more efficient.
|
|
|
40d7c1 |
- Otherwise, a single-line search is typically faster, so that
|
|
|
40d7c1 |
- pcre_exec doesn't waste time validating the entire input
|
|
|
40d7c1 |
- buffer. */
|
|
|
40d7c1 |
- bool multiline = input_textbin == TEXTBIN_UNKNOWN;
|
|
|
40d7c1 |
-
|
|
|
40d7c1 |
for (; p < buf + size; p = line_start = line_end + 1)
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
- bool too_big;
|
|
|
40d7c1 |
-
|
|
|
40d7c1 |
- if (multiline)
|
|
|
40d7c1 |
- {
|
|
|
40d7c1 |
- size_t pcre_size_max = MIN (INT_MAX, SIZE_MAX - 1);
|
|
|
40d7c1 |
- size_t scan_size = MIN (pcre_size_max + 1, buf + size - p);
|
|
|
40d7c1 |
- line_end = memrchr (p, eolbyte, scan_size);
|
|
|
40d7c1 |
- too_big = ! line_end;
|
|
|
40d7c1 |
- }
|
|
|
40d7c1 |
- else
|
|
|
40d7c1 |
- {
|
|
|
40d7c1 |
- line_end = memchr (p, eolbyte, buf + size - p);
|
|
|
40d7c1 |
- too_big = INT_MAX < line_end - p;
|
|
|
40d7c1 |
- }
|
|
|
40d7c1 |
-
|
|
|
40d7c1 |
- if (too_big)
|
|
|
40d7c1 |
+ /* A single-line search is typically faster, so that
|
|
|
40d7c1 |
+ pcre_exec doesn't waste time validating the entire input
|
|
|
40d7c1 |
+ buffer. */
|
|
|
40d7c1 |
+ line_end = memchr (p, eolbyte, buf + size - p);
|
|
|
40d7c1 |
+ if (INT_MAX < line_end - p)
|
|
|
40d7c1 |
error (EXIT_TROUBLE, 0, _("exceeded PCRE's line length limit"));
|
|
|
40d7c1 |
|
|
|
40d7c1 |
for (;;)
|
|
|
40d7c1 |
@@ -247,27 +228,11 @@ Pexecute (char const *buf, size_t size, size_t *match_size,
|
|
|
40d7c1 |
int options = 0;
|
|
|
40d7c1 |
if (!bol)
|
|
|
40d7c1 |
options |= PCRE_NOTBOL;
|
|
|
40d7c1 |
- if (multiline)
|
|
|
40d7c1 |
- options |= PCRE_NO_UTF8_CHECK;
|
|
|
40d7c1 |
|
|
|
40d7c1 |
e = jit_exec (subject, line_end - subject, search_offset,
|
|
|
40d7c1 |
options, sub);
|
|
|
40d7c1 |
if (e != PCRE_ERROR_BADUTF8)
|
|
|
40d7c1 |
- {
|
|
|
40d7c1 |
- if (0 < e && multiline && sub[1] - sub[0] != 0)
|
|
|
40d7c1 |
- {
|
|
|
40d7c1 |
- char const *nl = memchr (subject + sub[0], eolbyte,
|
|
|
40d7c1 |
- sub[1] - sub[0]);
|
|
|
40d7c1 |
- if (nl)
|
|
|
40d7c1 |
- {
|
|
|
40d7c1 |
- /* This match crosses a line boundary; reject it. */
|
|
|
40d7c1 |
- p = subject + sub[0];
|
|
|
40d7c1 |
- line_end = nl;
|
|
|
40d7c1 |
- continue;
|
|
|
40d7c1 |
- }
|
|
|
40d7c1 |
- }
|
|
|
40d7c1 |
- break;
|
|
|
40d7c1 |
- }
|
|
|
40d7c1 |
+ break;
|
|
|
40d7c1 |
int valid_bytes = sub[0];
|
|
|
40d7c1 |
|
|
|
40d7c1 |
/* Try to match the string before the encoding error. */
|
|
|
40d7c1 |
@@ -337,15 +302,6 @@ Pexecute (char const *buf, size_t size, size_t *match_size,
|
|
|
40d7c1 |
beg = matchbeg;
|
|
|
40d7c1 |
end = matchend;
|
|
|
40d7c1 |
}
|
|
|
40d7c1 |
- else if (multiline)
|
|
|
40d7c1 |
- {
|
|
|
40d7c1 |
- char const *prev_nl = memrchr (line_start - 1, eolbyte,
|
|
|
40d7c1 |
- matchbeg - (line_start - 1));
|
|
|
40d7c1 |
- char const *next_nl = memchr (matchend, eolbyte,
|
|
|
40d7c1 |
- line_end + 1 - matchend);
|
|
|
40d7c1 |
- beg = prev_nl + 1;
|
|
|
40d7c1 |
- end = next_nl + 1;
|
|
|
40d7c1 |
- }
|
|
|
40d7c1 |
else
|
|
|
40d7c1 |
{
|
|
|
40d7c1 |
beg = line_start;
|
|
|
40d7c1 |
diff --git a/tests/Makefile.am b/tests/Makefile.am
|
|
|
40d7c1 |
index d379821..2ade5be 100644
|
|
|
40d7c1 |
--- a/tests/Makefile.am
|
|
|
40d7c1 |
+++ b/tests/Makefile.am
|
|
|
40d7c1 |
@@ -70,6 +70,7 @@ TESTS = \
|
|
|
40d7c1 |
empty \
|
|
|
40d7c1 |
empty-line \
|
|
|
40d7c1 |
empty-line-mb \
|
|
|
40d7c1 |
+ encoding-error \
|
|
|
40d7c1 |
epipe \
|
|
|
40d7c1 |
equiv-classes \
|
|
|
40d7c1 |
ere \
|
|
|
40d7c1 |
diff --git a/tests/Makefile.in b/tests/Makefile.in
|
|
|
40d7c1 |
index 6de6f49..b5bd7b5 100644
|
|
|
40d7c1 |
--- a/tests/Makefile.in
|
|
|
40d7c1 |
+++ b/tests/Makefile.in
|
|
|
40d7c1 |
@@ -1406,6 +1406,7 @@ TESTS = \
|
|
|
40d7c1 |
empty \
|
|
|
40d7c1 |
empty-line \
|
|
|
40d7c1 |
empty-line-mb \
|
|
|
40d7c1 |
+ encoding-error \
|
|
|
40d7c1 |
epipe \
|
|
|
40d7c1 |
equiv-classes \
|
|
|
40d7c1 |
ere \
|
|
|
40d7c1 |
@@ -1997,6 +1998,13 @@ empty-line-mb.log: empty-line-mb
|
|
|
40d7c1 |
--log-file $$b.log --trs-file $$b.trs \
|
|
|
40d7c1 |
$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
|
|
|
40d7c1 |
"$$tst" $(AM_TESTS_FD_REDIRECT)
|
|
|
40d7c1 |
+encoding-error.log: encoding-error
|
|
|
40d7c1 |
+ @p='encoding-error'; \
|
|
|
40d7c1 |
+ b='encoding-error'; \
|
|
|
40d7c1 |
+ $(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \
|
|
|
40d7c1 |
+ --log-file $$b.log --trs-file $$b.trs \
|
|
|
40d7c1 |
+ $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
|
|
|
40d7c1 |
+ "$$tst" $(AM_TESTS_FD_REDIRECT)
|
|
|
40d7c1 |
epipe.log: epipe
|
|
|
40d7c1 |
@p='epipe'; \
|
|
|
40d7c1 |
b='epipe'; \
|
|
|
40d7c1 |
diff --git a/tests/encoding-error b/tests/encoding-error
|
|
|
40d7c1 |
new file mode 100755
|
|
|
40d7c1 |
index 0000000..fe52de2
|
|
|
40d7c1 |
--- a/dev/null
|
|
|
40d7c1 |
+++ b/tests/encoding-error
|
|
|
40d7c1 |
@@ -0,0 +1,41 @@
|
|
|
40d7c1 |
+#! /bin/sh
|
|
|
40d7c1 |
+# Test grep's behavior on encoding errors.
|
|
|
40d7c1 |
+#
|
|
|
40d7c1 |
+# Copyright 2015 Free Software Foundation, Inc.
|
|
|
40d7c1 |
+#
|
|
|
40d7c1 |
+# Copying and distribution of this file, with or without modification,
|
|
|
40d7c1 |
+# are permitted in any medium without royalty provided the copyright
|
|
|
40d7c1 |
+# notice and this notice are preserved.
|
|
|
40d7c1 |
+
|
|
|
40d7c1 |
+. "${srcdir=.}/init.sh"; path_prepend_ ../src
|
|
|
40d7c1 |
+
|
|
|
40d7c1 |
+require_en_utf8_locale_
|
|
|
40d7c1 |
+
|
|
|
40d7c1 |
+LC_ALL=en_US.UTF-8
|
|
|
40d7c1 |
+export LC_ALL
|
|
|
40d7c1 |
+
|
|
|
40d7c1 |
+printf 'Alfred Jones\n' > a || framework_failure_
|
|
|
40d7c1 |
+printf 'John Smith\n' >j || framework_failure_
|
|
|
40d7c1 |
+printf 'Pedro P\xe9rez\n' >p || framework_failure_
|
|
|
40d7c1 |
+cat a p j >in || framework_failure_
|
|
|
40d7c1 |
+
|
|
|
40d7c1 |
+fail=0
|
|
|
40d7c1 |
+
|
|
|
40d7c1 |
+grep '^A' in >out || fail=1
|
|
|
40d7c1 |
+compare a out || fail=1
|
|
|
40d7c1 |
+
|
|
|
40d7c1 |
+grep '^P' in >out || fail=1
|
|
|
40d7c1 |
+printf 'Binary file in matches\n' >exp || framework_failure_
|
|
|
40d7c1 |
+compare exp out || fail=1
|
|
|
40d7c1 |
+
|
|
|
40d7c1 |
+grep '^J' in >out || fail=1
|
|
|
40d7c1 |
+compare j out || fail=1
|
|
|
40d7c1 |
+
|
|
|
40d7c1 |
+grep '^X' in >out
|
|
|
40d7c1 |
+test $? = 1 || fail=1
|
|
|
40d7c1 |
+compare /dev/null out || fail=1
|
|
|
40d7c1 |
+
|
|
|
40d7c1 |
+grep -a . in >out || fail=1
|
|
|
40d7c1 |
+compare in out
|
|
|
40d7c1 |
+
|
|
|
40d7c1 |
+Exit $fail
|