Skip to main content

specfold_core/runner/
tools.rs

1//! The programs a shell command needs, and which of them are not installed. A missing program
2//! inside a substitution (`diff <(jq …) <(jq …)`) can make a check compare two empty outputs and
3//! pass, so substitutions are read too.
4
5use std::path::Path;
6
7const BUILTINS: &[&str] = &[
8    "cd", "echo", "printf", "test", "[", "[[", "true", "false", "exit", "export", "set", "unset", "read", "shift",
9    "return", "local", "eval", "source", ".", ":", "pwd", "type", "wait", "trap", "alias", "ulimit", "umask", "let",
10    "declare", "readonly", "break", "continue", "getopts", "hash", "kill",
11];
12/// Words after which the next word is a command.
13const LEADING: &[&str] =
14    &["if", "then", "else", "elif", "while", "until", "do", "!", "{", "(", "exec", "command", "nohup"];
15/// Words that open a construct whose first word is not a command.
16const OPAQUE: &[&str] = &["for", "case", "select", "function", "fi", "done", "esac", "}", ")", "in"];
17/// Wrappers that take options or a value before the command they run.
18const WRAPPERS: &[&str] = &["env", "time", "nice", "timeout", "xargs", "sudo"];
19
20/// Splits off `$( … )`, `<( … )`, `>( … )` and backtick bodies, returning the outer text with
21/// each replaced by a placeholder word, and the bodies.
22fn substitutions(cmd: &str) -> (String, Vec<String>) {
23    let chars: Vec<char> = cmd.chars().collect();
24    let (mut outer, mut inner) = (String::new(), vec![]);
25    let (mut single, mut i) = (false, 0);
26    while i < chars.len() {
27        let c = chars[i];
28        if c == '\'' {
29            single = !single;
30        }
31        let opens = !single && i + 1 < chars.len() && matches!(c, '$' | '<' | '>') && chars[i + 1] == '(';
32        if opens || (!single && c == '`') {
33            let start = if c == '`' { i + 1 } else { i + 2 };
34            let mut depth = 1;
35            let mut j = start;
36            while j < chars.len() && depth > 0 {
37                match (c, chars[j]) {
38                    ('`', '`') => depth = 0,
39                    (_, '(') if c != '`' => depth += 1,
40                    (_, ')') if c != '`' => depth -= 1,
41                    _ => {}
42                }
43                j += 1;
44            }
45            let end = if depth == 0 { j - 1 } else { j };
46            inner.push(chars[start..end].iter().collect());
47            outer.push_str(" _sub_ ");
48            i = j;
49            continue;
50        }
51        outer.push(c);
52        i += 1;
53    }
54    (outer, inner)
55}
56
57/// An `&` that belongs to a redirection (`2>&1`, `<&0`, `&>file`, `>&2`) rather than a list.
58fn redirect_amp(chars: &[char], i: usize) -> bool {
59    let prev = i.checked_sub(1).map(|p| chars[p]);
60    let next = chars.get(i + 1).copied();
61    matches!(prev, Some('>' | '<')) || (next == Some('>') && prev != Some('&'))
62}
63
64/// Splits on unquoted `&&`, `||`, `;`, `|`, `&` and newlines.
65fn segments(cmd: &str) -> Vec<String> {
66    let chars: Vec<char> = cmd.chars().collect();
67    let (mut out, mut cur) = (vec![], String::new());
68    let (mut single, mut double) = (false, false);
69    for (i, &c) in chars.iter().enumerate() {
70        match c {
71            '\'' if !double => single = !single,
72            '"' if !single => double = !double,
73            '&' if !single && !double && redirect_amp(&chars, i) => {}
74            ';' | '|' | '&' | '\n' if !single && !double => {
75                out.push(std::mem::take(&mut cur));
76                continue;
77            }
78            _ => {}
79        }
80        cur.push(c);
81    }
82    out.push(cur);
83    out
84}
85
86/// Whitespace-separated words with quotes removed.
87fn words(segment: &str) -> Vec<String> {
88    let (mut out, mut cur) = (vec![], String::new());
89    let (mut single, mut double) = (false, false);
90    for c in segment.chars() {
91        match c {
92            '\'' if !double => single = !single,
93            '"' if !single => double = !double,
94            c if c.is_whitespace() && !single && !double => {
95                if !cur.is_empty() {
96                    out.push(std::mem::take(&mut cur));
97                }
98            }
99            c => cur.push(c),
100        }
101    }
102    if !cur.is_empty() {
103        out.push(cur);
104    }
105    out
106}
107
108fn is_assignment(w: &str) -> bool {
109    w.split_once('=').is_some_and(|(name, _)| {
110        !name.is_empty()
111            && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
112            && !name.starts_with(|c: char| c.is_ascii_digit())
113    })
114}
115
116/// The word in a segment that names the program it runs, if one can be read without guessing.
117fn program(segment: &str) -> Option<String> {
118    let mut ws = words(segment).into_iter().peekable();
119    while let Some(w) = ws.next() {
120        if OPAQUE.contains(&w.as_str()) {
121            return None;
122        }
123        if LEADING.contains(&w.as_str()) || is_assignment(&w) {
124            continue;
125        }
126        if WRAPPERS.contains(&w.as_str()) {
127            while ws.peek().is_some_and(|n| {
128                n.starts_with('-')
129                    || is_assignment(n)
130                    || n.chars().all(|c| c.is_ascii_digit() || c == '.' || c == 's' || c == 'm')
131            }) {
132                ws.next();
133            }
134            continue;
135        }
136        if w == "_sub_" || w.contains(['$', '*', '?', '<', '>']) || BUILTINS.contains(&w.as_str()) {
137            return None;
138        }
139        return Some(w);
140    }
141    None
142}
143
144fn installed(root: &Path, prog: &str) -> bool {
145    if prog.contains('/') {
146        let p = Path::new(prog);
147        return if p.is_absolute() { p.exists() } else { root.join(p).exists() };
148    }
149    let Some(path) = std::env::var_os("PATH") else { return true };
150    std::env::split_paths(&path).any(|dir| is_executable(&dir.join(prog)))
151}
152
153#[cfg(unix)]
154fn is_executable(p: &Path) -> bool {
155    use std::os::unix::fs::PermissionsExt;
156    p.metadata().is_ok_and(|m| m.is_file() && m.permissions().mode() & 0o111 != 0)
157}
158
159#[cfg(not(unix))]
160fn is_executable(p: &Path) -> bool {
161    p.is_file()
162}
163
164/// Programs the command needs that cannot be found. Words that cannot be read without
165/// running the shell (variables, globs) are not judged.
166pub fn missing_programs(root: &Path, cmd: &str) -> Vec<String> {
167    if cfg!(windows) {
168        return vec![];
169    }
170    let (outer, inner) = substitutions(cmd);
171    let mut out: Vec<String> =
172        segments(&outer).iter().filter_map(|s| program(s)).filter(|p| !installed(root, p)).collect();
173    for body in inner {
174        out.extend(missing_programs(root, &body));
175    }
176    out.sort();
177    out.dedup();
178    out
179}
180
181#[cfg(test)]
182mod tests {
183    use super::*;
184
185    fn progs(cmd: &str) -> Vec<String> {
186        let (outer, inner) = substitutions(cmd);
187        let mut v: Vec<String> = segments(&outer).iter().filter_map(|s| program(s)).collect();
188        for b in inner {
189            v.extend(progs(&b));
190        }
191        v
192    }
193
194    #[test]
195    fn reads_the_programs_a_command_runs() {
196        assert_eq!(progs("cargo test -q && grep -c x f | wc -l"), ["cargo", "grep", "wc"]);
197        assert_eq!(progs("FOO=1 BAR=2 node scripts/check.mjs"), ["node"]);
198        assert_eq!(progs("env -i PATH=/bin jq . a.json"), ["jq"]);
199        assert_eq!(progs("diff <(jq -S . a) <(yq -o json b)"), ["diff", "jq", "yq"]);
200        assert_eq!(progs("test \"$(rg -c x f)\" = 0"), ["rg"]);
201        assert_eq!(progs("! grep -q 'a | b; c' file"), ["grep"]);
202        assert_eq!(progs("echo \"x && y\""), Vec::<String>::new());
203        assert_eq!(progs("$TOOL run"), Vec::<String>::new());
204        assert_eq!(progs("if [ -f a ]; then make; fi"), ["make"]);
205        assert_eq!(progs("timeout 30 cargo test"), ["cargo"]);
206    }
207
208    #[test]
209    fn redirections_are_not_commands() {
210        assert_eq!(progs("[ -d a ] && ls a/*.toml >/dev/null 2>&1"), ["ls"]);
211        assert_eq!(progs("make &> build.log && cat build.log"), ["make", "cat"]);
212        assert_eq!(progs("echo x >&2; jq . a <&0"), ["jq"]);
213        assert_eq!(progs("sleep 1 & wait"), ["sleep"]);
214    }
215
216    #[cfg(unix)]
217    #[test]
218    fn reports_only_missing_programs() {
219        let root = std::env::temp_dir();
220        assert!(missing_programs(&root, "sh -c true && cat /dev/null").is_empty());
221        assert_eq!(missing_programs(&root, "diff <(no-such-tool-xyz a) b"), ["no-such-tool-xyz"]);
222        assert_eq!(missing_programs(&root, "./no/such/script.sh"), ["./no/such/script.sh"]);
223    }
224}