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