shell.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #define INPUT_BUFFER_SIZE 512
  18. int shell_readline(int fd, char *buf);
  19. extern char *shell_current_path;
  20. /**
  21. * @brief 解析shell命令
  22. *
  23. * @param buf 输入缓冲区
  24. * @param argc 返回值:参数数量
  25. * @param argv 返回值:参数列表
  26. * @return int
  27. */
  28. int parse_command(char *buf, int *argc, char ***argv);
  29. /**
  30. * @brief shell主循环
  31. *
  32. * @param kb_fd 键盘文件描述符
  33. */
  34. void main_loop(int kb_fd)
  35. {
  36. unsigned char input_buffer[INPUT_BUFFER_SIZE] = {0};
  37. // 初始化当前工作目录的路径
  38. shell_current_path = (char *)malloc(3);
  39. memset(shell_current_path, 0, 3);
  40. shell_current_path[0] = '/';
  41. shell_current_path[1] = '\0';
  42. // shell命令行的主循环
  43. while (true)
  44. {
  45. int argc = 0;
  46. char **argv;
  47. printf("[DragonOS] %s # ", shell_current_path);
  48. memset(input_buffer, 0, INPUT_BUFFER_SIZE);
  49. // 循环读取每一行到buffer
  50. int count = shell_readline(kb_fd, input_buffer);
  51. if (count)
  52. {
  53. int cmd_num = parse_command(input_buffer, &argc, &argv);
  54. printf("\n");
  55. if (cmd_num >= 0)
  56. shell_run_built_in_command(cmd_num, argc, argv);
  57. }
  58. else
  59. printf("\n");
  60. }
  61. }
  62. int main()
  63. {
  64. // 打开键盘文件
  65. char kb_file_path[] = "/dev/keyboard.dev";
  66. int kb_fd = open(kb_file_path, 0);
  67. // printf("keyboard fd = %d\n", kb_fd);
  68. main_loop(kb_fd);
  69. while (1)
  70. ;
  71. }
  72. /**
  73. * @brief 循环读取每一行
  74. *
  75. * @param fd 键盘文件描述符
  76. * @param buf 输入缓冲区
  77. * @return 读取的字符数
  78. */
  79. int shell_readline(int fd, char *buf)
  80. {
  81. int key = 0;
  82. int count = 0;
  83. while (1)
  84. {
  85. key = keyboard_analyze_keycode(fd);
  86. if (key == '\n')
  87. return count;
  88. if (key)
  89. {
  90. if (key == '\b')
  91. {
  92. if (count > 0)
  93. {
  94. buf[--count] = 0;
  95. printf("%c", '\b');
  96. }
  97. }
  98. else
  99. {
  100. buf[count++] = key;
  101. printf("%c", key);
  102. }
  103. }
  104. // 输入缓冲区满了之后,直接返回
  105. if (count >= INPUT_BUFFER_SIZE - 1)
  106. return count;
  107. pause_cpu();
  108. }
  109. }
  110. /**
  111. * @brief 解析shell命令
  112. *
  113. * @param buf 输入缓冲区
  114. * @param argc 返回值:参数数量
  115. * @param argv 返回值:参数列表
  116. * @return int 主命令的编号
  117. */
  118. int parse_command(char *buf, int *argc, char ***argv)
  119. {
  120. // printf("parse command\n");
  121. int index = 0; // 当前访问的是buf的第几位
  122. // 去除命令前导的空格
  123. while (index < INPUT_BUFFER_SIZE && buf[index] == ' ')
  124. ++index;
  125. // 计算参数数量
  126. for (int i = index; i < (INPUT_BUFFER_SIZE - 1); ++i)
  127. {
  128. // 到达了字符串末尾
  129. if (!buf[i])
  130. break;
  131. if (buf[i] != ' ' && (buf[i + 1] == ' ' || buf[i + 1] == '\0'))
  132. ++(*argc);
  133. }
  134. // printf("\nargc=%d\n", *argc);
  135. // 为指向每个指令的指针分配空间
  136. *argv = (char **)malloc(sizeof(char **) * (*argc));
  137. memset(*argv, 0, sizeof(char **) * (*argc));
  138. // 将每个命令都单独提取出来
  139. for (int i = 0; i < *argc && index < INPUT_BUFFER_SIZE; ++i)
  140. {
  141. // 提取出命令,以空格作为分割
  142. *((*argv) + i) = &buf[index];
  143. while (index < (INPUT_BUFFER_SIZE - 1) && buf[index] && buf[index] != ' ')
  144. ++index;
  145. buf[index++] = '\0';
  146. // 删除命令间多余的空格
  147. while (index < INPUT_BUFFER_SIZE && buf[index] == ' ')
  148. ++index;
  149. // printf("%s\n", (*argv)[i]);
  150. }
  151. // 以第一个命令作为主命令,查找其在命令表中的编号
  152. return shell_find_cmd(**argv);
  153. }