shell.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. int a = 1/0;
  61. main_loop(kb_fd);
  62. while (1)
  63. ;
  64. }
  65. /**
  66. * @brief 循环读取每一行
  67. *
  68. * @param fd 键盘文件描述符
  69. * @param buf 输入缓冲区
  70. * @return 读取的字符数
  71. */
  72. int shell_readline(int fd, char *buf)
  73. {
  74. int key = 0;
  75. int count = 0;
  76. while (1)
  77. {
  78. key = keyboard_analyze_keycode(fd);
  79. if (key == '\n')
  80. return count;
  81. if (key)
  82. {
  83. if (key == '\b')
  84. {
  85. if (count > 0)
  86. {
  87. buf[--count] = 0;
  88. printf("%c", '\b');
  89. }
  90. }
  91. else
  92. {
  93. buf[count++] = key;
  94. printf("%c", key);
  95. }
  96. }
  97. // 输入缓冲区满了之后,直接返回
  98. if (count >= INPUT_BUFFER_SIZE - 1)
  99. return count;
  100. pause_cpu();
  101. }
  102. }
  103. void print_ascii_logo()
  104. {
  105. printf("\n\n");
  106. printf(" ____ ___ ____ \n");
  107. printf("| _ \\ _ __ __ _ __ _ ___ _ __ / _ \\ / ___| \n");
  108. printf("| | | || '__| / _` | / _` | / _ \\ | '_ \\ | | | |\\___ \\ \n");
  109. printf("| |_| || | | (_| || (_| || (_) || | | || |_| | ___) |\n");
  110. printf("|____/ |_| \\__,_| \\__, | \\___/ |_| |_| \\___/ |____/ \n");
  111. printf(" |___/ \n");
  112. printf("\n\n");
  113. }