From d65a7011868c72a764e4d96526f9c9620a77d84f Mon Sep 17 00:00:00 2001 From: hugogogo Date: Wed, 10 Nov 2021 13:46:17 +0100 Subject: [PATCH] add ctrl-c and ctrl-d aka eof handling --- Makefile | 2 +- headers/minishell_prototypes.h | 13 +++--- srcs/init.c | 10 +---- srcs/shell_loop.c | 43 +++----------------- srcs/signals.c | 74 +++++----------------------------- 5 files changed, 26 insertions(+), 116 deletions(-) diff --git a/Makefile b/Makefile index 0f7d936..696f6f0 100644 --- a/Makefile +++ b/Makefile @@ -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 \ diff --git a/headers/minishell_prototypes.h b/headers/minishell_prototypes.h index d1724d2..3e9f08d 100644 --- a/headers/minishell_prototypes.h +++ b/headers/minishell_prototypes.h @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); diff --git a/srcs/init.c b/srcs/init.c index f6e6484..814ec56 100644 --- a/srcs/init.c +++ b/srcs/init.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } diff --git a/srcs/shell_loop.c b/srcs/shell_loop.c index 878be8a..11bc76b 100644 --- a/srcs/shell_loop.c +++ b/srcs/shell_loop.c @@ -6,16 +6,12 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); + } } } diff --git a/srcs/signals.c b/srcs/signals.c index f715783..e076bd5 100644 --- a/srcs/signals.c +++ b/srcs/signals.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); +}