math correction

This commit is contained in:
Antoine Riard 2017-03-17 23:17:48 +01:00
parent 30cddb4983
commit a1cb831783
13 changed files with 115 additions and 119 deletions

View file

@ -265,7 +265,6 @@ line-editing/resize.c\
main/data_exit.c\ main/data_exit.c\
main/data_init.c\ main/data_init.c\
main/data_singleton.c\ main/data_singleton.c\
main/error_msg.c\
main/ft_putast.c\ main/ft_putast.c\
main/main.c\ main/main.c\
main/shell_init.c\ main/shell_init.c\

1
42sh/file Normal file
View file

@ -0,0 +1 @@
/Users/ariard/Projects/42sh

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 22:59:57 by jhalford #+# #+# */ /* Created: 2017/03/14 22:59:57 by jhalford #+# #+# */
/* Updated: 2017/03/16 14:19:03 by jhalford ### ########.fr */ /* Updated: 2017/03/17 18:59:55 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,12 +19,6 @@
# define BT_EXPORT_LP (1 << 0) # define BT_EXPORT_LP (1 << 0)
# 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"
t_execf *is_builtin(t_process *p); t_execf *is_builtin(t_process *p);
int builtin_export(const char *path, char *const av[], char *const envp[]); 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_unset(const char *path, char *const av[], char *const envp[]);

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/17 00:20:53 by ariard ### ########.fr */ /* Updated: 2017/03/17 18:56:44 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -1,97 +1,115 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* exec_math.c :+: :+: :+: */ /* builtin_math.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 10:58:49 by ariard #+# #+# */ /* Created: 2017/03/17 18:54:00 by ariard #+# #+# */
/* Updated: 2017/03/16 16:40:27 by jhalford ### ########.fr */ /* Updated: 2017/03/17 19:59:42 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
/* #include "minishell.h" */ #include "minishell.h"
/* static int init_math(char **var, char **value, char **operator, char **operand) */ # define MATHERR_0 "math : invalid number of arguments\n"
/* { */ # define MATHERR_1 "math : invalid variable name\n"
/* *var = NULL; */ # define MATHERR_2 "math : invalid operator\n"
/* *value = NULL; */ # define MATHERR_3 "math : invalid operand\n"
/* *operator = NULL; */ # define MATHERR_4 "math : division by 0\n"
/* *operand = NULL; */
/* return (0); */
/* } */
/* static int get_value(char **var, char **value) */ static int error_msg(char *msg)
/* { */ {
/* char *temp; */ ft_dprintf(2, "{red}%s{eoc}", msg);
return (-1);
}
/* if (!word_is_assignment(var)) */ static int init_math(char **var, char **value, char **operator,
/* return (error_msg(MATHERR_1)); */ char **operand)
/* temp = ft_sstrstr(data_singleton()->local_var, *var); */ {
/* if (temp) */ *var = NULL;
/* { */ *value = NULL;
/* temp += ft_strlenchr(temp, '=') + 1; */ *operator = NULL;
/* *value = ft_strdup(temp); */ *operand = NULL;
/* if (!(ft_stris(*value, &ft_isdigit))) */ return (0);
/* { */ }
/* ft_strdel(value); */
/* *value = ft_itoa(0); */
/* } */
/* } */
/* else */
/* *value = ft_itoa(0); */
/* return (0); */
/* } */
/* static int do_math(char **value, char *operator, char *operand) */ static int get_value(char *var, char **value)
/* { */ {
/* long ope1; */ char *temp;
/* long ope2; */ char *esc;
int ret;
/* ope1 = ft_atoi(*value); */ esc = ft_strnew((ft_strlen(var) >> 3) + 1);
/* if (operand) */ ret = word_is_assignment((char *[]) {var, (esc + 1)});
/* ope2 = ft_atoi(operand); */ ft_strdel(&esc);
/* else */ if (!ret)
/* ope2 = 0; */ return (error_msg(MATHERR_1));
/* if ((operator[0] == '/' || operator[0] == '%') && ope2 == 0) */ temp = ft_sstrstr(data_singleton()->local_var, var);
/* return (error_msg(MATHERR_4)); */ if (temp)
/* else */ {
/* { */ temp += ft_strlenchr(temp, '=') + 1;
/* ope1 = (operator[0] == '+') ? ope1 + ope2 : ope1; */ *value = ft_strdup(temp);
/* ope1 = (operator[0] == '-') ? ope1 - ope2 : ope1; */ if (!(ft_stris(*value, &ft_isdigit)))
/* ope1 = (operator[0] == '/') ? ope1 / ope2 : ope1; */ {
/* ope1 = (operator[0] == '*') ? ope1 * ope2 : ope1; */ ft_strdel(value);
/* ope1 = (operator[0] == '%') ? ope1 % ope2 : ope1; */ *value = ft_itoa(0);
/* } */ }
/* ft_strdel(value); */ }
/* *value = ft_itoa(ope1); */ else
/* return (0); */ *value = ft_itoa(0);
/* } */ return (0);
}
/* int builtin_math(const char *path, char *const av[], char *const envp[]) */ static int do_math(char **value, char *operator, char *operand)
/* { */ {
/* char *var; */ long ope1;
/* char *value; */ long ope2;
/* char *operator; */
/* char *operand; */
/* (void)path; */ ope1 = ft_atoi(*value);
/* (void)envp; */ if (operand)
/* if (!av || !av[1] || !av[2] || !av[3] || av[4]) */ ope2 = ft_atoi(operand);
/* return (error_msg(MATHERR_0)); */ else
/* init_math(&var, &value, &operator, &operand); */ ope2 = 0;
/* var = av[1]; */ if ((operator[0] == '/' || operator[0] == '%') && ope2 == 0)
/* if (get_value(&var, &value) == -1) */ return (error_msg(MATHERR_4));
/* return (-1); */ else
/* operator = av[2]; */ {
/* if (!(ft_strlen(operator) == 1 && (operator[0] == '+' || operator[0] == '-' */ ope1 = (operator[0] == '+') ? ope1 + ope2 : ope1;
/* || operator[0] == '/' || operator[0] == '*' || operator[0] == '%'))) */ ope1 = (operator[0] == '-') ? ope1 - ope2 : ope1;
/* return (error_msg(MATHERR_2)); */ ope1 = (operator[0] == '/') ? ope1 / ope2 : ope1;
/* operand = av[3]; */ ope1 = (operator[0] == '*') ? ope1 * ope2 : ope1;
/* if (!ft_stris(operand, &ft_isdigit)) */ ope1 = (operator[0] == '%') ? ope1 % ope2 : ope1;
/* return (error_msg(MATHERR_3)); */ }
/* if (do_math(&value, operator, operand) == -1) */ ft_strdel(value);
/* return (-1); */ *value = ft_itoa(ope1);
/* builtin_setenv("setenv", (char *[]){"local", var, value, 0}, data_singleton()->local_var); */ return (0);
/* return (0); */ }
/* } */
int builtin_math(const char *path, char *const av[], char *const envp[])
{
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);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 13:09:57 by jhalford #+# #+# */ /* Created: 2016/12/13 13:09:57 by jhalford #+# #+# */
/* Updated: 2017/03/16 15:54:15 by jhalford ### ########.fr */ /* Updated: 2017/03/17 18:56:00 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -29,7 +29,7 @@ t_stof g_builtin[] =
{"read", &builtin_read}, {"read", &builtin_read},
{"hash", &builtin_hash}, {"hash", &builtin_hash},
{"history", &builtin_history}, {"history", &builtin_history},
/* {"math", &builtin_math}, */ {"math", &builtin_math},
{NULL, NULL}, {NULL, NULL},
}; };

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/27 18:19:55 by wescande #+# #+# */ /* Created: 2017/01/27 18:19:55 by wescande #+# #+# */
/* Updated: 2017/03/14 23:11:59 by wescande ### ########.fr */ /* Updated: 2017/03/17 19:31:16 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/19 16:28:49 by gwojda #+# #+# */ /* Created: 2016/12/19 16:28:49 by gwojda #+# #+# */
/* Updated: 2017/03/17 15:54:45 by gwojda ### ########.fr */ /* Updated: 2017/03/17 20:04:42 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -82,8 +82,8 @@ int ft_read_stdin(char **input)
if (read_stdin(&ret, &j) < 0) if (read_stdin(&ret, &j) < 0)
return (-1); return (-1);
DG("key value hex = %x", ret); DG("key value hex = %x", ret);
if (ft_completion(ret, str, pos)) // if (ft_completion(ret, str, pos))
continue ; // continue ;
while (g_key[j].value && g_key[j].value != ret) while (g_key[j].value && g_key[j].value != ret)
++j; ++j;
if (g_key[j].value && (ret = g_key[j].f(str, pos))) if (g_key[j].value && (ret = g_key[j].f(str, pos)))

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */ /* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */
/* Updated: 2017/03/17 16:10:40 by gwojda ### ########.fr */ /* Updated: 2017/03/17 20:02:57 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -1,19 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* error_msg.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/16 14:14:47 by jhalford #+# #+# */
/* Updated: 2017/03/16 14:17:29 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int error_msg(char *msg)
{
ft_dprintf(2, "{red}%s{eoc}\n", msg);
return (-1);
}

View file

@ -88,8 +88,12 @@ static int handle_instruction()
{ {
if ((ret = do_readline_routine(&stream)) > 0) if ((ret = do_readline_routine(&stream)) > 0)
return (ret); return (ret);
sleep(10);
return (1);
if (do_lexer_routine(stream) > 0) if (do_lexer_routine(stream) > 0)
continue ; continue ;
sleep(10);
return (1);
token_print(data->token); token_print(data->token);
if (do_parser_routine() > 0) if (do_parser_routine() > 0)
break ; break ;

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 20:49:15 by ariard #+# #+# */ /* Created: 2017/02/15 20:49:15 by ariard #+# #+# */
/* Updated: 2017/03/17 17:29:43 by ariard ### ########.fr */ /* Updated: 2017/03/17 19:31:52 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 18:32:59 by ariard #+# #+# */ /* Created: 2017/02/15 18:32:59 by ariard #+# #+# */
/* Updated: 2017/03/14 00:50:34 by ariard ### ########.fr */ /* Updated: 2017/03/17 19:50:08 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -88,7 +88,6 @@ int build_tree(t_btree **ast, t_list **lst)
{ {
if ((isseparator(token, cache) && g_treematch[i].type == token->type)) if ((isseparator(token, cache) && g_treematch[i].type == token->type))
{ {
DG("build %s", read_state(g_treematch[i].type));
cache = token->type; cache = token->type;
return (g_treematch[i].add(ast, lst)); return (g_treematch[i].add(ast, lst));
} }