shell.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <libc/unistd.h>
  2. #include <libc/stdio.h>
  3. #include <libc/fcntl.h>
  4. #include <libc/stdlib.h>
  5. #include <libKeyboard/keyboard.h>
  6. #include <libc/string.h>
  7. #include <libc/stddef.h>
  8. #include "cmd.h"
  9. #define pause_cpu() asm volatile("pause\n\t");
  10. /**
  11. * @brief 循环读取每一行
  12. *
  13. * @param fd 键盘文件描述符
  14. * @param buf 输入缓冲区
  15. * @return 读取的字符数
  16. */
  17. int shell_readline(int fd, char *buf);
  18. void print_ascii_logo();
  19. extern char *shell_current_path;
  20. /**
  21. * @brief shell主循环
  22. *
  23. * @param kb_fd 键盘文件描述符
  24. */
  25. void main_loop(int kb_fd)
  26. {
  27. unsigned char input_buffer[INPUT_BUFFER_SIZE] = {0};
  28. // 初始化当前工作目录的路径
  29. shell_current_path = (char *)malloc(3);
  30. memset(shell_current_path, 0, 3);
  31. shell_current_path[0] = '/';
  32. shell_current_path[1] = '\0';
  33. // shell命令行的主循环
  34. while (true)
  35. {
  36. int argc = 0;
  37. char **argv;
  38. printf("[DragonOS] %s # ", shell_current_path);
  39. memset(input_buffer, 0, INPUT_BUFFER_SIZE);
  40. // 循环读取每一行到buffer
  41. int count = shell_readline(kb_fd, input_buffer);
  42. if (count)
  43. {
  44. int cmd_num = parse_command(input_buffer, &argc, &argv);
  45. printf("\n");
  46. if (cmd_num >= 0)
  47. shell_run_built_in_command(cmd_num, argc, argv);
  48. }
  49. else
  50. printf("\n");
  51. }
  52. }
  53. int main()
  54. {
  55. // 打开键盘文件
  56. char kb_file_path[] = "/dev/keyboard.dev";
  57. int kb_fd = open(kb_file_path, 0);
  58. // printf("keyboard fd = %d\n", kb_fd);
  59. print_ascii_logo();
  60. main_loop(kb_fd);
  61. while (1)
  62. ;
  63. }
  64. /**
  65. * @brief 循环读取每一行
  66. *
  67. * @param fd 键盘文件描述符
  68. * @param buf 输入缓冲区
  69. * @return 读取的字符数
  70. */
  71. int shell_readline(int fd, char *buf)
  72. {
  73. int key = 0;
  74. int count = 0;
  75. while (1)
  76. {
  77. key = keyboard_analyze_keycode(fd);
  78. if (key == '\n')
  79. return count;
  80. if (key)
  81. {
  82. if (key == '\b')
  83. {
  84. if (count > 0)
  85. {
  86. buf[--count] = 0;
  87. printf("%c", '\b');
  88. }
  89. }
  90. else
  91. {
  92. buf[count++] = key;
  93. printf("%c", key);
  94. }
  95. }
  96. // 输入缓冲区满了之后,直接返回
  97. if (count >= INPUT_BUFFER_SIZE - 1)
  98. return count;
  99. pause_cpu();
  100. }
  101. }
  102. void print_ascii_logo()
  103. {
  104. printf("\n\n");
  105. printf(" ____ ___ ____ \n");
  106. printf("| _ \\ _ __ __ _ __ _ ___ _ __ / _ \\ / ___| \n");
  107. printf("| | | || '__| / _` | / _` | / _ \\ | '_ \\ | | | |\\___ \\ \n");
  108. printf("| |_| || | | (_| || (_| || (_) || | | || |_| | ___) |\n");
  109. printf("|____/ |_| \\__,_| \\__, | \\___/ |_| |_| \\___/ |____/ \n");
  110. printf(" |___/ \n");
  111. printf("\n\n");
  112. }