last commit

This commit is contained in:
Antoine Riard 2017-03-08 23:18:55 +01:00
parent 6fa5153df2
commit 791b99e469
20 changed files with 115 additions and 44 deletions

View file

@ -269,6 +269,7 @@ parser/build_tree.c\
parser/error_syntax.c\
parser/eval_sym.c\
parser/ft_parse.c\
parser/heredoc_parser.c\
parser/parser_init.c\
parser/pop_stack.c\
parser/produce_sym.c\

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
/* Updated: 2017/03/08 12:05:37 by jhalford ### ########.fr */
/* Updated: 2017/03/08 20:53:04 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -42,6 +42,7 @@ struct s_data
t_comp *comp;
t_exec exec;
t_jobc jobc;
t_list *heredoc_queue;
char **local_var;
t_list *lst_func;
};

View file

@ -30,6 +30,7 @@ struct s_parser
t_parstate state;
t_list *stack;
t_sym *new_sym;
t_list *heredoc_queue;
};
struct s_aggrematch
@ -74,6 +75,8 @@ int aggregate_sym(t_list **stack, t_sym *new_sym, t_parstate *state);
int push_stack(t_list **stack, t_sym new_sym);
int pop_stack(t_list **stack, t_sym erase_sym);
int pop_heredoc(t_list **heredoc_queue, t_list **lst);
int error_syntax(t_list **token, t_parser *parser, t_btree **ast);
int error_EOF(t_list **token, t_parser *parser, t_btree **ast);
@ -158,6 +161,7 @@ struct s_redir
t_type type;
int n;
char *word;
char *heredoc_data;
/* int close; */
};

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */
/* Updated: 2017/03/08 16:08:38 by ariard ### ########.fr */
/* Updated: 2017/03/08 17:16:00 by ariard ### ########.fr */
/* Updated: 2017/03/07 18:35:11 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -193,6 +193,7 @@ enum e_sym
REDIR,
CMD,
HEREDOCDATA,
HEREDOC_DELIM,
ALL = 200,
TERMINUS = 300,
};

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/21 21:05:23 by ariard #+# #+# */
/* Updated: 2017/02/24 16:22:08 by ariard ### ########.fr */
/* Updated: 2017/03/08 18:33:31 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,7 +16,6 @@ int insert_newline(t_list **alst)
{
t_token *token;
DG("insert newline");
token = token_init();
token->type = TK_NEWLINE;
ft_lsteadd(alst, ft_lstnew(token, sizeof(*token)));

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/05 16:58:24 by jhalford #+# #+# */
/* Updated: 2017/03/08 15:52:31 by jhalford ### ########.fr */
/* Updated: 2017/03/08 17:52:05 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/08 15:51:17 by jhalford #+# #+# */
/* Updated: 2017/03/08 15:53:57 by jhalford ### ########.fr */
/* Updated: 2017/03/08 20:04:52 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,8 +18,13 @@ int lexer_heredoc(t_list **alst, t_lexer *lexer)
token = (*alst)->content;
token->type = HEREDOCDATA;
token_append_str(token, lexer->str, 0, 0);
//token_append_char(token, '\n', 0, 0);
while (lexer->str[lexer->pos])
if (token_append_char(token, lexer->str[lexer->pos++], 0, 0))
return (1);
return (0);
//ft_strappend(&lexer->str, (char[]){'\n', 0});
//lexer->pos++;
/* heredoc_lst = *(t_list**)lexer->heredoc_stack->content; */
/* heredoc_tok = heredoc_lst->content; */
/* if (!(heredoc_lst->next)) */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 12:07:11 by jhalford #+# #+# */
/* Updated: 2017/03/08 12:10:37 by jhalford ### ########.fr */
/* Updated: 2017/03/08 19:07:20 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */
/* Updated: 2017/03/08 14:39:07 by jhalford ### ########.fr */
/* Updated: 2017/03/08 21:02:29 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -31,6 +31,7 @@ int data_init(void)
data->local_var = NULL;
data->lst_func = NULL;
data->heredoc_queue = NULL;
if ((term_name = ft_getenv(data->env, "TERM")) == NULL)
return (-1);
if (tgetent(NULL, term_name) != 1)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/10 11:36:39 by jhalford #+# #+# */
/* Updated: 2017/02/20 20:57:52 by ariard ### ########.fr */
/* Updated: 2017/03/08 20:50:57 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
/* Updated: 2017/03/08 16:06:44 by ariard ### ########.fr */
/* Updated: 2017/03/08 21:58:19 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -29,7 +29,7 @@ int handle_instruction(int fd)
while (1)
{
if ((ret = readline(fd, get_lexer_stack(lexer) ||
parser.state == UNDEFINED, &str)))
parser.state == UNDEFINED || lexer.state == HEREDOC, &str)))
{
if (ret == -1)
return (-1);
@ -52,10 +52,15 @@ int handle_instruction(int fd)
token_print(token);
if (insert_newline(&token))
return (1);
DG("exit newline");
if (ft_parse(&ast, &token, &parser))
continue ;
if (parser.state == SUCCESS)
DG();
lexer.state = (data_singleton()->heredoc_queue) ? HEREDOC : 0;
if (data_singleton()->heredoc_queue)
DG("still in HEREDOC");
if (lexer.state)
continue;
else if (parser.state == SUCCESS)
break ;
else if (parser.state == ERROR && SH_IS_INTERACTIVE(data_singleton()->opts))
{
@ -67,8 +72,8 @@ int handle_instruction(int fd)
}
DG("Before execution:");
// btree_print(STDBUG, ast, &ft_putast);
if (ft_exec(&ast))
return (1);
// if (ft_exec(&ast))
// return (1);
instruction_free(&token, &parser, &ast);
ft_add_str_in_history(lexer.str);
return (0);

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 20:49:15 by ariard #+# #+# */
/* Updated: 2017/03/07 20:10:10 by wescande ### ########.fr */
/* Updated: 2017/03/08 16:19:23 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -88,13 +88,6 @@ int add_cmd(t_btree **ast, t_list **lst)
else
node->type = CMD;
if (token->type == TK_WORD || token->type == TK_ASSIGNEMENT_WORD)
/* if ((my_tab = (char **)malloc(sizeof(char *) * 4)))
{
my_tab[0] = ft_strdup(token->data);
my_tab[1] = (char *)dup_char_esc(token->esc, token->size >> 3);
my_tab[2] = (char *)dup_char_esc(token->esc2, token->size >> 3);
my_tab[3] = NULL;
}*/
ft_ld_pushback(&node->data.cmd.token,
gen_tab(token->data, token->esc, token->esc2, 1));
return (0);

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 16:39:05 by ariard #+# #+# */
/* Updated: 2017/03/07 22:46:46 by ariard ### ########.fr */
/* Updated: 2017/03/08 22:11:07 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -60,6 +60,7 @@ int add_redir_word(t_btree **ast, t_list **lst)
t_astnode *node;
t_token *token;
t_redir *redir;
t_redir *temp;
token = (*lst)->content;
node = (*ast)->item;
@ -68,11 +69,12 @@ int add_redir_word(t_btree **ast, t_list **lst)
DG("add file");
redir = (ft_lstlast(node->data.cmd.redir))->content;
DG("now redir.type :%s", read_state(redir->type));
DG("n is ? : %d", redir->n);
redir->word = ft_strdup(token->data);
if (redir->type == TK_DLESS)
redir->word = NULL;
else
redir->word = ft_strdup(token->data);
{
temp = ft_lstlast((data_singleton()->heredoc_queue))->content;
temp->word = redir->word;
}
}
return (0);
}
@ -83,6 +85,7 @@ int add_redir_type(t_btree **ast, t_list **lst)
t_token *token;
t_redir redir;
t_redir *temp;
t_redir *temp_heredoc;
if (!*ast)
gen_node(ast);
@ -90,16 +93,23 @@ int add_redir_type(t_btree **ast, t_list **lst)
node = (*ast)->item;
if (!(node->type == TK_IO_NUMBER))
{
DG("add redir");
redir.n = (token->type == TK_LESS || token->type == TK_DLESS
|| token->type == TK_LESSAND) ? STDIN : STDOUT;
redir.type = token->type;
redir.heredoc_data = NULL;
ft_lsteadd(&node->data.cmd.redir, ft_lstnew(&redir, sizeof(redir)));
if (token->type == TK_DLESS)
ft_lsteadd(&data_singleton()->heredoc_queue, ft_lstnew(&redir, sizeof(redir)));
}
else
{
temp = (ft_lstlast(node->data.cmd.redir))->content;
temp->type = token->type;
if (token->type == TK_DLESS)
{
temp_heredoc = data_singleton()->heredoc_queue->content;
temp_heredoc->n = temp->n;
}
}
node->type = REDIR;
return (0);

View file

@ -103,6 +103,7 @@ t_aggrematch g_aggrematch[] =
{NEWLINE_LIST, COMPLETE_COMMANDS, LINEBREAK, 0},
{NEWLINE_LIST, LINEBREAK, PROGRAM, LINEBREAK},
{NEWLINE_LIST, FOR_WORDLIST, SEQUENTIAL_SEP, 0},
{NEWLINE_LIST, PROGRAM, PROGRAM, PROGRAM},
{SEQUENTIAL_SEP, FOR_WORDLIST, SEQUENTIAL_SEP, FOR_WORDLIST},
//to check
{FILENAME, TK_LESS, IO_FILE, TK_LESS},
@ -335,21 +336,21 @@ int aggregate_sym(t_list **stack, t_sym *new_sym, t_parstate *state)
return (1);
i = 0;
head = (*stack)->content;
DG("aggregate head %s && sym %s",
read_state(*head), read_state(*new_sym));
// DG("aggregate head %s && sym %s",
// read_state(*head), read_state(*new_sym));
while (g_aggrematch[i].top)
{
if (*new_sym == g_aggrematch[i].top
&& MATCH_STACK(*head, g_aggrematch[i].under))
{
DG("MATCH : %s", read_state(g_aggrematch[i].new_sym));
// DG("MATCH : %s", read_state(g_aggrematch[i].new_sym));
*new_sym = g_aggrematch[i].new_sym;
if (g_aggrematch[i].erase_sym)
{
pop_stack(stack, g_aggrematch[i].erase_sym);
head = (*stack)->content;
DG("stack after pop: %s", read_state(*head));
// DG("stack after pop: %s", read_state(*head));
}
if (eval_sym(stack, *new_sym))
{

View file

@ -551,6 +551,7 @@ t_stackmatch g_stackmatch[] =
{NEWLINE_LIST, TK_BANG},
{NEWLINE_LIST, TK_PAREN_OPEN},
{NEWLINE_LIST, TK_LBRACE},
{NEWLINE_LIST, PROGRAM},
{HERE_END, TK_DLESS},
{HERE_END, TK_DLESS},
{HERE_END, TK_DLESSDASH},
@ -1123,7 +1124,7 @@ int eval_sym(t_list **stack, t_sym new_sym)
if (!*stack)
return (1);
head = (*stack)->content;
DG("eval head %s && sym %s", read_state(*head), read_state(new_sym));
// DG("eval head %s && sym %s", read_state(*head), read_state(new_sym));
i = 0;
while (g_stackmatch[i].top)
{

View file

@ -34,6 +34,8 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser)
{
t_sym *head;
if (pop_heredoc(&parser->heredoc_queue, token))
return (0);
while (*token)
{
produce_sym(&parser->stack, parser->new_sym, token);
@ -53,7 +55,7 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser)
else
parser->state = UNDEFINED;
build_tree(ast, token);
btree_print(STDBUG, *ast, &ft_putast);
// btree_print(STDBUG, *ast, &ft_putast);
if ((end_instruction(&parser->stack) && !(*token)->next))
insert_linebreak(token);
else

View file

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* heredoc_parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/08 16:21:05 by ariard #+# #+# */
/* Updated: 2017/03/08 23:18:29 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int pop_heredoc(t_list **heredoc_queue, t_list **lst)
{
t_token *token;
t_list *temp;
t_list *temp2;
t_redir *head;
temp = NULL;
if (!heredoc_queue && !*heredoc_queue)
return (0);
token = (*lst)->content;
if (token->type == HEREDOCDATA)
{
head = data_singleton()->heredoc_queue->content;
temp = data_singleton()->heredoc_queue;
DG("compare %s with %s", (char *)token->data, head->word);
if (head && ft_strcmp((char *)token->data, head->word) == 0)
{
temp2 = temp->next;
free(temp);
data_singleton()->heredoc_queue = temp2;
}
else
head->heredoc_data = ft_strjoin(head->heredoc_data, token->data);
ft_lstdel(lst, &token_free);
return (1);
}
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/21 16:14:04 by ariard #+# #+# */
/* Updated: 2017/03/06 17:04:33 by ariard ### ########.fr */
/* Updated: 2017/03/08 21:56:56 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,4 +19,5 @@ void parser_init(t_parser *parser)
parser->stack = NULL;
push_stack(&parser->stack, TERMINUS);
push_stack(&parser->stack, LINEBREAK);
parser->heredoc_queue = NULL;
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 17:58:34 by ariard #+# #+# */
/* Updated: 2017/03/07 21:24:40 by wescande ### ########.fr */
/* Updated: 2017/03/08 18:00:22 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -96,6 +96,7 @@ t_prodmatch g_prodmatch[] =
{TK_NEWLINE, CONDITION, NEWLINE_LIST},
{TK_NEWLINE, FOR_WORDLIST, NEWLINE_LIST},
{TK_NEWLINE, SEQUENTIAL_SEP, NEWLINE_LIST},
{TK_NEWLINE, PROGRAM, NEWLINE_LIST},
{TK_SEMI, MATH_SUP, SEPARATOR_OP},
{TK_SEMI, CMD_SUPERIOR, SEPARATOR_OP},
{TK_SEMI, LIST, SEPARATOR_OP},
@ -118,8 +119,8 @@ int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst)
return (1);
token = (*lst)->content;
head = (*stack)->content;
DG("produce stack : %s && token : %s", read_state(*head),
read_state(token->type));
// DG("produce stack : %s && token : %s", read_state(*head),
// read_state(token->type));
i = 0;
*new_sym = 0;
while (g_prodmatch[i].new_sym)
@ -127,7 +128,7 @@ int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst)
if (token->type == g_prodmatch[i].token
&& *head == g_prodmatch[i].stack)
{
DG("MATCH : %s", read_state(g_prodmatch[i].new_sym));
// DG("MATCH : %s", read_state(g_prodmatch[i].new_sym));
*new_sym = g_prodmatch[i].new_sym;
}
i++;

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 15:32:10 by ariard #+# #+# */
/* Updated: 2017/03/08 00:23:48 by wescande ### ########.fr */
/* Updated: 2017/03/08 17:39:50 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,8 @@
char *read_state(t_sym current)
{
if (current == HEREDOCDATA)
return ("HEREDOCDATA");
if (current == TERMINUS)
return ("TERMINUS");
if (current == SEQUENCE)