diff --git a/42sh/Makefile b/42sh/Makefile index c84761fb..2441243e 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/21 15:47:55 by ariard ### ########.fr # +# Updated: 2017/03/21 17:18:57 by ariard ### ########.fr # # # # **************************************************************************** # NAME = 42sh CC = gcc -FLAGS = -Wall -Wextra -Werror -fvisibility=hidden -fsanitize=address +FLAGS = -Wall -Wextra -Werror -fvisibility=hidden #-fsanitize=address D_FLAGS = -g DELTA = $$(echo "$$(tput cols)-47"|bc) @@ -300,6 +300,7 @@ parser/read_stack.c\ parser/redir_init.c\ parser/stack_init.c\ parser/sym_free.c\ +parser/tree_func_free.c\ parser/tree_wrapper.c SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index cb531282..f80bc6e3 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/21 14:05:41 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 18:09:39 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 1fa276a9..99b4713e 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -144,6 +144,7 @@ int join_ast(t_btree **ast, t_btree **new_node); int gen_node(t_btree **ast); int superflous_token(t_btree **ast, t_list **list); void sym_free(void *data, size_t size); +void tree_func_free(void *data, size_t content_size); struct s_distrostree { diff --git a/42sh/libft/src/btree/btree_del.c b/42sh/libft/src/btree/btree_del.c index f9abecb6..e6c11c28 100644 --- a/42sh/libft/src/btree/btree_del.c +++ b/42sh/libft/src/btree/btree_del.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/05 11:43:03 by jhalford #+# #+# */ -/* Updated: 2016/12/05 11:53:24 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 19:10:56 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ void btree_del(t_btree **root, void (*del)(void *, size_t)) { if (root && *root && del) { + DG(); btree_del(&(*root)->left, del); btree_del(&(*root)->right, del); btree_delone(root, del); diff --git a/42sh/libft/src/sstr/ft_sstrstr.c b/42sh/libft/src/sstr/ft_sstrstr.c index 53a82346..7170a6e6 100644 --- a/42sh/libft/src/sstr/ft_sstrstr.c +++ b/42sh/libft/src/sstr/ft_sstrstr.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 11:29:54 by ariard #+# #+# */ -/* Updated: 2017/03/07 12:44:17 by ariard ### ########.fr */ +/* Updated: 2017/03/21 18:14:45 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,9 +21,6 @@ char *ft_sstrstr(char **sstr, char *find) size = ft_strlen(find); while ((*sstr)) { - DG("size : %d", size); - DG("sstr : %s", *sstr); - DG("find : %s", find); if (ft_strncmp(*sstr, find, size) == 0) return (*sstr); sstr++; diff --git a/42sh/src/builtin/builtin_exit.c b/42sh/src/builtin/builtin_exit.c index fae8ee28..f9625fc9 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/21 15:51:03 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 18:10:58 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_math.c b/42sh/src/builtin/builtin_math.c index ebd0e6c3..6d620c12 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/20 23:21:16 by ariard ### ########.fr */ +/* Updated: 2017/03/21 18:08:31 by ariard ### ########.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" +#define MATHERR_1 "math : invalid variable name" +#define MATHERR_2 "math : invalid operator" +#define MATHERR_3 "math : invalid operand" +#define MATHERR_4 "math : division by 0" static int init_math(char **var, char **value, char **operator, char **operand) @@ -38,7 +38,7 @@ static int get_value(char *var, char **value) ret = word_is_assignment((char *[]) {var, (esc + 1)}); ft_strdel(&esc); if (!ret) - return (error_msg(MATHERR_1)); + return (SH_ERR(MATHERR_1)); temp = ft_sstrstr(data_singleton()->local_var, var); if (temp) { @@ -66,7 +66,7 @@ static int do_math(char **value, char *operator, char *operand) else ope2 = 0; if ((operator[0] == '/' || operator[0] == '%') && ope2 == 0) - return (error_msg(MATHERR_4)); + return (SH_ERR(MATHERR_4)); else { ope1 = (operator[0] == '+') ? ope1 + ope2 : ope1; @@ -90,17 +90,17 @@ int builtin_math(const char *path, char *const av[], char *const envp[]) (void)path; (void)envp; if (!av || !av[1] || !av[2] || !av[3] || av[4]) - return (builtin_return_status(0, error_msg(MATHERR_0))); + return (builtin_return_status(0, SH_ERR(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))); + return (builtin_return_status(0, SH_ERR(MATHERR_2))); operand = av[3]; if (!ft_stris(operand, &ft_isdigit)) - return (builtin_return_status(0, error_msg(MATHERR_3))); + return (builtin_return_status(0, SH_ERR(MATHERR_3))); if (do_math(&value, operator, operand)) return (builtin_return_status(0, 1)); builtin_setenv("setenv", (char *[]){"local", var, value, 0}, NULL); diff --git a/42sh/src/builtin/builtin_setenv.c b/42sh/src/builtin/builtin_setenv.c index a385acc5..4cfda406 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/21 15:20:27 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 18:11:31 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/ast_free.c b/42sh/src/exec/ast_free.c index 17576e73..756ea439 100644 --- a/42sh/src/exec/ast_free.c +++ b/42sh/src/exec/ast_free.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/05 11:50:51 by jhalford #+# #+# */ -/* Updated: 2017/03/21 14:22:58 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 19:14:31 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_leaf.c b/42sh/src/exec/exec_leaf.c index 60ac11d6..2c70f904 100644 --- a/42sh/src/exec/exec_leaf.c +++ b/42sh/src/exec/exec_leaf.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 15:47:30 by wescande #+# #+# */ -/* Updated: 2017/03/21 13:16:25 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 18:52:16 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/node_copy.c b/42sh/src/exec/node_copy.c index b323fee7..5b829c78 100644 --- a/42sh/src/exec/node_copy.c +++ b/42sh/src/exec/node_copy.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 03:38:36 by wescande #+# #+# */ -/* Updated: 2017/03/20 16:28:33 by gwojda ### ########.fr */ +/* Updated: 2017/03/21 18:44:57 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/plaunch_function.c b/42sh/src/exec/plaunch_function.c index 87c7e34c..872a9542 100644 --- a/42sh/src/exec/plaunch_function.c +++ b/42sh/src/exec/plaunch_function.c @@ -6,14 +6,30 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 03:23:59 by wescande #+# #+# */ -/* Updated: 2017/03/20 18:15:21 by gwojda ### ########.fr */ +/* Updated: 2017/03/21 18:10:44 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +#define FUNCERR_0 SHELL_NAME ":maximum nested function level reached\n" + int plaunch_function(t_process *p) { + char *func_lvl; + int value; + + if ((func_lvl = ft_sstrstr(data_singleton()->env, "FUNC_LVL"))) + { + func_lvl += ft_strlenchr(func_lvl, '=') + 1; + if ((value = ft_atoi(func_lvl)) >= 199) + return (SH_ERR(FUNCERR_0)); + value += 1; + } + else + value = 0; + builtin_setenv("setenv", (char *[]){"env", "FUNC_LVL", + ft_itoa(value), 0}, NULL); ft_exec(&p->data.function.content); return (ft_atoi(ft_getenv(data_singleton()->env, "?"))); } diff --git a/42sh/src/exec/process_launch.c b/42sh/src/exec/process_launch.c index 95f7575d..a03e34f2 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/21 01:26:45 by wescande ### ########.fr */ +/* Updated: 2017/03/21 18:52:29 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/glob/lib_perso/ft_ld_clear.c b/42sh/src/glob/lib_perso/ft_ld_clear.c index c36d9695..3cbdf315 100644 --- a/42sh/src/glob/lib_perso/ft_ld_clear.c +++ b/42sh/src/glob/lib_perso/ft_ld_clear.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 19:56:49 by wescande #+# #+# */ -/* Updated: 2017/03/07 21:18:59 by wescande ### ########.fr */ +/* Updated: 2017/03/21 18:41:08 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/glob/lib_perso/ft_ld_del.c b/42sh/src/glob/lib_perso/ft_ld_del.c index 50667e81..84fcb403 100644 --- a/42sh/src/glob/lib_perso/ft_ld_del.c +++ b/42sh/src/glob/lib_perso/ft_ld_del.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 15:54:53 by wescande #+# #+# */ -/* Updated: 2017/03/07 21:17:50 by wescande ### ########.fr */ +/* Updated: 2017/03/21 18:26:53 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/glob/lib_perso/ft_ld_front.c b/42sh/src/glob/lib_perso/ft_ld_front.c index 1599f49e..bc7c5fd7 100644 --- a/42sh/src/glob/lib_perso/ft_ld_front.c +++ b/42sh/src/glob/lib_perso/ft_ld_front.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 18:26:11 by wescande #+# #+# */ -/* Updated: 2017/01/05 14:17:27 by wescande ### ########.fr */ +/* Updated: 2017/03/21 18:32:33 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/glob/lib_perso/ft_tabdel.c b/42sh/src/glob/lib_perso/ft_tabdel.c index 680c7b83..80d56c90 100644 --- a/42sh/src/glob/lib_perso/ft_tabdel.c +++ b/42sh/src/glob/lib_perso/ft_tabdel.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/04 16:29:54 by wescande #+# #+# */ -/* Updated: 2017/03/07 21:18:08 by wescande ### ########.fr */ +/* Updated: 2017/03/21 18:41:20 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 0f667833..eda26e06 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/20 14:45:40 by gwojda #+# #+# */ -/* Updated: 2017/03/21 16:35:36 by ariard ### ########.fr */ +/* Updated: 2017/03/21 19:05:45 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_func.c b/42sh/src/parser/add_func.c index 12d8ae08..3aa20c87 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/17 17:42:22 by ariard ### ########.fr */ +/* Updated: 2017/03/21 19:36:30 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -86,22 +86,25 @@ t_list *is_already_func(t_btree **new) t_btree **ast; char **new_name; char **old_name; + int ret; tmp = data_singleton()->lst_func; new_name = token_to_argv(((t_astnode *)(*new)->item)->data.cmd.token, 1); - while (tmp) + ret = 1; + while (tmp && ret) { - ast = tmp->content; - if (!*ast) - return (NULL); + if ((ast = tmp->content) && !*ast) + break; old_name = token_to_argv(((t_astnode *) (*ast)->item)->data.cmd.token, 1); - if (!new_name || !new_name[0] || !old_name || !old_name[0]) - return (NULL); - if (!ft_strcmp(new_name[0], old_name[0])) - return (tmp); + ret = (new_name && new_name[0] && old_name && old_name[0] + && !ft_strcmp(new_name[0], old_name[0])) ? 0 : 1; + ft_tabdel(&old_name); tmp = tmp->next; } + ft_tabdel(&new_name); + if (!ret) + return (tmp); return (NULL); } @@ -111,10 +114,12 @@ int add_one_func(t_btree **ast, t_list **lst) t_list *old_func; (void)lst; - func_ast = btree_map(*ast, node_copy); + func_ast = btree_map(*ast, &node_copy); if ((old_func = is_already_func(&func_ast))) + { ft_lst_delif(&data_singleton()->lst_func, - old_func->content, &ft_addrcmp, &ast_free); + old_func->content, &ft_addrcmp, &tree_func_free); + } ft_lsteadd(&data_singleton()->lst_func, ft_lstnew(&func_ast, sizeof(*ast))); return (0); } diff --git a/42sh/src/parser/tree_func_free.c b/42sh/src/parser/tree_func_free.c new file mode 100644 index 00000000..e224cdcc --- /dev/null +++ b/42sh/src/parser/tree_func_free.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tree_func_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/21 19:02:54 by ariard #+# #+# */ +/* Updated: 2017/03/21 19:14:16 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void tree_func_free(void *data, size_t content_size) +{ + t_btree **root; + + (void)content_size; + root = data; + btree_del(root, &ast_free); +}