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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
use std::io::{Error};
use std::path::{PathBuf};
use std::process::ExitStatus;
use std::process::Command as Process;
use super::builtins::{cd, set, unset};
// > overwrite
// >> append
// | pipe
#[derive(Debug)]
enum Redirect {
Std,
FileOverwrite(String),
FileAppend(String),
}
#[derive(Debug, PartialEq, Copy, Clone)]
pub(in crate::commandline) enum RunIf { Always, ExitSuccess, ExitFailure }
impl RunIf {
pub(in crate::commandline) fn can_run(&self, status: &Option<ExitStatus>) -> bool {
if *self == RunIf::Always {
return true;
}
match status {
Some(s) => {
(*self == RunIf::ExitSuccess && s.success())
|| (*self == RunIf::ExitFailure && !s.success())
}
None => true,
}
}
}
pub(in crate::commandline) enum RunResult {
Command(ExitStatus),
Builtin,
}
#[derive(Debug)]
pub struct Command {
args: Vec<String>,
stdout: Redirect,
pub(in crate::commandline) when: RunIf,
}
impl Command {
pub(in crate::commandline) fn new(line: &str, when: RunIf) -> Command {
let (args, stdout) = tokenize(line);
Command { args, stdout, when }
}
pub(in crate::commandline) fn run(
&self,
home: &PathBuf,
status: &Option<ExitStatus>,
) -> Result<RunResult, Error> {
match self.args[0].as_str() {
"!" => {
println!("{:?}", status);
Ok(RunResult::Builtin)
}
"cd" => cd(&self.args[1..], home),
"exit" => {
std::process::exit(0);
}
"set" => set(&self.args[1..]),
"unset" => unset(&self.args[1..]),
_ => {
let mut child = Process::new(&self.args[0]).args(&self.args[1..]).spawn()?;
Ok(RunResult::Command(child.wait().unwrap()))
}
}
}
}
enum TokenType {
Argument,
StdoutFileOverwrite,
StdoutFileAppend,
}
fn tokenize(line: &str) -> (Vec<String>, Redirect) {
let mut args: Vec<String> = Vec::new();
let mut stdout = Redirect::Std;
let mut iter = line.chars().peekable();
let mut token = String::new();
let mut token_type = TokenType::Argument;
let mut quote = false;
while let Some(i) = iter.next() {
match i {
' ' => {
if quote {
token.push(' ');
}
}
'\'' => match iter.peek() {
Some(&'\'') | Some(&'>') | Some(&'&') => {
token.push(iter.next().unwrap());
}
_ => {
quote = !quote;
}
},
'>' => {
if iter.peek() == Some(&'>') {
token_type = TokenType::StdoutFileAppend;
iter.next();
} else {
token_type = TokenType::StdoutFileOverwrite;
}
}
_ => {
token.push(i);
}
}
if !token.is_empty() && ((iter.peek() == Some(&' ') && !quote) || iter.peek() == None) {
match token_type {
TokenType::Argument => args.push(token),
TokenType::StdoutFileOverwrite => stdout = Redirect::FileOverwrite(token),
TokenType::StdoutFileAppend => stdout = Redirect::FileAppend(token),
}
token = String::new();
}
}
(args, stdout)
}
/*
#[test]
fn test_tokenizer() {
{
let ls = tokenize("ls -l");
assert_eq!(ls.args, vec!("ls", "-l"));
let string = tokenize("ls -l 'something else'");
assert_eq!(string.args, vec!("ls", "-l", "something else"));
let escape = tokenize("ls -l 'junction jan''s'");
assert_eq!(escape.args, vec!("ls", "-l", "junction jan\'s"));
}
{
let o = tokenize("&& ls");
assert_eq!(o.args, vec!("ls"));
assert_eq!(o.when, RunOn::ExitSuccess);
let f = tokenize("|| ls");
assert_eq!(f.args, vec!("ls"));
assert_eq!(f.when, RunOn::ExitFailure);
}
println!("{:?}", tokenize("ls -l something'>"));
println!("{:?}", tokenize("ls -l something'>'>"));
println!("{:?}", tokenize("ls -l something >output"));
println!("{:?}", tokenize("ls -l something > output"));
println!("{:?}", tokenize("ls -l something >'junction jan''s'"));
println!("{:?}", tokenize("ls -l something >>output"));
println!("{:?}", tokenize("ls -l something >> output"));
println!("{:?}", tokenize("ls -l something >>'junction jan''s'"));
}
*/
|