Merge pull request #21 from wang904/patch-add-shell-history

Patch add shell history
This commit is contained in:
login 2022-08-18 13:40:25 +08:00 committed by GitHub
commit 2c48f67a77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 9 deletions

View File

@ -9,7 +9,7 @@
#include "cmd.h" #include "cmd.h"
#define pause_cpu() asm volatile("pause\n\t"); #define pause_cpu() asm volatile("pause\n\t");
#define MEM_HISTORY 1024
/** /**
* @brief * @brief
* *
@ -21,9 +21,11 @@
int shell_readline(int fd, char *buf); int shell_readline(int fd, char *buf);
void print_ascii_logo(); void print_ascii_logo();
extern char *shell_current_path; extern char *shell_current_path;
//保存的历史命令
char history_commands[MEM_HISTORY][INPUT_BUFFER_SIZE];
int count_history;
//现在对应的命令
int pointer;
/** /**
* @brief shell主循环 * @brief shell主循环
* *
@ -31,7 +33,8 @@ extern char *shell_current_path;
*/ */
void main_loop(int kb_fd) void main_loop(int kb_fd)
{ {
count_history = 0;
pointer = 1;
unsigned char input_buffer[INPUT_BUFFER_SIZE] = {0}; unsigned char input_buffer[INPUT_BUFFER_SIZE] = {0};
// 初始化当前工作目录的路径 // 初始化当前工作目录的路径
@ -40,7 +43,6 @@ void main_loop(int kb_fd)
memset(shell_current_path, 0, 3); memset(shell_current_path, 0, 3);
shell_current_path[0] = '/'; shell_current_path[0] = '/';
shell_current_path[1] = '\0'; shell_current_path[1] = '\0';
// shell命令行的主循环 // shell命令行的主循环
while (true) while (true)
{ {
@ -56,10 +58,19 @@ void main_loop(int kb_fd)
if (count) if (count)
{ {
char command_origin[strlen(input_buffer)];
strcpy(command_origin, input_buffer);
int cmd_num = parse_command(input_buffer, &argc, &argv); int cmd_num = parse_command(input_buffer, &argc, &argv);
printf("\n"); printf("\n");
if (cmd_num >= 0) 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); shell_run_built_in_command(cmd_num, argc, argv);
}
} }
else else
printf("\n"); printf("\n");
@ -80,7 +91,37 @@ int main()
while (1) 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 * @brief
* *
@ -92,14 +133,31 @@ int shell_readline(int fd, char *buf)
{ {
int key = 0; int key = 0;
int count = 0; int count = 0;
while (1) while (1)
{ {
key = keyboard_analyze_keycode(fd); 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') if (key == '\n')
return count; return count;
if (key) if (key && key != 0x50 && key != 0xc8)
{ {
if (key == '\b') if (key == '\b')
{ {

View File

@ -427,6 +427,7 @@ int keyboard_analyze_keycode(int fd)
case 0xc8: case 0xc8:
arrow_u = false; arrow_u = false;
key = OTHER_KEY; key = OTHER_KEY;
return 0xc8;
break; break;
case 0x4b: case 0x4b:
arrow_l = true; arrow_l = true;
@ -439,6 +440,7 @@ int keyboard_analyze_keycode(int fd)
case 0x50: case 0x50:
arrow_d = true; arrow_d = true;
key = OTHER_KEY; key = OTHER_KEY;
return 0x50;
break; break;
case 0xd0: case 0xd0:
arrow_d = false; arrow_d = false;