From a1cb831783954b7df98d80482584c1a55506d571 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Fri, 17 Mar 2017 23:17:48 +0100 Subject: [PATCH] math correction --- 42sh/Makefile | 1 - 42sh/file | 1 + 42sh/includes/builtin.h | 8 +- 42sh/includes/minishell.h | 2 +- 42sh/src/builtin/builtin_math.c | 180 +++++++++++++++++-------------- 42sh/src/builtin/is_builtin.c | 4 +- 42sh/src/glob/is_char_esc.c | 2 +- 42sh/src/line-editing/get_key.c | 6 +- 42sh/src/line-editing/readline.c | 2 +- 42sh/src/main/error_msg.c | 19 ---- 42sh/src/main/main.c | 4 + 42sh/src/parser/add_cmd.c | 2 +- 42sh/src/parser/build_tree.c | 3 +- 13 files changed, 115 insertions(+), 119 deletions(-) create mode 100644 42sh/file delete mode 100644 42sh/src/main/error_msg.c diff --git a/42sh/Makefile b/42sh/Makefile index 98dd358c..8f480a02 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -265,7 +265,6 @@ line-editing/resize.c\ main/data_exit.c\ main/data_init.c\ main/data_singleton.c\ -main/error_msg.c\ main/ft_putast.c\ main/main.c\ main/shell_init.c\ diff --git a/42sh/file b/42sh/file new file mode 100644 index 00000000..ce09b016 --- /dev/null +++ b/42sh/file @@ -0,0 +1 @@ +/Users/ariard/Projects/42sh diff --git a/42sh/includes/builtin.h b/42sh/includes/builtin.h index f7b728c4..cc50725e 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/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 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); int builtin_export(const char *path, char *const av[], char *const envp[]); int builtin_unset(const char *path, char *const av[], char *const envp[]); diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index eaeb4c37..417d9a2d 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/17 00:20:53 by ariard ### ########.fr */ +/* Updated: 2017/03/17 18:56:44 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_math.c b/42sh/src/builtin/builtin_math.c index f8eeda67..589b009a 100644 --- a/42sh/src/builtin/builtin_math.c +++ b/42sh/src/builtin/builtin_math.c @@ -1,97 +1,115 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* exec_math.c :+: :+: :+: */ +/* builtin_math.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/03/07 10:58:49 by ariard #+# #+# */ -/* Updated: 2017/03/16 16:40:27 by jhalford ### ########.fr */ +/* Created: 2017/03/17 18:54:00 by ariard #+# #+# */ +/* 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) */ -/* { */ -/* *var = NULL; */ -/* *value = NULL; */ -/* *operator = NULL; */ -/* *operand = NULL; */ -/* return (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" -/* static int get_value(char **var, char **value) */ -/* { */ -/* char *temp; */ +static int error_msg(char *msg) +{ + ft_dprintf(2, "{red}%s{eoc}", msg); + return (-1); +} -/* if (!word_is_assignment(var)) */ -/* 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); */ -/* } */ +static int init_math(char **var, char **value, char **operator, + char **operand) +{ + *var = NULL; + *value = NULL; + *operator = NULL; + *operand = NULL; + return (0); +} -/* static int do_math(char **value, char *operator, char *operand) */ -/* { */ -/* long ope1; */ -/* long ope2; */ +static int get_value(char *var, char **value) +{ + char *temp; + char *esc; + int ret; -/* 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); */ -/* } */ + 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); +} -/* int builtin_math(const char *path, char *const av[], char *const envp[]) */ -/* { */ -/* char *var; */ -/* char *value; */ -/* char *operator; */ -/* char *operand; */ +static int do_math(char **value, char *operator, char *operand) +{ + long ope1; + long ope2; -/* (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); */ -/* } */ + 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; + + (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/is_builtin.c b/42sh/src/builtin/is_builtin.c index 876727c7..42af9103 100644 --- a/42sh/src/builtin/is_builtin.c +++ b/42sh/src/builtin/is_builtin.c @@ -6,7 +6,7 @@ /* 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}, {"hash", &builtin_hash}, {"history", &builtin_history}, - /* {"math", &builtin_math}, */ + {"math", &builtin_math}, {NULL, NULL}, }; diff --git a/42sh/src/glob/is_char_esc.c b/42sh/src/glob/is_char_esc.c index edc80cb7..083556e5 100644 --- a/42sh/src/glob/is_char_esc.c +++ b/42sh/src/glob/is_char_esc.c @@ -6,7 +6,7 @@ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/get_key.c b/42sh/src/line-editing/get_key.c index c99659fc..0c94301d 100644 --- a/42sh/src/line-editing/get_key.c +++ b/42sh/src/line-editing/get_key.c @@ -6,7 +6,7 @@ /* 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) return (-1); DG("key value hex = %x", ret); - if (ft_completion(ret, str, pos)) - continue ; +// if (ft_completion(ret, str, pos)) +// continue ; while (g_key[j].value && g_key[j].value != ret) ++j; if (g_key[j].value && (ret = g_key[j].f(str, pos))) diff --git a/42sh/src/line-editing/readline.c b/42sh/src/line-editing/readline.c index 0b84ab3d..e9fb831c 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/17 16:10:40 by gwojda ### ########.fr */ +/* Updated: 2017/03/17 20:02:57 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/error_msg.c b/42sh/src/main/error_msg.c deleted file mode 100644 index d53f828a..00000000 --- a/42sh/src/main/error_msg.c +++ /dev/null @@ -1,19 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* error_msg.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* 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); -} diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 647f30ea..d9f8de6b 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -88,8 +88,12 @@ static int handle_instruction() { if ((ret = do_readline_routine(&stream)) > 0) return (ret); + sleep(10); + return (1); if (do_lexer_routine(stream) > 0) continue ; + sleep(10); + return (1); token_print(data->token); if (do_parser_routine() > 0) break ; diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index 4b52abca..95a4690e 100644 --- a/42sh/src/parser/add_cmd.c +++ b/42sh/src/parser/add_cmd.c @@ -6,7 +6,7 @@ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/build_tree.c b/42sh/src/parser/build_tree.c index ee0188a2..37a2c560 100644 --- a/42sh/src/parser/build_tree.c +++ b/42sh/src/parser/build_tree.c @@ -6,7 +6,7 @@ /* 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)) { - DG("build %s", read_state(g_treematch[i].type)); cache = token->type; return (g_treematch[i].add(ast, lst)); }