variable assignement protection

This commit is contained in:
Jack Halford 2017-03-22 23:23:58 +01:00
parent cd5fefa671
commit 20b9b8f3a6
16 changed files with 54 additions and 102 deletions

View file

@ -13,7 +13,7 @@
NAME = 42sh
CC = gcc
FLAGS = -Wall -Wextra -Werror #-fvisibility=hidden# -fsanitize=address
FLAGS = -Wall -Wextra -Werror -fvisibility=hidden -fsanitize=address
D_FLAGS = -g
DELTA = $$(echo "$$(tput cols)-47"|bc)

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/22 17:22:51 by ariard #+# #+# */
/* Updated: 2017/03/22 17:25:11 by ariard ### ########.fr */
/* Updated: 2017/03/22 21:50:33 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/05 11:53:36 by jhalford #+# #+# */
/* Updated: 2017/01/11 14:42:45 by jhalford ### ########.fr */
/* Updated: 2017/03/22 23:19:24 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 11:29:54 by ariard #+# #+# */
/* Updated: 2017/03/21 18:14:45 by ariard ### ########.fr */
/* Updated: 2017/03/22 21:54:40 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/08/03 16:17:21 by jhalford #+# #+# */
/* Updated: 2017/03/17 20:36:22 by wescande ### ########.fr */
/* Updated: 2017/03/22 22:21:21 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 11:01:15 by ariard #+# #+# */
/* Updated: 2017/03/21 15:43:51 by jhalford ### ########.fr */
/* Updated: 2017/03/22 23:12:37 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 16:10:54 by ariard #+# #+# */
/* Updated: 2017/03/07 11:30:02 by ariard ### ########.fr */
/* Updated: 2017/03/22 22:12:03 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,103 +6,60 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/17 18:54:00 by ariard #+# #+# */
/* Updated: 2017/03/21 18:08:31 by ariard ### ########.fr */
/* Updated: 2017/03/22 22:26:37 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#define MATHERR_0 "math : invalid number of arguments"
#define MATHERR_1 "math : invalid variable name"
#define MATHERR_2 "math : invalid operator"
#define MATHERR_3 "math : invalid operand"
#define MATHERR_0 "usage: math variable operator(+-/*%) operand"
#define MATHERR_2 "math: %c: invalid operator"
#define MATHERR_3 "math: %s: operand must be digits only"
#define MATHERR_4 "math: division by 0"
#define MATHERR_5 "math: modulo by 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 get_value(char *var, char **value)
{
char *temp;
char *esc;
int ret;
esc = ft_strnew((ft_strlen(var) >> 3) + 1);
ret = word_is_assignment((char *[]) {var, (esc + 1)});
ft_strdel(&esc);
if (!ret)
return (SH_ERR(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 do_math(char **value, char *operator, char *operand)
static char *do_math(char *value, char operator, char *operand)
{
long ope1;
long ope2;
ope1 = ft_atoi(*value);
if (operand)
ope1 = ft_atoi(value);
ope2 = ft_atoi(operand);
else
ope2 = 0;
if ((operator[0] == '/' || operator[0] == '%') && ope2 == 0)
return (SH_ERR(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);
DG("value %s -> %i", value, ope1);
DG("operand %s -> %i", operand, ope2);
if ((operator == '/') && ope2 == 0)
return (SH_ERR(MATHERR_4) ? NULL : NULL);
if ((operator == '%') && ope2 == 0)
return (SH_ERR(MATHERR_5) ? NULL : NULL);
ope1 = (operator == '+') ? ope1 + ope2 : ope1;
ope1 = (operator == '-') ? ope1 - ope2 : ope1;
ope1 = (operator == '/') ? ope1 / ope2 : ope1;
ope1 = (operator == '*') ? ope1 * ope2 : ope1;
ope1 = (operator == '%') ? ope1 % ope2 : ope1;
DG("output=%s (%i)", ft_itoa(ope1), ope1);
return (ft_itoa(ope1));
}
int builtin_math(const char *path, char *const av[], char *const envp[])
{
char *var;
char *value;
char *operator;
char operator;
char *operand;
(void)path;
(void)envp;
if (!av || !av[1] || !av[2] || !av[3] || av[4])
return (SH_ERR(MATHERR_0));
init_math(&var, &value, &operator, &operand);
var = av[1];
if (get_value(var, &value))
return (1);
operator = av[2];
if (ft_strlen(operator) != 1 || !(ft_strchr("+-/*%", operator[0])))
return (SH_ERR(MATHERR_2));
value = ft_getenv(data_singleton()->local_var, av[1]);
operator = av[2][0];
if (!(ft_strchr("+-/*%", operator)))
return (SH_ERR(MATHERR_2, operator));
operand = av[3];
if (!ft_stris(operand, &ft_isdigit))
return (SH_ERR(MATHERR_3));
if (do_math(&value, operator, operand))
return (SH_ERR(MATHERR_3, operand));
if (!(value = do_math(value, operator, operand)))
return (1);
builtin_setenv("setenv", (char *[]){"local", var, value, 0}, NULL);
builtin_setenv("setenv", (char *[]){"math", av[1], value, 0}, NULL);
ft_strdel(&value);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */
/* Updated: 2017/03/22 16:21:49 by gwojda ### ########.fr */
/* Updated: 2017/03/22 22:08:20 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/05 11:50:51 by jhalford #+# #+# */
/* Updated: 2017/03/21 19:14:31 by ariard ### ########.fr */
/* Updated: 2017/03/22 21:50:11 by jhalford ### ########.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/22 18:27:03 by jhalford ### ########.fr */
/* Updated: 2017/03/22 21:48:12 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,33 +6,28 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 11:12:05 by ariard #+# #+# */
/* Updated: 2017/03/22 21:34:39 by ariard ### ########.fr */
/* Updated: 2017/03/22 23:23:44 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int set_var(char *stream, char **var, char **value)
{
*var = ft_strdupchr(stream, '=');
stream += ft_strlenchr(stream, '=') + 1;
*value = ft_strdup(stream);
return (0);
}
int exec_var(t_btree **ast)
{
t_astnode *node;
char **av;
char *var;
char *value;
char *equal;
node = (*ast)->item;
av = token_to_argv(node->data.cmd.token, 1);
set_var(av[0], &var, &value);
if (!(av = token_to_argv(node->data.cmd.token, 1)))
return (0);
var = av[0];
if ((equal = ft_strchr(av[0], '=')))
*equal = 0;
value = equal ? equal + 1 : NULL;
builtin_setenv("internal", (char*[]){"local", var, value, 0}, NULL);
ft_strdel(&var);
ft_strdel(&value);
ft_tabdel(&av);
ft_sstrfree(av);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/08 03:38:36 by wescande #+# #+# */
/* Updated: 2017/03/21 18:44:57 by ariard ### ########.fr */
/* Updated: 2017/03/22 21:50:06 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/08 00:58:02 by wescande #+# #+# */
/* Updated: 2017/03/20 16:29:37 by gwojda ### ########.fr */
/* Updated: 2017/03/22 23:17:54 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/08 02:37:04 by wescande #+# #+# */
/* Updated: 2017/03/20 15:50:03 by gwojda ### ########.fr */
/* Updated: 2017/03/22 21:48:34 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 20:36:04 by wescande #+# #+# */
/* Updated: 2017/03/20 15:59:05 by gwojda ### ########.fr */
/* Updated: 2017/03/22 21:48:03 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */