esh_main_loop.c 12 KB

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