MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / build_grep_cmd

Function build_grep_cmd

src/mcp/mcp.c:4490–4555  ·  view source on GitHub ↗

Build the grep/search command string based on scoped vs recursive mode. * On Windows, uses PowerShell Select-String with tab-delimited output. * On POSIX, uses grep with colon-delimited output. */

Source from the content-addressed store, hash-verified

4488 * On Windows, uses PowerShell Select-String with tab-delimited output.
4489 * On POSIX, uses grep with colon-delimited output. */
4490static void build_grep_cmd(char *cmd, size_t cmd_sz, bool use_regex, bool scoped,
4491 const char *file_pattern, const char *tmpfile, const char *filelist,
4492 const char *root_path) {
4493#ifdef _WIN32
4494 const char *sm = use_regex ? "" : " -SimpleMatch";
4495 if (scoped) {
4496 if (file_pattern) {
4497 snprintf(
4498 cmd, cmd_sz,
4499 "powershell -Command \"$pat = Get-Content '%s'; "
4500 "Get-Content '%s' | ForEach-Object { Select-String -LiteralPath $_ -Pattern $pat%s "
4501 "-ErrorAction SilentlyContinue }"
4502 " | Where-Object { $_.Path -like '*%s' }"
4503 " | ForEach-Object { $_.Path + [char]9 + $_.LineNumber + [char]9 + $_.Line }\"",
4504 tmpfile, filelist, sm, file_pattern);
4505 } else {
4506 snprintf(
4507 cmd, cmd_sz,
4508 "powershell -Command \"$pat = Get-Content '%s'; "
4509 "Get-Content '%s' | ForEach-Object { Select-String -LiteralPath $_ -Pattern $pat%s "
4510 "-ErrorAction SilentlyContinue }"
4511 " | ForEach-Object { $_.Path + [char]9 + $_.LineNumber + [char]9 + $_.Line }\"",
4512 tmpfile, filelist, sm);
4513 }
4514 } else {
4515 if (file_pattern) {
4516 snprintf(
4517 cmd, cmd_sz,
4518 "powershell -Command \"Get-ChildItem -Recurse -Path '%s\\*' -Include '%s' -File "
4519 "-ErrorAction SilentlyContinue"
4520 " | Select-String -Pattern (Get-Content '%s')%s -ErrorAction SilentlyContinue"
4521 " | ForEach-Object { $_.Path + [char]9 + $_.LineNumber + [char]9 + $_.Line }\"",
4522 root_path, file_pattern, tmpfile, sm);
4523 } else {
4524 snprintf(
4525 cmd, cmd_sz,
4526 "powershell -Command \"Get-ChildItem -Recurse -Path '%s\\*' -File -ErrorAction "
4527 "SilentlyContinue"
4528 " | Select-String -Pattern (Get-Content '%s')%s -ErrorAction SilentlyContinue"
4529 " | ForEach-Object { $_.Path + [char]9 + $_.LineNumber + [char]9 + $_.Line }\"",
4530 root_path, tmpfile, sm);
4531 }
4532 }
4533#else
4534 const char *flag = use_regex ? "-E" : "-F";
4535 if (scoped) {
4536 if (file_pattern) {
4537 /* -0: read NUL-separated paths from the filelist so paths containing
4538 * spaces stay one argument (issue #687). Pairs with the NUL separator
4539 * written by write_scoped_filelist. */
4540 snprintf(cmd, cmd_sz, "xargs -0 grep -Hn %s --include='%s' -f '%s' < '%s' 2>/dev/null",
4541 flag, file_pattern, tmpfile, filelist);
4542 } else {
4543 snprintf(cmd, cmd_sz, "xargs -0 grep -Hn %s -f '%s' < '%s' 2>/dev/null", flag, tmpfile,
4544 filelist);
4545 }
4546 } else {
4547 if (file_pattern) {

Callers 1

handle_search_codeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected