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

@@ -26,7 +26,7 @@ SRCS = main.c init.c free.c generic.c \
lexing.c \ lexing.c \
parsing.c \ parsing.c \
valid_syntax.c valid_pipeline.c valid_command.c valid_io_redirect.c \ valid_syntax.c valid_pipeline.c valid_command.c valid_io_redirect.c \
fill_cmd.c \ fill_cmd.c cmd_expansion.c \
env.c exit.c echo.c env.c exit.c echo.c
DIR_OBJS = builds DIR_OBJS = builds

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */ /* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
/* Updated: 2021/10/28 20:49:30 by hulamy ### ########.fr */ /* Updated: 2021/10/28 21:10:45 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -28,6 +28,7 @@ int valid_syntax(t_token *token_list);
int valid_token(t_token **token_list, enum e_token_id token_id); int valid_token(t_token **token_list, enum e_token_id token_id);
int valid_command_separator(const t_token *token_list); int valid_command_separator(const t_token *token_list);
t_cmd **fill_cmd(t_token *token, char **envp); t_cmd **fill_cmd(t_token *token, char **envp);
void cmd_expansion(t_cmd **cmd_arr, char **envp);
// Builtins // Builtins
int builtin_env(int argc, char *argv[], t_all *c); int builtin_env(int argc, char *argv[], t_all *c);

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */ /* 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); return (i);
} }
// T_T //void handle_argv(t_token **token, t_cmd *cmd)
// T_L //{
// T_G // int argc;
// T_P // int i;
// T_D //
// T_D // argc = nbr_argv(*token);
// T_W // cmd->argv = calloc(argc + 1, sizeof(char *));
void handle_argv(t_token **token, t_cmd *cmd) // cmd->argv[argc] = NULL;
{ // i = 0;
int argc; // while (*token && (*token)->id != T_PIPE)
int i; // {
// if ((*token)->id == T_WORD)
argc = nbr_argv(*token); // {
cmd->argv = calloc(argc + 1, sizeof(char *)); // cmd->argv[i] = ft_strdup((*token)->content);
cmd->argv[argc] = NULL; // i++;
i = 0; // }
while (*token && (*token)->id != T_PIPE) // *token = (*token)->next;
{ // }
if ((*token)->id == T_WORD) // if (*token && (*token)->id == T_PIPE)
{ // *token = (*token)->next;
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) void handle_fd(t_token *token, t_cmd **cmd)
{ {
int *pipes_fd; int *pipes_fd;
@@ -142,15 +136,15 @@ t_cmd **fill_cmd(t_token *token, char **envp)
int pipes; int pipes;
int i; int i;
pipes = nbr_pipes(token); // pipes = nbr_pipes(token);
cmd = ft_calloc(pipes + 2, sizeof(t_cmd*)); // cmd = ft_calloc(pipes + 2, sizeof(t_cmd*));
cmd[pipes + 1] = NULL; // cmd[pipes + 1] = NULL;
i = -1; // i = -1;
while (++i <= pipes) // while (++i <= pipes)
{ // {
cmd[i] = ft_calloc(1, sizeof(t_cmd)); // cmd[i] = ft_calloc(1, sizeof(t_cmd));
ft_bzero(cmd[i], sizeof(t_cmd)); // ft_bzero(cmd[i], sizeof(t_cmd));
} // }
i = 0; i = 0;
while (i <= pipes) while (i <= pipes)
{ {
@@ -162,4 +156,4 @@ t_cmd **fill_cmd(t_token *token, char **envp)
} }
return(cmd); return(cmd);
} }
*/

View File

@@ -11,10 +11,113 @@
enum e_token_id id; enum e_token_id id;
} t_binary_tree; */ } 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 **parsing(t_token *token_list, char **envp)
{ {
t_cmd **cmd_arr; t_cmd **cmd_arr;
size_t cmd_nbr;
(void)envp;
/* t_binary_tree *syntax_tree; /* t_binary_tree *syntax_tree;
syntax_tree = ft_calloc(1, sizeof *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)) if (!valid_syntax(token_list))
return (NULL); return (NULL);
// Pipes count (determine cmd_nbr) // Pipes count (determine cmd_nbr)
cmd_nbr = count_pipes(token_list);
// Struct CMD alloc/fill // 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 // 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 // 2.9.1 - 3) Redirection
// handle_fd(token, cmd + i);
return (cmd_arr); return (cmd_arr);
} }

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */ /* 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); c->cmd_arr = parsing(c->token_list, c->envp);
if (c->cmd_arr == NULL) if (c->cmd_arr == NULL)
ft_putstr_fd("Syntax KO:\n-----------\n", 1); ft_putstr_fd("Syntax KO:\n-----------\n", 1);
// else else
// ft_putstr_fd("Syntax OK:\n-----------\n", 1); ft_putstr_fd("Syntax OK:\n-----------\n", 1);
// ft_putstr_fd("TOKENS LIST :\n-----------\n", 1); ft_putstr_fd("TOKENS LIST :\n-----------\n", 1);
// ft_lstprint((t_list *)c->token_list, 1); ft_lstprint((t_list *)c->token_list, 1);
execute_cmd(c->envp, c->cmd_arr); // execute_cmd(c->envp, c->cmd_arr);
ft_lstclear((t_list **)&c->token_list, free); ft_lstclear((t_list **)&c->token_list, free);
} }
} }