fix env + fix cliopts
This commit is contained in:
parent
c42abf75db
commit
38caf98d79
11 changed files with 84 additions and 110 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/01/20 15:02:39 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/22 18:36:44 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/03/25 01:24:42 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -50,10 +50,10 @@ struct termios bt_read_term(int init);
|
|||
int bt_read_terminit(t_read *data);
|
||||
int bt_read_exit(t_read *data);
|
||||
|
||||
int bt_read_getdelim(char ***argv, t_read *data);
|
||||
int bt_read_getnchars(char ***argv, t_read *data);
|
||||
int bt_read_getprompt(char ***argv, t_read *data);
|
||||
int bt_read_gettimeout(char ***argv, t_read *data);
|
||||
int bt_read_getfd(char ***argv, t_read *data);
|
||||
int bt_read_getdelim(char *opt_arg, t_read *data);
|
||||
int bt_read_getnchars(char *opt_arg, t_read *data);
|
||||
int bt_read_getprompt(char *opt_arg, t_read *data);
|
||||
int bt_read_gettimeout(char *opt_argv, t_read *data);
|
||||
int bt_read_getfd(char *opt_arg, t_read *data);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/24 17:11:16 by wescande ### ########.fr */
|
||||
/* Updated: 2017/03/25 01:41:33 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -61,7 +61,7 @@ int shell_init(int ac, char **av, char **env);
|
|||
void shell_exit(void);
|
||||
int data_init(int ac, char **av, char **env);
|
||||
void data_exit(void);
|
||||
int get_c_arg(char ***av, t_data *data);
|
||||
int get_c_arg(char *opt_arg, t_data *data);
|
||||
|
||||
void shell_resetfds(void);
|
||||
void shell_resetsig(void);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/14 20:04:04 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/25 01:19:12 by wescande ### ########.fr */
|
||||
/* Updated: 2017/03/25 02:02:09 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -21,9 +21,13 @@ static char *check_required(char ***av, char *arg)
|
|||
{
|
||||
char *ret;
|
||||
|
||||
DG("%p, av", av);
|
||||
DG("%p, *av", *av);
|
||||
DG("%p, arg", arg);
|
||||
DG("%s, *arg", arg);
|
||||
if (!av || !*av)
|
||||
return (NULL);
|
||||
if (!arg || !*arg)
|
||||
if (!arg || !*arg || !*(arg + 1))
|
||||
return (*++(*av));
|
||||
ret = arg + 1;
|
||||
return (ret);
|
||||
|
|
@ -72,8 +76,8 @@ static int cliopts_parse_short(
|
|||
tmp = tmp ? tmp : **av;
|
||||
if ((map->get)(tmp, data))
|
||||
return (ERR_SET(E_CO_MISS, *arg));
|
||||
if (map->arg_required && !(*(arg += ft_strlen(arg))))
|
||||
++(*av);
|
||||
if (map->arg_required)
|
||||
break ;
|
||||
}
|
||||
((t_data_template*)data)->flag |= map->flag_on;
|
||||
((t_data_template*)data)->flag &= ~map->flag_off;
|
||||
|
|
@ -100,7 +104,6 @@ static int cliopts_parse_long(
|
|||
return (ERR_SET(E_CO_MISS, *arg));
|
||||
if ((map->get)(tmp, data))
|
||||
return (ERR_SET(E_CO_MISSL, arg));
|
||||
++(*av);
|
||||
}
|
||||
++(*av);
|
||||
return (0);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/11/03 18:04:07 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/23 18:31:06 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/03/25 01:38:51 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -17,10 +17,10 @@ void ft_sstrdel(char **sstr, int index)
|
|||
int i;
|
||||
|
||||
i = index;
|
||||
free(sstr[index]);
|
||||
while (sstr[i])
|
||||
ft_strdel(&sstr[index]);
|
||||
while (i == index || sstr[i])
|
||||
{
|
||||
sstr[i] = sstr[i + 1];
|
||||
i++;
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,53 +6,53 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/01/21 18:00:03 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/21 15:52:26 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/03/25 01:27:06 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
int bt_read_getdelim(char ***av, t_read *data)
|
||||
int bt_read_getdelim(char *opt_arg, t_read *data)
|
||||
{
|
||||
if (!av || !*av)
|
||||
if (!opt_arg)
|
||||
return (1);
|
||||
if (data)
|
||||
data->delim = ***av;
|
||||
data->delim = *opt_arg;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int bt_read_getnchars(char ***av, t_read *data)
|
||||
int bt_read_getnchars(char *opt_arg, t_read *data)
|
||||
{
|
||||
if (!av || !*av)
|
||||
if (!opt_arg)
|
||||
return (1);
|
||||
if (data)
|
||||
data->nchars = ft_atoi(**av);
|
||||
data->nchars = ft_atoi(opt_arg);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int bt_read_getprompt(char ***av, t_read *data)
|
||||
int bt_read_getprompt(char *opt_arg, t_read *data)
|
||||
{
|
||||
if (!av || !*av || !**av)
|
||||
if (!opt_arg)
|
||||
return (1);
|
||||
if (data)
|
||||
data->prompt = **av;
|
||||
data->prompt = opt_arg;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int bt_read_gettimeout(char ***av, t_read *data)
|
||||
int bt_read_gettimeout(char *opt_arg, t_read *data)
|
||||
{
|
||||
if (!av || !*av)
|
||||
if (!opt_arg)
|
||||
return (1);
|
||||
if (data)
|
||||
data->timeout = ft_atoi(**av);
|
||||
data->timeout = ft_atoi(opt_arg);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int bt_read_getfd(char ***av, t_read *data)
|
||||
int bt_read_getfd(char *opt_arg, t_read *data)
|
||||
{
|
||||
if (!av || !*av)
|
||||
if (!opt_arg)
|
||||
return (1);
|
||||
if (data)
|
||||
data->fd = ft_atoi(**av);
|
||||
data->fd = ft_atoi(opt_arg);
|
||||
return (0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/12/03 11:57:53 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/25 00:52:32 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/03/25 01:20:39 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -21,9 +21,9 @@
|
|||
|
||||
static t_cliopts g_cdopts[] =
|
||||
{
|
||||
{'P', NULL, BT_CD_P, BT_CD_L, NULL},
|
||||
{'L', NULL, BT_CD_L, BT_CD_P, NULL},
|
||||
{0, NULL, 0, 0, NULL},
|
||||
{'P', NULL, BT_CD_P, BT_CD_L, NULL, 0},
|
||||
{'L', NULL, BT_CD_L, BT_CD_P, NULL, 0},
|
||||
{0, NULL, 0, 0, NULL, 0},
|
||||
};
|
||||
|
||||
static char *bt_cd_target(char *arg)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/22 16:20:31 by gwojda #+# #+# */
|
||||
/* Updated: 2017/03/25 01:19:51 by wescande ### ########.fr */
|
||||
/* Updated: 2017/03/25 02:10:38 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -15,14 +15,14 @@
|
|||
#define ENV_USAGE "env [-i] [name=value]... [utility [argument...]]"
|
||||
#define ENV_NOFILE "env: %s: No such file or directory"
|
||||
#define ENV_NOPERM "env: %s: Permission denied"
|
||||
# define BT_ENV_LI (1 << 0)
|
||||
# define BT_ENV_LU (1 << 1)
|
||||
/* # define BT_ENV_LI (1 << 0) */
|
||||
/* # define BT_ENV_LU (1 << 1) */
|
||||
|
||||
static t_cliopts g_env_opts[] =
|
||||
{
|
||||
{'i', NULL, 0, 0, &bt_env_opt_i, 0},
|
||||
{'u', NULL, 0, 0, &bt_env_opt_u, 1},
|
||||
{0, 0, 0, 0, 0},
|
||||
{0, 0, 0, 0, 0, 0},
|
||||
};
|
||||
|
||||
int bt_env_opt_i(char *opt_arg, t_env_data *data)
|
||||
|
|
@ -32,53 +32,20 @@ int bt_env_opt_i(char *opt_arg, t_env_data *data)
|
|||
return (0);
|
||||
}
|
||||
|
||||
static void env_freeone(char **env, char *arg)
|
||||
{
|
||||
int i;
|
||||
char *tmp;
|
||||
|
||||
while (env && *env && (i = -1))
|
||||
{
|
||||
if (ft_strcmp(*env, arg) == '='
|
||||
&& ft_strlen(arg) == ft_strlenchr(*env, '='))
|
||||
{
|
||||
tmp = *env;
|
||||
while (*env)
|
||||
{
|
||||
*env = *(env + 1);
|
||||
++env;
|
||||
}
|
||||
ft_strdel(&tmp);
|
||||
return ;
|
||||
}
|
||||
++env;
|
||||
}
|
||||
}
|
||||
|
||||
int bt_env_opt_u(char *opt_arg, t_env_data *data)
|
||||
{
|
||||
DG();
|
||||
int i;
|
||||
char *tmp;
|
||||
char **env;
|
||||
char **tmp_env;
|
||||
|
||||
env = data->custom_env;
|
||||
while (env && *env && (i = -1))
|
||||
if (!(env = data->custom_env))
|
||||
return (0);
|
||||
i = -1;
|
||||
while (env[++i])
|
||||
{
|
||||
if (ft_strcmp(*env, opt_arg) == '='
|
||||
&& ft_strlen(opt_arg) == ft_strlenchr(*env, '='))
|
||||
{
|
||||
tmp = *env;
|
||||
tmp_env = env + 1;
|
||||
while (*env)
|
||||
{
|
||||
*env = *(env + 1);
|
||||
++env;
|
||||
}
|
||||
env = tmp_env;
|
||||
ft_strdel(&tmp);
|
||||
}
|
||||
++env;
|
||||
if (ft_strcmp(env[i], opt_arg) == '='
|
||||
&& ft_strlen(opt_arg) == ft_strlenchr(env[i], '='))
|
||||
ft_sstrdel(env, i);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -103,9 +70,16 @@ static int bt_env_parse(t_env_data *data, char **av)
|
|||
if (cliopts_get(av, g_env_opts, data))
|
||||
return (1);
|
||||
bt_env_getcustom(&data->av_data, data);
|
||||
if (!(data->flag & BT_ENV_LI))
|
||||
return (0);
|
||||
}
|
||||
|
||||
int display_env(char **av)
|
||||
{
|
||||
if (av && *av)
|
||||
{
|
||||
data->custom_env = ft_sstrmerge(data_singleton()->env, data->custom_env);
|
||||
ft_sstrprint(av, '\n');
|
||||
ft_putchar('\n');
|
||||
ft_tabdel(&av);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -121,10 +95,7 @@ int builtin_env(const char *path,
|
|||
if (bt_env_parse(&dat, (char**)argv))
|
||||
return (ft_perror("env") && SH_ERR("usage: %s", ENV_USAGE));
|
||||
if (!*dat.av_data)
|
||||
{
|
||||
ft_sstrprint(dat.custom_env, '\n');
|
||||
return (ft_putchar('\n') * 0);
|
||||
}
|
||||
return (display_env(dat.custom_env));
|
||||
else if ((pid = fork()) == 0)
|
||||
{
|
||||
if (!(path = ft_strchr(dat.av_data[0], '/') ? ft_strdup(dat.av_data[0])
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/02/15 11:39:37 by gwojda #+# #+# */
|
||||
/* Updated: 2017/03/25 00:57:26 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/03/25 01:39:34 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -17,8 +17,8 @@
|
|||
|
||||
static t_cliopts g_export_opts[] =
|
||||
{
|
||||
{'p', NULL, BT_EXPORT_LP, 0, NULL},
|
||||
{0, NULL, 0, 0, NULL},
|
||||
{'p', NULL, BT_EXPORT_LP, 0, NULL, 0},
|
||||
{0, NULL, 0, 0, NULL, 0},
|
||||
};
|
||||
|
||||
int bt_export_print(void)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/24 23:30:45 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/03/25 01:28:33 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -17,14 +17,14 @@
|
|||
|
||||
t_cliopts g_read_opts[] =
|
||||
{
|
||||
{'d', NULL, BT_READ_LD, 0, bt_read_getdelim},
|
||||
{'n', NULL, BT_READ_LN, 0, bt_read_getnchars},
|
||||
{'p', NULL, BT_READ_LP, 0, bt_read_getprompt},
|
||||
{'r', NULL, BT_READ_LR, 0, NULL},
|
||||
{'s', NULL, BT_READ_LS, 0, NULL},
|
||||
{'t', NULL, BT_READ_LT, 0, bt_read_gettimeout},
|
||||
{'u', NULL, BT_READ_LU, 0, bt_read_getfd},
|
||||
{0, 0, 0, 0, 0},
|
||||
{'d', NULL, BT_READ_LD, 0, bt_read_getdelim, 1},
|
||||
{'n', NULL, BT_READ_LN, 0, bt_read_getnchars, 1},
|
||||
{'p', NULL, BT_READ_LP, 0, bt_read_getprompt, 1},
|
||||
{'r', NULL, BT_READ_LR, 0, NULL, 0},
|
||||
{'s', NULL, BT_READ_LS, 0, NULL, 0},
|
||||
{'t', NULL, BT_READ_LT, 0, bt_read_gettimeout, 1},
|
||||
{'u', NULL, BT_READ_LU, 0, bt_read_getfd, 1},
|
||||
{0, 0, 0, 0, 0, 0},
|
||||
};
|
||||
|
||||
int bt_read_init(t_read *data, char **av)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/12/15 17:43:01 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/24 23:33:03 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/03/25 01:39:55 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -16,8 +16,8 @@
|
|||
|
||||
t_cliopts g_jobs_opts[] =
|
||||
{
|
||||
{'l', NULL, JOBS_OPT_L, 0, NULL},
|
||||
{0, 0, 0, 0, 0},
|
||||
{'l', NULL, JOBS_OPT_L, 0, NULL, 0},
|
||||
{0, 0, 0, 0, 0, 0},
|
||||
};
|
||||
|
||||
static void bt_jobs_all(int opts)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/24 18:57:17 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/03/25 01:41:36 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -15,17 +15,17 @@
|
|||
|
||||
static t_cliopts g_opts[] =
|
||||
{
|
||||
{'c', NULL, SH_OPTS_LC, SH_OPTS_JOBC | SH_INTERACTIVE, get_c_arg},
|
||||
{-1, "no-jobcontrol", 0, SH_OPTS_JOBC, NULL},
|
||||
{0, 0, 0, 0, 0},
|
||||
{'c', NULL, SH_OPTS_LC, SH_OPTS_JOBC | SH_INTERACTIVE, get_c_arg, 1},
|
||||
{-1, "no-jobcontrol", 0, SH_OPTS_JOBC, NULL, 0},
|
||||
{0, 0, 0, 0, 0, 0},
|
||||
};
|
||||
|
||||
int get_c_arg(char ***av, t_data *data)
|
||||
int get_c_arg(char *opt_arg, t_data *data)
|
||||
{
|
||||
if (!av || !*av)
|
||||
if (!opt_arg)
|
||||
return (1);
|
||||
if (data)
|
||||
data->c_arg = **av;
|
||||
data->c_arg = opt_arg;
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue