esh_main_loop.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. const 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. break;
  59. }
  60. case 'B': // Down
  61. {
  62. const 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. break;
  85. }
  86. case 'C': // Right
  87. if (current_command_pos < command_length) {
  88. printf("%c", command[current_command_pos]);
  89. ++current_command_pos;
  90. }
  91. break;
  92. case 'D': // Left
  93. if (current_command_pos > 0) {
  94. printf("\b");
  95. --current_command_pos;
  96. }
  97. break;
  98. }
  99. continue;
  100. }
  101. if (input_char == 4) {
  102. // Ctrl + d
  103. printf("\n");
  104. exit(0);
  105. }
  106. if (input_char == 127) {
  107. // Backspace
  108. if (current_command_pos > 0) {
  109. for (int i = current_command_pos - 1; i < command_length; ++i) {
  110. command[i] = command[i + 1];
  111. }
  112. command[command_length - 1] = '\0';
  113. printf("\b");
  114. printf("%s", command + current_command_pos - 1);
  115. printf(" \b");
  116. --current_command_pos;
  117. --command_length;
  118. for (int i = command_length - 1; i >= current_command_pos; --i) {
  119. printf("\b");
  120. }
  121. }
  122. continue;
  123. }
  124. if (input_char == 3) {
  125. // Ctrl + c
  126. printf("\n");
  127. exit(0);
  128. }
  129. if (EShIsShellLetter(input_char)) {
  130. for (int i = command_length; i >= current_command_pos; --i) {
  131. command[i + 1] = command[i];
  132. }
  133. command[current_command_pos] = input_char;
  134. printf("%s", command + current_command_pos);
  135. ++current_command_pos;
  136. ++command_length;
  137. for (int i = command_length; i > current_command_pos; --i) {
  138. printf("\b");
  139. }
  140. continue;
  141. }
  142. }
  143. if (command_length == 0) {
  144. printf("\n");
  145. continue;
  146. }
  147. EShAddCommandToHistory(command);
  148. EShJob* jobs;
  149. int jobs_num = 0;
  150. jobs = EShParseCommandIntoJobs(command, &jobs_num);
  151. // EShPrintJobsDebugInfo(command, jobs, jobs_num);
  152. EShExecuteJobs(jobs_num, jobs);
  153. }
  154. free(command);
  155. free(uncomplete_command);
  156. /* restore the original terminal attributes */
  157. tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr);
  158. }
  159. void EShShowMsg() {
  160. EShUpdateInviteMessage();
  161. printf("%s", esh_info_global->invite_message);
  162. }
  163. EShJob* EShParseCommandIntoJobs(char* command, int* jobs_num) {
  164. EShJob* jobs = EShMakeJobsList();
  165. *jobs_num = 1;
  166. int current_job_cmd_length = 0;
  167. for (int i = 0; i < strlen(command); ++i) {
  168. ESH_JOB_DELIMITER delimiter = EshIsJobDelimiter(command + i);
  169. if (delimiter != NOT_A_DELIMITER) {
  170. EShSplitJobCommand(&jobs[*jobs_num - 1], ' ');
  171. if (delimiter == SEMICOLON) {
  172. jobs[*jobs_num].job_start_condition = NO_CONDITION;
  173. }
  174. if (delimiter == LOGIC_AND) {
  175. jobs[*jobs_num].job_start_condition = PREVIOUS_EXIT_SUCCESS;
  176. }
  177. if (delimiter == LOGIC_OR) {
  178. jobs[*jobs_num].job_start_condition = PREVIOUS_EXIT_FAILED;
  179. }
  180. // TODO: delimiter == BIT_OR and BIT_AND
  181. ++(*jobs_num);
  182. current_job_cmd_length = 0;
  183. i += EShGetJobDelimiterSize(EshIsJobDelimiter(command + i)) - 1;
  184. continue;
  185. }
  186. jobs[*jobs_num - 1].command[current_job_cmd_length] = command[i];
  187. ++current_job_cmd_length;
  188. }
  189. EShSplitJobCommand(&jobs[*jobs_num - 1], ' ');
  190. return jobs;
  191. }
  192. void EShSplitJobCommand(EShJob* job, char delim) {
  193. int token_status = 0; // 0 - token result[result_size] finished
  194. // 1 - token result[result_size] not finished
  195. int current_token_length = 0;
  196. for (int i = 0; i < strlen(job->command); ++i) {
  197. while (job->command[i] == delim && i < strlen(job->command)) {
  198. if (token_status == 1) {
  199. token_status = 0;
  200. }
  201. ++i;
  202. }
  203. if (i == strlen(job->command)) {
  204. if (token_status == 1) {
  205. ++job->command_tokens_num;
  206. }
  207. break;
  208. }
  209. if (token_status == 0) {
  210. token_status = 1;
  211. ++job->command_tokens_num;
  212. current_token_length = 0;
  213. }
  214. job->command_tokens[job->command_tokens_num - 1][current_token_length] = job->command[i];
  215. ++current_token_length;
  216. }
  217. }
  218. void EShExecuteJobs(int jobs_num, EShJob* jobs_list) {
  219. for (int i = 0; i < jobs_num; ++i) {
  220. EShRunJob(&jobs_list[i]);
  221. }
  222. }
  223. void EShSetJobCommandType(EShJob* job) {
  224. }
  225. void EShRunJob(EShJob* job) {
  226. // TODO: PIPES INIT & BG PROCESS SUPPORT
  227. int pid = fork();
  228. if (pid == 0) {
  229. for (int i = job->command_tokens_num; i < esh_info_global->max_command_tokens; ++i) {
  230. free(job->command_tokens[i]);
  231. job->command_tokens[i] = NULL;
  232. }
  233. execvp(job->command_tokens[0], job->command_tokens);
  234. printf("Esh: run command %s failed.\n", job->command);
  235. exit(1); // execlp failed
  236. } else {
  237. waitpid(pid, NULL, 0);
  238. printf("\n");
  239. }
  240. }
  241. int EmptyCommand(const char* command) {
  242. }
  243. int InnerCommand(const char* command) {
  244. }
  245. void EShProcessInnerJob(const char* command) {
  246. }
  247. void EShProcessExecJob(const char* command) {
  248. }
  249. void EShPrintJobsDebugInfo(const char* command, EShJob* jobs, int jobs_num) {
  250. printf("\nD: Splitted command (%s) into %d jobs:\n", command, jobs_num);
  251. for (int i = 0; i < jobs_num; ++i) {
  252. printf("===================\n"
  253. "Job #%d:\n"
  254. "Cmd: %s\n"
  255. "Cmd tokens: %d ",
  256. i, jobs[i].command, jobs[i].command_tokens_num);
  257. for (int j = 0; j < jobs[i].command_tokens_num; ++j) {
  258. printf("{%s} ", jobs[i].command_tokens[j]);
  259. }
  260. printf("\n===================\n");
  261. }
  262. printf("\n");
  263. }