1mod ansi_c;
2pub(crate) mod arith;
3mod brace;
4#[cfg(test)]
5mod brace_bash_tests;
6mod budget;
7mod cd;
8pub(crate) mod check;
9mod classify_budget;
10#[cfg(test)]
11mod classify_budget_tests;
12mod display;
13mod echoed;
14pub(crate) mod escape;
15pub(crate) mod eval;
16mod explain;
17mod functions;
18#[cfg(test)]
19mod glob_tests;
20pub(crate) mod heredocs;
21mod loops;
22#[cfg(test)]
23mod loops_tests;
24pub(crate) mod netargs;
25#[cfg(test)]
26mod normalize_tests;
27pub(crate) mod opaque;
28#[cfg(test)]
29mod opaque_tests;
30mod parse;
31mod printed;
32#[cfg(test)]
33mod proptests;
34mod reserved;
35mod scope_budget;
36mod sub_close;
37
38#[derive(Debug, Clone, PartialEq, Eq)]
39pub struct Script(pub Vec<Stmt>);
40
41#[derive(Debug, Clone, PartialEq, Eq)]
42pub struct Stmt {
43 pub pipeline: Pipeline,
44 pub op: Option<ListOp>,
45}
46
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub enum ListOp {
49 And,
50 Or,
51 Semi,
52 Amp,
53}
54
55#[derive(Debug, Clone, PartialEq, Eq)]
56pub struct Pipeline {
57 pub bang: bool,
58 pub commands: Vec<Cmd>,
59}
60
61#[derive(Debug, Clone, PartialEq, Eq)]
62pub enum Cmd {
63 Simple(SimpleCmd),
64 Subshell {
65 body: Script,
66 redirs: Vec<Redir>,
67 },
68 BraceGroup {
69 body: Script,
70 redirs: Vec<Redir>,
71 },
72 For {
73 var: String,
74 items: Vec<Word>,
75 body: Script,
76 redirs: Vec<Redir>,
77 },
78 While {
79 cond: Script,
80 body: Script,
81 redirs: Vec<Redir>,
82 },
83 Until {
84 cond: Script,
85 body: Script,
86 redirs: Vec<Redir>,
87 },
88 If {
89 branches: Vec<Branch>,
90 else_body: Option<Script>,
91 redirs: Vec<Redir>,
92 },
93 DoubleBracket {
94 words: Vec<Word>,
95 redirs: Vec<Redir>,
96 },
97 Case {
101 subject: Word,
102 arms: Vec<CaseArm>,
103 redirs: Vec<Redir>,
104 },
105 FunctionDef {
109 name: String,
110 body: Script,
111 },
112}
113
114#[derive(Debug, Clone, PartialEq, Eq)]
115pub struct Branch {
116 pub cond: Script,
117 pub body: Script,
118}
119
120#[derive(Debug, Clone, PartialEq, Eq)]
123pub struct CaseArm {
124 pub patterns: Vec<Word>,
125 pub body: Script,
126}
127
128#[derive(Debug, Clone, PartialEq, Eq)]
129pub struct SimpleCmd {
130 pub env: Vec<(String, Word)>,
131 pub words: Vec<Word>,
132 pub redirs: Vec<Redir>,
133}
134
135#[derive(Debug, Clone, PartialEq, Eq)]
136pub struct Word(pub Vec<WordPart>);
137
138#[derive(Debug, Clone, PartialEq, Eq)]
139pub enum WordPart {
140 Lit(String),
141 Escape(char),
142 SQuote(String),
143 AnsiC(String),
145 DQuote(Word),
146 CmdSub(Script),
147 ProcSub(Script),
148 Backtick(String),
149 Arith(Word),
154}
155
156#[derive(Debug, Clone, Copy, PartialEq, Eq)]
161pub enum WriteMode {
162 Truncate,
164 Append,
166 TruncateBoth,
170 AppendBoth,
172 Clobber,
174}
175
176#[derive(Debug, Clone, PartialEq, Eq)]
177pub enum Redir {
178 Write {
179 fd: u32,
180 target: Word,
181 mode: WriteMode,
182 },
183 Read {
184 fd: u32,
185 target: Word,
186 },
187 ReadWrite {
191 fd: u32,
192 target: Word,
193 },
194 HereStr(Word),
195 HereDoc {
196 delimiter: String,
197 strip_tabs: bool,
198 body: Word,
203 },
204 DupFd {
205 src: u32,
206 dst: String,
207 },
208}
209
210pub use check::{command_verdict, is_safe_command, is_safe_pipeline};
211pub(crate) use explain::denied_inner_words;
212pub use explain::{Explanation, SegmentReport, explain, explain_with_coverage};
213pub use parse::parse;
214
215impl Word {
216 pub fn eval(&self) -> String {
217 eval::eval_word(self)
218 }
219
220 pub fn expand(&self) -> Vec<String> {
224 eval::expand_word(self)
225 }
226
227 pub fn literal(s: &str) -> Self {
228 Word(vec![WordPart::Lit(s.to_string())])
229 }
230
231 pub fn normalize(&self) -> Self {
232 let mut parts = Vec::new();
233 for part in &self.0 {
234 let part = match part {
235 WordPart::DQuote(inner) => WordPart::DQuote(inner.normalize()),
236 WordPart::CmdSub(s) => WordPart::CmdSub(s.normalize()),
237 WordPart::ProcSub(s) => WordPart::ProcSub(s.normalize()),
238 other => other.clone(),
239 };
240 if let WordPart::Lit(s) = &part
241 && let Some(WordPart::Lit(prev)) = parts.last_mut()
242 {
243 prev.push_str(s);
244 continue;
245 }
246 parts.push(part);
247 }
248 Word(parts)
249 }
250}
251
252impl Script {
253 pub fn is_empty(&self) -> bool {
254 self.0.is_empty()
255 }
256
257 pub fn normalize(&self) -> Self {
258 Script(self.0.iter().map(|stmt| Stmt { pipeline: stmt.pipeline.normalize(), op: stmt.op }).collect())
259 }
260
261 pub fn normalize_as_body(&self) -> Self {
262 let mut s = self.normalize();
263 if let Some(last) = s.0.last_mut()
264 && last.op.is_none()
265 {
266 last.op = Some(ListOp::Semi);
267 }
268 s
269 }
270}
271
272impl Pipeline {
273 fn normalize(&self) -> Self {
274 Pipeline { bang: self.bang, commands: self.commands.iter().map(|c| c.normalize()).collect() }
275 }
276}
277
278impl Cmd {
279 fn normalize(&self) -> Self {
280 match self {
281 Cmd::Simple(s) => Cmd::Simple(s.normalize()),
282 Cmd::Subshell { body, redirs } => Cmd::Subshell { body: body.normalize(), redirs: normalize_redirs(redirs) },
283 Cmd::BraceGroup { body, redirs } => Cmd::BraceGroup { body: body.normalize_as_body(), redirs: normalize_redirs(redirs) },
284 Cmd::For { var, items, body, redirs } => Cmd::For {
285 var: var.clone(),
286 items: items.iter().map(|w| w.normalize()).collect(),
287 body: body.normalize_as_body(),
288 redirs: normalize_redirs(redirs),
289 },
290 Cmd::While { cond, body, redirs } => {
291 Cmd::While { cond: cond.normalize_as_body(), body: body.normalize_as_body(), redirs: normalize_redirs(redirs) }
292 }
293 Cmd::Until { cond, body, redirs } => {
294 Cmd::Until { cond: cond.normalize_as_body(), body: body.normalize_as_body(), redirs: normalize_redirs(redirs) }
295 }
296 Cmd::If { branches, else_body, redirs } => Cmd::If {
297 branches: branches
298 .iter()
299 .map(|b| Branch { cond: b.cond.normalize_as_body(), body: b.body.normalize_as_body() })
300 .collect(),
301 else_body: else_body.as_ref().map(|e| e.normalize_as_body()),
302 redirs: normalize_redirs(redirs),
303 },
304 Cmd::DoubleBracket { words, redirs } => {
305 Cmd::DoubleBracket { words: words.iter().map(|w| w.normalize()).collect(), redirs: normalize_redirs(redirs) }
306 }
307 Cmd::Case { subject, arms, redirs } => Cmd::Case {
308 subject: subject.normalize(),
309 arms: arms
310 .iter()
311 .map(|a| CaseArm { patterns: a.patterns.iter().map(|w| w.normalize()).collect(), body: a.body.normalize_as_body() })
312 .collect(),
313 redirs: normalize_redirs(redirs),
314 },
315 Cmd::FunctionDef { name, body } => Cmd::FunctionDef { name: name.clone(), body: body.normalize_as_body() },
316 }
317 }
318}
319
320impl SimpleCmd {
321 fn normalize(&self) -> Self {
322 SimpleCmd {
323 env: self.env.iter().map(|(k, v)| (k.clone(), v.normalize())).collect(),
324 words: self.words.iter().map(|w| w.normalize()).collect(),
325 redirs: normalize_redirs(&self.redirs),
326 }
327 }
328}
329
330fn normalize_redirs(redirs: &[Redir]) -> Vec<Redir> {
331 redirs
332 .iter()
333 .map(|r| match r {
334 Redir::Write { fd, target, mode } => Redir::Write { fd: *fd, target: target.normalize(), mode: *mode },
335 Redir::Read { fd, target } => Redir::Read { fd: *fd, target: target.normalize() },
336 Redir::ReadWrite { fd, target } => Redir::ReadWrite { fd: *fd, target: target.normalize() },
337 Redir::HereStr(w) => Redir::HereStr(w.normalize()),
338 Redir::HereDoc { .. } | Redir::DupFd { .. } => r.clone(),
339 })
340 .collect()
341}