DragonOS/user/apps/shell/shell.c

181 lines
4.1 KiB
C
Raw Normal View History

#include <libc/unistd.h>
#include <libc/stdio.h>
#include <libc/fcntl.h>
2022-05-07 05:46:23 +00:00
#include <libc/stdlib.h>
2022-05-21 13:49:56 +00:00
#include <libKeyboard/keyboard.h>
2022-05-24 08:37:28 +00:00
#include <libc/string.h>
#include <libc/stddef.h>
#include "cmd.h"
#define pause_cpu() asm volatile("pause\n\t");
/**
* @brief
*
* @param fd
* @param buf
* @return
*/
#define INPUT_BUFFER_SIZE 512
int shell_readline(int fd, char *buf);
extern char *shell_current_path;
/**
* @brief shell命令
*
* @param buf
* @param argc
* @param argv
* @return int
*/
int parse_command(char *buf, int *argc, char ***argv);
/**
* @brief shell主循环
*
* @param kb_fd
*/
2022-05-31 13:55:06 +00:00
void main_loop(int kb_fd)
{
2022-05-31 13:55:06 +00:00
2022-05-24 08:37:28 +00:00
unsigned char input_buffer[INPUT_BUFFER_SIZE] = {0};
2022-05-07 05:46:23 +00:00
2022-05-24 08:37:28 +00:00
// 初始化当前工作目录的路径
2022-05-25 14:50:32 +00:00
shell_current_path = (char *)malloc(3);
2022-05-31 13:55:06 +00:00
2022-06-07 15:18:26 +00:00
memset(shell_current_path, 0, 3);
2022-05-25 14:50:32 +00:00
shell_current_path[0] = '/';
shell_current_path[1] = '\0';
2022-05-21 13:49:56 +00:00
2022-05-24 08:37:28 +00:00
// shell命令行的主循环
2022-05-21 13:49:56 +00:00
while (true)
{
2022-05-24 08:37:28 +00:00
int argc = 0;
char **argv;
2022-05-07 06:14:11 +00:00
2022-05-25 14:50:32 +00:00
printf("[DragonOS] %s # ", shell_current_path);
2022-05-31 13:55:06 +00:00
2022-05-24 08:37:28 +00:00
memset(input_buffer, 0, INPUT_BUFFER_SIZE);
2022-05-07 06:14:11 +00:00
2022-05-24 08:37:28 +00:00
// 循环读取每一行到buffer
2022-05-25 14:50:32 +00:00
int count = shell_readline(kb_fd, input_buffer);
2022-05-25 14:50:32 +00:00
if (count)
{
int cmd_num = parse_command(input_buffer, &argc, &argv);
printf("\n");
if (cmd_num >= 0)
shell_run_built_in_command(cmd_num, argc, argv);
}
else
printf("\n");
2022-05-24 08:37:28 +00:00
}
}
2022-05-07 06:14:11 +00:00
2022-05-24 08:37:28 +00:00
int main()
{
// 打开键盘文件
char kb_file_path[] = "/dev/keyboard.dev";
int kb_fd = open(kb_file_path, 0);
// printf("keyboard fd = %d\n", kb_fd);
2022-05-07 06:14:11 +00:00
2022-05-24 08:37:28 +00:00
main_loop(kb_fd);
while (1)
;
}
/**
* @brief
*
* @param fd
* @param buf
* @return
*/
int shell_readline(int fd, char *buf)
{
int key = 0;
int count = 0;
2022-05-11 03:33:29 +00:00
2022-05-24 08:37:28 +00:00
while (1)
2022-05-11 03:33:29 +00:00
{
2022-05-24 08:37:28 +00:00
key = keyboard_analyze_keycode(fd);
if (key == '\n')
return count;
2022-05-24 08:37:28 +00:00
if (key)
2022-05-11 03:33:29 +00:00
{
if (key == '\b')
{
if (count > 0)
{
buf[--count] = 0;
printf("%c", '\b');
}
}
else
{
buf[count++] = key;
printf("%c", key);
}
2022-05-11 03:33:29 +00:00
}
2022-05-24 08:37:28 +00:00
// 输入缓冲区满了之后,直接返回
if (count >= INPUT_BUFFER_SIZE - 1)
return count;
pause_cpu();
2022-05-11 03:33:29 +00:00
}
2022-05-24 08:37:28 +00:00
}
/**
* @brief shell命令
*
* @param buf
* @param argc
* @param argv
* @return int
*/
int parse_command(char *buf, int *argc, char ***argv)
{
2022-05-25 14:50:32 +00:00
// printf("parse command\n");
2022-05-24 08:37:28 +00:00
int index = 0; // 当前访问的是buf的第几位
// 去除命令前导的空格
while (index < INPUT_BUFFER_SIZE && buf[index] == ' ')
++index;
// 计算参数数量
2022-05-25 14:50:32 +00:00
for (int i = index; i < (INPUT_BUFFER_SIZE - 1); ++i)
2022-05-24 08:37:28 +00:00
{
// 到达了字符串末尾
if (!buf[i])
break;
if (buf[i] != ' ' && (buf[i + 1] == ' ' || buf[i + 1] == '\0'))
++(*argc);
}
// printf("\nargc=%d\n", *argc);
// 为指向每个指令的指针分配空间
*argv = (char **)malloc(sizeof(char **) * (*argc));
memset(*argv, 0, sizeof(char **) * (*argc));
// 将每个命令都单独提取出来
for (int i = 0; i < *argc && index < INPUT_BUFFER_SIZE; ++i)
{
// 提取出命令,以空格作为分割
*((*argv) + i) = &buf[index];
2022-05-25 14:50:32 +00:00
while (index < (INPUT_BUFFER_SIZE - 1) && buf[index] && buf[index] != ' ')
2022-05-24 08:37:28 +00:00
++index;
buf[index++] = '\0';
// 删除命令间多余的空格
while (index < INPUT_BUFFER_SIZE && buf[index] == ' ')
++index;
// printf("%s\n", (*argv)[i]);
}
// 以第一个命令作为主命令,查找其在命令表中的编号
return shell_find_cmd(**argv);
}