Browse Source

bugfix: 修复shell中,用户可以删除多余的字符的bug,以及\b会被录入到参数字符串的bug

fslongjin 2 years ago
parent
commit
156c2c2389
2 changed files with 25 additions and 6 deletions
  1. 11 3
      user/apps/shell/cmd.c
  2. 14 3
      user/apps/shell/shell.c

+ 11 - 3
user/apps/shell/cmd.c

@@ -141,11 +141,19 @@ int shell_cmd_cd(int argc, char **argv)
             return 0;
         }
         else
-            goto fail;; // 出错则直接忽略
+            goto fail;
+        ; // 出错则直接忽略
     }
     else
     {
-        int new_len = current_dir_len + dest_len;
+        int dest_offset = 0;
+        if (dest_len > 2)
+        {
+            if (argv[1][0] == '.' && argv[1][1] == '/') // 相对路径
+                dest_offset = 2;
+        }
+        
+        int new_len = current_dir_len + dest_len - dest_offset;
         // ======进入相对路径=====
         if (new_len >= SHELL_CWD_MAX_SIZE - 1)
         {
@@ -160,7 +168,7 @@ int shell_cmd_cd(int argc, char **argv)
 
         if (current_dir_len > 1)
             new_path[current_dir_len] = '/';
-        strcat(new_path, argv[1]);
+        strcat(new_path, argv[1] + dest_offset);
 
         if (chdir(new_path) == 0) // 成功切换目录
         {

+ 14 - 3
user/apps/shell/shell.c

@@ -65,7 +65,6 @@ static void main_loop(int kb_fd)
             printf("\n");
             if (cmd_num >= 0)
                 shell_run_built_in_command(cmd_num, argc, argv);
-
         }
         else
             printf("\n");
@@ -104,8 +103,20 @@ int shell_readline(int fd, char *buf)
 
         if (key)
         {
-            buf[count++] = key;
-            printf("%c", key);
+            if (key == '\b')
+            {
+                if (count > 0)
+                {
+                    buf[--count] = 0;
+                    printf("%c", '\b');
+                }
+            }
+            else
+            {
+                buf[count++] = key;
+
+                printf("%c", key);
+            }
         }
 
         // 输入缓冲区满了之后,直接返回