From aa64a3a375674e019573c6471898e67fe588a191 Mon Sep 17 00:00:00 2001 From: wang904 <1234366@qq.com> Date: Thu, 18 Aug 2022 10:29:35 +0800 Subject: [PATCH 1/4] update: history commands --- user/apps/shell/shell.c | 75 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 67 insertions(+), 8 deletions(-) diff --git a/user/apps/shell/shell.c b/user/apps/shell/shell.c index 4f6c99cf..dae09f58 100644 --- a/user/apps/shell/shell.c +++ b/user/apps/shell/shell.c @@ -9,7 +9,7 @@ #include "cmd.h" #define pause_cpu() asm volatile("pause\n\t"); - +#define mem_history 1024 /** * @brief 循环读取每一行 * @@ -21,9 +21,11 @@ int shell_readline(int fd, char *buf); void print_ascii_logo(); extern char *shell_current_path; - - - +//保存的历史命令 +char history_commands[mem_history][256]; +int count_history; +//现在对应的命令 +int pointer; /** * @brief shell主循环 * @@ -31,7 +33,8 @@ extern char *shell_current_path; */ void main_loop(int kb_fd) { - + count_history = 0; + pointer = 1; unsigned char input_buffer[INPUT_BUFFER_SIZE] = {0}; // 初始化当前工作目录的路径 @@ -40,7 +43,6 @@ void main_loop(int kb_fd) memset(shell_current_path, 0, 3); shell_current_path[0] = '/'; shell_current_path[1] = '\0'; - // shell命令行的主循环 while (true) { @@ -56,10 +58,19 @@ void main_loop(int kb_fd) if (count) { + char *command_origin[strlen(input_buffer)]; + strcpy(command_origin, input_buffer); int cmd_num = parse_command(input_buffer, &argc, &argv); + printf("\n"); if (cmd_num >= 0) + { + //加入历史命令 + strcpy(history_commands[count_history], command_origin); + count_history++; + pointer = count_history; shell_run_built_in_command(cmd_num, argc, argv); + } } else printf("\n"); @@ -80,7 +91,37 @@ int main() while (1) ; } - +/** + * @brief 清除缓冲区 + * + * @param count 缓冲区大小 + * @param buf 缓冲区内容 + */ +void clear_command(int count, char *buf) +{ + for (int i = 0; i < count; i++) + { + printf("%c", '\b'); + } + memset(buf, 0, sizeof(buf)); +} +/** + * @brief 切换命令(写入到缓冲区) + * + * @param buf 缓冲区 + * @param type 如果为1,就向下,如果为-1,就向上 + */ +void change_command(char *buf, int type) +{ + pointer -= type; + //处理边界 + if (pointer >= count_history) + pointer--; + if (pointer < 0) + pointer++; + strcpy(buf, history_commands[pointer]); + printf("%s", buf); +} /** * @brief 循环读取每一行 * @@ -96,10 +137,28 @@ int shell_readline(int fd, char *buf) while (1) { key = keyboard_analyze_keycode(fd); + //向上方向键 + if (count_history != 0 && key == 0xc8) + { + clear_command(count, buf); + count = 0; + //向历史 + change_command(buf, 1); + count = strlen(buf); + } + //向下方向键 + if (count_history != 0 && key == 0x50) + { + clear_command(count, buf); + count = 0; + //向现在 + change_command(buf, -1); + count = strlen(buf); + } if (key == '\n') return count; - if (key) + if (key && key != 0x50 && key != 0xc8) { if (key == '\b') { From d0d79c828d47a7f41b0351951e89e8f8c34fb727 Mon Sep 17 00:00:00 2001 From: wang904 <1234366@qq.com> Date: Thu, 18 Aug 2022 10:35:52 +0800 Subject: [PATCH 2/4] fix keyboard --- user/apps/shell/shell.c | 1 - user/libs/libKeyboard/keyboard.c | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/user/apps/shell/shell.c b/user/apps/shell/shell.c index dae09f58..9cdda33c 100644 --- a/user/apps/shell/shell.c +++ b/user/apps/shell/shell.c @@ -133,7 +133,6 @@ int shell_readline(int fd, char *buf) { int key = 0; int count = 0; - while (1) { key = keyboard_analyze_keycode(fd); diff --git a/user/libs/libKeyboard/keyboard.c b/user/libs/libKeyboard/keyboard.c index 58a41335..9000f495 100644 --- a/user/libs/libKeyboard/keyboard.c +++ b/user/libs/libKeyboard/keyboard.c @@ -427,6 +427,7 @@ int keyboard_analyze_keycode(int fd) case 0xc8: arrow_u = false; key = OTHER_KEY; + return 0xc8; break; case 0x4b: arrow_l = true; @@ -439,6 +440,7 @@ int keyboard_analyze_keycode(int fd) case 0x50: arrow_d = true; key = OTHER_KEY; + return 0x50; break; case 0xd0: arrow_d = false; From 1c2b423d770f2f0def807805c98a6cd3c4117aae Mon Sep 17 00:00:00 2001 From: fslongjin Date: Thu, 18 Aug 2022 13:37:31 +0800 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- user/apps/shell/shell.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/user/apps/shell/shell.c b/user/apps/shell/shell.c index 9cdda33c..aad96db0 100644 --- a/user/apps/shell/shell.c +++ b/user/apps/shell/shell.c @@ -22,7 +22,7 @@ int shell_readline(int fd, char *buf); void print_ascii_logo(); extern char *shell_current_path; //保存的历史命令 -char history_commands[mem_history][256]; +char history_commands[mem_history][INPUT_BUFFER_SIZE]; int count_history; //现在对应的命令 int pointer; @@ -58,7 +58,7 @@ void main_loop(int kb_fd) if (count) { - char *command_origin[strlen(input_buffer)]; + char command_origin[strlen(input_buffer)]; strcpy(command_origin, input_buffer); int cmd_num = parse_command(input_buffer, &argc, &argv); From 801c1fa6c6e2e2a0f1cd62899ec25b33cd1c32f1 Mon Sep 17 00:00:00 2001 From: fslongjin Date: Thu, 18 Aug 2022 13:39:41 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E5=AE=8F=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E7=9A=84=E6=A0=BC=E5=BC=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- user/apps/shell/shell.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/user/apps/shell/shell.c b/user/apps/shell/shell.c index aad96db0..fcd0ac9d 100644 --- a/user/apps/shell/shell.c +++ b/user/apps/shell/shell.c @@ -9,7 +9,7 @@ #include "cmd.h" #define pause_cpu() asm volatile("pause\n\t"); -#define mem_history 1024 +#define MEM_HISTORY 1024 /** * @brief 循环读取每一行 * @@ -22,7 +22,7 @@ int shell_readline(int fd, char *buf); void print_ascii_logo(); extern char *shell_current_path; //保存的历史命令 -char history_commands[mem_history][INPUT_BUFFER_SIZE]; +char history_commands[MEM_HISTORY][INPUT_BUFFER_SIZE]; int count_history; //现在对应的命令 int pointer;