Junbo-Zheng opened a new pull request, #3741:
URL: https://github.com/apache/nuttx-apps/pull/3741
## Summary
Add a standalone `grep` application under `apps/system/grep/`, registered as
a built-in NSH app via `nuttx_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: `0` on match, `1` on no match,
- `2` on error.
`SYSTEM_GREP` does `select LIBC_REGEX`, so enabling `CONFIG_SYSTEM_GREP=y`
alone is sufficient -- the regex dependency and its `ALLOW_MIT_COMPONENTS` gate
are pulled in automatically by Kconfig, avoiding a link-time `undefined
reference to regcomp` if a user enables grep without the regex library.
## Impact
- **Users**: Adds a new `grep` builtin command to NSH; no existing command
or behavior is changed.
- **Build**: Adds a new `apps/system/grep/` app discovered via the existing
`system/*/Make.defs` wildcard; no shared build files are modified. New
`CONFIG_SYSTEM_GREP` Kconfig option (default `n`).
- **Hardware**: None -- host-side `sim` and any board with NSH + a
filesystem.
- **Documentation**: None -- usage follows standard grep conventions.
- **Security & Compatibility**: None -- opt-in (default `n`), no change to
existing symbols or behavior.
## Testing
Built and ran on `sim:nsh` (Ubuntu 22.04 host, gcc):
```
cd nuttx
tools/configure.sh sim:nsh
enable CONFIG_SYSTEM_GREP=y
make olddefconfig
make -j
./nuttx
```
`help` lists `grep` among the builtin apps:
Builtin Apps:
```
dd gcov grep nsh sh
```
Functional matrix verified inside NSH against a 4-line test file:
| command | result |
|---------|--------|
| `grep error` | case-sensitive match only |
| `grep -i error` | case-insensitive, both cases |
| `grep -in error` | matches with line numbers |
| `grep -v error` | inverted, non-matching lines |
| `grep "^ERR"` | regex anchor, line-head only |
| `grep nomatch` | no output, exit code 1 |
Pipe usage verified with real NSH commands -- grep works as a pipe consumer
reading stdin from other commands:
```
nsh>
nsh>
nsh>
nsh>
nsh> ps
TID PID PPID PRI POLICY TYPE NPX STATE EVENT SIGMASK
STACK COMMAND
0 0 0 0 FIFO Kthread - Ready
0000000000000000 0069584 Idle_Task
1 0 0 224 FIFO Kthread - Waiting Semaphore
0000000000000000 0067448 sim_loop_wq 0x7f4d736003f0 0x7f4d73600470
2 0 0 224 FIFO Kthread - Waiting Semaphore
0000000000000000 0067472 hpwork 0x40170fe0 0x40171060
4 4 0 100 FIFO Task - Running
0000000000000000 0067496 nsh_main
nsh>
nsh>
nsh>
nsh> ps | grep work
sh [7:100]
2 0 0 224 FIFO Kthread - Waiting Semaphore
0000000000000000 0067472 hpwork 0x40170fe0 0x40171060
8 8 4 100 FIFO Task - Ready
0000000000000000 0067488 grep work
nsh>
nsh>
nsh>
nsh> ps | grep sim
sh [9:100]
1 0 0 224 FIFO Kthread - Waiting Semaphore
0000000000000000 0067448 sim_loop_wq 0x7f4d736003f0 0x7f4d73600470
10 10 4 100 FIFO Task - Ready
0000000000000000 0067488 grep sim
nsh>
nsh>
nsh> uname -a
NuttX 12.13.0 0790b52e64 Aug 20 2026 23:30:32 sim sim
nsh>
nsh> help
help usage: help [-v] [<cmd>]
. dirname free mount set umount
[ date help mv kill unset
? df hexdump pidof pkill uptime
alias dmesg losetup poweroff sleep watch
unalias echo ln quit usleep xd
basename env ls printf source wait
break exec du ps test
cat exit mkdir pwd time
cd expr mkfatfs readlink true
cp false mkfifo rm truncate
cmp fdinfo mkrd rmdir uname
Builtin Apps:
dd gcov grep nsh sh
dumpstack gpio hello ostest
nsh>
```
Signed-off-by: Junbo Zheng <[email protected]>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]