123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- #include "esh_main_loop.h"
- #include "esh_misc.h"
- #include "esh_history.h"
- void EShRunLoop() {
- struct termios orig_term_attr;
- struct termios new_term_attr;
- /* set the terminal to raw mode */
- tcgetattr(fileno(stdin), &orig_term_attr);
- memcpy(&new_term_attr, &orig_term_attr, sizeof(struct termios));
- new_term_attr.c_lflag &= ~(ECHO | ICANON | ISIG | IEXTEN);
- new_term_attr.c_cc[VTIME] = 0;
- new_term_attr.c_cc[VMIN] = 1;
- tcsetattr(fileno(stdin), TCSANOW, &new_term_attr);
- char* command = malloc(esh_info_global->max_command_length);
- char* uncomplete_command = malloc(esh_info_global->max_command_length); // Used to restore after moves history
- for (;;) {
- EShShowMsg();
- memset(command, 0, esh_info_global->max_command_length);
- int command_length = 0;
- int current_command_pos = 0;
- int current_history_step = 0;
- char input_char;
- memset(uncomplete_command, 0, esh_info_global->max_command_length);
- while (input_char = fgetc(stdin)) {
- if (input_char == 10) {
- printf("\n");
- break; // 'Enter' pressed;
- }
- if (input_char == EOF) {
- continue;
- }
- if (input_char == '\033') { // Arrow up/down/left/right sequence
- fgetc(stdin);
- switch(fgetc(stdin)) {
- case 'A':
- {
- if (current_history_step == 0) {
- strcpy(uncomplete_command, command);
- }
- char* new_command = EShReceiveCommandFromHistory(current_history_step);
- if (new_command != NULL) {
- ++current_history_step;
- for (int i = current_command_pos; i < command_length; ++i) {
- printf(" ");
- ++current_command_pos;
- }
- for (int i = current_command_pos; i > 0; --i) {
- printf("\b \b");
- }
- strcpy(command, new_command);
- command_length = strlen(new_command);
- current_command_pos = command_length;
- for (int i = 0; i < command_length; ++i) {
- printf("%c", command[i]);
- }
- }
- free(new_command);
- break;
- }
- case 'B': // Down
- {
- char* new_command;
- if (current_history_step > 0) {
- --current_history_step;
- }
- if (current_history_step > 0) {
- new_command = EShReceiveCommandFromHistory(current_history_step - 1);
- } else {
- new_command = uncomplete_command;
- }
- for (int i = current_command_pos; i < command_length; ++i) {
- printf(" ");
- ++current_command_pos;
- }
- for (int i = current_command_pos; i > 0; --i) {
- printf("\b \b");
- }
- strcpy(command, new_command);
- command_length = strlen(new_command);
- current_command_pos = command_length;
- for (int i = 0; i < command_length; ++i) {
- printf("%c", command[i]);
- }
- if (current_history_step > 0) {
- free(new_command);
- }
- break;
- }
- case 'C': // Right
- if (current_command_pos < command_length) {
- printf("%c", command[current_command_pos]);
- ++current_command_pos;
- }
- break;
- case 'D': // Left
- if (current_command_pos > 0) {
- printf("\b");
- --current_command_pos;
- }
- break;
- }
- continue;
- }
- if (input_char == 4) {
- // Ctrl + d
- printf("\n");
- exit(0);
- }
- if (input_char == 127) {
- // Backspace
- if (current_command_pos > 0) {
- for (int i = current_command_pos - 1; i < command_length; ++i) {
- command[i] = command[i + 1];
- }
- command[command_length - 1] = '\0';
- printf("\b");
- printf("%s", command + current_command_pos - 1);
- printf(" \b");
- --current_command_pos;
- --command_length;
- for (int i = command_length - 1; i >= current_command_pos; --i) {
- printf("\b");
- }
- }
- continue;
- }
- if (input_char == 3) {
- // Ctrl + c
- printf("\n");
- exit(0);
- }
- if (EShIsShellLetter(input_char)) {
- for (int i = command_length; i >= current_command_pos; --i) {
- command[i + 1] = command[i];
- }
- command[current_command_pos] = input_char;
- printf("%s", command + current_command_pos);
- ++current_command_pos;
- ++command_length;
- for (int i = command_length; i > current_command_pos; --i) {
- printf("\b");
- }
- continue;
- }
- }
- if (command_length == 0) {
- printf("\n");
- continue;
- }
- EShAddCommandToHistory(command);
-
- EShJob* jobs;
- int jobs_num;
- EShParseCommandIntoJobs(command, &jobs, &jobs_num);
- EShExecuteJobs(jobs_num, jobs);
- }
- /* restore the original terminal attributes */
- tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr);
- }
- void EShShowMsg() {
- EShUpdateInviteMessage();
- printf("%s", esh_info_global->invite_message);
- }
- void EShParseCommandIntoJobs(char* command, EShJob** jobs_ptr, int* jobs_num_ptr) {
- }
- void EShExecuteJobs(int jobs_num, EShJob* jobs_list) {
- for (int i = 0; i < jobs_num; ++i) {
- EShRunJob(jobs_list[i]);
- }
- }
- void EShSetJobCommandType(EShJob* job) {
- }
- void EShRunJob(EShJob* job) {
- // TODO: PIPES INIT & BG PROCESS SUPPORT
- int pid = fork();
- if (pid == 0) {
- execvp(job->command[0], job->command);
- printf("Esh: run command %s failed.\n", job->command);
- exit(1); // execlp failed
- } else {
- waitpid(pid, NULL, 0);
- printf("\n");
- }
- }
- int EmptyCommand(const char* command) {
- }
- int InnerCommand(const char* command) {
- }
- void EShProcessInnerJob(const char* command) {
- }
- void EShProcessExecJob(const char* command) {
-
- }
|