use crate::{cst, pathctx};
pub(crate) struct Operand {
pub(crate) text: String,
pub(crate) resolved: String,
}
pub(crate) fn operand_words(command: &str) -> Option<Vec<Operand>> {
let _guard = cst::check::ClassifyGuard::enter(command.len())?;
let Some(script) = cst::parse(command) else {
return Some(shell_words::split(command).ok()?.into_iter().map(operand).collect());
};
let mut out = Vec::new();
collect_script_words(&script, &mut out);
Some(out)
}
fn operand(word: String) -> Operand {
let opaque = cst::check::is_opaque_value;
let expanded = pathctx::expand_vars(&word, false).into_owned();
let text = if opaque(&expanded) && !opaque(&word) { word } else { expanded };
let resolved = pathctx::place(&text, None).into_judged();
let resolved = if opaque(&resolved) && !opaque(&text) { text.clone() } else { resolved };
Operand { text, resolved }
}
fn collect_word(word: &cst::Word, out: &mut Vec<Operand>) {
out.extend(word.expand().into_iter().map(operand));
for part in &word.0 {
collect_part_subs(part, out);
}
}
fn collect_part_subs(part: &cst::WordPart, out: &mut Vec<Operand>) {
use cst::WordPart;
match part {
WordPart::CmdSub(script) | WordPart::ProcSub(script) => collect_script_words(script, out),
WordPart::DQuote(inner) => collect_word(inner, out),
WordPart::Arith(inner) => collect_word(inner, out),
WordPart::Lit(_) | WordPart::Escape(_) | WordPart::SQuote(_) | WordPart::AnsiC(_) | WordPart::Backtick(_) => {}
}
}
fn collect_script_words(script: &cst::Script, out: &mut Vec<Operand>) {
cst::check::walk_with_scope(
script,
|stmt| {
for cmd in &stmt.pipeline.commands {
collect_cmd_words(cmd, out);
}
},
|(), ()| (),
);
}
fn collect_redir_words(redirs: &[cst::Redir], out: &mut Vec<Operand>) {
use cst::Redir;
for redir in redirs {
match redir {
Redir::Write { target, .. } | Redir::Read { target, .. } | Redir::ReadWrite { target, .. } | Redir::HereStr(target) => {
collect_word(target, out)
}
Redir::HereDoc { body, .. } => {
for part in &body.0 {
collect_part_subs(part, out);
}
}
Redir::DupFd { .. } => {}
}
}
}
fn collect_cmd_words(cmd: &cst::Cmd, out: &mut Vec<Operand>) {
use cst::Cmd;
let words = |ws: &[cst::Word], out: &mut Vec<Operand>| {
for w in ws {
collect_word(w, out);
}
};
match cmd {
Cmd::Simple(s) => {
words(&s.words, out);
collect_redir_words(&s.redirs, out);
}
Cmd::Subshell { body, redirs } | Cmd::BraceGroup { body, redirs } => {
collect_script_words(body, out);
collect_redir_words(redirs, out);
}
Cmd::For { items, body, redirs, .. } => {
words(items, out);
collect_script_words(body, out);
collect_redir_words(redirs, out);
}
Cmd::While { cond, body, redirs } | Cmd::Until { cond, body, redirs } => {
collect_script_words(cond, out);
collect_script_words(body, out);
collect_redir_words(redirs, out);
}
Cmd::If { branches, else_body, redirs } => {
collect_redir_words(redirs, out);
for branch in branches {
collect_script_words(&branch.cond, out);
collect_script_words(&branch.body, out);
}
if let Some(body) = else_body {
collect_script_words(body, out);
}
}
Cmd::DoubleBracket { words: ws, redirs } => {
words(ws, out);
collect_redir_words(redirs, out);
}
Cmd::Case { subject, arms, redirs } => {
collect_word(subject, out);
for arm in arms {
collect_script_words(&arm.body, out);
}
collect_redir_words(redirs, out);
}
Cmd::FunctionDef { body, .. } => collect_script_words(body, out),
}
}