esh_main_loop.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include "esh_main_loop.h"
  2. #include "esh_misc.h"
  3. #include "esh_history.h"
  4. void EShRunLoop() {
  5. struct termios orig_term_attr;
  6. struct termios new_term_attr;
  7. /* set the terminal to raw mode */
  8. tcgetattr(fileno(stdin), &orig_term_attr);
  9. memcpy(&new_term_attr, &orig_term_attr, sizeof(struct termios));
  10. new_term_attr.c_lflag &= ~(ECHO | ICANON | ISIG | IEXTEN);
  11. new_term_attr.c_cc[VTIME] = 0;
  12. new_term_attr.c_cc[VMIN] = 1;
  13. tcsetattr(fileno(stdin), TCSANOW, &new_term_attr);
  14. char* command = malloc(esh_info_global->max_command_length);
  15. char* uncomplete_command = malloc(esh_info_global->max_command_length); // Used to restore after moves history
  16. for (;;) {
  17. EShShowMsg();
  18. memset(command, 0, esh_info_global->max_command_length);
  19. int command_length = 0;
  20. int current_command_pos = 0;
  21. int current_history_step = 0;
  22. char input_char;
  23. memset(uncomplete_command, 0, esh_info_global->max_command_length);
  24. while (input_char = fgetc(stdin)) {
  25. if (input_char == 10) {
  26. printf("\n");
  27. break; // 'Enter' pressed;
  28. }
  29. if (input_char == EOF) {
  30. continue;
  31. }
  32. if (input_char == '\033') { // Arrow up/down/left/right sequence
  33. fgetc(stdin);
  34. switch(fgetc(stdin)) {
  35. case 'A':
  36. {
  37. if (current_history_step == 0) {
  38. strcpy(uncomplete_command, command);
  39. }
  40. char* new_command = EShReceiveCommandFromHistory(current_history_step);
  41. if (new_command != NULL) {
  42. ++current_history_step;
  43. for (int i = current_command_pos; i < command_length; ++i) {
  44. printf(" ");
  45. ++current_command_pos;
  46. }
  47. for (int i = current_command_pos; i > 0; --i) {
  48. printf("\b \b");
  49. }
  50. strcpy(command, new_command);
  51. command_length = strlen(new_command);
  52. current_command_pos = command_length;
  53. for (int i = 0; i < command_length; ++i) {
  54. printf("%c", command[i]);
  55. }
  56. }
  57. free(new_command);
  58. break;
  59. }
  60. case 'B': // Down
  61. {
  62. char* new_command;
  63. if (current_history_step > 0) {
  64. --current_history_step;
  65. }
  66. if (current_history_step > 0) {
  67. new_command = EShReceiveCommandFromHistory(current_history_step - 1);
  68. } else {
  69. new_command = uncomplete_command;
  70. }
  71. for (int i = current_command_pos; i < command_length; ++i) {
  72. printf(" ");
  73. ++current_command_pos;
  74. }
  75. for (int i = current_command_pos; i > 0; --i) {
  76. printf("\b \b");
  77. }
  78. strcpy(command, new_command);
  79. command_length = strlen(new_command);
  80. current_command_pos = command_length;
  81. for (int i = 0; i < command_length; ++i) {
  82. printf("%c", command[i]);
  83. }
  84. if (current_history_step > 0) {
  85. free(new_command);
  86. }
  87. break;
  88. }
  89. case 'C': // Right
  90. if (current_command_pos < command_length) {
  91. printf("%c", command[current_command_pos]);
  92. ++current_command_pos;
  93. }
  94. break;
  95. case 'D': // Left
  96. if (current_command_pos > 0) {
  97. printf("\b");
  98. --current_command_pos;
  99. }
  100. break;
  101. }
  102. continue;
  103. }
  104. if (input_char == 4) {
  105. // Ctrl + d
  106. printf("\n");
  107. exit(0);
  108. }
  109. if (input_char == 127) {
  110. // Backspace
  111. if (current_command_pos > 0) {
  112. for (int i = current_command_pos - 1; i < command_length; ++i) {
  113. command[i] = command[i + 1];
  114. }
  115. command[command_length - 1] = '\0';
  116. printf("\b");
  117. printf("%s", command + current_command_pos - 1);
  118. printf(" \b");
  119. --current_command_pos;
  120. --command_length;
  121. for (int i = command_length - 1; i >= current_command_pos; --i) {
  122. printf("\b");
  123. }
  124. }
  125. continue;
  126. }
  127. if (input_char == 3) {
  128. // Ctrl + c
  129. printf("\n");
  130. exit(0);
  131. }
  132. if (EShIsShellLetter(input_char)) {
  133. for (int i = command_length; i >= current_command_pos; --i) {
  134. command[i + 1] = command[i];
  135. }
  136. command[current_command_pos] = input_char;
  137. printf("%s", command + current_command_pos);
  138. ++current_command_pos;
  139. ++command_length;
  140. for (int i = command_length; i > current_command_pos; --i) {
  141. printf("\b");
  142. }
  143. continue;
  144. }
  145. }
  146. if (command_length == 0) {
  147. printf("\n");
  148. continue;
  149. }
  150. EShAddCommandToHistory(command);
  151. int pid = fork();
  152. if (pid == 0) {
  153. execlp(command, command, NULL);
  154. printf("Esh: run command %s failed.\n", command);
  155. exit(1); // execlp failed
  156. } else {
  157. waitpid(pid, NULL, 0);
  158. printf("\n");
  159. }
  160. }
  161. /* restore the original terminal attributes */
  162. tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr);
  163. }
  164. void EShShowMsg() {
  165. EShUpdateInviteMessage();
  166. printf("%s", esh_info_global->invite_message);
  167. }
  168. void EShParseCommandIntoJobs(char* command) {
  169. }
  170. void EShExecuteJobs(int jobs_num, EShJob* jobs_list) {
  171. }
  172. void EShSetJobCommandType(EShJob* job) {
  173. }
  174. void EShRunJob(EShJob* job) {
  175. }
  176. int EmptyCommand(const char* command) {
  177. }
  178. int InnerCommand(const char* command) {
  179. }
  180. void EShProcessInnerJob(const char* command) {
  181. }
  182. void EShProcessExecJob(const char* command) {
  183. }