From 55808527910f8ed0d42a41719d1531e2ab257abb Mon Sep 17 00:00:00 2001 From: tomoron Date: Tue, 2 Apr 2024 12:32:18 +0200 Subject: [PATCH] =?UTF-8?q?wip=20(=C3=A7a=20compile=20pas)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- srcs/cd.c | 6 +-- srcs/commands.c | 19 +++---- srcs/debug.c | 14 +---- srcs/echo.c | 17 +++---- srcs/exec_bonus.c | 5 +- srcs/exit.c | 9 ++-- srcs/export.c | 11 ++-- srcs/input_redirections.c | 43 +++++++++------- srcs/lst_token.c | 5 +- srcs/minishell.h | 22 ++++---- srcs/output_redirections.c | 42 +++++++-------- srcs/parsing.c | 36 ++----------- srcs/parsing_bonus.c | 101 +++++++++++++++++++++---------------- srcs/pipe.c | 9 ++-- 14 files changed, 155 insertions(+), 184 deletions(-) diff --git a/srcs/cd.c b/srcs/cd.c index 85df981..705e2f0 100644 --- a/srcs/cd.c +++ b/srcs/cd.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/16 21:02:54 by marde-vr #+# #+# */ -/* Updated: 2024/03/27 16:21:48 by tomoron ### ########.fr */ +/* Updated: 2024/04/02 00:43:31 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,13 +16,13 @@ int cd(t_token *args) { char *new_wd; - if (args->next && args->next->next && args->next->next->type == ARG) + if (args->next && args->next->next) { ft_printf_fd(2, "minishell: cd: too many arguments\n"); g_return_code = 1; return (1); } - if (!args->next || args->next->type != ARG) + if (!args->next) new_wd = getenv("HOME"); else new_wd = args->next->value; diff --git a/srcs/commands.c b/srcs/commands.c index b6daee8..df38cb2 100644 --- a/srcs/commands.c +++ b/srcs/commands.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:22:15 by marde-vr #+# #+# */ -/* Updated: 2024/04/01 20:01:21 by marde-vr ### ########.fr */ +/* Updated: 2024/04/02 02:02:02 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,13 +19,13 @@ int get_cmd_count(t_token *cmds) count = 0; cur_cmd = cmds; - while (cur_cmd->next != 0) + while (cur_cmd->next) { - if (cur_cmd->type != PIPE) + if (/*cur_cmd->type != PIPE*/ 1) count++; cur_cmd = cur_cmd->next; } - if (cur_cmd->type != PIPE) + if (/*cur_cmd->type != PIPE */ 1) count++; return (count); } @@ -37,17 +37,12 @@ int get_args_count(t_token *cmds) count = 0; cur_cmd = cmds; - if (cur_cmd->type == ARG) + if (cur_cmd) count++; while (cur_cmd->next) { - if (/*cur_cmd->type == PIPE*/ 0) - break ; cur_cmd = cur_cmd->next; - if (cur_cmd->type == ARG) - count++; - else if (/*cur_cmd->type != PIPE*/ 1) - cur_cmd = cur_cmd->next; + count++; } return (count); } @@ -67,7 +62,7 @@ char **get_cmd_args(t_msh *msh) i = 0; while (i < args_count) { - if (cur_cmd->type == ARG) + if (cur_cmd) { if (!i) cmd_args[i++] = remove_path(cur_cmd->value); diff --git a/srcs/debug.c b/srcs/debug.c index 15a5c67..fbb2503 100644 --- a/srcs/debug.c +++ b/srcs/debug.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/18 15:46:50 by tomoron #+# #+# */ -/* Updated: 2024/04/01 20:07:08 by tomoron ### ########.fr */ +/* Updated: 2024/04/02 00:45:25 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,17 +17,7 @@ void print_parsed_token(t_token *token) while (token) { printf("["); - if (token->type == ARG) - printf("ARG : \"%s\"", token->value); - else if (token->type == RED_O) - printf("RED_O"); - else if (token->type == RED_O_APP) - printf("RED_O_APP"); - else if (token->type == RED_I) - printf("RED_I"); - else if (token->type == HERE_DOC) - printf("HERE_DOC"); - printf("] "); + printf("ARG : \"%s\"", token->value); token = token->next; } printf("\n"); diff --git a/srcs/echo.c b/srcs/echo.c index db2f851..2f1c08f 100755 --- a/srcs/echo.c +++ b/srcs/echo.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/07 15:30:37 by tomoron #+# #+# */ -/* Updated: 2024/03/28 13:21:05 by tomoron ### ########.fr */ +/* Updated: 2024/04/02 00:44:56 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,17 +18,12 @@ void put_args(t_token *args) int first; first = 1; - while (args && args->type == ARG) + while (args) { - if (args->type != ARG) - args = args->next; - else - { - if (!first) - ft_putchar_fd(' ', STDOUT_FILENO); - ft_putstr_fd(args->value, STDOUT_FILENO); - first = 0; - } + if (!first) + ft_putchar_fd(' ', STDOUT_FILENO); + ft_putstr_fd(args->value, STDOUT_FILENO); + first = 0; args = args->next; } } diff --git a/srcs/exec_bonus.c b/srcs/exec_bonus.c index 541d9e9..4f22941 100644 --- a/srcs/exec_bonus.c +++ b/srcs/exec_bonus.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */ -/* Updated: 2024/04/01 21:56:12 by tomoron ### ########.fr */ +/* Updated: 2024/04/02 00:09:50 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -117,7 +117,8 @@ void end_execution(t_msh *msh, int cmd_count) if (!g_return_code && WIFEXITED(status)) g_return_code = WEXITSTATUS(status); if (WIFSIGNALED(status) && WTERMSIG(status) == SIGQUIT) - printf("Quit (core dumped)\n"); + printf("Quit (core dumped)\n"); + //TODO: (core dumped) WCOREDUMP free(msh->pids); msh->pids = 0; //signal(SIGINT, signal_handler_interactive); //enables ctrl-C diff --git a/srcs/exit.c b/srcs/exit.c index fe6103c..50c50d0 100755 --- a/srcs/exit.c +++ b/srcs/exit.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */ -/* Updated: 2024/04/01 20:07:47 by marde-vr ### ########.fr */ +/* Updated: 2024/04/02 00:42:19 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,9 +25,9 @@ void get_exit_bt_return_code(t_msh *msh, int *exit_code) t_token *cur_cmd; cur_cmd = msh->cmds->next; - if (cur_cmd && cur_cmd->type == ARG && !ft_strisnbr(cur_cmd->value)) + if (cur_cmd && !ft_strisnbr(cur_cmd->value)) numeric_arg_err(cur_cmd->value, exit_code); - else if (cur_cmd && cur_cmd->type == ARG) + else if (cur_cmd) *exit_code = (unsigned char)ft_atoi(cur_cmd->value); else *exit_code = g_return_code; @@ -40,8 +40,7 @@ int exit_bt(t_msh *msh) cur_cmd = msh->cmds->next; ft_printf("exit\n"); - if (cur_cmd && cur_cmd->next && cur_cmd->next->type == ARG - && ft_strisnbr(cur_cmd->value)) + if (cur_cmd && cur_cmd->next && ft_strisnbr(cur_cmd->value)) ft_putstr_fd("minishell: exit: too many arguments\n", 2); else { diff --git a/srcs/export.c b/srcs/export.c index 4227597..fd4d184 100644 --- a/srcs/export.c +++ b/srcs/export.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/18 18:29:20 by marde-vr #+# #+# */ -/* Updated: 2024/04/01 13:20:34 by marde-vr ### ########.fr */ +/* Updated: 2024/04/02 02:16:18 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,10 +45,9 @@ int ft_export(t_msh *msh) int len; cmd = msh->cmds; - if (cmd && (!cmd->next || (cmd->next && cmd->next->type != ARG))) + if (cmd && cmd->next) print_env_declare(msh->env); - if (cmd && cmd->next && cmd->next->type == ARG && (!cmd->next->next - || (cmd->next->next && cmd->next->next->type != ARG))) + if (cmd && cmd->next && !cmd->next->next) { arg = cmd->next->value; len = 0; @@ -62,6 +61,8 @@ int ft_export(t_msh *msh) value = ft_strdup(arg + len); msh->env = env_add_back(msh->env, name, value); } +//export += +//replacer si ça existe deja sauf si il y a pas de = return (0); } @@ -97,7 +98,7 @@ int ft_unset(t_msh *msh) cmd = msh->cmds; if (cmd) cmd = cmd->next; - while (cmd && cmd->type == ARG) + while (cmd) { delete_from_env(msh, cmd->value); cmd = cmd->next; diff --git a/srcs/input_redirections.c b/srcs/input_redirections.c index dd0933c..296c26e 100644 --- a/srcs/input_redirections.c +++ b/srcs/input_redirections.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:15:27 by marde-vr #+# #+# */ -/* Updated: 2024/04/01 20:08:48 by marde-vr ### ########.fr */ +/* Updated: 2024/04/02 02:13:50 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,11 +27,11 @@ void redirect_input(t_msh *msh) } } -void open_input_file(t_msh *msh, t_token **cur_token) +void open_input_file(t_msh *msh, t_cmd **cur_token) { - if ((*cur_token)->type == HERE_DOC) + if ((*cur_token)->cmd_type == HERE_DOC) handle_here_doc(msh, (*cur_token)->next->value); - if ((*cur_token)->type == RED_I) + if ((*cur_token)->cmd_type == RED_I) { if (msh->in_fd != 0) close(msh->in_fd); @@ -45,34 +45,39 @@ void open_input_file(t_msh *msh, t_token **cur_token) } } -void get_in_type(t_msh *msh, t_token *tokens) +void get_in_type(t_msh *msh, t_cmd *tokens) { - t_token *cur_token; + t_cmd *cur_token; cur_token = tokens; - while (cur_token && cur_token->next && cur_token->type == ARG) + while (cur_token && cur_token->next && cur_token->cmd_type == CMD) cur_token = cur_token->next; - if (cur_token->type) + // if (cur_token->type) + //{ + // msh->in_type = cur_token->type; + // if (cur_token->type == HERE_DOC || cur_token->type == RED_I) + // open_input_file(msh, &cur_token); + //} pas sur de ce que c'est censé faire , j'ai fais un truc différent en dessous + if (cur_token->cmd_type == HERE_DOC || cur_token->cmd_type == RED_I) { - msh->in_type = cur_token->type; - if (cur_token->type == HERE_DOC || cur_token->type == RED_I) - open_input_file(msh, &cur_token); + msh->in_type = cur_token->cmd_type; + open_input_file(msh, &cur_token); } - while (cur_token && cur_token->next && cur_token->next->type == ARG) + while (cur_token && cur_token->next && cur_token->next->cmd_type == CMD) cur_token = cur_token->next; - if (cur_token->next && (cur_token->next->type == HERE_DOC - || cur_token->next->type == RED_I)) + if (cur_token->next && (cur_token->next->cmd_type == HERE_DOC + || cur_token->next->cmd_type == RED_I)) get_in_type(msh, cur_token); } -int first_is_in_type(t_msh *msh) +int first_is_in_type(t_cmd *cmd) { - t_token *cur_token; + t_cmd *cur_token; - cur_token = msh->cmds; - while (cur_token && cur_token->type == ARG && cur_token->next) + cur_token = cmd; + while (cur_token && cur_token->cmd_type == CMD && cur_token->next) cur_token = cur_token->next; - if (cur_token->type == RED_I || cur_token->type == HERE_DOC) + if (cur_token->cmd_type == RED_I || cur_token->cmd_type == HERE_DOC) return (1); return (0); } diff --git a/srcs/lst_token.c b/srcs/lst_token.c index eee029f..d0ded65 100755 --- a/srcs/lst_token.c +++ b/srcs/lst_token.c @@ -6,13 +6,13 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */ -/* Updated: 2024/03/28 12:34:31 by tomoron ### ########.fr */ +/* Updated: 2024/04/02 00:44:16 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -t_token *token_add_back(t_token *token, char *value, t_token_type type) +t_token *token_add_back(t_token *token, char *value) { t_token *res; t_token *current; @@ -26,7 +26,6 @@ t_token *token_add_back(t_token *token, char *value, t_token_type type) if (!res) return (token); res->value = value; - res->type = type; if (!token) return (res); current = token; diff --git a/srcs/minishell.h b/srcs/minishell.h index d1ff68c..8a3a858 100755 --- a/srcs/minishell.h +++ b/srcs/minishell.h @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */ -/* Updated: 2024/04/01 21:56:27 by tomoron ### ########.fr */ +/* Updated: 2024/04/02 02:07:36 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,7 +31,11 @@ typedef enum e_cmd_type PAREN, AND, OR, - PIPE + PIPE, + RED_O_APP, + HERE_DOC, + RED_O, + RED_I } t_cmd_type; typedef struct s_cmd @@ -60,8 +64,8 @@ typedef struct s_msh t_env *env; t_token *cmds; int *pids; - t_token_type in_type; - t_token_type out_type; + t_cmd_type in_type; + t_cmd_type out_type; int in_fd; int out_fd; int locked_return_code; @@ -70,7 +74,7 @@ typedef struct s_msh extern int g_return_code; -t_token *token_add_back(t_token *res, char *token, t_token_type type); +t_token *token_add_back(t_token *res, char *token); t_cmd *cmd_add_back(t_cmd *res, char *cmd, t_cmd_type type); void *here_doc_variables(int write, int index, void *data); int add_var_to_str(char *res, char **command, t_env *env); @@ -82,9 +86,9 @@ void child(t_msh *msh, char **cmd_args, int i); t_token *parse_command(char *command, t_env *env); void parent(t_msh *msh, int i, int cmd_count); char *ft_get_env(t_env *env, char *var_name); -void get_out_type(t_msh *msh, t_token *cmds); +void get_out_type(t_msh *msh, t_cmd *cmds); void handle_here_doc(t_msh *msh, char *eof); -void get_in_type(t_msh *msh, t_token *tokens); +void get_in_type(t_msh *msh, t_cmd *tokens); void signal_handler_interactive(int signum); int get_token_len(char *cmd, t_env *env); void signal_handler_here_doc(int signum); @@ -95,7 +99,7 @@ void remove_command_from_msh(t_msh *msh); void ft_exit(t_msh *msh, int error_code); void signal_handler_command(int signum); void ft_exit(t_msh *msh, int exit_code); -void redirect_output(t_msh *msh); +void redirect_output(t_msh *msh, int i); char **split_paths_from_env(t_env *env); int add_return_code_to_str(char *res); void redirect_input(t_msh *msh); @@ -109,7 +113,7 @@ int get_args_count(t_token *cmds); char **env_to_char_tab(t_env *env); void print_parsed_cmd(t_cmd *cmd);//debug int get_cmd_count(t_token *cmds); -int first_is_in_type(t_msh *msh); +int first_is_in_type(t_cmd *cmd); int contains_newline(char *str); int check_var_name(char *name); char **get_cmd_args(t_msh *msh); diff --git a/srcs/output_redirections.c b/srcs/output_redirections.c index 5157e45..4257b6a 100644 --- a/srcs/output_redirections.c +++ b/srcs/output_redirections.c @@ -6,15 +6,16 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 19:10:52 by marde-vr #+# #+# */ -/* Updated: 2024/04/01 20:09:27 by marde-vr ### ########.fr */ +/* Updated: 2024/04/02 02:08:43 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void redirect_output(t_msh *msh) +void redirect_output(t_msh *msh, int i) { - if (/*msh->out_type != PIPE*/ 1) + (void)i; + if (msh->out_type != PIPE) { if (dup2(msh->out_fd, 1) < 0) ft_exit(msh, 1); @@ -26,48 +27,43 @@ void redirect_output(t_msh *msh) } } -void open_out_file(t_msh *msh, t_token **cur_cmd) +void open_out_file(t_msh *msh, t_cmd **cur_cmd) { - msh->out_type = (*cur_cmd)->type; + msh->out_type = (*cur_cmd)->cmd_type; if (msh->out_type == RED_O) msh->out_fd = open((*cur_cmd)->next->value, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (msh->out_type == RED_O_APP) - msh->out_fd = open((*cur_cmd)->next->value, - O_CREAT | O_RDWR | O_APPEND, 0644); + msh->out_fd = open((*cur_cmd)->next->value, O_CREAT | O_RDWR | O_APPEND, + 0644); if (msh->out_fd == -1) { g_return_code = 1; perror("open"); return ; } - if (/*(*cur_cmd)->type != PIPE*/ 1) + if ((*cur_cmd)->cmd_type != PIPE) { - while ((*cur_cmd)->next && (*cur_cmd)->next->type == ARG) + while ((*cur_cmd)->next && (*cur_cmd)->next->cmd_type == CMD) *cur_cmd = (*cur_cmd)->next; - if ((*cur_cmd)->next && ((*cur_cmd)->next->type == RED_O - || (*cur_cmd)->next->type == RED_O_APP)) + if ((*cur_cmd)->next && ((*cur_cmd)->next->cmd_type == RED_O + || (*cur_cmd)->next->cmd_type == RED_O_APP)) get_out_type(msh, *cur_cmd); } } -void get_out_type(t_msh *msh, t_token *cmds) +void get_out_type(t_msh *msh, t_cmd *cmds) { - t_token *cur_cmd; + t_cmd *cur_cmd; - msh->out_type = ARG; + msh->out_type = CMD; msh->out_fd = 0; cur_cmd = cmds; - if (cmds->type && msh->cmds == cmds) - { - while (msh->cmds->type != ARG && msh->cmds->next->next) - msh->cmds = msh->cmds->next->next; - } - while (cur_cmd && cur_cmd->next && (cur_cmd->type == ARG - || cur_cmd->type > 3)) + while (cur_cmd && cur_cmd->next && cur_cmd->cmd_type != AND + && cur_cmd->cmd_type != OR) cur_cmd = cur_cmd->next; - if (!cur_cmd->type) - msh->out_type = ARG; + if (cur_cmd->cmd_type == CMD || cur_cmd->cmd_type == PAREN) + msh->out_type = 0; else open_out_file(msh, &cur_cmd); } diff --git a/srcs/parsing.c b/srcs/parsing.c index ccc5e0a..78096c1 100755 --- a/srcs/parsing.c +++ b/srcs/parsing.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/09 15:26:01 by tomoron #+# #+# */ -/* Updated: 2024/03/29 14:31:04 by tomoron ### ########.fr */ +/* Updated: 2024/04/02 00:42:36 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" @@ -58,50 +58,22 @@ char *get_token(char **cmd, int *in_quote, int *in_dquote, t_env *env) return (res); } -t_token_type get_token_type(char **command) -{ - t_token_type res; - - while (ft_isspace(**command)) - (*command)++; - if ((*command)[0] == '>' && (*command)[1] == '>') - res = RED_O_APP; - else if ((*command)[0] == '<' && (*command)[1] == '<') - res = HERE_DOC; - else if ((*command)[0] == '>') - res = RED_O; - else if ((*command)[0] == '<') - res = RED_I; - else - res = ARG; - if (res == RED_O_APP || res == HERE_DOC) - (*command) += 2; - if (res == RED_O || res == RED_I) - (*command)++; - return (res); -} - t_token *parse_command(char *command, t_env *env) { int in_quote; int in_dquote; t_token *res; char *value; - t_token_type type; in_quote = 0; in_dquote = 0; res = 0; while (command && *command) { - type = get_token_type(&command); - if (type == ARG) - value = get_token(&command, &in_quote, &in_dquote, env); - else - value = 0; - if (type == ARG && value == 0) + value = get_token(&command, &in_quote, &in_dquote, env); + if (!value) return (free_token(res)); - res = token_add_back(res, value, type); + res = token_add_back(res, value); while (ft_isspace(*command)) command++; } diff --git a/srcs/parsing_bonus.c b/srcs/parsing_bonus.c index 8f8a83f..5827f73 100644 --- a/srcs/parsing_bonus.c +++ b/srcs/parsing_bonus.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/27 14:40:44 by tomoron #+# #+# */ -/* Updated: 2024/04/01 20:04:55 by tomoron ### ########.fr */ +/* Updated: 2024/04/02 02:08:14 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,37 +40,43 @@ t_cmd_type get_cmd_type_bonus(char **cmd) { t_cmd_type res; - while (ft_isspace(**cmd)) - (*cmd)++; if (**cmd == '|' && (*cmd)[1] != '|') res = PIPE; else if (**cmd == '|' && (*cmd)[1] == '|') res = OR; else if (**cmd == '&' && (*cmd)[1] == '&') res = AND; + else if (**cmd == '>' && (*cmd)[1] == '>') + res = RED_O_APP; + else if (**cmd == '<' && (*cmd)[1] == '<') + res = HERE_DOC; + else if (**cmd == '>') + res = RED_O; + else if (**cmd == '<') + res = RED_I; else if (**cmd == '(') res = PAREN; else res = CMD; - if(res != CMD) + if (res != CMD) (*cmd)++; - if (res == OR || res == AND) + if (res == OR || res == AND || res == RED_O_APP || res == HERE_DOC) (*cmd)++; return (res); } -int get_parenthesis_cmd_len(char *cmd) +int get_parenthesis_cmd_len(char *cmd) { - int len; - int parenthesis; - int in_quote; - int in_dquote; + int len; + int parenthesis; + int in_quote; + int in_dquote; len = 0; parenthesis = 1; in_quote = 0; in_dquote = 0; - while(cmd[len] && parenthesis) + while (cmd[len] && parenthesis) { if (cmd[len] == '\'' && !in_dquote) in_quote = !in_quote; @@ -80,20 +86,21 @@ int get_parenthesis_cmd_len(char *cmd) parenthesis += 1 * (-(cmd[len] == ')')); len++; } - return(len - 1); + return (len - 1); } -int get_normal_cmd_len(char *cmd) +int get_normal_cmd_len(char *cmd) { - int len; - int in_quote; - int in_dquote; + int len; + int in_quote; + int in_dquote; len = 0; in_quote = 0; in_dquote = 0; - while(cmd[len] && (in_quote || in_dquote || (cmd[len] != '|' && - cmd[len] != '&' && cmd[len] != '(' && cmd[len] != ')'))) + while (cmd[len] && (in_quote || in_dquote || (cmd[len] != '|' + && cmd[len] != '&' && cmd[len] != '(' && cmd[len] != ')' + && cmd[len] != '<' && cmd[len] != '>'))) { if (cmd[len] == '\'' && !in_dquote) in_quote = !in_quote; @@ -101,36 +108,35 @@ int get_normal_cmd_len(char *cmd) in_dquote = !in_dquote; len++; } - return(len); + return (len); } -char *get_cmd_value(char **cmd , t_cmd_type type) -{ - int len; - char *res; +char *get_cmd_value(char **cmd, t_cmd_type type) +{ + int len; + char *res; if (type == PAREN) len = get_parenthesis_cmd_len(*cmd); else - len = get_normal_cmd_len(*cmd); + len = get_normal_cmd_len(*cmd); res = ft_substr(*cmd, 0, len); (*cmd) += len; - if(type == PAREN) + if (type == PAREN) (*cmd)++; - return(res); + return (res); } void print_syntax_error_bonus(t_cmd *cmd) { if (cmd->cmd_type == CMD || cmd->cmd_type == PAREN) - return; - + return ; ft_printf_fd(2, "minishell : syntax error near unexpected token `"); - if(cmd->cmd_type == AND) - ft_printf_fd(2, "AND"); - if(cmd->cmd_type == OR) - ft_printf_fd(2, "OR"); - if(cmd->cmd_type == PIPE) - ft_printf_fd(2, "PIPE"); + if (cmd->cmd_type == AND) + ft_printf_fd(2, "&&"); + if (cmd->cmd_type == OR) + ft_printf_fd(2, "||"); + if (cmd->cmd_type == PIPE) + ft_printf_fd(2, "|"); ft_printf_fd(2, "'\n"); } @@ -139,24 +145,27 @@ t_cmd *check_cmds_syntax(t_cmd *cmds) t_cmd_type last; t_token *token; - if(cmds->cmd_type == OR || cmds->cmd_type == AND || cmds->cmd_type == PIPE) - return(cmds); + if (cmds->cmd_type == OR || cmds->cmd_type == AND || cmds->cmd_type == PIPE) + return (cmds); last = cmds->cmd_type; cmds = cmds->next; - while(cmds) + while (cmds) { - if(cmds->cmd_type == OR || cmds->cmd_type == AND || cmds->cmd_type == PIPE) - if((last != CMD && last != PAREN) || (!cmds->next && cmds->next->cmd_type != CMD && cmds->next->cmd_type != PAREN)) - return(cmds); - if(cmds->cmd_type == CMD || cmds->cmd_type == PAREN) + if (cmds->cmd_type == OR || cmds->cmd_type == AND + || cmds->cmd_type == PIPE) + if ((last != CMD && last != PAREN) || (!cmds->next + && cmds->next->cmd_type != CMD + && cmds->next->cmd_type != PAREN)) + return (cmds); + if (cmds->cmd_type == CMD || cmds->cmd_type == PAREN) { token = parse_command(cmds->value, 0); - if(!token) - return(cmds); + if (!token) + return (cmds); free_token(token); } } - return(0); + return (0); } t_cmd *parsing_bonus(char *cmd) @@ -170,9 +179,13 @@ t_cmd *parsing_bonus(char *cmd) return (0); while (*cmd) { + while (ft_isspace(*cmd)) + cmd++; type = get_cmd_type_bonus(&cmd); if (type == CMD || type == PAREN) value = get_cmd_value(&cmd, type); + else if (type == RED_O || type == RED_O_APP) + value = 0; // TODO: set value to the next argument else value = 0; res = cmd_add_back(res, value, type); diff --git a/srcs/pipe.c b/srcs/pipe.c index 8ab6174..7a73713 100644 --- a/srcs/pipe.c +++ b/srcs/pipe.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */ -/* Updated: 2024/04/01 20:09:14 by marde-vr ### ########.fr */ +/* Updated: 2024/04/02 02:07:25 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,9 +48,10 @@ void execute_command(t_msh *msh, char **cmd_args) void child(t_msh *msh, char **cmd_args, int i) { - if ((msh->in_type != ARG/* && msh->in_type != PIPE*/) - || (/*msh->in_type == PIPE &&*/ i > 0)) - redirect_input(msh); + // flemme + //if ((msh->in_type != ARG /*&& msh->in_type != PIPE*/) + // || (/*msh->in_type == PIPE &&*/ i > 0)) + // redirect_input(msh); if (/*msh->out_type == PIPE ||*/ msh->out_type == RED_O || msh->out_type == RED_O_APP) redirect_output(msh, i);