norme builtin
This commit is contained in:
parent
5b5a4442e2
commit
3c39c485cb
5 changed files with 89 additions and 90 deletions
|
|
@ -3,10 +3,10 @@
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* bt_read_term.c :+: :+: :+: */
|
/* bt_read_term.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/01/25 16:02:05 by jhalford #+# #+# */
|
/* 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);
|
return (term);
|
||||||
}
|
}
|
||||||
|
|
||||||
int bt_read_terminit(t_read *data)
|
int bt_read_terminit(t_read *data)
|
||||||
{
|
{
|
||||||
struct termios term;
|
struct termios term;
|
||||||
|
|
||||||
|
|
@ -39,7 +39,7 @@ int bt_read_terminit(t_read *data)
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int bt_read_exit(t_read *data)
|
int bt_read_exit(t_read *data)
|
||||||
{
|
{
|
||||||
struct termios term;
|
struct termios term;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,13 @@
|
||||||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */
|
/* 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"
|
#include "minishell.h"
|
||||||
|
|
||||||
static int env_usage(int arg_miss, char c)
|
static int env_usage(int arg_miss, char c)
|
||||||
{
|
{
|
||||||
if (arg_miss)
|
if (arg_miss)
|
||||||
ft_dprintf(2, "{red}env: option requires an argument -- u{eoc}\n");
|
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);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void env_freeone(char **env, char *arg)
|
static void env_freeone(char **env, char *arg)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
char *tmp;
|
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;
|
char **arg_split;
|
||||||
|
|
||||||
|
|
@ -57,7 +57,7 @@ static void env_replace(char ***custom_env, char *arg)
|
||||||
*custom_env = ft_sstradd(*custom_env, 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))
|
while (*(++*arg))
|
||||||
{
|
{
|
||||||
|
|
@ -86,7 +86,8 @@ static int env_treat_flag(char ***custom_env, char *const *arg[])
|
||||||
return (0);
|
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;
|
char **env;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,13 @@
|
||||||
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/02/15 11:39:37 by gwojda #+# #+# */
|
/* 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"
|
#include "minishell.h"
|
||||||
|
|
||||||
static t_cliopts export_opts[] =
|
static t_cliopts g_export_opts[] =
|
||||||
{
|
{
|
||||||
{'p', NULL, BT_EXPORT_LP, 0, NULL},
|
{'p', NULL, BT_EXPORT_LP, 0, NULL},
|
||||||
{0, NULL, 0, 0, NULL},
|
{0, NULL, 0, 0, NULL},
|
||||||
|
|
@ -44,23 +44,20 @@ int builtin_export(
|
||||||
(void)envp;
|
(void)envp;
|
||||||
(void)path;
|
(void)path;
|
||||||
data.flag = 0;
|
data.flag = 0;
|
||||||
if (cliopts_get((char**)av, export_opts, &data))
|
if (cliopts_get((char**)av, g_export_opts, &data))
|
||||||
ft_perror();
|
ft_perror();
|
||||||
if (data.flag & BT_EXPORT_LP)
|
if (data.flag & BT_EXPORT_LP)
|
||||||
return (bt_export_print());
|
return (bt_export_print());
|
||||||
av = data.av_data;
|
av = data.av_data;
|
||||||
while (*av)
|
while (*av)
|
||||||
{
|
{
|
||||||
if ((equal = ft_strchr(*av, '=')))
|
equal = ft_strchr(*av, '=');
|
||||||
{
|
if (equal)
|
||||||
*equal = 0;
|
*equal = 0;
|
||||||
builtin_setenv("internal", (char*[]){"global", *av, equal + 1}, NULL);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
equal = ft_getenv(data_singleton()->local_var, *av);
|
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);
|
builtin_unsetenv("internal", (char*[]){"local", *av, NULL}, NULL);
|
||||||
av++;
|
av++;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,17 +6,17 @@
|
||||||
/* 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/17 19:59:42 by ariard ### ########.fr */
|
/* Updated: 2017/03/20 14:23:26 by gwojda ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
# define MATHERR_0 "math : invalid number of arguments\n"
|
#define MATHERR_0 "math : invalid number of arguments\n"
|
||||||
# define MATHERR_1 "math : invalid variable name\n"
|
#define MATHERR_1 "math : invalid variable name\n"
|
||||||
# define MATHERR_2 "math : invalid operator\n"
|
#define MATHERR_2 "math : invalid operator\n"
|
||||||
# define MATHERR_3 "math : invalid operand\n"
|
#define MATHERR_3 "math : invalid operand\n"
|
||||||
# define MATHERR_4 "math : division by 0\n"
|
#define MATHERR_4 "math : division by 0\n"
|
||||||
|
|
||||||
static int error_msg(char *msg)
|
static int error_msg(char *msg)
|
||||||
{
|
{
|
||||||
|
|
@ -38,78 +38,79 @@ static int get_value(char *var, char **value)
|
||||||
{
|
{
|
||||||
char *temp;
|
char *temp;
|
||||||
char *esc;
|
char *esc;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
esc = ft_strnew((ft_strlen(var) >> 3) + 1);
|
esc = ft_strnew((ft_strlen(var) >> 3) + 1);
|
||||||
ret = word_is_assignment((char *[]) {var, (esc + 1)});
|
ret = word_is_assignment((char *[]) {var, (esc + 1)});
|
||||||
ft_strdel(&esc);
|
ft_strdel(&esc);
|
||||||
if (!ret)
|
if (!ret)
|
||||||
return (error_msg(MATHERR_1));
|
return (error_msg(MATHERR_1));
|
||||||
temp = ft_sstrstr(data_singleton()->local_var, var);
|
temp = ft_sstrstr(data_singleton()->local_var, var);
|
||||||
if (temp)
|
if (temp)
|
||||||
{
|
{
|
||||||
temp += ft_strlenchr(temp, '=') + 1;
|
temp += ft_strlenchr(temp, '=') + 1;
|
||||||
*value = ft_strdup(temp);
|
*value = ft_strdup(temp);
|
||||||
if (!(ft_stris(*value, &ft_isdigit)))
|
if (!(ft_stris(*value, &ft_isdigit)))
|
||||||
{
|
{
|
||||||
ft_strdel(value);
|
ft_strdel(value);
|
||||||
*value = ft_itoa(0);
|
*value = ft_itoa(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
*value = ft_itoa(0);
|
*value = ft_itoa(0);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int do_math(char **value, char *operator, char *operand)
|
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)
|
if (operand)
|
||||||
ope2 = ft_atoi(operand);
|
ope2 = ft_atoi(operand);
|
||||||
else
|
else
|
||||||
ope2 = 0;
|
ope2 = 0;
|
||||||
if ((operator[0] == '/' || operator[0] == '%') && ope2 == 0)
|
if ((operator[0] == '/' || operator[0] == '%') && ope2 == 0)
|
||||||
return (error_msg(MATHERR_4));
|
return (error_msg(MATHERR_4));
|
||||||
else
|
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;
|
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);
|
ft_strdel(value);
|
||||||
*value = ft_itoa(ope1);
|
*value = ft_itoa(ope1);
|
||||||
return (0);
|
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 *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 (error_msg(MATHERR_0));
|
return (error_msg(MATHERR_0));
|
||||||
init_math(&var, &value, &operator, &operand);
|
init_math(&var, &value, &operator, &operand);
|
||||||
var = av[1];
|
var = av[1];
|
||||||
if (get_value(var, &value) == -1)
|
if (get_value(var, &value) == -1)
|
||||||
return (-1);
|
return (-1);
|
||||||
operator = av[2];
|
operator = av[2];
|
||||||
if (!(ft_strlen(operator) == 1 && (operator[0] == '+' || operator[0] == '-'
|
if (!(ft_strlen(operator) == 1 && (operator[0] == '+' || operator[0] == '-'
|
||||||
|| operator[0] == '/' || operator[0] == '*' || operator[0] == '%')))
|
|| operator[0] == '/' || operator[0] == '*' || operator[0] == '%')))
|
||||||
return (error_msg(MATHERR_2));
|
return (error_msg(MATHERR_2));
|
||||||
operand = av[3];
|
operand = av[3];
|
||||||
if (!ft_stris(operand, &ft_isdigit))
|
if (!ft_stris(operand, &ft_isdigit))
|
||||||
return (error_msg(MATHERR_3));
|
return (error_msg(MATHERR_3));
|
||||||
if (do_math(&value, operator, operand) == -1)
|
if (do_math(&value, operator, operand) == -1)
|
||||||
return (-1);
|
return (-1);
|
||||||
builtin_setenv("setenv", (char *[]){"local", var, value, 0}, data_singleton()->local_var);
|
builtin_setenv("setenv", (char *[]){"local", var, value, 0},
|
||||||
return (0);
|
data_singleton()->local_var);
|
||||||
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,10 @@
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* builtin_read.c :+: :+: :+: */
|
/* builtin_read.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */
|
/* 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->timeout = 0;
|
||||||
data->input = NULL;
|
data->input = NULL;
|
||||||
if ((cliopts_get(av, g_read_opts, data)))
|
if ((cliopts_get(av, g_read_opts, data)))
|
||||||
return(ft_perror() ? 2 : 2);
|
return (ft_perror() ? 2 : 2);
|
||||||
if (bt_read_terminit(data) < 0)
|
if (bt_read_terminit(data) < 0)
|
||||||
return (-1);
|
return (-1);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int bt_read_loop(t_read *data)
|
int bt_read_loop(t_read *data)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int esc;
|
int esc;
|
||||||
|
|
@ -74,7 +74,7 @@ int bt_read_loop(t_read *data)
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int bt_read_assign(t_read *data)
|
int bt_read_assign(t_read *data)
|
||||||
{
|
{
|
||||||
char *input;
|
char *input;
|
||||||
char **names;
|
char **names;
|
||||||
|
|
@ -97,7 +97,7 @@ int bt_read_assign(t_read *data)
|
||||||
return (0);
|
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;
|
t_read data;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue