|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+use std::io::{BufRead, Write};
|
|
|
2
|
+use std::str::FromStr;
|
|
|
3
|
+use std::env;
|
|
|
4
|
+use std::ffi::OsString;
|
|
|
5
|
+
|
|
|
6
|
+mod shellerror;
|
|
|
7
|
+
|
|
|
8
|
+#[derive(PartialEq, Debug)]
|
|
|
9
|
+pub enum Command {
|
|
|
10
|
+ Empty,
|
|
|
11
|
+ Exit,
|
|
|
12
|
+ Cd(Option<OsString>),
|
|
|
13
|
+}
|
|
|
14
|
+
|
|
|
15
|
+#[derive(PartialEq, Debug)]
|
|
|
16
|
+pub struct CommandNotFoundError;
|
|
|
17
|
+
|
|
|
18
|
+impl FromStr for Command {
|
|
|
19
|
+ type Err = CommandNotFoundError;
|
|
|
20
|
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
21
|
+
|
|
|
22
|
+ let mut parts = s.split_whitespace();
|
|
|
23
|
+
|
|
|
24
|
+ match parts.next() {
|
|
|
25
|
+ Some("exit") => Ok(Command::Exit),
|
|
|
26
|
+ Some("cd") => Ok(Command::parse_cd(parts.next())),
|
|
|
27
|
+ Some(cmd) => {
|
|
|
28
|
+ //For use in next homework, to execute programs.
|
|
|
29
|
+ let _ = cmd;
|
|
|
30
|
+ Err(CommandNotFoundError)
|
|
|
31
|
+ }
|
|
|
32
|
+ None => Ok(Command::Empty),
|
|
|
33
|
+ }
|
|
|
34
|
+ }
|
|
|
35
|
+}
|
|
|
36
|
+
|
|
|
37
|
+
|
|
|
38
|
+impl Command {
|
|
|
39
|
+ /// If the passed String exists, set the path of the Cd in the enum (as OsString)
|
|
|
40
|
+ /// Returns **Command**
|
|
|
41
|
+ pub fn parse_cd(cmd: Option<&str>) -> Self {
|
|
|
42
|
+ match cmd {
|
|
|
43
|
+ None => Command::Cd(None),
|
|
|
44
|
+ Some(dest) => Command::Cd(Some(OsString::from(dest))),
|
|
|
45
|
+ }
|
|
|
46
|
+ }
|
|
|
47
|
+
|
|
|
48
|
+ /// If this instance is of Kind **Command::Cd** either go to HOME or navigate to the given path
|
|
|
49
|
+ pub fn exec_cd(&self) {
|
|
|
50
|
+ if let Command::Cd(None) = *self {
|
|
|
51
|
+ let possible_home = env::home_dir();
|
|
|
52
|
+
|
|
|
53
|
+ if let Some(home) = possible_home {
|
|
|
54
|
+ let home_path = home.as_path();
|
|
|
55
|
+ let _ = env::set_current_dir(home_path);
|
|
|
56
|
+ }
|
|
|
57
|
+
|
|
|
58
|
+ }
|
|
|
59
|
+
|
|
|
60
|
+ if let Command::Cd(Some(ref path)) = *self {
|
|
|
61
|
+ match path.to_str() {
|
|
|
62
|
+ Some(_) => {
|
|
|
63
|
+ let _ = env::set_current_dir(path);
|
|
|
64
|
+ }
|
|
|
65
|
+ None => {}
|
|
|
66
|
+ }
|
|
|
67
|
+
|
|
|
68
|
+ }
|
|
|
69
|
+ }
|
|
|
70
|
+}
|
|
|
71
|
+pub struct Shell<R: BufRead, W: Write> {
|
|
|
72
|
+ pub reader: R,
|
|
|
73
|
+ pub writer: W,
|
|
|
74
|
+ pub should_exit: bool,
|
|
|
75
|
+ pub name: String,
|
|
|
76
|
+}
|
|
|
77
|
+
|
|
|
78
|
+impl<R: BufRead, W: Write> Shell<R, W> {
|
|
|
79
|
+ /// Creates a new Shell with a name, input and output.
|
|
|
80
|
+ pub fn new(input: R, output: W, name: String) -> Self {
|
|
|
81
|
+ Shell {
|
|
|
82
|
+ reader: input,
|
|
|
83
|
+ writer: output,
|
|
|
84
|
+ should_exit: false,
|
|
|
85
|
+ name: name,
|
|
|
86
|
+ }
|
|
|
87
|
+ }
|
|
|
88
|
+ /// Initializes the Shell Loop.
|
|
|
89
|
+ /// Starts the shell.
|
|
|
90
|
+ pub fn start(&mut self) -> Result<(), &str> {
|
|
|
91
|
+ self.shell_loop()
|
|
|
92
|
+ }
|
|
|
93
|
+
|
|
|
94
|
+ /// Loops while exit hasn't been called.
|
|
|
95
|
+ /// When *prompt()* returns (the user hit enter) a Command is created through the FromStr-Trait.
|
|
|
96
|
+ /// If the Command-creation succeeds, run the command.
|
|
|
97
|
+ fn shell_loop(&mut self) -> Result<(), &str> {
|
|
|
98
|
+ while !self.should_exit {
|
|
|
99
|
+ if let Ok(Some(line)) = self.prompt() {
|
|
|
100
|
+ let _ = Command::from_str(&line).and_then(|cmd| self.run(cmd));
|
|
|
101
|
+ }
|
|
|
102
|
+ }
|
|
|
103
|
+ Ok(())
|
|
|
104
|
+ }
|
|
|
105
|
+
|
|
|
106
|
+ /// Prints the shell prompt.
|
|
|
107
|
+ /// Waits for user input and returns the read line.
|
|
|
108
|
+ pub fn prompt(&mut self) -> Result<Option<String>, &str> {
|
|
|
109
|
+ match env::current_dir() {
|
|
|
110
|
+ Ok(pwd) => {
|
|
|
111
|
+ let _ = self.writer.write(
|
|
|
112
|
+ format!("{} {} > ", self.name, pwd.display())
|
|
|
113
|
+ .as_bytes(),
|
|
|
114
|
+ );
|
|
|
115
|
+ let _ = self.writer.flush();
|
|
|
116
|
+
|
|
|
117
|
+ let mut line: String = String::new();
|
|
|
118
|
+ let read_result = self.reader.read_line(&mut line);
|
|
|
119
|
+ match read_result {
|
|
|
120
|
+ Ok(_) => Ok(Some(line)),
|
|
|
121
|
+ Err(_) => Ok(None),
|
|
|
122
|
+ }
|
|
|
123
|
+ }
|
|
|
124
|
+ Err(_) => return Err("Path Error"),
|
|
|
125
|
+ }
|
|
|
126
|
+ }
|
|
|
127
|
+
|
|
|
128
|
+ /// Runs a command.
|
|
|
129
|
+ /// Currently only `cd` and `exit` are working.
|
|
|
130
|
+ fn run(&mut self, command: Command) -> Result<(), CommandNotFoundError> {
|
|
|
131
|
+
|
|
|
132
|
+ match command {
|
|
|
133
|
+ Command::Empty => {}
|
|
|
134
|
+ Command::Exit => self.should_exit = true,
|
|
|
135
|
+ Command::Cd(_) => {
|
|
|
136
|
+ command.exec_cd();
|
|
|
137
|
+ }
|
|
|
138
|
+ }
|
|
|
139
|
+ Ok(())
|
|
|
140
|
+ }
|
|
|
141
|
+}
|