Files
42_INT_07_minishell/srcs/parsing/parsing.c
LuckyLaszlo 3baf91afb3 replaced occurrences of last exit_status
+ deleted envp comments
+ WIP Macro exit status
+ TODO : Invalid free of environ in readline
2021-11-27 12:59:16 +01:00

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/27 11:09:48 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_cmd_array(t_cmd **cmd_arr)
{
int i;
i = 0;
while (cmd_arr[i])
{
printf("CMD %i, fd_in=%i, fd_out=%i\n", i, cmd_arr[i]->fd_in, cmd_arr[i]->fd_out);
ft_putstr_fd(" |", 1);
print_matrix(cmd_arr[i]->argv, "|\n |");
i++;
if (cmd_arr[i])
ft_putstr_fd("----------------\n", 1);
}
}
t_cmd **parsing(t_token *token_list)
{
t_cmd **cmd_arr;
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
cmd_arr = cmd_array_alloc(1 + count_pipes(token_list));
if (!cmd_arr)
return (NULL);
// 2.9.1 - 3) Redirection
if (!redirections(token_list, cmd_arr))
return (ft_retp_free(NULL, &cmd_arr, (t_free_f)free_pipeline));
// Struct CMD fill
if (!cmd_array_fill_argv(token_list, cmd_arr))
return (ft_retp_free(NULL, &cmd_arr, (t_free_f)free_pipeline));
print_cmd_array(cmd_arr);
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
| 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
;
*/