-
Notifications
You must be signed in to change notification settings - Fork 17
/
bash.rs
31 lines (30 loc) · 834 Bytes
/
bash.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
pub fn is_literal_bash_string(command: &[u8]) -> bool {
let mut previous = None;
for &c in command {
if b"\t\n !\"$&'()*,;<>?[\\]^`{|}".contains(&c) {
return false;
}
if previous == None && b"#-~".contains(&c) {
// Special case: `-` isn't a part of bash syntax, but it is treated
// as an argument of `exec`.
return false;
}
if (previous == Some(b':') || previous == Some(b'=')) && c == b'~' {
return false;
}
previous = Some(c);
}
true
}
pub fn quote(arg: &[u8]) -> Vec<u8> {
let mut result = vec![b'\''];
for &i in arg {
if i == b'\'' {
result.extend_from_slice(br#"'\''"#)
} else {
result.push(i)
}
}
result.push(b'\'');
result
}