This commit is contained in:
Antoine Riard 2017-03-21 19:38:47 +01:00
parent 634d498b03
commit 6673422632
20 changed files with 85 additions and 42 deletions

View file

@ -6,14 +6,14 @@
# By: wescande <wescande@student.42.fr> +#+ +:+ +#+ # # By: wescande <wescande@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2016/08/29 21:32:58 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 NAME = 42sh
CC = gcc CC = gcc
FLAGS = -Wall -Wextra -Werror -fvisibility=hidden -fsanitize=address FLAGS = -Wall -Wextra -Werror -fvisibility=hidden #-fsanitize=address
D_FLAGS = -g D_FLAGS = -g
DELTA = $$(echo "$$(tput cols)-47"|bc) DELTA = $$(echo "$$(tput cols)-47"|bc)
@ -300,6 +300,7 @@ parser/read_stack.c\
parser/redir_init.c\ parser/redir_init.c\
parser/stack_init.c\ parser/stack_init.c\
parser/sym_free.c\ parser/sym_free.c\
parser/tree_func_free.c\
parser/tree_wrapper.c parser/tree_wrapper.c
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -144,6 +144,7 @@ int join_ast(t_btree **ast, t_btree **new_node);
int gen_node(t_btree **ast); int gen_node(t_btree **ast);
int superflous_token(t_btree **ast, t_list **list); int superflous_token(t_btree **ast, t_list **list);
void sym_free(void *data, size_t size); void sym_free(void *data, size_t size);
void tree_func_free(void *data, size_t content_size);
struct s_distrostree struct s_distrostree
{ {

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/05 11:43:03 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) if (root && *root && del)
{ {
DG();
btree_del(&(*root)->left, del); btree_del(&(*root)->left, del);
btree_del(&(*root)->right, del); btree_del(&(*root)->right, del);
btree_delone(root, del); btree_delone(root, del);

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 11:29:54 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); size = ft_strlen(find);
while ((*sstr)) while ((*sstr))
{ {
DG("size : %d", size);
DG("sstr : %s", *sstr);
DG("find : %s", find);
if (ft_strncmp(*sstr, find, size) == 0) if (ft_strncmp(*sstr, find, size) == 0)
return (*sstr); return (*sstr);
sstr++; sstr++;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:28:41 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,17 +6,17 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/17 18:54:00 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" #include "minishell.h"
#define MATHERR_0 "math : invalid number of arguments\n" #define MATHERR_0 "math : invalid number of arguments"
#define MATHERR_1 "math : invalid variable name\n" #define MATHERR_1 "math : invalid variable name"
#define MATHERR_2 "math : invalid operator\n" #define MATHERR_2 "math : invalid operator"
#define MATHERR_3 "math : invalid operand\n" #define MATHERR_3 "math : invalid operand"
#define MATHERR_4 "math : division by 0\n" #define MATHERR_4 "math : division by 0"
static int init_math(char **var, char **value, char **operator, static int init_math(char **var, char **value, char **operator,
char **operand) char **operand)
@ -38,7 +38,7 @@ static int get_value(char *var, char **value)
ret = word_is_assignment((char *[]) {var, (esc + 1)}); ret = word_is_assignment((char *[]) {var, (esc + 1)});
ft_strdel(&esc); ft_strdel(&esc);
if (!ret) if (!ret)
return (error_msg(MATHERR_1)); return (SH_ERR(MATHERR_1));
temp = ft_sstrstr(data_singleton()->local_var, var); temp = ft_sstrstr(data_singleton()->local_var, var);
if (temp) if (temp)
{ {
@ -66,7 +66,7 @@ static int do_math(char **value, char *operator, char *operand)
else else
ope2 = 0; ope2 = 0;
if ((operator[0] == '/' || operator[0] == '%') && ope2 == 0) if ((operator[0] == '/' || operator[0] == '%') && ope2 == 0)
return (error_msg(MATHERR_4)); return (SH_ERR(MATHERR_4));
else else
{ {
ope1 = (operator[0] == '+') ? ope1 + ope2 : ope1; 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)path;
(void)envp; (void)envp;
if (!av || !av[1] || !av[2] || !av[3] || av[4]) 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); init_math(&var, &value, &operator, &operand);
var = av[1]; var = av[1];
if (get_value(var, &value)) if (get_value(var, &value))
return (builtin_return_status(0, 1)); return (builtin_return_status(0, 1));
operator = av[2]; operator = av[2];
if (ft_strlen(operator) != 1 || !(ft_strchr("+-/*%", operator[0]))) 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]; operand = av[3];
if (!ft_stris(operand, &ft_isdigit)) 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)) if (do_math(&value, operator, operand))
return (builtin_return_status(0, 1)); return (builtin_return_status(0, 1));
builtin_setenv("setenv", (char *[]){"local", var, value, 0}, NULL); builtin_setenv("setenv", (char *[]){"local", var, value, 0}, NULL);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:25:17 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/05 11:50:51 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 15:47:30 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/08 03:38:36 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,14 +6,30 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/08 03:23:59 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" #include "minishell.h"
#define FUNCERR_0 SHELL_NAME ":maximum nested function level reached\n"
int plaunch_function(t_process *p) 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); ft_exec(&p->data.function.content);
return (ft_atoi(ft_getenv(data_singleton()->env, "?"))); return (ft_atoi(ft_getenv(data_singleton()->env, "?")));
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/13 22:21:19 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 19:56:49 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 15:54:53 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 18:26:11 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/04 16:29:54 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/20 14:45:40 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/24 23:43:07 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; t_btree **ast;
char **new_name; char **new_name;
char **old_name; char **old_name;
int ret;
tmp = data_singleton()->lst_func; tmp = data_singleton()->lst_func;
new_name = token_to_argv(((t_astnode *)(*new)->item)->data.cmd.token, 1); 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 = tmp->content) && !*ast)
if (!*ast) break;
return (NULL);
old_name = token_to_argv(((t_astnode *) old_name = token_to_argv(((t_astnode *)
(*ast)->item)->data.cmd.token, 1); (*ast)->item)->data.cmd.token, 1);
if (!new_name || !new_name[0] || !old_name || !old_name[0]) ret = (new_name && new_name[0] && old_name && old_name[0]
return (NULL); && !ft_strcmp(new_name[0], old_name[0])) ? 0 : 1;
if (!ft_strcmp(new_name[0], old_name[0])) ft_tabdel(&old_name);
return (tmp);
tmp = tmp->next; tmp = tmp->next;
} }
ft_tabdel(&new_name);
if (!ret)
return (tmp);
return (NULL); return (NULL);
} }
@ -111,10 +114,12 @@ int add_one_func(t_btree **ast, t_list **lst)
t_list *old_func; t_list *old_func;
(void)lst; (void)lst;
func_ast = btree_map(*ast, node_copy); func_ast = btree_map(*ast, &node_copy);
if ((old_func = is_already_func(&func_ast))) if ((old_func = is_already_func(&func_ast)))
{
ft_lst_delif(&data_singleton()->lst_func, 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))); ft_lsteadd(&data_singleton()->lst_func, ft_lstnew(&func_ast, sizeof(*ast)));
return (0); return (0);
} }

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tree_func_free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}