fill cmd et execute pipes ok

This commit is contained in:
hugogogo
2021-10-28 21:00:00 +02:00
parent 62955af5b8
commit b1b8a61921
11 changed files with 394 additions and 43 deletions

165
srcs/parsing/fill_cmd.c Normal file
View File

@@ -0,0 +1,165 @@
#include "minishell.h"
int nbr_pipes(t_token *token)
{
int nb;
nb = 0;
while (token)
{
if (token->id == T_PIPE)
nb++;
token = token->next;
}
return (nb);
}
int nbr_argv(t_token *token)
{
int i;
i = 0;
while (token && token->id != T_PIPE)
{
token = token->next;
i++;
}
return (i);
}
// T_T
// T_L
// T_G
// T_P
// T_D
// T_D
// T_W
void handle_argv(t_token **token, t_cmd *cmd)
{
int argc;
int i;
argc = nbr_argv(*token);
cmd->argv = calloc(argc + 1, sizeof(char *));
cmd->argv[argc] = NULL;
i = 0;
while (*token && (*token)->id != T_PIPE)
{
if ((*token)->id == T_WORD)
{
cmd->argv[i] = ft_strdup((*token)->content);
i++;
}
*token = (*token)->next;
}
if (*token && (*token)->id == T_PIPE)
*token = (*token)->next;
}
void handle_fd(t_token *token, t_cmd **cmd)
{
int *pipes_fd;
(*cmd)->fd_out = 1;
while (token && token->id != T_PIPE)
{
// T_LESS : '<',
if (token->id == T_LESS)
{
if ((*cmd)->fd_in != 0)
close((*cmd)->fd_in);
(*cmd)->fd_in = open(token->next->content, O_RDONLY | O_CREAT);
token->id = T_TOKEN;
token->next->id = T_TOKEN;
}
// T_GREAT : '>',
if (token->id == T_GREAT)
{
(*cmd)->fd_out = open(token->next->content, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
token->id = T_TOKEN;
token->next->id = T_TOKEN;
}
// T_DGREAT : '>>'
if (token->id == T_DGREAT)
{
(*cmd)->fd_out = open(token->next->content, O_WRONLY | O_CREAT | O_APPEND, S_IRWXU);
token->id = T_TOKEN;
token->next->id = T_TOKEN;
}
// T_DLESS : '<<' (heredoc)
// if (token->id == T_DGREAT)
// {
// cmd->fd_out = open(token->next->content, O_WRONLY | O_TMPFILE | O_APPEND, S_IRWXU);
// token->out = T_TOKEN;
// token->next->id = T_TOKEN;
// }
token = token->next;
}
if (token && token->id == T_PIPE)
{
pipes_fd = calloc(2, sizeof(int));
pipe(pipes_fd);
if ((*cmd)->fd_out == 1)
(*cmd)->fd_out = pipes_fd[1];
else
close(pipes_fd[1]);
cmd[1]->fd_in = pipes_fd[0];
}
}
int handle_builtin(t_token *token, t_cmd *cmd)
{
(void)token;
(void)cmd;
return (0);
}
void handle_cmd(char **argv, char **envp)
{
int i;
char **path;
char *cmd;
i = 0;
while (envp[i] && ft_strncmp(envp[i], "PATH=", 5))
i++;
path = ft_split(envp[i] + 5, ':'); // 5 = lentgh of "PATH="
i = -1;
while (*path && i != 0)
{
cmd = ft_strjoin(path[0], "/");
cmd = ft_strjoin(cmd, argv[0]);
i = access(cmd, X_OK);
path++;
}
argv[0] = cmd;
}
t_cmd **fill_cmd(t_token *token, char **envp)
{
t_cmd **cmd;
int pipes;
int i;
pipes = nbr_pipes(token);
cmd = ft_calloc(pipes + 2, sizeof(t_cmd*));
cmd[pipes + 1] = NULL;
i = -1;
while (++i <= pipes)
{
cmd[i] = ft_calloc(1, sizeof(t_cmd));
ft_bzero(cmd[i], sizeof(t_cmd));
}
i = 0;
while (i <= pipes)
{
handle_fd(token, cmd + i);
handle_argv(&token, cmd[i]);
if (!handle_builtin(token, cmd[i]))
handle_cmd(cmd[i]->argv, envp);
i++;
}
return(cmd);
}

View File

@@ -1,14 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/24 10:52:40 by lperrey #+# #+# */
/* Updated: 2021/10/24 19:19:08 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
@@ -22,13 +11,10 @@
enum e_token_id id;
} t_binary_tree; */
t_cmd **parsing(t_token *token_list)
t_cmd **parsing(t_token *token_list, char **envp)
{
t_cmd **cmd_arr;
size_t cmd_nbr;
(void)cmd_arr;
(void)cmd_nbr;
/* t_binary_tree *syntax_tree;
syntax_tree = ft_calloc(1, sizeof *syntax_tree);
@@ -40,13 +26,13 @@ t_cmd **parsing(t_token *token_list)
// Pipes count (determine cmd_nbr)
// Struct CMD alloc/fill
cmd_arr = fill_cmd(token_list, envp);
// 2.9.1 - 2) Expansion
// 2.9.1 - 3) Redirection
//return (cmd_arr);
return ((t_cmd **)1); //temp test
return (cmd_arr);
}
/* -------------------------------------------------------