diff --git a/Makefile b/Makefile index b239c10..e46605e 100644 --- a/Makefile +++ b/Makefile @@ -26,6 +26,7 @@ SRCS = main.c init.c free.c generic.c \ lexing.c \ parsing.c \ valid_syntax.c valid_pipeline.c valid_command.c valid_io_redirect.c \ + fill_cmd.c \ env.c exit.c echo.c DIR_OBJS = builds diff --git a/cat b/cat new file mode 100644 index 0000000..e69de29 diff --git a/file.txt b/file.txt new file mode 100755 index 0000000..680d3ea --- /dev/null +++ b/file.txt @@ -0,0 +1,15 @@ +builds +cat +echo +file.txt +headers +libft +Makefile +minishell +minishell.en.subject.pdf +parsing.txt +README.md +ressources +srcs +tests +valgrind_readline.supp diff --git a/headers/minishell_prototypes.h b/headers/minishell_prototypes.h index f6b043f..00b7150 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/10/24 19:20:09 by lperrey ### ########.fr */ +/* Updated: 2021/10/28 20:49:30 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,10 +23,11 @@ void shell_loop(t_all *c); t_token *input_to_tokens(char *input); // Parser -t_cmd **parsing(t_token *token_list); +t_cmd **parsing(t_token *token_list, char **envp); int valid_syntax(t_token *token_list); int valid_token(t_token **token_list, enum e_token_id token_id); int valid_command_separator(const t_token *token_list); +t_cmd **fill_cmd(t_token *token, char **envp); // Builtins int builtin_env(int argc, char *argv[], t_all *c); @@ -43,7 +44,9 @@ char *ft_strjoinfree_s2(const char *s1, char *s2); void ft_lstprint(t_list *lst, int fd); int ft_isinset_str(char *str, char *set); size_t ft_2d_arrlen(void *ptr); // Replace ft_arrlen() -void *ft_dup_2d_arr(void *ptr); +char **ft_dup_2d_char_arr(char **ptr); void *ft_resize_2d_arr(void *ptr, size_t add_nbr); +void print_matrix(char **matrix, char *sep); +void print_token(t_token *token, char *sep); #endif diff --git a/srcs/execute.c b/srcs/execute.c new file mode 100644 index 0000000..2933074 --- /dev/null +++ b/srcs/execute.c @@ -0,0 +1,110 @@ +#include "minishell.h" + +void close_fd(t_cmd *cmd) +{ + if (cmd->fd_in != 0) + close(cmd->fd_in); + if (cmd->fd_out != 1) + close(cmd->fd_out); +} + +void exec_cmd(char **envp, t_list *cmd_list) +{ + t_cmd *cmd; + pid_t pid; + pid_t wpid; + int status; + + while(cmd_list) + { + cmd = cmd_list->content; + pid = fork(); + if (pid == 0) + { + if (cmd->fd_in != 0) + dup2(cmd->fd_in, STDIN_FILENO); + if (cmd->fd_out != 1) + dup2(cmd->fd_out, STDOUT_FILENO); + close_fd(cmd); + execve(cmd->argv[0], cmd->argv, envp); + } + else + close_fd(cmd); + cmd_list = cmd_list->next; + } + // waitpid pour la derniere commande (pour '$?') + while ((wpid = wait(&status)) > 0); +} + +int nbr_pipes(char *input) +{ + int i; + int count; + + i = -1; + count = 0; + while (input[++i]) + if (input[i] == '|') + count++; + return (count); +} + +int handle_fdd(char *input, int fdin, t_cmd *cmd) +{ + int *pipes_fd; + int next_in; + char *tmp; + + cmd->fd_in = fdin; + cmd->fd_out = 1; + next_in = 0; + if (input + 1) + { + pipes_fd = calloc(2, sizeof(int)); + pipe(pipes_fd); + cmd->fd_out = pipes_fd[1]; + next_in = pipes_fd[0]; + } + tmp = ft_strchr(input, '>'); + if (tmp) + { + tmp[0] = '\0'; + tmp = ft_strtrim(tmp + 2, " "); // +2 for "> " + if (cmd->fd_out != 1) + close(cmd->fd_out); + cmd->fd_out = open(tmp, O_RDWR | O_TRUNC); + } + tmp = ft_strchr(input, '<'); + if (tmp) + { + tmp[0] = '\0'; + tmp = ft_strtrim(tmp + 2, " "); // +2 for "> " + if (cmd->fd_in != 0) + close(cmd->fd_in); + cmd->fd_in = open(tmp, O_RDONLY); + } +printf(" handle_fd: %s\n", input); + return (next_in); +} + +t_cmd *fill_cmd(char *input, char **envp) +{ + t_cmd *cmd; + char **input_split; + int i; + int tmp_fd; + + cmd = calloc(nbr_pipes(input), sizeof(t_cmd)); + input_split = ft_split(input, '|'); + tmp_fd = 0; + i = 0; + while (input_split[i]) + { + tmp_fd = handle_fdd(input_split[i], tmp_fd, &cmd[i]); + cmd[i].argv = ft_split(input_split[i], ' '); + cmd[i].builtin = cmd_path(cmd[i].argv, envp); + i++; + } + return (cmd); +} + diff --git a/srcs/generic.c b/srcs/generic.c index 43f2b9c..38806aa 100644 --- a/srcs/generic.c +++ b/srcs/generic.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/08 09:25:35 by lperrey #+# #+# */ -/* Updated: 2021/10/23 15:19:07 by lperrey ### ########.fr */ +/* Updated: 2021/10/28 17:04:03 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -85,7 +85,7 @@ size_t ft_2d_arrlen(void *ptr) // Replace ft_arrlen() return (len); } -void *ft_dup_2d_arr(void *ptr) +char **ft_dup_2d_char_arr(char **ptr) { unsigned int i; char **arr; @@ -98,7 +98,9 @@ void *ft_dup_2d_arr(void *ptr) i = 0; while (arr[i]) { - new_arr[i] = arr[i]; + new_arr[i] = ft_strdup(arr[i]); + if (!new_arr[i]) + return (ft_retp_free(NULL, new_arr, ft_free_2d_arr)); i++; } new_arr[i] = NULL; @@ -122,3 +124,44 @@ void *ft_resize_2d_arr(void *ptr, size_t add_nbr) free(arr); return (new_arr); } + +void print_matrix(char **matrix, char *sep) +{ + int i; + + i = 0; + while (matrix[i]) + { + printf("%s", matrix[i]); + if (matrix[i + 1]) + printf("%s", sep); + fflush(stdout); + i++; + } + write(1, "\n", 1); +} + +void print_token(t_token *token, char *sep) +{ + while(token) + { + printf("%s - ", token->content); + if(token->id == T_TOKEN) + printf("T_TOKEN"); + else if(token->id == T_LESS) + printf("T_LESS"); + else if(token->id == T_GREAT) + printf("T_GREAT"); + else if(token->id == T_PIPE) + printf("T_PIPE"); + else if(token->id == T_DLESS) + printf("T_DLESS"); + else if(token->id == T_DGREAT) + printf("T_DGREAT"); + else if(token->id == T_WORD) + printf("T_WORD"); + printf("%s", sep); + token = token->next; + } +} + diff --git a/srcs/init.c b/srcs/init.c index 6264069..fd42712 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/10/23 16:04:03 by lperrey ### ########.fr */ +/* Updated: 2021/10/28 11:07:40 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,9 +18,9 @@ static char *init_prompt(char *prompt_base); int init(t_all *c, char *envp[]) { ft_bzero(c, sizeof *c); - c->envp = ft_dup_2d_arr(envp); + c->envp = ft_dup_2d_char_arr(envp); if (!c->envp) - return (ft_reti_perror(0, "ft_dup_2d_arr(envp) error")); + return (ft_reti_perror(0, "ft_dup_2d_char_arr(envp) error")); c->prompt_base = init_prompt_base(); if (!c->prompt_base) return (ft_reti_perror(0, "init_prompt_base() error")); diff --git a/srcs/main.c b/srcs/main.c index 9a55ed0..eb745a5 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */ -/* Updated: 2021/10/27 14:48:03 by hulamy ### ########.fr */ +/* Updated: 2021/10/28 15:12:04 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,12 +24,3 @@ int main(int argc, char *argv[], char *envp[]) return (0); } - -/* -** idea about malloc protection : -** have them(mallocand similar) done in a specific function that would check their return and accordingly exit the program or continue -** -> so that it can be done in one line -** void calloc_or_exit(int num, int size); -** and possibly give it a pointer to a function that will clean before exit -** void calloc_or_exit(int num, int size, void (*f)(void *ptr), void *ptr); -*/ diff --git a/srcs/parsing/fill_cmd.c b/srcs/parsing/fill_cmd.c new file mode 100644 index 0000000..dfc3eb1 --- /dev/null +++ b/srcs/parsing/fill_cmd.c @@ -0,0 +1,165 @@ + +#include "minishell.h" + +int nbr_pipes(t_token *token) +{ + int nb; + + nb = 0; + while (token) + { + if (token->id == T_PIPE) + nb++; + token = token->next; + } + return (nb); +} + +int nbr_argv(t_token *token) +{ + int i; + + i = 0; + while (token && token->id != T_PIPE) + { + token = token->next; + i++; + } + return (i); +} + +// T_T +// T_L +// T_G +// T_P +// T_D +// T_D +// T_W +void handle_argv(t_token **token, t_cmd *cmd) +{ + int argc; + int i; + + argc = nbr_argv(*token); + cmd->argv = calloc(argc + 1, sizeof(char *)); + cmd->argv[argc] = NULL; + i = 0; + while (*token && (*token)->id != T_PIPE) + { + if ((*token)->id == T_WORD) + { + cmd->argv[i] = ft_strdup((*token)->content); + i++; + } + *token = (*token)->next; + } + if (*token && (*token)->id == T_PIPE) + *token = (*token)->next; +} + +void handle_fd(t_token *token, t_cmd **cmd) +{ + int *pipes_fd; + + (*cmd)->fd_out = 1; + while (token && token->id != T_PIPE) + { + // T_LESS : '<', + if (token->id == T_LESS) + { + if ((*cmd)->fd_in != 0) + close((*cmd)->fd_in); + (*cmd)->fd_in = open(token->next->content, O_RDONLY | O_CREAT); + token->id = T_TOKEN; + token->next->id = T_TOKEN; + } + // T_GREAT : '>', + if (token->id == T_GREAT) + { + (*cmd)->fd_out = open(token->next->content, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); + token->id = T_TOKEN; + token->next->id = T_TOKEN; + } + // T_DGREAT : '>>' + if (token->id == T_DGREAT) + { + (*cmd)->fd_out = open(token->next->content, O_WRONLY | O_CREAT | O_APPEND, S_IRWXU); + token->id = T_TOKEN; + token->next->id = T_TOKEN; + } + // T_DLESS : '<<' (heredoc) + // if (token->id == T_DGREAT) + // { + // cmd->fd_out = open(token->next->content, O_WRONLY | O_TMPFILE | O_APPEND, S_IRWXU); + // token->out = T_TOKEN; + // token->next->id = T_TOKEN; + // } + token = token->next; + } + if (token && token->id == T_PIPE) + { + pipes_fd = calloc(2, sizeof(int)); + pipe(pipes_fd); + if ((*cmd)->fd_out == 1) + (*cmd)->fd_out = pipes_fd[1]; + else + close(pipes_fd[1]); + cmd[1]->fd_in = pipes_fd[0]; + } +} + +int handle_builtin(t_token *token, t_cmd *cmd) +{ + (void)token; + (void)cmd; + return (0); +} + +void handle_cmd(char **argv, char **envp) +{ + int i; + char **path; + char *cmd; + + i = 0; + while (envp[i] && ft_strncmp(envp[i], "PATH=", 5)) + i++; + path = ft_split(envp[i] + 5, ':'); // 5 = lentgh of "PATH=" + i = -1; + while (*path && i != 0) + { + cmd = ft_strjoin(path[0], "/"); + cmd = ft_strjoin(cmd, argv[0]); + i = access(cmd, X_OK); + path++; + } + argv[0] = cmd; +} + +t_cmd **fill_cmd(t_token *token, char **envp) +{ + t_cmd **cmd; + int pipes; + int i; + + pipes = nbr_pipes(token); + cmd = ft_calloc(pipes + 2, sizeof(t_cmd*)); + cmd[pipes + 1] = NULL; + i = -1; + while (++i <= pipes) + { + cmd[i] = ft_calloc(1, sizeof(t_cmd)); + ft_bzero(cmd[i], sizeof(t_cmd)); + } + i = 0; + while (i <= pipes) + { + handle_fd(token, cmd + i); + handle_argv(&token, cmd[i]); + if (!handle_builtin(token, cmd[i])) + handle_cmd(cmd[i]->argv, envp); + i++; + } + return(cmd); +} + diff --git a/srcs/parsing/parsing.c b/srcs/parsing/parsing.c index 49c9fd8..96fc2fa 100644 --- a/srcs/parsing/parsing.c +++ b/srcs/parsing/parsing.c @@ -1,14 +1,3 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parsing.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: lperrey +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2021/10/24 10:52:40 by lperrey #+# #+# */ -/* Updated: 2021/10/24 19:19:08 by lperrey ### ########.fr */ -/* */ -/* ************************************************************************** */ #include "minishell.h" @@ -22,13 +11,10 @@ enum e_token_id id; } t_binary_tree; */ -t_cmd **parsing(t_token *token_list) +t_cmd **parsing(t_token *token_list, char **envp) { t_cmd **cmd_arr; - size_t cmd_nbr; - (void)cmd_arr; - (void)cmd_nbr; /* t_binary_tree *syntax_tree; syntax_tree = ft_calloc(1, sizeof *syntax_tree); @@ -40,13 +26,13 @@ t_cmd **parsing(t_token *token_list) // Pipes count (determine cmd_nbr) // Struct CMD alloc/fill + cmd_arr = fill_cmd(token_list, envp); // 2.9.1 - 2) Expansion // 2.9.1 - 3) Redirection - //return (cmd_arr); - return ((t_cmd **)1); //temp test + return (cmd_arr); } /* ------------------------------------------------------- diff --git a/srcs/shell_loop.c b/srcs/shell_loop.c index 2aa41f6..188e909 100644 --- a/srcs/shell_loop.c +++ b/srcs/shell_loop.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */ -/* Updated: 2021/10/24 19:17:03 by lperrey ### ########.fr */ +/* Updated: 2021/10/28 20:45:09 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,42 @@ static char **tokens_list_to_argv(t_token *t); // temp test +void close_fd(t_cmd *cmd) +{ + if (cmd->fd_in != 0) + close(cmd->fd_in); + if (cmd->fd_out != 1) + close(cmd->fd_out); +} + +void execute_cmd(char **envp, t_cmd **cmd_arr) +{ + pid_t pid; + pid_t wpid; + int status; + int i; + + i = 0; + while(cmd_arr[i]) + { + pid = fork(); + if (pid == 0) + { + if (cmd_arr[i]->fd_in != 0) + dup2(cmd_arr[i]->fd_in, STDIN_FILENO); + if (cmd_arr[i]->fd_out != 1) + dup2(cmd_arr[i]->fd_out, STDOUT_FILENO); + close_fd(cmd_arr[i]); + execve(cmd_arr[i]->argv[0], cmd_arr[i]->argv, envp); + } + else + close_fd(cmd_arr[i]); + i++; + } + // waitpid pour la derniere commande (pour '$?') + while ((wpid = wait(&status)) > 0); +} + void shell_loop(t_all *c) { char *line_input; @@ -40,13 +76,14 @@ void shell_loop(t_all *c) builtin_echo(ft_lstsize((t_list *)c->token_list) + 1, tokens_list_to_argv(c->token_list), c); else { - if (parsing(c->token_list)) - ft_putstr_fd("Syntax OK:\n-----------\n", 1); - else + c->cmd_arr = parsing(c->token_list, c->envp); + if (c->cmd_arr == NULL) ft_putstr_fd("Syntax KO:\n-----------\n", 1); - - ft_putstr_fd("TOKENS LIST :\n-----------\n", 1); - ft_lstprint((t_list *)c->token_list, 1); + // else + // ft_putstr_fd("Syntax OK:\n-----------\n", 1); + // ft_putstr_fd("TOKENS LIST :\n-----------\n", 1); + // ft_lstprint((t_list *)c->token_list, 1); + execute_cmd(c->envp, c->cmd_arr); ft_lstclear((t_list **)&c->token_list, free); } }