Browse Source

Unknown Command and cargo doc now working

Lorenz Bung 7 years ago
parent
commit
db5a7dd27e
2 changed files with 15 additions and 5 deletions
  1. 4
    0
      hw7/task2/Cargo.toml
  2. 11
    5
      hw7/task2/src/lib.rs

+ 4
- 0
hw7/task2/Cargo.toml View File

5
 
5
 
6
 [dependencies]
6
 [dependencies]
7
 nix = "0.9.0"
7
 nix = "0.9.0"
8
+
9
+[[bin]]
10
+doc = false
11
+name = "task2"

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

108
     pub fn prompt(&mut self) -> Result<Option<String>, ErrorType> {
108
     pub fn prompt(&mut self) -> Result<Option<String>, ErrorType> {
109
         match env::current_dir() {
109
         match env::current_dir() {
110
             Ok(pwd) => {
110
             Ok(pwd) => {
111
-                let _ = self.writer.write(
112
-                    format!("{} {} > ", self.name, pwd.display())
111
+                let _ = self.writer.write_all(
112
+                    format!("{} {}> ", self.name, pwd.display())
113
                         .as_bytes(),
113
                         .as_bytes(),
114
                 );
114
                 );
115
                 let _ = self.writer.flush();
115
                 let _ = self.writer.flush();
126
     }
126
     }
127
 
127
 
128
     /// Runs a command.
128
     /// Runs a command.
129
-    /// Currently only `cd` and `exit` are working.
130
     fn run(&mut self, command: Command) -> Result<(), ErrorType> {
129
     fn run(&mut self, command: Command) -> Result<(), ErrorType> {
131
 
130
 
132
         match command {
131
         match command {
185
         Ok(())
184
         Ok(())
186
     }
185
     }
187
 
186
 
188
-    fn execute(&self, cmd: &str) {
187
+    fn execute(&mut self, cmd: &str) {
189
         let parts: Vec<&str> = cmd.split_whitespace().collect();
188
         let parts: Vec<&str> = cmd.split_whitespace().collect();
190
         let args: Vec<CString> = parts.iter().map(|f| CString::new(*f).unwrap()).collect();
189
         let args: Vec<CString> = parts.iter().map(|f| CString::new(*f).unwrap()).collect();
190
+        let cmd = args.clone();
191
         let t = args.into_boxed_slice();
191
         let t = args.into_boxed_slice();
192
 
192
 
193
-        execvp(&t[0], &t).unwrap();
193
+        match execvp(&t[0], &t) {
194
+            Ok(_) => {},
195
+            Err(_) => {
196
+                let _ = self.writer.write_all(
197
+                    format!("{:?}: command not found\n", cmd[0]).as_bytes());
198
+            },
199
+        }
194
     }
200
     }
195
 }
201
 }

Loading…
Cancel
Save