|
|
@@ -1,52 +1,114 @@
|
|
|
1
|
+use std::io::{BufRead, Write, Read, StdinLock, StdoutLock};
|
|
1
|
2
|
use std::io;
|
|
2
|
|
-use std::io::prelude::*;
|
|
3
|
|
-use command;
|
|
4
|
3
|
|
|
5
|
|
-struct Shell<R, W> {
|
|
|
4
|
+use command::*;
|
|
|
5
|
+use std::str::FromStr;
|
|
|
6
|
+use std::env;
|
|
|
7
|
+
|
|
|
8
|
+///Tuple-Structs which hold Std-in and -out locks.
|
|
|
9
|
+pub struct R<'a>(pub StdinLock<'a>);
|
|
|
10
|
+pub struct W<'a>(pub StdoutLock<'a>);
|
|
|
11
|
+
|
|
|
12
|
+pub struct Shell<R, W> {
|
|
6
|
13
|
pub reader: R,
|
|
7
|
14
|
pub writer: W,
|
|
8
|
15
|
pub should_exit: bool,
|
|
9
|
16
|
pub name: String,
|
|
10
|
17
|
}
|
|
11
|
18
|
|
|
12
|
|
-impl Shell<R, W> {
|
|
13
|
|
- /// Creates a new Shell.
|
|
14
|
|
- pub fn new(input: R, output: W, name: String) -> Self {
|
|
|
19
|
+impl<'a> Shell<R<'a>, W<'a>> {
|
|
|
20
|
+ /// Creates a new Shell with a name, input and output.
|
|
|
21
|
+ pub fn new(input: R<'a>, output: W<'a>, name: String) -> Self {
|
|
15
|
22
|
Shell {
|
|
16
|
|
- reader = input,
|
|
17
|
|
- writer = output,
|
|
18
|
|
- should_exit = false,
|
|
19
|
|
- name = name,
|
|
|
23
|
+ reader: input,
|
|
|
24
|
+ writer: output,
|
|
|
25
|
+ should_exit: false,
|
|
|
26
|
+ name: name,
|
|
20
|
27
|
}
|
|
21
|
28
|
}
|
|
22
|
|
- /// Initializes the Shell Loop
|
|
23
|
|
- pub fn start() -> Result<Self, &str> {
|
|
24
|
|
- shell_loop();
|
|
|
29
|
+ /// Initializes the Shell Loop.
|
|
|
30
|
+ /// Starts the shell.
|
|
|
31
|
+ pub fn start(&mut self) -> Result<(), &str> {
|
|
|
32
|
+ self.shell_loop()
|
|
25
|
33
|
}
|
|
26
|
|
- /// Waits for user inputs.
|
|
27
|
|
- fn shell_loop() -> Result {
|
|
28
|
|
- loop {
|
|
|
34
|
+
|
|
|
35
|
+ /// Loops while exit hasn't been called.
|
|
|
36
|
+ /// When *prompt()* returns (the user hit enter) a Command is created through the FromStr-Trait.
|
|
|
37
|
+ /// If the Command-creation succeeds, run the command.
|
|
|
38
|
+ fn shell_loop(&mut self) -> Result<(), &str> {
|
|
|
39
|
+ while !self.should_exit {
|
|
|
40
|
+ if let Ok(line) = self.prompt() {
|
|
|
41
|
+ let _ = Command::from_str(&line).and_then(|cmd| self.run(cmd));
|
|
|
42
|
+ }
|
|
29
|
43
|
}
|
|
|
44
|
+ Ok(())
|
|
30
|
45
|
}
|
|
|
46
|
+
|
|
31
|
47
|
/// Prints the shell prompt.
|
|
32
|
|
- fn prompt() -> Result<Option, &str> {
|
|
|
48
|
+ /// Waits for user input and returns the read line.
|
|
|
49
|
+ fn prompt(&mut self) -> Result<String, &str> {
|
|
|
50
|
+ match env::current_dir() {
|
|
|
51
|
+ Ok(pwd) => {
|
|
|
52
|
+ let _ = self.writer.write(
|
|
|
53
|
+ format!("{} {} > ", self.name, pwd.display())
|
|
|
54
|
+ .as_bytes(),
|
|
|
55
|
+ );
|
|
|
56
|
+ let _ = self.writer.flush();
|
|
|
57
|
+
|
|
|
58
|
+ let mut line: String = String::new();
|
|
|
59
|
+ let read_result = self.reader.read_line(&mut line);
|
|
|
60
|
+ match read_result {
|
|
|
61
|
+ Ok(_) => Ok(line),
|
|
|
62
|
+ Err(_) => Err("Error reading."),
|
|
|
63
|
+ }
|
|
|
64
|
+ }
|
|
|
65
|
+ Err(_) => return Err("Path Error"),
|
|
|
66
|
+ }
|
|
33
|
67
|
}
|
|
|
68
|
+
|
|
34
|
69
|
/// Runs a command.
|
|
35
|
|
- fn run(&str) {
|
|
|
70
|
+ /// Currently only `cd` and `exit` are working.
|
|
|
71
|
+ fn run(&mut self, command: Command) -> Result<(), CommandNotFoundError> {
|
|
|
72
|
+
|
|
|
73
|
+ match command {
|
|
|
74
|
+ Command::Empty => {}
|
|
|
75
|
+ Command::Exit => self.should_exit = true,
|
|
|
76
|
+ Command::Cd(_) => {
|
|
|
77
|
+ command.exec_cd();
|
|
|
78
|
+ }
|
|
|
79
|
+ }
|
|
|
80
|
+ Ok(())
|
|
36
|
81
|
}
|
|
37
|
82
|
}
|
|
38
|
83
|
|
|
39
|
|
-impl BufRead for Shell<R, W> {
|
|
40
|
|
- fn fill_buf(&mut self) -> Result<&[u8]> {
|
|
41
|
|
- stdin.fill_buf()
|
|
|
84
|
+/// Implement Read-Trait for Tuple-Struct R
|
|
|
85
|
+/// Simply calls `read` of StdinLock
|
|
|
86
|
+impl<'a> Read for R<'a> {
|
|
|
87
|
+ fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
|
|
|
88
|
+ self.0.read(buf)
|
|
42
|
89
|
}
|
|
|
90
|
+}
|
|
|
91
|
+
|
|
|
92
|
+/// Implement BufRead-Trait for Tuple-Struct R
|
|
|
93
|
+/// Simply calls `fill_buff` and `consume` from StdinLock
|
|
|
94
|
+impl<'a> BufRead for R<'a> {
|
|
|
95
|
+ fn fill_buf(&mut self) -> Result<&[u8], io::Error> {
|
|
|
96
|
+ self.0.fill_buf()
|
|
|
97
|
+ }
|
|
|
98
|
+
|
|
43
|
99
|
fn consume(&mut self, amt: usize) {
|
|
|
100
|
+ self.0.consume(amt)
|
|
44
|
101
|
}
|
|
45
|
102
|
}
|
|
46
|
103
|
|
|
47
|
|
-impl Write for Shell<R, W> {
|
|
48
|
|
- fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
|
|
104
|
+/// Implement Write-Trait for Tuple-Struct W
|
|
|
105
|
+/// Simply calls `write` and `flush` from StdoutLock
|
|
|
106
|
+impl<'a> Write for W<'a> {
|
|
|
107
|
+ fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
|
|
|
108
|
+ self.0.write(buf)
|
|
49
|
109
|
}
|
|
50
|
|
- fn flush(&mut self) -> Result<()> {
|
|
|
110
|
+
|
|
|
111
|
+ fn flush(&mut self) -> Result<(), io::Error> {
|
|
|
112
|
+ self.0.flush()
|
|
51
|
113
|
}
|
|
52
|
114
|
}
|