136 lines
4.0 KiB
C
136 lines
4.0 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* parsing.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2021/10/24 10:52:40 by lperrey #+# #+# */
|
|
/* Updated: 2021/11/29 12:28:07 by lperrey ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "minishell.h"
|
|
|
|
void save_redirections_words(t_token *t)
|
|
{
|
|
while (t)
|
|
{
|
|
if (t->id == '>' || t->id == T_DGREAT
|
|
|| t->id == '<' || t->id == T_DLESS)
|
|
{
|
|
t = t->next;
|
|
t->id = T_REDIRECTION_WORD;
|
|
}
|
|
t = t->next;
|
|
}
|
|
}
|
|
|
|
void print_pipeline(t_cmd **pipeline)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
while (pipeline[i])
|
|
{
|
|
printf("CMD %i, fd_in=%i, fd_out=%i\n", i, pipeline[i]->fd_in, pipeline[i]->fd_out);
|
|
ft_putstr_fd(" |", 1);
|
|
print_matrix(pipeline[i]->argv, "|\n |");
|
|
i++;
|
|
if (pipeline[i])
|
|
ft_putstr_fd("----------------\n", 1);
|
|
}
|
|
}
|
|
|
|
t_cmd **parsing(t_token *token_list)
|
|
{
|
|
t_cmd **pipeline;
|
|
|
|
if (!valid_syntax(token_list))
|
|
return (NULL);
|
|
|
|
// 2.9.1 - 1) Save Words
|
|
save_redirections_words(token_list);
|
|
|
|
// 2.9.1 - 2) Expansion
|
|
// TEST TOKENS PRINT
|
|
//ft_putstr_fd("TOKENS LIST :\n-----------\n", 1);
|
|
//ft_lstprint((t_list *)token_list, 1);
|
|
//
|
|
// getenv() ne va pas fonctionner avec le changement d'environnement prévu jusqu'ici.
|
|
// TODO : Revoir le changement d'environnement (avec extern char **environ ?)
|
|
// OU Integrer un equivalent perso comme dans pipex
|
|
if (!words_expansions(token_list))
|
|
return (NULL);
|
|
//
|
|
//ft_putstr_fd("TOKENS LIST EXPANDED :\n-----------\n", 1);
|
|
//ft_lstprint((t_list *)token_list, 1);
|
|
|
|
// Struct CMD alloc
|
|
pipeline = pipeline_alloc(1 + count_pipes(token_list));
|
|
if (!pipeline)
|
|
return (NULL);
|
|
|
|
// 2.9.1 - 3) Redirection
|
|
if (!redirections(token_list, pipeline))
|
|
return (ft_retp_free(NULL, &pipeline, (t_free_f)free_pipeline));
|
|
|
|
// Struct CMD fill
|
|
if (!pipeline_fill_argv(token_list, pipeline))
|
|
return (ft_retp_free(NULL, &pipeline, (t_free_f)free_pipeline));
|
|
print_pipeline(pipeline);
|
|
|
|
return (pipeline);
|
|
}
|
|
|
|
/* -------------------------------------------------------
|
|
The grammar symbols
|
|
------------------------------------------------------- */
|
|
/*
|
|
%token WORD
|
|
%token LESS // '<'
|
|
%token GREAT // '>'
|
|
%token DLESS // '<<'
|
|
%token DGREAT // '>>'
|
|
%token PIPE // '|'
|
|
*/
|
|
|
|
/* -------------------------------------------------------
|
|
The Simplified Grammar
|
|
------------------------------------------------------- */
|
|
/*
|
|
%start program
|
|
%%
|
|
pipeline : command
|
|
| pipeline '|' 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
|
|
;
|
|
*/
|