From 7eda051f5c59373845f8fa82f4d1f72d97276d18 Mon Sep 17 00:00:00 2001 From: narnaud Date: Tue, 3 May 2022 15:53:41 +0200 Subject: [PATCH] rework: caller --- Makefile | 2 +- caller.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ lexer.c | 4 ++-- minishell.c | 47 ++++++++++++++++++++++++++++++++++++++++--- minishell.h | 11 +++++----- parser.c | 13 +++++------- 6 files changed, 115 insertions(+), 20 deletions(-) create mode 100644 caller.c diff --git a/Makefile b/Makefile index 53020d7..1780521 100755 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ NAME = minishell LIBFT = libft.a -SRCS = minishell.c lexer.c parser.c utils.c +SRCS = minishell.c lexer.c parser.c utils.c caller.c OBJS = ${SRCS:.c=.o} READLINE_INC = ~/.brew/opt/readline/include diff --git a/caller.c b/caller.c new file mode 100644 index 0000000..564fb16 --- /dev/null +++ b/caller.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* caller.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */ +/* Updated: 2022/05/03 13:57:06 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" +/* +int command_call(char *argv[], int input_fd, int output_fd) +{ + if (ft_strncmp(argv[0], "echo", 4) == 0) + return (ft_echo(argv, output_fd)); + else if (ft_strncmp(argv[0], "pwd", 3) == 0) + return (ft_pwd(argv, input_fd, output_fd)); + else if (ft_strncmp(argv[0], "cd", 2) == 0) + return (ft_cd(argv, input_fd, output_fd)); + else if (ft_strncmp(argv[0], "export", 6) == 0) + return (ft_export(argv, input_fd, output_fd)); + else if (ft_strncmp(argv[0], "unset", 5) == 0) + return (ft_unset(argv, input_fd, output_fd)); + else if (ft_strncmp(argv[0], "env", 3) == 0) + return (ft_env(argv, input_fd, output_fd)); + else if (ft_strncmp(argv[0], "exit", 4) == 0) + return (close_minishell(0)); + else + { + errno = EINVAL; + return (-1); + } +} +*/ +void exe(char *argv[], char *const envp[]) +{ + char **path_dirs; + char *path; + size_t path_length; + + + execve(argv[0], argv, envp); + path_dirs = ft_split(getenv("PATH"), ':'); + while (path_dirs) + { + path_length = ft_strlen(*path_dirs) + ft_strlen(argv[0]) + 2; + path = ft_calloc(path_length, sizeof(char)); + 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, argv[0], ft_strlen(argv[0])); + free(path); + execve(path, argv, envp); + path_dirs++; + } +} diff --git a/lexer.c b/lexer.c index 1bc322e..0e25dbb 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:42:26 by narnaud ### ########.fr */ +/* Updated: 2022/05/03 11:46:33 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,7 +52,7 @@ int check_register(t_lexer *lex, char **line, char *tmp) if (lex->state != ROOT_ST) return (0); spaces = 0; - if (ft_isspace(**line) ) + if (ft_isspace(**line)) { while (ft_isspace(**line)) (*line)++; diff --git a/minishell.c b/minishell.c index 8cd35f2..553b68c 100644 --- a/minishell.c +++ b/minishell.c @@ -6,12 +6,49 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */ -/* Updated: 2022/05/03 11:06:49 by narnaud ### ########.fr */ +/* Updated: 2022/05/03 15:50:18 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ # include "minishell.h" +// Note: Pipes are broken +int caller(t_command *cmd) +{ + int cmd_ret; + pid_t pid; + int pip[2]; + int status; + + + if (cmd->next && cmd->fd[0] == 0 && cmd->next->fd[1] == 0) + { + pipe(pip); + cmd->fd[1] = pip[1]; + cmd->next->fd[0] = pip[0]; + } + pid = fork(); + if (!pid) + { + cmd_ret = 1; //command_call(command->argv); + if (cmd_ret == 1) + { + if (cmd->fd[0]) + dup2(cmd->fd[0], STDIN_FILENO); + if (cmd->fd[1]) + dup2(cmd->fd[1], STDOUT_FILENO); + exe(cmd->argv, cmd->envp); + exit(1); + } + } + if (cmd->fd[1]) + close(cmd->fd[1]); + if (cmd->next) + return (caller(cmd->next)); + waitpid(pid, &status, 0); + return (cmd_ret); +} + t_command *parser(t_token *tok, t_command *prev) { int i; @@ -23,7 +60,8 @@ t_command *parser(t_token *tok, t_command *prev) if (prev) cmd->prev = prev; i = 0; - cmd->argv = ft_calloc(count_arguments(tok) + 1, sizeof(char *)); + cmd->argc = count_arguments(tok); + cmd->argv = ft_calloc(cmd->argc + 1, sizeof(char *)); while(tok && tok->type != PIPE) { if (tok->type) @@ -32,6 +70,9 @@ t_command *parser(t_token *tok, t_command *prev) cmd->argv[i++] = tok->value; 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]); cmd->argv[i] = NULL; if (tok && tok->type == PIPE) cmd->next = parser(tok->next, cmd); @@ -79,7 +120,7 @@ int launcher(t_env env) halt(EXIT_FAILURE); if (is_empty(line)) return (EXIT_FAILURE); - parser(lexer(line), NULL); + caller(parser(lexer(line), NULL)); add_history(line); return (0); } diff --git a/minishell.h b/minishell.h index b70d334..dbf8ec4 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 11:39:52 by narnaud ### ########.fr */ +/* Updated: 2022/05/03 15:40:23 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ # define PIPE_MAX_SIZE 8192 # define PATHS_MAX_SIZE 126 # define PROMPT_PREFIX 0 -# define DEBUG 1 +# define DEBUG 0 # include "libft/libft.h" # include @@ -39,7 +39,7 @@ void halt(int ret_code); int is_empty(char *line); int init_file(char *filename, int mode); -// ----------------------------------Piper.c +// ----------------------------------Caller.c typedef struct s_env { int null; @@ -47,17 +47,16 @@ typedef struct s_env typedef struct s_command { + int argc; char **argv; char **envp; - int argc; int fd[2]; - int input; - int output; char *heredoc; struct s_command *next; struct s_command *prev; } t_command; +void exe(char *argv[], char *const envp[]); // ----------------------------------Parser.c diff --git a/parser.c b/parser.c index 0d44e92..fa62f86 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/02 17:43:27 by narnaud ### ########.fr */ +/* Updated: 2022/05/03 12:43:48 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,14 +17,11 @@ size_t count_arguments(t_token *tok) size_t ret; ret = 0; - while (tok->value) + while (tok && tok->type != PIPE) { - if (tok->type == WORD && tok->value[0] != '|') + if (!tok->type) ret++; - if (tok->next) - tok = tok->next; - else - break ; + tok = tok->next; } return (ret); } @@ -35,7 +32,7 @@ void update_redir(t_command *cmd, t_token *tok) { if (cmd->fd[1]) close(cmd->fd[1]); - cmd->output = init_file(tok->value, O_TRUNC | O_WRONLY); + cmd->fd[1] = init_file(tok->value, O_TRUNC | O_WRONLY); } else if (tok->type == ADD) {