Enable ICANNON after exec

This commit is contained in:
savalet
2025-04-29 16:41:41 +02:00
parent 3bfb463a61
commit 0163e0a58f
5 changed files with 78 additions and 41 deletions

View File

@@ -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;
}

View File

@@ -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)
{

54
src/repl.c Normal file
View File

@@ -0,0 +1,54 @@
/*
** EPITECH PROJECT, 2025
** __
** File description:
** _
*/
#include <signal.h>
#include <termios.h>
#include <unistd.h>
#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;
}
}

16
src/repl.h Normal file
View File

@@ -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 */

View File

@@ -5,7 +5,6 @@
** _
*/
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -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 <signal.h>
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;
}