variable assignement protection
This commit is contained in:
parent
cd5fefa671
commit
20b9b8f3a6
16 changed files with 54 additions and 102 deletions
|
|
@ -13,7 +13,7 @@
|
||||||
NAME = 42sh
|
NAME = 42sh
|
||||||
|
|
||||||
CC = gcc
|
CC = gcc
|
||||||
FLAGS = -Wall -Wextra -Werror #-fvisibility=hidden# -fsanitize=address
|
FLAGS = -Wall -Wextra -Werror -fvisibility=hidden -fsanitize=address
|
||||||
D_FLAGS = -g
|
D_FLAGS = -g
|
||||||
|
|
||||||
DELTA = $$(echo "$$(tput cols)-47"|bc)
|
DELTA = $$(echo "$$(tput cols)-47"|bc)
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/03/22 17:22:51 by ariard #+# #+# */
|
/* 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/12/05 11:53:36 by jhalford #+# #+# */
|
/* 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/03/07 11:29:54 by ariard #+# #+# */
|
/* 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/08/03 16:17:21 by jhalford #+# #+# */
|
/* 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/03/07 11:01:15 by ariard #+# #+# */
|
/* 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: ariard <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: ariard <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/12/01 16:10:54 by ariard #+# #+# */
|
/* 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,103 +6,60 @@
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/03/17 18:54:00 by ariard #+# #+# */
|
/* 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"
|
#include "minishell.h"
|
||||||
|
|
||||||
#define MATHERR_0 "math : invalid number of arguments"
|
#define MATHERR_0 "usage: math variable operator(+-/*%) operand"
|
||||||
#define MATHERR_1 "math : invalid variable name"
|
#define MATHERR_2 "math: %c: invalid operator"
|
||||||
#define MATHERR_2 "math : invalid operator"
|
#define MATHERR_3 "math: %s: operand must be digits only"
|
||||||
#define MATHERR_3 "math : invalid operand"
|
#define MATHERR_4 "math: division by 0"
|
||||||
#define MATHERR_4 "math : division by 0"
|
#define MATHERR_5 "math: modulo by 0"
|
||||||
|
|
||||||
static int init_math(char **var, char **value, char **operator,
|
static char *do_math(char *value, char operator, char *operand)
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
long ope1;
|
long ope1;
|
||||||
long ope2;
|
long ope2;
|
||||||
|
|
||||||
ope1 = ft_atoi(*value);
|
ope1 = ft_atoi(value);
|
||||||
if (operand)
|
ope2 = ft_atoi(operand);
|
||||||
ope2 = ft_atoi(operand);
|
DG("value %s -> %i", value, ope1);
|
||||||
else
|
DG("operand %s -> %i", operand, ope2);
|
||||||
ope2 = 0;
|
if ((operator == '/') && ope2 == 0)
|
||||||
if ((operator[0] == '/' || operator[0] == '%') && ope2 == 0)
|
return (SH_ERR(MATHERR_4) ? NULL : NULL);
|
||||||
return (SH_ERR(MATHERR_4));
|
if ((operator == '%') && ope2 == 0)
|
||||||
else
|
return (SH_ERR(MATHERR_5) ? NULL : NULL);
|
||||||
{
|
ope1 = (operator == '+') ? ope1 + ope2 : ope1;
|
||||||
ope1 = (operator[0] == '+') ? ope1 + ope2 : ope1;
|
ope1 = (operator == '-') ? ope1 - ope2 : ope1;
|
||||||
ope1 = (operator[0] == '-') ? ope1 - ope2 : ope1;
|
ope1 = (operator == '/') ? ope1 / ope2 : ope1;
|
||||||
ope1 = (operator[0] == '/') ? ope1 / ope2 : ope1;
|
ope1 = (operator == '*') ? ope1 * ope2 : ope1;
|
||||||
ope1 = (operator[0] == '*') ? ope1 * ope2 : ope1;
|
ope1 = (operator == '%') ? ope1 % ope2 : ope1;
|
||||||
ope1 = (operator[0] == '%') ? ope1 % ope2 : ope1;
|
DG("output=%s (%i)", ft_itoa(ope1), ope1);
|
||||||
}
|
return (ft_itoa(ope1));
|
||||||
ft_strdel(value);
|
|
||||||
*value = ft_itoa(ope1);
|
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int builtin_math(const char *path, char *const av[], char *const envp[])
|
int builtin_math(const char *path, char *const av[], char *const envp[])
|
||||||
{
|
{
|
||||||
char *var;
|
|
||||||
char *value;
|
char *value;
|
||||||
char *operator;
|
char operator;
|
||||||
char *operand;
|
char *operand;
|
||||||
|
|
||||||
(void)path;
|
(void)path;
|
||||||
(void)envp;
|
(void)envp;
|
||||||
if (!av || !av[1] || !av[2] || !av[3] || av[4])
|
if (!av || !av[1] || !av[2] || !av[3] || av[4])
|
||||||
return (SH_ERR(MATHERR_0));
|
return (SH_ERR(MATHERR_0));
|
||||||
init_math(&var, &value, &operator, &operand);
|
value = ft_getenv(data_singleton()->local_var, av[1]);
|
||||||
var = av[1];
|
operator = av[2][0];
|
||||||
if (get_value(var, &value))
|
if (!(ft_strchr("+-/*%", operator)))
|
||||||
return (1);
|
return (SH_ERR(MATHERR_2, operator));
|
||||||
operator = av[2];
|
|
||||||
if (ft_strlen(operator) != 1 || !(ft_strchr("+-/*%", operator[0])))
|
|
||||||
return (SH_ERR(MATHERR_2));
|
|
||||||
operand = av[3];
|
operand = av[3];
|
||||||
if (!ft_stris(operand, &ft_isdigit))
|
if (!ft_stris(operand, &ft_isdigit))
|
||||||
return (SH_ERR(MATHERR_3));
|
return (SH_ERR(MATHERR_3, operand));
|
||||||
if (do_math(&value, operator, operand))
|
if (!(value = do_math(value, operator, operand)))
|
||||||
return (1);
|
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);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */
|
/* 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/12/05 11:50:51 by jhalford #+# #+# */
|
/* 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/03/07 15:47:30 by wescande #+# #+# */
|
/* 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,33 +6,28 @@
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/03/07 11:12:05 by ariard #+# #+# */
|
/* 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"
|
#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)
|
int exec_var(t_btree **ast)
|
||||||
{
|
{
|
||||||
t_astnode *node;
|
t_astnode *node;
|
||||||
char **av;
|
char **av;
|
||||||
char *var;
|
char *var;
|
||||||
char *value;
|
char *value;
|
||||||
|
char *equal;
|
||||||
|
|
||||||
node = (*ast)->item;
|
node = (*ast)->item;
|
||||||
av = token_to_argv(node->data.cmd.token, 1);
|
if (!(av = token_to_argv(node->data.cmd.token, 1)))
|
||||||
set_var(av[0], &var, &value);
|
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);
|
builtin_setenv("internal", (char*[]){"local", var, value, 0}, NULL);
|
||||||
ft_strdel(&var);
|
ft_sstrfree(av);
|
||||||
ft_strdel(&value);
|
|
||||||
ft_tabdel(&av);
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/03/08 03:38:36 by wescande #+# #+# */
|
/* 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/03/08 00:58:02 by wescande #+# #+# */
|
/* 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/03/08 02:37:04 by wescande #+# #+# */
|
/* 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/03/07 20:36:04 by wescande #+# #+# */
|
/* 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue