Browse Source

Added a non working shell error type

Lorenz Bung 7 years ago
parent
commit
7f35f058cc
3 changed files with 20 additions and 13 deletions
  1. 6
    0
      hw7/task2/src/errortype.rs
  2. 14
    11
      hw7/task2/src/lib.rs
  3. 0
    2
      hw7/task2/src/shellerror.rs

+ 6
- 0
hw7/task2/src/errortype.rs View File

1
+#[derive(Debug, PartialEq)]
2
+pub enum ErrorType {
3
+    PathError,
4
+    BrokenPipeError,
5
+    ForkError,
6
+}

+ 14
- 11
hw7/task2/src/lib.rs View File

9
 use nix::sys::wait::{wait, waitpid, WaitStatus};
9
 use nix::sys::wait::{wait, waitpid, WaitStatus};
10
 use nix::unistd::{dup2, close, execvp, fork, pipe, read, write};
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
 #[derive(PartialEq, Debug)]
16
 #[derive(PartialEq, Debug)]
17
 pub enum Command {
17
 pub enum Command {
22
 }
22
 }
23
 
23
 
24
 impl FromStr for Command {
24
 impl FromStr for Command {
25
-    type Err = ShellError;
25
+    type Err = ErrorType;
26
     fn from_str(s: &str) -> Result<Self, Self::Err> {
26
     fn from_str(s: &str) -> Result<Self, Self::Err> {
27
 
27
 
28
         let mut parts = s.split_whitespace();
28
         let mut parts = s.split_whitespace();
90
     }
90
     }
91
     /// Initializes the Shell Loop.
91
     /// Initializes the Shell Loop.
92
     /// Starts the shell.
92
     /// Starts the shell.
93
-    pub fn start(&mut self) -> Result<(), &str> {
93
+    pub fn start(&mut self) -> Result<(), ErrorType> {
94
         self.shell_loop()
94
         self.shell_loop()
95
     }
95
     }
96
 
96
 
97
     /// Loops while exit hasn't been called.
97
     /// Loops while exit hasn't been called.
98
     /// When *prompt()* returns (the user hit enter) a Command is created through the FromStr-Trait.
98
     /// When *prompt()* returns (the user hit enter) a Command is created through the FromStr-Trait.
99
     /// If the Command-creation succeeds, run the command.
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
         while !self.should_exit {
101
         while !self.should_exit {
102
             if let Ok(Some(line)) = self.prompt() {
102
             if let Ok(Some(line)) = self.prompt() {
103
                 let _ = Command::from_str(&line).and_then(|cmd| self.run(cmd));
103
                 let _ = Command::from_str(&line).and_then(|cmd| self.run(cmd));
108
 
108
 
109
     /// Prints the shell prompt.
109
     /// Prints the shell prompt.
110
     /// Waits for user input and returns the read line.
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
         match env::current_dir() {
112
         match env::current_dir() {
113
             Ok(pwd) => {
113
             Ok(pwd) => {
114
                 let _ = self.writer.write(
114
                 let _ = self.writer.write(
124
                     Err(_) => Ok(None),
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
     /// Runs a command.
131
     /// Runs a command.
132
     /// Currently only `cd` and `exit` are working.
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
         match command {
135
         match command {
136
             Command::Empty => {},
136
             Command::Empty => {},
156
                                     Ok(Parent { child }) => {
156
                                     Ok(Parent { child }) => {
157
                                         wait();
157
                                         wait();
158
                                         if let Some(second) = commands_vec.get(1) {
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
                                             dup2(reader, 0).unwrap();
163
                                             dup2(reader, 0).unwrap();
161
                                             self.execute(second);
164
                                             self.execute(second);
162
                                         }
165
                                         }
170
                                             self.execute(first);
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
             },

+ 0
- 2
hw7/task2/src/shellerror.rs View File

1
-#[derive(PartialEq, Debug)]
2
-pub struct ShellError;

Loading…
Cancel
Save