From 0390007bfbb82cde2b1b8da2ce82c79c09ed1127 Mon Sep 17 00:00:00 2001 From: narnaud Date: Fri, 6 May 2022 12:15:35 +0200 Subject: [PATCH] Some functions refactoring and use handle_status on last cmd only --- caller.c | 5 +++-- minishell.c | 65 ++++++++++++++++++++++++++++++++++++++--------------- minishell.h | 6 ++--- utils.c | 28 +---------------------- 4 files changed, 53 insertions(+), 51 deletions(-) diff --git a/caller.c b/caller.c index da44324..b6c1554 100755 --- a/caller.c +++ b/caller.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */ -/* Updated: 2022/05/06 11:22:52 by narnaud ### ########.fr */ +/* Updated: 2022/05/06 12:14:27 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -107,7 +107,8 @@ int piper(t_datas *datas, t_command *cmd) if (cmd->next) piper(datas, cmd->next); waitpid(pid, &status, 0); - handle_status(datas, status); + if (!cmd->next) + handle_status(datas, status); if (cmd->fd[0]) close(cmd->fd[0]); return (1); diff --git a/minishell.c b/minishell.c index 6625f79..d6557d2 100755 --- a/minishell.c +++ b/minishell.c @@ -6,16 +6,59 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */ -/* Updated: 2022/05/06 11:23:19 by narnaud ### ########.fr */ +/* Updated: 2022/05/06 12:03:13 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ # include "minishell.h" -int main(int argc, char **argv, char **envp) + +void termios(int ctl) +{ + struct termios termios_p; + int tty; + + tty = ttyslot(); + if (tcgetattr(tty, &termios_p) < 0) + exit ((perror("stdin"), EXIT_FAILURE)); + if (ctl) + { + termios_p.c_lflag |= ECHOCTL; + signal(SIGINT, nothing); + signal(SIGQUIT, nothing); + } + else + { + termios_p.c_lflag &= ~(ECHOCTL); + signal(SIGINT, sigs_handler); + signal(SIGQUIT, sigs_handler); + } + termios_p.c_cc[VQUIT] = ctl * 28; + tcsetattr(tty, TCSANOW, &termios_p); +} + +void prompt(t_datas *datas) { + char *line; t_command *cmd; + + termios(0); + line = readline("$ "); + termios(1); + if (line == NULL) + halt(EXIT_FAILURE); + if (is_empty(line)) + return ; + cmd = parser(datas, lexer(datas, line), NULL); + if (!cmd->prev && !cmd->next && is_builtin(cmd->argv[0])) + builtin_call(datas, cmd); + else + piper(datas, cmd); + add_history(line); +} + +int main(int argc, char **argv, char **envp) +{ t_datas datas; - char *line; int i; i = 0; @@ -26,21 +69,7 @@ int main(int argc, char **argv, char **envp) if (argc < 2) (void)argv; while (1) - { - termios(0); - line = readline("$ "); - termios(1); - if (line == NULL) - halt(EXIT_FAILURE); - if (is_empty(line)) - continue ; - cmd = parser(&datas, lexer(&datas, line), NULL); - if (!cmd->prev && !cmd->next && is_builtin(cmd->argv[0])) - builtin_call(&datas, cmd); - else - piper(&datas, cmd); - add_history(line); - } + prompt(&datas); clear_history(); return (EXIT_SUCCESS); } diff --git a/minishell.h b/minishell.h index c5d08fd..414445b 100755 --- a/minishell.h +++ b/minishell.h @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */ -/* Updated: 2022/05/06 11:38:29 by narnaud ### ########.fr */ +/* Updated: 2022/05/06 11:49:32 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,8 +50,8 @@ typedef struct s_datas // ----------------------------------Utils.c int is_empty(char *line); void halt(int ret_code); -void termios(int ctl); void sigs_handler(int sig_num); +void nothing(int sig_num); void handle_status(t_datas *datas, int status); // ----------------------------------Builtins.c @@ -74,7 +74,6 @@ pid_t caller(t_datas *datas, t_command *cmd); int piper(t_datas *datas, t_command *cmd); // ----------------------------------Parser.c - typedef enum e_type { WORD, @@ -97,7 +96,6 @@ void update_redir(t_datas *datas, t_command *cmd, t_token *tok); t_command *parser(t_datas *datas, t_token *tok, t_command *prev); // ----------------------------------Lexer.c - typedef enum e_state { OLD_ST, diff --git a/utils.c b/utils.c index 6aa053a..6ce579c 100755 --- a/utils.c +++ b/utils.c @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/03 08:57:53 by narnaud #+# #+# */ -/* Updated: 2022/05/06 11:39:05 by narnaud ### ########.fr */ +/* Updated: 2022/05/06 12:13:48 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,29 +55,3 @@ void halt(int ret_code) printf("exit\n"); exit(ret_code); } - -void termios(int ctl) -{ - struct termios termios_p; - int tty; - - tty = ttyslot(); - if (tcgetattr(tty, &termios_p) < 0) - exit ((perror("stdin"), EXIT_FAILURE)); - if (ctl) - { - termios_p.c_lflag |= ECHOCTL; - signal(SIGINT, nothing); - signal(SIGQUIT, nothing); - } - else - { - termios_p.c_lflag &= ~(ECHOCTL); - signal(SIGINT, sigs_handler); - signal(SIGQUIT, sigs_handler); - } - termios_p.c_cc[VQUIT] = ctl * 28; - tcsetattr(tty, TCSANOW, &termios_p); -} - -