esh_main_loop.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. #include "esh_main_loop.h"
  2. #include "esh_misc.h"
  3. #include "esh_history.h"
  4. #include "esh_init.h"
  5. #include "esh_deinit.h"
  6. #include "string_misc.h"
  7. #include <signal.h>
  8. struct termios orig_term_attr;
  9. struct termios new_term_attr;
  10. volatile sig_atomic_t main_loop_is_running;
  11. void EShRestoreOriginalTerminalConfig() {
  12. /* restore the original terminal attributes */
  13. tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr);
  14. }
  15. void EShRunLoopExitSignalHandler(int signum) {
  16. EShRestoreOriginalTerminalConfig();
  17. main_loop_is_running = 0;
  18. }
  19. void EShRunLoop() {
  20. main_loop_is_running = 1;
  21. /* registering exit signals */
  22. signal(SIGINT, EShRunLoopExitSignalHandler);
  23. signal(SIGQUIT, EShRunLoopExitSignalHandler);
  24. signal(SIGILL, EShRunLoopExitSignalHandler);
  25. signal(SIGABRT, EShRunLoopExitSignalHandler);
  26. signal(SIGKILL, EShRunLoopExitSignalHandler);
  27. /* set the terminal to semi-raw mode */
  28. tcgetattr(fileno(stdin), &orig_term_attr);
  29. memcpy(&new_term_attr, &orig_term_attr, sizeof(struct termios));
  30. new_term_attr.c_lflag &= ~(ECHO | ICANON | ISIG | IEXTEN);
  31. new_term_attr.c_cc[VTIME] = 0;
  32. new_term_attr.c_cc[VMIN] = 1;
  33. tcsetattr(fileno(stdin), TCSANOW, &new_term_attr);
  34. char* command = malloc(esh_info_global->max_command_length);
  35. char* uncomplete_command = malloc(esh_info_global->max_command_length); // Used to restore after moves history
  36. while (main_loop_is_running) {
  37. EShShowMsg();
  38. memset(command, 0, esh_info_global->max_command_length);
  39. int command_length = 0;
  40. int current_command_pos = 0;
  41. int current_history_step = 0;
  42. char input_char;
  43. memset(uncomplete_command, 0, esh_info_global->max_command_length);
  44. while (input_char = fgetc(stdin)) {
  45. if (input_char == 10) {
  46. printf("\n");
  47. break; // 'Enter' pressed;
  48. }
  49. if (input_char == EOF) {
  50. continue;
  51. }
  52. if (input_char == '\033') { // Arrow up/down/left/right sequence
  53. fgetc(stdin);
  54. switch(fgetc(stdin)) {
  55. case 'A':
  56. {
  57. if (current_history_step == 0) {
  58. strcpy(uncomplete_command, command);
  59. }
  60. const char* new_command = EShReceiveCommandFromHistory(current_history_step);
  61. if (new_command != NULL) {
  62. ++current_history_step;
  63. for (int i = current_command_pos; i < command_length; ++i) {
  64. printf(" ");
  65. ++current_command_pos;
  66. }
  67. for (int i = current_command_pos; i > 0; --i) {
  68. printf("\b \b");
  69. }
  70. strcpy(command, new_command);
  71. command_length = strlen(new_command);
  72. current_command_pos = command_length;
  73. for (int i = 0; i < command_length; ++i) {
  74. printf("%c", command[i]);
  75. }
  76. }
  77. break;
  78. }
  79. case 'B': // Down
  80. {
  81. const char* new_command;
  82. if (current_history_step > 0) {
  83. --current_history_step;
  84. }
  85. if (current_history_step > 0) {
  86. new_command = EShReceiveCommandFromHistory(current_history_step - 1);
  87. } else {
  88. new_command = uncomplete_command;
  89. }
  90. for (int i = current_command_pos; i < command_length; ++i) {
  91. printf(" ");
  92. ++current_command_pos;
  93. }
  94. for (int i = current_command_pos; i > 0; --i) {
  95. printf("\b \b");
  96. }
  97. strcpy(command, new_command);
  98. command_length = strlen(new_command);
  99. current_command_pos = command_length;
  100. for (int i = 0; i < command_length; ++i) {
  101. printf("%c", command[i]);
  102. }
  103. break;
  104. }
  105. case 'C': // Right
  106. if (current_command_pos < command_length) {
  107. printf("%c", command[current_command_pos]);
  108. ++current_command_pos;
  109. }
  110. break;
  111. case 'D': // Left
  112. if (current_command_pos > 0) {
  113. printf("\b");
  114. --current_command_pos;
  115. }
  116. break;
  117. }
  118. continue;
  119. }
  120. if (input_char == 4) {
  121. // Ctrl + d
  122. tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr);
  123. return;
  124. }
  125. if (input_char == 127) {
  126. // Backspace
  127. if (current_command_pos > 0) {
  128. for (int i = current_command_pos - 1; i < command_length; ++i) {
  129. command[i] = command[i + 1];
  130. }
  131. command[command_length - 1] = '\0';
  132. printf("\b");
  133. printf("%s", command + current_command_pos - 1);
  134. printf(" \b");
  135. --current_command_pos;
  136. --command_length;
  137. for (int i = command_length - 1; i >= current_command_pos; --i) {
  138. printf("\b");
  139. }
  140. }
  141. continue;
  142. }
  143. if (input_char == 3) {
  144. // Ctrl + c
  145. tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr);
  146. printf("\n");
  147. return;
  148. }
  149. if (EShIsShellLetter(input_char)) {
  150. for (int i = command_length; i >= current_command_pos; --i) {
  151. command[i + 1] = command[i];
  152. }
  153. command[current_command_pos] = input_char;
  154. printf("%s", command + current_command_pos);
  155. ++current_command_pos;
  156. ++command_length;
  157. for (int i = command_length; i > current_command_pos; --i) {
  158. printf("\b");
  159. }
  160. continue;
  161. }
  162. }
  163. if (command_length == 0) {
  164. printf("\n");
  165. continue;
  166. }
  167. EShAddCommandToHistory(command);
  168. int jobs_num = 0;
  169. EShJob* jobs = EShParseCommandIntoJobs(command, &jobs_num);
  170. // EShPrintJobsDebugInfo(command, jobs, jobs_num);
  171. EShExecuteJobs(jobs_num, jobs);
  172. EShFreeJobsList(jobs);
  173. }
  174. free(command);
  175. free(uncomplete_command);
  176. EShRestoreOriginalTerminalConfig();
  177. }
  178. void EShShowMsg() {
  179. EShUpdateInviteMessage();
  180. printf("%s", esh_info_global->invite_message);
  181. }
  182. EShJob* EShParseCommandIntoJobs(char* command, int* jobs_num) {
  183. EShJob* jobs = EShMakeJobsList();
  184. *jobs_num = 1;
  185. int current_job_cmd_length = 0;
  186. for (int i = 0; i < strlen(command); ++i) {
  187. ESH_JOB_DELIMITER delimiter = EshIsJobDelimiter(command + i);
  188. if (delimiter != NOT_A_DELIMITER) {
  189. EShSplitJobCommand(&jobs[*jobs_num - 1], ' ');
  190. if (delimiter == SEMICOLON) {
  191. jobs[*jobs_num].job_start_condition = NO_CONDITION;
  192. }
  193. if (delimiter == LOGIC_AND) {
  194. jobs[*jobs_num].job_start_condition = PREVIOUS_EXIT_SUCCESS;
  195. }
  196. if (delimiter == LOGIC_OR) {
  197. jobs[*jobs_num].job_start_condition = PREVIOUS_EXIT_FAILED;
  198. }
  199. if (delimiter == BIT_AND) {
  200. printf(ANSI_COLOR_RED "\nESh note: running processes in background is still not available, selected job will be running in foreground :(\n" ANSI_COLOR_RESET);
  201. fflush(stdout);
  202. }
  203. if (delimiter == BIT_OR) {
  204. int pipes[2];
  205. if (pipe(pipes) != 0) {
  206. printf(ANSI_COLOR_RED "\nESh error: cannot spawn pipe: %s\n" ANSI_COLOR_RESET, strerror(errno));
  207. fflush(stdout);
  208. }
  209. jobs[*jobs_num - 1].stdout_fd = pipes[1];
  210. jobs[*jobs_num].stdin_fd = pipes[0];
  211. }
  212. ++(*jobs_num);
  213. current_job_cmd_length = 0;
  214. i += EShGetJobDelimiterSize(EshIsJobDelimiter(command + i)) - 1;
  215. continue;
  216. }
  217. jobs[*jobs_num - 1].command[current_job_cmd_length] = command[i];
  218. ++current_job_cmd_length;
  219. }
  220. EShSplitJobCommand(&jobs[*jobs_num - 1], ' ');
  221. return jobs;
  222. }
  223. void EShSplitJobCommand(EShJob* job, char delim) {
  224. int token_status = 0; // 0 - token result[result_size] finished
  225. // 1 - token result[result_size] not finished
  226. int current_token_length = 0;
  227. for (int i = 0; i < strlen(job->command); ++i) {
  228. while (job->command[i] == delim && i < strlen(job->command)) {
  229. if (token_status == 1) {
  230. token_status = 0;
  231. }
  232. ++i;
  233. }
  234. if (i == strlen(job->command)) {
  235. if (token_status == 1) {
  236. ++job->command_tokens_num;
  237. }
  238. break;
  239. }
  240. if (token_status == 0) {
  241. token_status = 1;
  242. ++job->command_tokens_num;
  243. current_token_length = 0;
  244. }
  245. job->command_tokens[job->command_tokens_num - 1][current_token_length] = job->command[i];
  246. ++current_token_length;
  247. }
  248. for (int i = 0; i < job->command_tokens_num; ++i) {
  249. EShSubstituteShellValuesArgument(&job->command_tokens[i]);
  250. }
  251. }
  252. void EShExecuteJobs(int jobs_num, EShJob* jobs_list) {
  253. int prev_job_exit_code = 0;
  254. for (int i = 0; i < jobs_num; ++i) {
  255. if (strlen(jobs_list[i].command) == 0)
  256. continue;
  257. if (jobs_list[i].job_start_condition == NO_CONDITION ||
  258. (jobs_list[i].job_start_condition == PREVIOUS_EXIT_SUCCESS && prev_job_exit_code == 0) ||
  259. (jobs_list[i].job_start_condition == PREVIOUS_EXIT_FAILED && prev_job_exit_code != 0))
  260. {
  261. EShRunJob(&jobs_list[i]);
  262. }
  263. }
  264. }
  265. void EShSetJobCommandType(EShJob* job) {
  266. }
  267. int EShRunJob(EShJob* job) {
  268. if (EShIsInnerJob(job)) {
  269. return EShProcessInnerJob(job);
  270. } else {
  271. return EShProcessExecJob(job);
  272. }
  273. // TODO: PIPES INIT & BG PROCESS SUPPORT
  274. }
  275. int EShIsInnerJob(EShJob* job) {
  276. return strcmp(job->command_tokens[0], "cd") == 0 ||
  277. strcmp(job->command_tokens[0], "exit") == 0;
  278. // TODO: jobs, bg, fg (!)
  279. }
  280. int EShProcessInnerJob(EShJob* job) {
  281. if (strcmp(job->command_tokens[0], "cd") == 0) {
  282. if (chdir(job->command_tokens[1]) == -1) {
  283. printf(ANSI_COLOR_RED "Esh: cannot change directory: %s\n" ANSI_COLOR_RESET, strerror(errno));
  284. fflush(stdout);
  285. return -1;
  286. }
  287. getcwd(esh_info_global->current_working_dir, PATH_MAX);
  288. EShOptimiseCurrentWorkingDirectory();
  289. EShUpdateInviteMessage();
  290. return 0;
  291. }
  292. if (strcmp(job->command_tokens[0], "exit") == 0) {
  293. EShExit(0);
  294. return 0; // Actually, no need in it.
  295. }
  296. }
  297. int EShProcessExecJob(EShJob* job) {
  298. int pid = fork();
  299. if (pid == 0) {
  300. if (job->stdin_fd != -1) {
  301. dup2(job->stdin_fd, STDIN_FILENO);
  302. close(job->stdin_fd);
  303. job->stdin_fd = -1;
  304. }
  305. if (job->stdout_fd != -1) {
  306. dup2(job->stdout_fd, STDOUT_FILENO);
  307. close(job->stdout_fd);
  308. job->stdout_fd = -1;
  309. }
  310. if (job->stderr_fd != -1) {
  311. dup2(job->stderr_fd, STDERR_FILENO);
  312. close(job->stderr_fd);
  313. job->stderr_fd = -1;
  314. }
  315. for (int i = job->command_tokens_num; i < esh_info_global->max_command_tokens; ++i) {
  316. free(job->command_tokens[i]);
  317. job->command_tokens[i] = NULL;
  318. }
  319. execvp(job->command_tokens[0], job->command_tokens);
  320. printf(ANSI_COLOR_RED "Esh: run command %s failed.\n" ANSI_COLOR_RESET, job->command);
  321. fflush(stdout);
  322. exit(1); // execlp failed
  323. } else {
  324. if (job->stdin_fd != -1) {
  325. close(job->stdin_fd);
  326. job->stdin_fd = -1;
  327. }
  328. if (job->stdout_fd != -1) {
  329. close(job->stdout_fd);
  330. job->stdout_fd = -1;
  331. }
  332. if (job->stderr_fd != -1) {
  333. close(job->stderr_fd);
  334. job->stderr_fd = -1;
  335. }
  336. job->job_pid = pid;
  337. int status;
  338. waitpid(pid, &status, 0);
  339. if (WIFEXITED(status)) {
  340. return WEXITSTATUS(status);
  341. } else {
  342. return -1; // No exit status received;
  343. }
  344. }
  345. }
  346. void EShSubstituteShellValuesArgument(char** arg) {
  347. char* new_str = *arg;
  348. // Processing tilda
  349. new_str = repl_str(new_str, "~", esh_info_global->user_data->pw_dir);
  350. // Processing system variables
  351. char* envvar_begin = new_str - 1;
  352. while ((envvar_begin = strchr(envvar_begin + 1, '%')) != NULL) {
  353. char* envvar_end = strchr(envvar_begin + 1, '%');
  354. if (envvar_end == NULL)
  355. break;
  356. char envvar_name[1024 * 1024];
  357. memset(envvar_name, 0, 1024 * 1024);
  358. memcpy(envvar_name, envvar_begin + 1, (envvar_end - envvar_begin - 1));
  359. char* envvar_value = getenv(envvar_name);
  360. if (envvar_value == NULL) {
  361. continue;
  362. }
  363. char envvar_name_overed[1024 * 1024];
  364. memset(envvar_name_overed, 0, 1024 * 1024);
  365. strcat(envvar_name_overed, "%");
  366. strcat(envvar_name_overed, envvar_name);
  367. strcat(envvar_name_overed, "%");
  368. new_str = repl_str(new_str, envvar_name_overed, envvar_value);
  369. }
  370. free(*arg);
  371. *arg = new_str;
  372. }
  373. void EShPrintJobsDebugInfo(const char* command, EShJob* jobs, int jobs_num) {
  374. printf("\nD: Splitted command (%s) into %d jobs:\n", command, jobs_num);
  375. for (int i = 0; i < jobs_num; ++i) {
  376. printf("===================\n"
  377. "Job #%d:\n"
  378. "Cmd: %s\n"
  379. "Cmd tokens: %d ",
  380. i, jobs[i].command, jobs[i].command_tokens_num);
  381. for (int j = 0; j < jobs[i].command_tokens_num; ++j) {
  382. printf("{%s} ", jobs[i].command_tokens[j]);
  383. }
  384. printf("\n===================\n");
  385. }
  386. printf("\n");
  387. }