From 64694ed5bac7f73957bde1638739b915d2b17f8c Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Sat, 25 Mar 2017 00:56:48 +0100 Subject: [PATCH] builtin_cd has cliopts --- 42sh/includes/builtin.h | 3 +- 42sh/libft/includes/cliopts.h | 2 +- 42sh/libft/src/cliopts/cliopts_get.c | 7 +--- 42sh/src/builtin/builtin_cd.c | 62 ++++++++++++---------------- 42sh/src/builtin/builtin_export.c | 7 +++- 42sh/src/builtin/builtin_read.c | 2 +- 42sh/src/builtin/builtin_setenv.c | 2 +- 42sh/src/builtin/is_builtin.c | 2 +- 42sh/src/exec/redirect_great.c | 2 +- 42sh/src/job_control/builtin_jobs.c | 9 ++-- 10 files changed, 44 insertions(+), 54 deletions(-) diff --git a/42sh/includes/builtin.h b/42sh/includes/builtin.h index 289c7b09..fd7e5477 100644 --- a/42sh/includes/builtin.h +++ b/42sh/includes/builtin.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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) diff --git a/42sh/libft/includes/cliopts.h b/42sh/libft/includes/cliopts.h index a4eaff23..ad5687ab 100644 --- a/42sh/libft/includes/cliopts.h +++ b/42sh/libft/includes/cliopts.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/src/cliopts/cliopts_get.c b/42sh/libft/src/cliopts/cliopts_get.c index 0576d5df..12c317f1 100644 --- a/42sh/libft/src/cliopts/cliopts_get.c +++ b/42sh/libft/src/cliopts/cliopts_get.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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)) diff --git a/42sh/src/builtin/builtin_cd.c b/42sh/src/builtin/builtin_cd.c index 69228787..b0e75244 100644 --- a/42sh/src/builtin/builtin_cd.c +++ b/42sh/src/builtin/builtin_cd.c @@ -6,48 +6,44 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } diff --git a/42sh/src/builtin/builtin_export.c b/42sh/src/builtin/builtin_export.c index e2fa3f8d..6e7edb79 100644 --- a/42sh/src/builtin/builtin_export.c +++ b/42sh/src/builtin/builtin_export.c @@ -6,12 +6,15 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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; diff --git a/42sh/src/builtin/builtin_read.c b/42sh/src/builtin/builtin_read.c index c250a9b5..899b4f80 100644 --- a/42sh/src/builtin/builtin_read.c +++ b/42sh/src/builtin/builtin_read.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_setenv.c b/42sh/src/builtin/builtin_setenv.c index b360b052..3f79f8e2 100644 --- a/42sh/src/builtin/builtin_setenv.c +++ b/42sh/src/builtin/builtin_setenv.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/is_builtin.c b/42sh/src/builtin/is_builtin.c index dd931eba..a86f6cd6 100644 --- a/42sh/src/builtin/is_builtin.c +++ b/42sh/src/builtin/is_builtin.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/redirect_great.c b/42sh/src/exec/redirect_great.c index 8f4fe029..a7ae1f37 100644 --- a/42sh/src/exec/redirect_great.c +++ b/42sh/src/exec/redirect_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job_control/builtin_jobs.c b/42sh/src/job_control/builtin_jobs.c index a23b8184..7ef0e117 100644 --- a/42sh/src/job_control/builtin_jobs.c +++ b/42sh/src/job_control/builtin_jobs.c @@ -6,12 +6,14 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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))