shell.c 3.1 KB

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