diff --git a/minishell.h b/minishell.h index 6246368..7cbf707 100755 --- a/minishell.h +++ b/minishell.h @@ -35,6 +35,7 @@ typedef struct s_command { char **argv; + int argc; int fd[2]; char *heredoc; struct s_command *next; @@ -58,6 +59,8 @@ void halt(int ret_code); int replace_var(t_datas *datas, char **line, \ char *tmp, int tmp_i); int file_error(char *cmd, char *file, char *msg); +char *expend_str(t_datas *datas, char *line); + // ----------------------------------Builtins.c int close_minishell(int exit_code); int ft_echo(t_command *cmd); @@ -95,7 +98,6 @@ typedef struct s_token struct s_token *next; } t_token; -char *expend_str(t_datas *datas, char *line); size_t count_arguments(t_token *tok); void update_redir(t_datas *datas, t_command *cmd,\ t_token *tok); diff --git a/parser.c b/parser.c index 5f0f6e3..1a69a5a 100755 --- a/parser.c +++ b/parser.c @@ -12,26 +12,6 @@ #include "minishell.h" -char *expend_str(t_datas *datas, char *line) -{ - char *tmp; - int tmp_i; - - tmp = ft_calloc(1024, sizeof *tmp); - if (!tmp) - free(tmp); - tmp_i = 0; - while (*line) - { - if (*line == '$') - tmp_i = replace_var(datas, &line, tmp, tmp_i); - tmp[tmp_i] = *line; - line++; - tmp_i++; - } - return (tmp); -} - int push_heredoc(t_datas *datas, char *str) { char *line; @@ -66,60 +46,60 @@ size_t count_arguments(t_token *tok) void update_redir(t_datas *datas, t_command *cmd, t_token *tok) { - if (tok->type == OUT) - { - if (cmd->fd[1]) - close(cmd->fd[1]); - cmd->fd[1] = open(tok->value, O_CREAT | O_TRUNC | O_WRONLY, 0644); - } - else if (tok->type == ADD) - { - if (cmd->fd[1]) - close(cmd->fd[1]); - cmd->fd[1] = open(tok->value, O_CREAT | O_APPEND | O_WRONLY, 0644); - } - else if (tok->type == IN) - { - if (cmd->fd[0]) - close(cmd->fd[0]); - cmd->fd[0] = open(tok->value, O_RDONLY); - } - else if (tok->type == HD) - { - if (cmd->fd[0]) - close(cmd->fd[0]); - cmd->fd[0] = push_heredoc(datas, tok->value); - } + if (tok->type == OUT) + { + if (cmd->fd[1]) + close(cmd->fd[1]); + cmd->fd[1] = open(tok->value, O_CREAT | O_TRUNC | O_WRONLY, 0644); + } + else if (tok->type == ADD) + { + if (cmd->fd[1]) + close(cmd->fd[1]); + cmd->fd[1] = open(tok->value, O_CREAT | O_APPEND | O_WRONLY, 0644); + } + else if (tok->type == IN) + { + if (cmd->fd[0]) + close(cmd->fd[0]); + cmd->fd[0] = open(tok->value, O_RDONLY); + } + else if (tok->type == HD) + { + if (cmd->fd[0]) + close(cmd->fd[0]); + cmd->fd[0] = push_heredoc(datas, tok->value); + } } +t_token *parse_cmd(t_datas *datas, t_token *tok, t_command *cmd) +{ + t_token *prev_tok; + + if (tok->type) + update_redir(datas, cmd, tok); + else + cmd->argv[cmd->argc++] = ft_strdup(tok->value); + prev_tok = tok; + tok = tok->next; + free(prev_tok->value); + free(prev_tok); + return (tok); +} t_command *parser(t_datas *datas, t_token *tok, t_command *prev) { - int i; t_command *cmd; - t_token *prev_tok; if (!tok) return (NULL); cmd = ft_calloc(1, sizeof(t_command)); if (prev) cmd->prev = prev; - i = 0; cmd->argv = ft_calloc(count_arguments(tok) + 1, sizeof(char *)); + cmd->argc = 0; while(tok && tok->type != PIPE) - { - if (tok->type) - update_redir(datas, cmd, tok); - else - cmd->argv[i++] = ft_strdup(tok->value); - prev_tok = tok; - tok = tok->next; - free(prev_tok->value); - free(prev_tok); - } - if (DEBUG) - printf("######\n->cmd : %s, in_fd : %d, out_fd : %d\n", \ - cmd->argv[0], cmd->fd[0], cmd->fd[1]); - cmd->argv[i] = NULL; + tok = parse_cmd(datas, tok, cmd); + cmd->argv[cmd->argc] = NULL; if (tok && tok->type == PIPE) { cmd->next = parser(datas, tok->next, cmd); diff --git a/utils_2.c b/utils_2.c index 734f8e9..13e19eb 100644 --- a/utils_2.c +++ b/utils_2.c @@ -74,3 +74,23 @@ int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i) free(value); return (tmp_i); } + +char *expend_str(t_datas *datas, char *line) +{ + char *tmp; + int tmp_i; + + tmp = ft_calloc(1024, sizeof *tmp); + if (!tmp) + free(tmp); + tmp_i = 0; + while (*line) + { + if (*line == '$') + tmp_i = replace_var(datas, &line, tmp, tmp_i); + tmp[tmp_i] = *line; + line++; + tmp_i++; + } + return (tmp); +}