use super::commands::classify_wrapped;
use super::{ClassifyError, ShellContext, Sink};
const FIND_EXEC: &[&str] = &["-exec", "-execdir", "-ok", "-okdir"];
const OUTPUT_SHORT: &[(&str, &str)] = &[("curl", "-o"), ("wget", "-O")];
pub(super) fn classify(
argv: &[String],
program: &str,
ctx: &ShellContext<'_>,
sink: &mut Sink,
depth: u8,
) -> Result<(), ClassifyError> {
let mut i = 1;
while let Some(arg) = argv.get(i) {
i += 1;
if program == "find" && FIND_EXEC.contains(&arg.as_str()) {
let rest = &argv[i..];
let end = rest
.iter()
.position(|a| a == ";" || a == "+")
.unwrap_or(rest.len());
if end > 0 {
classify_wrapped(&rest[..end], ctx, sink, depth)?;
}
i += end + 1;
continue;
}
let short = OUTPUT_SHORT
.iter()
.any(|(p, flag)| *p == program && arg == flag);
let target = match arg
.strip_prefix("--output=")
.or_else(|| arg.strip_prefix("--output-document="))
{
Some(value) => Some(value),
None if arg == "--output" || arg == "--output-document" || short => {
argv.get(i).map(String::as_str)
}
None => None,
};
if let Some(path) = target.filter(|p| !p.is_empty() && *p != "-") {
sink.write(ctx, path)?;
}
}
Ok(())
}