aboutsummaryrefslogtreecommitdiff
path: root/usr.bin/grep
Commit message (Collapse)AuthorAgeFilesLines
* grep/tests: Fix up the zgrep executable path in a newly added testMark Johnston2026-01-121-1/+1
| | | | Fixes: da5d94d29a5e ("Remove obsolete BUGS section from zgrep(1) man page, add test")
* Remove obsolete BUGS section from zgrep(1) man page, add testCraig Leres2026-01-112-18/+11
| | | | | | | | | | I forgot to check/update the man page with D54217. While here add a test for multiple -e flags. Reviewed by: markj Approved by: markj MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D54632
* grep: add testcase to test color when matches is greater than MAX_MATCHESViacheslav Chimishuk2026-01-091-0/+1
| | | | | Reviewed by: imp, kevans Pull Request: https://github.com/freebsd/freebsd-src/pull/1442
* Fix zgrep(1) wrapper regression, add testCraig Leres2025-12-142-1/+11
| | | | | | | | | | | | @bdrewery reported D48873 broke "bzgrep -q '1 2' /var/log/messages.0.bz2" The mistake was in using $* instead of "$@" (I suspect $* is never right). Reported by: bdrewery Reviewed by: markj Approved by: markj Fixes: 1070477cc8b7 ("Fix remaining zgrep(1) wrapper script regressions") MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D54217
* grep: toss in some explicit fflush()Kyle Evans2025-08-081-0/+5
| | | | | | | | grep|tee of the src/ tree for infrequently-occurring strings is fairly annoying; drop some tactical flushes at line-match boundaries to reduce the long stalls. In the case of `grep -o`, we'll flush after multiple lines if there are multiple matches within a single line of text, while for non`-o` we'll flush generally after every line.
* Replace last few remaining MAN[1-8] with MANDag-Erling Smørgrav2025-07-261-1/+1
| | | | | Reviewed by: ivy Differential Revision: https://reviews.freebsd.org/D51530
* grep: avoid duplicated lines when we're coloring outputKyle Evans2025-03-202-13/+74
| | | | | | | | | | | | | | | | | | | | | For the default uncolored output, we'll just output a line once and then move on. For colored output, we'll output multiple matches per line with context from the line interspersed and may end up writing out some match context multiple times as we don't persist which part of the lines have already been printed. Fix it by tracking the length of line printed thus far in printline() and retaining it across successive calls to printline() in the same line. printline() should indicate whether it terminated the line or not to avoid tracking the logic for that in multiple places: -o lines are always terminated, so it's generally only some --color contexts where we wouldn't have terminated. Add a test to make sure that we're only printing one line going forward. Reported and tested by: Jamie Landeg-Jones <jamie catflap org> Reviewed by: emaste Differential Revision: https://reviews.freebsd.org/D49324
* Fix remaining zgrep(1) wrapper script regressionsCraig Leres2025-02-091-7/+45
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Fix short flags without whitespace, e.g: zgrep -wefoo test Fix multiple -e flags: zgrep -e foo -e xxx test Previously only the last pattern would be used. Clean up possible leading blank in ${grep_args}. Update comment: 2.51 -> 2.6.0 Add a test case for the last known zgrep wrapper issue: recursion (-r) not implemented. Reviewers: markj, kevans, ngie, bapt Reviewed By: markj Subscribers: imp Differential Revision: https://reviews.freebsd.org/D48873
* grep: Default to -p instead of -S.John Baldwin2024-09-041-1/+1
| | | | | | | | | | | This matches the documented behavior in the manpage as well as the default behavior on macOS. PR: 280676 Reported by: Radosław Piliszek <radoslaw.piliszek@gmail.com> Reviewed by: kevans MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D46256
* grep: Fix various bugs in recursive tree handlingJohn Baldwin2024-09-041-7/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The -OpS options were effectively ignored due to a collection of bugs in the use of fts(3): - fts_open(3) requires one of FTS_PHYSICAL or FTS_LOGICAL to be specified, but in the -O case, only FTS_COMFOLLOW was given. Fix this to use FTS_COMFOLLOW | FTS_PHYSICAL. - The switch on the entry type returned by fts_read() did not check for symbolic links, so symbolic links fell into the default case and were always passed to procfile() even when -p was given. Fix this by adding cases in the switch statement to explicitly ignore FTS_SL. - FTS_NOSTAT was passed to fts_open(), so fts_open() couldn't detect symbolic links when FTS_PHYSICAL was passed, instead both regular files and symbolic links were returned as FTS_NSOK entries. Fix by only using FTS_NOSTAT with FTS_LOGICAL. While here, fix a few other nits: - Treat FTS_NS as an error like FTS_DNR and FTS_ERR. - Just ignore FTS_DP. The logic to skip descending into skipped directories is only relevant when a directory is first visited, not after the directory has been visited. - Use warnc instead of warnx + strerror. PR: 280676 Reviewed by: kevans MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D46255
* Remove residual blank line at start of MakefileWarner Losh2024-07-151-1/+0
| | | | | | | This is a residual of the $FreeBSD$ removal. MFC After: 3 days (though I'll just run the command on the branches) Sponsored by: Netflix
* usr.bin: Automated cleanup of cdefs and other formattingWarner Losh2023-11-272-2/+0
| | | | | | | | | | | | | | | | Apply the following automated changes to try to eliminate no-longer-needed sys/cdefs.h includes as well as now-empty blank lines in a row. Remove /^#if.*\n#endif.*\n#include\s+<sys/cdefs.h>.*\n/ Remove /\n+#include\s+<sys/cdefs.h>.*\n+#if.*\n#endif.*\n+/ Remove /\n+#if.*\n#endif.*\n+/ Remove /^#if.*\n#endif.*\n/ Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/types.h>/ Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/param.h>/ Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/capsicum.h>/ Sponsored by: Netflix
* usr.bin: Remove ancient SCCS tags.Warner Losh2023-11-271-2/+0
| | | | | | | | Remove ancient SCCS tags from the tree, automated scripting, with two minor fixup to keep things compiling. All the common forms in the tree were removed with a perl script. Sponsored by: Netflix
* grep: don't rely on implementation-defined malloc(0) behaviorKyle Evans2023-11-051-0/+4
| | | | | | | | The very few places that rely on malloc/calloc of a zero-size region won't attempt to dereference it, so just return NULL rather than rolling the dice with the underlying malloc implementation. Reported by: brooks, Shawn Webb
* grep: Suppress EISDIR when reading.Dag-Erling Smørgrav2023-09-081-0/+2
| | | | | | | MFC after: 1 week Sponsored by: NetApp, Inc. Sponsored by: Klara, Inc. Reviewed by: kevans
* Remove my middle name.Dag-Erling Smørgrav2023-08-175-5/+5
|
* Remove $FreeBSD$: one-line nroff patternWarner Losh2023-08-161-1/+0
| | | | Remove /^\.\\"\s*\$FreeBSD\$$\n/
* Remove $FreeBSD$: two-line nroff patternWarner Losh2023-08-161-2/+0
| | | | Remove /^\.\\"\n\.\\"\s*\$FreeBSD\$$\n/
* Remove $FreeBSD$: one-line sh patternWarner Losh2023-08-167-7/+0
| | | | Remove /^\s*#[#!]?\s*\$FreeBSD\$.*$\n/
* Remove $FreeBSD$: one-line .c patternWarner Losh2023-08-164-8/+0
| | | | Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/
* Remove $FreeBSD$: one-line .c comment patternWarner Losh2023-08-165-5/+0
| | | | Remove /^/[*/]\s*\$FreeBSD\$.*\n/
* spdx: The BSD-2-Clause-FreeBSD identifier is obsolete, drop -FreeBSDWarner Losh2023-05-126-6/+6
| | | | | | | | | The SPDX folks have obsoleted the BSD-2-Clause-FreeBSD identifier. Catch up to that fact and revert to their recommended match of BSD-2-Clause. Discussed with: pfg MFC After: 3 days Sponsored by: Netflix
* Update/fix Makefile.depend for userlandSimon J. Gerraty2023-04-191-0/+1
|
* grep: remove tautological conditionKyle Evans2023-03-091-2/+1
| | | | | | st_size is an off_t, it cannot hold values larger than OFF_MAX. CID: 1008931
* Revert "grep: remove tautological condition"Kyle Evans2023-03-091-1/+2
| | | | | | This reverts commit f6d6c66889001208aee7b0c46efe1c8ddffda57c. Gremlins snuck into my tree and injected some WIP.
* grep: remove tautological conditionKyle Evans2023-03-091-2/+1
| | | | | | st_size is an off_t, it cannot hold values larger than OFF_MAX. CID: 1008931
* grep: properly switch EOL indicator with -zKyle Evans2023-01-042-0/+14
| | | | | | | | | | | | -z is supposed to use only the NUL byte as EOL, but we were inadvertently using both newline and NUL due to REG_NEWLINE in cflags. The odds of anyone relying on this bsdgrep-specific bug are quite low, so let's just fix it. At least one port in the wild has been reported to expect the intended behavior. Reported by: Hill Ma <maahiuzeon@gmail.com> Triaged by: the self-proclaimed peanut gallery on Discord
* grep.1: Add an ENVIRONMENT sectionJose Luis Duran2022-12-181-1/+17
| | | | | | | | Document the environment variables used by grep(1). Reviewed by: pauamma, kevans MFC after: 5 days Differential Revision: https://reviews.freebsd.org/D37726
* grep: correct argument name in man pageElvin Aslanov2022-11-011-1/+1
| | | | | | MFC after: 1 week Fixes: 4dc88ebedf94 ("Add BSD grep to the base system...") Pull Request: https://github.com/freebsd/freebsd-src/pull/622
* grep: Add quotes in manpage when using wildcards with --includeDaniel Engberg2022-10-051-2/+2
| | | | | | | Examples uses --include=*.h which doesn't work as intended Approved by: kevans Differential Revision: https://reviews.freebsd.org/D36883
* pkgbase: Put more binaries/lib in runtimeEmmanuel Vadot2021-12-211-0/+2
| | | | | | | | | Move some needed binaries/libs from FreeBSD-utilities to FreeBSD_runtime. This is everything needed to boot to multiuser with FreeBSD-rc installed. MFC after: 2 weeks Sponsored by: Beckhoff Automation GmbH & Co. KG Differential Revision: https://reviews.freebsd.org/D33435
* grep: fix/remove references to -PKyle Evans2021-11-102-5/+5
| | | | | | | | | | | | -P in gnugrepland means PCRE, which we do not support. We may eventually support it if onigmo ends up getting imported as a more performant regex implementation, and we can re-add it properly in these places (and more) when that time comes. The optstr change is a functional nop; the case was not explicitly handled, thus ending in usage() anyways. Reported by: Vladimir Misev (via twitter)
* grep: fix combination of quite and count flagMariusz Zaborski2021-07-091-1/+1
| | | | | | | | | | | | | | | | | When the quite (-q) flag is provided, we don't expect any output. Currently, the behavior is broken: $ grep -cq flag util.c 1 $ grep -cs flag util.c 55 First of all, we print a number to stdout. Secondly, it just returns 0 or 1 (which is unexpected). GNU grep with c and q flags doesn't print anything. Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D31108
* Fix zgrep --versionMateusz Piotrowski2021-04-181-3/+3
| | | | | | | | | | | | | | "zgrep --version" is expected to print the version information in the same way as "zgrep -V". However, the case handling the --version flag is never reached, so "zgrep --version" prints: zgrep: missing pattern instead of: grep (BSD grep, GNU compatible) 2.6.0-FreeBSD Reviewed by: yuripv Approved by: yuripv (src) MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D29813
* grep: Fix an incorrect description of the -C flagMateusz Piotrowski2021-04-172-14/+11
| | | | | | | | | | | It seems that the number of lines is no longer an optional parameter to the -C flag. Document it accordingly both in the manual page and the usage message. Reviewed by: yuripv Approved by: yuripv MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D28509
* usr.bin/grep: Fix Address OOB read errorAlex Richardson2021-02-092-5/+10
| | | | | | | | | | | | | | | | | | | | | | | I found this when compiling all the bootstrap tools with -fsanitize=addres: ==65590==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x62d000008400 at pc 0x000000473053 bp 0x7ffc1c7dd910 sp 0x7ffc1c7dd0b8 READ of size 32769 at 0x62d000008400 thread T0 #0 0x473052 in regexec (/local/scratch/alr48/cheri/build/freebsd-amd64-build/local/scratch/alr48/cheri/freebsd/amd64.amd64/tmp/legacy/bin/grep+0x473052) #1 0x4c9cf3 in procline /local/scratch/alr48/cheri/freebsd/usr.bin/grep/util.c:539:8 #2 0x4c8687 in procfile /local/scratch/alr48/cheri/freebsd/usr.bin/grep/util.c:379:18 #3 0x4c6596 in main /local/scratch/alr48/cheri/freebsd/usr.bin/grep/grep.c:714:8 0x62d000008400 is located 0 bytes to the right of 32768-byte region [0x62d000000400,0x62d000008400) allocated by thread T0 here: #0 0x493d5d in malloc (/local/scratch/alr48/cheri/build/freebsd-amd64-build/local/scratch/alr48/cheri/freebsd/amd64.amd64/tmp/legacy/bin/grep+0x493d5d) #1 0x4cad75 in grep_malloc /local/scratch/alr48/cheri/freebsd/usr.bin/grep/util.c:656:13 #2 0x4c8129 in procfile /local/scratch/alr48/cheri/freebsd/usr.bin/grep/util.c #3 0x4c6596 in main /local/scratch/alr48/cheri/freebsd/usr.bin/grep/grep.c:714:8 SUMMARY: AddressSanitizer: heap-buffer-overflow (/local/scratch/alr48/cheri/build/freebsd-amd64-build/local/scratch/alr48/cheri/freebsd/amd64.amd64/tmp/legacy/bin/grep+0x473052) in regexec Reviewed By: kevans MFC after: 1 week
* grep: fix -A handling in conjunction with -m match limitationKyle Evans2021-02-081-1/+20
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The basic issue here is that grep, when given -m 1, would stop all line processing once it hit the match count and exit immediately. The problem with exiting immediately is that -A processing only happens when subsequent lines are processed and do not match. The fix here is relatively easy; when bsdgrep matches a line, it resets the 'tail' of the matching context to the value supplied to -A and dumps anything that's been queued up for -B. After the current line has been printed and tail is reset, we check our mcount and do what's needed. Therefore, at the time that we decide we're doing nothing, we know that 'tail' of the context is correct and we can simply continue on if there's still more to pick up. With this change, we still bail out immediately if there's been no -A flag. If -A was supplied, we signal that we should continue on. However, subsequent lines will not even bothere to try and process the line. We have reached the match count, so even if the next line would match then we must process it if it hadn't. Thus, the loop in procfile() can short-circuit and just process the line as a non-match until procmatches() indicates that it's safe to stop. A test has been added to reflect both that we should be picking up the next line and that the next line should be considered a non-match even if it should have been. PR: 253350 MFC-after: 3 days
* grep: turn off -w if -x is specifiedKyle Evans2021-02-052-1/+8
| | | | | -x overcomes -w in gnugrep, and it should here as well. Flip it off as needed to avoid confusing other parts of grep.
* grep: fix null pattern and empty pattern file behaviorKyle Evans2021-02-052-30/+16
| | | | | | | | | | | | | | | | | | | | | | | | The null pattern semantics were terrible because I tried to match gnugrep, but I got it wrong. Let's unwind that: - The null pattern should match every line if neither -w nor -x. - The null pattern should match empty lines if -x. - The null pattern should not match any lines if -w. The first two will stop processing (shortcut) even if additional patterns are specified. In any other case, we will continue processing other patterns. If no other patterns are specified beside a null pattern, then we match if neither -w nor -x or set and do not match if either of those are specified. The justification for -w is that it should match on a whole word, but the null pattern deos not have a whole word to match on. Empty pattern files should never match anything, and more importantly, -v should cause everything to be written. PR: 253209 MFC-after: 4 days
* grep: fix LINKS in MakefileMartin Tournoij2021-01-181-1/+1
| | | | | | zstdegrep was listed twice, instead of zstdfgrep Pull Request: https://github.com/freebsd/freebsd-src/pull/450
* grep: tests: stop testing for a nonexistent version of grepKyle Evans2020-12-251-16/+3
| | | | Differential Revision: https://reviews.freebsd.org/D27732
* build: remove the option to build gnugrepKyle Evans2020-12-251-18/+8
| | | | | | | | | Unconditionally install bsdgrep as grep, bootstrap or not. Remove all build glue and stop installing both gnugrep and libgnuregex now that all consumers of the latter are gone. Relnotes: yes Differential Revision: https://reviews.freebsd.org/D27732
* grep: replace the internal queue with a ring bufferKyle Evans2020-12-093-52/+78
| | | | | | | | | | | | | We know up front how many items we can have in the queue (-B/Bflag), so pay the cost of those particular allocations early on. The reduced queue maintenance overhead seemed to yield about an ~8% improvement for my earlier `grep -C8 -r closefrom .` test. MFC after: 2 weeks Notes: svn path=/head/; revision=368483
* grep: tests: stop expecting a failure of gnuext w/ bsdgrepKyle Evans2020-12-091-3/+1
| | | | | | | | libregex now supports these and we no longer offer to not link against libregex. Notes: svn path=/head/; revision=368482
* fts_read: Handle error from a NULL return better.Bryan Drewery2020-12-081-1/+3
| | | | | | | | | | | | | | | | | | | | This is addressing cases such as fts_read(3) encountering an [EIO] from fchdir(2) when FTS_NOCHDIR is not set. That would otherwise be seen as a successful traversal in some of these cases while silently discarding expected work. As noted in r264201, fts_read() does not set errno to 0 on a successful EOF so it needs to be set before calling it. Otherwise we might see a random error from one of the iterations. gzip is ignoring most errors and could be improved separately. Reviewed by: vangyzen Sponsored by: Dell EMC Differential Revision: https://reviews.freebsd.org/D27184 Notes: svn path=/head/; revision=368467
* bsdgrep: don't link against libregex for bootstrapKyle Evans2020-12-061-0/+2
| | | | | | | | | | | | | | | | | r368355 removed the GNU_GREP_COMPAT knob (off by default) and forgot that bsdgrep may be built/used for bootstrap on some systems. All base uses should strive to use only POSIX-compliant expressions anyways and we haven't had libregex by default here up to this point, so just don't do that if we're bootstrapping. Note that the resulting binary has the wrong `grep -V` information as it falsely claims to be GNU compatible, but it is only for bootstrap. Reported by: GitHub cross-builds via yuripv Notes: svn path=/head/; revision=368392
* Retire GNU_GREP_COMPAT knobKyle Evans2020-12-052-9/+1
| | | | | | | | | | | | | This was introduced and then disabled by default primarily to avoid dealing with bugs in libgnuregex. rS363823 switched to using libregex for it, so let's just rip the option out now so we can make sure we're getting tested with libregex via bsdgrep. Reviewed by: emaste Differential Revision: https://reviews.freebsd.org/D27476 Notes: svn path=/head/; revision=368355
* bzgrep: make flag --no-filename workEugene Grosbein2020-11-201-4/+4
| | | | | | | | PR: 248813 MFC after: 1 week Notes: svn path=/head/; revision=367866
* grep(1): Add more EXAMPLESFernando Apesteguía2020-11-191-8/+57
| | | | | | | | | | | | | | * Add more EXAMPLES covering flags: -A, -B, -c, -f, -i, -H, -l, -q, -R, -w * While here, change existing wording to use the imperative (remove "To find") * Reword first example to be consistent with how grep(1) understand words (-w) Approved by: manpages (bcr@) Differential Revision: https://reviews.freebsd.org/D27264 Notes: svn path=/head/; revision=367850
* zgrep: fix exit status with multiple filesEric van Gyzen2020-10-011-5/+12
| | | | | | | | | | | | | | zgrep should exit with success when given multiple files and the pattern is found in at least one file. Prior to this change, it would exit with success only if the pattern was found in _every_ file. Reviewed by: dab ngie MFC after: 2 weeks Sponsored by: Dell EMC Isilon Differential Revision: https://reviews.freebsd.org/D26616 Notes: svn path=/head/; revision=366345