From e9633119e5467ebca1580e6597891ea60ef52212 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 6 Mar 2017 16:58:47 +0100 Subject: [PATCH 1/4] >& and <& check if fd is open for reading/writing specifically --- 42sh/includes/exec.h | 4 ++-- 42sh/src/exec/fd_is_valid.c | 9 ++++++--- 42sh/src/exec/redirect_greatand.c | 4 ++-- 42sh/src/exec/redirect_lessand.c | 4 ++-- 42sh/src/main/shell_get_opts.c | 2 +- 42sh/yolo | 0 6 files changed, 13 insertions(+), 10 deletions(-) delete mode 100644 42sh/yolo diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 4afa61d6..81b783cb 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ -/* Updated: 2017/03/06 12:33:24 by wescande ### ########.fr */ +/* Updated: 2017/03/06 16:56:35 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -126,7 +126,7 @@ void process_free(void *content, size_t content_size); void process_reset(t_process *p); void process_resetfds(void); -int fd_is_valid(int fd); +int fd_is_valid(int fd, int flag); int bad_fd(int fd); int process_redirect(t_process *p); int redirect_great(t_redir *redir); diff --git a/42sh/src/exec/fd_is_valid.c b/42sh/src/exec/fd_is_valid.c index d89e0f49..39fac840 100644 --- a/42sh/src/exec/fd_is_valid.c +++ b/42sh/src/exec/fd_is_valid.c @@ -6,13 +6,16 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/03 13:46:40 by jhalford #+# #+# */ -/* Updated: 2017/03/05 19:44:16 by jhalford ### ########.fr */ +/* Updated: 2017/03/06 16:58:38 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int fd_is_valid(int fd) +int fd_is_valid(int fd, int has_flag) { - return (fcntl(fd, F_GETFD) != -1 || errno != EBADF); + int flags; + + flags = fcntl(fd, F_GETFD); + return ((flags != -1 || errno != EBADF) && flags & has_flag); } diff --git a/42sh/src/exec/redirect_greatand.c b/42sh/src/exec/redirect_greatand.c index c210c647..002375e8 100644 --- a/42sh/src/exec/redirect_greatand.c +++ b/42sh/src/exec/redirect_greatand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:12:31 by jhalford #+# #+# */ -/* Updated: 2017/03/05 19:44:10 by jhalford ### ########.fr */ +/* Updated: 2017/03/06 16:54:43 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,7 +33,7 @@ int redirect_greatand(t_redir *redir) return (0); if (fdold > 9) return (bad_fd(fdold)); - if (fd_is_valid(fdold)) + if (fd_is_valid(fdold, O_RDONLY)) dup2_close(fdold, fdnew); else return (bad_fd(fdold)); diff --git a/42sh/src/exec/redirect_lessand.c b/42sh/src/exec/redirect_lessand.c index 0c3b5982..bb9987d6 100644 --- a/42sh/src/exec/redirect_lessand.c +++ b/42sh/src/exec/redirect_lessand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:11:18 by jhalford #+# #+# */ -/* Updated: 2017/03/05 19:43:42 by jhalford ### ########.fr */ +/* Updated: 2017/03/06 16:53:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,7 +33,7 @@ int redirect_lessand(t_redir *redir) return (0); if (fdold > 9) return (bad_fd(fdold)); - if (fd_is_valid(fdold)) + if (fd_is_valid(fdold, O_WRONLY)) dup2_close(fdold, fdnew); else return (bad_fd(fdold)); diff --git a/42sh/src/main/shell_get_opts.c b/42sh/src/main/shell_get_opts.c index 0eee3eb4..dc75dddd 100644 --- a/42sh/src/main/shell_get_opts.c +++ b/42sh/src/main/shell_get_opts.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/11 14:04:48 by jhalford #+# #+# */ -/* Updated: 2017/03/05 17:45:37 by jhalford ### ########.fr */ +/* Updated: 2017/03/06 16:52:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/yolo b/42sh/yolo deleted file mode 100644 index e69de29b..00000000 From 14be331b4eef1a7277f5de92bd5af806cab5e969 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Tue, 7 Mar 2017 12:22:05 +0100 Subject: [PATCH 2/4] expr math doing --- 42sh/Makefile | 1 + 42sh/includes/exec.h | 3 +- 42sh/libft | 2 +- 42sh/src/builtin/bt_read_get.c | 2 +- 42sh/src/builtin/builtin_env.c | 2 +- 42sh/src/builtin/builtin_setenv.c | 2 +- 42sh/src/exec/exec_math.c | 81 +++++++++++++++++++++++++++++++ 42sh/src/exec/exec_var.c | 6 +-- 42sh/src/exec/ft_exec.c | 3 +- 42sh/src/parser/add_case.c | 3 +- 42sh/src/parser/add_func.c | 4 +- 42sh/src/parser/add_var.c | 2 +- 12 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 42sh/src/exec/exec_math.c diff --git a/42sh/Makefile b/42sh/Makefile index fe951535..ce3e375c 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -77,6 +77,7 @@ exec/exec_semi.c\ exec/exec_until.c\ exec/exec_var.c\ exec/exec_while.c\ +exec/exec_math.c\ exec/fd_is_valid.c\ exec/ft_exec.c\ exec/ft_findexec.c\ diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index d43904a2..0aacdb69 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ -/* Updated: 2017/03/06 19:16:30 by ariard ### ########.fr */ +/* Updated: 2017/03/07 11:47:50 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -116,6 +116,7 @@ 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 exec_math(t_btree **ast); int launch_process(t_process *p); int set_process(t_process *p, t_btree *ast); diff --git a/42sh/libft b/42sh/libft index dc155bf5..318efc7c 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit dc155bf51cc9de83df073669a1d2a9a915f16121 +Subproject commit 318efc7cfb7b7cc9d3714fa19fd2be7382b6adec diff --git a/42sh/src/builtin/bt_read_get.c b/42sh/src/builtin/bt_read_get.c index 62700b5a..f2e70ab1 100644 --- a/42sh/src/builtin/bt_read_get.c +++ b/42sh/src/builtin/bt_read_get.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/21 18:00:03 by jhalford #+# #+# */ -/* Updated: 2017/02/03 15:58:41 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 11:27:49 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_env.c b/42sh/src/builtin/builtin_env.c index 100cd236..9458300d 100644 --- a/42sh/src/builtin/builtin_env.c +++ b/42sh/src/builtin/builtin_env.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */ -/* Updated: 2017/03/03 16:07:30 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 11:29:18 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_setenv.c b/42sh/src/builtin/builtin_setenv.c index 3c803bd5..d5d5f99f 100644 --- a/42sh/src/builtin/builtin_setenv.c +++ b/42sh/src/builtin/builtin_setenv.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */ -/* Updated: 2017/02/17 13:18:25 by gwojda ### ########.fr */ +/* Updated: 2017/03/07 11:28:05 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_math.c b/42sh/src/exec/exec_math.c new file mode 100644 index 00000000..9bf8a3c7 --- /dev/null +++ b/42sh/src/exec/exec_math.c @@ -0,0 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_math.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/07 10:58:49 by ariard #+# #+# */ +/* Updated: 2017/03/07 12:19:25 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +static int get_math(char *stream, char **var, char **value, char **operator) +{ + char *temp; + + *var = ft_strduptr(stream, &ft_isalpha); + temp = ft_sstrstr(data_singleton()->env, *var); + if (temp) + { + temp += ft_strlenchr(temp, '=') + 1; + *value = ft_strdup(temp); + if (!(ft_stris(*value, &ft_isdigit))) + { + ft_strdel(value); + *value = ft_itoa(0); + } + } + else + *value = ft_itoa(0); + stream += ft_strlen(*var); + *operator = ft_strdup(stream); + return (0); +} + +static int do_math(char **value, char *operator) +{ + long ope1; + long ope2; + + ope1 = ft_atoi(*value); + if (operator[2]) + ope2 = ft_atoi(&operator[2]); + else + ope2 = 0; + if (operator[0] == '/' && ope2 == 0) + ope1 = 0; + else + { + ope1 = (operator[0] == '+') ? ope1 + ope2 : ope1; + ope1 = (operator[0] == '-') ? ope1 - ope2 : ope1; + ope1 = (operator[0] == '/') ? ope1 / ope2 : ope1; + ope1 = (operator[0] == '*') ? ope1 * ope2 : ope1; + ope1 = (operator[0] == '%') ? ope1 % ope2 : ope1; + } + ft_strdel(value); + *value = ft_itoa(ope1); + return (0); +} + +int exec_math(t_btree **ast) +{ + t_astnode *node; + char **av; + char *var; + char *value; + char *operator; + + node = (*ast)->item; + av = token_to_argv(node->data.cmd.wordlist, 1); + get_math(av[0], &var, &value, &operator); + DG("var : %s", var); + DG("value : %s", value); + DG("operator : %s", operator); + do_math(&value, operator); + DG("value : %s", value); + builtin_setenv("setenv", (char *[]){var, value, 0}, data_singleton()->local_var); + return (0); +} diff --git a/42sh/src/exec/exec_var.c b/42sh/src/exec/exec_var.c index 3bfc50ce..ac98e0cb 100644 --- a/42sh/src/exec/exec_var.c +++ b/42sh/src/exec/exec_var.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* exec_while.c :+: :+: :+: */ +/* exec_var.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/01/30 17:33:53 by ariard #+# #+# */ -/* Updated: 2017/03/03 20:32:27 by wescande ### ########.fr */ +/* Created: 2017/03/07 11:12:05 by ariard #+# #+# */ +/* Updated: 2017/03/07 12:17:13 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index b24b7612..2a8cc5c3 100644 --- a/42sh/src/exec/ft_exec.c +++ b/42sh/src/exec/ft_exec.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */ -/* Updated: 2017/03/06 18:08:06 by ariard ### ########.fr */ +/* Updated: 2017/03/07 11:50:18 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,6 +29,7 @@ t_execmap g_execmap[] = {TK_CASE, &exec_case}, {TK_PAREN_OPEN, &exec_case_branch}, {TK_ASSIGNEMENT_WORD, &exec_var}, + {MATH, &exec_math}, /* {TK_SUBSHELL, &exec_}, */ {CMD, &exec_cmd}, {0, 0}, diff --git a/42sh/src/parser/add_case.c b/42sh/src/parser/add_case.c index f5862cef..4b4d5415 100644 --- a/42sh/src/parser/add_case.c +++ b/42sh/src/parser/add_case.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/04 20:42:13 by ariard #+# #+# */ -/* Updated: 2017/03/06 19:36:37 by ariard ### ########.fr */ +/* Updated: 2017/03/07 11:52:45 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -90,6 +90,7 @@ int add_pattern(t_btree **ast, t_list **lst) t_token *token; char **my_tab; + DG("add pattern"); token = (*lst)->content; node = (*ast)->item; if ((my_tab = (char **)malloc(sizeof(char *) * 4))) diff --git a/42sh/src/parser/add_func.c b/42sh/src/parser/add_func.c index ab3e0ae2..cc481f37 100644 --- a/42sh/src/parser/add_func.c +++ b/42sh/src/parser/add_func.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/24 23:43:07 by ariard #+# #+# */ -/* Updated: 2017/03/03 14:27:25 by ariard ### ########.fr */ +/* Updated: 2017/03/07 10:49:15 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -88,7 +88,7 @@ int add_one_func(t_btree **ast, t_list **lst) t_btree *func_ast; (void)lst; - func_ast = btree_map(*ast, &id); +// func_ast = btree_map(*ast, &id); ft_lsteadd(&data_singleton()->lst_func, ft_lstnew(&func_ast, sizeof(*ast))); DG("arbre ajoute"); return (0); diff --git a/42sh/src/parser/add_var.c b/42sh/src/parser/add_var.c index ad11bf54..db9ce5c2 100644 --- a/42sh/src/parser/add_var.c +++ b/42sh/src/parser/add_var.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/03 15:08:16 by ariard #+# #+# */ -/* Updated: 2017/03/03 16:17:27 by ariard ### ########.fr */ +/* Updated: 2017/03/07 10:47:43 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ From 5118a00f288268220bad05119c00bf7532505e03 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Tue, 7 Mar 2017 13:35:40 +0100 Subject: [PATCH 3/4] math ok, parsing io number tjr problemes --- 42sh/src/exec/exec_math.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/42sh/src/exec/exec_math.c b/42sh/src/exec/exec_math.c index 9bf8a3c7..c44ea5f9 100644 --- a/42sh/src/exec/exec_math.c +++ b/42sh/src/exec/exec_math.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 10:58:49 by ariard #+# #+# */ -/* Updated: 2017/03/07 12:19:25 by ariard ### ########.fr */ +/* Updated: 2017/03/07 13:33:49 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -71,11 +71,7 @@ int exec_math(t_btree **ast) node = (*ast)->item; av = token_to_argv(node->data.cmd.wordlist, 1); get_math(av[0], &var, &value, &operator); - DG("var : %s", var); - DG("value : %s", value); - DG("operator : %s", operator); do_math(&value, operator); - DG("value : %s", value); builtin_setenv("setenv", (char *[]){var, value, 0}, data_singleton()->local_var); return (0); } From a5f186eb592098de705b604bfb3872d85d43336e Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 7 Mar 2017 15:05:04 +0100 Subject: [PATCH 4/4] new process logic --- 42sh/includes/exec.h | 77 +++++++++++++++---------------- 42sh/includes/minishell.h | 2 +- 42sh/includes/types.h | 2 +- 42sh/src/builtin/is_builtin.c | 4 +- 42sh/src/exec/exec_command.c | 2 +- 42sh/src/exec/ft_exec.c | 4 +- 42sh/src/exec/launch_file.c | 59 +++++++++++++++++++++++ 42sh/src/exec/launch_process.c | 64 +++++++++---------------- 42sh/src/exec/process_setexec.c | 24 +++++----- 42sh/src/exec/set_process.c | 2 +- 42sh/src/hash_table/ft_add_hash.c | 4 +- 42sh/src/main/main.c | 2 +- 42sh/src/parser/eval_sym.c | 2 +- 42sh/src/parser/produce_sym.c | 2 +- 14 files changed, 145 insertions(+), 105 deletions(-) create mode 100644 42sh/src/exec/launch_file.c diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 81b783cb..7c9c54a8 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ -/* Updated: 2017/03/06 16:56:35 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 15:04:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,19 +16,12 @@ # define PIPE_READ 0 # define PIPE_WRITE 1 -# define PROCESS_BUILTIN (1 << 0) -# define PROCESS_BINARY (1 << 1) -# define PROCESS_SCRIPT (1 << 2) -# define PROCESS_SUBSHELL (1 << 3) -# define PROCESS_UNKNOWN (1 << 4) -# define PROCESS_CONTROL (1 << 5) -# define PROCESS_COMPLETED (1 << 6) -# define PROCESS_SUSPENDED (1 << 7) -# define PROCESS_RUNNING (1 << 8) -# define PROCESS_CONTINUED (1 << 9) +# define PROCESS_COMPLETED (1 << 0) +# define PROCESS_SUSPENDED (1 << 1) +# define PROCESS_RUNNING (1 << 2) +# define PROCESS_CONTINUED (1 << 3) -# define PROCESS_TYPE_MASK (1 << 0 | 1 << 1 | 1 << 2 | 1 << 3 | 1 << 4 | 1 << 5) -# define PROCESS_STATE_MASK (1 << 6 | 1 << 7 | 1 << 8 | 1 << 9) +# define PROCESS_STATE_MASK ((1 << 4) - (1 << 0)) # define IS_PIPESTART(p) ((p).fdin == STDIN) # define IS_PIPEEND(p) ((p).fdout == STDOUT) @@ -39,12 +32,13 @@ # define EXEC_OR_IF (1 << 3) # define EXEC_IF_BRANCH (1 << 4) # define EXEC_CASE_BRANCH (1 << 5) + +# define EXEC_AOL_MASK (EXEC_AND_IF | EXEC_OR_IF) + # define EXEC_IS_BG(j) (j & EXEC_BG) # define EXEC_IS_FG(j) (!EXEC_IS_BG(j)) # define EXEC_IS_AND_IF(j) (j & EXEC_AND_IF) # define EXEC_IS_OR_IF(j) (j & EXEC_OR_IF) -# define EXEC_AOL_MASK (EXEC_AND_IF | EXEC_OR_IF) - # define EXEC_IS_IF_BRANCH(j) (j & EXEC_IF_BRANCH) # define EXEC_IS_CASE_BRANCH(j) (j & EXEC_CASE_BRANCH) @@ -52,18 +46,34 @@ # include "types.h" # include "job_control.h" +enum e_process_type +{ + PROCESS_FUNCTION, + PROCESS_BUILTIN, + PROCESS_FILE, + PROCESS_SUBSHELL, + PROCESS_WHILE, + PROCESS_IF, + PROCESS_FOR, + PROCESS_CASE, +}; + +typedef enum e_process_type t_process_type; + struct s_process { - char **av; - char *path; - t_execf *execf; - pid_t pid; - int fdin; - int fdout; - int to_close; - t_list *redirs; - int status; - t_flag attrs; + /* char **av; */ + /* char *path; */ + /* t_execf *execf; */ + t_process_type type; + t_process_data data; + pid_t pid; + int fdin; + int fdout; + int to_close; + t_list *redirs; + int status; + t_flag attrs; }; struct s_exec @@ -77,22 +87,11 @@ struct s_exec int control_count; }; -struct s_execmap -{ - t_type type; - int (*f)(t_btree **ast); -}; - -struct s_redirmap -{ - t_flag type; - int (*f)(t_redir *redir); -}; - #include "minishell.h" -extern t_execmap g_execmap[]; -extern t_redirmap g_redirmap[]; +extern t_itof g_execmap[]; +extern t_itof g_redirmap[]; +extern t_itof g_launchmap[]; int ft_exec(t_btree **ast); diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 59e15890..7fab094a 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ -/* Updated: 2017/03/04 16:46:52 by ariard ### ########.fr */ +/* Updated: 2017/03/07 14:27:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/types.h b/42sh/includes/types.h index 68f377fc..237c8902 100644 --- a/42sh/includes/types.h +++ b/42sh/includes/types.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */ -/* Updated: 2017/03/06 15:31:28 by ariard ### ########.fr */ +/* Updated: 2017/03/07 14:35:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/is_builtin.c b/42sh/src/builtin/is_builtin.c index c0f39c2f..5a9577f8 100644 --- a/42sh/src/builtin/is_builtin.c +++ b/42sh/src/builtin/is_builtin.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 13:09:57 by jhalford #+# #+# */ -/* Updated: 2017/03/02 21:00:13 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 14:43:50 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,7 +37,7 @@ t_execf *is_builtin(t_process *p) i = -1; while (g_builtin[++i].name) { - if (ft_strcmp(g_builtin[i].name, p->av[0]) == 0) + if (ft_strcmp(g_builtin[i].name, p->data.cmd.av[0]) == 0) return (g_builtin[i].f); } return (NULL); diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index fdf22068..53bf4591 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/06 15:03:36 by ariard ### ########.fr */ +/* Updated: 2017/03/07 15:04:00 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index a56782fa..fe167aa0 100644 --- a/42sh/src/exec/ft_exec.c +++ b/42sh/src/exec/ft_exec.c @@ -6,13 +6,13 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */ -/* Updated: 2017/03/06 15:01:32 by ariard ### ########.fr */ +/* Updated: 2017/03/07 14:54:28 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" -t_execmap g_execmap[] = +t_itof g_execmap[] = { {TK_NEWLINE, &exec_semi}, {TK_SEMI, &exec_semi}, diff --git a/42sh/src/exec/launch_file.c b/42sh/src/exec/launch_file.c new file mode 100644 index 00000000..c482d419 --- /dev/null +++ b/42sh/src/exec/launch_file.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* launch_file.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/07 14:53:31 by jhalford #+# #+# */ +/* Updated: 2017/03/07 14:54:18 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +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) + { + ft_dprintf(2, "{red}%s: command not found: %s{eoc}\n", SHELL_NAME, p->av[0]); + exit(127); + } + 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 c1029d29..ebf4c0eb 100644 --- a/42sh/src/exec/launch_process.c +++ b/42sh/src/exec/launch_process.c @@ -6,59 +6,39 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 14:20:45 by jhalford #+# #+# */ -/* Updated: 2017/03/06 16:20:54 by wescande ### ########.fr */ +/* Updated: 2017/03/07 15:04:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +t_itof g_launchmap[] = +{ + {PROCESS_FUNCTION, NULL}, + {PROCESS_BUILTIN, NULL}, + {PROCESS_FILE, launch_file}, + {PROCESS_SUBSHELL, NULL}, + {PROCESS_WHILE, NULL}, + {PROCESS_IF, NULL}, + {PROCESS_FOR, NULL}, + {PROCESS_CASE, NULL}, +}; + int launch_process(t_process *p) { - int pid; + int i; + i = 0; /* DG("gonna launch [%s]", p->av[0]); */ /* DG("fdin=[%i]", p->fdin); */ /* DG("fdout=[%i]", p->fdout); */ - if (p->attrs & PROCESS_BUILTIN && IS_PIPESINGLE(*p)) + while (g_launchmap[i].type) { - if (process_redirect(p)) - return (1); - set_exitstatus((*p->execf)(p->path, p->av, data_singleton()->env), 1); - return (1); + if (p->type == g_launchmap[i].type) + if (!g_launchmap[i].f) + return (-1); + return ((*g_launchmap[i].f)(p)); + i++; } - 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) - { - ft_dprintf(2, "{red}%s: command not found: %s{eoc}\n", SHELL_NAME, p->av[0]); - exit(127); - } - 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); + return (-1); } diff --git a/42sh/src/exec/process_setexec.c b/42sh/src/exec/process_setexec.c index 0971a463..1114f927 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/06 12:26:01 by wescande ### ########.fr */ +/* Updated: 2017/03/07 14:49:45 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,18 +15,20 @@ int process_setexec(t_process *p) { p->path = NULL; - if ((p->execf = is_builtin(p))) - p->attrs |= PROCESS_BUILTIN; - else if (ft_strchr(p->av[0], '/')) - { - p->execf = &execve; - p->attrs |= PROCESS_SCRIPT; - p->path = ft_strdup(p->av[0]); - } + /* 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->execf = &execve; - p->attrs |= PROCESS_BINARY; + 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.path = ft_strdup(p->av[0]); } else { diff --git a/42sh/src/exec/set_process.c b/42sh/src/exec/set_process.c index 7ec05157..32a67674 100644 --- a/42sh/src/exec/set_process.c +++ b/42sh/src/exec/set_process.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/05 14:54:45 by jhalford #+# #+# */ -/* Updated: 2017/03/06 12:26:11 by wescande ### ########.fr */ +/* Updated: 2017/03/07 15:02:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/hash_table/ft_add_hash.c b/42sh/src/hash_table/ft_add_hash.c index 055d7654..403ee64d 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/04 18:51:01 by ariard ### ########.fr */ +/* Updated: 2017/03/07 14:43:33 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,6 @@ int ft_add_hash(t_process *p) hash.key = ft_strdup(p->av[0]); id = ft_hash_str(p->av[0]); ft_lsteadd(&(g_hash[id]), ft_lstnew(&hash, sizeof(t_hash))); - p->path = ft_strdup(hash.path); + p->data.cmd.path = ft_strdup(hash.path); return (1); } diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 2576f453..0e3d9c1c 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 12:39:14 by wescande ### ########.fr */ +/* Updated: 2017/03/07 14:40:24 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c index 8f0b9597..9dc520c7 100644 --- a/42sh/src/parser/eval_sym.c +++ b/42sh/src/parser/eval_sym.c @@ -187,7 +187,7 @@ t_stackmatch g_stackmatch[] = {TK_GREATAND, NEWLINE_LIST}, {TK_GREATAND, PIPE_SEMI_SEQUENCE}, {TK_GREATAND, SEQUENCE}, -// watch ! +// watch ! {TK_GREATAND, CMD_SUPERIOR}, {TK_GREATAND, AND_OR_MAJOR}, diff --git a/42sh/src/parser/produce_sym.c b/42sh/src/parser/produce_sym.c index c2302123..4556574f 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/05 15:12:59 by ariard ### ########.fr */ +/* Updated: 2017/03/07 14:36:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */