add ctrl-c and ctrl-d aka eof handling

This commit is contained in:
hugogogo
2021-11-10 13:46:17 +01:00
parent 0a33916c75
commit d65a701186
5 changed files with 26 additions and 116 deletions

View File

@@ -22,7 +22,7 @@ LIBFT_D = ./libft
LIBFT = $(LIBFT_D)/libft.a
SRCS = main.c init.c free.c generic.c \
signals.c terminal.c \
signals.c \
shell_loop.c shell_script.c \
lexing.c fill_token.c check_operators.c \
parsing.c \

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
/* Updated: 2021/11/02 14:03:51 by hulamy ### ########.fr */
/* Updated: 2021/11/10 13:41:58 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,11 +15,12 @@
// Init
int init(t_all *c, char *envp[]);
int set_signals_handling(struct sigaction *ori_signal_behaviour,
struct sigaction *signal_behaviour);
int set_terminal_attributes(struct termios *ori_termios,
struct termios *interactive_termios,
int *termios_changed);
//int set_signals_handling(struct sigaction *ori_signal_behaviour,
// struct sigaction *signal_behaviour);
int set_signals_handling(struct sigaction *signal_behaviour);
//int set_terminal_attributes(struct termios *ori_termios,
// struct termios *interactive_termios,
// int *termios_changed);
// Shell modes
void shell_loop(t_all *c);

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
/* Updated: 2021/11/02 13:50:33 by hulamy ### ########.fr */
/* Updated: 2021/11/10 13:43:59 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -28,13 +28,7 @@ int init(t_all *c, char *envp[])
c->prompt = init_prompt(c->prompt_base);
if (!c->prompt)
return (ft_reti_perror(0, "init_prompt() error"));
set_signals_handling(&c->ori_signal_behaviour, &c->signal_behaviour);
if (isatty(STDIN_FILENO))
{
if (!set_terminal_attributes(&c->ori_termios, &c->interactive_termios,
&c->termios_changed))
return (ft_reti_perror(0, "set_terminal_attributes() error"));
}
set_signals_handling(&c->signal_behaviour);
return (1);
}

View File

@@ -6,16 +6,12 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
/* Updated: 2021/11/02 14:15:04 by hulamy ### ########.fr */
/* Updated: 2021/11/10 13:43:10 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
//static char **tokens_list_to_argv(t_token *t); // temp test
void sigint_handler(int signum); //tmp
void sigquit_aka_eof_handler(int signum); //tmp
void close_fd(t_cmd *cmd);
void execute_cmd(char **envp, t_cmd **cmd_arr);
@@ -33,44 +29,15 @@ void shell_loop(t_all *c)
{
add_history(line_input);
c->token_list = input_to_tokens(line_input);
// TEMP
// A faire aprés être sortie du mode interactif
// - Ignorer tout les signaux
// - Remettre ori_termios
c->signal_behaviour.sa_handler = SIG_IGN;
sigaction(SIGINT, &c->signal_behaviour, NULL);
sigaction(SIGQUIT, &c->signal_behaviour, NULL);
tcsetattr(STDIN_FILENO, TCSANOW, &c->ori_termios);
if (!fork())
{
char *arg_test[3];
arg_test[0] = ft_strdup("sleep");
arg_test[1] = ft_strdup("3");
arg_test[2] = NULL;
sigaction(SIGQUIT, &c->ori_signal_behaviour, NULL);
sigaction(SIGINT, &c->ori_signal_behaviour, NULL);
execve("/bin/sleep", arg_test, c->envp);
}
else
{
int wait_test;
wait(&wait_test);
c->signal_behaviour.sa_handler = sigint_handler;
sigaction(SIGINT, &c->signal_behaviour, NULL);
c->signal_behaviour.sa_handler = SIG_IGN;
sigaction(SIGQUIT, &c->signal_behaviour, NULL);
tcsetattr(STDIN_FILENO, TCSANOW, &c->interactive_termios);
}
// EXEC
c->cmd_arr = parsing(c->token_list, c->envp);
execute_cmd(c->envp, c->cmd_arr);
ft_lstclear((t_list **)&c->token_list, free);
}
else if (!line_input)
write(1, "\n", 1);
{
write(1, "exit\n", 5);
exit(0);
}
}
}

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/23 18:56:53 by lperrey #+# #+# */
/* Updated: 2021/10/30 14:28:08 by lperrey ### ########.fr */
/* Updated: 2021/11/10 13:44:09 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,71 +14,19 @@
void sigint_handler(int signum)
{
// Comment virer LE ^D De l'affichage ? Il ne fait pas partie de "rl_line_buffer".
if (rl_line_buffer && *rl_line_buffer)
{
return ;
}
else
{
free_exit(g_all, g_all->last_exit_status);
}
return ;
}
/* void sigquit_aka_eof_handler(int signum)
{
//ft_putstr_fd("TESTS\n", 1);
ft_putstr_fd("\n", 1);
(void)signum;
write(1, "\n", 1);
rl_replace_line("", 1);
rl_on_new_line();
rl_redisplay();
return ;
} */
int set_signals_handling(struct sigaction *ori_signal_behaviour,
struct sigaction *signal_behaviour)
{
ori_signal_behaviour->sa_handler = SIG_DFL;
/* ctrl-D exit the shell.
eof = ^D; */
signal_behaviour->sa_handler = sigint_handler;
sigaction(SIGINT, signal_behaviour, NULL);
/* ctrl-\ do nothing.
quit = ^\; */
signal_behaviour->sa_handler = SIG_IGN;
//signal_behaviour->sa_handler = sigquit_aka_eof_handler;
sigaction(SIGQUIT, signal_behaviour, NULL);
/*
** remap (^D to ^C) and (^C to ^D) in terminal
** ^D is now "SIGINT" (handle here)
** ^C is now EOF (handle in shell_loop())
*/
return (1);
}
/*
ctrl-C print a new prompt on a newline.
intr = ^C;
ctrl-D exit the shell.
eof = ^D;
ctrl-\ do nothing.
quit = ^\;
*/
/*
speed 38400 baud; rows 22; columns 90; line = 0;
erase = ^?; kill = ^U;
eol = M-^?; eol2 = M-^?;
swtch = M-^?; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V;
discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 hupcl -cstopb cread -clocal -crtscts
-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc ixany
imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
-flusho -extproc
*/
int set_signals_handling(struct sigaction *signal_behaviour)
{
signal_behaviour->sa_handler = sigint_handler;
sigaction(SIGINT, signal_behaviour, NULL);
signal_behaviour->sa_handler = SIG_IGN;
sigaction(SIGQUIT, signal_behaviour, NULL);
return (1);
}