exec if + elif + else ok to-do:syntax error

This commit is contained in:
ariard@student.42.fr 2017-02-06 19:48:17 +01:00
parent a211bfd1b0
commit 490f81ee23
22 changed files with 162 additions and 33 deletions

View file

@ -52,11 +52,12 @@ exec/exec_or_if.c\
exec/exec_pipe.c\ exec/exec_pipe.c\
exec/exec_semi.c\ exec/exec_semi.c\
exec/exec_while.c\ exec/exec_while.c\
exec/exec_if.c\
exec/exec_else.c\
exec/exec_elif.c\
exec/ft_exec.c\ exec/ft_exec.c\
exec/ft_findexec.c\ exec/ft_findexec.c\
exec/launch_process.c\ exec/launch_process.c\
exec/loop_del.c\
exec/loop_exec.c\
exec/process_redirect.c\ exec/process_redirect.c\
exec/process_reset.c\ exec/process_reset.c\
exec/process_setexec.c\ exec/process_setexec.c\
@ -183,6 +184,7 @@ parser/parse_subshell.c\
parser/parse_while.c\ parser/parse_while.c\
parser/parse_if.c\ parser/parse_if.c\
parser/parse_elif.c\ parser/parse_elif.c\
parser/parse_else.c\
parser/parse_word.c parser/parse_word.c
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */
/* Updated: 2017/02/03 15:09:26 by ariard ### ########.fr */ /* Updated: 2017/02/06 18:59:24 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -89,7 +89,9 @@ int exec_dgreat(t_btree **ast);
int exec_command(t_btree **ast); int exec_command(t_btree **ast);
int exec_while(t_btree **ast); int exec_while(t_btree **ast);
int exec_list(t_btree **ast); int exec_if(t_btree **ast);
int exec_elif(t_btree **ast);
int exec_else(t_btree **ast);
int exec_default(t_btree **ast); int exec_default(t_btree **ast);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
/* Updated: 2017/02/03 14:42:54 by ariard ### ########.fr */ /* Updated: 2017/02/06 18:33:10 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -52,6 +52,7 @@ struct s_script
{ {
char *buffer; char *buffer;
int size; int size;
int lc;
}; };
typedef struct s_script t_script; typedef struct s_script t_script;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */ /* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */
/* Updated: 2017/02/06 15:45:33 by ariard ### ########.fr */ /* Updated: 2017/02/06 19:02:48 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -81,6 +81,7 @@ int parse_if(t_btree **ast, t_list **start, t_list **lst);
int parse_do(t_btree **ast, t_list **start, t_list **lst); int parse_do(t_btree **ast, t_list **start, t_list **lst);
int parse_done(t_btree **ast, t_list **start, t_list **lst); int parse_done(t_btree **ast, t_list **start, t_list **lst);
int parse_elif(t_btree **ast, t_list **start, t_list **lst); int parse_elif(t_btree **ast, t_list **start, t_list **lst);
int parse_else(t_btree **ast, t_list **start, t_list **lst);
int delete_newline(t_list **start, t_list **lst); int delete_newline(t_list **start, t_list **lst);
int parse_head(t_btree **ast, t_btree **new_ast, t_list **start, t_list **lst); int parse_head(t_btree **ast, t_btree **new_ast, t_list **start, t_list **lst);

View file

@ -1,7 +1,13 @@
if ls while ls
do
if ls
then then
echo hello echo hello
elif ls else
then while ls
echo world do
echo hello world
pwd
done
fi fi
done

28
42sh/src/exec/exec_elif.c Normal file
View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_elif.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/06 18:08:53 by ariard #+# #+# */
/* Updated: 2017/02/06 19:26:00 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int exec_elif(t_btree **ast)
{
int test;
(void)ast;
test = 1;
if (test && data_singleton()->script.lc == 0)
{
ft_exec(&(*ast)->right);
data_singleton()->script.lc = 1;
}
// btree_delone(ast, &ast_free);
return (0);
}

20
42sh/src/exec/exec_else.c Normal file
View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_else.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/06 18:55:07 by ariard #+# #+# */
/* Updated: 2017/02/06 19:13:05 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int exec_else(t_btree **ast)
{
ft_exec(&(*ast)->right);
//btree_delone(ast, &ast_free);
return (0);
}

21
42sh/src/exec/exec_if.c Normal file
View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_if.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/06 18:07:31 by ariard #+# #+# */
/* Updated: 2017/02/06 18:55:00 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int exec_if(t_btree **ast)
{
ft_exec(&(*ast)->right);
data_singleton()->script.lc = 0;
// btree_delone(ast, &ast_free);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 20:52:05 by jhalford #+# #+# */ /* Created: 2016/11/30 20:52:05 by jhalford #+# #+# */
/* Updated: 2017/02/05 22:19:21 by ariard ### ########.fr */ /* Updated: 2017/02/06 18:34:38 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/30 17:33:53 by ariard #+# #+# */ /* Created: 2017/01/30 17:33:53 by ariard #+# #+# */
/* Updated: 2017/02/06 17:05:21 by ariard ### ########.fr */ /* Updated: 2017/02/06 18:12:10 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */ /* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */
/* Updated: 2017/02/06 14:53:27 by ariard ### ########.fr */ /* Updated: 2017/02/06 18:58:09 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,6 +24,9 @@ t_execmap g_execmap[] =
{TK_GREAT, &exec_great}, {TK_GREAT, &exec_great},
{TK_DGREAT, &exec_dgreat}, {TK_DGREAT, &exec_dgreat},
{TK_WHILE, &exec_while}, {TK_WHILE, &exec_while},
{TK_IF, &exec_if},
{TK_ELIF, &exec_elif},
{TK_ELSE, &exec_else},
{TK_COMMAND | TK_SUBSHELL, &exec_command}, {TK_COMMAND | TK_SUBSHELL, &exec_command},
{0, 0}, {0, 0},
}; };

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */ /* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */
/* Updated: 2017/02/06 16:14:40 by ariard ### ########.fr */ /* Updated: 2017/02/06 19:16:59 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -38,6 +38,8 @@ char *ft_putast(void *nodein)
return ("IF"); return ("IF");
else if (node->type == TK_ELIF) else if (node->type == TK_ELIF)
return ("ELIF"); return ("ELIF");
else if (node->type == TK_ELSE)
return ("ELSE");
else if (node->type == TK_NEWLINE) else if (node->type == TK_NEWLINE)
return ("NEW"); return ("NEW");
else if (node->type == TK_GREAT) else if (node->type == TK_GREAT)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
/* Updated: 2017/02/03 16:33:52 by ariard ### ########.fr */ /* Updated: 2017/02/06 18:54:43 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/22 23:06:34 by ariard #+# #+# */ /* Created: 2017/01/22 23:06:34 by ariard #+# #+# */
/* Updated: 2017/02/06 17:59:20 by ariard ### ########.fr */ /* Updated: 2017/02/06 19:26:06 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,14 +26,14 @@ int shell_script()
DG("after post_tokenize"); DG("after post_tokenize");
token_print(token); token_print(token);
// while (token) while (token)
// { {
if (parse(&ast, &token)) if (parse(&ast, &token))
return (1); return (1);
btree_print(STDBUG, ast, &ft_putast); // btree_print(STDBUG, ast, &ft_putast);
// if (ft_exec(&ast)) if (ft_exec(&ast))
// return (1); return (1);
ast = NULL; ast = NULL;
// } }
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */ /* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */
/* Updated: 2017/02/06 17:11:26 by ariard ### ########.fr */ /* Updated: 2017/02/06 19:26:04 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,7 @@
t_parser g_parser[] = t_parser g_parser[] =
{ {
{TK_WHILE | TK_IF | TK_ELIF | TK_NEWLINE | TK_SEMI, &get_sub_instruction}, {TK_WHILE | TK_IF | TK_ELIF | TK_NEWLINE | TK_SEMI | TK_ELSE, &get_sub_instruction},
{TK_AND_IF | TK_OR_IF, &parse_separator}, {TK_AND_IF | TK_OR_IF, &parse_separator},
{TK_AMP, &parse_separator}, {TK_AMP, &parse_separator},
{TK_PIPE, &parse_separator}, {TK_PIPE, &parse_separator},

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/03 16:56:55 by ariard #+# #+# */ /* Created: 2017/02/03 16:56:55 by ariard #+# #+# */
/* Updated: 2017/02/06 17:17:59 by ariard ### ########.fr */ /* Updated: 2017/02/06 19:18:17 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/05 22:05:26 by ariard #+# #+# */ /* Created: 2017/02/05 22:05:26 by ariard #+# #+# */
/* Updated: 2017/02/06 15:49:31 by ariard ### ########.fr */ /* Updated: 2017/02/06 19:08:17 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,6 +25,8 @@ int get_sub_instruction(t_btree **ast, t_list **start, t_list **lst)
return (parse_if(ast, start, lst)); return (parse_if(ast, start, lst));
else if (token->type == TK_ELIF) else if (token->type == TK_ELIF)
return (parse_elif(ast, start, lst)); return (parse_elif(ast, start, lst));
else if (token->type == TK_ELSE)
return (parse_else(ast, start, lst));
else if (token->type == TK_SEMI) else if (token->type == TK_SEMI)
return (parse_separator(ast, start, lst)); return (parse_separator(ast, start, lst));
return (0); return (0);

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/04 16:52:51 by ariard #+# #+# */ /* Created: 2017/02/04 16:52:51 by ariard #+# #+# */
/* Updated: 2017/02/06 17:42:08 by ariard ### ########.fr */ /* Updated: 2017/02/06 19:26:02 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/06 14:50:54 by ariard #+# #+# */ /* Created: 2017/02/06 14:50:54 by ariard #+# #+# */
/* Updated: 2017/02/06 17:54:22 by ariard ### ########.fr */ /* Updated: 2017/02/06 19:22:58 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,7 +25,7 @@ static int parse_after_elif(t_btree **ast, t_list **lst)
while ((temp = temp->next)) while ((temp = temp->next))
{ {
token = temp->content; token = temp->content;
if (nest == 0 && (token->type & TK_ELIF)) if (nest == 0 && (token->type & (TK_ELIF | TK_ELSE)))
break; break;
else if (token->type & TK_IF) else if (token->type & TK_IF)
nest++; nest++;
@ -52,7 +52,7 @@ static int parse_loop(t_btree **ast, t_list **start, t_list **lst)
token = (*lst)->content; token = (*lst)->content;
if (token->type & TK_THEN) if (token->type & TK_THEN)
nest++; nest++;
else if (token->type & TK_ELIF) else if (token->type & (TK_ELIF | TK_ELSE))
nest--; nest--;
if (nest == 1 && (token->type & TK_THEN)) if (nest == 1 && (token->type & TK_THEN))
break; break;

View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_elif.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/06 14:50:54 by ariard #+# #+# */
/* Updated: 2017/02/06 19:22:56 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int parse_after_else(t_btree **ast, t_list **lst)
{
t_list *temp;
temp = (*lst);
while ((temp = temp->next));
if (temp)
ft_parse(ast, &temp);
return (0);
}
static int parse_loop(t_btree **ast, t_list **start, t_list **lst)
{
delete_newline(start, lst);
ft_parse(&(*ast)->right, lst);
return (0);
}
int parse_else(t_btree **ast, t_list **start, t_list **lst)
{
t_btree *new_ast;
parse_after_else(ast, lst);
parse_head(ast, &new_ast, start, lst);
*start = *lst;
parse_loop(&new_ast, start, lst);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/05 23:15:23 by ariard #+# #+# */ /* Created: 2017/02/05 23:15:23 by ariard #+# #+# */
/* Updated: 2017/02/06 17:46:01 by ariard ### ########.fr */ /* Updated: 2017/02/06 19:20:47 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -72,7 +72,7 @@ static int parse_head_condition(t_btree **ast,
int parse_if(t_btree **ast, t_list **start, t_list **lst) int parse_if(t_btree **ast, t_list **start, t_list **lst)
{ {
t_btree *new_ast; t_btree *new_ast;
parse_after_condition(ast, start, lst); parse_after_condition(ast, start, lst);
parse_head_condition(ast, &new_ast, start, lst); parse_head_condition(ast, &new_ast, start, lst);
return (0); return (0);

View file

@ -83,7 +83,6 @@ int parse_head(t_btree **ast,
del = (*lst); del = (*lst);
*lst = (*lst)->next; *lst = (*lst)->next;
//check that// //check that//
del->next = NULL;
ft_lst_delif(start, del->content, &ft_addrcmp, &token_free); ft_lst_delif(start, del->content, &ft_addrcmp, &token_free);
delete_newline(start, lst); delete_newline(start, lst);
return (0); return (0);