Files
42_INT_07_minishell/srcs/parsing/fill_cmd.c
2021-10-30 15:27:41 +02:00

160 lines
3.0 KiB
C

#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);
}
//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);
}
*/