exec flow control
This commit is contained in:
parent
a154aa85b6
commit
9c0984658e
20 changed files with 158 additions and 60 deletions
|
|
@ -74,12 +74,14 @@ exec/exec_redir.c\
|
||||||
exec/exec_semi.c\
|
exec/exec_semi.c\
|
||||||
exec/exec_until.c\
|
exec/exec_until.c\
|
||||||
exec/exec_while.c\
|
exec/exec_while.c\
|
||||||
|
exec/exec_var.c\
|
||||||
|
exec/exec_for.c\
|
||||||
|
exec/exec_case.c\
|
||||||
|
exec/exec_case_branch.c\
|
||||||
exec/fd_is_valid.c\
|
exec/fd_is_valid.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_resetfds.c\
|
exec/process_resetfds.c\
|
||||||
|
|
|
||||||
|
|
@ -35,8 +35,6 @@
|
||||||
# define IS_PIPEEND(a) (a & PROCESS_PIPEEND)
|
# define IS_PIPEEND(a) (a & PROCESS_PIPEEND)
|
||||||
# define IS_PIPESINGLE(a) ((a & PROCESS_PIPESTART) && (a & PROCESS_PIPEEND))
|
# define IS_PIPESINGLE(a) ((a & PROCESS_PIPESTART) && (a & PROCESS_PIPEEND))
|
||||||
|
|
||||||
# define SCRIPT_LOOP (1 << 0)
|
|
||||||
|
|
||||||
# include "libft.h"
|
# include "libft.h"
|
||||||
# include "types.h"
|
# include "types.h"
|
||||||
# include "job_control.h"
|
# include "job_control.h"
|
||||||
|
|
@ -53,7 +51,9 @@ struct s_process
|
||||||
int toclose;
|
int toclose;
|
||||||
int status;
|
int status;
|
||||||
t_flag attributes;
|
t_flag attributes;
|
||||||
t_flag script;
|
t_condition if_branch;
|
||||||
|
t_condition case_branch;
|
||||||
|
char *case_pattern;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct s_exec
|
struct s_exec
|
||||||
|
|
@ -102,7 +102,10 @@ int exec_elif(t_btree **ast);
|
||||||
int exec_else(t_btree **ast);
|
int exec_else(t_btree **ast);
|
||||||
int exec_until(t_btree **ast);
|
int exec_until(t_btree **ast);
|
||||||
int exec_default(t_btree **ast);
|
int exec_default(t_btree **ast);
|
||||||
|
int exec_var(t_btree **ast);
|
||||||
|
int exec_for(t_btree **ast);
|
||||||
|
int exec_case(t_btree **ast);
|
||||||
|
int exec_case_branch(t_btree **ast);
|
||||||
|
|
||||||
int launch_process(t_process *p);
|
int launch_process(t_process *p);
|
||||||
int process_setexec(t_type type, t_process *p);
|
int process_setexec(t_type type, t_process *p);
|
||||||
|
|
@ -128,7 +131,6 @@ void set_exitstatus(int status, int override);
|
||||||
|
|
||||||
void ast_free(void *data, size_t content_size);
|
void ast_free(void *data, size_t content_size);
|
||||||
|
|
||||||
|
char **token_to_argv(t_astnode *node);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,8 @@ struct s_data
|
||||||
t_comp *comp;
|
t_comp *comp;
|
||||||
t_exec exec;
|
t_exec exec;
|
||||||
t_jobc jobc;
|
t_jobc jobc;
|
||||||
|
char **local_var;
|
||||||
|
t_list *lst_func;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern t_stof g_builtins[];
|
extern t_stof g_builtins[];
|
||||||
|
|
|
||||||
|
|
@ -121,6 +121,7 @@ int add_subshell_cmd(t_btree **ast, t_list **lst);
|
||||||
int add_subshell_sep(t_btree **ast, t_list **lst);
|
int add_subshell_sep(t_btree **ast, t_list **lst);
|
||||||
int add_func_cmd(t_btree **ast, t_list **lst);
|
int add_func_cmd(t_btree **ast, t_list **lst);
|
||||||
int add_func_sep(t_btree **ast, t_list **lst);
|
int add_func_sep(t_btree **ast, t_list **lst);
|
||||||
|
int add_one_func(t_btree **ast, t_list **lst);
|
||||||
int isloop(t_btree **ast, t_list **lst);
|
int isloop(t_btree **ast, t_list **lst);
|
||||||
int isdir(t_btree **ast, t_list **lst);
|
int isdir(t_btree **ast, t_list **lst);
|
||||||
int iscase(t_btree **ast, t_list **lst);
|
int iscase(t_btree **ast, t_list **lst);
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@ typedef union u_astdata t_astdata;
|
||||||
typedef union u_word t_word;
|
typedef union u_word t_word;
|
||||||
typedef long long t_type;
|
typedef long long t_type;
|
||||||
|
|
||||||
|
typedef int t_condition;
|
||||||
typedef struct s_job t_job;
|
typedef struct s_job t_job;
|
||||||
typedef struct s_execmap t_execmap;
|
typedef struct s_execmap t_execmap;
|
||||||
typedef struct s_redirmap t_redirmap;
|
typedef struct s_redirmap t_redirmap;
|
||||||
|
|
|
||||||
|
|
@ -1,27 +1,25 @@
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* loop_exec.c :+: :+: :+: */
|
/* exec_if.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/01/30 21:02:10 by ariard #+# #+# */
|
/* Created: 2017/02/06 18:07:31 by ariard #+# #+# */
|
||||||
/* Updated: 2017/01/31 19:27:49 by ariard ### ########.fr */
|
/* Updated: 2017/02/20 22:31:46 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "exec.h"
|
#include "exec.h"
|
||||||
|
|
||||||
int loop_exec(t_list *list_ast)
|
int exec_case(t_btree **ast)
|
||||||
{
|
{
|
||||||
|
t_astnode *node;
|
||||||
|
char **av;
|
||||||
|
|
||||||
// DG("begin exec loop");
|
data_singleton()->exec.process.case_branch = 0;
|
||||||
// ft_print_all_ast(*list_ast);
|
node = (*ast)->item;
|
||||||
while (list_ast)
|
av = token_to_argv(node);
|
||||||
{
|
data_singleton()->exec.process.case_pattern = av[0];
|
||||||
if (ft_exec((t_btree **)list_ast->content))
|
|
||||||
return (1);
|
|
||||||
list_ast = list_ast->next;
|
|
||||||
}
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
30
42sh/src/exec/exec_case_branch.c
Normal file
30
42sh/src/exec/exec_case_branch.c
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* exec_if.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2017/02/06 18:07:31 by ariard #+# #+# */
|
||||||
|
/* Updated: 2017/02/20 22:31:46 by jhalford ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "exec.h"
|
||||||
|
|
||||||
|
int exec_case_branch(t_btree **ast)
|
||||||
|
{
|
||||||
|
t_astnode *node;
|
||||||
|
char **av;
|
||||||
|
|
||||||
|
if (data_singleton()->exec.process.case_branch == 1)
|
||||||
|
return (0);
|
||||||
|
node = (*ast)->item;
|
||||||
|
av = token_to_argv(node);
|
||||||
|
if (ft_strcmp(av[0], data_singleton()->exec.process.case_pattern) == 1)
|
||||||
|
{
|
||||||
|
data_singleton()->exec.process.case_branch = 1;
|
||||||
|
ft_exec(&(*ast)->right);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
@ -12,7 +12,7 @@
|
||||||
|
|
||||||
#include "exec.h"
|
#include "exec.h"
|
||||||
|
|
||||||
static char **token_to_argv(t_astnode *node)
|
char **token_to_argv(t_astnode *node)
|
||||||
{
|
{
|
||||||
char **my_tab;
|
char **my_tab;
|
||||||
int index;
|
int index;
|
||||||
|
|
@ -20,7 +20,7 @@ static char **token_to_argv(t_astnode *node)
|
||||||
char **content;
|
char **content;
|
||||||
t_ld *ld;
|
t_ld *ld;
|
||||||
|
|
||||||
if (node->type == TK_COMMAND)
|
if (node->type == TK_WORD || node->type == TK_ASSIGNEMENT_WORD)
|
||||||
{
|
{
|
||||||
ld = node->data.token;
|
ld = node->data.token;
|
||||||
my_tab = NULL;
|
my_tab = NULL;
|
||||||
|
|
|
||||||
|
|
@ -14,14 +14,13 @@
|
||||||
|
|
||||||
int exec_elif(t_btree **ast)
|
int exec_elif(t_btree **ast)
|
||||||
{
|
{
|
||||||
int test;
|
if (data_singleton()->exec.process.if_branch == 1)
|
||||||
|
return (0);
|
||||||
(void)ast;
|
ft_exec(&(*ast)->left);
|
||||||
test = 0;
|
if (data_singleton()->exec.process.status == 1)
|
||||||
if (test)
|
|
||||||
{
|
{
|
||||||
|
data_singleton()->exec.process.if_branch = 1;
|
||||||
ft_exec(&(*ast)->right);
|
ft_exec(&(*ast)->right);
|
||||||
}
|
}
|
||||||
// btree_delone(ast, &ast_free);
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,10 @@
|
||||||
|
|
||||||
int exec_else(t_btree **ast)
|
int exec_else(t_btree **ast)
|
||||||
{
|
{
|
||||||
|
if (data_singleton()->exec.process.if_branch == 0)
|
||||||
|
{
|
||||||
|
data_singleton()->exec.process.if_branch = 1;
|
||||||
ft_exec(&(*ast)->right);
|
ft_exec(&(*ast)->right);
|
||||||
//btree_delone(ast, &ast_free);
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
35
42sh/src/exec/exec_for.c
Normal file
35
42sh/src/exec/exec_for.c
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* exec_until.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2017/02/06 20:42:20 by ariard #+# #+# */
|
||||||
|
/* Updated: 2017/02/06 20:42:21 by ariard ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "exec.h"
|
||||||
|
|
||||||
|
int exec_for(t_btree **ast)
|
||||||
|
{
|
||||||
|
t_astnode *node;
|
||||||
|
t_list *temp;
|
||||||
|
// char **av = NULL;
|
||||||
|
char *var;
|
||||||
|
|
||||||
|
|
||||||
|
node = (*ast)->item;
|
||||||
|
temp = node->data.wordlist;
|
||||||
|
var = temp->content;
|
||||||
|
builtin_setenv("setenv", (char*[]){var, 0}, data_singleton()->local_var);
|
||||||
|
while (temp)
|
||||||
|
{
|
||||||
|
//process expansion av = token_to_char(temp->content)
|
||||||
|
builtin_setenv("setenv", (char*[]){var, 0}, data_singleton()->local_var);
|
||||||
|
ft_exec(&(*ast)->right);
|
||||||
|
temp = temp->next;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
15
42sh/src/exec/exec_func.c
Normal file
15
42sh/src/exec/exec_func.c
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* exec_while.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2017/01/30 17:33:53 by ariard #+# #+# */
|
||||||
|
/* Updated: 2017/02/06 22:05:35 by ariard ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "exec.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -14,7 +14,12 @@
|
||||||
|
|
||||||
int exec_if(t_btree **ast)
|
int exec_if(t_btree **ast)
|
||||||
{
|
{
|
||||||
|
data_singleton()->exec.process.if_branch = 0;
|
||||||
|
ft_exec(&(*ast)->left);
|
||||||
|
if (data_singleton()->exec.process.status == 1)
|
||||||
|
{
|
||||||
|
data_singleton()->exec.process.if_branch = 1;
|
||||||
ft_exec(&(*ast)->right);
|
ft_exec(&(*ast)->right);
|
||||||
// btree_delone(ast, &ast_free);
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,12 +14,11 @@
|
||||||
|
|
||||||
int exec_until(t_btree **ast)
|
int exec_until(t_btree **ast)
|
||||||
{
|
{
|
||||||
int test;
|
ft_exec(&(*ast)->left);
|
||||||
|
while (data_singleton()->exec.process.status == 0)
|
||||||
test = 0;
|
{
|
||||||
while (test++ != 10)
|
|
||||||
ft_exec(&(*ast)->right);
|
ft_exec(&(*ast)->right);
|
||||||
|
ft_exec(&(*ast)->left);
|
||||||
// btree_delone(ast, &ast_free);
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,27 +1,24 @@
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* loop_del.c :+: :+: :+: */
|
/* exec_while.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/01/31 22:39:20 by ariard #+# #+# */
|
/* Created: 2017/01/30 17:33:53 by ariard #+# #+# */
|
||||||
/* Updated: 2017/01/31 23:20:40 by ariard ### ########.fr */
|
/* Updated: 2017/02/06 22:05:35 by ariard ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "exec.h"
|
#include "exec.h"
|
||||||
|
|
||||||
int loop_del(t_list *list_ast)
|
int exec_var(t_btree **ast)
|
||||||
{
|
{
|
||||||
t_list *temp;
|
t_astnode *node;
|
||||||
|
char **av;
|
||||||
|
|
||||||
while (list_ast)
|
node = (*ast)->item;
|
||||||
{
|
av = token_to_argv(node);
|
||||||
btree_del((t_btree **)list_ast->content, &ast_free);
|
builtin_setenv("setenv", av, data_singleton()->local_var);
|
||||||
temp = list_ast;
|
|
||||||
list_ast = list_ast->next;
|
|
||||||
ft_lstdelone(&temp, NULL);
|
|
||||||
}
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
@ -14,12 +14,11 @@
|
||||||
|
|
||||||
int exec_while(t_btree **ast)
|
int exec_while(t_btree **ast)
|
||||||
{
|
{
|
||||||
int test;
|
ft_exec(&(*ast)->left);
|
||||||
|
while (data_singleton()->exec.process.status == 1)
|
||||||
test = 10;
|
{
|
||||||
while (test--)
|
|
||||||
ft_exec(&(*ast)->right);
|
ft_exec(&(*ast)->right);
|
||||||
|
ft_exec(&(*ast)->left);
|
||||||
// btree_delone(ast, &ast_free);
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,6 @@ int data_init(void)
|
||||||
data->env = ft_sstrdup(environ);
|
data->env = ft_sstrdup(environ);
|
||||||
data->comp = NULL;
|
data->comp = NULL;
|
||||||
data->opts = SH_OPTS_JOBC;
|
data->opts = SH_OPTS_JOBC;
|
||||||
|
|
||||||
data->exec.process.path = NULL;
|
data->exec.process.path = NULL;
|
||||||
data->exec.process.av = NULL;
|
data->exec.process.av = NULL;
|
||||||
data->exec.process.toclose = STDIN;
|
data->exec.process.toclose = STDIN;
|
||||||
|
|
@ -45,6 +44,9 @@ int data_init(void)
|
||||||
|
|
||||||
data->jobc.first_job = NULL;
|
data->jobc.first_job = NULL;
|
||||||
data->jobc.current_id = 1;
|
data->jobc.current_id = 1;
|
||||||
|
|
||||||
|
data->local_var = NULL;
|
||||||
|
data->lst_func = NULL;
|
||||||
if ((term_name = ft_getenv(data->env, "TERM")) == NULL)
|
if ((term_name = ft_getenv(data->env, "TERM")) == NULL)
|
||||||
return (-1);
|
return (-1);
|
||||||
if (tgetent(NULL, term_name) != 1)
|
if (tgetent(NULL, term_name) != 1)
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ int add_cmd(t_btree **ast, t_list **lst)
|
||||||
my_tab = NULL;
|
my_tab = NULL;
|
||||||
node = (*ast)->item;
|
node = (*ast)->item;
|
||||||
node->type = token->type;
|
node->type = token->type;
|
||||||
if (token->type == TK_WORD)
|
if (token->type == TK_WORD || token->type == TK_ASSIGNEMENT_WORD)
|
||||||
{
|
{
|
||||||
my_tab = ft_sstradd(my_tab, token->data);
|
my_tab = ft_sstradd(my_tab, token->data);
|
||||||
my_tab = ft_sstradd(my_tab, (char *)token->esc);
|
my_tab = ft_sstradd(my_tab, (char *)token->esc);
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,10 @@ int isfunc(t_btree **ast, t_list **lst)
|
||||||
{
|
{
|
||||||
node = (*ast)->item;
|
node = (*ast)->item;
|
||||||
if (node->type == TK_WORD && token->type == CLOSE_LIST)
|
if (node->type == TK_WORD && token->type == CLOSE_LIST)
|
||||||
|
{
|
||||||
node->type = FNAME;
|
node->type = FNAME;
|
||||||
|
add_one_func(ast, lst);
|
||||||
|
}
|
||||||
if (node->type == FNAME && node->full == 0)
|
if (node->type == FNAME && node->full == 0)
|
||||||
return (1);
|
return (1);
|
||||||
if (isfunc(&(*ast)->right, lst) == 1)
|
if (isfunc(&(*ast)->right, lst) == 1)
|
||||||
|
|
@ -62,4 +65,9 @@ int add_func_sep(t_btree **ast, t_list **lst)
|
||||||
return (add_sep(&(*ast)->right, lst));
|
return (add_sep(&(*ast)->right, lst));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int add_one_func(t_btree **ast, t_list **lst)
|
||||||
|
{
|
||||||
|
(void)lst;
|
||||||
|
ft_lsteadd(&data_singleton()->lst_func, ft_lstnew(ast, sizeof(*ast)));
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue