esh_main_loop.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "esh_main_loop.h"
  2. #include "esh_misc.h"
  3. void EShRunLoop() {
  4. struct termios orig_term_attr;
  5. struct termios new_term_attr;
  6. /* set the terminal to raw mode */
  7. tcgetattr(fileno(stdin), &orig_term_attr);
  8. memcpy(&new_term_attr, &orig_term_attr, sizeof(struct termios));
  9. new_term_attr.c_lflag &= ~(ECHO | ICANON | ISIG | IEXTEN);
  10. new_term_attr.c_cc[VTIME] = 0;
  11. new_term_attr.c_cc[VMIN] = 1;
  12. tcsetattr(fileno(stdin), TCSANOW, &new_term_attr);
  13. for (;;) {
  14. EShShowMsg();
  15. char* command = malloc(esh_info_global->max_command_length);
  16. memset(command, 0, esh_info_global->max_command_length);
  17. int command_length = 0;
  18. int current_command_pos = 0;
  19. char input_char;
  20. while (input_char = fgetc(stdin)) {
  21. if (input_char == 10) {
  22. printf("\n");
  23. break; // 'Enter' pressed;
  24. }
  25. if (input_char == EOF) {
  26. continue;
  27. }
  28. if (input_char == '\033') { // Arrow up/down/left/right sequence
  29. fgetc(stdin);
  30. switch(fgetc(stdin)) {
  31. case 'A': // Up
  32. break;
  33. case 'B': // Down
  34. break;
  35. case 'C': // Right
  36. if (current_command_pos < command_length) {
  37. printf("%c", command[current_command_pos]);
  38. ++current_command_pos;
  39. }
  40. break;
  41. case 'D': // Left
  42. if (current_command_pos > 0) {
  43. printf("\b");
  44. --current_command_pos;
  45. }
  46. break;
  47. }
  48. continue;
  49. }
  50. if (input_char == 4) {
  51. // Ctrl + d
  52. printf("\n");
  53. exit(0);
  54. }
  55. if (input_char == 127) {
  56. // Backspace
  57. if (current_command_pos > 0) {
  58. for (int i = current_command_pos - 1; i < command_length; ++i) {
  59. command[i] = command[i + 1];
  60. }
  61. command[command_length - 1] = '\0';
  62. printf("\b");
  63. printf("%s", command + current_command_pos - 1);
  64. printf(" \b");
  65. --current_command_pos;
  66. --command_length;
  67. for (int i = command_length - 1; i >= current_command_pos; --i) {
  68. printf("\b");
  69. }
  70. }
  71. continue;
  72. }
  73. if (input_char == 3) {
  74. // Ctrl + c
  75. printf("\n");
  76. exit(0);
  77. }
  78. if (EShIsShellLetter(input_char)) {
  79. for (int i = command_length; i >= current_command_pos; --i) {
  80. command[i + 1] = command[i];
  81. }
  82. command[current_command_pos] = input_char;
  83. printf("%s", command + current_command_pos);
  84. ++current_command_pos;
  85. ++command_length;
  86. for (int i = command_length; i > current_command_pos; --i) {
  87. printf("\b");
  88. }
  89. continue;
  90. }
  91. }
  92. if (command_length == 0) {
  93. printf("\n");
  94. continue;
  95. }
  96. int pid = fork();
  97. if (pid == 0) {
  98. execlp(command, command, NULL);
  99. printf("Esh: run command %s failed.\n", command);
  100. exit(1); // execlp failed
  101. } else {
  102. waitpid(pid, NULL, 0);
  103. printf("\n");
  104. }
  105. }
  106. /* restore the original terminal attributes */
  107. tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr);
  108. }
  109. void EShShowMsg() {
  110. EShUpdateInviteMessage();
  111. printf("%s", esh_info_global->invite_message);
  112. }
  113. void EShParseCommandIntoJobs(char* command) {
  114. }
  115. void EShExecuteJobs(int jobs_num, EShJob* jobs_list) {
  116. }
  117. void EShSetJobCommandType(EShJob* job) {
  118. }
  119. void EShRunJob(EShJob* job) {
  120. }
  121. int EmptyCommand(const char* command) {
  122. }
  123. int InnerCommand(const char* command) {
  124. }
  125. void EShProcessInnerJob(const char* command) {
  126. }
  127. void EShProcessExecJob(const char* command) {
  128. }