|
|
@@ -9,9 +9,9 @@ use nix::unistd::ForkResult::{Child, Parent};
|
|
9
|
9
|
use nix::sys::wait::{wait, waitpid, WaitStatus};
|
|
10
|
10
|
use nix::unistd::{dup2, close, execvp, fork, pipe, read, write};
|
|
11
|
11
|
|
|
12
|
|
-mod shellerror;
|
|
|
12
|
+mod errortype;
|
|
13
|
13
|
|
|
14
|
|
-use shellerror::ShellError;
|
|
|
14
|
+use errortype::ErrorType;
|
|
15
|
15
|
|
|
16
|
16
|
#[derive(PartialEq, Debug)]
|
|
17
|
17
|
pub enum Command {
|
|
|
@@ -22,7 +22,7 @@ pub enum Command {
|
|
22
|
22
|
}
|
|
23
|
23
|
|
|
24
|
24
|
impl FromStr for Command {
|
|
25
|
|
- type Err = ShellError;
|
|
|
25
|
+ type Err = ErrorType;
|
|
26
|
26
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
27
|
27
|
|
|
28
|
28
|
let mut parts = s.split_whitespace();
|
|
|
@@ -90,14 +90,14 @@ impl<R: BufRead, W: Write> Shell<R, W> {
|
|
90
|
90
|
}
|
|
91
|
91
|
/// Initializes the Shell Loop.
|
|
92
|
92
|
/// Starts the shell.
|
|
93
|
|
- pub fn start(&mut self) -> Result<(), &str> {
|
|
|
93
|
+ pub fn start(&mut self) -> Result<(), ErrorType> {
|
|
94
|
94
|
self.shell_loop()
|
|
95
|
95
|
}
|
|
96
|
96
|
|
|
97
|
97
|
/// Loops while exit hasn't been called.
|
|
98
|
98
|
/// When *prompt()* returns (the user hit enter) a Command is created through the FromStr-Trait.
|
|
99
|
99
|
/// If the Command-creation succeeds, run the command.
|
|
100
|
|
- fn shell_loop(&mut self) -> Result<(), &str> {
|
|
|
100
|
+ fn shell_loop(&mut self) -> Result<(), ErrorType> {
|
|
101
|
101
|
while !self.should_exit {
|
|
102
|
102
|
if let Ok(Some(line)) = self.prompt() {
|
|
103
|
103
|
let _ = Command::from_str(&line).and_then(|cmd| self.run(cmd));
|
|
|
@@ -108,7 +108,7 @@ impl<R: BufRead, W: Write> Shell<R, W> {
|
|
108
|
108
|
|
|
109
|
109
|
/// Prints the shell prompt.
|
|
110
|
110
|
/// Waits for user input and returns the read line.
|
|
111
|
|
- pub fn prompt(&mut self) -> Result<Option<String>, &str> {
|
|
|
111
|
+ pub fn prompt(&mut self) -> Result<Option<String>, ErrorType> {
|
|
112
|
112
|
match env::current_dir() {
|
|
113
|
113
|
Ok(pwd) => {
|
|
114
|
114
|
let _ = self.writer.write(
|
|
|
@@ -124,13 +124,13 @@ impl<R: BufRead, W: Write> Shell<R, W> {
|
|
124
|
124
|
Err(_) => Ok(None),
|
|
125
|
125
|
}
|
|
126
|
126
|
}
|
|
127
|
|
- Err(_) => return Err("Path Error"),
|
|
|
127
|
+ Err(_) => return Err(ErrorType::PathError),
|
|
128
|
128
|
}
|
|
129
|
129
|
}
|
|
130
|
130
|
|
|
131
|
131
|
/// Runs a command.
|
|
132
|
132
|
/// Currently only `cd` and `exit` are working.
|
|
133
|
|
- fn run(&mut self, command: Command) -> Result<(), ShellError> {
|
|
|
133
|
+ fn run(&mut self, command: Command) -> Result<(), ErrorType> {
|
|
134
|
134
|
|
|
135
|
135
|
match command {
|
|
136
|
136
|
Command::Empty => {},
|
|
|
@@ -156,7 +156,10 @@ impl<R: BufRead, W: Write> Shell<R, W> {
|
|
156
|
156
|
Ok(Parent { child }) => {
|
|
157
|
157
|
wait();
|
|
158
|
158
|
if let Some(second) = commands_vec.get(1) {
|
|
159
|
|
- close(writer).unwrap();
|
|
|
159
|
+ match close(writer) {
|
|
|
160
|
+ Ok(_) => {},
|
|
|
161
|
+ Err(_) => return Err(ErrorType::BrokenPipeError),
|
|
|
162
|
+ }
|
|
160
|
163
|
dup2(reader, 0).unwrap();
|
|
161
|
164
|
self.execute(second);
|
|
162
|
165
|
}
|
|
|
@@ -170,11 +173,11 @@ impl<R: BufRead, W: Write> Shell<R, W> {
|
|
170
|
173
|
self.execute(first);
|
|
171
|
174
|
}
|
|
172
|
175
|
}
|
|
173
|
|
- Err(_) => println!("Fatal error: Fork failed"),
|
|
|
176
|
+ Err(_) => return Err(ErrorType::ForkError),
|
|
174
|
177
|
}
|
|
175
|
178
|
}
|
|
176
|
179
|
}
|
|
177
|
|
- Err(_) => println!("Fatal error: Fork failed"),
|
|
|
180
|
+ Err(_) => return Err(ErrorType::ForkError),
|
|
178
|
181
|
}
|
|
179
|
182
|
|
|
180
|
183
|
},
|