shell.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 <libc/sys/stat.h>
  9. #include "cmd.h"
  10. #define pause_cpu() asm volatile("pause\n\t");
  11. #define MEM_HISTORY 1024
  12. /**
  13. * @brief 循环读取每一行
  14. *
  15. * @param fd 键盘文件描述符
  16. * @param buf 输入缓冲区
  17. * @return 读取的字符数
  18. */
  19. int shell_readline(int fd, char *buf);
  20. void print_ascii_logo();
  21. extern char *shell_current_path;
  22. //保存的历史命令(瞬时更改)
  23. char history_commands[MEM_HISTORY][INPUT_BUFFER_SIZE];
  24. //真正的历史命令
  25. char real_history_commands[MEM_HISTORY][INPUT_BUFFER_SIZE];
  26. int count_history;
  27. //现在对应的命令
  28. int current_command_index;
  29. /**
  30. * @brief shell主循环
  31. *
  32. * @param kb_fd 键盘文件描述符
  33. */
  34. void main_loop(int kb_fd)
  35. {
  36. count_history = 0;
  37. current_command_index = 0;
  38. unsigned char input_buffer[INPUT_BUFFER_SIZE] = {0};
  39. // 初始化当前工作目录的路径
  40. shell_current_path = (char *)malloc(3);
  41. memset(shell_current_path, 0, 3);
  42. shell_current_path[0] = '/';
  43. shell_current_path[1] = '\0';
  44. // shell命令行的主循环
  45. while (true)
  46. {
  47. int argc = 0;
  48. char **argv;
  49. printf("[DragonOS] %s # ", shell_current_path);
  50. memset(input_buffer, 0, INPUT_BUFFER_SIZE);
  51. // 循环读取每一行到buffer
  52. count_history++;
  53. int count = shell_readline(kb_fd, input_buffer);
  54. if (!count || current_command_index < count_history - 1)
  55. count_history--;
  56. if (count)
  57. {
  58. strcpy(real_history_commands[count_history - 1], input_buffer);
  59. count_history++;
  60. memset(history_commands, 0, sizeof(history_commands));
  61. for (int i = 0; i <= count_history - 2; i++)
  62. strcpy(history_commands[i], real_history_commands[i]);
  63. current_command_index = count_history - 1;
  64. }
  65. if (count)
  66. {
  67. char command_origin[strlen(input_buffer)];
  68. strcpy(command_origin, input_buffer);
  69. int cmd_num = parse_command(input_buffer, &argc, &argv);
  70. printf("\n");
  71. if (cmd_num >= 0)
  72. shell_run_built_in_command(cmd_num, argc, argv);
  73. }
  74. else
  75. printf("\n");
  76. }
  77. }
  78. int main()
  79. {
  80. // 打开键盘文件
  81. char kb_file_path[] = "/dev/char/ps2.kb0";
  82. int kb_fd = open(kb_file_path, 0);
  83. print_ascii_logo();
  84. // printf("before mkdir\n");
  85. // mkdir("/aaac", 0);
  86. // printf("after mkdir\n");
  87. main_loop(kb_fd);
  88. while (1)
  89. ;
  90. }
  91. /**
  92. * @brief 清除缓冲区
  93. *
  94. * @param count 缓冲区大小
  95. * @param buf 缓冲区内容
  96. */
  97. void clear_command(int count, char *buf)
  98. {
  99. for (int i = 0; i < count; i++)
  100. printf("%c", '\b');
  101. memset(buf, 0, sizeof(buf));
  102. }
  103. /**
  104. * @brief 切换命令(写入到缓冲区)
  105. *
  106. * @param buf 缓冲区
  107. * @param type 如果为1,就向下,如果为-1,就向上
  108. */
  109. void change_command(char *buf, int type)
  110. {
  111. current_command_index -= type;
  112. //处理边界
  113. if (current_command_index < 0)
  114. current_command_index++;
  115. if (current_command_index >= count_history - 1)
  116. current_command_index = count_history - 2;
  117. strcpy(buf, history_commands[current_command_index]);
  118. printf("%s", buf);
  119. }
  120. /**
  121. * @brief 循环读取每一行
  122. *
  123. * @param fd 键盘文件描述符
  124. * @param buf 输入缓冲区
  125. * @return 读取的字符数
  126. */
  127. int shell_readline(int fd, char *buf)
  128. {
  129. int key = 0;
  130. int count = 0;
  131. while (1)
  132. {
  133. key = keyboard_analyze_keycode(fd);
  134. //向上方向键
  135. if (count_history != 0 && key == 0xc8)
  136. {
  137. clear_command(count, buf);
  138. count = 0;
  139. //向历史
  140. change_command(buf, 1);
  141. count = strlen(buf);
  142. }
  143. //向下方向键
  144. if (count_history != 0 && key == 0x50)
  145. {
  146. clear_command(count, buf);
  147. count = 0;
  148. //向现在
  149. change_command(buf, -1);
  150. count = strlen(buf);
  151. }
  152. if (key == '\n')
  153. {
  154. if (count > 0 && current_command_index >= count_history)
  155. {
  156. memset(history_commands[current_command_index - 1], 0, sizeof(history_commands[current_command_index - 1]));
  157. count_history--;
  158. }
  159. return count;
  160. }
  161. if (key && key != 0x50 && key != 0xc8)
  162. {
  163. if (key == '\b')
  164. {
  165. if (count > 0)
  166. {
  167. buf[--count] = 0;
  168. printf("%c", '\b');
  169. }
  170. }
  171. else
  172. {
  173. buf[count++] = key;
  174. printf("%c", key);
  175. }
  176. if (count > 0 && current_command_index >= count_history)
  177. {
  178. memset(history_commands[count_history], 0, sizeof(history_commands[count_history]));
  179. strcpy(history_commands[count_history], buf);
  180. }
  181. else if (count > 0)
  182. {
  183. memset(history_commands[current_command_index], 0, sizeof(history_commands[current_command_index]));
  184. strcpy(history_commands[current_command_index], buf);
  185. }
  186. }
  187. // 输入缓冲区满了之后,直接返回
  188. if (count >= INPUT_BUFFER_SIZE - 1)
  189. return count;
  190. pause_cpu();
  191. }
  192. }
  193. void print_ascii_logo()
  194. {
  195. printf("\n\n");
  196. printf(" ____ ___ ____ \n");
  197. printf("| _ \\ _ __ __ _ __ _ ___ _ __ / _ \\ / ___| \n");
  198. printf("| | | || '__| / _` | / _` | / _ \\ | '_ \\ | | | |\\___ \\ \n");
  199. printf("| |_| || | | (_| || (_| || (_) || | | || |_| | ___) |\n");
  200. printf("|____/ |_| \\__,_| \\__, | \\___/ |_| |_| \\___/ |____/ \n");
  201. printf(" |___/ \n");
  202. printf("\n\n");
  203. }