gestion d erreurs math builtin

This commit is contained in:
Antoine Riard 2017-03-15 21:06:35 +01:00
parent a68ad0af01
commit 6401e1759f
5 changed files with 26 additions and 12 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 00:48:28 by ariard ### ########.fr */
/* Updated: 2017/03/15 20:37:38 by ariard ### ########.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[]);

@ -1 +1 @@
Subproject commit 938a355bac5f6d4c7941968d3ef0da0d984a9329
Subproject commit 89d3f6e67e707e42fca42cc89e3f39e5f840908a

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: 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 */
/* */
/* ************************************************************************** */