ajout execution des while, if, for, case, until
This commit is contained in:
parent
bac6e9d6d8
commit
26222441cc
14 changed files with 128 additions and 23 deletions
|
|
@ -86,6 +86,7 @@ exec/launch_file.c\
|
|||
exec/launch_for.c\
|
||||
exec/launch_if.c\
|
||||
exec/launch_process.c\
|
||||
exec/launch_until.c\
|
||||
exec/launch_while.c\
|
||||
exec/mark_process_status.c\
|
||||
exec/process_redirect.c\
|
||||
|
|
@ -107,6 +108,7 @@ exec/set_process_cmd.c\
|
|||
exec/set_process_for.c\
|
||||
exec/set_process_if.c\
|
||||
exec/set_process_map.c\
|
||||
exec/set_process_until.c\
|
||||
exec/set_process_while.c\
|
||||
exec/token_to_argv.c\
|
||||
glob/command_getoutput.c\
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/07 21:08:54 by wescande ### ########.fr */
|
||||
/* Updated: 2017/03/07 22:23:06 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -83,9 +83,11 @@ enum e_process_type
|
|||
PROCESS_FILE,
|
||||
PROCESS_SUBSHELL,
|
||||
PROCESS_WHILE,
|
||||
PROCESS_UNTIL,
|
||||
PROCESS_IF,
|
||||
PROCESS_FOR,
|
||||
PROCESS_CASE,
|
||||
PROCESS_MAX
|
||||
};
|
||||
|
||||
typedef enum e_process_type t_process_type;
|
||||
|
|
@ -184,6 +186,7 @@ int error_badidentifier(char *name);
|
|||
int launch_process(t_process *p);
|
||||
int launch_if(t_process *p);
|
||||
int launch_while(t_process *p);
|
||||
int launch_until(t_process *p);
|
||||
int launch_for(t_process *p);
|
||||
int launch_case(t_process *p);
|
||||
int launch_file(t_process *p);
|
||||
|
|
@ -195,4 +198,9 @@ int launch_builtin(t_process *p);
|
|||
int set_process(t_process *p, t_btree *ast);
|
||||
int set_process_map(t_process *p, t_btree *ast, t_cmd *cmd);
|
||||
int set_process_cmd(t_process *p, t_btree *ast, t_cmd *cmd);
|
||||
int set_process_while(t_process *p, t_btree *ast, t_cmd *cmd);
|
||||
int set_process_until(t_process *p, t_btree *ast, t_cmd *cmd);
|
||||
int set_process_if(t_process *p, t_btree *ast, t_cmd *cmd);
|
||||
int set_process_for(t_process *p, t_btree *ast, t_cmd *cmd);
|
||||
int set_process_case(t_process *p, t_btree *ast, t_cmd *cmd);
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/07 15:47:30 by wescande #+# #+# */
|
||||
/* Updated: 2017/03/07 17:34:51 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/03/07 21:30:29 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -17,9 +17,11 @@ int exec_leaf(t_btree **ast)
|
|||
t_process p;
|
||||
t_job *job;
|
||||
|
||||
DG("in exec leaf");
|
||||
job = &data_singleton()->exec.job;
|
||||
if (set_process(&p, *ast))
|
||||
return (1);
|
||||
DG("set_process done");
|
||||
if (!(launch_process(&p)))
|
||||
{
|
||||
job_addprocess(&p);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/07 14:53:31 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/07 20:58:31 by wescande ### ########.fr */
|
||||
/* Updated: 2017/03/07 21:33:56 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -16,6 +16,7 @@ int launch_file(t_process *p)
|
|||
{
|
||||
int pid;
|
||||
|
||||
DG("in file");
|
||||
pid = fork();
|
||||
if (pid == 0)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/12/13 14:20:45 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/07 21:07:33 by wescande ### ########.fr */
|
||||
/* Updated: 2017/03/07 22:21:07 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -19,6 +19,7 @@ t_itof g_launchmap[] =
|
|||
{PROCESS_FILE, launch_file},
|
||||
{PROCESS_SUBSHELL, NULL},
|
||||
{PROCESS_WHILE, launch_while},
|
||||
{PROCESS_UNTIL, launch_until},
|
||||
{PROCESS_IF, launch_if},
|
||||
{PROCESS_FOR, launch_for},
|
||||
{PROCESS_CASE, launch_case},
|
||||
|
|
@ -27,9 +28,21 @@ t_itof g_launchmap[] =
|
|||
|
||||
int launch_process(t_process *p)
|
||||
{
|
||||
int i;
|
||||
// int i;
|
||||
int pid;
|
||||
|
||||
if (p->type >= PROCESS_MAX)
|
||||
return (-1);
|
||||
if (!g_launchmap[p->type].f)
|
||||
return (-1);
|
||||
p->attrs &= ~PROCESS_STATE_MASK;
|
||||
p->attrs |= PROCESS_RUNNING;
|
||||
if (!(pid = (*g_launchmap[p->type].f)(p)))
|
||||
return (-1);
|
||||
p->pid = pid;
|
||||
process_setgroup(p, pid);
|
||||
return (0);
|
||||
/*
|
||||
i = 0;
|
||||
while (g_launchmap[i].id)
|
||||
{
|
||||
|
|
@ -49,4 +62,5 @@ int launch_process(t_process *p)
|
|||
i++;
|
||||
}
|
||||
return (-1);
|
||||
*/
|
||||
}
|
||||
|
|
|
|||
49
42sh/src/exec/launch_until.c
Normal file
49
42sh/src/exec/launch_until.c
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* launch_until.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/07 22:04:42 by wescande #+# #+# */
|
||||
/* Updated: 2017/03/07 22:06:50 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
static int do_until(t_process *p)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ft_exec(&p->data.d_while.condition);
|
||||
ret = ft_atoi(ft_getenv(data_singleton()->env, "?"));
|
||||
while (ft_strcmp(ft_getenv(data_singleton()->env, "?"), "0"))
|
||||
{
|
||||
ft_exec(&p->data.d_while.content);
|
||||
ret = ft_atoi(ft_getenv(data_singleton()->env, "?"));
|
||||
ft_exec(&p->data.d_while.condition);
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
|
||||
int launch_until(t_process *p)
|
||||
{
|
||||
pid_t pid;
|
||||
|
||||
if (SH_IS_INTERACTIVE(data_singleton()->opts))
|
||||
{
|
||||
pid = fork();
|
||||
if (pid == 0)
|
||||
{
|
||||
data_singleton()->opts &= ~SH_INTERACTIVE;
|
||||
data_singleton()->opts &= ~SH_OPTS_JOBC;
|
||||
exit(do_until(p));
|
||||
}
|
||||
else if (pid > 0)
|
||||
return (pid);
|
||||
}
|
||||
else
|
||||
do_until(p);
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/12/13 17:07:10 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/07 16:54:13 by wescande ### ########.fr */
|
||||
/* Updated: 2017/03/07 21:44:37 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/07 20:36:04 by wescande #+# #+# */
|
||||
/* Updated: 2017/03/07 21:14:19 by wescande ### ########.fr */
|
||||
/* Updated: 2017/03/07 22:01:20 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -17,5 +17,6 @@ int set_process_case(t_process *p, t_btree *ast, t_cmd *cmd)
|
|||
(void)cmd;
|
||||
p->data.d_case.list_word = ft_ld_copy(((t_astnode *)ast->item)->data.cmd.wordlist, tab_esc_copy);
|
||||
p->data.d_case.content = ast_copy(ast->right);
|
||||
p->type = PROCESS_CASE;
|
||||
return (0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/07 15:06:05 by wescande #+# #+# */
|
||||
/* Updated: 2017/03/07 21:12:31 by wescande ### ########.fr */
|
||||
/* Updated: 2017/03/07 21:44:40 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/07 19:38:05 by wescande #+# #+# */
|
||||
/* Updated: 2017/03/07 21:14:33 by wescande ### ########.fr */
|
||||
/* Updated: 2017/03/07 22:00:34 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -17,5 +17,6 @@ int set_process_for(t_process *p, t_btree *ast, t_cmd *cmd)
|
|||
(void)cmd;
|
||||
p->data.d_for.list_word = ft_ld_copy(((t_astnode *)ast->item)->data.cmd.wordlist, tab_esc_copy);
|
||||
p->data.d_for.content = ast_copy(ast->right);
|
||||
p->type = PROCESS_FOR;
|
||||
return (0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/07 19:19:50 by wescande #+# #+# */
|
||||
/* Updated: 2017/03/07 21:10:47 by wescande ### ########.fr */
|
||||
/* Updated: 2017/03/07 21:59:44 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -17,5 +17,6 @@ int set_process_if(t_process *p, t_btree *ast, t_cmd *cmd)
|
|||
(void)cmd;
|
||||
p->data.d_if.condition = ast_copy(ast->left);
|
||||
p->data.d_if.content = ast_copy(ast->right);
|
||||
p->type = PROCESS_IF;
|
||||
return (0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/07 15:08:12 by wescande #+# #+# */
|
||||
/* Updated: 2017/03/07 15:50:56 by wescande ### ########.fr */
|
||||
/* Updated: 2017/03/07 22:24:21 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -20,13 +20,13 @@ t_itof g_setprocessmap[] =
|
|||
{TK_AND_IF, NULL},
|
||||
{TK_OR_IF,NULL},
|
||||
{TK_PIPE, NULL},
|
||||
{TK_WHILE, NULL},
|
||||
{TK_IF, NULL},
|
||||
{TK_WHILE, &set_process_while},
|
||||
{TK_IF, &set_process_if},
|
||||
{TK_ELIF, NULL},
|
||||
{TK_ELSE, NULL},
|
||||
{TK_UNTIL, NULL},
|
||||
{TK_FOR, NULL},
|
||||
{TK_CASE, NULL},
|
||||
{TK_UNTIL, &set_process_until},
|
||||
{TK_FOR, &set_process_for},
|
||||
{TK_CASE, &set_process_case},
|
||||
{TK_PAREN_OPEN, NULL},
|
||||
{TK_ASSIGNEMENT_WORD, NULL},
|
||||
{MATH, NULL},
|
||||
|
|
@ -38,15 +38,18 @@ t_itof g_setprocessmap[] =
|
|||
int set_process_map(t_process *p, t_btree *ast, t_cmd *cmd)
|
||||
{
|
||||
int i;
|
||||
t_astnode *item;
|
||||
|
||||
i = 0;
|
||||
i = -1;
|
||||
if (!ast)
|
||||
return (0);
|
||||
while (g_setprocessmap[i].id)
|
||||
{
|
||||
if (p->type == g_setprocessmap[i].id)
|
||||
item = ast->item;
|
||||
while (g_setprocessmap[++i].id)
|
||||
if (item->type == g_setprocessmap[i].id)
|
||||
{
|
||||
if (!g_setprocessmap[i].f)
|
||||
return (0);
|
||||
return ((*g_setprocessmap[i].f)(p, ast, cmd));
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
|
|
|||
22
42sh/src/exec/set_process_until.c
Normal file
22
42sh/src/exec/set_process_until.c
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* set_process_until.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/07 22:22:24 by wescande #+# #+# */
|
||||
/* Updated: 2017/03/07 22:22:36 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
int set_process_until(t_process *p, t_btree *ast, t_cmd *cmd)
|
||||
{
|
||||
(void)cmd;
|
||||
p->data.d_while.condition = ast_copy(ast->left);
|
||||
p->data.d_while.content = ast_copy(ast->right);
|
||||
p->type = PROCESS_UNTIL;
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/07 19:38:10 by wescande #+# #+# */
|
||||
/* Updated: 2017/03/07 21:11:38 by wescande ### ########.fr */
|
||||
/* Updated: 2017/03/07 21:57:55 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -17,5 +17,6 @@ int set_process_while(t_process *p, t_btree *ast, t_cmd *cmd)
|
|||
(void)cmd;
|
||||
p->data.d_while.condition = ast_copy(ast->left);
|
||||
p->data.d_while.content = ast_copy(ast->right);
|
||||
p->type = PROCESS_WHILE;
|
||||
return (0);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue