From 7c10094c6520643c7ad269b18898044bfdb59fde Mon Sep 17 00:00:00 2001 From: narnaud Date: Thu, 5 May 2022 10:12:34 +0200 Subject: [PATCH] -fix: builtins not fork when launched alone, -new: t_datas for env and exit_code, -rm: argc useless in t_command --- caller.c | 19 ++++++++----------- env.c | 53 +++++++++++++++++++++++++++-------------------------- lexer.c | 5 +---- minishell.c | 45 ++++++++++++++++++++++++++------------------- minishell.h | 39 +++++++++++++++++++++------------------ parser.c | 4 ++-- utils.c | 10 ++-------- 7 files changed, 87 insertions(+), 88 deletions(-) diff --git a/caller.c b/caller.c index 53fbede..a548b8a 100644 --- a/caller.c +++ b/caller.c @@ -6,13 +6,13 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */ -/* Updated: 2022/05/03 20:29:01 by narnaud ### ########.fr */ +/* Updated: 2022/05/05 09:40:42 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int command_call(t_command *cmd) +int command_call(t_datas *datas, t_command *cmd) { if (ft_strncmp(cmd->argv[0], "echo", 5) == 0) return (ft_echo(cmd)); @@ -21,28 +21,25 @@ int command_call(t_command *cmd) else if (ft_strncmp(cmd->argv[0], "cd", 3) == 0) return (ft_cd(cmd)); else if (ft_strncmp(cmd->argv[0], "export", 7) == 0) - return (ft_export(cmd)); + return (ft_export(datas, cmd)); else if (ft_strncmp(cmd->argv[0], "unset", 6) == 0) - return (ft_unset(cmd)); + return (ft_unset(datas, cmd)); else if (ft_strncmp(cmd->argv[0], "env", 4) == 0) - return (ft_env(cmd)); + return (ft_env(datas)); else if (ft_strncmp(cmd->argv[0], "exit", 5) == 0) return (close_minishell(0)); else - { - errno = EINVAL; return (-1); - } } -void exe(t_command *cmd) +void exe(t_datas *datas, t_command *cmd) { char **path_dirs; char *path; size_t path_length; if (ft_strchr(cmd->argv[0], '/')) - execve(cmd->argv[0], cmd->argv, *cmd->envp); + execve(cmd->argv[0], cmd->argv, datas->envp); else { path_dirs = ft_split(getenv("PATH"), ':'); @@ -53,7 +50,7 @@ void exe(t_command *cmd) ft_memcpy(path, *path_dirs, ft_strlen(*path_dirs)); ft_memcpy(path + ft_strlen(*path_dirs),"/", 1); ft_memcpy(path + ft_strlen(*path_dirs) + 1, cmd->argv[0], ft_strlen(cmd->argv[0])); - execve(path, cmd->argv, *cmd->envp); + execve(path, cmd->argv, datas->envp); path_dirs++; } } diff --git a/env.c b/env.c index 31d75d8..39eb825 100755 --- a/env.c +++ b/env.c @@ -6,51 +6,52 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/02/16 09:41:29 by narnaud #+# #+# */ -/* Updated: 2022/05/03 18:17:46 by narnaud ### ########.fr */ +/* Updated: 2022/05/05 09:30:51 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int ft_unset(t_command *cmd) +int is_valid_identifier(char *ident) { - if (!cmd->argv[1]) + if (ft_isalpha(ident[0]) || ident[0] == '_') { - //read(input_fd, PIPE_MAX_SIZE); + while (*ident) + { + if (!ft_isalpha(*ident) && !ft_isdigit(*ident) && *ident != '_') + return (0); + ident++; + } return (1); } return (0); } -int ft_env(t_command *cmd) +int ft_unset(t_datas *datas, t_command *cmd) { - int i; - - i = 0; - while ((*cmd->envp)[i]) + (void)datas; + if (!cmd->argv[1]) { - printf("%s\n", (*cmd->envp)[i]); - i++; + //read(input_fd, PIPE_MAX_SIZE); + return (1); } return (0); } -int is_valid_identifier(char *ident) +int ft_env(t_datas *datas) { - if (ft_isalpha(ident[0]) || ident[0] == '_') + int i; + + i = 0; + while (datas->envp[i]) { - while (*ident) - { - if (!ft_isalpha(*ident) && !ft_isdigit(*ident) && *ident != '_') - return (0); - ident++; - } - return (1); + printf("%s\n", datas->envp[i]); + i++; } return (0); } -int ft_export(t_command *cmd) +int ft_export(t_datas *datas, t_command *cmd) { char **new; char **env; @@ -63,18 +64,18 @@ int ft_export(t_command *cmd) printf ("export: not an identifier: %s\n", new[0]); return (0); } - while ((*cmd->envp)[i]) + while (datas->envp[i]) { - env = ft_split((*cmd->envp)[i], '='); + env = ft_split(datas->envp[i], '='); if (!ft_strncmp(new[0], env[0], ft_strlen(new[0]) + 1)) { - (*cmd->envp)[i] = cmd->argv[1]; + datas->envp[i] = cmd->argv[1]; return (1); } i++; } - (*cmd->envp)[i] = cmd->argv[1]; - (*cmd->envp)[i + 1] = NULL; + datas->envp[i] = cmd->argv[1]; + datas->envp[i + 1] = NULL; return (1); } diff --git a/lexer.c b/lexer.c index f7cb6dd..8ad6848 100644 --- a/lexer.c +++ b/lexer.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 13:44:57 by narnaud #+# #+# */ -/* Updated: 2022/05/03 11:46:33 by narnaud ### ########.fr */ +/* Updated: 2022/05/05 08:43:52 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,7 +44,6 @@ int create_token(t_lexer *lex, char str[]) return (1); } - int check_register(t_lexer *lex, char **line, char *tmp) { int spaces; @@ -93,7 +92,6 @@ int replace_var(char **line, char *tmp, int tmp_i) char *var_name; char *value; - // TODO : free split and var_name i = 1; while (ft_isalpha((*line)[i]) || ft_isdigit((*line)[i]) || (*line)[i] == '_') @@ -109,7 +107,6 @@ int replace_var(char **line, char *tmp, int tmp_i) return (tmp_i); } - int check_state(t_lexer *lex, char **line) { t_state new; diff --git a/minishell.c b/minishell.c index dafa1cb..23292c2 100644 --- a/minishell.c +++ b/minishell.c @@ -6,13 +6,13 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */ -/* Updated: 2022/05/03 20:31:44 by narnaud ### ########.fr */ +/* Updated: 2022/05/05 10:04:02 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ # include "minishell.h" -int caller(t_command *cmd) +int caller(t_datas *datas, t_command *cmd) { pid_t pid; int pip[2]; @@ -24,6 +24,8 @@ int caller(t_command *cmd) cmd->fd[1] = pip[1]; cmd->next->fd[0] = pip[0]; } + if (!cmd->prev && !cmd->next && command_call(datas, cmd) != -1) + return (1); pid = fork(); if (!pid) { @@ -31,25 +33,28 @@ int caller(t_command *cmd) dup2(cmd->fd[0], STDIN_FILENO); if (cmd->fd[1]) dup2(cmd->fd[1], STDOUT_FILENO); - if (command_call(cmd) == -1) - exe(cmd); - exit(1); + if (command_call(datas, cmd) == -1) + exe(datas, cmd); + exit(EXIT_SUCCESS); } if (cmd->fd[1]) close(cmd->fd[1]); if (cmd->next) - return (caller(cmd->next)); + return (caller(datas, cmd->next)); waitpid(pid, &status, 0); - //if (WIFEXITED(status) && WEXITSTATUS(status)) - // write(1, "\n", 1); if (WIFSIGNALED(status)) - printf("QUIT: %d\n", WTERMSIG(status)); + { + datas->exit_code = WTERMSIG(status); + if (datas->exit_code == 3) + printf("Quit: 3\n"); + } + if (cmd->fd[0]) close(cmd->fd[0]); return (1); } -t_command *parser(char ***envp, t_token *tok, t_command *prev) +t_command *parser(t_token *tok, t_command *prev) { int i; t_command *cmd; @@ -60,9 +65,7 @@ t_command *parser(char ***envp, t_token *tok, t_command *prev) if (prev) cmd->prev = prev; i = 0; - cmd->argc = count_arguments(tok); - cmd->argv = ft_calloc(cmd->argc + 1, sizeof(char *)); - cmd->envp = envp; + cmd->argv = ft_calloc(count_arguments(tok) + 1, sizeof(char *)); while(tok && tok->type != PIPE) { if (tok->type) @@ -72,12 +75,12 @@ t_command *parser(char ***envp, t_token *tok, t_command *prev) tok = tok->next; } if (DEBUG) - printf("######\n->cmd : %s, argc : %d, in_fd : %d, out_fd : %d\n", \ - cmd->argv[0], cmd->argc, cmd->fd[0], cmd->fd[1]); + printf("######\n->cmd : %s, in_fd : %d, out_fd : %d\n", \ + cmd->argv[0], cmd->fd[0], cmd->fd[1]); cmd->argv[i] = NULL; if (tok && tok->type == PIPE) - cmd->next = parser(envp, tok->next, cmd); - return (cmd); + cmd->next = parser(tok->next, cmd); + return(cmd); } t_token *lexer(char *line) @@ -112,7 +115,11 @@ t_token *lexer(char *line) int main(int argc, char **argv, char **envp) { - char *line; + t_datas datas; + char *line; + + datas.envp = envp; + datas.exit_code = 0; if (argc < 2) (void)argv; while (1) @@ -124,7 +131,7 @@ int main(int argc, char **argv, char **envp) halt(EXIT_FAILURE); if (is_empty(line)) continue ; - caller(parser(&envp, lexer(line), NULL)); + caller(&datas, parser(lexer(line), NULL)); add_history(line); } clear_history(); diff --git a/minishell.h b/minishell.h index 2e5acb9..8566bd1 100644 --- a/minishell.h +++ b/minishell.h @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */ -/* Updated: 2022/05/03 20:22:29 by narnaud ### ########.fr */ +/* Updated: 2022/05/05 09:38:23 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,37 +33,40 @@ typedef struct s_command { - int argc; char **argv; - char ***envp; int fd[2]; char *heredoc; struct s_command *next; struct s_command *prev; } t_command; -// ----------------------------------Utils.c -int ft_echo(t_command *cmd); -int ft_pwd(t_command *cmd); -int ft_cd(t_command *cmd); -int close_minishell(int exit_code); - -// ----------------------------------Utils.c -int ft_unset(t_command *cmd); -int ft_env(t_command *cmd); -int is_valid_identifier(char *ident); -int ft_export(t_command *cmd); +typedef struct s_datas +{ + char **envp; + int exit_code; +} t_datas; // ----------------------------------Utils.c - -void halt(int ret_code); int is_empty(char *line); +void halt(int ret_code); void termios(int ctl); void sigs_handler(int sig_num); +// ----------------------------------Builtins.c +int close_minishell(int exit_code); +int ft_echo(t_command *cmd); +int ft_pwd(t_command *cmd); +int ft_cd(t_command *cmd); + +// ----------------------------------Env.c +int is_valid_identifier(char *ident); +int ft_unset(t_datas *datas, t_command *cmd); +int ft_env(t_datas *datas); +int ft_export(t_datas *datas, t_command *cmd); + // ----------------------------------Caller.c -void exe(t_command *cmd); -int command_call(t_command *cmd); +void exe(t_datas *datas, t_command *cmd); +int command_call(t_datas *datas, t_command *cmd); // ----------------------------------Parser.c diff --git a/parser.c b/parser.c index 54c8e1c..6ecc855 100644 --- a/parser.c +++ b/parser.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 16:09:25 by narnaud #+# #+# */ -/* Updated: 2022/05/03 12:43:48 by narnaud ### ########.fr */ +/* Updated: 2022/05/05 08:28:41 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,7 +37,7 @@ size_t count_arguments(t_token *tok) ret = 0; while (tok && tok->type != PIPE) { - if (!tok->type == WORD) + if (tok->type == WORD) ret++; tok = tok->next; } diff --git a/utils.c b/utils.c index f7a4615..8c6e5d9 100644 --- a/utils.c +++ b/utils.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/03 08:57:53 by narnaud #+# #+# */ -/* Updated: 2022/05/03 09:00:40 by narnaud ### ########.fr */ +/* Updated: 2022/05/05 09:18:28 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,10 +26,6 @@ int is_empty(char *line) void nothing(int sig_num) { (void)sig_num; - //if (sig_num == SIGQUIT) - // write(1, "QUIT: 3\n", 8); - //else if (sig_num == SIGINT) - // write(1, "\n", 1); } void termios(int ctl) { @@ -39,8 +35,6 @@ void termios(int ctl) tty = ttyslot(); if (tcgetattr(tty, &termios_p) < 0) exit ((perror("stdin"), EXIT_FAILURE)); - //termios_p.c_oflag &= CRDLY; - //termios_p.c_lflag &= ICANON; if (ctl) { termios_p.c_lflag |= ECHOCTL; @@ -69,6 +63,6 @@ void sigs_handler(int sig_num) void halt(int ret_code) { - printf("exit"); + printf("exit\n"); exit(ret_code); }