This commit is contained in:
Jack Halford 2017-03-15 22:03:26 +01:00
commit 0cf371e58a
20 changed files with 92 additions and 40 deletions

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 22:59:57 by jhalford #+# #+# */
/* Updated: 2017/03/15 18:44:41 by jhalford ### ########.fr */
/* Updated: 2017/03/15 21:55:22 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,6 +19,12 @@
# define BT_EXPORT_LP (1 << 0)
# define MATHERR_0 "{red}math : invalid number of arguments{eoc}\n"
# define MATHERR_1 "{red}math : invalid variable name{eoc}\n"
# define MATHERR_2 "{red}math : invalid operator{eoc}\n"
# define MATHERR_3 "{red}math : invalid operand{eoc}\n"
# define MATHERR_4 "{red}math : division by 0{eoc}\n"
t_execf *is_builtin(t_process *p);
int builtin_export(const char *path, char *const av[], char *const envp[]);
int builtin_unset(const char *path, char *const av[], char *const envp[]);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */
/* Updated: 2017/03/15 19:13:36 by jhalford ### ########.fr */
/* Updated: 2017/03/15 21:54:52 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -116,7 +116,6 @@ int add_pattern(t_btree **ast, t_list **lst);
int add_subshell_cmd(t_btree **ast, t_list **lst);
int add_subshell_sep(t_btree **ast, t_list **lst);
int add_func_cmd(t_btree **ast, t_list **lst);
int add_func_sep(t_btree **ast, t_list **lst);
int add_one_func(t_btree **ast, t_list **lst);
int add_pipe(t_btree **ast, t_list **lst);
int add_null(t_btree **ast, t_list **lst);
@ -178,10 +177,10 @@ union u_astdata
struct s_astnode
{
int pattern;
int nest;
int full;
int cache;
int pattern;
int nest;
int full;
int cache;
t_type type;
t_astdata data;
};

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 11:57:53 by jhalford #+# #+# */
/* Updated: 2017/03/15 10:52:44 by gwojda ### ########.fr */
/* Updated: 2017/03/15 20:55:40 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 10:58:49 by ariard #+# #+# */
/* Updated: 2017/03/14 22:45:23 by ariard ### ########.fr */
/* Updated: 2017/03/15 21:02:59 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -25,6 +25,8 @@ static int get_value(char **var, char **value)
{
char *temp;
if (!word_is_assignment(var))
return (ft_error_message(MATHERR_1));
temp = ft_sstrstr(data_singleton()->local_var, *var);
if (temp)
{
@ -51,10 +53,8 @@ static int do_math(char **value, char *operator, char *operand)
ope2 = ft_atoi(operand);
else
ope2 = 0;
if (ft_strlen(operator) > 1)
return (0);
if ((operator[0] == '/' || operator[0] == '%') && ope2 == 0)
ope1 = 0;
return (ft_error_message(MATHERR_4));
else
{
ope1 = (operator[0] == '+') ? ope1 + ope2 : ope1;
@ -77,13 +77,21 @@ 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 (ft_error_message(MATHERR_0));
init_math(&var, &value, &operator, &operand);
var = av[1];
get_value(&var, &value);
if (get_value(&var, &value) == -1)
return (-1);
operator = av[2];
operand = av[3];
// DG("math %s %s %s", var, operator, operand);
do_math(&value, operator, operand);
if (!(ft_strlen(operator) == 1 && (operator[0] == '+' || operator[0] == '-'
|| operator[0] == '/' || operator[0] == '*' || operator[0] == '%')))
return (ft_error_message(MATHERR_2));
operand = av[3];
if (!ft_stris(operand, &ft_isdigit))
return (ft_error_message(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);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 13:09:57 by jhalford #+# #+# */
/* Updated: 2017/03/15 15:58:53 by wescande ### ########.fr */
/* Updated: 2017/03/15 20:01:03 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -37,6 +37,7 @@ t_execf *is_builtin(t_process *p)
{
int i;
DG("in builtin");
i = -1;
while (g_builtin[++i].name)
{

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/05 11:50:51 by jhalford #+# #+# */
/* Updated: 2017/03/08 00:59:53 by ariard ### ########.fr */
/* Updated: 2017/03/15 19:18:02 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 15:47:30 by wescande #+# #+# */
/* Updated: 2017/03/15 04:18:47 by wescande ### ########.fr */
/* Updated: 2017/03/15 19:58:48 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/08 02:45:15 by wescande #+# #+# */
/* Updated: 2017/03/08 03:22:34 by wescande ### ########.fr */
/* Updated: 2017/03/15 18:57:03 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,13 +14,21 @@
t_btree *is_function(t_process *p)
{
t_list *tmp;
t_list *tmp;
t_btree **ast;
char **av;
tmp = data_singleton()->lst_func;
while (tmp)
{
if (!ft_strcmp(((t_astnode *)tmp->content)->data.str, p->data.cmd.av[0]))
return (btree_map(p->data.subshell.content, node_copy));
ast = tmp->content;
if (!*ast)
return (NULL);
av = token_to_argv(((t_astnode *)(*ast)->item)->data.cmd.token, 1);
if (!av || !av[0])
return (NULL);
if (!ft_strcmp(av[0], p->data.cmd.av[0]))
return (btree_map((*ast)->right, node_copy));
tmp = tmp->next;
}
return (NULL);

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/08 03:38:36 by wescande #+# #+# */
/* Updated: 2017/03/15 01:24:26 by ariard ### ########.fr */
/* Updated: 2017/03/15 16:48:40 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -30,7 +30,8 @@ void *node_copy(void *data)
new->data.cmd.redir = ft_lstmap(old->data.cmd.redir, &redir_copy);
new->data.cmd.token = ft_ld_copy(old->data.cmd.token, &tab_esc_copy);
}
if (old->type == TK_FOR || old->type == TK_PAREN_OPEN || old->type == TK_CASE)
if (old->type == TK_FOR || old->type == TK_PAREN_OPEN || old->type == TK_CASE
|| old->type == FNAME)
new->data.cmd.token = ft_ld_copy(old->data.cmd.token, &tab_esc_copy);
return (new);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/13 19:09:30 by jhalford #+# #+# */
/* Updated: 2017/03/14 00:04:09 by jhalford ### ########.fr */
/* Updated: 2017/03/15 18:13:05 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,8 +14,10 @@
int plaunch_brace(t_process *p)
{
DG(" do brace");
if (IS_PIPESINGLE(*p))
{
DG("is pipgesingle");
if (process_redirect(p))
set_exitstatus(1, 1);
else

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/08 03:23:59 by wescande #+# #+# */
/* Updated: 2017/03/13 20:28:57 by jhalford ### ########.fr */
/* Updated: 2017/03/15 19:17:01 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,7 @@
static int do_function(t_process *p)
{
DG("do function");
ft_exec(&p->data.function.content);
return (ft_atoi(ft_getenv(data_singleton()->env, "?")));
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/05 14:54:45 by jhalford #+# #+# */
/* Updated: 2017/03/15 00:57:31 by ariard ### ########.fr */
/* Updated: 2017/03/15 18:20:38 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -34,6 +34,7 @@ static int process_set_spec(t_process *p, t_btree *ast)
if (!ast)
return (0);
item = ast->item;
DG("process set spec");
while (g_setprocessmap[++i].id)
if (item->type == g_setprocessmap[i].id)
{

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 15:06:05 by wescande #+# #+# */
/* Updated: 2017/03/14 21:25:01 by ariard ### ########.fr */
/* Updated: 2017/03/15 18:48:13 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,7 +27,8 @@ int pset_cmd(t_process *p, t_btree *ast)
p->type = PROCESS_FILE;
if ((func = is_function(p)))
{
p->data.subshell.content = func;
btree_print(STDBUG, func, &ft_putast);
p->data.function.content = func;
p->type = PROCESS_FUNCTION;
}
else if ((p->data.cmd.execf = is_builtin(p)))

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 18:59:11 by wescande #+# #+# */
/* Updated: 2017/03/11 20:47:05 by ariard ### ########.fr */
/* Updated: 2017/03/15 20:55:15 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 20:49:15 by ariard #+# #+# */
/* Updated: 2017/03/15 02:12:28 by ariard ### ########.fr */
/* Updated: 2017/03/15 16:46:16 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -95,6 +95,7 @@ int add_cmd(t_btree **ast, t_list **lst)
if (token->type == TK_WORD || token->type == TK_ASSIGNMENT_WORD || token->type == TK_NAME)
{
DG("type is %s", read_state(node->type));
DG("data is %s", token->data);
ft_ld_pushback(&node->data.cmd.token,
gen_tab(token->data, token->esc, token->esc2, 1));
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/24 23:43:07 by ariard #+# #+# */
/* Updated: 2017/03/15 00:26:57 by ariard ### ########.fr */
/* Updated: 2017/03/15 20:07:38 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,7 +24,6 @@ int isfunc_name(t_btree **ast, t_list **lst)
if (node->type == CMD && token->type == TK_PAREN_OPEN)
{
node->type = FNAME;
node->data.str = ft_strdup(token->data);
return (1);
}
if (node->type == FNAME && token->type == TK_PAREN_CLOSE
@ -81,17 +80,39 @@ int add_func_cmd(t_btree **ast, t_list **lst)
return (add_cmd(&(*ast)->right, lst));
}
int add_func_sep(t_btree **ast, t_list **lst)
t_list *is_already_func(t_btree **new)
{
return (add_sep(&(*ast)->right, lst));
t_list *tmp;
t_btree **ast;
char **new_name;
char **old_name;
tmp = data_singleton()->lst_func;
new_name = token_to_argv(((t_astnode *)(*new)->item)->data.cmd.token, 1);
while (tmp)
{
ast = tmp->content;
if (!*ast)
return (NULL);
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);
tmp = tmp->next;
}
return (NULL);
}
int add_one_func(t_btree **ast, t_list **lst)
{
t_btree *func_ast;
t_list *old_func;
(void)lst;
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);
ft_lsteadd(&data_singleton()->lst_func, ft_lstnew(&func_ast, sizeof(*ast)));
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 19:12:07 by ariard #+# #+# */
/* Updated: 2017/03/15 01:41:06 by ariard ### ########.fr */
/* Updated: 2017/03/15 18:57:13 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,7 +27,7 @@ int add_sep(t_btree **ast, t_list **lst)
else if (issubshell(ast, lst))
return (add_subshell_sep(ast, lst));
else if (isfunc(ast, lst))
return (add_func_sep(ast, lst));
return (add_subshell_sep(ast, lst));
else if (isbang_sep(ast, lst))
return (add_bang_sep(ast, lst));
if (!*ast)

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/11 16:17:38 by ariard #+# #+# */
/* Updated: 2017/03/15 17:56:56 by jhalford ### ########.fr */
/* Updated: 2017/03/15 19:59:19 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 15:32:10 by ariard #+# #+# */
/* Updated: 2017/03/14 22:11:43 by ariard ### ########.fr */
/* Updated: 2017/03/15 16:33:57 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,8 @@
char *read_state(t_sym current)
{
if (current == FNAME)
return ("FNAME");
if (current == PIPE_CLOSE_SEQUENCE)
return ("PIPE_CLOSE_SEQUENCE");
if (current == TK_BANG)