norme builtin

This commit is contained in:
gwojda 2017-03-20 14:39:20 +01:00
parent 5b5a4442e2
commit 3c39c485cb
5 changed files with 89 additions and 90 deletions

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* bt_read_term.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/25 16:02:05 by jhalford #+# #+# */
/* Updated: 2017/03/15 21:23:41 by jhalford ### ########.fr */
/* Updated: 2017/03/20 14:23:46 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,7 +21,7 @@ struct termios bt_read_term(int init)
return (term);
}
int bt_read_terminit(t_read *data)
int bt_read_terminit(t_read *data)
{
struct termios term;
@ -39,7 +39,7 @@ int bt_read_terminit(t_read *data)
return (0);
}
int bt_read_exit(t_read *data)
int bt_read_exit(t_read *data)
{
struct termios term;

View file

@ -6,13 +6,13 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */
/* Updated: 2017/03/19 17:43:24 by wescande ### ########.fr */
/* Updated: 2017/03/20 14:38:06 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int env_usage(int arg_miss, char c)
static int env_usage(int arg_miss, char c)
{
if (arg_miss)
ft_dprintf(2, "{red}env: option requires an argument -- u{eoc}\n");
@ -22,7 +22,7 @@ static int env_usage(int arg_miss, char c)
return (1);
}
static void env_freeone(char **env, char *arg)
static void env_freeone(char **env, char *arg)
{
int i;
char *tmp;
@ -45,7 +45,7 @@ static void env_freeone(char **env, char *arg)
}
}
static void env_replace(char ***custom_env, char *arg)
static void env_replace(char ***custom_env, char *arg)
{
char **arg_split;
@ -57,7 +57,7 @@ static void env_replace(char ***custom_env, char *arg)
*custom_env = ft_sstradd(*custom_env, arg);
}
static int env_treat_flag(char ***custom_env, char *const *arg[])
static int env_treat_flag(char ***custom_env, char *const *arg[])
{
while (*(++*arg))
{
@ -86,7 +86,8 @@ static int env_treat_flag(char ***custom_env, char *const *arg[])
return (0);
}
int builtin_env(const char *path, char *const argv[], char *const envp[])
int builtin_env(const char *path, char *const argv[],
char *const envp[])
{
char **env;

View file

@ -6,13 +6,13 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 11:39:37 by gwojda #+# #+# */
/* Updated: 2017/03/18 11:46:32 by gwojda ### ########.fr */
/* Updated: 2017/03/20 14:37:26 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static t_cliopts export_opts[] =
static t_cliopts g_export_opts[] =
{
{'p', NULL, BT_EXPORT_LP, 0, NULL},
{0, NULL, 0, 0, NULL},
@ -44,23 +44,20 @@ int builtin_export(
(void)envp;
(void)path;
data.flag = 0;
if (cliopts_get((char**)av, export_opts, &data))
if (cliopts_get((char**)av, g_export_opts, &data))
ft_perror();
if (data.flag & BT_EXPORT_LP)
return (bt_export_print());
av = data.av_data;
while (*av)
{
if ((equal = ft_strchr(*av, '=')))
{
equal = ft_strchr(*av, '=');
if (equal)
*equal = 0;
builtin_setenv("internal", (char*[]){"global", *av, equal + 1}, NULL);
}
else
{
equal = ft_getenv(data_singleton()->local_var, *av);
builtin_setenv("internal", (char*[]){"global", *av, equal}, NULL);
}
equal ? equal++ : equal;
builtin_setenv("internal", (char*[]){"global", *av, equal}, NULL);
builtin_unsetenv("internal", (char*[]){"local", *av, NULL}, NULL);
av++;
}

View file

@ -6,17 +6,17 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/17 18:54:00 by ariard #+# #+# */
/* Updated: 2017/03/17 19:59:42 by ariard ### ########.fr */
/* Updated: 2017/03/20 14:23:26 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
# 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"
#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 error_msg(char *msg)
{
@ -38,78 +38,79 @@ static int get_value(char *var, char **value)
{
char *temp;
char *esc;
int ret;
int ret;
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);
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 ope2;
long ope1;
long ope2;
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);
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;
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);
(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

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* builtin_read.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */
/* Updated: 2017/03/20 08:31:25 by jhalford ### ########.fr */
/* Updated: 2017/03/20 14:24:19 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
@ -40,13 +40,13 @@ int bt_read_init(t_read *data, char **av)
data->timeout = 0;
data->input = NULL;
if ((cliopts_get(av, g_read_opts, data)))
return(ft_perror() ? 2 : 2);
return (ft_perror() ? 2 : 2);
if (bt_read_terminit(data) < 0)
return (-1);
return (0);
}
int bt_read_loop(t_read *data)
int bt_read_loop(t_read *data)
{
int i;
int esc;
@ -74,7 +74,7 @@ int bt_read_loop(t_read *data)
return (0);
}
int bt_read_assign(t_read *data)
int bt_read_assign(t_read *data)
{
char *input;
char **names;
@ -97,7 +97,7 @@ int bt_read_assign(t_read *data)
return (0);
}
int builtin_read(const char *path, char *const av[], char *const envp[])
int builtin_read(const char *path, char *const av[], char *const envp[])
{
t_read data;
int ret;