apps/system/grep: Add standalone grep with regexp support - #3741
Open
Junbo-Zheng wants to merge 1 commit into
Open
apps/system/grep: Add standalone grep with regexp support#3741Junbo-Zheng wants to merge 1 commit into
Junbo-Zheng wants to merge 1 commit into
Conversation
Add a standalone grep application under apps/system/grep/. It uses the NuttX libc regex library (regcomp/regexec) to support -i, -n, -v, -r, -H and -h options plus stdin input and recursive directory search. SYSTEM_GREP depends on LIBC_REGEX, so the regex library must be enabled first -- CONFIG_SYSTEM_GREP only becomes selectable once LIBC_REGEX is turned on. Signed-off-by: Junbo Zheng <zhengjunbo1@xiaomi.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add a standalone
grepapplication underapps/system/grep/, registered as a built-in NSH app vianuttx_add_application.The implementation uses the NuttX libc regex library (
regcomp/regexec) rather than substring matching, so it supports real regular expressions including anchors and character classes. Supported options:-i(case-insensitive),-n(line numbers),-v(invert match),-r(recursive directory search),-H(always print filename) and-h(never print filename), plus reading from stdin when no file is given. Exit codes follow POSIX grep:0on match,1on no match,2on error.SYSTEM_GREPdoesselect LIBC_REGEX, so enablingCONFIG_SYSTEM_GREP=yalone is sufficient -- the regex dependency and itsALLOW_MIT_COMPONENTSgate are pulled in automatically by Kconfig, avoiding a link-timeundefined reference to regcompif a user enables grep without the regex library.Impact
grepbuiltin command to NSH; no existing command or behavior is changed.apps/system/grep/app discovered via the existingsystem/*/Make.defswildcard; no shared build files are modified. NewCONFIG_SYSTEM_GREPKconfig option (defaultn).simand any board with NSH + a filesystem.n), no change to existing symbols or behavior.Testing
Built and ran on
sim:nsh(Ubuntu 22.04 host, gcc):helplistsgrepamong the builtin apps:Builtin Apps:
Functional matrix verified inside NSH against a 4-line test file:
grep errorgrep -i errorgrep -in errorgrep -v errorgrep "^ERR"grep nomatchPipe usage verified with real NSH commands -- grep works as a pipe consumer reading stdin from other commands:
Signed-off-by: Junbo Zheng zhengjunbo1@xiaomi.com