diff --git a/src/exec.c b/src/exec.c index 432a2b0..c23df71 100644 --- a/src/exec.c +++ b/src/exec.c @@ -21,6 +21,7 @@ #include "exec.h" #include "globbing.h" #include "path.h" +#include "repl.h" #include "u_mem.h" #include "u_str.h" @@ -143,12 +144,12 @@ int launch_bin(char *full_bin_path, char **args, ef_t *ef) pid_t pid = fork(); if (pid == 0) { + restore_term_flags(ef->exec_ctx); set_fd(ef); if (execve(full_bin_path, args, ef->env->env) < 0) { status = command_error(full_bin_path, args, errno); free_env(ef->env); - free((void *)args); - exit((free(ef->buffer), status)); + exit(((free((void *)args), free(ef->buffer)), status)); } } if (!(ef->flags & F_PIPE) || ef->p_i == ef->p_sz - 1) @@ -225,6 +226,7 @@ int execute(ef_t *ef) return RETURN_FAILURE; exec_the_args(ef, args); free((void *)args); + init_shell_repl(ef->exec_ctx); return ef->exec_ctx->history->last_exit_code != 0 ? RETURN_FAILURE : RETURN_SUCCESS; } diff --git a/src/readline.c b/src/readline.c index 2a03294..9ebef6d 100644 --- a/src/readline.c +++ b/src/readline.c @@ -16,6 +16,7 @@ #include "common.h" #include "readline.h" +#include "repl.h" #include "u_str.h" static @@ -80,28 +81,6 @@ bool append_null_terminator(buff_t *buff) return true; } -static -void ignore_sigint(void) -{ - WRITE_CONST(STDIN_FILENO, "\n"); - WRITE_CONST(STDOUT_FILENO, SHELL_PROMPT); -} - -static -bool handle_keys(buff_t *buff, char *read_buff) -{ - switch (*read_buff) { - case CTRL('d'): - buff->sz = 0; - return true; - case CTRL('c'): - ignore_sigint(); - return false; - default: - return false; - } -} - static int8_t handle_line_buff(buff_t *buff, char *read_buff, ssize_t read_size) { diff --git a/src/repl.c b/src/repl.c new file mode 100644 index 0000000..217c76f --- /dev/null +++ b/src/repl.c @@ -0,0 +1,54 @@ +/* +** EPITECH PROJECT, 2025 +** __ +** File description: +** _ +*/ + +#include +#include +#include + +#include "exec.h" +#include "u_str.h" + +void init_shell_repl(exec_ctx_t *exec_ctx) +{ + struct termios repl_settings; + + signal(SIGINT, SIG_IGN); + exec_ctx->is_running = true; + if (isatty(STDIN_FILENO)) { + tcgetattr(STDIN_FILENO, &repl_settings); + exec_ctx->saved_term_settings = repl_settings; + repl_settings.c_iflag = IXON; + repl_settings.c_lflag = ~(ECHO | ICANON); + tcsetattr(STDIN_FILENO, TCSANOW, &repl_settings); + } +} + +void restore_term_flags(exec_ctx_t *exec_ctx) +{ + tcsetattr(STDIN_FILENO, TCSANOW, &exec_ctx->saved_term_settings); +} + +static +void ignore_sigint(void) +{ + WRITE_CONST(STDIN_FILENO, "\n"); + WRITE_CONST(STDOUT_FILENO, SHELL_PROMPT); +} + +bool handle_keys(buff_t *buff, char *read_buff) +{ + switch (*read_buff) { + case CTRL('d'): + buff->sz = 0; + return true; + case CTRL('c'): + ignore_sigint(); + return false; + default: + return false; + } +} diff --git a/src/repl.h b/src/repl.h new file mode 100644 index 0000000..25f4868 --- /dev/null +++ b/src/repl.h @@ -0,0 +1,16 @@ +/* +** EPITECH PROJECT, 2025 +** __ +** File description: +** _ +*/ + +#ifndef REPL_H + #define REPL_H + #include "exec.h" + #include "u_str.h" + +void init_shell_repl(exec_ctx_t *exec_ctx); +void restore_term_flags(exec_ctx_t *exec_ctx); +bool handle_keys(buff_t *buff, char *read_buff); +#endif /* REPL_H */ diff --git a/src/shell.c b/src/shell.c index 8e148b3..92cb6a3 100644 --- a/src/shell.c +++ b/src/shell.c @@ -5,7 +5,6 @@ ** _ */ -#include #include #include #include @@ -18,6 +17,7 @@ #include "history.h" #include "local.h" #include "readline.h" +#include "repl.h" #include "shell.h" #include "u_str.h" #include "visitor.h" @@ -75,21 +75,6 @@ bool change_shell_command(buff_t *buff, exec_ctx_t *exec_ctx) return true; } -static -void init_shell_repl(exec_ctx_t *exec_ctx) -{ - struct termios repl_settings; - - exec_ctx->is_running = true; - if (isatty(STDIN_FILENO)) { - tcgetattr(STDIN_FILENO, &repl_settings); - exec_ctx->saved_term_settings = repl_settings; - repl_settings.c_iflag = IXON; - repl_settings.c_lflag = ~(ECHO | ICANON); - tcsetattr(STDIN_FILENO, TCSANOW, &repl_settings); - } -} - static int shell_loop(int is_a_tty, exec_ctx_t *exec_ctx) { @@ -136,6 +121,7 @@ bool error_in_init(exec_ctx_t *exec_ctx) return false; } +#include int shell(char **env_ptr) { alias_t alias = init_alias(); @@ -154,7 +140,7 @@ int shell(char **env_ptr) shell_result = shell_loop(isatty(STDIN_FILENO), &exec_ctx); if (isatty(STDIN_FILENO)) { WRITE_CONST(STDOUT_FILENO, "exit\n"); - tcsetattr(STDIN_FILENO, TCSANOW, &exec_ctx.saved_term_settings); + restore_term_flags(&exec_ctx); } return free_everything(&exec_ctx), shell_result; }