use crate::shell::{CommandOutput, Shell};
use regex::Regex;
use std::sync::atomic::Ordering;
const GREP_HELP_TEXT: &str = "\
Usage: grep [OPTION]... PATTERN [FILE]...
rg [OPTION]... PATTERN [PATH]...
Pattern matching:
-F, --fixed-strings Interpret pattern as fixed string
-e, --regexp PATTERN Use PATTERN as the pattern
-w, --word-regexp Match whole words only
Matching control:
-i Ignore case
-v Invert match (select non-matching lines)
-c Count matching lines
-o, --only-matching Print only matched parts
-l, --files-with-matches Print only file names
-n Show line numbers
-r, -R Recursive search
-N, --no-line-number Suppress line numbers (rg)
rg extras: -g (glob), -t (type), -T (type-not), -A/-B/-C (context),
-m (max-count), --max-depth, -S (smart-case), --count
-h, --help Show this help\n";
impl Shell {
pub fn cmd_rg(&self, args: &[&str], stdin: Option<&str>) -> CommandOutput {
if args.contains(&"-h") || args.contains(&"--help") {
return CommandOutput::success(GREP_HELP_TEXT.to_string());
}
let mut flags: Vec<String> = Vec::new();
let mut positional: Vec<String> = Vec::new();
let mut no_line_number = false;
let mut count_or_list = false;
let mut i = 0;
while i < args.len() {
let a = args[i];
match a {
"-N" | "--no-line-number" => no_line_number = true,
"-e" | "--regexp" => {
if i + 1 < args.len() {
positional.insert(0, args[i + 1].to_string());
i += 1;
}
}
"-g" | "--glob" | "-t" | "--type" | "-T" | "--type-not" | "-m" | "--max-count"
| "--max-depth" => {
i += 1;
}
"-A" | "-B" | "-C" | "--after-context" | "--before-context" | "--context" => {
if i + 1 < args.len() {
let flag = match args[i] {
"--after-context" => "-A",
"--before-context" => "-B",
"--context" => "-C",
other => other,
};
flags.push(flag.to_string());
flags.push(args[i + 1].to_string());
i += 1;
}
}
"--no-filename" => flags.push("--no-filename".to_string()),
"--fixed-strings" => flags.push("-F".to_string()),
"--ignore-case" | "-S" | "--smart-case" => flags.push("-i".to_string()),
"--files-with-matches" => {
flags.push("-l".to_string());
count_or_list = true;
}
"--word-regexp" => flags.push("-w".to_string()),
"--invert-match" => flags.push("-v".to_string()),
"--count" => {
flags.push("-c".to_string());
count_or_list = true;
}
a if a.starts_with("--") => {
crate::warn!("grep: warning: unsupported option '{}'", a);
} a if a.starts_with('-') && a.len() > 1 => {
if a.contains('c') || a.contains('l') {
count_or_list = true;
}
flags.push(a.to_string());
}
_ => positional.push(a.to_string()),
}
i += 1;
}
if !no_line_number && !count_or_list {
flags.push("-n".to_string());
}
let has_paths = positional.len() > 1;
if !has_paths && stdin.is_none() {
flags.push("-r".to_string());
positional.push(".".to_string());
} else if has_paths {
let any_dir = positional[1..].iter().any(|p| {
self.vfs
.resolve(p, &self.cwd)
.map(|r| r.is_dir())
.unwrap_or(false)
});
if any_dir {
flags.push("-r".to_string());
}
}
let mut full: Vec<&str> = flags.iter().map(|s| s.as_str()).collect();
full.extend(positional.iter().map(|s| s.as_str()));
self.cmd_grep(&full, stdin)
}
pub fn cmd_grep(&self, args: &[&str], stdin: Option<&str>) -> CommandOutput {
let help_requested = match args.iter().position(|a| *a == "--") {
Some(sep) => args[..sep].contains(&"--help"),
None => args.contains(&"--help"),
};
if help_requested {
return CommandOutput::success(GREP_HELP_TEXT.to_string());
}
let mut past_sep = false;
let mut pattern: Option<String> = None;
let mut extended = false;
let mut files = Vec::new();
let mut ignore_case = false;
let mut invert = false;
let mut count_only = false;
let mut show_line_number = false;
let mut fixed_strings = false;
let mut recursive = false;
let mut files_with_matches = false;
let mut only_matching = false;
let mut word_regexp = false;
let mut quiet = false;
let mut after_context: usize = 0;
let mut before_context: usize = 0;
let mut line_regexp = false;
let mut max_count: Option<usize> = None;
let mut files_without_match = false;
let mut no_filename = false;
let mut i = 0;
while i < args.len() {
let arg = args[i];
if past_sep {
if pattern.is_none() {
pattern = Some(arg.to_string());
} else {
files.push(arg.to_string());
}
i += 1;
continue;
}
match arg {
"-i" => ignore_case = true,
"-v" => invert = true,
"-c" => count_only = true,
"-n" => show_line_number = true,
"-F" | "--fixed-strings" => fixed_strings = true,
"-r" | "-R" => recursive = true,
"-l" | "--files-with-matches" => files_with_matches = true,
"-o" | "--only-matching" => only_matching = true,
"-w" | "--word-regexp" => word_regexp = true,
"-q" | "--quiet" | "--silent" => quiet = true,
"-x" | "--line-regexp" => line_regexp = true,
"-L" | "--files-without-match" => files_without_match = true,
"-h" | "--no-filename" => no_filename = true,
"-H" | "--with-filename" => no_filename = false,
"-m" | "--max-count" => {
if i + 1 < args.len() {
max_count = args[i + 1].parse().ok();
i += 1;
}
}
"-E" | "--extended-regexp" | "-P" | "--perl-regexp" => {
extended = true;
}
"-A" | "--after-context" => {
if i + 1 < args.len() {
after_context = args[i + 1].parse().unwrap_or(0);
i += 1;
}
}
"-B" | "--before-context" => {
if i + 1 < args.len() {
before_context = args[i + 1].parse().unwrap_or(0);
i += 1;
}
}
"-C" | "--context" => {
if i + 1 < args.len() {
after_context = args[i + 1].parse().unwrap_or(0);
before_context = args[i + 1].parse().unwrap_or(0);
i += 1;
}
}
"--" => past_sep = true, a if a.starts_with("-A") && a.len() > 2 => {
after_context = a[2..].parse().unwrap_or(0);
}
a if a.starts_with("-B") && a.len() > 2 => {
before_context = a[2..].parse().unwrap_or(0);
}
a if a.starts_with("-C") && a.len() > 2 => {
after_context = a[2..].parse().unwrap_or(0);
before_context = a[2..].parse().unwrap_or(0);
}
a if a.starts_with("-m") && a.len() > 2 => {
max_count = a[2..].parse().ok();
}
a if a.starts_with('-') && a.len() > 2 => {
for ch in a.chars().skip(1) {
match ch {
'i' => ignore_case = true,
'v' => invert = true,
'c' => count_only = true,
'n' => show_line_number = true,
'F' => fixed_strings = true,
'r' | 'R' => recursive = true,
'l' => files_with_matches = true,
'o' => only_matching = true,
'w' => word_regexp = true,
'q' => quiet = true,
'x' => line_regexp = true,
'L' => files_without_match = true,
'h' => no_filename = true,
'E' => {
extended = true;
}
'P' => {
extended = true;
} _ => crate::warn!("grep: warning: unsupported option '-{}'", ch),
}
}
}
_ if arg.starts_with('-') => {
crate::warn!("grep: warning: unsupported option '{}'", arg);
} _ if pattern.is_none() => pattern = Some(arg.to_string()),
_ => files.push(arg.to_string()),
}
i += 1;
}
let pattern = match pattern {
Some(p) => p,
None => return CommandOutput::error("grep: missing pattern\n".to_string(), 2),
};
let base = if fixed_strings {
regex::escape(&pattern)
} else if extended {
pattern.clone()
} else {
bre_to_regex(&pattern)
};
let effective_pattern = if line_regexp {
format!(r"^(?:{})$", base)
} else if word_regexp {
format!(r"\b{}\b", base)
} else {
base
};
let regex = match build_regex(&effective_pattern, ignore_case) {
Ok(r) => Some(r),
Err(e) => {
return CommandOutput::error(format!("grep: {e}"), 2);
}
};
let matcher: Box<dyn Fn(&str) -> Option<Vec<String>>> = match regex {
Some(re) => {
if only_matching {
Box::new(move |line: &str| {
let found: Vec<String> =
re.find_iter(line).map(|m| m.as_str().to_string()).collect();
if found.is_empty() {
None
} else {
Some(found)
}
})
} else {
Box::new(move |line: &str| {
if re.is_match(line) {
Some(vec![line.to_string()])
} else {
None
}
})
}
}
None => {
let p = if word_regexp {
pattern.clone()
} else {
pattern.clone()
};
if ignore_case {
let pl = p.to_lowercase();
if only_matching {
Box::new(move |line: &str| {
let lower = line.to_lowercase();
if lower.contains(&pl) {
Some(vec![find_substring_match(line, &p, true)])
} else {
None
}
})
} else {
Box::new(move |line: &str| {
if line.to_lowercase().contains(&pl) {
Some(vec![line.to_string()])
} else {
None
}
})
}
} else {
if only_matching {
Box::new(move |line: &str| {
if line.contains(&p) {
Some(vec![p.clone()])
} else {
None
}
})
} else {
Box::new(move |line: &str| {
if line.contains(&p) {
Some(vec![line.to_string()])
} else {
None
}
})
}
}
}
};
let mut output = String::new();
let mut stderr = String::new();
let mut file_errors = 0usize;
let mut total_matches = 0usize;
let mut all_files: Vec<String> = Vec::new();
for file in &files {
let path = self.vfs.resolve(file, &self.cwd);
match path {
Ok(p) if p.is_dir() && recursive => {
self.collect_files_recursive(file, &mut all_files, &mut stderr);
}
_ => {
all_files.push(file.to_string());
}
}
}
if all_files.is_empty() && files.is_empty() {
let input = match stdin {
Some(s) => s.to_string(),
None => return CommandOutput::error("grep: missing file operand\n".to_string(), 2),
};
let count = grep_lines(
&input,
&matcher,
invert,
count_only,
show_line_number,
files_with_matches,
only_matching,
quiet,
before_context,
after_context,
max_count,
None,
&mut output,
);
total_matches = count;
} else {
let multi_file = all_files.len() > 1;
for file in &all_files {
if self.cancel.load(Ordering::SeqCst) {
return CommandOutput {
stdout: output,
stderr: format!("{}cancelled\n", stderr),
exit_code: 143,
};
}
match self.read_text_lossy(file) {
Ok(content) => {
let count = grep_lines(
&content,
&matcher,
invert,
count_only,
show_line_number,
files_with_matches || files_without_match,
only_matching,
quiet,
before_context,
after_context,
max_count,
Some((file, multi_file && !no_filename)),
&mut output,
);
if count > 0 {
if files_with_matches {
output.push_str(file);
output.push('\n');
}
total_matches += 1; } else if !files_with_matches {
total_matches += 0; }
if files_without_match {
if count == 0 {
output.push_str(file);
output.push('\n');
}
}
if !files_with_matches {
total_matches += count;
}
if quiet && total_matches > 0 {
break;
}
}
Err(e) => {
stderr.push_str(&format!("grep: {}: {}\n", file, e));
file_errors += 1;
}
}
}
}
let exit_code = if file_errors > 0 {
2
} else if files_without_match {
if output.is_empty() {
1
} else {
0
}
} else if total_matches == 0 {
1
} else {
0
};
CommandOutput {
stdout: output,
stderr,
exit_code,
}
}
fn collect_files_recursive(&self, dir_path: &str, out: &mut Vec<String>, stderr: &mut String) {
if self.cancel.load(Ordering::SeqCst) {
return;
}
match self.vfs.list_dir(dir_path, &self.cwd) {
Ok(entries) => {
for entry in &entries {
let full = if dir_path.ends_with('/') {
format!("{}{}", dir_path, entry.name)
} else {
format!("{}/{}", dir_path, entry.name)
};
if entry.is_dir {
self.collect_files_recursive(&full, out, stderr);
} else {
out.push(full);
}
}
}
Err(e) => {
stderr.push_str(&format!("grep: {}: {}\n", dir_path, e));
}
}
}
}
fn bre_to_regex(pat: &str) -> String {
let mut out = String::new();
let mut chars = pat.chars().peekable();
while let Some(c) = chars.next() {
if c == '\\' {
match chars.next() {
Some(n @ ('+' | '?' | '|' | '(' | ')' | '{' | '}')) => out.push(n),
Some(n) => {
out.push('\\');
out.push(n);
}
None => out.push('\\'),
}
} else if matches!(c, '+' | '?' | '|' | '(' | ')' | '{' | '}') {
out.push('\\');
out.push(c);
} else {
out.push(c);
}
}
out
}
fn build_regex(pattern: &str, ignore_case: bool) -> Result<Regex, regex::Error> {
if ignore_case {
Regex::new(&format!("(?i){}", pattern))
} else {
Regex::new(pattern)
}
}
fn find_substring_match(line: &str, pattern: &str, ignore_case: bool) -> String {
if ignore_case {
let lower = line.to_lowercase();
let pl = pattern.to_lowercase();
if let Some(pos) = lower.find(&pl) {
line[pos..pos + pattern.len()].to_string()
} else {
pattern.to_string()
}
} else {
if let Some(pos) = line.find(pattern) {
line[pos..pos + pattern.len()].to_string()
} else {
pattern.to_string()
}
}
}
fn grep_lines(
content: &str,
matcher: &dyn Fn(&str) -> Option<Vec<String>>,
invert: bool,
count_only: bool,
show_line_number: bool,
files_with_matches: bool,
only_matching: bool,
quiet: bool,
before_context: usize,
after_context: usize,
max_count: Option<usize>,
file_label: Option<(&str, bool)>, output: &mut String,
) -> usize {
if files_with_matches || quiet {
for line in content.lines() {
let matches = matcher(line);
let has_match = matches.is_some();
if (invert && !has_match) || (!invert && has_match) {
return 1;
}
}
return 0;
}
let (fname, multi) = file_label.unwrap_or(("", false));
let lines: Vec<&str> = content.lines().collect();
let matched: Vec<bool> = lines
.iter()
.map(|l| {
let has = matcher(l).is_some();
if invert {
!has
} else {
has
}
})
.collect();
let mut count = matched.iter().filter(|&&m| m).count();
if let Some(m) = max_count {
count = count.min(m);
}
if count_only {
let prefix = if multi {
format!("{}:", fname)
} else {
String::new()
};
output.push_str(&format!("{}{}\n", prefix, count));
return count;
}
let context_on = before_context > 0 || after_context > 0;
if context_on {
let mut last_printed: isize = -(before_context as isize + after_context as isize + 2);
let mut i = 0;
let mut printed_matches = 0usize;
while i < lines.len() {
if !matched[i] {
i += 1;
continue;
}
if let Some(m) = max_count {
if printed_matches >= m {
break;
}
}
printed_matches += 1;
let start = i.saturating_sub(before_context);
let end = (i + after_context + 1).min(lines.len());
if (start as isize) > last_printed + 1 && !output.is_empty() {
output.push_str("--\n");
}
for j in start..end {
let prefix = if multi {
if show_line_number {
format!("{}-{}-", fname, j + 1)
} else {
format!("{}-", fname)
}
} else if show_line_number {
format!("{}-", j + 1)
} else {
String::new()
};
output.push_str(&prefix);
output.push_str(lines[j]);
output.push('\n');
last_printed = j as isize;
}
i = end;
}
return count;
}
let mut printed_matches = 0usize;
for (line_num, line) in lines.iter().enumerate() {
if !matched[line_num] {
continue;
}
if let Some(m) = max_count {
if printed_matches >= m {
break;
}
}
printed_matches += 1;
let prefix = if multi {
format!("{}:", fname)
} else {
String::new()
};
if only_matching {
if let Some(matches) = matcher(line) {
for m in matches {
if !prefix.is_empty() {
output.push_str(&prefix);
}
if show_line_number {
output.push_str(&format!("{}:", line_num + 1));
}
output.push_str(&m);
output.push('\n');
}
}
} else {
if !prefix.is_empty() {
output.push_str(&prefix);
}
if show_line_number {
output.push_str(&format!("{}:", line_num + 1));
}
output.push_str(line);
output.push('\n');
}
}
count
}
#[cfg(test)]
mod tests {
use crate::shell::Shell;
use crate::vfs::Vfs;
use std::sync::atomic::{AtomicUsize, Ordering};
static TEST_COUNTER: AtomicUsize = AtomicUsize::new(0);
fn mk_shell() -> Shell {
use std::fs;
let n = TEST_COUNTER.fetch_add(1, Ordering::SeqCst);
let dir =
std::env::temp_dir().join(format!("fastshell_grep_test_{}_{}", std::process::id(), n));
let _ = fs::remove_dir_all(&dir);
let vfs = Vfs::new(dir).unwrap();
Shell::new(vfs)
}
#[test]
fn test_grep_help() {
let mut shell = mk_shell();
let out = shell.execute("grep", &["--help"], None);
assert_eq!(out.exit_code, 0);
assert!(!out.stdout.is_empty());
}
#[test]
fn test_grep_help_long() {
let mut shell = mk_shell();
let out = shell.execute("grep", &["--help"], None);
assert_eq!(out.exit_code, 0);
assert!(!out.stdout.is_empty());
}
#[test]
fn test_rg_help() {
let mut shell = mk_shell();
let out = shell.execute("rg", &["-h"], None);
assert_eq!(out.exit_code, 0);
assert!(!out.stdout.is_empty());
}
#[test]
fn test_rg_help_long() {
let mut shell = mk_shell();
let out = shell.execute("rg", &["--help"], None);
assert_eq!(out.exit_code, 0);
assert!(!out.stdout.is_empty());
}
#[test]
fn test_grep_quiet_no_output_on_match() {
let mut shell = mk_shell();
let out = shell.execute("grep", &["-q", "foo"], Some("foobar\nbaz\n"));
assert_eq!(out.exit_code, 0, "grep -q with match should exit 0");
assert!(
out.stdout.is_empty(),
"grep -q should not print matches: {}",
out.stdout
);
}
#[test]
fn test_grep_quiet_exit_1_on_no_match() {
let mut shell = mk_shell();
let out = shell.execute("grep", &["-q", "zzz"], Some("foobar\nbaz\n"));
assert_eq!(out.exit_code, 1, "grep -q without match should exit 1");
}
#[test]
fn test_grep_extended_regexp_no_warning() {
let mut shell = mk_shell();
let out = shell.execute("grep", &["-E", "^foo[0-9]+$"], Some("foo123\nbar\n"));
assert_eq!(out.exit_code, 0);
assert!(out.stdout.contains("foo123"));
assert!(
!out.stderr.contains("unsupported"),
"-E should not warn: {}",
out.stderr
);
}
#[test]
fn test_grep_context_after() {
let mut shell = mk_shell();
let out = shell.execute("grep", &["-A", "1", "foo"], Some("a\nfoo\nb\nc\n"));
assert_eq!(out.exit_code, 0);
assert!(
out.stdout.contains("foo"),
"match line should be present: {}",
out.stdout
);
assert!(
out.stdout.contains("\nb\n"),
"after-context line should follow"
);
assert!(
!out.stdout.contains("\nc\n"),
"line outside context should be absent"
);
}
#[test]
fn test_grep_context_before() {
let mut shell = mk_shell();
let out = shell.execute("grep", &["-B", "1", "foo"], Some("a\nfoo\nb\n"));
assert!(
out.stdout.contains("a"),
"before-context line should be present: {}",
out.stdout
);
assert!(out.stdout.contains("foo"));
}
#[test]
fn test_grep_context_both() {
let mut shell = mk_shell();
let out = shell.execute("grep", &["-C", "1", "foo"], Some("a\nfoo\nb\n"));
assert!(out.stdout.contains("a"));
assert!(out.stdout.contains("foo"));
assert!(out.stdout.contains("b"));
}
#[test]
fn test_grep_context_separator() {
let mut shell = mk_shell();
let out = shell.execute("grep", &["-A", "1", "x"], Some("x\n1\n2\n3\n4\nx\n5\n"));
assert!(
out.stdout.contains("--"),
"separate context groups should be split by --: {}",
out.stdout
);
}
}