From d3d55386d95ead98fa880166ee6f6aa860db7228 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Sat, 11 Dec 2021 04:27:57 +0100 Subject: [PATCH 01/14] refactoring parsing (wip) --- headers/minishell_prototypes.h | 6 +-- .../expansions/new_token_for_each_field.c | 14 +++---- srcs/parsing/expansions/words_expansions.c | 37 +++++++++++++------ srcs/parsing/parsing.c | 27 ++++++-------- 4 files changed, 48 insertions(+), 36 deletions(-) diff --git a/headers/minishell_prototypes.h b/headers/minishell_prototypes.h index 20ff829..1178276 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/12/05 17:36:01 by lperrey ### ########.fr */ +/* Updated: 2021/12/10 18:46:48 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,8 +40,8 @@ int valid_command_separator(const t_token *token_list); size_t count_pipes(t_token *token_list); t_cmd **pipeline_alloc(size_t cmd_nbr); int pipeline_fill_argv(t_token *token_list, t_cmd **pipeline); -int words_expansions(t_token *token_list); -int token_expansions(t_token **t); +int expansions(t_token *token_list, t_cmd **pipeline); +int token_expansions(t_token *t); int redirections(t_token *token_list, t_cmd **pipeline); // Exec diff --git a/srcs/parsing/expansions/new_token_for_each_field.c b/srcs/parsing/expansions/new_token_for_each_field.c index 029106b..91d72f1 100644 --- a/srcs/parsing/expansions/new_token_for_each_field.c +++ b/srcs/parsing/expansions/new_token_for_each_field.c @@ -6,15 +6,15 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */ -/* Updated: 2021/11/30 18:51:13 by lperrey ### ########.fr */ +/* Updated: 2021/12/10 19:11:52 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static t_token *insert_tokens(t_token *t, t_token *insert_lst); +static void insert_tokens(t_token *t, t_token *insert_lst); -int new_token_for_each_field(char **fields, t_token **t) +int new_token_for_each_field(char **fields, t_token *t) { t_token head; t_token *insert_lst; @@ -38,11 +38,11 @@ int new_token_for_each_field(char **fields, t_token **t) i++; } free(fields); - *t = insert_tokens(*t, head.next); + insert_tokens(t, head.next); return (1); } -static t_token *insert_tokens(t_token *t, t_token *insert_lst) +static void insert_tokens(t_token *t, t_token *insert_lst) { t_token *tmp; t_token *insert_lst_last; @@ -50,12 +50,12 @@ static t_token *insert_tokens(t_token *t, t_token *insert_lst) t->id = 0; ft_free_null(&t->content); if (!insert_lst) - return (t); + return ; tmp = t->next; t->next = insert_lst; insert_lst_last = (t_token *)ft_lstlast((t_list *)insert_lst); insert_lst_last->next = tmp; - return (insert_lst_last); + //return (insert_lst_last); // return inutile pour reusinage } diff --git a/srcs/parsing/expansions/words_expansions.c b/srcs/parsing/expansions/words_expansions.c index 17f0a46..e217d8b 100644 --- a/srcs/parsing/expansions/words_expansions.c +++ b/srcs/parsing/expansions/words_expansions.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */ -/* Updated: 2021/11/30 18:35:59 by lperrey ### ########.fr */ +/* Updated: 2021/12/10 19:51:33 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ t_list *expand_token(char *content); char *rejoin_after_expand(t_list *expand_lst); -int new_token_for_each_field(char **fields, t_token **t); +int new_token_for_each_field(char **fields, t_token *t); // 1 - chaque bout dans un element d'une t_list // (telle quelle si non expand, ou VARIABLE de env) @@ -25,28 +25,43 @@ int new_token_for_each_field(char **fields, t_token **t); // (ft_lstadd_front() sur le token original, puis set le token orignal à : // t->id = 0 ; free(t->content) ; t->content = NULL ; pour qu'il soit ignoré sur la suite du parsing) -int words_expansions(t_token *token_list) +int expansions(t_token *t, t_cmd **pipeline) { - while (token_list) + int i; + t_token *next_token; + + i = 0; + while (t) { - if (token_list->id == T_WORD) - token_expansions(&token_list); - token_list = token_list->next; + if (t->id == '|') + i++; + if (!pipeline[i]->error && t->id == T_WORD) + { + next_token = t->next; + if (!token_expansions(t)) + { + pipeline[i]->error = EXIT_EXPANSION; + } + while (t != next_token) + t = t->next; + } + else + t = t->next; } return (1); } -int token_expansions(t_token **t) +int token_expansions(t_token *t) { void *tmp; char **tmp_split; // 1 - tmp = expand_token((*t)->content); + tmp = (t_list*)expand_token(t->content); if (!tmp) return (0); // 2 - tmp = rejoin_after_expand(tmp); + tmp = (char*)rejoin_after_expand(tmp); if (!tmp) return (0); // 3 @@ -55,7 +70,7 @@ int token_expansions(t_token **t) if (!tmp_split) return (0); // 4 - tmp = ft_dup_2d_arr(tmp_split, (t_dup_f)ft_strdup_quotes); + tmp = (char**)ft_dup_2d_arr(tmp_split, (t_dup_f)ft_strdup_quotes); ft_free_2d_arr(tmp_split); tmp_split = tmp; if (!tmp_split) diff --git a/srcs/parsing/parsing.c b/srcs/parsing/parsing.c index bf2708f..7d1b27c 100644 --- a/srcs/parsing/parsing.c +++ b/srcs/parsing/parsing.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/24 10:52:40 by lperrey #+# #+# */ -/* Updated: 2021/12/06 01:08:11 by lperrey ### ########.fr */ +/* Updated: 2021/12/10 19:58:17 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,25 +52,22 @@ t_cmd **parsing(t_token *token_list) // 2.9.1 - 1) Save Words save_redirections_words(token_list); - // 2.9.1 - 2) Expansion - // TEST TOKENS PRINT - //ft_putstr_fd("TOKENS LIST :\n-----------\n", 1); - //ft_lstprint((t_list *)token_list, 1); - // - // getenv() ne va pas fonctionner avec le changement d'environnement prévu jusqu'ici. - // TODO : Revoir le changement d'environnement (avec extern char **environ ?) - // OU Integrer un equivalent perso comme dans pipex - if (!words_expansions(token_list)) - return (NULL); - // - //ft_putstr_fd("TOKENS LIST EXPANDED :\n-----------\n", 1); - //ft_lstprint((t_list *)token_list, 1); - // Struct CMD alloc pipeline = pipeline_alloc(1 + count_pipes(token_list)); if (!pipeline) return (NULL); + // 2.9.1 - 2) Expansion + // TEST TOKENS PRINT +/* ft_putstr_fd("TOKENS LIST :\n-----------\n", STDERR_FILENO); + ft_lstprint((t_list *)token_list, STDERR_FILENO); */ + // + if (!expansions(token_list, pipeline)) + return (ft_retp_free(NULL, &pipeline, (t_free_f)free_pipeline)); + // +/* ft_putstr_fd("TOKENS LIST EXPANDED :\n-----------\n", STDERR_FILENO); + ft_lstprint((t_list *)token_list, STDERR_FILENO); */ + // 2.9.1 - 3) Redirection if (!redirections(token_list, pipeline)) return (ft_retp_free(NULL, &pipeline, (t_free_f)free_pipeline)); From 82f0362323bb6ce18b0b0c7c70b7068d03e2e0e8 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Sat, 11 Dec 2021 04:28:43 +0100 Subject: [PATCH 02/14] expand redirections --- srcs/parsing/redirections/redirections.c | 80 ++++++++++++++++++++---- 1 file changed, 67 insertions(+), 13 deletions(-) diff --git a/srcs/parsing/redirections/redirections.c b/srcs/parsing/redirections/redirections.c index d8d7539..34a2a36 100644 --- a/srcs/parsing/redirections/redirections.c +++ b/srcs/parsing/redirections/redirections.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/11 18:46:43 by lperrey #+# #+# */ -/* Updated: 2021/12/09 20:38:05 by lperrey ### ########.fr */ +/* Updated: 2021/12/11 04:22:07 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,8 @@ int redirections(t_token *t, t_cmd **pipeline) i = 0; while (t) { + if (t->id == '|') + i++; if (!pipeline[i]->error) { if (t->id == '<' || t->id == T_DLESS) @@ -37,13 +39,65 @@ int redirections(t_token *t, t_cmd **pipeline) return (0); } } - if (t->id == '|') - i++; t = t->next; } return (1); } +void test_tokens_print(t_list *lst) +{ + while (lst) + { + printf("CONTENT = %s\n", lst->content); + printf("ID = %i\n", ((t_token*)lst)->id); + lst = lst->next; + } +} + +// TODO : Expansion + quote removal sur le word t->next->content. +// si plus d'un champ ou aucun champ aprés expansion, +// message d'erreur comme bash "bash: $VAR: ambiguous redirect" +int EXPAND_AND_QUOTE_REMOVAL(t_token *t) +{ + t_token *head; + t_token *next_token; + int ret; + + ret = 1; + head = t; + t = t->next; + +/* ft_putstr_fd("Token Before:\n-----------\n", STDERR_FILENO); + test_tokens_print((t_list *)t); + ft_putstr_fd("-----------\n", STDERR_FILENO); */ + + next_token = t->next; + t->next = NULL; + if (!token_expansions(t)) + return (0); + head->next = t->next; + free(t); + +/* ft_putstr_fd("Head After:\n-----------\n", STDERR_FILENO); + test_tokens_print((t_list *)head); + ft_putstr_fd("-----------\n", STDERR_FILENO); */ + + if (head->next) + head->next->id = T_REDIRECTION_WORD; // Eventuellement a integrer dans token_expansions() + if (ft_lstsize((t_list *)head->next) != 1) + { + ret = 0; + ft_putstr_fd("minishell: ambiguous redirect\n", STDERR_FILENO); // tmp message + } + ((t_token *)ft_lstlast((t_list *)head))->next = next_token; + +/* ft_putstr_fd("HEAD after adjust/rejoin:\n-----------\n", STDERR_FILENO); + test_tokens_print((t_list *)head); + ft_putstr_fd("-----------\n", STDERR_FILENO); */ + + return (ret); +} + static int redirect_cmd_input(t_token *t, t_cmd *cmd) { if (cmd->fd_in != STDIN_FILENO) @@ -51,11 +105,11 @@ static int redirect_cmd_input(t_token *t, t_cmd *cmd) perror("close()"); if (t->id == '<') { - // TODO : Expansion + quote removal sur le word t->next->content. - // si plus d'un champ ou aucun champ aprés expansion, - // message d'erreur comme bash "bash: $VAR: ambiguous redirect" - // OU prise en compte seulement du premier champ. - //EXPAND_AND_QUOTE_REMOVAL_PLACEHOLDER(); + if (!EXPAND_AND_QUOTE_REMOVAL(t)) + { + cmd->error = EXIT_REDIRECTION; + return (0); + } cmd->fd_in = open(t->next->content, O_RDONLY); if (cmd->fd_in == -1) { @@ -87,11 +141,11 @@ static int redirect_cmd_output(t_token *t, t_cmd *cmd) if (cmd->fd_out != STDOUT_FILENO) if (close(cmd->fd_out) == -1) perror("close()"); - // TODO : Expansion + quote removal sur le word t->next->content. - // si plus d'un champ ou aucun champ aprés expansion, - // message d'erreur comme bash "bash: $VAR: ambiguous redirect" - // OU prise en compte seulement du premier champ. - //EXPAND_AND_QUOTE_REMOVAL_PLACEHOLDER(); + if (!EXPAND_AND_QUOTE_REMOVAL(t)) + { + cmd->error = EXIT_REDIRECTION; + return (0); + } flags = O_WRONLY | O_CREAT; if (t->id == '>') flags = flags | O_TRUNC; From 465c4de21ec1dfbd423e1f06698c9a3a3c01d694 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Sat, 11 Dec 2021 04:58:14 +0100 Subject: [PATCH 03/14] update submodule commit --- minishell_tests | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/minishell_tests b/minishell_tests index 7f638a0..57cff22 160000 --- a/minishell_tests +++ b/minishell_tests @@ -1 +1 @@ -Subproject commit 7f638a0bf4e4a08150fb472b1b1e663c60df3038 +Subproject commit 57cff2229388be8cf2ad210859b4677a15b3b760 From a7de843dc194db07cc408b36506b50e0beb6d777 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Sat, 11 Dec 2021 05:53:55 +0100 Subject: [PATCH 04/14] check of cmd->error : - before calling cmd_find_access() - before calling simple_command_builtin() --- srcs/exec/pipeline.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/srcs/exec/pipeline.c b/srcs/exec/pipeline.c index 3a0354f..e8d61d2 100644 --- a/srcs/exec/pipeline.c +++ b/srcs/exec/pipeline.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */ -/* Updated: 2021/12/01 16:49:37 by lperrey ### ########.fr */ +/* Updated: 2021/12/11 05:24:07 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,14 +18,25 @@ static pid_t pipeline_exec(t_cmd *pipeline[], t_all *c); int pipeline(t_all *c) { - if (!open_pipes(c->pipeline)) + t_cmd **pipeline; + int ret; + + pipeline = c->pipeline; + if (!open_pipes(pipeline)) return (0); - if (!pipeline_find_access(c->pipeline, c->path)) + if (!pipeline_find_access(pipeline, c->path)) return (0); - if (ft_2d_arrlen(c->pipeline) == 1 && c->pipeline[0]->builtin_f) - simple_command_builtin(c->pipeline[0], c); + if (pipeline[0]->builtin_f && ft_2d_arrlen(pipeline) == 1) + { + if (!pipeline[0]->error) + { + ret = simple_command_builtin(pipeline[0], c); + if (ret != EXIT_SUCCESS) + set_last_exit_status(ret); + } + } else - wait_subshell(pipeline_exec(c->pipeline, c)); + wait_subshell(pipeline_exec(pipeline, c)); free_pipeline(&c->pipeline); return (1); } @@ -62,8 +73,11 @@ static int pipeline_find_access(t_cmd *pipeline[], char *path[]) i = 0; while (pipeline[i]) { - if (!cmd_find_access(pipeline[i], path)) - return (0); + if (!pipeline[i]->error) + { + if (!cmd_find_access(pipeline[i], path)) + return (0); + } i++; } return (1); From 9c660d4f92f87cce4835fcf4c07ba05233ef3845 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Sat, 11 Dec 2021 20:19:26 +0100 Subject: [PATCH 05/14] expand_redirection() : rejoin token list on error + words_expansions.c -> expansions.c + generic.c misc --- Makefile | 2 +- srcs/generic.c | 6 +- .../{words_expansions.c => expansions.c} | 4 +- srcs/parsing/redirections/redirections.c | 94 +++++++------------ 4 files changed, 41 insertions(+), 65 deletions(-) rename srcs/parsing/expansions/{words_expansions.c => expansions.c} (94%) diff --git a/Makefile b/Makefile index 9a2a361..bc7bcd0 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ SRCS = main.c init.c retrieve_path.c free.c generic.c error_wrappers.c \ lexing.c fill_token.c check_operators.c \ parsing.c create_pipeline.c \ valid_syntax.c valid_pipeline.c valid_command.c valid_io_redirect.c \ - words_expansions.c expand_token.c rejoin_after_expand.c new_token_for_each_field.c \ + expansions.c expand_token.c rejoin_after_expand.c new_token_for_each_field.c \ ft_split_quotes.c ft_strdup_quotes.c \ redirections.c here_doc.c \ exec_cmd_line.c pipeline.c \ diff --git a/srcs/generic.c b/srcs/generic.c index e42a4ff..7e54576 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/12/05 17:35:32 by lperrey ### ########.fr */ +/* Updated: 2021/12/11 20:16:36 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -202,13 +202,13 @@ void *ft_lstnew_generic(size_t lst_size, size_t content_size) void ft_perror_io(char *err_str, char *io_file) { - ft_putstr_fd(err_str, 2); + ft_putstr_fd(err_str, STDERR_FILENO); perror(io_file); } int ft_reti_perror_io(int ret, char *err_str, char *io_file) { - ft_putstr_fd(err_str, 2); + ft_putstr_fd(err_str, STDERR_FILENO); perror(io_file); return (ret); } diff --git a/srcs/parsing/expansions/words_expansions.c b/srcs/parsing/expansions/expansions.c similarity index 94% rename from srcs/parsing/expansions/words_expansions.c rename to srcs/parsing/expansions/expansions.c index e217d8b..f61db9b 100644 --- a/srcs/parsing/expansions/words_expansions.c +++ b/srcs/parsing/expansions/expansions.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* words_expansions.c :+: :+: :+: */ +/* expansions.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */ -/* Updated: 2021/12/10 19:51:33 by lperrey ### ########.fr */ +/* Updated: 2021/12/11 20:10:37 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/srcs/parsing/redirections/redirections.c b/srcs/parsing/redirections/redirections.c index 34a2a36..e111f56 100644 --- a/srcs/parsing/redirections/redirections.c +++ b/srcs/parsing/redirections/redirections.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/11 18:46:43 by lperrey #+# #+# */ -/* Updated: 2021/12/11 04:22:07 by lperrey ### ########.fr */ +/* Updated: 2021/12/11 20:16:31 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ int here_doc(char *delimiter); static int redirect_cmd_input(t_token *t, t_cmd *cmd); static int redirect_cmd_output(t_token *t, t_cmd *cmd); +static int expand_redirection(t_token *t); int redirections(t_token *t, t_cmd **pipeline) { @@ -44,60 +45,6 @@ int redirections(t_token *t, t_cmd **pipeline) return (1); } -void test_tokens_print(t_list *lst) -{ - while (lst) - { - printf("CONTENT = %s\n", lst->content); - printf("ID = %i\n", ((t_token*)lst)->id); - lst = lst->next; - } -} - -// TODO : Expansion + quote removal sur le word t->next->content. -// si plus d'un champ ou aucun champ aprés expansion, -// message d'erreur comme bash "bash: $VAR: ambiguous redirect" -int EXPAND_AND_QUOTE_REMOVAL(t_token *t) -{ - t_token *head; - t_token *next_token; - int ret; - - ret = 1; - head = t; - t = t->next; - -/* ft_putstr_fd("Token Before:\n-----------\n", STDERR_FILENO); - test_tokens_print((t_list *)t); - ft_putstr_fd("-----------\n", STDERR_FILENO); */ - - next_token = t->next; - t->next = NULL; - if (!token_expansions(t)) - return (0); - head->next = t->next; - free(t); - -/* ft_putstr_fd("Head After:\n-----------\n", STDERR_FILENO); - test_tokens_print((t_list *)head); - ft_putstr_fd("-----------\n", STDERR_FILENO); */ - - if (head->next) - head->next->id = T_REDIRECTION_WORD; // Eventuellement a integrer dans token_expansions() - if (ft_lstsize((t_list *)head->next) != 1) - { - ret = 0; - ft_putstr_fd("minishell: ambiguous redirect\n", STDERR_FILENO); // tmp message - } - ((t_token *)ft_lstlast((t_list *)head))->next = next_token; - -/* ft_putstr_fd("HEAD after adjust/rejoin:\n-----------\n", STDERR_FILENO); - test_tokens_print((t_list *)head); - ft_putstr_fd("-----------\n", STDERR_FILENO); */ - - return (ret); -} - static int redirect_cmd_input(t_token *t, t_cmd *cmd) { if (cmd->fd_in != STDIN_FILENO) @@ -105,7 +52,7 @@ static int redirect_cmd_input(t_token *t, t_cmd *cmd) perror("close()"); if (t->id == '<') { - if (!EXPAND_AND_QUOTE_REMOVAL(t)) + if (!expand_redirection(t)) { cmd->error = EXIT_REDIRECTION; return (0); @@ -113,7 +60,7 @@ static int redirect_cmd_input(t_token *t, t_cmd *cmd) cmd->fd_in = open(t->next->content, O_RDONLY); if (cmd->fd_in == -1) { - ft_perror_io("open() ", t->next->content); // todo error + ft_perror_io("open() ", t->next->content); cmd->error = EXIT_REDIRECTION; } } @@ -122,7 +69,7 @@ static int redirect_cmd_input(t_token *t, t_cmd *cmd) cmd->fd_in = here_doc(t->next->content); if (cmd->fd_in == -1) { - ft_putstr_fd("minishell: heredoc error\n", 2); + ft_putstr_fd("minishell: heredoc error\n", STDERR_FILENO); cmd->error = EXIT_REDIRECTION; } else if (cmd->fd_in > EXIT_SIGNAL) @@ -141,7 +88,7 @@ static int redirect_cmd_output(t_token *t, t_cmd *cmd) if (cmd->fd_out != STDOUT_FILENO) if (close(cmd->fd_out) == -1) perror("close()"); - if (!EXPAND_AND_QUOTE_REMOVAL(t)) + if (!expand_redirection(t)) { cmd->error = EXIT_REDIRECTION; return (0); @@ -159,3 +106,32 @@ static int redirect_cmd_output(t_token *t, t_cmd *cmd) } return (1); } + +static int expand_redirection(t_token *t) +{ + t_token *head; + t_token *next_token; + int ret; + + ret = 1; + head = t; + t = t->next; + next_token = t->next; + t->next = NULL; + if (!token_expansions(t)) + { + ((t_token *)ft_lstlast((t_list *)head))->next = next_token; + return (0); + } + head->next = t->next; + free(t); + if (head->next) + head->next->id = T_REDIRECTION_WORD; // Eventuellement a integrer dans token_expansions() + if (ft_lstsize((t_list *)head->next) != 1) + { + ret = 0; + ft_putstr_fd("minishell: ambiguous redirect\n", STDERR_FILENO); // tmp message + } + ((t_token *)ft_lstlast((t_list *)head))->next = next_token; + return (ret); +} From 5c1d8f527c4316cfaa21b73c7726716c9d7e0aa0 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Sat, 11 Dec 2021 21:22:43 +0100 Subject: [PATCH 06/14] bugfix, pipeline must continue if expand_redirection() fail + ft_perror_io() delete --- headers/minishell_prototypes.h | 3 +-- srcs/exec/find_access.c | 4 ++-- srcs/generic.c | 8 +------- srcs/parsing/redirections/here_doc.c | 6 +++--- srcs/parsing/redirections/redirections.c | 12 ++++++------ 5 files changed, 13 insertions(+), 20 deletions(-) diff --git a/headers/minishell_prototypes.h b/headers/minishell_prototypes.h index 1178276..c6f57fa 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/12/10 18:46:48 by lperrey ### ########.fr */ +/* Updated: 2021/12/11 20:59:40 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -87,7 +87,6 @@ t_list *ft_lstbeforelast(t_list *lst); void *ft_lstnew_generic(size_t lst_size, size_t content_size); typedef void *(*t_dup_f)(void *); void *ft_dup_2d_arr(void *ptr, void *(*dup_func)(void *)); -void ft_perror_io(char *err_str, char *io_file); int ft_reti_perror_io(int ret, char *err_str, char *io_file); void ft_free_null(void *ptr); char *ft_getenv(char *env_var); diff --git a/srcs/exec/find_access.c b/srcs/exec/find_access.c index 22d44a1..4759b1f 100644 --- a/srcs/exec/find_access.c +++ b/srcs/exec/find_access.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */ -/* Updated: 2021/12/05 16:20:55 by lperrey ### ########.fr */ +/* Updated: 2021/12/11 20:37:43 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -103,7 +103,7 @@ static int handle_access_error(char *file_name) int tmp; tmp = errno; - ft_perror_io("minishell: ", file_name); + shell_perror(file_name, "", "", 0); errno = tmp; if (errno == EACCES) return (EXIT_CMD_NOT_FOUND); diff --git a/srcs/generic.c b/srcs/generic.c index 7e54576..6d2f03e 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/12/11 20:16:36 by lperrey ### ########.fr */ +/* Updated: 2021/12/11 20:59:40 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -200,12 +200,6 @@ void *ft_lstnew_generic(size_t lst_size, size_t content_size) return (elem); } -void ft_perror_io(char *err_str, char *io_file) -{ - ft_putstr_fd(err_str, STDERR_FILENO); - perror(io_file); -} - int ft_reti_perror_io(int ret, char *err_str, char *io_file) { ft_putstr_fd(err_str, STDERR_FILENO); diff --git a/srcs/parsing/redirections/here_doc.c b/srcs/parsing/redirections/here_doc.c index c5d5b73..f705bc8 100644 --- a/srcs/parsing/redirections/here_doc.c +++ b/srcs/parsing/redirections/here_doc.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/11 18:46:43 by lperrey #+# #+# */ -/* Updated: 2021/12/09 05:31:16 by lperrey ### ########.fr */ +/* Updated: 2021/12/11 20:42:27 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,7 +37,7 @@ int here_doc(char *delimiter) ret = here_doc_write(delimiter, here_doc); free(delimiter); if (close(here_doc) == -1) - ft_perror_io("close() ", TMP_HERE_DOC); + shell_perror(TMP_HERE_DOC, ": ", "", 0); if (ret != 0) { if (unlink(TMP_HERE_DOC) == -1) @@ -46,7 +46,7 @@ int here_doc(char *delimiter) } here_doc = open(TMP_HERE_DOC, O_RDONLY); if (here_doc == -1) - ft_perror_io("open() ", TMP_HERE_DOC); + shell_perror(TMP_HERE_DOC, ": ", "", 0); if (unlink(TMP_HERE_DOC) == -1) return (ft_reti_perror_io(-1, "unlink() ", TMP_HERE_DOC)); return (here_doc); diff --git a/srcs/parsing/redirections/redirections.c b/srcs/parsing/redirections/redirections.c index e111f56..f89eac0 100644 --- a/srcs/parsing/redirections/redirections.c +++ b/srcs/parsing/redirections/redirections.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/11 18:46:43 by lperrey #+# #+# */ -/* Updated: 2021/12/11 20:16:31 by lperrey ### ########.fr */ +/* Updated: 2021/12/11 21:15:03 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,12 +55,12 @@ static int redirect_cmd_input(t_token *t, t_cmd *cmd) if (!expand_redirection(t)) { cmd->error = EXIT_REDIRECTION; - return (0); + return (1); } cmd->fd_in = open(t->next->content, O_RDONLY); if (cmd->fd_in == -1) { - ft_perror_io("open() ", t->next->content); + shell_perror(t->next->content, ": ", "", 0); cmd->error = EXIT_REDIRECTION; } } @@ -69,7 +69,7 @@ static int redirect_cmd_input(t_token *t, t_cmd *cmd) cmd->fd_in = here_doc(t->next->content); if (cmd->fd_in == -1) { - ft_putstr_fd("minishell: heredoc error\n", STDERR_FILENO); + shell_error("heredoc error", ": ", "", 0); cmd->error = EXIT_REDIRECTION; } else if (cmd->fd_in > EXIT_SIGNAL) @@ -91,7 +91,7 @@ static int redirect_cmd_output(t_token *t, t_cmd *cmd) if (!expand_redirection(t)) { cmd->error = EXIT_REDIRECTION; - return (0); + return (1); } flags = O_WRONLY | O_CREAT; if (t->id == '>') @@ -101,7 +101,7 @@ static int redirect_cmd_output(t_token *t, t_cmd *cmd) cmd->fd_out = open(t->next->content, flags, S_IRWXU); if (cmd->fd_out == -1) { - ft_perror_io("open() ", t->next->content); + shell_perror(t->next->content, ": ", "", EXIT_REDIRECTION); cmd->error = EXIT_REDIRECTION; } return (1); From e5f033694b54ae1294993fa79072b7ebf8c6a7dc Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Wed, 15 Dec 2021 13:42:41 +0100 Subject: [PATCH 07/14] expansions refactoring WIP --- srcs/parsing/expansions/expand_token.c | 59 +++++++++++----------- srcs/parsing/expansions/expansions.c | 8 +-- srcs/parsing/parsing.c | 63 +++++++++++++----------- srcs/parsing/redirections/redirections.c | 4 +- 4 files changed, 69 insertions(+), 65 deletions(-) diff --git a/srcs/parsing/expansions/expand_token.c b/srcs/parsing/expansions/expand_token.c index 4cc2fe8..fa8d4c4 100644 --- a/srcs/parsing/expansions/expand_token.c +++ b/srcs/parsing/expansions/expand_token.c @@ -6,13 +6,13 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */ -/* Updated: 2021/12/05 18:14:24 by lperrey ### ########.fr */ +/* Updated: 2021/12/15 13:33:18 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static t_list *ret_parameter_expansion(char *content, int *i); +static char *env_var_expansion(char *content, int *i); t_list *expand_token(char *content) { @@ -30,13 +30,20 @@ t_list *expand_token(char *content) { if (content[i] == '$') { - expand->next = ret_parameter_expansion(content, &i); + expand->next = ft_lstnew(NULL); expand = expand->next; if (!expand) {//todo wrap perror("expand_token() error"); return (ft_lstclear(&head.next, free)); } + + expand->content = env_var_expansion(content, &i); + if (!expand->content) + {//todo wrap + perror("expand_token() error"); + return (ft_lstclear(&head.next, free)); + } } else { @@ -65,41 +72,35 @@ t_list *expand_token(char *content) return (head.next); } -// a voir si je retourne pas plutot un "char *". -// Malloc la lst dans la fonction est peut-être un peu superflu et pas super clair. -static t_list *ret_parameter_expansion(char *content, int *i) +static char *env_var_expansion(char *content, int *i) { - t_list *expand; + char *expansion; char *tmp; - int i_tmp; + int i_exp; - tmp = ft_calloc(ft_strlen(&content[*i]) + 1, 1); - if (!tmp) - return (NULL); - expand = ft_lstnew(NULL); - if (!expand) - return (ft_retp_free(NULL, tmp, free)); (*i)++; // skip '$' if (content[*i] == '?') { (*i)++; - expand->content = ft_itoa(get_last_exit_status()); - return (ft_retp_free(expand, tmp, free)); + expansion = ft_itoa(get_last_exit_status()); + return (expansion); } - else if (content[*i] != '_' && !ft_isalpha(content[*i])) - { - tmp[0] = '$'; - expand->content = tmp; - return (expand); - } - i_tmp = 0; + expansion = ft_calloc(ft_strlen(&content[*i - 1]) + 1, 1); // - 1 pour le premier skip + if (!expansion) + return (NULL); + expansion[0] = '$'; + if (content[*i] != '_' && !ft_isalpha(content[*i])) + return (expansion); + i_exp = 0; while (content[*i] == '_' || ft_isalnum(content[*i])) - tmp[i_tmp++] = content[(*i)++]; - expand->content = getenv(tmp); - free(tmp); - if (expand->content) - expand->content = ft_strdup(expand->content); - return (expand); + expansion[i_exp++] = content[(*i)++]; + tmp = getenv(expansion); + ft_free_null(&expansion); + if (tmp) + expansion = ft_strdup(tmp); + else // fix zob, a mieux faire + expansion = ft_calloc(1, 1);; // + return (expansion); } /* diff --git a/srcs/parsing/expansions/expansions.c b/srcs/parsing/expansions/expansions.c index f61db9b..b644740 100644 --- a/srcs/parsing/expansions/expansions.c +++ b/srcs/parsing/expansions/expansions.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */ -/* Updated: 2021/12/11 20:10:37 by lperrey ### ########.fr */ +/* Updated: 2021/12/15 00:20:47 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -70,9 +70,9 @@ int token_expansions(t_token *t) if (!tmp_split) return (0); // 4 - tmp = (char**)ft_dup_2d_arr(tmp_split, (t_dup_f)ft_strdup_quotes); - ft_free_2d_arr(tmp_split); - tmp_split = tmp; + tmp = tmp_split; + tmp_split = ft_dup_2d_arr(tmp_split, (t_dup_f)ft_strdup_quotes); + ft_free_2d_arr(tmp); if (!tmp_split) return (0); // 5 diff --git a/srcs/parsing/parsing.c b/srcs/parsing/parsing.c index 7d1b27c..35ca10a 100644 --- a/srcs/parsing/parsing.c +++ b/srcs/parsing/parsing.c @@ -6,41 +6,14 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/24 10:52:40 by lperrey #+# #+# */ -/* Updated: 2021/12/10 19:58:17 by lperrey ### ########.fr */ +/* Updated: 2021/12/12 21:18:52 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void save_redirections_words(t_token *t) -{ - while (t) - { - if (t->id == '>' || t->id == T_DGREAT - || t->id == '<' || t->id == T_DLESS) - { - t = t->next; - t->id = T_REDIRECTION_WORD; - } - t = t->next; - } -} - -void print_pipeline(t_cmd **pipeline) -{ - int i; - - i = 0; - while (pipeline[i]) - { - printf("CMD %i, fd_in=%i, fd_out=%i\n", i, pipeline[i]->fd_in, pipeline[i]->fd_out); - ft_putstr_fd(" |", 1); - print_matrix(pipeline[i]->argv, "|\n |"); - i++; - if (pipeline[i]) - ft_putstr_fd("----------------\n", 1); - } -} +void save_redirections_words(t_token *t); +void print_pipeline(t_cmd **pipeline); t_cmd **parsing(t_token *token_list) { @@ -80,6 +53,36 @@ t_cmd **parsing(t_token *token_list) return (pipeline); } +void save_redirections_words(t_token *t) +{ + while (t) + { + if (t->id == '>' || t->id == T_DGREAT + || t->id == '<' || t->id == T_DLESS) + { + t = t->next; + t->id = T_REDIRECTION_WORD; + } + t = t->next; + } +} + +void print_pipeline(t_cmd **pipeline) +{ + int i; + + i = 0; + while (pipeline[i]) + { + printf("CMD %i, fd_in=%i, fd_out=%i\n", i, pipeline[i]->fd_in, pipeline[i]->fd_out); + ft_putstr_fd(" |", 1); + print_matrix(pipeline[i]->argv, "|\n |"); + i++; + if (pipeline[i]) + ft_putstr_fd("----------------\n", 1); + } +} + /* ------------------------------------------------------- The grammar symbols ------------------------------------------------------- */ diff --git a/srcs/parsing/redirections/redirections.c b/srcs/parsing/redirections/redirections.c index f89eac0..a1f354d 100644 --- a/srcs/parsing/redirections/redirections.c +++ b/srcs/parsing/redirections/redirections.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/11 18:46:43 by lperrey #+# #+# */ -/* Updated: 2021/12/11 21:15:03 by lperrey ### ########.fr */ +/* Updated: 2021/12/11 21:32:31 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -130,7 +130,7 @@ static int expand_redirection(t_token *t) if (ft_lstsize((t_list *)head->next) != 1) { ret = 0; - ft_putstr_fd("minishell: ambiguous redirect\n", STDERR_FILENO); // tmp message + shell_error("", "", "ambiguous redirect", 0); } ((t_token *)ft_lstlast((t_list *)head))->next = next_token; return (ret); From 5a0a237aaa186d2d112d50553ba6541322aea9b7 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Thu, 16 Dec 2021 03:12:32 +0100 Subject: [PATCH 08/14] expansions refactoring done --- srcs/parsing/expansions/expand_token.c | 128 +++++++++++++++---------- 1 file changed, 76 insertions(+), 52 deletions(-) diff --git a/srcs/parsing/expansions/expand_token.c b/srcs/parsing/expansions/expand_token.c index fa8d4c4..1559d26 100644 --- a/srcs/parsing/expansions/expand_token.c +++ b/srcs/parsing/expansions/expand_token.c @@ -6,91 +6,115 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */ -/* Updated: 2021/12/15 13:33:18 by lperrey ### ########.fr */ +/* Updated: 2021/12/16 03:03:27 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static char *env_var_expansion(char *content, int *i); +static t_list *content_copy(char *content, int *i, int *quotes_state); +static t_list *content_expand(char *content, int *i); +static char *env_var_expansion(char *content, int *i); t_list *expand_token(char *content) { - int in_quotes; + int quotes_state; int i; - int i_exp; t_list head; t_list *expand; - in_quotes = 0; - i = 0; head.next = NULL; expand = &head; + quotes_state = 0; + i = 0; while (content[i]) { if (content[i] == '$') - { - expand->next = ft_lstnew(NULL); - expand = expand->next; - if (!expand) - {//todo wrap - perror("expand_token() error"); - return (ft_lstclear(&head.next, free)); - } - - expand->content = env_var_expansion(content, &i); - if (!expand->content) - {//todo wrap - perror("expand_token() error"); - return (ft_lstclear(&head.next, free)); - } - } + expand->next = content_expand(content, &i); else - { - expand->next = ft_lstnew_generic(sizeof(t_list), ft_strlen(&content[i]) + 1); - expand = expand->next; - i_exp = 0; - if (!expand) - {//todo wrap - perror("expand_token() error"); - return (ft_lstclear(&head.next, free)); - } - while (content[i] && (content[i] != '$' || in_quotes == IN_QUOTES)) - { - // quoting - if (content[i] == '\'' && in_quotes != IN_DQUOTES) - { - if (in_quotes == IN_QUOTES) - in_quotes = 0; - else - in_quotes = IN_QUOTES; - } - ((char *)expand->content)[i_exp++] = content[i++]; - } + expand->next = content_copy(content, &i, "es_state); + expand = expand->next; + if (!expand) + {//todo wrap + perror("expand_token() error"); + return (ft_lstclear(&head.next, free)); } } return (head.next); } +static t_list *content_copy(char *content, int *i, int *quotes_state) +{ + int i_exp; + t_list *expand; + + expand = ft_lstnew_generic(sizeof(t_list), ft_strlen(&content[*i]) + 1); + if (!expand) + return (NULL); + i_exp = 0; + while (content[*i] && (content[*i] != '$' || *quotes_state == IN_QUOTES)) + { + if (content[*i] == '\'' && *quotes_state != IN_DQUOTES) + { + if (*quotes_state == IN_QUOTES) + *quotes_state = 0; + else + *quotes_state = IN_QUOTES; + } + ((char *)expand->content)[i_exp++] = content[(*i)++]; + } + return (expand); +} + +static t_list *content_expand(char *content, int *i) +{ + t_list *expand; + + expand = ft_lstnew(NULL); + if (!expand) + return (NULL); + expand->content = env_var_expansion(content, i); + if (!expand->content) + { + free(expand); + return (NULL); + } + return (expand); +} + +static char *retrieve_var(char *content, int *i); + static char *env_var_expansion(char *content, int *i) { char *expansion; - char *tmp; - int i_exp; (*i)++; // skip '$' if (content[*i] == '?') { (*i)++; expansion = ft_itoa(get_last_exit_status()); - return (expansion); } - expansion = ft_calloc(ft_strlen(&content[*i - 1]) + 1, 1); // - 1 pour le premier skip + else if (content[*i] == '_' || ft_isalpha(content[*i])) + expansion = retrieve_var(content, i); + else + { + expansion = ft_calloc(1 + 1, 1); + if (!expansion) + return (NULL); + expansion[0] = '$'; + } + return (expansion); +} + +static char *retrieve_var(char *content, int *i) +{ + char *expansion; + char *tmp; + int i_exp; + + expansion = ft_calloc(ft_strlen(&content[*i - 1]) + 1, 1); // *i - 1 for '$' skip if (!expansion) return (NULL); - expansion[0] = '$'; - if (content[*i] != '_' && !ft_isalpha(content[*i])) - return (expansion); i_exp = 0; while (content[*i] == '_' || ft_isalnum(content[*i])) expansion[i_exp++] = content[(*i)++]; @@ -98,8 +122,8 @@ static char *env_var_expansion(char *content, int *i) ft_free_null(&expansion); if (tmp) expansion = ft_strdup(tmp); - else // fix zob, a mieux faire - expansion = ft_calloc(1, 1);; // + else + expansion = ft_calloc(1, 1); return (expansion); } From f53969cd4596cea8b7c586a14b38b7ffa7798302 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Thu, 16 Dec 2021 03:37:34 +0100 Subject: [PATCH 09/14] srcs files moved --- Makefile | 18 +++++++++++------- .../expansions => generic}/ft_split_MODIF.c | 0 .../expansions => generic}/ft_split_quotes.c | 0 .../expansions => generic}/ft_strdup_quotes.c | 0 srcs/{ => generic}/generic.c | 0 srcs/{ => misc}/error_wrappers.c | 0 srcs/{ => misc}/free.c | 0 srcs/{ => misc}/init.c | 0 srcs/{ => misc}/last_exit_status.c | 0 srcs/{ => misc}/retrieve_path.c | 0 srcs/{ => misc}/signals.c | 0 srcs/{ => misc}/terminal.c | 0 12 files changed, 11 insertions(+), 7 deletions(-) rename srcs/{parsing/expansions => generic}/ft_split_MODIF.c (100%) rename srcs/{parsing/expansions => generic}/ft_split_quotes.c (100%) rename srcs/{parsing/expansions => generic}/ft_strdup_quotes.c (100%) rename srcs/{ => generic}/generic.c (100%) rename srcs/{ => misc}/error_wrappers.c (100%) rename srcs/{ => misc}/free.c (100%) rename srcs/{ => misc}/init.c (100%) rename srcs/{ => misc}/last_exit_status.c (100%) rename srcs/{ => misc}/retrieve_path.c (100%) rename srcs/{ => misc}/signals.c (100%) rename srcs/{ => misc}/terminal.c (100%) diff --git a/Makefile b/Makefile index bc7bcd0..f897df3 100644 --- a/Makefile +++ b/Makefile @@ -5,11 +5,14 @@ CC = clang CFLAGS = -Wall -Wextra $(INCLUDES) -g # add -Werror, del -g VPATH = $(DIR_SRCS) -DIR_SRCS = srcs srcs/builtins \ +DIR_SRCS = srcs \ + srcs/builtins \ srcs/lexing \ srcs/parsing srcs/parsing/valid_syntax \ srcs/parsing/expansions srcs/parsing/redirections \ - srcs/exec + srcs/exec \ + srcs/generic \ + srcs/misc \ INCLUDES = -I$(HEADERS_D) -I$(LIBFT_D) @@ -25,19 +28,20 @@ LIBS = -L $(LIBFT_D) -lft \ LIBFT_D = ./libft LIBFT = $(LIBFT_D)/libft.a -SRCS = main.c init.c retrieve_path.c free.c generic.c error_wrappers.c \ - signals.c \ +SRCS = main.c \ shell_loop.c shell_script.c \ + init.c retrieve_path.c free.c \ + signals.c error_wrappers.c last_exit_status.c \ lexing.c fill_token.c check_operators.c \ parsing.c create_pipeline.c \ valid_syntax.c valid_pipeline.c valid_command.c valid_io_redirect.c \ expansions.c expand_token.c rejoin_after_expand.c new_token_for_each_field.c \ - ft_split_quotes.c ft_strdup_quotes.c \ redirections.c here_doc.c \ exec_cmd_line.c pipeline.c \ find_access.c subshell_exec.c subshell_wait.c simple_cmd_builtin.c \ - last_exit_status.c \ - cd.c pwd.c export.c unset.c exit.c env.c echo.c + cd.c pwd.c export.c unset.c exit.c env.c echo.c \ + generic.c \ + ft_split_quotes.c ft_strdup_quotes.c \ DIR_OBJS = builds OBJS = $(SRCS:%.c=$(DIR_OBJS)/%.o) diff --git a/srcs/parsing/expansions/ft_split_MODIF.c b/srcs/generic/ft_split_MODIF.c similarity index 100% rename from srcs/parsing/expansions/ft_split_MODIF.c rename to srcs/generic/ft_split_MODIF.c diff --git a/srcs/parsing/expansions/ft_split_quotes.c b/srcs/generic/ft_split_quotes.c similarity index 100% rename from srcs/parsing/expansions/ft_split_quotes.c rename to srcs/generic/ft_split_quotes.c diff --git a/srcs/parsing/expansions/ft_strdup_quotes.c b/srcs/generic/ft_strdup_quotes.c similarity index 100% rename from srcs/parsing/expansions/ft_strdup_quotes.c rename to srcs/generic/ft_strdup_quotes.c diff --git a/srcs/generic.c b/srcs/generic/generic.c similarity index 100% rename from srcs/generic.c rename to srcs/generic/generic.c diff --git a/srcs/error_wrappers.c b/srcs/misc/error_wrappers.c similarity index 100% rename from srcs/error_wrappers.c rename to srcs/misc/error_wrappers.c diff --git a/srcs/free.c b/srcs/misc/free.c similarity index 100% rename from srcs/free.c rename to srcs/misc/free.c diff --git a/srcs/init.c b/srcs/misc/init.c similarity index 100% rename from srcs/init.c rename to srcs/misc/init.c diff --git a/srcs/last_exit_status.c b/srcs/misc/last_exit_status.c similarity index 100% rename from srcs/last_exit_status.c rename to srcs/misc/last_exit_status.c diff --git a/srcs/retrieve_path.c b/srcs/misc/retrieve_path.c similarity index 100% rename from srcs/retrieve_path.c rename to srcs/misc/retrieve_path.c diff --git a/srcs/signals.c b/srcs/misc/signals.c similarity index 100% rename from srcs/signals.c rename to srcs/misc/signals.c diff --git a/srcs/terminal.c b/srcs/misc/terminal.c similarity index 100% rename from srcs/terminal.c rename to srcs/misc/terminal.c From 20c71bccbb17f50b4539ef4cdead114e5ca82a4e Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Thu, 16 Dec 2021 04:01:29 +0100 Subject: [PATCH 10/14] generic.c file split --- Makefile | 2 + srcs/generic/ft_2d_arr_func.c | 67 +++++++++ srcs/generic/ft_free_null.c | 19 +++ srcs/generic/ft_getenv.c | 70 +++++++++ srcs/generic/ft_is_posix_name.c | 29 ++++ srcs/generic/ft_isinset_str.c | 37 +++++ srcs/generic/ft_lst_func.c | 56 +++++++ srcs/generic/ft_strjoinfree.c | 41 ++++++ srcs/generic/generic.c | 252 +------------------------------- 9 files changed, 322 insertions(+), 251 deletions(-) create mode 100644 srcs/generic/ft_2d_arr_func.c create mode 100644 srcs/generic/ft_free_null.c create mode 100644 srcs/generic/ft_getenv.c create mode 100644 srcs/generic/ft_is_posix_name.c create mode 100644 srcs/generic/ft_isinset_str.c create mode 100644 srcs/generic/ft_lst_func.c create mode 100644 srcs/generic/ft_strjoinfree.c diff --git a/Makefile b/Makefile index f897df3..419d2e0 100644 --- a/Makefile +++ b/Makefile @@ -42,6 +42,8 @@ SRCS = main.c \ cd.c pwd.c export.c unset.c exit.c env.c echo.c \ generic.c \ ft_split_quotes.c ft_strdup_quotes.c \ + ft_strjoinfree.c ft_lst_func.c ft_isinset_str.c ft_is_posix_name.c \ + ft_getenv.c ft_free_null.c ft_2d_arr_func.c \ DIR_OBJS = builds OBJS = $(SRCS:%.c=$(DIR_OBJS)/%.o) diff --git a/srcs/generic/ft_2d_arr_func.c b/srcs/generic/ft_2d_arr_func.c new file mode 100644 index 0000000..39b763e --- /dev/null +++ b/srcs/generic/ft_2d_arr_func.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_2d_arr_func.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lperrey +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/08 09:25:35 by lperrey #+# #+# */ +/* Updated: 2021/12/16 03:43:33 by lperrey ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +// Replace ft_arrlen() +size_t ft_2d_arrlen(void *ptr) +{ + size_t len; + char **arr; + + arr = (char **)ptr; + len = 0; + while (arr[len] != NULL) + len++; + return (len); +} + +void *ft_dup_2d_arr(void *ptr, void *(*dup_func)(void *)) +{ + unsigned int i; + char **arr; + char **new_arr; + + new_arr = ft_calloc(ft_2d_arrlen(ptr) + 1, sizeof (void *)); + if (!new_arr) + return (NULL); + arr = (char **)ptr; + i = 0; + while (arr[i]) + { + new_arr[i] = dup_func(arr[i]); + if (!new_arr[i]) + return (ft_retp_free(NULL, new_arr, ft_free_2d_arr)); + i++; + } + return (new_arr); +} + +void *ft_resize_2d_arr(void *ptr, size_t add_nbr) +{ + unsigned int i; + char **arr; + char **new_arr; + + new_arr = ft_calloc(ft_2d_arrlen(ptr) + add_nbr + 1, sizeof (void *)); + if (!new_arr) + return (NULL); + arr = (char **)ptr; + i = 0; + while (arr[i]) + { + new_arr[i] = arr[i]; + i++; + } + free(arr); + return (new_arr); +} diff --git a/srcs/generic/ft_free_null.c b/srcs/generic/ft_free_null.c new file mode 100644 index 0000000..1c87157 --- /dev/null +++ b/srcs/generic/ft_free_null.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_free_null.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lperrey +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/08 09:25:35 by lperrey #+# #+# */ +/* Updated: 2021/12/16 03:59:21 by lperrey ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void ft_free_null(void *ptr) +{ + free(*(char **)ptr); + *(char **)ptr = NULL; +} diff --git a/srcs/generic/ft_getenv.c b/srcs/generic/ft_getenv.c new file mode 100644 index 0000000..c3c2475 --- /dev/null +++ b/srcs/generic/ft_getenv.c @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_getenv.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lperrey +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/08 09:25:35 by lperrey #+# #+# */ +/* Updated: 2021/12/16 03:44:06 by lperrey ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +/* +** Search for an environement variable, +** and return a pointer to the first character after "env_var=" . +** Return NULL if env_var not found. +*/ +char *ft_getenv(char *env_var) +{ + int i; + char *tmp; + size_t env_var_len; + + env_var_len = ft_strlen(env_var); + i = 0; + tmp = NULL; + while (!tmp && environ[i]) + { + if (environ[i][env_var_len] == '=') + { + if (ft_strncmp(environ[i], env_var, env_var_len) == 0) + tmp = &environ[i][env_var_len + 1]; + else + i++; + } + else + i++; + } + return (tmp); +} + +/* +** Like ft_getenv(), but return position of env_var instead of value. +** If env_var not found, return last position of **environ (== NULL) +*/ +size_t ft_getenv_position(char *env_var) +{ + int i; + int found; + size_t env_var_len; + + env_var_len = ft_strlen(env_var); + i = 0; + found = 0; + while (!found && environ[i]) + { + if (environ[i][env_var_len] == '=') + { + if (ft_strncmp(environ[i], env_var, env_var_len) == 0) + found = 1; + else + i++; + } + else + i++; + } + return (i); +} diff --git a/srcs/generic/ft_is_posix_name.c b/srcs/generic/ft_is_posix_name.c new file mode 100644 index 0000000..20d9df4 --- /dev/null +++ b/srcs/generic/ft_is_posix_name.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_is_posix_name.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lperrey +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/08 09:25:35 by lperrey #+# #+# */ +/* Updated: 2021/12/16 03:50:43 by lperrey ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int ft_is_posix_name(char *str) +{ + unsigned int i; + + if (str[0] != '_' && !ft_isalpha(str[0])) + return (0); + i = 1; + while (str[i]) + { + if (str[i] != '_' && !ft_isalnum(str[i])) + return (0); + i++; + } + return (i); +} diff --git a/srcs/generic/ft_isinset_str.c b/srcs/generic/ft_isinset_str.c new file mode 100644 index 0000000..d16823f --- /dev/null +++ b/srcs/generic/ft_isinset_str.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isinset_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lperrey +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/08 09:25:35 by lperrey #+# #+# */ +/* Updated: 2021/12/16 03:45:50 by lperrey ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int ft_isinset_str(char *str, char *set) +{ + size_t i; + size_t i_set; + int valid; + + i = 0; + while (str[i]) + { + valid = 0; + i_set = 0; + while (set[i_set] && !valid) + { + if (str[i] == set[i_set]) + valid = 1; + i_set++; + } + if (!valid) + return (0); + i++; + } + return (i); +} diff --git a/srcs/generic/ft_lst_func.c b/srcs/generic/ft_lst_func.c new file mode 100644 index 0000000..5835388 --- /dev/null +++ b/srcs/generic/ft_lst_func.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_func.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lperrey +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/08 09:25:35 by lperrey #+# #+# */ +/* Updated: 2021/12/16 03:45:39 by lperrey ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void ft_lstprint(t_list *lst, int fd) +{ + while (lst) + { + ft_putendl_fd(lst->content, fd); + lst = lst->next; + } +} + +t_list *ft_lstbeforelast(t_list *lst) +{ + if (!lst || !lst->next) + return (NULL); + while (lst->next->next) + lst = lst->next; + return (lst); +} + +/* if "content_size == 0", return lst with "lst->content == NULL" */ +void *ft_lstnew_generic(size_t lst_size, size_t content_size) +{ + t_list *elem; + void *content; + + if (content_size == 0) + content = NULL; + else + { + content = ft_calloc(content_size, 1); + if (!content) + return (NULL); + } + elem = ft_calloc(1, lst_size); + if (!elem) + { + free(content); + return (NULL); + } + elem->content = content; + elem->next = NULL; + return (elem); +} diff --git a/srcs/generic/ft_strjoinfree.c b/srcs/generic/ft_strjoinfree.c new file mode 100644 index 0000000..44f5ab1 --- /dev/null +++ b/srcs/generic/ft_strjoinfree.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoinfree.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lperrey +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/08 09:25:35 by lperrey #+# #+# */ +/* Updated: 2021/12/16 03:39:09 by lperrey ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +char *ft_strjoinfree(char *s1, char *s2) +{ + char *str; + + str = ft_strjoin(s1, s2); + free(s1); + free(s2); + return (str); +} + +char *ft_strjoinfree_s1(char *s1, const char *s2) +{ + char *str; + + str = ft_strjoin(s1, s2); + free(s1); + return (str); +} + +char *ft_strjoinfree_s2(const char *s1, char *s2) +{ + char *str; + + str = ft_strjoin(s1, s2); + free(s2); + return (str); +} diff --git a/srcs/generic/generic.c b/srcs/generic/generic.c index 6d2f03e..33024bc 100644 --- a/srcs/generic/generic.c +++ b/srcs/generic/generic.c @@ -6,149 +6,12 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/08 09:25:35 by lperrey #+# #+# */ -/* Updated: 2021/12/11 20:59:40 by lperrey ### ########.fr */ +/* Updated: 2021/12/16 03:50:35 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -char *ft_strjoinfree(char *s1, char *s2) -{ - char *str; - - str = ft_strjoin(s1, s2); - free(s1); - free(s2); - return (str); -} - -char *ft_strjoinfree_s1(char *s1, const char *s2) -{ - char *str; - - str = ft_strjoin(s1, s2); - free(s1); - return (str); -} - -char *ft_strjoinfree_s2(const char *s1, char *s2) -{ - char *str; - - str = ft_strjoin(s1, s2); - free(s2); - return (str); -} - -void ft_lstprint(t_list *lst, int fd) -{ - while (lst) - { - ft_putendl_fd(lst->content, fd); - lst = lst->next; - } -} - -int ft_isinset_str(char *str, char *set) -{ - size_t i; - size_t i_set; - int valid; - - i = 0; - while (str[i]) - { - valid = 0; - i_set = 0; - while (set[i_set] && !valid) - { - if (str[i] == set[i_set]) - valid = 1; - i_set++; - } - if (!valid) - return (0); - i++; - } - return (i); -} - -size_t ft_2d_arrlen(void *ptr) // Replace ft_arrlen() -{ - size_t len; - char **arr; - - arr = (char **)ptr; - len = 0; - while (arr[len] != NULL) - len++; - return (len); -} -/* -char **ft_dup_2d_char_arr(char **ptr) // Superflu si ft_dup_2d_arr() fonctionne -{ - unsigned int i; - char **arr; - char **new_arr; - - new_arr = malloc((ft_2d_arrlen(ptr) + 1) * sizeof (void *)); - if (!new_arr) - return (NULL); - arr = (char **)ptr; - i = 0; - while (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; - return (new_arr); -} */ - -// Test generic. Pas certain que ça fonctionne bien avec le pointeur sur fonction -void *ft_dup_2d_arr(void *ptr, void *(*dup_func)(void *)) -{ - unsigned int i; - char **arr; - char **new_arr; - - new_arr = ft_calloc(ft_2d_arrlen(ptr) + 1, sizeof (void *)); - if (!new_arr) - return (NULL); - arr = (char **)ptr; - i = 0; - while (arr[i]) - { - new_arr[i] = dup_func(arr[i]); - if (!new_arr[i]) - return (ft_retp_free(NULL, new_arr, ft_free_2d_arr)); - i++; - } - return (new_arr); -} - -void *ft_resize_2d_arr(void *ptr, size_t add_nbr) -{ - unsigned int i; - char **arr; - char **new_arr; - - new_arr = ft_calloc(ft_2d_arrlen(ptr) + add_nbr + 1, sizeof (void *)); - if (!new_arr) - return (NULL); - arr = (char **)ptr; - i = 0; - while (arr[i]) - { - new_arr[i] = arr[i]; - i++; - } - free(arr); - return (new_arr); -} - // pour imprimer une char ** en precisant comment separer les char * void print_matrix(char **matrix, char *sep) { @@ -166,122 +29,9 @@ void print_matrix(char **matrix, char *sep) write(1, "\n", 1); } -t_list *ft_lstbeforelast(t_list *lst) -{ - if (!lst || !lst->next) - return (NULL); - while (lst->next->next) - lst = lst->next; - return (lst); -} - -/* if "content_size == 0", return lst with "lst->content == NULL" */ -void *ft_lstnew_generic(size_t lst_size, size_t content_size) -{ - t_list *elem; - void *content; - - if (content_size == 0) - content = NULL; - else - { - content = ft_calloc(content_size, 1); - if (!content) - return (NULL); - } - elem = ft_calloc(1, lst_size); - if (!elem) - { - free(content); - return (NULL); - } - elem->content = content; - elem->next = NULL; - return (elem); -} - int ft_reti_perror_io(int ret, char *err_str, char *io_file) { ft_putstr_fd(err_str, STDERR_FILENO); perror(io_file); return (ret); } - -void ft_free_null(void *ptr) -{ - free(*(char**)ptr); - *(char**)ptr = NULL; -} - -/* -** Search for an environement variable, -** and return a pointer to the first character after "env_var=" . -** Return NULL if env_var not found. -*/ -char *ft_getenv(char *env_var) -{ - int i; - char *tmp; - size_t env_var_len; - - env_var_len = ft_strlen(env_var); - i = 0; - tmp = NULL; - while (!tmp && environ[i]) - { - if (environ[i][env_var_len] == '=') - { - if (ft_strncmp(environ[i], env_var, env_var_len) == 0) - tmp = &environ[i][env_var_len + 1]; - else - i++; - } - else - i++; - } - return (tmp); -} - -/* -** Like ft_getenv(), but return position of env_var instead of value. -** If env_var not found, return last position of **environ (== NULL) -*/ -size_t ft_getenv_position(char *env_var) -{ - int i; - int found; - size_t env_var_len; - - env_var_len = ft_strlen(env_var); - i = 0; - found = 0; - while (!found && environ[i]) - { - if (environ[i][env_var_len] == '=') - { - if (ft_strncmp(environ[i], env_var, env_var_len) == 0) - found = 1; - else - i++; - } - else - i++; - } - return (i); -} - -int ft_is_posix_name(char *str) -{ - unsigned int i; - - if (str[0] != '_' && !ft_isalpha(str[0])) - return (0); - i = 1; - while (str[i]) - { - if (str[i] != '_' && !ft_isalnum(str[i])) - return (0); - i++; - } - return (i); -} From 06f1987ae46e693f2ee65088d83ab565ea995c47 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Thu, 16 Dec 2021 05:05:25 +0100 Subject: [PATCH 11/14] small adjust in files --- Makefile | 1 - headers/minishell_prototypes.h | 7 +++--- srcs/debug.c | 35 +++++++++++++++++++++++++++ srcs/generic/generic.c | 37 ----------------------------- srcs/misc/error_wrappers.c | 9 ++++++- srcs/parsing/parsing.c | 43 +++++++++++----------------------- 6 files changed, 60 insertions(+), 72 deletions(-) create mode 100644 srcs/debug.c delete mode 100644 srcs/generic/generic.c diff --git a/Makefile b/Makefile index 419d2e0..0a3a5b0 100644 --- a/Makefile +++ b/Makefile @@ -40,7 +40,6 @@ SRCS = main.c \ exec_cmd_line.c pipeline.c \ find_access.c subshell_exec.c subshell_wait.c simple_cmd_builtin.c \ cd.c pwd.c export.c unset.c exit.c env.c echo.c \ - generic.c \ ft_split_quotes.c ft_strdup_quotes.c \ ft_strjoinfree.c ft_lst_func.c ft_isinset_str.c ft_is_posix_name.c \ ft_getenv.c ft_free_null.c ft_2d_arr_func.c \ diff --git a/headers/minishell_prototypes.h b/headers/minishell_prototypes.h index c6f57fa..8e5fb94 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/12/11 20:59:40 by lperrey ### ########.fr */ +/* Updated: 2021/12/16 04:50:31 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -72,6 +72,7 @@ typedef void (*t_free_f)(void *); // generic // Error wrappers int shell_error(char *s1, char *s2, char *s3, int ret_val); int shell_perror(char *s1, char *s2, char *s3, int ret_val); +int ft_reti_perror_io(int ret, char *err_str, char *io_file); // Generic char *ft_strjoinfree(char *s1, char *s2); @@ -82,12 +83,10 @@ int ft_isinset_str(char *str, char *set); size_t ft_2d_arrlen(void *ptr); // Replace ft_arrlen() 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); t_list *ft_lstbeforelast(t_list *lst); void *ft_lstnew_generic(size_t lst_size, size_t content_size); typedef void *(*t_dup_f)(void *); void *ft_dup_2d_arr(void *ptr, void *(*dup_func)(void *)); -int ft_reti_perror_io(int ret, char *err_str, char *io_file); void ft_free_null(void *ptr); char *ft_getenv(char *env_var); size_t ft_getenv_position(char *env_var); @@ -96,7 +95,7 @@ int ft_is_posix_name(char *str); char **ft_split_quotes(char const *s, char c); char *ft_strdup_quotes(const char *s); -// signals.c +// Signals handler void sigint_handler_interactive(int signum); void sigint_handler_heredoc(int signum); diff --git a/srcs/debug.c b/srcs/debug.c new file mode 100644 index 0000000..d3dd7e5 --- /dev/null +++ b/srcs/debug.c @@ -0,0 +1,35 @@ + +#include "minishell.h" + +// pour imprimer une char ** en precisant comment separer les char * +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_pipeline(t_cmd **pipeline) +{ + int i; + + i = 0; + while (pipeline[i]) + { + printf("CMD %i, fd_in=%i, fd_out=%i\n", i, pipeline[i]->fd_in, pipeline[i]->fd_out); + ft_putstr_fd(" |", 1); + print_matrix(pipeline[i]->argv, "|\n |"); + i++; + if (pipeline[i]) + ft_putstr_fd("----------------\n", 1); + } +} diff --git a/srcs/generic/generic.c b/srcs/generic/generic.c deleted file mode 100644 index 33024bc..0000000 --- a/srcs/generic/generic.c +++ /dev/null @@ -1,37 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* generic.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: lperrey +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2021/10/08 09:25:35 by lperrey #+# #+# */ -/* Updated: 2021/12/16 03:50:35 by lperrey ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -// pour imprimer une char ** en precisant comment separer les char * -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); -} - -int ft_reti_perror_io(int ret, char *err_str, char *io_file) -{ - ft_putstr_fd(err_str, STDERR_FILENO); - perror(io_file); - return (ret); -} diff --git a/srcs/misc/error_wrappers.c b/srcs/misc/error_wrappers.c index aef5775..7871e30 100644 --- a/srcs/misc/error_wrappers.c +++ b/srcs/misc/error_wrappers.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/12/01 17:16:30 by lperrey #+# #+# */ -/* Updated: 2021/12/05 16:26:48 by lperrey ### ########.fr */ +/* Updated: 2021/12/16 04:38:05 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,3 +43,10 @@ int shell_perror(char *s1, char *s2, char *s3, int ret_val) perror(NULL); return (ret_val); } + +int ft_reti_perror_io(int ret, char *err_str, char *io_file) +{ + ft_putstr_fd(err_str, STDERR_FILENO); + perror(io_file); + return (ret); +} diff --git a/srcs/parsing/parsing.c b/srcs/parsing/parsing.c index 35ca10a..3a9755e 100644 --- a/srcs/parsing/parsing.c +++ b/srcs/parsing/parsing.c @@ -6,14 +6,13 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/24 10:52:40 by lperrey #+# #+# */ -/* Updated: 2021/12/12 21:18:52 by lperrey ### ########.fr */ +/* Updated: 2021/12/16 04:58:12 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" void save_redirections_words(t_token *t); -void print_pipeline(t_cmd **pipeline); t_cmd **parsing(t_token *token_list) { @@ -24,32 +23,19 @@ t_cmd **parsing(t_token *token_list) // 2.9.1 - 1) Save Words save_redirections_words(token_list); - // Struct CMD alloc pipeline = pipeline_alloc(1 + count_pipes(token_list)); if (!pipeline) return (NULL); - // 2.9.1 - 2) Expansion - // TEST TOKENS PRINT -/* ft_putstr_fd("TOKENS LIST :\n-----------\n", STDERR_FILENO); - ft_lstprint((t_list *)token_list, STDERR_FILENO); */ - // if (!expansions(token_list, pipeline)) return (ft_retp_free(NULL, &pipeline, (t_free_f)free_pipeline)); - // -/* ft_putstr_fd("TOKENS LIST EXPANDED :\n-----------\n", STDERR_FILENO); - ft_lstprint((t_list *)token_list, STDERR_FILENO); */ - // 2.9.1 - 3) Redirection if (!redirections(token_list, pipeline)) return (ft_retp_free(NULL, &pipeline, (t_free_f)free_pipeline)); - // Struct CMD fill if (!pipeline_fill_argv(token_list, pipeline)) return (ft_retp_free(NULL, &pipeline, (t_free_f)free_pipeline)); - //print_pipeline(pipeline); - return (pipeline); } @@ -67,21 +53,20 @@ void save_redirections_words(t_token *t) } } -void print_pipeline(t_cmd **pipeline) -{ - int i; +/* + 2.9.1 Simple Commands + 2.9.1 - 1) Save Words + 2.9.1 - 2) Expansion + 2.9.1 - 3) Redirection +https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html +#tag_18_09_01 +*/ - i = 0; - while (pipeline[i]) - { - printf("CMD %i, fd_in=%i, fd_out=%i\n", i, pipeline[i]->fd_in, pipeline[i]->fd_out); - ft_putstr_fd(" |", 1); - print_matrix(pipeline[i]->argv, "|\n |"); - i++; - if (pipeline[i]) - ft_putstr_fd("----------------\n", 1); - } -} +/* + 2.10 Shell Grammar +https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html +#tag_18_10 +*/ /* ------------------------------------------------------- The grammar symbols From 81cc6fbff6c49467463b3efb28f26a86c489581d Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Thu, 16 Dec 2021 06:04:03 +0100 Subject: [PATCH 12/14] handle_access_error() exit status --- srcs/exec/find_access.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/srcs/exec/find_access.c b/srcs/exec/find_access.c index 4759b1f..13e1b2b 100644 --- a/srcs/exec/find_access.c +++ b/srcs/exec/find_access.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */ -/* Updated: 2021/12/11 20:37:43 by lperrey ### ########.fr */ +/* Updated: 2021/12/16 05:57:42 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -103,10 +103,11 @@ static int handle_access_error(char *file_name) int tmp; tmp = errno; - shell_perror(file_name, "", "", 0); + shell_perror(file_name, ": ", "", 0); errno = tmp; if (errno == EACCES) + return (EXIT_CMD_NOT_EXE); + else if (errno == ENOENT) return (EXIT_CMD_NOT_FOUND); return (1); - // 126 / EXIT_CMD_NOT_EXE } From e5852579ef834ae347fb605d4c5f5bfec630d134 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Thu, 16 Dec 2021 06:15:26 +0100 Subject: [PATCH 13/14] submodule minishell_tests --- minishell_tests | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/minishell_tests b/minishell_tests index 57cff22..427cec8 160000 --- a/minishell_tests +++ b/minishell_tests @@ -1 +1 @@ -Subproject commit 57cff2229388be8cf2ad210859b4677a15b3b760 +Subproject commit 427cec8cb4dc9cbd7fd963a443160bdd31683519 From 59f0b7603b3061cfd359e592068376447d69671e Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Thu, 16 Dec 2021 06:39:29 +0100 Subject: [PATCH 14/14] deleted terminal.c --- headers/minishell_prototypes.h | 5 +-- headers/minishell_structs.h | 7 +--- srcs/misc/free.c | 4 +- srcs/misc/terminal.c | 76 ---------------------------------- 4 files changed, 3 insertions(+), 89 deletions(-) delete mode 100644 srcs/misc/terminal.c diff --git a/headers/minishell_prototypes.h b/headers/minishell_prototypes.h index 8e5fb94..b9d2944 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/12/16 04:50:31 by lperrey ### ########.fr */ +/* Updated: 2021/12/16 06:35:04 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,9 +20,6 @@ extern char **environ; int init(t_all *c); char *init_prompt(char *prompt_base); char **retrieve_path(void); -int set_terminal_attributes(struct termios *ori_termios, - struct termios *interactive_termios, - int *termios_changed); // WIP, TEST, TEMP, PLACEHOLDER, NOT IMPORTANT, :) void set_signals_behaviour(void); // Shell modes diff --git a/headers/minishell_structs.h b/headers/minishell_structs.h index 69b5f79..8fe4e6b 100644 --- a/headers/minishell_structs.h +++ b/headers/minishell_structs.h @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/08 02:35:52 by lperrey #+# #+# */ -/* Updated: 2021/12/01 16:49:37 by lperrey ### ########.fr */ +/* Updated: 2021/12/16 06:34:29 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,11 +42,6 @@ typedef struct s_all char *prompt_base; char *prompt; t_token *token_list; -// struct termios ori_termios; -// struct termios interactive_termios; -// int termios_changed; -// struct sigaction ori_signal_behaviour; -// struct sigaction signal_behaviour; } t_all; #endif diff --git a/srcs/misc/free.c b/srcs/misc/free.c index 303a182..0e8c95b 100644 --- a/srcs/misc/free.c +++ b/srcs/misc/free.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/10 23:53:17 by lperrey #+# #+# */ -/* Updated: 2021/12/02 00:42:33 by lperrey ### ########.fr */ +/* Updated: 2021/12/16 06:35:03 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,8 +20,6 @@ int exit_free(t_all *c, int exit_status) ft_free_2d_arr(environ); ft_free_2d_arr(c->path); free_pipeline(&c->pipeline); - //if (c->termios_changed) - // tcsetattr(STDIN_FILENO, TCSANOW, &c->ori_termios); rl_clear_history(); close_stdio(); exit(exit_status); diff --git a/srcs/misc/terminal.c b/srcs/misc/terminal.c deleted file mode 100644 index b33f969..0000000 --- a/srcs/misc/terminal.c +++ /dev/null @@ -1,76 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* terminal.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: lperrey +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2021/10/27 00:10:04 by lperrey #+# #+# */ -/* Updated: 2021/10/30 14:17:16 by lperrey ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -#define CTRL_C 03 -#define CTRL_D 04 -#define CTRL_BACKSLASH 034 - -int set_terminal_attributes(struct termios *ori_termios, - struct termios *interactive_termios, - int *termios_changed) -{ - tcgetattr(STDIN_FILENO, ori_termios); - *interactive_termios = *ori_termios; - - interactive_termios->c_cc[VINTR] = CTRL_D; - interactive_termios->c_cc[VEOF] = CTRL_C; - - //interactive_termios->c_cc[VQUIT] = CTRL_C; - //interactive_termios->c_cc[VEOF] = CTRL_BACKSLASH; - - //interactive_termios->c_cc[VEOL] = CTRL_C; - - *termios_changed = 1; - tcsetattr(STDIN_FILENO, TCSANOW, interactive_termios); - - return (1); -} - - //printf("STDIN_FILENO = %s\n ", ttyname(STDIN_FILENO)); - //printf("STDOUT_FILENO = %s\n ", ttyname(STDOUT_FILENO)); - //printf("STDERR_FILENO = %s\n ", ttyname(STDERR_FILENO)); - //ft_putendl_fd(ttyname(STDIN_FILENO), 1); - //ft_putendl_fd(ttyname(STDOUT_FILENO), 1); - //ft_putendl_fd(ttyname(STDERR_FILENO), 1); - // ft_printf("BEFORE\n"); - // ft_printf("i_io.c_cc[VEOF] = %i\ni_termios.c_cc[VINTR] = %i\n", (*interactive_termios)->c_cc[VEOF], (*interactive_termios)->c_cc[VINTR]); - // ft_printf("o_io.c_cc[VEOF] = %i\no_termios.c_cc[VINTR] = %i\n", (*ori_termios)->c_cc[VEOF], (*ori_termios)->c_cc[VINTR]); - // ft_printf("AFTER\n"); - // ft_printf("i_io.c_cc[VEOF] = %i\ni_termios.c_cc[VINTR] = %i\n", (*interactive_termios)->c_cc[VEOF], (*interactive_termios)->c_cc[VINTR]); - // ft_printf("o_io.c_cc[VEOF] = %i\no_termios.c_cc[VINTR] = %i\n", (*ori_termios)->c_cc[VEOF], (*ori_termios)->c_cc[VINTR]); - -void wip_test() -{ - char term_desc[2048]; - char *term_type; - int term_width; - int term_height; - int ret; - - term_type = getenv("TERM"); - if (term_type == 0) - ft_putstr_fd("Specify a terminal type with `setenv TERM '.\n", 2); - ret = tgetent(term_desc, term_type); - if (ret < 0) - ft_putstr_fd("Could not access the termcap data base.\n", 2); - if (ret == 0) - ft_putstr_fd("Terminal type `%s' is not defined.\n", 2); - term_height = tgetnum ("li"); - term_width = tgetnum ("co"); - /* Extract information that termcap functions use. */ -/* temp = tgetstr ("pc", BUFFADDR); - PC = temp ? *temp : 0; - BC = tgetstr ("le", BUFFADDR); - UP = tgetstr ("up", BUFFADDR); */ -}