add ctrl-c and ctrl-d aka eof handling
This commit is contained in:
2
Makefile
2
Makefile
@@ -22,7 +22,7 @@ LIBFT_D = ./libft
|
|||||||
LIBFT = $(LIBFT_D)/libft.a
|
LIBFT = $(LIBFT_D)/libft.a
|
||||||
|
|
||||||
SRCS = main.c init.c free.c generic.c \
|
SRCS = main.c init.c free.c generic.c \
|
||||||
signals.c terminal.c \
|
signals.c \
|
||||||
shell_loop.c shell_script.c \
|
shell_loop.c shell_script.c \
|
||||||
lexing.c fill_token.c check_operators.c \
|
lexing.c fill_token.c check_operators.c \
|
||||||
parsing.c \
|
parsing.c \
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/08 02:59:58 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
|
// Init
|
||||||
int init(t_all *c, char *envp[]);
|
int init(t_all *c, char *envp[]);
|
||||||
int set_signals_handling(struct sigaction *ori_signal_behaviour,
|
//int set_signals_handling(struct sigaction *ori_signal_behaviour,
|
||||||
struct sigaction *signal_behaviour);
|
// struct sigaction *signal_behaviour);
|
||||||
int set_terminal_attributes(struct termios *ori_termios,
|
int set_signals_handling(struct sigaction *signal_behaviour);
|
||||||
struct termios *interactive_termios,
|
//int set_terminal_attributes(struct termios *ori_termios,
|
||||||
int *termios_changed);
|
// struct termios *interactive_termios,
|
||||||
|
// int *termios_changed);
|
||||||
|
|
||||||
// Shell modes
|
// Shell modes
|
||||||
void shell_loop(t_all *c);
|
void shell_loop(t_all *c);
|
||||||
|
|||||||
10
srcs/init.c
10
srcs/init.c
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/08 09:22:12 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);
|
c->prompt = init_prompt(c->prompt_base);
|
||||||
if (!c->prompt)
|
if (!c->prompt)
|
||||||
return (ft_reti_perror(0, "init_prompt() error"));
|
return (ft_reti_perror(0, "init_prompt() error"));
|
||||||
set_signals_handling(&c->ori_signal_behaviour, &c->signal_behaviour);
|
set_signals_handling(&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"));
|
|
||||||
}
|
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,16 +6,12 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/04 05:59:26 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"
|
#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 close_fd(t_cmd *cmd);
|
||||||
void execute_cmd(char **envp, t_cmd **cmd_arr);
|
void execute_cmd(char **envp, t_cmd **cmd_arr);
|
||||||
|
|
||||||
@@ -33,44 +29,15 @@ void shell_loop(t_all *c)
|
|||||||
{
|
{
|
||||||
add_history(line_input);
|
add_history(line_input);
|
||||||
c->token_list = input_to_tokens(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);
|
c->cmd_arr = parsing(c->token_list, c->envp);
|
||||||
execute_cmd(c->envp, c->cmd_arr);
|
execute_cmd(c->envp, c->cmd_arr);
|
||||||
ft_lstclear((t_list **)&c->token_list, free);
|
ft_lstclear((t_list **)&c->token_list, free);
|
||||||
}
|
}
|
||||||
else if (!line_input)
|
else if (!line_input)
|
||||||
write(1, "\n", 1);
|
{
|
||||||
|
write(1, "exit\n", 5);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/23 18:56:53 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)
|
void sigint_handler(int signum)
|
||||||
{
|
{
|
||||||
// Comment virer LE ^D De l'affichage ? Il ne fait pas partie de "rl_line_buffer".
|
(void)signum;
|
||||||
if (rl_line_buffer && *rl_line_buffer)
|
write(1, "\n", 1);
|
||||||
{
|
|
||||||
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);
|
|
||||||
rl_replace_line("", 1);
|
rl_replace_line("", 1);
|
||||||
rl_on_new_line();
|
rl_on_new_line();
|
||||||
rl_redisplay();
|
rl_redisplay();
|
||||||
return ;
|
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
int set_signals_handling(struct sigaction *signal_behaviour)
|
||||||
ctrl-C print a new prompt on a newline.
|
{
|
||||||
intr = ^C;
|
signal_behaviour->sa_handler = sigint_handler;
|
||||||
ctrl-D exit the shell.
|
sigaction(SIGINT, signal_behaviour, NULL);
|
||||||
eof = ^D;
|
signal_behaviour->sa_handler = SIG_IGN;
|
||||||
ctrl-\ do nothing.
|
sigaction(SIGQUIT, signal_behaviour, NULL);
|
||||||
quit = ^\;
|
return (1);
|
||||||
*/
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
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
|
|
||||||
*/
|
|
||||||
|
|||||||
Reference in New Issue
Block a user