Преглед на файлове

修复exec出现空指针错误的问题 (#6)

修复exec命令出现空指针错误的问题,修复内容为空时回车不会换行的问题
MemoryShore преди 1 година
родител
ревизия
732e872536
променени са 2 файла, в които са добавени 38 реда и са изтрити 37 реда
  1. 27 24
      src/shell/command/mod.rs
  2. 11 13
      src/shell/mod.rs

+ 27 - 24
src/shell/command/mod.rs

@@ -432,25 +432,29 @@ impl Shell {
     }
 
     pub fn shell_cmd_exec(&self, args: &Vec<String>) -> Result<(), CommandError> {
+        let mut args = args.clone();
         if args.len() <= 0 {
             return Err(CommandError::WrongArgumentCount(args.len()));
         }
         let path = args.get(0).unwrap();
         let mut real_path = String::new();
-        if !path.starts_with('/') && !path.starts_with('.') {
-            let mut prefix_collection = Env::path();
-            prefix_collection.insert(0, self.current_dir());
-            for prefix in prefix_collection {
-                real_path = format!("{}/{}", prefix, path);
+        if !path.contains('/') {
+            let mut dir_collection = Env::path();
+            dir_collection.insert(0, self.current_dir());
+            for dir in dir_collection {
+                real_path = format!("{}/{}", dir, path);
                 if Path::new(&real_path).is_file() {
                     break;
                 }
             }
-        }
-
-        match self.is_file(&real_path) {
-            Ok(str) => real_path = str,
-            Err(e) => return Err(e),
+            if real_path.is_empty() {
+                return Err(CommandError::FileNotFound(path.clone()));
+            }
+        } else {
+            match self.is_file(path) {
+                Ok(path) => real_path = path,
+                Err(e) => return Err(e),
+            }
         }
 
         let pid: libc::pid_t = unsafe {
@@ -459,23 +463,22 @@ impl Shell {
                 .unwrap()
         };
 
+        let name = &real_path[real_path.rfind('/').unwrap_or(0)..];
+        *args.get_mut(0).unwrap() = name.to_string();
         let mut retval = 0;
         if pid == 0 {
             let path_cstr = std::ffi::CString::new(real_path).unwrap();
-            let mut argv: *const *const i8 = std::ptr::null();
-            if args.len() > 1 {
-                let args_cstr = args
-                    .iter()
-                    .skip(1)
-                    .map(|str| std::ffi::CString::new(str.as_str()).unwrap())
-                    .collect::<Vec<std::ffi::CString>>();
-                let mut args_ptr = args_cstr
-                    .iter()
-                    .map(|c_str| c_str.as_ptr())
-                    .collect::<Vec<*const i8>>();
-                args_ptr.push(std::ptr::null());
-                argv = args_ptr.as_ptr();
-            }
+            let args_cstr = args
+                .iter()
+                .map(|str| std::ffi::CString::new(str.as_str()).unwrap())
+                .collect::<Vec<std::ffi::CString>>();
+            let mut args_ptr = args_cstr
+                .iter()
+                .map(|c_str| c_str.as_ptr())
+                .collect::<Vec<*const i8>>();
+            args_ptr.push(std::ptr::null());
+            let argv = args_ptr.as_ptr();
+
             unsafe {
                 libc::execv(path_cstr.as_ptr(), argv);
             }

+ 11 - 13
src/shell/mod.rs

@@ -50,12 +50,12 @@ impl Shell {
                 break;
             }
             let command_bytes = self.history_commands.last().unwrap().clone();
-            let mut temp = command_bytes.clone();
-            temp.retain(|byte| *byte != b' ');
-            if temp.len() == 0 {
+            if command_bytes.starts_with(&[b' ']) {
                 self.history_commands.pop().unwrap();
             } else {
                 self.executed_commands.push(command_bytes.clone());
+            }
+            if !command_bytes.iter().all(|&byte| byte == b' ') {
                 self.exec_command_in_bytes(&command_bytes);
             }
         }
@@ -175,16 +175,14 @@ impl Shell {
                     }
 
                     SpecialKeycode::LF | SpecialKeycode::CR => {
-                        if cursor > 0 {
-                            Printer::set_cursor(buf, cursor, buf.len());
-                            println!();
-                            let mut command = buf.clone();
-                            buf = history_commands.get_mut(len).unwrap();
-                            buf.clear();
-                            buf.append(&mut command);
-
-                            return 1;
-                        }
+                        Printer::set_cursor(buf, cursor, buf.len());
+                        println!();
+                        let mut command = buf.clone();
+                        buf = history_commands.get_mut(len).unwrap();
+                        buf.clear();
+                        buf.append(&mut command);
+
+                        return 1;
                     }
 
                     SpecialKeycode::BackSpace => {