esh_main_loop.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #include "esh_main_loop.h"
  2. #include "esh_misc.h"
  3. #include "esh_history.h"
  4. #include "esh_init.h"
  5. void EShRunLoop() {
  6. struct termios orig_term_attr;
  7. struct termios new_term_attr;
  8. /* set the terminal to semi-raw mode */
  9. tcgetattr(fileno(stdin), &orig_term_attr);
  10. memcpy(&new_term_attr, &orig_term_attr, sizeof(struct termios));
  11. new_term_attr.c_lflag &= ~(ECHO | ICANON | ISIG | IEXTEN);
  12. new_term_attr.c_cc[VTIME] = 0;
  13. new_term_attr.c_cc[VMIN] = 1;
  14. tcsetattr(fileno(stdin), TCSANOW, &new_term_attr);
  15. char* command = malloc(esh_info_global->max_command_length);
  16. char* uncomplete_command = malloc(esh_info_global->max_command_length); // Used to restore after moves history
  17. for (;;) {
  18. EShShowMsg();
  19. memset(command, 0, esh_info_global->max_command_length);
  20. int command_length = 0;
  21. int current_command_pos = 0;
  22. int current_history_step = 0;
  23. char input_char;
  24. memset(uncomplete_command, 0, esh_info_global->max_command_length);
  25. while (input_char = fgetc(stdin)) {
  26. if (input_char == 10) {
  27. printf("\n");
  28. break; // 'Enter' pressed;
  29. }
  30. if (input_char == EOF) {
  31. continue;
  32. }
  33. if (input_char == '\033') { // Arrow up/down/left/right sequence
  34. fgetc(stdin);
  35. switch(fgetc(stdin)) {
  36. case 'A':
  37. {
  38. if (current_history_step == 0) {
  39. strcpy(uncomplete_command, command);
  40. }
  41. char* new_command = EShReceiveCommandFromHistory(current_history_step);
  42. if (new_command != NULL) {
  43. ++current_history_step;
  44. for (int i = current_command_pos; i < command_length; ++i) {
  45. printf(" ");
  46. ++current_command_pos;
  47. }
  48. for (int i = current_command_pos; i > 0; --i) {
  49. printf("\b \b");
  50. }
  51. strcpy(command, new_command);
  52. command_length = strlen(new_command);
  53. current_command_pos = command_length;
  54. for (int i = 0; i < command_length; ++i) {
  55. printf("%c", command[i]);
  56. }
  57. }
  58. free(new_command);
  59. break;
  60. }
  61. case 'B': // Down
  62. {
  63. char* new_command;
  64. if (current_history_step > 0) {
  65. --current_history_step;
  66. }
  67. if (current_history_step > 0) {
  68. new_command = EShReceiveCommandFromHistory(current_history_step - 1);
  69. } else {
  70. new_command = uncomplete_command;
  71. }
  72. for (int i = current_command_pos; i < command_length; ++i) {
  73. printf(" ");
  74. ++current_command_pos;
  75. }
  76. for (int i = current_command_pos; i > 0; --i) {
  77. printf("\b \b");
  78. }
  79. strcpy(command, new_command);
  80. command_length = strlen(new_command);
  81. current_command_pos = command_length;
  82. for (int i = 0; i < command_length; ++i) {
  83. printf("%c", command[i]);
  84. }
  85. if (current_history_step > 0) {
  86. free(new_command);
  87. }
  88. break;
  89. }
  90. case 'C': // Right
  91. if (current_command_pos < command_length) {
  92. printf("%c", command[current_command_pos]);
  93. ++current_command_pos;
  94. }
  95. break;
  96. case 'D': // Left
  97. if (current_command_pos > 0) {
  98. printf("\b");
  99. --current_command_pos;
  100. }
  101. break;
  102. }
  103. continue;
  104. }
  105. if (input_char == 4) {
  106. // Ctrl + d
  107. printf("\n");
  108. exit(0);
  109. }
  110. if (input_char == 127) {
  111. // Backspace
  112. if (current_command_pos > 0) {
  113. for (int i = current_command_pos - 1; i < command_length; ++i) {
  114. command[i] = command[i + 1];
  115. }
  116. command[command_length - 1] = '\0';
  117. printf("\b");
  118. printf("%s", command + current_command_pos - 1);
  119. printf(" \b");
  120. --current_command_pos;
  121. --command_length;
  122. for (int i = command_length - 1; i >= current_command_pos; --i) {
  123. printf("\b");
  124. }
  125. }
  126. continue;
  127. }
  128. if (input_char == 3) {
  129. // Ctrl + c
  130. printf("\n");
  131. exit(0);
  132. }
  133. if (EShIsShellLetter(input_char)) {
  134. for (int i = command_length; i >= current_command_pos; --i) {
  135. command[i + 1] = command[i];
  136. }
  137. command[current_command_pos] = input_char;
  138. printf("%s", command + current_command_pos);
  139. ++current_command_pos;
  140. ++command_length;
  141. for (int i = command_length; i > current_command_pos; --i) {
  142. printf("\b");
  143. }
  144. continue;
  145. }
  146. }
  147. if (command_length == 0) {
  148. printf("\n");
  149. continue;
  150. }
  151. EShAddCommandToHistory(command);
  152. EShJob* jobs;
  153. int jobs_num = 0;
  154. jobs = EShParseCommandIntoJobs(command, &jobs_num);
  155. // EShPrintJobsDebugInfo(command, jobs, jobs_num);
  156. EShExecuteJobs(jobs_num, jobs);
  157. }
  158. /* restore the original terminal attributes */
  159. tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr);
  160. }
  161. void EShShowMsg() {
  162. EShUpdateInviteMessage();
  163. printf("%s", esh_info_global->invite_message);
  164. }
  165. EShJob* EShParseCommandIntoJobs(char* command, int* jobs_num) {
  166. EShJob* jobs = EShMakeJobsList();
  167. *jobs_num = 1;
  168. int current_job_cmd_length = 0;
  169. for (int i = 0; i < strlen(command); ++i) {
  170. ESH_JOB_DELIMITER delimiter = EshIsJobDelimiter(command + i);
  171. if (delimiter != NOT_A_DELIMITER) {
  172. EShSplitJobCommand(&jobs[*jobs_num - 1], ' ');
  173. if (delimiter == SEMICOLON) {
  174. jobs[*jobs_num].job_start_condition = NO_CONDITION;
  175. }
  176. if (delimiter == LOGIC_AND) {
  177. jobs[*jobs_num].job_start_condition = PREVIOUS_EXIT_SUCCESS;
  178. }
  179. if (delimiter == LOGIC_OR) {
  180. jobs[*jobs_num].job_start_condition = PREVIOUS_EXIT_FAILED;
  181. }
  182. // TODO: delimiter == BIT_OR and BIT_AND
  183. ++(*jobs_num);
  184. current_job_cmd_length = 0;
  185. i += EShGetJobDelimiterSize(EshIsJobDelimiter(command + i)) - 1;
  186. continue;
  187. }
  188. jobs[*jobs_num - 1].command[current_job_cmd_length] = command[i];
  189. ++current_job_cmd_length;
  190. }
  191. EShSplitJobCommand(&jobs[*jobs_num - 1], ' ');
  192. return jobs;
  193. }
  194. void EShSplitJobCommand(EShJob* job, char delim) {
  195. int token_status = 0; // 0 - token result[result_size] finished
  196. // 1 - token result[result_size] not finished
  197. int current_token_length = 0;
  198. for (int i = 0; i < strlen(job->command); ++i) {
  199. while (job->command[i] == delim && i < strlen(job->command)) {
  200. if (token_status == 1) {
  201. token_status = 0;
  202. }
  203. ++i;
  204. }
  205. if (i == strlen(job->command)) {
  206. if (token_status == 1) {
  207. ++job->command_tokens_num;
  208. }
  209. break;
  210. }
  211. if (token_status == 0) {
  212. token_status = 1;
  213. ++job->command_tokens_num;
  214. current_token_length = 0;
  215. }
  216. job->command_tokens[job->command_tokens_num - 1][current_token_length] = job->command[i];
  217. ++current_token_length;
  218. }
  219. }
  220. void EShExecuteJobs(int jobs_num, EShJob* jobs_list) {
  221. for (int i = 0; i < jobs_num; ++i) {
  222. EShRunJob(&jobs_list[i]);
  223. }
  224. }
  225. void EShSetJobCommandType(EShJob* job) {
  226. }
  227. void EShRunJob(EShJob* job) {
  228. // TODO: PIPES INIT & BG PROCESS SUPPORT
  229. int pid = fork();
  230. if (pid == 0) {
  231. for (int i = job->command_tokens_num; i < esh_info_global->max_command_tokens; ++i) {
  232. free(job->command_tokens[i]);
  233. job->command_tokens[i] = NULL;
  234. }
  235. execvp(job->command_tokens[0], job->command_tokens);
  236. printf("Esh: run command %s failed.\n", job->command);
  237. exit(1); // execlp failed
  238. } else {
  239. waitpid(pid, NULL, 0);
  240. printf("\n");
  241. }
  242. }
  243. int EmptyCommand(const char* command) {
  244. }
  245. int InnerCommand(const char* command) {
  246. }
  247. void EShProcessInnerJob(const char* command) {
  248. }
  249. void EShProcessExecJob(const char* command) {
  250. }
  251. void EShPrintJobsDebugInfo(const char* command, EShJob* jobs, int jobs_num) {
  252. printf("\nD: Splitted command (%s) into %d jobs:\n", command, jobs_num);
  253. for (int i = 0; i < jobs_num; ++i) {
  254. printf("===================\n"
  255. "Job #%d:\n"
  256. "Cmd: %s\n"
  257. "Cmd tokens: %d ",
  258. i, jobs[i].command, jobs[i].command_tokens_num);
  259. for (int j = 0; j < jobs[i].command_tokens_num; ++j) {
  260. printf("{%s} ", jobs[i].command_tokens[j]);
  261. }
  262. printf("\n===================\n");
  263. }
  264. printf("\n");
  265. }