builtin_cd has cliopts
This commit is contained in:
parent
66ec5363ca
commit
64694ed5ba
10 changed files with 44 additions and 54 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/14 22:59:57 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/24 15:13:06 by wescande ### ########.fr */
|
||||
/* Updated: 2017/03/24 23:17:32 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -17,7 +17,6 @@
|
|||
# include "libft.h"
|
||||
# include "builtin_read.h"
|
||||
|
||||
# define BT_EXPORT_LP (1 << 0)
|
||||
|
||||
# define BT_ENV_LI (1 << 0)
|
||||
# define BT_ENV_LU (1 << 1)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/14 20:22:56 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/20 15:48:05 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/03/24 23:20:58 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/14 20:04:04 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/24 15:02:26 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/03/25 00:03:30 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -96,11 +96,8 @@ int cliopts_get(char **av, t_cliopts opt_map[], void *data)
|
|||
av++;
|
||||
while (av && *av)
|
||||
{
|
||||
if (ft_strcmp(*av, "--") == 0)
|
||||
{
|
||||
av++;
|
||||
if (ft_strcmp(*av, "-") == 0 || (ft_strcmp(*av, "--") == 0 && av++))
|
||||
break ;
|
||||
}
|
||||
else if ((*av)[0] == '-' && (*av)[1] == '-')
|
||||
{
|
||||
if (cliopts_parse_long(&av, opt_map, data))
|
||||
|
|
|
|||
|
|
@ -6,48 +6,44 @@
|
|||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/12/03 11:57:53 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/24 23:12:30 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/03/25 00:52:32 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
#define CD_OPT_L (1 << 0)
|
||||
#define CD_OPT_P (1 << 1)
|
||||
#define HAS_CDOPT_P(x) (x & CD_OPT_P)
|
||||
#define HAS_CDOPT_L(x) (x & CD_OPT_L)
|
||||
#define CDERR_1 "cd: no such file or directory: %s"
|
||||
#define CDERR_2 "cd: HOME not set"
|
||||
#define CDERR_3 "cd: too many arguments{eoc}"
|
||||
#define BT_CD_L (1 << 0)
|
||||
#define BT_CD_P (1 << 1)
|
||||
#define CD_USAGE "usage: cd [-L|-P] [dir]"
|
||||
#define CDERR_1 "cd: no such file or directory: %s"
|
||||
#define CDERR_2 "cd: %s not set"
|
||||
#define CDERR_3 "cd: too many arguments"
|
||||
|
||||
static t_cliopts g_cdopts[] =
|
||||
{
|
||||
{'P', NULL, CD_OPT_P, CD_OPT_L, NULL},
|
||||
{'L', NULL, CD_OPT_L, CD_OPT_P, NULL},
|
||||
{'P', NULL, BT_CD_P, BT_CD_L, NULL},
|
||||
{'L', NULL, BT_CD_L, BT_CD_P, NULL},
|
||||
{0, NULL, 0, 0, NULL},
|
||||
};
|
||||
|
||||
static char *builtin_cd_special(char *const av[], char *const env[])
|
||||
static char *bt_cd_target(char *arg)
|
||||
{
|
||||
char *target;
|
||||
|
||||
if (!*av)
|
||||
if (!arg)
|
||||
{
|
||||
if (!(target = ft_getenv((char**)env, "HOME")))
|
||||
{
|
||||
SH_ERR(CDERR_2);
|
||||
return (NULL);
|
||||
}
|
||||
if (!(target = ft_getenv(data_singleton()->env, "HOME")))
|
||||
SH_ERR(CDERR_2, "HOME");
|
||||
}
|
||||
else if (*av && *(av + 1))
|
||||
else if (ft_strcmp(arg, "-") == 0)
|
||||
{
|
||||
SH_ERR(CDERR_3);
|
||||
return (NULL);
|
||||
DG("doing -");
|
||||
if (!(target = ft_getenv(data_singleton()->env, "OLDPWD")))
|
||||
SH_ERR(CDERR_2, "OLDPWD");
|
||||
DG("found OLDPWD %s", target);
|
||||
}
|
||||
else if (ft_strcmp(*av, "-") == 0)
|
||||
target = ft_strdup(ft_getenv((char**)env, "OLDPWD"));
|
||||
else
|
||||
target = *av;
|
||||
target = arg;
|
||||
return (target);
|
||||
}
|
||||
|
||||
|
|
@ -56,7 +52,7 @@ void setwd(char *var)
|
|||
char *cwd;
|
||||
|
||||
cwd = getcwd(NULL, 0);
|
||||
builtin_setenv(NULL, (char*[4]){"setenv", var, cwd, NULL}, NULL);
|
||||
builtin_setenv(NULL, (char*[]){"cd", var, cwd, NULL}, NULL);
|
||||
free(cwd);
|
||||
}
|
||||
|
||||
|
|
@ -66,24 +62,20 @@ int builtin_cd(const char *path,
|
|||
char *target;
|
||||
t_data_template data;
|
||||
|
||||
(void)envp;
|
||||
(void)path;
|
||||
data.flag = CD_OPT_L;
|
||||
(void)envp;
|
||||
data.flag = BT_CD_L;
|
||||
if (cliopts_get((char**)av, g_cdopts, &data))
|
||||
return (ft_perror("cd") && SH_ERR(CD_USAGE));
|
||||
if (data.av_data[0] && data.av_data[1])
|
||||
return (SH_ERR(CDERR_3) && SH_ERR(CD_USAGE));
|
||||
if (!(target = bt_cd_target(*data.av_data)))
|
||||
return (1);
|
||||
/* i = builtin_cd_opts(av, &opts); */
|
||||
setwd("OLDPWD");
|
||||
if (!(target = builtin_cd_special(data.av_data, envp)))
|
||||
return (1);
|
||||
if (chdir(target))
|
||||
{
|
||||
SH_ERR(CDERR_1, target);
|
||||
return (1);
|
||||
}
|
||||
return (SH_ERR(CDERR_1, target));
|
||||
else if (target != *data.av_data)
|
||||
ft_printf("%s\n", target);
|
||||
setwd("PWD");
|
||||
if (!ft_strcmp(*data.av_data, "-"))
|
||||
free(target);
|
||||
return (0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,12 +6,15 @@
|
|||
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/02/15 11:39:37 by gwojda #+# #+# */
|
||||
/* Updated: 2017/03/24 19:33:00 by wescande ### ########.fr */
|
||||
/* Updated: 2017/03/24 23:18:44 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
#define BT_EXPORT_LP (1 << 0)
|
||||
#define READ_USAGE "usage: export [name[=value] ...] or export -p"
|
||||
|
||||
static t_cliopts g_export_opts[] =
|
||||
{
|
||||
{'p', NULL, BT_EXPORT_LP, 0, NULL},
|
||||
|
|
@ -44,7 +47,7 @@ int builtin_export(
|
|||
(void)path;
|
||||
data.flag = 0;
|
||||
if (cliopts_get((char**)av, g_export_opts, &data))
|
||||
return (ft_perror("export") ? 1 : 1);
|
||||
return (ft_perror("export") && SH_ERR(READ_USAGE));
|
||||
if (data.flag & BT_EXPORT_LP)
|
||||
return (bt_export_print());
|
||||
av = data.av_data;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/24 16:23:51 by gwojda ### ########.fr */
|
||||
/* Updated: 2017/03/24 23:30:45 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/24 23:12:59 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/03/24 23:14:52 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/12/13 13:09:57 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/24 15:12:42 by wescande ### ########.fr */
|
||||
/* Updated: 2017/03/24 23:29:53 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/02/06 22:03:53 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/24 20:09:09 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/03/25 00:16:53 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,12 +6,14 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/12/15 17:43:01 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/24 18:48:46 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/03/24 23:33:03 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
#define JOBS_USAGE "usage: jobs [-l] [id ...]"
|
||||
|
||||
t_cliopts g_jobs_opts[] =
|
||||
{
|
||||
{'l', NULL, JOBS_OPT_L, 0, NULL},
|
||||
|
|
@ -67,14 +69,11 @@ int builtin_jobs(const char *path, char *const av[], char *const envp[])
|
|||
(void)path;
|
||||
(void)envp;
|
||||
if (!SH_HAS_JOBC(data_singleton()->opts))
|
||||
{
|
||||
DG("no job control :(");
|
||||
return (SH_ERR("jobs: %s", SH_MSG_NOJOBC));
|
||||
}
|
||||
do_job_notification();
|
||||
ft_bzero(&data, sizeof(t_data_template));
|
||||
if (cliopts_get((char**)av, g_jobs_opts, &data))
|
||||
return (ft_perror("jobs"));
|
||||
return (ft_perror("jobs") && SH_ERR(JOBS_USAGE));
|
||||
if (!*data.av_data)
|
||||
bt_jobs_all(data.flag);
|
||||
else if (bt_jobs_spec(data.av_data, data.flag))
|
||||
|
|
|
|||
Loading…
Reference in a new issue