shell.c 4.3 KB

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