From f482f782547bf95d0b585bd228d7eb2bfb01e5a5 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 20 Mar 2017 09:56:58 +0100 Subject: [PATCH] data_init and get_fd refactoring --- 42sh/includes/exec.h | 5 +- 42sh/includes/minishell.h | 4 +- 42sh/libft/includes/ft_printf.h | 3 +- 42sh/libft/includes/libft.h | 2 +- 42sh/libft/includes/sys.h | 5 +- 42sh/libft/src/cliopts/cliopts_get.c | 2 +- 42sh/libft/src/ft_printf/ft_printf.c | 2 +- 42sh/libft/src/ft_printf/ft_printf_parse.c | 2 +- 42sh/libft/src/ft_printf/ft_transform.c | 15 ++++- 42sh/libft/src/str/ft_strdup.c | 2 +- 42sh/src/builtin/bt_read_get.c | 2 +- 42sh/src/builtin/builtin_read.c | 4 +- 42sh/src/builtin/builtin_setenv.c | 2 +- 42sh/src/exec/exec_reset.c | 2 +- 42sh/src/line_editing/readline.c | 4 +- 42sh/src/main/data_init.c | 47 ++++++++++---- 42sh/src/main/main.c | 2 +- 42sh/src/main/shell_init.c | 72 ++++++++++++++-------- 18 files changed, 117 insertions(+), 60 deletions(-) diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index d7a15af5..302c2a52 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ -/* Updated: 2017/03/16 20:24:46 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 09:46:31 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,9 +17,6 @@ # include "job_control.h" # include -# define PIPE_READ 0 -# define PIPE_WRITE 1 - # define PROCESS_COMPLETED (1 << 0) # define PROCESS_SUSPENDED (1 << 1) # define PROCESS_RUNNING (1 << 2) diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 20db1a21..e4687eca 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ -/* Updated: 2017/03/18 19:05:37 by ariard ### ########.fr */ +/* Updated: 2017/03/20 09:37:28 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,6 +42,7 @@ struct s_data { t_flag opts; char **av_data; + char *c_arg; int fd; char **env; int argc; @@ -60,6 +61,7 @@ int shell_init(int ac, char **av); void shell_exit(void); int data_init(int ac, char **av); void data_exit(void); +int get_c_arg(char ***av, t_data *data); void content_free(void *data, size_t content_size); diff --git a/42sh/libft/includes/ft_printf.h b/42sh/libft/includes/ft_printf.h index 4f6d169d..e9299538 100644 --- a/42sh/libft/includes/ft_printf.h +++ b/42sh/libft/includes/ft_printf.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:22:54 by jhalford #+# #+# */ -/* Updated: 2017/03/14 18:07:10 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 09:23:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,6 +42,7 @@ struct s_fmt char conversion; int valid; t_conv conv; + }; int ft_vdprintf(int fd, const char *format, va_list ap); diff --git a/42sh/libft/includes/libft.h b/42sh/libft/includes/libft.h index 167be558..e34e6c54 100644 --- a/42sh/libft/includes/libft.h +++ b/42sh/libft/includes/libft.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:49:04 by jhalford #+# #+# */ -/* Updated: 2017/03/18 19:08:44 by ariard ### ########.fr */ +/* Updated: 2017/03/20 09:37:38 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/includes/sys.h b/42sh/libft/includes/sys.h index 3d883032..9f63db23 100644 --- a/42sh/libft/includes/sys.h +++ b/42sh/libft/includes/sys.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 17:24:23 by jhalford #+# #+# */ -/* Updated: 2017/03/14 17:24:24 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 09:38:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,9 @@ # define STDERR STDERR_FILENO # define STDBUG 3 +# define PIPE_READ 0 +# define PIPE_WRITE 1 + # include # include diff --git a/42sh/libft/src/cliopts/cliopts_get.c b/42sh/libft/src/cliopts/cliopts_get.c index b9551f1d..1c693cfa 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/15 21:18:20 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 09:28:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/src/ft_printf/ft_printf.c b/42sh/libft/src/ft_printf/ft_printf.c index f392b79e..f0647e09 100644 --- a/42sh/libft/src/ft_printf/ft_printf.c +++ b/42sh/libft/src/ft_printf/ft_printf.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:33:27 by jhalford #+# #+# */ -/* Updated: 2017/03/17 21:33:37 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 09:22:14 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/src/ft_printf/ft_printf_parse.c b/42sh/libft/src/ft_printf/ft_printf_parse.c index 96108f0d..e051faea 100644 --- a/42sh/libft/src/ft_printf/ft_printf_parse.c +++ b/42sh/libft/src/ft_printf/ft_printf_parse.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:33:24 by jhalford #+# #+# */ -/* Updated: 2017/02/18 13:08:53 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 09:17:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/src/ft_printf/ft_transform.c b/42sh/libft/src/ft_printf/ft_transform.c index 6651e619..94bfc6ac 100644 --- a/42sh/libft/src/ft_printf/ft_transform.c +++ b/42sh/libft/src/ft_printf/ft_transform.c @@ -6,12 +6,25 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:33:32 by jhalford #+# #+# */ -/* Updated: 2016/12/13 11:19:29 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 09:23:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_printf.h" +enum e_conversion = +{ + CONV_D, + CONV_I, + CONV_U, + CONV_O, + CONV_x, + CONV_X, + CONV_B, + CONV_S, + CONV_C, +}; + char *ft_transform(t_fmt *fmt, va_list ap) { char *ret; diff --git a/42sh/libft/src/str/ft_strdup.c b/42sh/libft/src/str/ft_strdup.c index 429692cc..bb3ade4b 100644 --- a/42sh/libft/src/str/ft_strdup.c +++ b/42sh/libft/src/str/ft_strdup.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/07 14:25:41 by jhalford #+# #+# */ -/* Updated: 2017/03/07 14:27:58 by ariard ### ########.fr */ +/* Updated: 2017/03/20 08:27:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/bt_read_get.c b/42sh/src/builtin/bt_read_get.c index 27ff5f50..77360eaa 100644 --- a/42sh/src/builtin/bt_read_get.c +++ b/42sh/src/builtin/bt_read_get.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/21 18:00:03 by jhalford #+# #+# */ -/* Updated: 2017/03/15 21:15:40 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 08:41:18 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_read.c b/42sh/src/builtin/builtin_read.c index c500523a..5aac937f 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/20 08:08:55 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 08:31:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -109,7 +109,7 @@ int builtin_read(const char *path, char *const av[], char *const envp[]) ; else if ((ret = bt_read_loop(&data))) ; - else if (bt_read_assign(&data)) + else if (data.input && bt_read_assign(&data)) ret = 1; if (ret == -1) exit(1); diff --git a/42sh/src/builtin/builtin_setenv.c b/42sh/src/builtin/builtin_setenv.c index 049ed1b4..1dcf264a 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/15 16:29:51 by wescande ### ########.fr */ +/* Updated: 2017/03/20 08:24:10 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_reset.c b/42sh/src/exec/exec_reset.c index 8b45d70e..b83d6928 100644 --- a/42sh/src/exec/exec_reset.c +++ b/42sh/src/exec/exec_reset.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 14:31:42 by jhalford #+# #+# */ -/* Updated: 2017/03/16 16:55:19 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 08:51:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line_editing/readline.c b/42sh/src/line_editing/readline.c index 4a7ed5d7..73fe089a 100644 --- a/42sh/src/line_editing/readline.c +++ b/42sh/src/line_editing/readline.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */ -/* Updated: 2017/03/18 14:55:08 by gwojda ### ########.fr */ +/* Updated: 2017/03/20 09:46:30 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ int readline(int has_prompt, char **input) { int ret; - if (!has_prompt) + if (!has_prompt) data_singleton()->line.prompt_size = 1; if (!SH_IS_INTERACTIVE(data_singleton()->opts)) return ((ret = get_next_line(data_singleton()->fd, input)) >= 0 ? !ret : ret); diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index ea3b9d60..599454fd 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */ -/* Updated: 2017/03/19 16:49:29 by wescande ### ########.fr */ +/* Updated: 2017/03/20 09:55:33 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,26 +14,49 @@ extern char **environ; -int data_init(int ac, char **av) +static int localenv_init(void) +{ + t_data *data; + + data = data_singleton(); + data->local_var = NULL; + builtin_setenv(NULL, (char *[]){"local", "IFS", " \t\n", 0}, NULL); + builtin_setenv(NULL, (char *[]){"local", "PS2", " >", 0}, NULL); + return (0); +} + +static int shlvl_inc(void) +{ + t_data *data; + char *shlvl; + + data = data_singleton(); + if ((shlvl = ft_getenv(data->env, "SHLVL"))) + { + if (!(shlvl = ft_itoa(ft_atoi(shlvl) + 1))) + return (-1); + } + else if (!(shlvl = ft_strdup("1"))) + return (-1); + builtin_setenv(NULL, (char *[]){"setenv", "SHLVL", shlvl, 0}, NULL); + ft_strdel(&shlvl); + return (0); +} + +int data_init(int ac, char **av) { t_data *data; char *term_name; - char *shlvl; data = data_singleton(); data->argc = ac; data->argv = ft_sstrdup(av); data->env = ft_sstrdup(environ); - data->local_var = NULL; - builtin_setenv(NULL, (char *[]){"local", "IFS", "\n ", 0}, NULL); + data->c_arg = NULL; set_exitstatus(0, 1); - shlvl = ft_getenv(data->env, "SHLVL"); - if (shlvl) - shlvl = ft_itoa(ft_atoi(shlvl) + 1); - else - shlvl = ft_strdup("1"); - builtin_setenv(NULL, (char *[]){"setenv", "SHLVL", shlvl, 0}, NULL); - ft_strdel(&shlvl); + localenv_init(); + if (shlvl_inc()) + return (-1); data->comp = NULL; data->opts = SH_INTERACTIVE | SH_OPTS_JOBC; exec_reset(); diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 25eb52a5..cb58fdca 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -65,7 +65,7 @@ int main(int ac, char **av) g_argv = av; setlocale(LC_ALL, ""); DG("{inv}{bol}{gre}start of shell{eoc}"); - if (shell_init(ac, av)) + if (shell_init(ac, av) != 0) return (1); DG("JOBC is %s", SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF"); token = NULL; diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index f5bff666..11ac219f 100644 --- a/42sh/src/main/shell_init.c +++ b/42sh/src/main/shell_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */ -/* Updated: 2017/03/19 16:50:42 by wescande ### ########.fr */ +/* Updated: 2017/03/20 09:54:18 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,43 +14,47 @@ static t_cliopts g_opts[] = { - {'c', NULL, SH_OPTS_LC, SH_OPTS_JOBC | SH_INTERACTIVE, NULL}, + {'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}, }; -int get_input_fd(char **av) +int get_c_arg(char ***av, t_data *data) { - t_data *data; + if (!av || !*av) + return (1); + if (data) + data->c_arg = **av; + return (0); +} + +static int get_input_fd(t_data *data) +{ + static int fds[2] = {-1, STDIN}; char *file; - int fds[2]; - int fd; struct stat buf; - data = data_singleton(); - file = *data->av_data; - if (SH_IS_INTERACTIVE(data->opts)) - return (STDIN); - else if (data->opts & SH_OPTS_LC) + /* fds = (int[2]){-1, STDIN}; */ + if (data->opts & SH_OPTS_LC) { + DG("-c"); + file = data->c_arg; pipe(fds); write(fds[PIPE_WRITE], file, ft_strlen(file)); close(fds[PIPE_WRITE]); - dup2_close(fds[PIPE_READ], (fd = 10)); - fcntl(fd, F_SETFD, FD_CLOEXEC); - return (fd); } - else if (file && !stat(file, &buf)) + else if ((file = *data->av_data) && !stat(file, &buf)) { - fd = -1; if (S_ISDIR(buf.st_mode)) - ft_printf("{red}%s: %s: is a directory\n{eoc}", av[0], file); - else if ((fd = open(file, O_RDONLY | O_CLOEXEC)) < 0) - ft_printf("{red}%s: %s: No such file or directory\n{eoc}", av[0], file); - if (fd > 0 && !dup2_close(fd, 10) && (fd = 10)) - return (fd); + ft_printf("{red}%s: %s: is a directory\n{eoc}", data->argv[0], file); + else if ((fds[PIPE_READ] = open(file, O_RDONLY | O_CLOEXEC)) < 0) + ft_printf("{red}%s: %s: No such file or directory\n{eoc}", + data->argv[0], file); } - return (STDIN); + fds[PIPE_WRITE] = fds[PIPE_READ] != -1 ? + fcntl(fds[PIPE_READ], F_DUPFD_CLOEXEC, 10) : STDIN; + close(fds[PIPE_READ]); + return (fds[PIPE_WRITE]); } static int interactive_settings(void) @@ -73,25 +77,39 @@ static int interactive_settings(void) { ft_dprintf(2, "{red}Couldnt put the shell in it's own process group{eoc}\n"); - exit(1); + return (-1); } tcsetpgrp(STDIN, *shell_pgid); tcgetattr(STDIN, &data->jobc.shell_tmodes); return (0); } +static int usage(void) +{ + ft_dprintf(2, "usage: 42sh [-c command | [<]script] [--no-jobcontrol]\n"); + return (0); +} + int shell_init(int ac, char **av) { t_data *data; data = data_singleton(); - data_init(ac, av); + DG(); + if (data_init(ac, av) < 0) + return (-1); + DG(); if (cliopts_get(av, g_opts, data)) + { + usage(); return (ft_perror()); + } + DG(); if (!isatty(STDIN) || *data->av_data) data->opts &= ~(SH_INTERACTIVE | SH_OPTS_JOBC); - data->fd = get_input_fd(av); - if (SH_IS_INTERACTIVE(data->opts)) - interactive_settings(); + if ((data->fd = get_input_fd(data)) < 0) + return (-1); + if (SH_IS_INTERACTIVE(data->opts) && interactive_settings() < 0) + return (-1); return (0); }