refonte de la gestion des cmd dans parsing

This commit is contained in:
hugogogo
2021-10-29 13:26:38 +02:00
parent b1b8a61921
commit 15bc4d2158
7 changed files with 182 additions and 48 deletions

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
/* Updated: 2021/10/28 15:12:04 by hulamy ### ########.fr */
/* Updated: 2021/10/29 01:10:02 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */

View File

@@ -0,0 +1,28 @@
#include "minishell.h"
void cmd_expansion(t_cmd **cmd_arr, char **envp)
{
char **str;
char *var;
int i;
int j;
(void)envp;
i = 0;
while (cmd_arr[i])
{
j = 0;
str = cmd_arr[i]->argv;
while (str[j])
{
var = ft_strchr(str[j], '$');
if (var != NULL)
{
printf("%s\n", var);
}
j++;
}
i++;
}
}

View File

@@ -28,35 +28,29 @@ int nbr_argv(t_token *token)
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_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;
@@ -142,15 +136,15 @@ t_cmd **fill_cmd(t_token *token, char **envp)
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));
}
// 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)
{
@@ -162,4 +156,4 @@ t_cmd **fill_cmd(t_token *token, char **envp)
}
return(cmd);
}
*/

View File

@@ -11,10 +11,113 @@
enum e_token_id id;
} t_binary_tree; */
size_t count_pipes(t_token *token)
{
size_t nb;
nb = 0;
while (token)
{
if (token->id == T_PIPE)
nb++;
token = token->next;
}
return (nb + 1);
}
t_cmd **create_cmd(t_token *token_list, size_t cmd_nbr)
{
t_cmd **cmd_arr;
size_t i;
(void)token_list;
cmd_arr = ft_calloc(cmd_nbr + 1, sizeof(t_cmd *));
cmd_arr[cmd_nbr] = NULL;
i = 0;
while (i < cmd_nbr)
{
cmd_arr[i] = ft_calloc(1, sizeof(t_cmd));
ft_bzero(cmd_arr[i], sizeof(t_cmd));
i++;
}
return (cmd_arr);
}
// T_TOKEN = 0,
// T_LESS = '<',
// T_GREAT = '>',
// T_PIPE = '|',
// T_DLESS, //'<<'
// T_DGREAT, //'>>'
// T_WORD
// count nbr word in cmd, minus redirection and heredoc
int next_cmd(t_token **token)
{
int i;
i = 0;
while (*token && (*token)->id != T_PIPE)
{
if ((*token)->id != T_WORD)
i--;
else
i++;
*token = (*token)->next;
}
if (*token && (*token)->id == T_PIPE)
*token = (*token)->next;
return (i);
}
void handle_argv(t_token *token, t_cmd **cmd_arr, size_t cmd_nbr)
{
int argc;
int i;
(void)cmd_arr;
(void)token;
(void)cmd_nbr;
while (cmd_nbr)
{
argc = next_cmd(&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;
// }
// cmd_nbr--;
}
// 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;
}
t_cmd **parsing(t_token *token_list, char **envp)
{
t_cmd **cmd_arr;
size_t cmd_nbr;
(void)envp;
/* t_binary_tree *syntax_tree;
syntax_tree = ft_calloc(1, sizeof *syntax_tree);
@@ -24,13 +127,21 @@ t_cmd **parsing(t_token *token_list, char **envp)
if (!valid_syntax(token_list))
return (NULL);
// Pipes count (determine cmd_nbr)
cmd_nbr = count_pipes(token_list);
// Struct CMD alloc/fill
cmd_arr = fill_cmd(token_list, envp);
cmd_arr = create_cmd(token_list, cmd_nbr);
// cmd_arr = fill_cmd(token_list, envp);
// 2.9.1 - 2) Expansion
// cmd_expansion(cmd_arr, envp);
handle_argv(token_list, cmd_arr, cmd_nbr);
// if (!handle_builtin(token, cmd[i]))
// handle_cmd(cmd[i]->argv, envp);
// 2.9.1 - 3) Redirection
// handle_fd(token, cmd + i);
return (cmd_arr);
}

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
/* Updated: 2021/10/28 20:45:09 by hulamy ### ########.fr */
/* Updated: 2021/10/29 12:49:08 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -79,11 +79,11 @@ void shell_loop(t_all *c)
c->cmd_arr = parsing(c->token_list, c->envp);
if (c->cmd_arr == NULL)
ft_putstr_fd("Syntax KO:\n-----------\n", 1);
// else
// ft_putstr_fd("Syntax OK:\n-----------\n", 1);
// ft_putstr_fd("TOKENS LIST :\n-----------\n", 1);
// ft_lstprint((t_list *)c->token_list, 1);
execute_cmd(c->envp, c->cmd_arr);
else
ft_putstr_fd("Syntax OK:\n-----------\n", 1);
ft_putstr_fd("TOKENS LIST :\n-----------\n", 1);
ft_lstprint((t_list *)c->token_list, 1);
// execute_cmd(c->envp, c->cmd_arr);
ft_lstclear((t_list **)&c->token_list, free);
}
}