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