From 714b0854be5a3c202e7fa3e8f577c6fc4523dcd9 Mon Sep 17 00:00:00 2001 From: wescande Date: Mon, 20 Mar 2017 11:37:57 +0100 Subject: [PATCH 1/7] catch fix on master --- 42sh/Makefile | 3 ++- 42sh/includes/exec.h | 3 ++- 42sh/includes/lexer.h | 3 ++- 42sh/src/builtin/builtin_env.c | 14 ++++++++----- 42sh/src/exec/process_launch.c | 33 +++++++++++++++++++------------ 42sh/src/glob/command_getoutput.c | 18 ++++++++++++++++- 6 files changed, 52 insertions(+), 22 deletions(-) diff --git a/42sh/Makefile b/42sh/Makefile index 7d58c40b..beb66d8e 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -6,7 +6,7 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/03/19 15:14:11 by gwojda ### ########.fr # +# Updated: 2017/03/20 11:37:21 by wescande ### ########.fr # # # # **************************************************************************** # @@ -302,6 +302,7 @@ NB = $(words $(SRC_BASE)) INDEX = 0 all : + @make -j -C $(LIBFT_DIR) @make $(NAME) $(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 302c2a52..e055e34f 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/20 09:46:31 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 11:34:35 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -139,6 +139,7 @@ struct s_exec }; int exec_reset(void); +int do_the_muther_forker(t_process *p); int process_setgroup(t_process *p, pid_t pid); void process_setsig(void); void process_reset(t_process *p); diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index a0bc1ed1..2920d059 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ -/* Updated: 2017/03/19 17:13:05 by wescande ### ########.fr */ +/* Updated: 2017/03/20 11:22:08 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -83,6 +83,7 @@ void token_print(t_list *lst); int reduce_parens(t_list **alst, char *str); int bquotes_expand(t_list **alst); char *command_getoutput(char *command, char *const av[], char **env, int pipe_mode); +int command_setoutput(char *command, char *const av[], char **env, int pipe_mode); int ft_is_delim(char c); int ft_is_delim_list(char c); diff --git a/42sh/src/builtin/builtin_env.c b/42sh/src/builtin/builtin_env.c index b6fc4c3b..61594a26 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/19 17:43:24 by wescande ### ########.fr */ +/* Updated: 2017/03/20 11:29:17 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,8 @@ static int env_usage(int arg_miss, char c) else if (c) ft_dprintf(2, "{red}env: illegal option -- %c{eoc}\n", c); ft_dprintf(2, "usage: env [-i] [-u name] ... [name=value] ... cmd\n"); - return (1); + set_exitstatus(1, 1); + return (0); } static void env_freeone(char **env, char *arg) @@ -89,15 +90,17 @@ static int env_treat_flag(char ***custom_env, char *const *arg[]) int builtin_env(const char *path, char *const argv[], char *const envp[]) { char **env; + pid_t pid; (void)path; + pid = 0; if (!argv || ft_strcmp(*argv, "env")) return (env_usage(0, 0)); env = ft_sstrdup((char **)envp); if (env_treat_flag(&env, &argv)) { ft_sstrfree(env); - return (1); + return (0); } if (!*argv) { @@ -106,7 +109,8 @@ int builtin_env(const char *path, char *const argv[], char *const envp[]) ft_putchar('\n'); } else - command_getoutput(NULL, argv, env, 0); + pid = command_setoutput(NULL, argv, env, 0); ft_tabdel(&env); - return (0); + set_exitstatus(0, 1); + return (pid); } diff --git a/42sh/src/exec/process_launch.c b/42sh/src/exec/process_launch.c index 234fdd4d..a7ac2e26 100644 --- a/42sh/src/exec/process_launch.c +++ b/42sh/src/exec/process_launch.c @@ -6,27 +6,16 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/13 22:21:19 by jhalford #+# #+# */ -/* Updated: 2017/03/17 23:49:43 by wescande ### ########.fr */ +/* Updated: 2017/03/20 11:34:39 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" - int do_the_muther_forker(t_process *p) { pid_t pid; - if (IS_PIPESINGLE(*p) && p->type != PROCESS_FILE && p->type != PROCESS_SUBSHELL) - { - if (process_redirect(p)) - { - set_exitstatus(1, 1); - return (0); - } - set_exitstatus(p->map.launch(p), 1); - return (0); - } if ((pid = fork()) == -1) { ft_dprintf(3, "{red}%s: internal fork error{eoc}\n", SHELL_NAME); @@ -34,6 +23,8 @@ int do_the_muther_forker(t_process *p) } else if (pid) return (pid); + if (!p) + return (pid); DG("START OF FORK"); if (process_redirect(p)) exit (1); @@ -45,6 +36,22 @@ int do_the_muther_forker(t_process *p) exit(p->map.launch(p)); } +static int should_i_fork(t_process *p) +{ + if (IS_PIPESINGLE(*p) && p->type != PROCESS_FILE && p->type != PROCESS_SUBSHELL) + { + if (process_redirect(p)) + { + set_exitstatus(1, 1); + return (0); + } + return (p->map.launch(p)); + /* set_exitstatus(p->map.launch(p), 1); */ + /* return (0); */ + } + return (do_the_muther_forker(p)); +} + int process_launch(t_process *p) { pid_t pid; @@ -52,7 +59,7 @@ int process_launch(t_process *p) DG("p->type=%i", p->type); p->attrs &= ~PROCESS_STATE_MASK; p->attrs |= PROCESS_RUNNING; - if (!(pid = do_the_muther_forker(p))) + if (!(pid = should_i_fork(p))) { DG("launcher did not fork!"); process_resetfds(p); diff --git a/42sh/src/glob/command_getoutput.c b/42sh/src/glob/command_getoutput.c index 7a3f387f..53297129 100644 --- a/42sh/src/glob/command_getoutput.c +++ b/42sh/src/glob/command_getoutput.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 19:44:25 by wescande #+# #+# */ -/* Updated: 2017/03/19 17:42:45 by wescande ### ########.fr */ +/* Updated: 2017/03/20 11:36:03 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -102,3 +102,19 @@ char *command_getoutput(char *command, char *const av[], char **env, int pipe_ return (manage_output(fds)); return (NULL); } + +int command_setoutput(char *command, char *const av[], char **env, int pipe_mode) +{ + /* int ret; */ + int pid; +(void)pipe_mode; + if (!command && !av) + return (0); + if (!(pid = do_the_muther_forker(NULL))) + execute_command(command, av, env); + return (pid); + /* waitpid(pid, &ret, WUNTRACED); */ + /* if (pipe_mode) */ + /* return (manage_output(fds)); */ + /* return (NULL); */ +} From 4ea510774d2da49ab5acedb68bb608f473031033 Mon Sep 17 00:00:00 2001 From: gwojda Date: Mon, 20 Mar 2017 12:57:06 +0100 Subject: [PATCH 2/7] fix leaks edition de ligne --- 42sh/src/line_editing/readline.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/42sh/src/line_editing/readline.c b/42sh/src/line_editing/readline.c index 4bb37745..068889a0 100644 --- a/42sh/src/line_editing/readline.c +++ b/42sh/src/line_editing/readline.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */ -/* Updated: 2017/03/20 09:57:09 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 12:56:36 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,6 +30,7 @@ int readline(int has_prompt, char **input) ft_putchar('\n'); if (has_prompt) ret = ft_history_parsing(has_prompt, input); + ft_strdel(&data_singleton()->line.copy_tmp); if (tcsetattr(0, TCSANOW, ft_save_termios(0)) == -1) return (-1); return (ret); From 5b5a4442e2ab803a69828729f605a3cef3ecb959 Mon Sep 17 00:00:00 2001 From: gwojda Date: Mon, 20 Mar 2017 14:18:26 +0100 Subject: [PATCH 3/7] norme --- 42sh/includes/builtin_read.h | 23 ++++++++++++----------- 42sh/includes/ft_curs.h | 6 +++--- 42sh/includes/ft_readline.h | 5 +---- 42sh/src/builtin/builtin_exit.c | 8 +++----- 42sh/src/history/surch_in_history.c | 15 +++++++-------- 42sh/src/line_editing/readline.c | 8 ++++++-- 6 files changed, 32 insertions(+), 33 deletions(-) diff --git a/42sh/includes/builtin_read.h b/42sh/includes/builtin_read.h index c40d11e4..25f39efa 100644 --- a/42sh/includes/builtin_read.h +++ b/42sh/includes/builtin_read.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* builtin_read.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/20 15:02:39 by jhalford #+# #+# */ -/* Updated: 2017/03/15 19:13:26 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 12:36:08 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,17 +30,18 @@ typedef struct s_readopt t_readopt; struct s_read { - t_flag opts; - char **names; - char delim; - int nchars; - char *prompt; - int timeout; - int fd; - char *input; + t_flag opts; + char **names; + char delim; + int nchars; + char *prompt; + int timeout; + int fd; + char *input; }; -int builtin_read(const char *path, char *const av[], char *const envp[]); +int builtin_read(const char *path, char *const av[], + char *const envp[]); int bt_read_init(t_read *data, char **av); diff --git a/42sh/includes/ft_curs.h b/42sh/includes/ft_curs.h index 94b1a6bd..d411b2c5 100644 --- a/42sh/includes/ft_curs.h +++ b/42sh/includes/ft_curs.h @@ -6,7 +6,7 @@ /* By: sbenning +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/08 16:34:51 by sbenning #+# #+# */ -/* Updated: 2017/03/15 18:47:56 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 12:33:06 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,7 +27,7 @@ typedef struct s_curs t_curs; ** Cursor data : cursor's coordonate and windows size */ -struct s_curs +struct s_curs { int co; int li; @@ -79,6 +79,6 @@ void curs_write(t_curs *curs, char *str, size_t len); void curs_write_static(t_curs *curs, char *str, size_t len); void curs_video_write(t_curs *curs, char *str, size_t len); void curs_video_write_static( - t_curs *curs, char *str, size_t len); + t_curs *curs, char *str, size_t len); #endif diff --git a/42sh/includes/ft_readline.h b/42sh/includes/ft_readline.h index 4282c854..9d78c82b 100644 --- a/42sh/includes/ft_readline.h +++ b/42sh/includes/ft_readline.h @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/23 10:35:44 by gwojda #+# #+# */ -/* Updated: 2017/03/17 17:24:03 by gwojda ### ########.fr */ +/* Updated: 2017/03/20 12:32:15 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,9 +57,6 @@ # define CORRUPT 1 -//# define STR data_singleton()->line.input -//# define POS data_singleton()->line.pos - # define ERROR_CNTL_R 1 typedef struct s_list_history diff --git a/42sh/src/builtin/builtin_exit.c b/42sh/src/builtin/builtin_exit.c index f64c4a15..2e56a7d5 100644 --- a/42sh/src/builtin/builtin_exit.c +++ b/42sh/src/builtin/builtin_exit.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:28:41 by jhalford #+# #+# */ -/* Updated: 2017/03/20 11:37:51 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 14:17:40 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,10 +30,8 @@ int builtin_exit(const char *path, char *const av[], char *const envp[]) data_singleton()->argv[0]); return (0); } - if (av && av[1]) - status = ft_atoi(av[1]); - else - status = ft_atoi(ft_getenv(data_singleton()->env, "?")); + status = (av && av[1]) ? + ft_atoi(av[1]) : ft_atoi(ft_getenv(data_singleton()->env, "?")); if (SH_IS_INTERACTIVE(data_singleton()->opts)) tcsetattr(STDIN, TCSANOW, &data_singleton()->jobc.shell_tmodes); job_hup_all(); diff --git a/42sh/src/history/surch_in_history.c b/42sh/src/history/surch_in_history.c index 69c3a8a0..6c16b270 100644 --- a/42sh/src/history/surch_in_history.c +++ b/42sh/src/history/surch_in_history.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/26 10:43:16 by gwojda #+# #+# */ -/* Updated: 2017/03/18 14:29:15 by gwojda ### ########.fr */ +/* Updated: 2017/03/20 14:09:01 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ static void ft_clear_prompt(size_t *pos, size_t srch_pos) ft_puttermcaps("cd"); } -static int ft_surch_and_realloc(char **str, char **str_srch, +static int search(char **str, char **str_srch, int ret, size_t *srch_pos) { if (!(*str_srch = ft_realloc_imput(*str_srch, ret, *srch_pos))) @@ -51,7 +51,7 @@ static void ft_give_new_prompt(char *str_srch, size_t srch_pos) } } -static void ft_modify_str(char *str_srch, size_t srch_pos, char **str, +static int ft_modify_str(char *str_srch, size_t srch_pos, char **str, size_t *pos) { ft_clear_prompt(pos, srch_pos); @@ -63,6 +63,7 @@ static void ft_modify_str(char *str_srch, size_t srch_pos, char **str, ft_get_next_str(*str, pos); } free(str_srch); + return (0); } int ft_surch_in_history(char **str, size_t *pos) @@ -79,9 +80,8 @@ int ft_surch_in_history(char **str, size_t *pos) ft_give_new_prompt(str_srch, srch_pos); ret = 0; read(0, &ret, sizeof(int)); - if (ft_isprint(ret) && - ft_surch_and_realloc(str, &str_srch, ret, &srch_pos) < 0) - return (-1); + if (ft_isprint(ret) && search(str, &str_srch, ret, &srch_pos) < 0) + return (-1); else if (!ft_isprint(ret) && ret == 127 && srch_pos) { --srch_pos; @@ -92,6 +92,5 @@ int ft_surch_in_history(char **str, size_t *pos) else if (ret != 127 && !ft_isprint(ret)) break ; } - ft_modify_str(str_srch, srch_pos, str, pos); - return (0); + return (ft_modify_str(str_srch, srch_pos, str, pos)); } diff --git a/42sh/src/line_editing/readline.c b/42sh/src/line_editing/readline.c index 068889a0..6205bbc9 100644 --- a/42sh/src/line_editing/readline.c +++ b/42sh/src/line_editing/readline.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */ -/* Updated: 2017/03/20 12:56:36 by gwojda ### ########.fr */ +/* Updated: 2017/03/20 14:16:03 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,11 @@ int readline(int has_prompt, char **input) if (!has_prompt) data_singleton()->line.prompt_size = 1; if (!SH_IS_INTERACTIVE(data_singleton()->opts)) - return ((ret = get_next_line(data_singleton()->fd, input)) >= 0 ? !ret : ret); + { + if ((ret = get_next_line(data_singleton()->fd, input)) >= 0) + return (!ret); + return (ret); + } readline_init(has_prompt); ret = ft_read_stdin(input); if (ret < 0) From 3c39c485cb0603dca49f79d2c7bb953819e5a17f Mon Sep 17 00:00:00 2001 From: gwojda Date: Mon, 20 Mar 2017 14:39:20 +0100 Subject: [PATCH 4/7] norme builtin --- 42sh/src/builtin/bt_read_term.c | 8 +- 42sh/src/builtin/builtin_env.c | 13 +-- 42sh/src/builtin/builtin_export.c | 17 ++-- 42sh/src/builtin/builtin_math.c | 129 +++++++++++++++--------------- 42sh/src/builtin/builtin_read.c | 12 +-- 5 files changed, 89 insertions(+), 90 deletions(-) diff --git a/42sh/src/builtin/bt_read_term.c b/42sh/src/builtin/bt_read_term.c index 0ba68f65..2fb1d5f1 100644 --- a/42sh/src/builtin/bt_read_term.c +++ b/42sh/src/builtin/bt_read_term.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* bt_read_term.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/25 16:02:05 by jhalford #+# #+# */ -/* Updated: 2017/03/15 21:23:41 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 14:23:46 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ struct termios bt_read_term(int init) return (term); } -int bt_read_terminit(t_read *data) +int bt_read_terminit(t_read *data) { struct termios term; @@ -39,7 +39,7 @@ int bt_read_terminit(t_read *data) return (0); } -int bt_read_exit(t_read *data) +int bt_read_exit(t_read *data) { struct termios term; diff --git a/42sh/src/builtin/builtin_env.c b/42sh/src/builtin/builtin_env.c index b6fc4c3b..1d698316 100644 --- a/42sh/src/builtin/builtin_env.c +++ b/42sh/src/builtin/builtin_env.c @@ -6,13 +6,13 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */ -/* Updated: 2017/03/19 17:43:24 by wescande ### ########.fr */ +/* Updated: 2017/03/20 14:38:06 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static int env_usage(int arg_miss, char c) +static int env_usage(int arg_miss, char c) { if (arg_miss) ft_dprintf(2, "{red}env: option requires an argument -- u{eoc}\n"); @@ -22,7 +22,7 @@ static int env_usage(int arg_miss, char c) return (1); } -static void env_freeone(char **env, char *arg) +static void env_freeone(char **env, char *arg) { int i; char *tmp; @@ -45,7 +45,7 @@ static void env_freeone(char **env, char *arg) } } -static void env_replace(char ***custom_env, char *arg) +static void env_replace(char ***custom_env, char *arg) { char **arg_split; @@ -57,7 +57,7 @@ static void env_replace(char ***custom_env, char *arg) *custom_env = ft_sstradd(*custom_env, arg); } -static int env_treat_flag(char ***custom_env, char *const *arg[]) +static int env_treat_flag(char ***custom_env, char *const *arg[]) { while (*(++*arg)) { @@ -86,7 +86,8 @@ static int env_treat_flag(char ***custom_env, char *const *arg[]) return (0); } -int builtin_env(const char *path, char *const argv[], char *const envp[]) +int builtin_env(const char *path, char *const argv[], + char *const envp[]) { char **env; diff --git a/42sh/src/builtin/builtin_export.c b/42sh/src/builtin/builtin_export.c index 36772cf9..4f18d296 100644 --- a/42sh/src/builtin/builtin_export.c +++ b/42sh/src/builtin/builtin_export.c @@ -6,13 +6,13 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 11:39:37 by gwojda #+# #+# */ -/* Updated: 2017/03/18 11:46:32 by gwojda ### ########.fr */ +/* Updated: 2017/03/20 14:37:26 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static t_cliopts export_opts[] = +static t_cliopts g_export_opts[] = { {'p', NULL, BT_EXPORT_LP, 0, NULL}, {0, NULL, 0, 0, NULL}, @@ -44,23 +44,20 @@ int builtin_export( (void)envp; (void)path; data.flag = 0; - if (cliopts_get((char**)av, export_opts, &data)) + if (cliopts_get((char**)av, g_export_opts, &data)) ft_perror(); if (data.flag & BT_EXPORT_LP) return (bt_export_print()); av = data.av_data; while (*av) { - if ((equal = ft_strchr(*av, '='))) - { + equal = ft_strchr(*av, '='); + if (equal) *equal = 0; - builtin_setenv("internal", (char*[]){"global", *av, equal + 1}, NULL); - } else - { equal = ft_getenv(data_singleton()->local_var, *av); - builtin_setenv("internal", (char*[]){"global", *av, equal}, NULL); - } + equal ? equal++ : equal; + builtin_setenv("internal", (char*[]){"global", *av, equal}, NULL); builtin_unsetenv("internal", (char*[]){"local", *av, NULL}, NULL); av++; } diff --git a/42sh/src/builtin/builtin_math.c b/42sh/src/builtin/builtin_math.c index 589b009a..e7711687 100644 --- a/42sh/src/builtin/builtin_math.c +++ b/42sh/src/builtin/builtin_math.c @@ -6,17 +6,17 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/17 18:54:00 by ariard #+# #+# */ -/* Updated: 2017/03/17 19:59:42 by ariard ### ########.fr */ +/* Updated: 2017/03/20 14:23:26 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -# define MATHERR_0 "math : invalid number of arguments\n" -# define MATHERR_1 "math : invalid variable name\n" -# define MATHERR_2 "math : invalid operator\n" -# define MATHERR_3 "math : invalid operand\n" -# define MATHERR_4 "math : division by 0\n" +#define MATHERR_0 "math : invalid number of arguments\n" +#define MATHERR_1 "math : invalid variable name\n" +#define MATHERR_2 "math : invalid operator\n" +#define MATHERR_3 "math : invalid operand\n" +#define MATHERR_4 "math : division by 0\n" static int error_msg(char *msg) { @@ -38,78 +38,79 @@ static int get_value(char *var, char **value) { char *temp; char *esc; - int ret; + int ret; esc = ft_strnew((ft_strlen(var) >> 3) + 1); ret = word_is_assignment((char *[]) {var, (esc + 1)}); ft_strdel(&esc); if (!ret) return (error_msg(MATHERR_1)); - temp = ft_sstrstr(data_singleton()->local_var, 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); - return (0); + temp = ft_sstrstr(data_singleton()->local_var, 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); + return (0); } static int do_math(char **value, char *operator, char *operand) { - long ope1; - long ope2; + long ope1; + long ope2; - ope1 = ft_atoi(*value); - if (operand) - ope2 = ft_atoi(operand); - else - ope2 = 0; - if ((operator[0] == '/' || operator[0] == '%') && ope2 == 0) - return (error_msg(MATHERR_4)); - 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); + ope1 = ft_atoi(*value); + if (operand) + ope2 = ft_atoi(operand); + else + ope2 = 0; + if ((operator[0] == '/' || operator[0] == '%') && ope2 == 0) + return (error_msg(MATHERR_4)); + 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 builtin_math(const char *path, char *const av[], char *const envp[]) { - char *var; - char *value; - char *operator; - char *operand; + char *var; + char *value; + char *operator; + char *operand; - (void)path; - (void)envp; - if (!av || !av[1] || !av[2] || !av[3] || av[4]) - return (error_msg(MATHERR_0)); - init_math(&var, &value, &operator, &operand); - var = av[1]; - if (get_value(var, &value) == -1) - return (-1); - operator = av[2]; - if (!(ft_strlen(operator) == 1 && (operator[0] == '+' || operator[0] == '-' - || operator[0] == '/' || operator[0] == '*' || operator[0] == '%'))) - return (error_msg(MATHERR_2)); - operand = av[3]; - if (!ft_stris(operand, &ft_isdigit)) - return (error_msg(MATHERR_3)); - if (do_math(&value, operator, operand) == -1) - return (-1); - builtin_setenv("setenv", (char *[]){"local", var, value, 0}, data_singleton()->local_var); - return (0); + (void)path; + (void)envp; + if (!av || !av[1] || !av[2] || !av[3] || av[4]) + return (error_msg(MATHERR_0)); + init_math(&var, &value, &operator, &operand); + var = av[1]; + if (get_value(var, &value) == -1) + return (-1); + operator = av[2]; + if (!(ft_strlen(operator) == 1 && (operator[0] == '+' || operator[0] == '-' + || operator[0] == '/' || operator[0] == '*' || operator[0] == '%'))) + return (error_msg(MATHERR_2)); + operand = av[3]; + if (!ft_stris(operand, &ft_isdigit)) + return (error_msg(MATHERR_3)); + if (do_math(&value, operator, operand) == -1) + return (-1); + builtin_setenv("setenv", (char *[]){"local", var, value, 0}, + data_singleton()->local_var); + return (0); } diff --git a/42sh/src/builtin/builtin_read.c b/42sh/src/builtin/builtin_read.c index 5aac937f..3a71d816 100644 --- a/42sh/src/builtin/builtin_read.c +++ b/42sh/src/builtin/builtin_read.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* builtin_read.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */ -/* Updated: 2017/03/20 08:31:25 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 14:24:19 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,13 +40,13 @@ int bt_read_init(t_read *data, char **av) data->timeout = 0; data->input = NULL; if ((cliopts_get(av, g_read_opts, data))) - return(ft_perror() ? 2 : 2); + return (ft_perror() ? 2 : 2); if (bt_read_terminit(data) < 0) return (-1); return (0); } -int bt_read_loop(t_read *data) +int bt_read_loop(t_read *data) { int i; int esc; @@ -74,7 +74,7 @@ int bt_read_loop(t_read *data) return (0); } -int bt_read_assign(t_read *data) +int bt_read_assign(t_read *data) { char *input; char **names; @@ -97,7 +97,7 @@ int bt_read_assign(t_read *data) return (0); } -int builtin_read(const char *path, char *const av[], char *const envp[]) +int builtin_read(const char *path, char *const av[], char *const envp[]) { t_read data; int ret; From 229864750fb81e5c66741347364f1d9f2bc8d791 Mon Sep 17 00:00:00 2001 From: wescande Date: Mon, 20 Mar 2017 14:42:49 +0100 Subject: [PATCH 5/7] new Makefile compile libft without relink. change on exec for forker. change on all builtin for return status & set exit status --- 42sh/Makefile | 9 +- 42sh/includes/builtin.h | 3 +- 42sh/libft/Makefile | 4 +- 42sh/src/builtin/builtin_cd.c | 8 +- 42sh/src/builtin/builtin_echo.c | 4 +- 42sh/src/builtin/builtin_env.c | 18 ++-- 42sh/src/builtin/builtin_export.c | 6 +- 42sh/src/builtin/builtin_hash.c | 8 +- 42sh/src/builtin/builtin_history.c | 6 +- 42sh/src/builtin/builtin_math.c | 123 +++++++++++------------ 42sh/src/builtin/builtin_read.c | 9 +- 42sh/src/builtin/builtin_return_status.c | 19 ++++ 42sh/src/builtin/builtin_setenv.c | 9 +- 42sh/src/builtin/builtin_unset.c | 10 +- 42sh/src/builtin/builtin_unsetenv.c | 4 +- 42sh/src/exec/set_exitstatus.c | 5 +- 16 files changed, 136 insertions(+), 109 deletions(-) create mode 100644 42sh/src/builtin/builtin_return_status.c diff --git a/42sh/Makefile b/42sh/Makefile index beb66d8e..d76d7dbe 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -6,14 +6,14 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/03/20 11:37:21 by wescande ### ########.fr # +# Updated: 2017/03/20 12:04:31 by wescande ### ########.fr # # # # **************************************************************************** # NAME = 42sh CC = gcc -FLAGS = -Wall -Wextra -Werror -g +FLAGS = -Wall -Wextra -Werror -fvisibility=hidden D_FLAGS = -g DELTA = $$(echo "$$(tput cols)-47"|bc) @@ -40,6 +40,7 @@ builtin/builtin_hash.c\ builtin/builtin_history.c\ builtin/builtin_math.c\ builtin/builtin_read.c\ +builtin/builtin_return_status.c\ builtin/builtin_setenv.c\ builtin/builtin_unset.c\ builtin/builtin_unsetenv.c\ @@ -302,8 +303,8 @@ NB = $(words $(SRC_BASE)) INDEX = 0 all : - @make -j -C $(LIBFT_DIR) - @make $(NAME) + @make -C $(LIBFT_DIR) + @make -j $(NAME) $(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) @$(CC) $(OBJS) -o $(NAME) \ diff --git a/42sh/includes/builtin.h b/42sh/includes/builtin.h index cc50725e..c95f8067 100644 --- a/42sh/includes/builtin.h +++ b/42sh/includes/builtin.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 22:59:57 by jhalford #+# #+# */ -/* Updated: 2017/03/17 18:59:55 by ariard ### ########.fr */ +/* Updated: 2017/03/20 12:51:23 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,7 @@ # define BT_EXPORT_LP (1 << 0) t_execf *is_builtin(t_process *p); +int builtin_return_status(int ret, int status); int builtin_export(const char *path, char *const av[], char *const envp[]); int builtin_unset(const char *path, char *const av[], char *const envp[]); int builtin_env(const char *path, char *const argv[], char *const envp[]); diff --git a/42sh/libft/Makefile b/42sh/libft/Makefile index 415c540f..638a8571 100644 --- a/42sh/libft/Makefile +++ b/42sh/libft/Makefile @@ -6,7 +6,7 @@ # By: jhalford +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2017/02/07 16:09:36 by jhalford #+# #+# # -# Updated: 2017/03/18 14:15:35 by gwojda ### ########.fr # +# Updated: 2017/03/20 12:05:06 by wescande ### ########.fr # # # # **************************************************************************** # @@ -207,7 +207,7 @@ all: $(NAME): $(OBJ_DIR) $(OBJS) @$(AR) $(NAME) $(OBJS) @ranlib $(NAME) - @strip -x $(NAME) + @# @strip -x $(NAME) @printf "\r\033[48;5;15;38;5;25m✅ MAKE $(NAME)\033[0m\033[K\n" $(OBJ_DIR)%.o: $(SRC_DIR)%.c | $(OBJ_DIR) diff --git a/42sh/src/builtin/builtin_cd.c b/42sh/src/builtin/builtin_cd.c index d8f46e2e..29bd507e 100644 --- a/42sh/src/builtin/builtin_cd.c +++ b/42sh/src/builtin/builtin_cd.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:57:53 by jhalford #+# #+# */ -/* Updated: 2017/03/16 14:17:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 12:54:02 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -81,14 +81,14 @@ int builtin_cd(const char *path, opts = CDOPT_L; i = builtin_cd_opts(av, &opts); if (!(target = builtin_cd_special(av + i, envp))) - return (1); + return (builtin_return_status(0, 1)); cwd = getcwd(NULL, 0); builtin_setenv(path, (char*[4]){"setenv", "OLDPWD", cwd, NULL}, envp); free(cwd); if (chdir(target)) { ft_dprintf(2, CDERR_1, target); - return (1); + return (builtin_return_status(0, 1)); } else if (target != av[i]) ft_printf("%s\n", target); @@ -97,5 +97,5 @@ int builtin_cd(const char *path, free(cwd); if (!ft_strcmp(*(av + i), "-")) free(target); - return (0); + return (builtin_return_status(0, 0)); } diff --git a/42sh/src/builtin/builtin_echo.c b/42sh/src/builtin/builtin_echo.c index 2fd4f3de..57dd30bd 100644 --- a/42sh/src/builtin/builtin_echo.c +++ b/42sh/src/builtin/builtin_echo.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:21:41 by jhalford #+# #+# */ -/* Updated: 2017/01/20 00:10:31 by ariard ### ########.fr */ +/* Updated: 2017/03/20 14:40:00 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,5 +25,5 @@ int builtin_echo(const char *path, char *const av[], char *const envp[]) ft_putchar(' '); } ft_putchar('\n'); - return (0); + return (builtin_return_status(0, 0)); } diff --git a/42sh/src/builtin/builtin_env.c b/42sh/src/builtin/builtin_env.c index 61594a26..4ef5c73c 100644 --- a/42sh/src/builtin/builtin_env.c +++ b/42sh/src/builtin/builtin_env.c @@ -6,11 +6,12 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */ -/* Updated: 2017/03/20 11:29:17 by wescande ### ########.fr */ +/* Updated: 2017/03/20 12:52:05 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +#define US_ENV "env [-i] [-u name] [name=value]... [utility [argument...]]" static int env_usage(int arg_miss, char c) { @@ -18,9 +19,8 @@ static int env_usage(int arg_miss, char c) ft_dprintf(2, "{red}env: option requires an argument -- u{eoc}\n"); else if (c) ft_dprintf(2, "{red}env: illegal option -- %c{eoc}\n", c); - ft_dprintf(2, "usage: env [-i] [-u name] ... [name=value] ... cmd\n"); - set_exitstatus(1, 1); - return (0); + ft_dprintf(2, "usage: %s\n", US_ENV); + return (1); } static void env_freeone(char **env, char *arg) @@ -87,7 +87,8 @@ static int env_treat_flag(char ***custom_env, char *const *arg[]) return (0); } -int builtin_env(const char *path, char *const argv[], char *const envp[]) +int builtin_env(const char *path, + char *const argv[], char *const envp[]) { char **env; pid_t pid; @@ -95,12 +96,12 @@ int builtin_env(const char *path, char *const argv[], char *const envp[]) (void)path; pid = 0; if (!argv || ft_strcmp(*argv, "env")) - return (env_usage(0, 0)); + return (builtin_return_status(0, env_usage(0, 0))); env = ft_sstrdup((char **)envp); if (env_treat_flag(&env, &argv)) { ft_sstrfree(env); - return (0); + return (builtin_return_status(0, 1)); } if (!*argv) { @@ -111,6 +112,5 @@ int builtin_env(const char *path, char *const argv[], char *const envp[]) else pid = command_setoutput(NULL, argv, env, 0); ft_tabdel(&env); - set_exitstatus(0, 1); - return (pid); + return (builtin_return_status(pid, 0)); } diff --git a/42sh/src/builtin/builtin_export.c b/42sh/src/builtin/builtin_export.c index 36772cf9..6cade0bb 100644 --- a/42sh/src/builtin/builtin_export.c +++ b/42sh/src/builtin/builtin_export.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 11:39:37 by gwojda #+# #+# */ -/* Updated: 2017/03/18 11:46:32 by gwojda ### ########.fr */ +/* Updated: 2017/03/20 14:01:11 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,7 +47,7 @@ int builtin_export( if (cliopts_get((char**)av, export_opts, &data)) ft_perror(); if (data.flag & BT_EXPORT_LP) - return (bt_export_print()); + return (builtin_return_status(0, bt_export_print())); av = data.av_data; while (*av) { @@ -64,5 +64,5 @@ int builtin_export( builtin_unsetenv("internal", (char*[]){"local", *av, NULL}, NULL); av++; } - return (0); + return (builtin_return_status(0, 0)); } diff --git a/42sh/src/builtin/builtin_hash.c b/42sh/src/builtin/builtin_hash.c index f030a63d..539f04b5 100644 --- a/42sh/src/builtin/builtin_hash.c +++ b/42sh/src/builtin/builtin_hash.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/18 16:37:43 by gwojda #+# #+# */ -/* Updated: 2017/02/18 16:51:43 by gwojda ### ########.fr */ +/* Updated: 2017/03/20 14:03:30 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,8 +43,8 @@ int builtin_hash(const char *path, char *const av[], char *const envp[]) (void)path; (void)envp; - if (ft_hash_opt(av)) - return (0); + if (!av || ft_hash_opt(av)) + return (builtin_return_status(0, 1)); i = 0; while (i < MAX_HASH) { @@ -52,5 +52,5 @@ int builtin_hash(const char *path, char *const av[], char *const envp[]) ft_hash_lst_print(g_hash[i]); ++i; } - return (0); + return (builtin_return_status(0, 0)); } diff --git a/42sh/src/builtin/builtin_history.c b/42sh/src/builtin/builtin_history.c index 0c653fbd..7d66d2b0 100644 --- a/42sh/src/builtin/builtin_history.c +++ b/42sh/src/builtin/builtin_history.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/24 14:54:53 by gwojda #+# #+# */ -/* Updated: 2017/02/20 20:30:18 by ariard ### ########.fr */ +/* Updated: 2017/03/20 14:04:02 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ int builtin_history(const char *path, char *const av[], char *const envp[]) head = data_singleton()->line.list_end; len = 1; if (!head) - return (0); + return (builtin_return_status(0, 0)); if (head && !head->str) head = head->next; while (head && head->str) @@ -34,5 +34,5 @@ int builtin_history(const char *path, char *const av[], char *const envp[]) ++len; head = head->next; } - return (0); + return (builtin_return_status(0, 0)); } diff --git a/42sh/src/builtin/builtin_math.c b/42sh/src/builtin/builtin_math.c index 589b009a..85bdc610 100644 --- a/42sh/src/builtin/builtin_math.c +++ b/42sh/src/builtin/builtin_math.c @@ -6,26 +6,26 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/17 18:54:00 by ariard #+# #+# */ -/* Updated: 2017/03/17 19:59:42 by ariard ### ########.fr */ +/* Updated: 2017/03/20 14:14:59 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -# define MATHERR_0 "math : invalid number of arguments\n" +# define MATHERR_0 "math : invalid number of arguments\n" # define MATHERR_1 "math : invalid variable name\n" # define MATHERR_2 "math : invalid operator\n" # define MATHERR_3 "math : invalid operand\n" -# define MATHERR_4 "math : division by 0\n" +# define MATHERR_4 "math : division by 0\n" static int error_msg(char *msg) { ft_dprintf(2, "{red}%s{eoc}", msg); - return (-1); + return (1); } static int init_math(char **var, char **value, char **operator, - char **operand) + char **operand) { *var = NULL; *value = NULL; @@ -45,71 +45,70 @@ static int get_value(char *var, char **value) ft_strdel(&esc); if (!ret) return (error_msg(MATHERR_1)); - temp = ft_sstrstr(data_singleton()->local_var, 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); - return (0); + temp = ft_sstrstr(data_singleton()->local_var, 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); + return (0); } static int do_math(char **value, char *operator, char *operand) { - long ope1; - long ope2; + long ope1; + long ope2; - ope1 = ft_atoi(*value); - if (operand) - ope2 = ft_atoi(operand); - else - ope2 = 0; - if ((operator[0] == '/' || operator[0] == '%') && ope2 == 0) - return (error_msg(MATHERR_4)); - 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); + ope1 = ft_atoi(*value); + if (operand) + ope2 = ft_atoi(operand); + else + ope2 = 0; + if ((operator[0] == '/' || operator[0] == '%') && ope2 == 0) + return (error_msg(MATHERR_4)); + 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 builtin_math(const char *path, char *const av[], char *const envp[]) { - char *var; - char *value; - char *operator; - char *operand; + char *var; + char *value; + char *operator; + char *operand; - (void)path; - (void)envp; - if (!av || !av[1] || !av[2] || !av[3] || av[4]) - return (error_msg(MATHERR_0)); - init_math(&var, &value, &operator, &operand); - var = av[1]; - if (get_value(var, &value) == -1) - return (-1); - operator = av[2]; - if (!(ft_strlen(operator) == 1 && (operator[0] == '+' || operator[0] == '-' - || operator[0] == '/' || operator[0] == '*' || operator[0] == '%'))) - return (error_msg(MATHERR_2)); - operand = av[3]; - if (!ft_stris(operand, &ft_isdigit)) - return (error_msg(MATHERR_3)); - if (do_math(&value, operator, operand) == -1) - return (-1); - builtin_setenv("setenv", (char *[]){"local", var, value, 0}, data_singleton()->local_var); - return (0); + (void)path; + (void)envp; + if (!av || !av[1] || !av[2] || !av[3] || av[4]) + return (builtin_return_status(0, error_msg(MATHERR_0))); + init_math(&var, &value, &operator, &operand); + var = av[1]; + if (get_value(var, &value)) + return (builtin_return_status(0, 1)); + operator = av[2]; + if (ft_strlen(operator) != 1 || !(ft_strchr("+-/*%", operator[0]))) + return (builtin_return_status(0, error_msg(MATHERR_2))); + operand = av[3]; + if (!ft_stris(operand, &ft_isdigit)) + return (builtin_return_status(0, error_msg(MATHERR_3))); + if (do_math(&value, operator, operand)) + return (builtin_return_status(0, 1)); + builtin_setenv("setenv", (char *[]){"local", var, value, 0}, data_singleton()->local_var); + return (builtin_return_status(0, 0)); } diff --git a/42sh/src/builtin/builtin_read.c b/42sh/src/builtin/builtin_read.c index 5aac937f..935ab533 100644 --- a/42sh/src/builtin/builtin_read.c +++ b/42sh/src/builtin/builtin_read.c @@ -6,11 +6,13 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */ -/* Updated: 2017/03/20 08:31:25 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 12:53:06 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +#define US_READ "read [-ers] [-u fd] [-t timeout] [-p prompt]" +#define US_READ_1 "[-n nchars] [-d delim] [name ...]" t_cliopts g_read_opts[] = { @@ -26,8 +28,7 @@ t_cliopts g_read_opts[] = void bt_read_usage(void) { - ft_dprintf(2, "{red}read: usage: read [-ers] [-u fd] [-t timeout]"); - ft_dprintf(2, " [-p prompt] [-n nchars] [-d delim] [name ...]{eoc}\n"); + ft_dprintf(2, "usage: read %s %s\n", US_READ, US_READ_1); } int bt_read_init(t_read *data, char **av) @@ -117,5 +118,5 @@ int builtin_read(const char *path, char *const av[], char *const envp[]) bt_read_usage(); if (ret != 2) bt_read_exit(&data); - return (ret); + return (builtin_return_status(0, ret)); } diff --git a/42sh/src/builtin/builtin_return_status.c b/42sh/src/builtin/builtin_return_status.c new file mode 100644 index 00000000..c6dfaf8f --- /dev/null +++ b/42sh/src/builtin/builtin_return_status.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin_return_status.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: wescande +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/20 12:48:04 by wescande #+# #+# */ +/* Updated: 2017/03/20 12:49:18 by wescande ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int builtin_return_status(int ret, int status) +{ + set_exitstatus(status, 1); + return (ret); +} diff --git a/42sh/src/builtin/builtin_setenv.c b/42sh/src/builtin/builtin_setenv.c index 1dcf264a..e8672cc8 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/03/20 08:24:10 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 14:38:25 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,10 +39,9 @@ int builtin_setenv(const char *path, { char ***env; - (void)envp; (void)path; if (!av || !av[0]) - return (1); + return (builtin_return_status(0, 1)); if (ft_strcmp(av[0], "local") == 0) env = &data_singleton()->local_var; else @@ -53,6 +52,6 @@ int builtin_setenv(const char *path, ft_putchar('\n'); } else - return (assign_var(av, env)); - return (0); + assign_var(av, env); + return (envp ? builtin_return_status(0, 0) : 0); } diff --git a/42sh/src/builtin/builtin_unset.c b/42sh/src/builtin/builtin_unset.c index c3afa192..d335e53e 100644 --- a/42sh/src/builtin/builtin_unset.c +++ b/42sh/src/builtin/builtin_unset.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 11:43:34 by gwojda #+# #+# */ -/* Updated: 2017/02/15 11:58:22 by gwojda ### ########.fr */ +/* Updated: 2017/03/20 14:27:43 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,5 +14,13 @@ int builtin_unset(const char *path, char *const av[], char *const envp[]) { + char *tmp; + char **av_tmp; + + tmp = av[0]; + av_tmp = (char **)av; + av_tmp[0] = "local"; + builtin_unsetenv(path, av, envp); + av_tmp[0] = tmp; return (builtin_unsetenv(path, av, envp)); } diff --git a/42sh/src/builtin/builtin_unsetenv.c b/42sh/src/builtin/builtin_unsetenv.c index 02896fa6..5275a6ff 100644 --- a/42sh/src/builtin/builtin_unsetenv.c +++ b/42sh/src/builtin/builtin_unsetenv.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:29:17 by jhalford #+# #+# */ -/* Updated: 2017/03/18 11:47:53 by gwojda ### ########.fr */ +/* Updated: 2017/03/20 14:40:05 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,5 +35,5 @@ int builtin_unsetenv(const char *path, char *const av[], char *const envp[]) } i++; } - return (0); + return (builtin_return_status(0, 0)); } diff --git a/42sh/src/exec/set_exitstatus.c b/42sh/src/exec/set_exitstatus.c index 79d0f216..f56e71a4 100644 --- a/42sh/src/exec/set_exitstatus.c +++ b/42sh/src/exec/set_exitstatus.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */ -/* Updated: 2017/03/14 00:54:47 by ariard ### ########.fr */ +/* Updated: 2017/03/20 14:38:34 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,7 +32,6 @@ void set_exitstatus(int status, int override) } } astatus = ft_itoa(exitval); - builtin_setenv("setenv", (char*[]){"setenv", "?", astatus, 0}, - data_singleton()->env); + builtin_setenv("setenv", (char*[]){"setenv", "?", astatus, 0}, NULL); ft_strdel(&astatus); } From 46387a1f6e5624c5b972c789f1a321bfa870482c Mon Sep 17 00:00:00 2001 From: gwojda Date: Mon, 20 Mar 2017 15:02:24 +0100 Subject: [PATCH 6/7] some norme -> completion, hash, builtin, history, lexer, line_edit --- 42sh/src/completion/c_match.c | 5 +++-- 42sh/src/job_control/builtin_fg.c | 6 +++--- 42sh/src/line_editing/readline.c | 2 +- 42sh/src/main/data_exit.c | 3 +-- 42sh/src/main/data_singleton.c | 4 ++-- 42sh/src/main/main.c | 11 ++++++----- 42sh/src/main/shell_init.c | 11 ++++++----- 42sh/src/parser/add_subshell.c | 12 ++++++------ 42sh/src/parser/read_stack.c | 7 +++---- 42sh/src/parser/sym_free.c | 4 ++-- 10 files changed, 33 insertions(+), 32 deletions(-) diff --git a/42sh/src/completion/c_match.c b/42sh/src/completion/c_match.c index 06440ac5..4df444b5 100644 --- a/42sh/src/completion/c_match.c +++ b/42sh/src/completion/c_match.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/10/15 13:27:14 by alao #+# #+# */ -/* Updated: 2017/03/19 15:34:38 by gwojda ### ########.fr */ +/* Updated: 2017/03/20 14:41:04 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,8 @@ static int c_chevron(t_comp *c) pos = c->ircmd; if (pos >= ft_strlen(c->rcmd)) - pos = ft_strlen(c->rcmd) - (ft_strlen(data_singleton()->line.input) - pos); + pos = ft_strlen(c->rcmd) - (ft_strlen(data_singleton()->line.input) + - pos); while (pos) { if (c->rcmd[pos] == '<' || c->rcmd[pos] == '>') diff --git a/42sh/src/job_control/builtin_fg.c b/42sh/src/job_control/builtin_fg.c index dfbe6104..74258367 100644 --- a/42sh/src/job_control/builtin_fg.c +++ b/42sh/src/job_control/builtin_fg.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* builtin_fg.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/08 14:30:07 by jhalford #+# #+# */ -/* Updated: 2017/03/20 12:12:16 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 14:42:22 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,7 +27,7 @@ int builtin_fg(const char *path, char *const av[], char *const envp[]) return (-1); } jobc = &data_singleton()->jobc; - job_getrank(&rank); + job_getrank(&rank); if (av[1] ? (id = *av[1]) : 0) { if ((jlist = ft_lst_find(jobc->first_job, &id, job_cmp_id))) diff --git a/42sh/src/line_editing/readline.c b/42sh/src/line_editing/readline.c index 6205bbc9..cbbc9438 100644 --- a/42sh/src/line_editing/readline.c +++ b/42sh/src/line_editing/readline.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */ -/* Updated: 2017/03/20 14:16:03 by gwojda ### ########.fr */ +/* Updated: 2017/03/20 14:49:03 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/data_exit.c b/42sh/src/main/data_exit.c index 566a1c23..d62a3ddb 100644 --- a/42sh/src/main/data_exit.c +++ b/42sh/src/main/data_exit.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/07 18:07:50 by jhalford #+# #+# */ -/* Updated: 2017/03/18 15:43:07 by gwojda ### ########.fr */ +/* Updated: 2017/03/20 14:44:51 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,6 @@ void data_exit(void) t_data *data; data = data_singleton(); - /* ft_strdel(&data->line.input); */ ft_sstrfree(data->env); ft_sstrfree(data->local_var); ft_sstrfree(data->argv); diff --git a/42sh/src/main/data_singleton.c b/42sh/src/main/data_singleton.c index 4dc9731c..31512ede 100644 --- a/42sh/src/main/data_singleton.c +++ b/42sh/src/main/data_singleton.c @@ -6,13 +6,13 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/10 11:36:39 by jhalford #+# #+# */ -/* Updated: 2017/03/16 15:12:53 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 14:45:01 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -t_data *data_singleton() +t_data *data_singleton(void) { static t_data *data = NULL; diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index b76b695f..f5f5a4d6 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -3,15 +3,16 @@ /* ::: :::::::: */ /* main.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Updated: 2017/03/17 10:51:23 by gwojda ### ########.fr */ +/* Created: 2017/03/20 14:45:40 by gwojda #+# #+# */ +/* Updated: 2017/03/20 14:46:44 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static int do_readline_routine(char **stream) +static int do_readline_routine(char **stream) { int ret; int has_prompt; @@ -55,7 +56,7 @@ static int handle_instruction(t_list **token, t_btree **ast) return (0); } -int main(int ac, char **av) +int main(int ac, char **av) { int ret; t_data *data; @@ -67,7 +68,7 @@ int main(int ac, char **av) DG("{inv}{bol}{gre}start of shell{eoc}"); if (shell_init(ac, av) != 0) return (1); - DG("JOBC is %s", SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF"); + DG("JOBC is %s", SH_HAS_JOBC(data_singleton()->opts) ? "ON" : "OFF"); token = NULL; ast = NULL; data = data_singleton(); diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index 5e1af3c5..0dc2c0f3 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/20 11:30:42 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 14:54:28 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ static t_cliopts g_opts[] = {0, 0, 0, 0, 0}, }; -int get_c_arg(char ***av, t_data *data) +int get_c_arg(char ***av, t_data *data) { if (!av || !*av) return (1); @@ -28,7 +28,7 @@ int get_c_arg(char ***av, t_data *data) return (0); } -static int get_input_fd(t_data *data) +static int get_input_fd(t_data *data) { static int fds[2] = {-1, STDIN}; char *file; @@ -47,7 +47,8 @@ static int get_input_fd(t_data *data) ft_printf("{red}%s: %s: No such file or directory\n{eoc}", data->argv[0], file); else if (S_ISDIR(buf.st_mode)) - ft_printf("{red}%s: %s: is a directory\n{eoc}", data->argv[0], file); + ft_printf("{red}%s: %s: is a directory\n{eoc}", data->argv[0], + file); else if ((fds[PIPE_READ] = open(file, O_RDONLY | O_CLOEXEC)) < 0) ft_printf("{red}%s: %s: No such file or directory\n{eoc}", data->argv[0], file); @@ -115,7 +116,7 @@ int shell_init(int ac, char **av) if ((data->fd = get_input_fd(data)) < 0) return (-1); DG(); - if (SH_IS_INTERACTIVE(data->opts) && interactive_settings() < 0) + if (SH_IS_INTERACTIVE(data->opts) && interactive_settings() < 0) return (-1); return (0); } diff --git a/42sh/src/parser/add_subshell.c b/42sh/src/parser/add_subshell.c index 5b70e8da..22bbb97e 100644 --- a/42sh/src/parser/add_subshell.c +++ b/42sh/src/parser/add_subshell.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/24 18:41:50 by ariard #+# #+# */ -/* Updated: 2017/03/18 15:50:28 by ariard ### ########.fr */ +/* Updated: 2017/03/20 14:57:34 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,16 +41,16 @@ int add_subshell_cmd(t_btree **ast, t_list **lst) token = (*lst)->content; node = (*ast)->item; if ((token->type == SUBSHELL && node->type == SUBSHELL) - || (token->type == TK_LBRACE && node->type == TK_LBRACE)) - node->nest++; + || (token->type == TK_LBRACE && node->type == TK_LBRACE)) + node->nest++; if (((token->type == TK_PAREN_CLOSE && node->type == SUBSHELL) - || (token->type == TK_RBRACE && node->type == TK_LBRACE)) && node->nest > 0) + || (token->type == TK_RBRACE && node->type == TK_LBRACE)) && node->nest > 0) node->nest--; else if (token->type == TK_PAREN_CLOSE && node->type == SUBSHELL - && node->nest == 0) + && node->nest == 0) return ((node->full = 1)); else if (token->type == TK_RBRACE && node->type == TK_LBRACE - && node->nest == 0) + && node->nest == 0) return ((node->full = 1)); return (add_cmd(&(*ast)->right, lst)); } diff --git a/42sh/src/parser/read_stack.c b/42sh/src/parser/read_stack.c index af8a85d7..c6ad591b 100644 --- a/42sh/src/parser/read_stack.c +++ b/42sh/src/parser/read_stack.c @@ -6,13 +6,13 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 15:32:10 by ariard #+# #+# */ -/* Updated: 2017/03/15 16:33:57 by ariard ### ########.fr */ +/* Updated: 2017/03/20 14:59:23 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -char *read_state(t_sym current) +char *read_state(t_sym current) { if (current == FNAME) return ("FNAME"); @@ -131,7 +131,7 @@ char *read_state(t_sym current) if (current == TK_DONE) return ("TK_DONE"); if (current == AND_OR_MINOR) - return("AND_OR_MINOR"); + return ("AND_OR_MINOR"); if (current == AND_OR_MAJOR) return ("AND_OR_MAJOR"); if (current == TK_COMMAND) @@ -227,7 +227,6 @@ int ft_read_stack(t_sym *stack) return (0); } - int ft_show_heredoc_data(t_astnode *node) { t_redir *redir; diff --git a/42sh/src/parser/sym_free.c b/42sh/src/parser/sym_free.c index 200be9ba..b26c2144 100644 --- a/42sh/src/parser/sym_free.c +++ b/42sh/src/parser/sym_free.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/18 17:13:31 by ariard #+# #+# */ -/* Updated: 2017/03/18 17:15:11 by ariard ### ########.fr */ +/* Updated: 2017/03/20 14:57:58 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,4 +19,4 @@ void sym_free(void *data, size_t size) (void)size; sym = data; free(sym); -} +} From 378aa39ceb594d93665f108fde368c0c1b6d4e73 Mon Sep 17 00:00:00 2001 From: wescande Date: Mon, 20 Mar 2017 15:11:04 +0100 Subject: [PATCH 7/7] rectif bquote --- 42sh/includes/lexer.h | 6 ++-- 42sh/src/builtin/builtin_env.c | 4 +-- 42sh/src/exec/process_launch.c | 11 ++++---- 42sh/src/glob/command_getoutput.c | 47 ++++++++++++++----------------- 42sh/src/glob/expand_bquote.c | 19 +++---------- 5 files changed, 35 insertions(+), 52 deletions(-) diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 0724da9d..8e78d178 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ -/* Updated: 2017/03/20 14:43:04 by wescande ### ########.fr */ +/* Updated: 2017/03/20 15:01:01 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -81,8 +81,8 @@ void token_print(t_list *lst); int reduce_parens(t_list **alst, char *str); int bquotes_expand(t_list **alst); -char *command_getoutput(char *command, char *const av[], char **env, int pipe_mode); -int command_setoutput(char *command, char *const av[], char **env, int pipe_mode); +char *command_getoutput(char *command); +int command_setoutput(char *const av[], char **env); int ft_is_delim(char c); int ft_is_delim_list(char c); diff --git a/42sh/src/builtin/builtin_env.c b/42sh/src/builtin/builtin_env.c index 735916b7..400e0022 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/20 14:43:49 by wescande ### ########.fr */ +/* Updated: 2017/03/20 15:02:31 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -110,7 +110,7 @@ int builtin_env(const char *path, ft_putchar('\n'); } else - pid = command_setoutput(NULL, argv, env, 0); + pid = command_setoutput(argv, env); ft_tabdel(&env); return (builtin_return_status(pid, 0)); } diff --git a/42sh/src/exec/process_launch.c b/42sh/src/exec/process_launch.c index 3d9d9285..1b5f89ae 100644 --- a/42sh/src/exec/process_launch.c +++ b/42sh/src/exec/process_launch.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/13 22:21:19 by jhalford #+# #+# */ -/* Updated: 2017/03/20 11:38:54 by wescande ### ########.fr */ +/* Updated: 2017/03/20 14:57:26 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,9 +36,10 @@ int do_the_muther_forker(t_process *p) exit(p->map.launch(p)); } -static int should_i_fork(t_process *p) +static int do_the_fork_if_i_have_to(t_process *p) { - if (IS_PIPESINGLE(*p) && p->type != PROCESS_FILE && p->type != PROCESS_SUBSHELL) + if (IS_PIPESINGLE(*p) + && p->type != PROCESS_FILE && p->type != PROCESS_SUBSHELL) { if (process_redirect(p)) { @@ -46,8 +47,6 @@ static int should_i_fork(t_process *p) return (0); } return (p->map.launch(p)); - /* set_exitstatus(p->map.launch(p), 1); */ - /* return (0); */ } return (do_the_muther_forker(p)); } @@ -58,7 +57,7 @@ int process_launch(t_process *p) DG("p->type=%i", p->type); p->state = PROCESS_RUNNING; - if (!(pid = should_i_fork(p))) + if (!(pid = do_the_fork_if_i_have_to(p))) { DG("launcher did not fork!"); process_resetfds(p); diff --git a/42sh/src/glob/command_getoutput.c b/42sh/src/glob/command_getoutput.c index 53297129..f51688a7 100644 --- a/42sh/src/glob/command_getoutput.c +++ b/42sh/src/glob/command_getoutput.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 19:44:25 by wescande #+# #+# */ -/* Updated: 2017/03/20 11:36:03 by wescande ### ########.fr */ +/* Updated: 2017/03/20 15:10:40 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,11 +49,12 @@ static char *manage_command(char *const av_cmd[]) return (command); } -static void execute_command(char *command, char *const av[], char **env) +static void execute_command(char *const av[], char **env) { t_list *token; t_btree *ast; t_data *data; + char *command; token = NULL; ast = NULL; @@ -61,7 +62,7 @@ static void execute_command(char *command, char *const av[], char **env) data->env = env; data->opts &= ~SH_INTERACTIVE; data->opts &= ~SH_OPTS_JOBC; - command = command ? command : manage_command(av); + command = manage_command(av); DG("command is %s", command); if (do_lexer_routine(&token, command)) { @@ -78,43 +79,37 @@ static void execute_command(char *command, char *const av[], char **env) exit(data_singleton()->parser.state == SUCCESS && ft_exec(&ast) < 0); } -char *command_getoutput(char *command, char *const av[], char **env, int pipe_mode) +char *command_getoutput(char *command) { int ret; int pid; int fds[2]; + char **av; - if (!command && !av) + if (!command) return (NULL); - if (pipe_mode) - pipe(fds); - if (!(pid = fork())) + pipe(fds); + if (!(pid = do_the_muther_forker(NULL))) { - if (pipe_mode) - { - close(fds[PIPE_READ]); - dup2_close(fds[PIPE_WRITE], STDOUT); - } - execute_command(command, av, env); + close(fds[PIPE_READ]); + dup2_close(fds[PIPE_WRITE], STDOUT); + av = ft_sstradd(NULL, data_singleton()->argv[0]); + av = ft_sstradd(av, "-c"); + av = ft_sstradd(av, command); + execve(data_singleton()->argv[0], av, data_singleton()->env); + exit(1); } waitpid(pid, &ret, WUNTRACED); - if (pipe_mode) - return (manage_output(fds)); - return (NULL); + return (manage_output(fds)); } -int command_setoutput(char *command, char *const av[], char **env, int pipe_mode) +int command_setoutput(char *const av[], char **env) { - /* int ret; */ int pid; -(void)pipe_mode; - if (!command && !av) + + if (!av) return (0); if (!(pid = do_the_muther_forker(NULL))) - execute_command(command, av, env); + execute_command(av, env); return (pid); - /* waitpid(pid, &ret, WUNTRACED); */ - /* if (pipe_mode) */ - /* return (manage_output(fds)); */ - /* return (NULL); */ } diff --git a/42sh/src/glob/expand_bquote.c b/42sh/src/glob/expand_bquote.c index 5a97fcb4..22f265f5 100644 --- a/42sh/src/glob/expand_bquote.c +++ b/42sh/src/glob/expand_bquote.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 17:47:53 by wescande #+# #+# */ -/* Updated: 2017/03/19 17:33:11 by wescande ### ########.fr */ +/* Updated: 2017/03/20 15:10:32 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -72,7 +72,7 @@ static char *get_output(char *command) char *output; int len; - if (!(output = command_getoutput(command, NULL, data_singleton()->env, 1))) + if (!(output = command_getoutput(command))) return (NULL); len = ft_strlen(output); while (output[--len] == '\n') @@ -80,18 +80,6 @@ static char *get_output(char *command) return (output); } -static char *treat_str(t_bquote *me, char *sta) -{ - if (sta && *me->str == '\n' - && !is_char_esc(me->esc, CH(*me->wk)[0], me->str)) - *me->str = ';'; - if (sta && *me->str == '\n' - && is_char_esc(me->esc, CH(*me->wk)[0], me->str)) - *me->str = ' '; - return (*me->str == '`' && !sta - && !is_char_esc(me->esc2, CH(*me->wk)[0], me->str) ? me->str : sta); -} - static int search_bquote(t_bquote *me) { char *sta; @@ -100,7 +88,8 @@ static int search_bquote(t_bquote *me) sta = NULL; while (*(++me->str)) { - sta = treat_str(me, sta); + sta = *me->str == '`' && !sta + && !is_char_esc(me->esc2, CH(*me->wk)[0], me->str) ? me->str : sta; if (sta && *me->str == '`' && me->str != sta && !is_char_esc(me->esc2, CH(*me->wk)[0], me->str)) {