Commit Graph

143 Commits

Author SHA1 Message Date
Kyle Evans
8433795d66 grep test: Fix copyright notice
The copyright notice was erroneously introduced as one from the NetBSD
foundation due to it being copied from a file in the NetBSD test suite, but
this file itself is not derived from or supplied with the NetBSD test suite.

MFC after:	3 days
2017-12-03 02:23:29 +00:00
Pedro F. Giffuni
1de7b4b805 various: general adoption of SPDX licensing ID tags.
Mainly focus on files that use BSD 2-Clause license, however the tool I
was using misidentified many licenses so this was mostly a manual - error
prone - task.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.

No functional change intended.
2017-11-27 15:37:16 +00:00
Bryan Drewery
ea825d0274 DIRDEPS_BUILD: Update dependencies.
Sponsored by:	Dell EMC Isilon
2017-10-31 00:07:04 +00:00
Ed Maste
6e7bfaecee fastmatch.h: remove duplicate #defines
Reviewed by:	kevans
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D12375
2017-09-15 13:34:00 +00:00
Kyle Evans
05ad821531 bsdgrep: add a primitive literal matcher
fgrep/grep -F will error out at runtime if compiled with a regex(3)
that does not define REG_NOSPEC or REG_LITERAL. glibc is one such regex(3)
implementation, and as it turns out they don't support literal matching at
all.

Provide a primitive literal matcher for use with glibc and other
implementations that don't support literal matching so that we don't
completely lose fgrep/grep -F if compiled against libgnuregex on stable/10,
stable/11, or other systems that we don't necessarily support.

This is a wholly unoptimized implementation with no plans to optimize it as
of now. This is due to both its use-case being primarily on unsupported
systems in the near-distant future and that it's reinventing the wheel that
we already have available as a feature of regex(3).

Reviewed by:	cem, emaste, ngie
Approved by:	emaste (mentor)
MFC after:	2 weeks
Differential Revision:	https://reviews.freebsd.org/D12056
2017-08-24 01:23:33 +00:00
Kyle Evans
de3d7a8286 bsdgrep: cast pmatch.rm_so to fix build when linking against libgnuregex
Reported by:	many
Approved by:	emaste (mentor)
MFC after:	immediate
2017-08-17 13:40:45 +00:00
Enji Cooper
d511b20a69 Add HAS_TESTS to all Makefiles that are currently using the
`SUBDIR.${MK_TESTS}+= tests` idiom.

This is a follow up to r321912.
2017-08-02 08:50:42 +00:00
Enji Cooper
4b330699f8 Convert traditional ${MK_TESTS} conditional idiom for including test
directories to SUBDIR.${MK_TESTS} idiom

This is being done to pave the way for future work (and homogenity) in
^/projects/make-check-sandbox .

No functional change intended.

MFC after:	1 weeks
2017-08-02 08:35:51 +00:00
Kyle Evans
0e95794283 bsdgrep(1): Don't exit before processing every file
Given an empty pattern (i.e. grep "" A B), bsdgrep(1) would previously exit()
with the appropriate exit code upon encountering an empty file. Likely intended
as an optimization, but this behavior is technically incorrect since an empty
pattern should match every line.

PR:		220924
Reviewed by:	emaste, cem (earlier version), ngie
Approved by:	emaste (mentor)
Differential Revision:	https://reviews.freebsd.org/D11698
2017-07-25 01:50:37 +00:00
Kyle Evans
26e1c38fbb Update copyright e-mail address to @FreeBSD.org address
Approved by:	emaste (mentor)
Differential Revision:	https://reviews.freebsd.org/D11508
2017-07-06 19:53:30 +00:00
Bryan Drewery
c99b67a794 Utilize SYSROOT from r320119 in places where DESTDIR may be wanting WORLDTMP.
Since buildenv exports SYSROOT all of these uses will now look in
WORLDTMP by default.

sys/boot/efi/loader/Makefile
        A LIBSTAND hack is no longer required for buildenv.

MFC after:	2 weeks
Sponsored by:	Dell EMC Isilon
2017-06-19 20:47:24 +00:00
Ed Maste
b05c7cdeb1 bsdgrep: bump version number and add Kyle Evans copyright
The following changes have been made over the last couple of months:

