|
|
@@ -5,6 +5,7 @@ use command::*;
|
|
5
|
5
|
use std::str::FromStr;
|
|
6
|
6
|
use std::env;
|
|
7
|
7
|
|
|
|
8
|
+///Tuple-Structs which hold Std-in and -out locks.
|
|
8
|
9
|
pub struct R<'a>(pub StdinLock<'a>);
|
|
9
|
10
|
pub struct W<'a>(pub StdoutLock<'a>);
|
|
10
|
11
|
|
|
|
@@ -15,9 +16,9 @@ pub struct Shell<R, W> {
|
|
15
|
16
|
pub name: String,
|
|
16
|
17
|
}
|
|
17
|
18
|
|
|
18
|
|
-impl <R:BufRead, W:Write> Shell<R, W> {
|
|
19
|
|
- /// Creates a new Shell.
|
|
20
|
|
- 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 {
|
|
21
|
22
|
Shell {
|
|
22
|
23
|
reader : input,
|
|
23
|
24
|
writer : output,
|
|
|
@@ -25,12 +26,15 @@ impl <R:BufRead, W:Write> Shell<R, W> {
|
|
25
|
26
|
name : name,
|
|
26
|
27
|
}
|
|
27
|
28
|
}
|
|
28
|
|
- /// Initializes the Shell Loop
|
|
|
29
|
+ /// Initializes the Shell Loop.
|
|
|
30
|
+ /// Starts the shell.
|
|
29
|
31
|
pub fn start(&mut self) -> Result<(), &str> {
|
|
30
|
32
|
self.shell_loop()
|
|
31
|
33
|
}
|
|
32
|
34
|
|
|
33
|
|
- /// Waits for user inputs.
|
|
|
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.
|
|
34
|
38
|
fn shell_loop(&mut self) -> Result<(), &str> {
|
|
35
|
39
|
while !self.should_exit {
|
|
36
|
40
|
if let Ok(line) = self.prompt() {
|
|
|
@@ -41,9 +45,8 @@ impl <R:BufRead, W:Write> Shell<R, W> {
|
|
41
|
45
|
}
|
|
42
|
46
|
|
|
43
|
47
|
/// Prints the shell prompt.
|
|
|
48
|
+ /// Waits for user input and returns the read line.
|
|
44
|
49
|
fn prompt (&mut self) -> Result<String, &str> {
|
|
45
|
|
-
|
|
46
|
|
-
|
|
47
|
50
|
match env::current_dir() {
|
|
48
|
51
|
Ok(pwd) => {
|
|
49
|
52
|
let _ = self.writer.write(format!("{} {} > ", self.name, pwd.display()).as_bytes());
|
|
|
@@ -63,7 +66,9 @@ impl <R:BufRead, W:Write> Shell<R, W> {
|
|
63
|
66
|
Err(_) => return Err("Path Error"),
|
|
64
|
67
|
}
|
|
65
|
68
|
}
|
|
|
69
|
+
|
|
66
|
70
|
/// Runs a command.
|
|
|
71
|
+ /// Currently only `cd` and `exit` are working.
|
|
67
|
72
|
fn run(&mut self, command: Command) -> Result<(), CommandNotFoundError>{
|
|
68
|
73
|
|
|
69
|
74
|
match command {
|
|
|
@@ -78,14 +83,17 @@ impl <R:BufRead, W:Write> Shell<R, W> {
|
|
78
|
83
|
|
|
79
|
84
|
}
|
|
80
|
85
|
|
|
|
86
|
+/// Implement Read-Trait for Tuple-Struct R
|
|
|
87
|
+/// Simply calls `read` of StdinLock
|
|
81
|
88
|
impl <'a>Read for R<'a> {
|
|
82
|
89
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
|
|
83
|
90
|
self.0.read(buf)
|
|
84
|
91
|
}
|
|
85
|
92
|
}
|
|
86
|
93
|
|
|
|
94
|
+/// Implement BufRead-Trait for Tuple-Struct R
|
|
|
95
|
+/// Simply calls `fill_buff` and `consume` from StdinLock
|
|
87
|
96
|
impl <'a>BufRead for R<'a> {
|
|
88
|
|
-
|
|
89
|
97
|
fn fill_buf(&mut self) -> Result<&[u8], io::Error> {
|
|
90
|
98
|
self.0.fill_buf()
|
|
91
|
99
|
}
|
|
|
@@ -95,11 +103,13 @@ impl <'a>BufRead for R<'a> {
|
|
95
|
103
|
}
|
|
96
|
104
|
}
|
|
97
|
105
|
|
|
|
106
|
+/// Implement Write-Trait for Tuple-Struct W
|
|
|
107
|
+/// Simply calls `write` and `flush` from StdoutLock
|
|
98
|
108
|
impl <'a>Write for W<'a> {
|
|
99
|
109
|
fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
|
|
100
|
110
|
self.0.write(buf)
|
|
101
|
111
|
}
|
|
102
|
|
-
|
|
|
112
|
+
|
|
103
|
113
|
fn flush(&mut self) -> Result<(), io::Error> {
|
|
104
|
114
|
self.0.flush()
|
|
105
|
115
|
}
|