Files
42_INT_07_minishell/srcs/parsing/parsing.c
2021-10-30 10:20:43 +02:00

198 lines
4.0 KiB
C

#include "minishell.h"
// A quoi bon un arbre binaire ? Je ne vois plus l'utilité.
/* typedef struct s_binary_tree
{
char *content;
struct s_binary_tree *sub;
struct s_binary_tree *sibling;
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, size_t cmd_nbr)
{
int argc;
int j;
int i;
i = 0;
while (cmd_nbr)
{
argc = next_cmd(token);
cmd[i]->argv = calloc(argc + 1, sizeof(char *));
cmd[i]->argv[argc] = NULL;
j = 0;
while (token && token->id != T_PIPE)
{
if (token->id == T_WORD)
{
cmd[i]->argv[j] = ft_strdup(token->content);
j++;
}
else if (token->id != T_PIPE)
token = token->next;
if (token->id != T_PIPE)
token = token->next;
}
if (token && token->id == T_PIPE)
token = token->next;
cmd_nbr--;
i++;
}
}
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);
if (!syntax_tree)
return (0); //WIP ERROR */
if (!valid_syntax(token_list))
return (NULL);
// Pipes count (determine cmd_nbr)
cmd_nbr = count_pipes(token_list);
// Struct CMD alloc/fill
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);
int j;
j = 0;
while (cmd_arr[j])
{
printf("%i\n", j);
print_matrix(cmd_arr[j]->argv, " / ");
j++;
}
// 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);
}
/* -------------------------------------------------------
The grammar symbols
------------------------------------------------------- */
/*
%token WORD
%token LESS // '<'
%token GREAT // '>'
%token DLESS // '<<'
%token DGREAT // '>>'
%token PIPE // '|'
*/
/* -------------------------------------------------------
The Simplified Grammar
------------------------------------------------------- */
/*
%start program
%%
pipeline : command
| pipe_sequence '|' command
;
command : cmd_prefix cmd_name cmd_suffix
| cmd_prefix cmd_name
| cmd_name cmd_suffix
| cmd_name
;
cmd_name : WORD // Apply rule 7a
;
cmd_prefix : io_redirect
| cmd_prefix io_redirect
;
cmd_suffix : io_redirect
| cmd_suffix io_redirect
| WORD
| cmd_suffix WORD
;
io_redirect : io_file
| io_here
;
io_file : '<' filename
| '>' filename
| DGREAT filename
;
filename : WORD // Apply rule 2
;
io_here : DLESS here_end
;
here_end : WORD // Apply rule 3
;
*/