Features:

 - With bsdgrep -r, the working directory is implied if no directory is
   specified
 - bsdgrep will now behave as bsdgrep -r does when it's named rgrep
 - bsdgrep now understands -z/--null-data to use \0 as EOL
 - GNU regex compatibility is now indicated with a "GNU compatible" in
   the version string

Fixes:

 - --mmap no longer hangs when coming across an EOF without an
   accompanying EOL
 - -o/--color matching generally improved, now produces earliest /
   longest matches
 - Context output now more closely aligns with GNU grep
 - Zero-length matches no longer exhibit broken behavior
 - Every output line now honors -b/-H/-n flags

Tests have been added for previous regressions as well as other
previously untested behaviors.

Various other fixes have been commited, and refactoring for further /
later improvements has taken place.

(The original submission changed the version string to 2.5.2, but I
decided to use 2.6.0 to reflect the addition of new features.)

Submitted by:	Kyle Evans <kevans91@ksu.edu>
Differential Revision:	https://reviews.freebsd.org/D10982
2017-05-29 13:10:01 +00:00
Enji Cooper
80c5ef109c :rgrep : use atf-check to check the exit code/save the output of grep -r instead
of calling grep -r without it, and saving the output to a file

This ensures that any errors thrown via grep -r are caught, not lost, and uses
existing atf-sh idioms for saving files.

Tested with:	bsdgrep, gnu grep (base, ports)
Sponsored by:	Dell EMC Isilon
2017-05-27 22:40:20 +00:00
Ed Maste
6068d3b2c9 bsdgrep: use safer sizeof() construct
Submitted by:	Kyle Evans <kevans91@ksu.edu>
2017-05-26 03:35:59 +00:00
Ed Maste
8bf4606408 bsdgrep: correct assumptions to prepare for chunking
Correct a couple of minor BSD grep assumptions that are valid for line
processing but not future chunk-based processing.

Submitted by:	Kyle Evans <kevans91@ksu.edu>
Reviewed by:	bapt, cem
Differential Revision:	https://reviews.freebsd.org/D10824
2017-05-26 02:30:26 +00:00
Ed Maste
6d635d3b32 bsdgrep: Correct per-line line metadata printing
Metadata printing with -b, -H, or -n flags suffered from a few flaws:

1) -b/offset printing was broken when used in conjunction with -o

2) With -o, bsdgrep did not print metadata for every match/line, just
   the first match of a line

3) There were no tests for this

Address these issues by outputting this data per-match if the -o flag is
specified, and prior to outputting any matches if -o but not --color,
since --color alone will not generate a new line of output for every
iteration over the matches.

To correct -b output, fudge the line offset as we're printing matches.

While here, make sure we're using grep_printline in -A context.  Context
printing should *never* look at the parsing context, just the line.

The tests included do not pass with gnugrep in base due to it exhibiting
similar quirky behavior that bsdgrep previously exhibited.

Submitted by:	Kyle Evans <kevans91@ksu.edu>
Reviewed by:	cem
Differential Revision:	https://reviews.freebsd.org/D10580
2017-05-20 11:20:03 +00:00
Ed Maste
fe8c9d5bf1 bsdgrep: emit more than MAX_LINE_MATCHES per line
We should not set an arbitrary cap on the number of matches on a line,
and in any case MAX_LINE_MATCHES of 32 is much too low.  Instead, if we
match more than MAX_LINE_MATCHES, keep processing and matching from the
last match until all are found.

