From 74b9f837797e5db1584645f406acb2149ceb3792 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Tue, 7 Mar 2017 15:52:54 +0100 Subject: [PATCH 1/2] renforcement stack + correction erreur syntax --- 42sh/src/main/main.c | 3 ++- 42sh/src/parser/aggregate_sym.c | 12 ++++++++---- 42sh/src/parser/eval_sym.c | 26 +++++++++++++++++++++++++- 42sh/src/parser/ft_parse.c | 2 +- 42sh/src/parser/pop_stack.c | 9 +++++---- 42sh/src/parser/produce_sym.c | 4 +++- 6 files changed, 44 insertions(+), 12 deletions(-) diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 79ddbe24..7f399ee6 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/03/06 17:58:39 by ariard ### ########.fr */ +/* Updated: 2017/03/07 14:43:24 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,6 +52,7 @@ 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) diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index 25d6f683..54e8da17 100644 --- a/42sh/src/parser/aggregate_sym.c +++ b/42sh/src/parser/aggregate_sym.c @@ -99,7 +99,7 @@ t_aggrematch g_aggrematch[] = {NEWLINE_LIST, COMPLETE_COMMANDS, LINEBREAK, 0}, {NEWLINE_LIST, LINEBREAK, PROGRAM, LINEBREAK}, {NEWLINE_LIST, FOR_WORDLIST, SEQUENTIAL_SEP, 0}, - + {SEQUENTIAL_SEP, FOR_WORDLIST, SEQUENTIAL_SEP, FOR_WORDLIST}, //to check {FILENAME, TK_LESS, IO_FILE, TK_LESS}, {FILENAME, TK_LESSAND, IO_FILE, TK_LESSAND}, @@ -322,6 +322,8 @@ int aggregate_sym(t_list **stack, t_sym *new_sym, t_parstate *state) t_sym *head; int i; + if (!*stack || !*new_sym || !*state) + return (1); i = 0; head = (*stack)->content; DG("aggregate head %s && sym %s", @@ -336,13 +338,15 @@ int aggregate_sym(t_list **stack, t_sym *new_sym, t_parstate *state) *new_sym = g_aggrematch[i].new_sym; if (g_aggrematch[i].erase_sym) { - if (pop_stack(stack, g_aggrematch[i].erase_sym)) - return (1); + pop_stack(stack, g_aggrematch[i].erase_sym); head = (*stack)->content; DG("stack after pop: %s", read_state(*head)); } if (eval_sym(stack, *new_sym)) - return ((*state = ERROR)); + { + *state = ERROR; + return (1); + } aggregate_sym(stack, new_sym, state); return (0); } diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c index d9a3de5c..0d5a3c09 100644 --- a/42sh/src/parser/eval_sym.c +++ b/42sh/src/parser/eval_sym.c @@ -343,7 +343,6 @@ t_stackmatch g_stackmatch[] = {TK_LBRACE, CASE_LIST_NS}, {TK_LBRACE, COMPLETE_CONDITION}, {TK_LBRACE, TK_IN}, - {SEQUENTIAL_SEP, NAME}, {TK_RBRACE, TK_SEMI}, {TK_RBRACE, END_COMMAND}, {TK_RBRACE, SEPARATOR_OP}, @@ -412,6 +411,7 @@ t_stackmatch g_stackmatch[] = {SEQUENTIAL_SEP, NAME}, {SEQUENTIAL_SEP, IN}, {SEQUENTIAL_SEP, WORDLIST}, + {SEQUENTIAL_SEP, FOR_WORDLIST}, {END_COMMAND, SEQUENCE}, {END_COMMAND, PIPE_SEMI_SEQUENCE}, {END_COMMAND, AND_OR_MAJOR}, @@ -810,6 +810,7 @@ t_stackmatch g_stackmatch[] = {PATTERN, CASE_LIST}, {PATTERN, TK_IN}, {CASE_LIST_NS, LINEBREAK}, + {CASE_LIST_NS, TK_IN}, {CASE_CLAUSE, LINEBREAK}, {CASE_CLAUSE, TK_PAREN_OPEN}, {CASE_CLAUSE, TK_LBRACE}, @@ -987,6 +988,26 @@ t_stackmatch g_stackmatch[] = {PIPE_SEMI_SEQUENCE, NEWLINE_LIST}, {PIPE_SEMI_SEQUENCE, AND_OR_MAJOR}, {SEQUENCE, PIPE_SEMI_SEQUENCE}, + {SEQUENCE, TK_WHILE}, + {SEQUENCE, TK_UNTIL}, + {SEQUENCE, TK_DO}, + {SEQUENCE, TK_PAREN_CLOSE}, + {SEQUENCE, TK_IF}, + {SEQUENCE, TK_ELIF}, + {SEQUENCE, TK_THEN}, + {SEQUENCE, TK_ELSE}, + {SEQUENCE, COMPOUND_LIST}, + {SEQUENCE, CASE_LIST_NS}, + {SEQUENCE, COMPLETE_CONDITION}, + {SEQUENCE, LINEBREAK}, + {SEQUENCE, TK_PAREN_OPEN}, + {SEQUENCE, TK_LBRACE}, + {SEQUENCE, COMPLETE_COMMANDS}, + {SEQUENCE, TK_BANG}, + {SEQUENCE, TK_BANG}, + {SEQUENCE, SEPARATOR_OP}, + {SEQUENCE, NEWLINE_LIST}, + {SEQUENCE, AND_OR_MAJOR}, {SEQUENCE, SEQUENCE}, {PIPELINE, TK_WHILE}, {PIPELINE, TK_UNTIL}, @@ -1038,6 +1059,7 @@ t_stackmatch g_stackmatch[] = {COMPLETE_COMMAND, COMPLETE_COMMANDS}, {COMPLETE_COMMAND, NEWLINE_LIST}, {COMPLETE_COMMANDS, COMPLETE_COMMANDS}, + {PROGRAM, TERMINUS}, {0, 0}, }; @@ -1046,6 +1068,8 @@ int eval_sym(t_list **stack, t_sym new_sym) t_sym *head; int i; + if (!*stack) + return (1); head = (*stack)->content; DG("eval head %s && sym %s", read_state(*head), read_state(new_sym)); i = 0; diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index f87d0bcb..dcd34b83 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -43,7 +43,7 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser) else { if (aggregate_sym(&parser->stack, parser->new_sym, &parser->state)) - return (1); + return (0); push_stack(&parser->stack, *parser->new_sym); } // ft_read_stack(parser->stack); diff --git a/42sh/src/parser/pop_stack.c b/42sh/src/parser/pop_stack.c index 1552953c..59e57a09 100644 --- a/42sh/src/parser/pop_stack.c +++ b/42sh/src/parser/pop_stack.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 19:12:44 by ariard #+# #+# */ -/* Updated: 2017/03/06 17:09:00 by ariard ### ########.fr */ +/* Updated: 2017/03/07 15:09:32 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,13 +17,14 @@ int pop_stack(t_list **stack, t_sym erase_sym) t_sym *head; t_list *temp; - while ((*stack) && *(head = (*stack)->content) != erase_sym) + while ((*stack) && (*(head = (*stack)->content) != erase_sym + && *head != TERMINUS)) { - temp = *stack; + temp = *stack; (*stack) = (*stack)->next; ft_lstdelone(&temp, NULL); } - if (*stack) + if ((*stack) && *(head = (*stack)->content) != TERMINUS) { temp = *stack; (*stack) = (*stack)->next; diff --git a/42sh/src/parser/produce_sym.c b/42sh/src/parser/produce_sym.c index 754f1205..3b60fa5e 100644 --- a/42sh/src/parser/produce_sym.c +++ b/42sh/src/parser/produce_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 17:58:34 by ariard #+# #+# */ -/* Updated: 2017/03/06 16:43:10 by ariard ### ########.fr */ +/* Updated: 2017/03/07 15:02:19 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -108,6 +108,8 @@ int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst) t_sym *head; int i; + if (!*stack || !*lst) + return (1); token = (*lst)->content; head = (*stack)->content; DG("produce stack : %s && token : %s", read_state(*head), From 2a215a65064ee2716df0571bfbc03ddc3abba493 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 7 Mar 2017 16:40:09 +0100 Subject: [PATCH 2/2] launch_file and launch_builtin --- 42sh/libft | 2 +- 42sh/src/exec/exec_command.c | 2 +- 42sh/src/exec/launch_builtin.c | 38 +++++++++++++++++++++++++++++++ 42sh/src/exec/launch_file.c | 36 ++++++++++------------------- 42sh/src/exec/launch_process.c | 15 +++++++++--- 42sh/src/exec/process_setexec.c | 22 +++++++++--------- 42sh/src/hash_table/ft_add_hash.c | 2 +- 42sh/src/hash_table/hash.c | 2 +- 42sh/src/main/data_init.c | 20 ++++------------ 42sh/src/main/main.c | 2 +- 42sh/src/main/shell_init.c | 2 +- 11 files changed, 84 insertions(+), 59 deletions(-) create mode 100644 42sh/src/exec/launch_builtin.c diff --git a/42sh/libft b/42sh/libft index 6a2672a1..bc489f86 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit 6a2672a19268c6481525d9aaee5bd35722bbd75a +Subproject commit bc489f8664fdc24317c31b3069811f54b1178643 diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index e3179079..ebff91f8 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ -/* Updated: 2017/03/07 15:09:45 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 16:39:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/launch_builtin.c b/42sh/src/exec/launch_builtin.c new file mode 100644 index 00000000..6f79c917 --- /dev/null +++ b/42sh/src/exec/launch_builtin.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* launch_builtin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/07 15:48:24 by jhalford #+# #+# */ +/* Updated: 2017/03/07 16:39:52 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int launch_builtin(t_process *p) +{ + pid_t pid; + + if (IS_PIPESINGLE(*p)) + { + if (process_redirect(p)) + return (1); + set_exitstatus((*p->execf)(p->path, p->av, data_singleton()->env), 1); + return (1); + } + pid = fork(); + if (pid == 0) + { + process_setgroup(p, 0); + process_setsig(); + if (process_redirect(p)) + exit (1); + exit((*p->execf)(p->path, p->av, data_singleton()->env)); + } + else if (pid > 0) + return (0); + else if (pid == -1) + ft_dprintf(2, "{red}%s: internal fork error{eoc}\n", SHELL_NAME); + return (1); +} diff --git a/42sh/src/exec/launch_file.c b/42sh/src/exec/launch_file.c index c482d419..c1e17302 100644 --- a/42sh/src/exec/launch_file.c +++ b/42sh/src/exec/launch_file.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 14:53:31 by jhalford #+# #+# */ -/* Updated: 2017/03/07 14:54:18 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 16:39:33 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,45 +14,33 @@ int launch_file(t_process *p) { int pid; - if (p->attrs & PROCESS_BUILTIN && IS_PIPESINGLE(*p)) - { - if (process_redirect(p)) - return (1); - set_exitstatus((*p->execf)(p->path, p->av, data_singleton()->env), 1); - return (1); - } - p->attrs &= ~PROCESS_STATE_MASK; - p->attrs |= PROCESS_RUNNING; - if (p->attrs & (PROCESS_BINARY | PROCESS_SCRIPT) - && access(p->path, X_OK) == -1) - { - ft_dprintf(2, "{red}%s: permission denied: %s{eoc}\n", SHELL_NAME, p->av[0]); - set_exitstatus(126, 1); - return (1); - } pid = fork(); if (pid == 0) { - if (p->attrs & PROCESS_UNKNOWN) + if (!p->stat) { ft_dprintf(2, "{red}%s: command not found: %s{eoc}\n", SHELL_NAME, p->av[0]); exit(127); } + else if (IS_DIR(p->stat.st_mode)) + { + ft_dprintf(2, "{red}%s: %s: Is a directory{eoc}\n", SHELL_NAME, p->av[0]); + exit(126); + } + else if (access(p->path, X_OK) == -1) + { + ft_dprintf(2, "{red}%s: permission denied: %s{eoc}\n", SHELL_NAME, p->av[0]); + exit(126); + } process_setgroup(p, 0); process_setsig(); if (process_redirect(p)) exit (1); - if (p->attrs & PROCESS_BUILTIN) - exit((*p->execf)(p->path, p->av, data_singleton()->env)); (*p->execf)(p->path, p->av, data_singleton()->env); ft_dprintf(2, "{red}%s: internal execve error on %s{eoc}\n", SHELL_NAME, p->av[0]); } else if (pid > 0) - { - p->pid = pid; - process_setgroup(p, pid); return (0); - } else if (pid == -1) ft_dprintf(2, "{red}%s: internal fork error{eoc}\n", SHELL_NAME); return (1); diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index ebf4c0eb..e9f758e8 100644 --- a/42sh/src/exec/launch_process.c +++ b/42sh/src/exec/launch_process.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 14:20:45 by jhalford #+# #+# */ -/* Updated: 2017/03/07 15:04:44 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 16:39:35 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ t_itof g_launchmap[] = { {PROCESS_FUNCTION, NULL}, - {PROCESS_BUILTIN, NULL}, + {PROCESS_BUILTIN, launch_builtin}, {PROCESS_FILE, launch_file}, {PROCESS_SUBSHELL, NULL}, {PROCESS_WHILE, NULL}, @@ -35,9 +35,18 @@ int launch_process(t_process *p) while (g_launchmap[i].type) { if (p->type == g_launchmap[i].type) + { if (!g_launchmap[i].f) return (-1); - return ((*g_launchmap[i].f)(p)); + p->attrs &= ~PROCESS_STATE_MASK; + p->attrs |= PROCESS_RUNNING; + if (!(*g_launchmap[i].f)(p)) + { + p->pid = pid; + process_setgroup(p, pid); + return (0); + } + } i++; } return (-1); diff --git a/42sh/src/exec/process_setexec.c b/42sh/src/exec/process_setexec.c index 1114f927..1f6c66ab 100644 --- a/42sh/src/exec/process_setexec.c +++ b/42sh/src/exec/process_setexec.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:07:10 by jhalford #+# #+# */ -/* Updated: 2017/03/07 14:49:45 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 16:39:31 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,27 +14,27 @@ int process_setexec(t_process *p) { - p->path = NULL; + p->data.cmd.path = NULL; + p->data.cmd.execf = NULL; /* if ((p->execf = is_function(p))) */ /* p->type = PROCESS_FUNCTION; */ if ((p->data.cmd.execf = is_builtin(p))) p->type = PROCESS_BUILTIN; - else if (ft_hash(p)) - { - p->data.cmd.execf = &execve; - p->type = PROCESS_FILE; - } else if (ft_strchr(p->av[0], '/')) { - p->data.cmd.execf = &execve; p->type = PROCESS_FILE; + p->data.cmd.execf = &execve; p->data.cmd.path = ft_strdup(p->av[0]); + stat(p->data.cmd.path, &p->data.cmd.stat); } else { - p->execf = NULL; - p->attrs |= PROCESS_UNKNOWN; - return (1); + p->type = PROCESS_FILE; + if (ft_hash(p)) + { + p->data.cmd.execf = &execve; + lstat(p->data.cmd.path, p->data.cmd.stat); + } } return (0); } diff --git a/42sh/src/hash_table/ft_add_hash.c b/42sh/src/hash_table/ft_add_hash.c index 403ee64d..53b0992c 100644 --- a/42sh/src/hash_table/ft_add_hash.c +++ b/42sh/src/hash_table/ft_add_hash.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/18 11:20:11 by gwojda #+# #+# */ -/* Updated: 2017/03/07 14:43:33 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 16:02:49 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/hash_table/hash.c b/42sh/src/hash_table/hash.c index b7022ec5..2276a4e1 100644 --- a/42sh/src/hash_table/hash.c +++ b/42sh/src/hash_table/hash.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/18 11:06:19 by gwojda #+# #+# */ -/* Updated: 2017/03/04 18:50:56 by ariard ### ########.fr */ +/* Updated: 2017/03/07 16:02:41 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 78d413f7..ce7e2a03 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */ -/* Updated: 2017/03/03 19:45:05 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 16:38:43 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,14 +23,6 @@ int data_init(void) data->env = ft_sstrdup(environ); data->comp = NULL; data->opts = 0; - /* data->exec.process.path = NULL; */ - /* data->exec.process.av = NULL; */ - /* data->exec.process.to_close = 0; */ - /* data->exec.process.fdin = STDIN; */ - /* data->exec.process.fdout = STDOUT; */ - /* data->exec.process.pid = 0; */ - /* data->exec.process.attributes = 0; */ - /* data->exec.process.redirs = NULL; */ data->exec.fd_save[0] = fcntl(0, F_DUPFD_CLOEXEC); data->exec.fd_save[1] = fcntl(1, F_DUPFD_CLOEXEC); data->exec.fd_save[2] = fcntl(2, F_DUPFD_CLOEXEC); @@ -38,12 +30,10 @@ int data_init(void) data->exec.fdin = STDIN; data->exec.attrs = 0; - /* data->exec.aol_status = NULL; */ - /* data->exec.aol_search = 0; */ - /* data->exec.job.id = 0; */ - /* data->exec.job.pgid = 0; */ - /* data->exec.job.attributes = 0; */ - /* data->exec.job.first_process = 0; */ + data->exec.job.id = 0; + data->exec.job.pgid = 0; + data->exec.job.attrs = 0; + data->exec.job.first_process = NULL; data->jobc.first_job = NULL; data->jobc.current_id = 1; diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index cf6c3dcc..2673700d 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/03/07 15:09:53 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 15:43:54 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index 272440d7..abf15a8e 100644 --- a/42sh/src/main/shell_init.c +++ b/42sh/src/main/shell_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */ -/* Updated: 2017/03/03 19:45:52 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 16:38:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */