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_init.c\
main/data_singleton.c\
main/error_msg.c\
main/ft_putast.c\
main/main.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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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[]);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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},
};

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)))

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
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 ;

View file

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