For the regression test, we produce 4096 matches (larger than we expect
we'll ever set MAX_LINE_MATCHES) and make sure we actually get 4096
lines of output with the -o flag.

We'll also make sure that every distinct line is getting its own line
number to detect line metadata not being printed as appropriate along
the way.

PR:		218811
Submitted by:	Kyle Evans <kevans91@ksu.edu>
Reported by:	jbeich
Reviewed by:	cem
Differential Revision:	https://reviews.freebsd.org/D10577
2017-05-20 03:51:31 +00:00
Ed Maste
9a1452026e bsdgrep: fix segfault with --mmap
r313948 partially fixed --mmap behavior but was incomplete.  This commit
generally reverts it and does it the more correct way- by just consuming
the rest of the buffer and moving on.

PR:		219402
Submitted by:	Kyle Evans <kevans91@ksu.edu>
Reviewed by:	cem
Differential Revision:	https://reviews.freebsd.org/D10820
2017-05-20 00:42:47 +00:00
Ed Maste
b5fc583c27 bsdgrep: don't allow negative -A / -B / -C
Previously, when given a negative -A/-B/-C argument bsdgrep would
overflow the respective context flag(s) and exhibited surprising
behavior.

Fix this by removing unsignedness of Aflag/Bflag and erroring out if
we're given a value < 0.  Also adjust the type used to track 'tail'
context in procfile() so that it accurately reflects the Aflag value
rather than overflowing and losing trailing context.

This also fixes an inconsistency previously existing between -n and
-C "n" behavior.  They are now both limited to LLONG_MAX, to be
consistent.

Add some test cases to make sure grep errors out properly for both
negative context values as well as non-numeric context values rather
than giving bogus matches.

Submitted by:	Kyle Evans <kevans91@ksu.edu>
Reviewed by:	cem
Differential Revision:	https://reviews.freebsd.org/D10675
2017-05-15 17:51:01 +00:00
Bryan Drewery
07676084ec DIRDEPS_BUILD: Update dependencies.
Sponsored by:	Dell EMC Isilon
2017-05-09 01:48:23 +00:00
Ed Maste
e2127de812 bsdgrep: don't ouptut matches with -c, -l, -L
Refactoring done in r317703 broke -c, -l, and -L flags implying
suppression of match printing.  Fortunately this is just a matter of not
doing any printing of the resulting matches and context printing was not
broken in this refactoring.

Add some regression tests since this area may still see further
refactoring, include different context flags as well even though they
were not broken in this case.

PR:		219077
Submitted by:	Kyle kevans91@ksu.edu
Reported by:	markj
Reviewed by:	cem, ngie
Differential Revision:	https://reviews.freebsd.org/D10607
2017-05-05 17:35:05 +00:00
Ed Maste
83fd8885c4 bsdgrep: correct uninitialized variable introduced in r317703
CID:		1374747
Submitted by:	Kyle Evans <kevans91@ksu.edu>
2017-05-03 13:47:02 +00:00
Ed Maste
476d209821 bsdgrep: avoid use of magic number for REG_NOSPEC
Submitted by:	Kyle Evans <kevans91 at ksu.edu>
Differential Revision:	https://reviews.freebsd.org/D10420
2017-05-02 21:08:38 +00:00
Ed Maste
41e04e8c77 bsdgrep: fix escape map building for multibyte strings
In BSD grep, fix escape map building in the regex parser. It was
previously using memory not explicitly initialized, and the MBS escape
map was being built based on a version of the pattern with escapes
already parsed out.

This is Kyle's change, but I restored the broken style that already
exists in this file.

Submitted by:	Kyle Evans <kevans91 at ksu.edu>
Reviewed by:	cem, Kyle Evans (my style changes)
Differential Revision:	https://reviews.freebsd.org/D10098
2017-05-02 20:44:06 +00:00
Ed Maste
a4f3f02be6 bsdgrep: fix -w flag matching with an empty pattern
-w flag matching with an empty pattern was generally 'broken', allowing
matches to occur on any line whether or not it actually matches -w
criteria.

This fix required a good amount of refactoring to address.  procline()
is altered to *only* process the line and return whether it was a match
or not, necessary to be able to short-circuit the whole function in case
of this matchall flag. -m flag handling is moved out as well because it
suffers from the same fate as context handling if we bypass any actual
pattern matching.

The matching context (matches, mostly) didn't previously exist outside
of procline(), so we go ahead and create context object for file
processing bits to pass around.  grep_printline() was created due to
this, for the scenarios where the matches don't actually matter and we
just want to print a line or two, a la flushing the context queue and
no -o or --color specified.

Damage from this broken behavior would have been mitigated by the fact
that it is unlikely users would invoke grep -w with an empty pattern.

This was identified while checking PR 105221 for problems it this may
cause in BSD grep, but PR 105221 is *not* a report of this behavior.

Submitted by:	Kyle Evans <kevans91 at ksu.edu>
Differential Revision:	https://reviews.freebsd.org/D10433
2017-05-02 20:39:33 +00:00
Ed Maste
a535623c78 bsdgrep: correct test sense from r317700
Kyle's change in review D10098 was correct. I introduced the error when
extracting a portion of that change.
2017-05-02 20:08:04 +00:00
Ed Maste
e0780d5479 bsdgrep: use calloc where appropriate in grep's tre-fastmatch
Also apply style(9) to a related NULL check.

Submitted by:	Kyle Evans <kevans91 at ksu.edu> (D10098)
2017-05-02 19:56:42 +00:00
Ed Maste
f76958b1c2 bsdgrep: correct nls usage data after r317049
r317049 added -z/--null-data to BSD grep but missed the update to nls
catalogs.

Submitted by:	Kyle Evans <kevans91 at ksu.edu>
Differential Revision:	https://reviews.freebsd.org/D10456
2017-05-02 13:47:15 +00:00
Ed Maste
cc41ba26bb bsdgrep: Add GNU compatible version string indicator
As reported in r218614 it's useful to have an indication of whether or not
BSD grep was built with GNU_GREP_COMPAT.

Submitted by:	Kyle Evans <kevans91 at ksu.edu>
Reported by:	mandree
Differential Revision:	https://reviews.freebsd.org/D10451
2017-05-02 13:38:20 +00:00
Ed Maste
945fc991b2 bsdgrep: fix -w -v matching improperly with certain patterns
-w and -v flag matching was mostly functional but had some minor
problems:

1. -w flag processing only allowed one iteration through pattern
   matching on a line. This was problematic if one pattern could match
   more than once, or if there were multiple patterns and the earliest/
   longest match was not the most ideal, and

2. Previous work "fixed" things to not further process a line if the
   first iteration through patterns produced no matches. This is clearly
   wrong if we're dealing with the more restrictive -w matching.

#2 breakage could have also occurred before recent broad rewrites, but
it would be more arbitrary based on input patterns as to whether or not
it actually affected things.

Fix both of these by forcing a retry of the patterns after advancing
just past the start of the first match if we're doing more restrictive
-w matching and we didn't get any hits to start with. Also move -v flag
processing outside of the loop so that we have a greater change to match
in the more restrictive cases. This wasn't strictly wrong, but it could
be a little more error prone.

While here, introduce some regressions tests for this behavior and fix
some excessive wrapping nearby that hindered readability. GNU grep
passes these new tests.

PR:		218467, 218811
Submitted by:	Kyle Evans <kevans91 at ksu.edu>
Reviewed by:	cem, ngie
Differential Revision:	https://reviews.freebsd.org/D10329
2017-05-02 02:32:10 +00:00
Enji Cooper
22c00e3b85 Only expect :grep_r_implied to pass with bsdgrep(1)
The test fails with gnu grep from base and ports.

Sponsored by:	Dell EMC Isilon
2017-04-22 21:40:31 +00:00
Ed Maste
3f39ffc893 bsdgrep: add BSD_GREP_FASTMATCH knob for built-in fastmatch
Bugs have been found in the fastmatch implementation as used in bsdgrep.
Some have been fixed (r316495) while fixes for others are in review
(D10098).

In comparison with the fastmatch implementation, Kyle Evans found that:

- regex(3)'s performance with literal expressions offers a speed
  improvement over fastmatch

- regex(3)'s performance, both with simple BREs and EREs, seems to be
  comparable

The regex implementation was imported in r226035, and the commit message
reports:

    This is a temporary solution until the whole regex library is
    not replaced so that BSD grep development can continue and the
    backported code gets some review and testing. This change only
    improves scalability slightly, there is no big performance boost
    yet but several minor bugs have been found and fixed.

Introduce a WITH_/WITHOUT_BSD_GREP_FASTMATCH knob to support testing
of both approaches.

PR:		175314, 194823
Submitted by:	Kyle Evans <kevans91 at ksu.edu>
Reviewed by:	bdrewery (in part)
Differential Revision:	https://reviews.freebsd.org/D10282
2017-04-21 14:36:09 +00:00
Ed Maste
e06ffa3230 bsdgrep: fix zero-length matches without the -o flag
r316477 broke zero-length matches when not using the -o flag, by
skipping over them entirely.

Add a regression test so that it doesn't break again in the future.

Submitted by:	Kyle Evans <kevans91 at ksu.edu>
Reviewed by:	cem emaste ngie
Differential Revision:	https://reviews.freebsd.org/D10333
2017-04-17 14:59:55 +00:00
Ed Maste
22130a21ba bsdgrep: remove output separators between overlapping segments
Make bsdgrep more sensitive to context overlaps.  If it's printing
context that either overlaps or is immediately adjacent to another bit
of context, don't print a separator.

- Non-overlapping segments no longer have two separators between them

- Overlapping segments no longer have separators between them with
  overlapping sections repeated

Submitted by:	Kyle Evans <kevans91 at ksu.edu>
Reviewed by:	cem
Differential Revision:	https://reviews.freebsd.org/D10105
2017-04-17 13:36:30 +00:00
Ed Maste
a461896a2f bsdgrep: for -r, use the working directory if none specified
This is more sensible than the previous behaviour of grepping stdin,
and matches newer GNU grep behaviour.

PR:		216307
Submitted by:	Kyle Evans <kevans91 at ksu.edu>
Reviewed by:	cem, emaste, ngie
Relnotes:	Yes
Differential Revision:	https://reviews.freebsd.org/
2017-04-17 13:22:39 +00:00
Ed Maste
5ee1ea02fd bsdgrep: add -z/--null-data support
-z treats input and output data as sequences of lines terminated by a
zero byte instead of a newline. This brings it more in line with GNU grep
and brings us closer to passing the current tests with BSD grep.

Submitted by:	Kyle Evans <kevans91 at ksu.edu>
Reviewed by:	cem
Relnotes:	Yes
Differential Revision:	https://reviews.freebsd.org/D10101
2017-04-17 13:14:18 +00:00
Ed Maste
d204af1e41 bsdgrep: Handle special case of single-byte NUL pattern
PR:		202022
Submitted by:	Kyle Evans <kevans91 at ksu.edu>
MFC after:	1 month
Differential Revision:	https://reviews.freebsd.org/D10102
2017-04-05 20:11:05 +00:00
Ed Maste
799c5faa8b bsdgrep: create additional tests for coverage on recent fixes
Create additional tests to cover regressions that were discovered by
PRs linked to reviews D10098, D10102, and D10104.

It is worth noting that neither bsdgrep(1) nor gnugrep(1) in the base
system currently pass all of these tests, and gnugrep(1) not quite being
up to snuff was also noted in at least one of the PRs.

PR:		175314 202022 195763 180990 197555 197531 181263 209116
Submitted by:	Kyle Evans <kevans91@ksu.edu>
Reviewed by:	cem, ngie, emaste
MFC after:	1 month
Differential Revision:	https://reviews.freebsd.org/D10112
2017-04-05 18:41:44 +00:00
Conrad Meyer
f48d142551 bsdgrep(1): Fix errors with invalid expressions
Invalid expressions with an ultimate compiled pattern length of 0 (e.g.,
"grep -E {") were not taken into account and caused a segfault while trying
to fill in the good suffix table.

Submitted by:	Kyle Evans <kevans91 at ksu.edu>
Reviewed by:	me
Differential Revision:	https://reviews.freebsd.org/D10113
2017-04-04 17:05:37 +00:00
Conrad Meyer
5dc41cb0f2 bsdgrep(1): Rip out "xmalloc" bits
xmalloc was a debug malloc implementation, but the x{malloc,calloc,free}
functions default to calling the malloc(3) equivalents.

Instead of relying on this malloc shim, we can devise better ways to debug
malloc issues that aren't misleading upon initial inspection.  (I.e., using
jemalloc's various built-in debugging capabilities.)

Submitted by:	Kyle Evans <kevans91 at ksu.edu>
Reviewed by:	emaste, cem
Differential Revision:	https://reviews.freebsd.org/D10269
2017-04-04 16:08:51 +00:00
Ed Maste
a5ed868511 bsdgrep: revert color changes from r316477
r316477 changed the color output to match exactly the in-tree GNU grep,
but introduces unnecessary escape sequences.

Submitted by:	Kyle Evans <kevans91 at ksu.edu>
Reported by:	ache
MFC after:	1 month
MFC with:	r316477
2017-04-04 14:17:50 +00:00
Ed Maste
a734ae9c14 bsdgrep: Initialize vars to avoid a false positive GCC warning
Reported by:	lwhsu
MFC after:	1 month
MFC with:	r316477
2017-04-04 13:34:19 +00:00
Conrad Meyer
bf10f246fe bsdgrep(1): create rgrep link
Create a convenience rgrep link for bsdgrep(1) that observes 'grep -r'
behavior.

A follow-up to r316473.

Submitted by:	Kyle Evans <kevans91 at ksu.edu>
Reviewed by:	emaste (earlier version), cem
Differential Revision:	https://reviews.freebsd.org/D10109
2017-04-04 05:56:42 +00:00
Ed Maste
87c485cfb5 bsdgrep: fix matching behaviour
- Set REG_NOTBOL if we've already matched beginning of line and we're
  examining later parts

- For each pattern we examine, apply it to the remaining bits of the
  line rather than (potentially) smaller subsets

- Check for REG_NOSUB after we've looked at all patterns initially
  matching the line

- Keep track of the last match we made to later determine if we're
  simply not matching any longer or if we need to proceed another byte
  because we hit a zero-length match

- Match the earliest and longest bit of each line before moving the
  beginning of what we match to further in the line, past the end of the
  longest match; this generally matches how gnugrep(1) seems to behave,
  and seems like pretty good behavior to me

- Finally, bail out of printing any matches if we were set to print all
  (empty pattern) but -o (output matches) was set

PR:		195763, 180990, 197555, 197531, 181263, 209116
Submitted by:	"Kyle Evans" <kevans91@ksu.edu>
Reviewed by:	cem
MFC after:	1 month
Relnotes:	Yes
Differential Revision:	https://reviews.freebsd.org/D10104
2017-04-03 23:16:51 +00:00
Ed Maste
491b8b1623 bsdgrep: treat rgrep as grep -r
Submitted by:	   Kyle Evans <kevans91@ksu.edu>
2017-04-03 21:10:02 +00:00
Ed Maste
6b91f51703 bsdgrep: document ignored option -u
MSDOS and Windows GNU grep uses -u to mean "print byte offsets as if
running on an UNIX system." The option has no effect on systems that
do not use CRLF line endings.

PR:		171200
Submitted by:	deeptech71@gmail.com, Anders Jensen-Waud
MFC after:	1 month
2017-02-19 17:40:24 +00:00
Ed Maste
5dff7be120 bsdgrep: fix EOF handling with --mmap
Rework part of the loop in grep_fgetln to return the rest of the line
and ensure that we still advance the buffer by the length of the rest
of the line.

PR:		165471
Submitted by:	Kyle Evans <kevans91@ksu.edu>
MFC after:	1 month
2017-02-19 17:23:27 +00:00
Dimitry Andric
00f060000f Fix a segfault in bsdgrep when parsing the invalid extended regexps "?"
or "+" (these are invalid, because there is no preceding operand).

When bsdgrep attempts to emulate GNU grep in discarding and ignoring the
invalid ? or + operators, some later logic in tre_compile_fast() goes
beyond the end of the buffer, leading to a crash.

Fix this by bailing out, and reporting a bad pattern instead.

Reported by:	Steve Kargl
MFC after:	1 week
2016-08-02 20:25:22 +00:00
Baptiste Daroussin
a43d43d574 Remove usage of _WITH_GETLINE from usr.bin 2016-07-30 01:07:47 +00:00
Ed Schouten
33f5799a81 Call basename() in a portable way.
Pull a copy of the filename string before calling basename(). Change the
loop to not return on its own, so we can put a free() statement at the
bottom.
2016-07-28 15:19:47 +00:00