From c6a23fb2ac754cd1a235fba552ebd6ab88620e95 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 14 Mar 2017 21:51:40 +0100 Subject: [PATCH] no more custom parsing, all is done w/ cliopts --- 42sh/Makefile | 1 - 42sh/includes/exec.h | 2 +- 42sh/includes/minishell.h | 10 ++-- 42sh/libft | 2 +- 42sh/src/builtin/builtin_export.c | 12 +++-- 42sh/src/builtin/builtin_setenv.c | 3 +- 42sh/src/builtin/builtin_unsetenv.c | 2 +- 42sh/src/exec/plaunch_file.c | 4 +- 42sh/src/line-editing/readline.c | 2 +- 42sh/src/main/data_init.c | 6 ++- 42sh/src/main/main.c | 36 ++++++-------- 42sh/src/main/shell_get_opts.c | 61 ----------------------- 42sh/src/main/shell_init.c | 77 +++++++++++++++++++---------- 13 files changed, 88 insertions(+), 130 deletions(-) delete mode 100644 42sh/src/main/shell_get_opts.c diff --git a/42sh/Makefile b/42sh/Makefile index dbbdd149..5961a2e6 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -270,7 +270,6 @@ main/data_singleton.c\ main/ft_putast.c\ main/instruction_free.c\ main/main.c\ -main/shell_get_opts.c\ main/shell_init.c\ parser/add_bang.c\ parser/add_case.c\ diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 83104812..576a1c3f 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/14 20:34:31 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 21:17:31 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 9a445751..cf2d6d05 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/14 19:58:37 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 21:34:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,10 +34,10 @@ struct s_data { + t_flag opts; char **env; int argc; char **argv; - t_flag opts; t_line line; t_comp *comp; t_exec exec; @@ -47,11 +47,9 @@ struct s_data t_list *lst_func; }; -void shell_get_opts(int ac, char **av); -char *shell_get_avdata(); -void shell_init(int ac, char **av); +int shell_init(int ac, char **av); void shell_exit(void); -int data_init(void); +int data_init(int ac, char **av); void data_exit(void); int instruction_free(t_list **token, t_parser *parser, diff --git a/42sh/libft b/42sh/libft index 2713f7ab..938a355b 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit 2713f7ab295d895824b22ac39524f85a7e5b7985 +Subproject commit 938a355bac5f6d4c7941968d3ef0da0d984a9329 diff --git a/42sh/src/builtin/builtin_export.c b/42sh/src/builtin/builtin_export.c index 03bea7ec..9c4ab1ca 100644 --- a/42sh/src/builtin/builtin_export.c +++ b/42sh/src/builtin/builtin_export.c @@ -6,19 +6,19 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 11:39:37 by gwojda #+# #+# */ -/* Updated: 2017/03/14 21:10:37 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 21:39:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -t_cliopts opts[] = +static t_cliopts export_opts[] = { {'p', NULL, BT_EXPORT_LP, 0, NULL}, {0, NULL, 0, 0, NULL}, }; -int bt_export_print(void) +int bt_export_print(void) { char **env; char *equal; @@ -35,7 +35,8 @@ int bt_export_print(void) return (0); } -int builtin_export(const char *path, char *const av[], char *const envp[]) +int builtin_export( + const char *path, char *const av[], char *const envp[]) { char *equal; t_btexport data; @@ -44,7 +45,8 @@ int builtin_export(const char *path, char *const av[], char *const envp[]) i = 0; (void)envp; (void)path; - if (cliopts_get((char**)av, opts, &data)) + data.flag = 0; + if (cliopts_get((char**)av, export_opts, &data)) ft_perror(); if (data.flag & BT_EXPORT_LP) return (bt_export_print()); diff --git a/42sh/src/builtin/builtin_setenv.c b/42sh/src/builtin/builtin_setenv.c index c00b5e6d..eb2024e1 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/14 21:11:41 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 21:40:07 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,7 +31,6 @@ int builtin_setenv(const char *path, char *const av[], char *const envp[]) } else { - DG("str3join(%s,%s,%s)", av[0], "=", av[1]); str = ft_str3join(av[0], "=", av[1]); while ((*env) && (*env)[i]) { diff --git a/42sh/src/builtin/builtin_unsetenv.c b/42sh/src/builtin/builtin_unsetenv.c index 2081f600..a336c836 100644 --- a/42sh/src/builtin/builtin_unsetenv.c +++ b/42sh/src/builtin/builtin_unsetenv.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:29:17 by jhalford #+# #+# */ -/* Updated: 2017/03/14 21:08:12 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 21:13:35 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/plaunch_file.c b/42sh/src/exec/plaunch_file.c index 0634c483..4b466d72 100644 --- a/42sh/src/exec/plaunch_file.c +++ b/42sh/src/exec/plaunch_file.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 14:53:31 by jhalford #+# #+# */ -/* Updated: 2017/03/13 23:15:06 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 21:48:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,7 +40,7 @@ int plaunch_file(t_process *p) } else if (S_ISDIR(p->data.cmd.stat->st_mode)) { - ft_dprintf(2, "{red}%s: %s: Is a directory{eoc}\n", SHELL_NAME, p->data.cmd.av[0]); + ft_dprintf(2, "{red}%s: %s: is a directory{eoc}\n", SHELL_NAME, p->data.cmd.av[0]); exit(126); } else if (access(p->data.cmd.path, X_OK) == -1) diff --git a/42sh/src/line-editing/readline.c b/42sh/src/line-editing/readline.c index a30d7d38..17b476d9 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/13 14:43:28 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 21:34:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 9c590e85..a337be26 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/13 23:48:04 by ariard ### ########.fr */ +/* Updated: 2017/03/14 21:39:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,12 +14,14 @@ extern char **environ; -int data_init(void) +int data_init(int ac, char **av) { t_data *data; char *term_name; data = data_singleton(); + data->argc = ac; + data->argv = ft_sstrdup(av); data->env = ft_sstrdup(environ); data->local_var = NULL; set_exitstatus(0, 1); diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 83a6623e..9140d82b 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/03/14 21:11:58 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 21:50:56 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,13 +31,11 @@ int handle_instruction(int fd) if ((ret = readline(fd, get_lexer_stack(lexer) || parser.state == UNDEFINED || lexer.state == HEREDOC, &str))) { - DG("readline trap"); if (ret == -1) return (-1); return (parser.state == UNDEFINED ? error_eof(&token, &parser, &ast) : 1); } - DG("INPUT STRING IS [%s]", str); if (lexer.state == HEREDOC) { ft_strappend(&lexer.str, (char[]){'\n', 0}); @@ -62,7 +60,6 @@ int handle_instruction(int fd) if (parser.state == ERROR) error_syntax(&token, &parser, &ast); lexer.state = data_singleton()->heredoc_queue ? HEREDOC : 0; - /* DG("lexer.state=%i", lexer.state); */ if (lexer.state) continue; else if (parser.state == SUCCESS) @@ -86,10 +83,11 @@ int handle_instruction(int fd) int get_input_fd(char **av) { - t_data *data; - char *file; - int fds[2]; - int fd; + t_data *data; + char *file; + int fds[2]; + int fd; + struct stat buf; data = data_singleton(); if (SH_IS_INTERACTIVE(data->opts)) @@ -98,22 +96,23 @@ int get_input_fd(char **av) { pipe(fds); fd = fds[PIPE_READ]; - /* file = shell_get_avdata(); */ file = *cliopts_getdata(av); write(fds[PIPE_WRITE], file, ft_strlen(file)); close(fds[PIPE_WRITE]); fcntl(fd, F_SETFD, FD_CLOEXEC); return (fd); } - /* else if ((file = shell_get_avdata())) */ else if ((file = *cliopts_getdata(av))) { - if ((fd = open(file, O_RDONLY | O_CLOEXEC)) < 0) - return (-1); + stat(file, &buf); + fd = -1; + if (S_ISDIR(buf.st_mode)) + ft_printf("{red}%s: %s: is a directory\n{eoc}", g_argv[0], file); + else if ((fd = open(file, O_RDONLY | O_CLOEXEC)) < 0) + ft_printf("{red}%s: %s: No such file or directory\n{eoc}", g_argv[0], file); return (fd); } - else - return (STDIN); + return (STDIN); } int main(int ac, char **av) @@ -123,16 +122,13 @@ int main(int ac, char **av) g_argv = av; setlocale(LC_ALL, ""); DG("{inv}{bol}{gre}start of shell{eoc}"); - shell_init(ac, av); - if ((fd = get_input_fd(av)) < 0) - { - ft_printf("{red}%s: %s: No such file or directory\n{eoc}", SHELL_NAME, *cliopts_getdata(av)); + if (shell_init(ac, av)) + return (1); + if ((fd = get_input_fd(av)) < 0) return (1); - } DG("JOBC is %s, fd=[%i]", SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF", fd); while (handle_instruction(fd) == 0) ; - DG("gonna exit"); builtin_exit(NULL, NULL, NULL); return (0); } diff --git a/42sh/src/main/shell_get_opts.c b/42sh/src/main/shell_get_opts.c deleted file mode 100644 index dc75dddd..00000000 --- a/42sh/src/main/shell_get_opts.c +++ /dev/null @@ -1,61 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* shell_get_opts.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/01/11 14:04:48 by jhalford #+# #+# */ -/* Updated: 2017/03/06 16:52:34 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -static void shell_parse_long_opt(char *str) -{ - if (ft_strcmp("no-jobcontrol", str) == 0) - data_singleton()->opts &= ~SH_OPTS_JOBC; -} - -static void shell_parse_short_opt(char *str) -{ - int i; - - i = 0; - while (str[i]) - { - if (*str == 'c') - { - data_singleton()->opts |= SH_OPTS_LC; - data_singleton()->opts &= ~SH_OPTS_JOBC; - data_singleton()->opts &= ~SH_INTERACTIVE; - } - i++; - } -} - -void shell_get_opts(int ac, char **av) -{ - int i; - - i = 1; - if (isatty(STDIN) && !av[1]) - { - data_singleton()->opts |= SH_INTERACTIVE; - data_singleton()->opts |= SH_OPTS_JOBC; - } - while (i < ac && av[i][0] == '-') - { - if (ft_strcmp(av[i], "--") == 0) - { - i++; - break ; - } - if (av[i][1] == '-') - shell_parse_long_opt(av[i] + 2); - else - shell_parse_short_opt(av[i] + 1); - i++; - } -} diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index d198c59a..828efaf6 100644 --- a/42sh/src/main/shell_init.c +++ b/42sh/src/main/shell_init.c @@ -6,43 +6,66 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */ -/* Updated: 2017/03/14 21:10:26 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 21:50:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void shell_init(int ac, char **av) +static t_cliopts shell_opts[] = +{ + {'c', NULL, SH_OPTS_LC, SH_OPTS_JOBC | SH_INTERACTIVE, NULL}, + {-1, "no-jobcontrol", 0, SH_OPTS_JOBC, NULL}, + {0, 0, 0, 0, 0}, +}; + +static int interactive_settings(void) { int *shell_pgid; t_data *data; - DG("check"); - data_init(); data = data_singleton(); - data->argc = ac; - data->argv = ft_sstrdup(av); - shell_get_opts(ac, av); - if (SH_IS_INTERACTIVE(data->opts)) + DG("interactive shell settings"); + shell_pgid = &data->jobc.shell_pgid; + while (tcgetpgrp(STDIN) != (*shell_pgid = getpgrp())) + kill(-*shell_pgid, SIGTTIN); + signal(SIGINT, sigint_handler); + signal(SIGQUIT, SIG_IGN); + signal(SIGTSTP, sigtstp_handler); + signal(SIGTTIN, SIG_IGN); + signal(SIGTTOU, SIG_IGN); + signal(SIGCHLD, SIG_DFL); + signal(SIGWINCH, sigwinch_resize); + *shell_pgid = getpid(); + if (setpgid(*shell_pgid, *shell_pgid)) { - DG("interactive shell settings"); - shell_pgid = &data->jobc.shell_pgid; - while (tcgetpgrp(STDIN) != (*shell_pgid = getpgrp())) - kill(-*shell_pgid, SIGTTIN); - signal(SIGINT, sigint_handler); - signal(SIGQUIT, SIG_IGN); - signal(SIGTSTP, sigtstp_handler); - signal(SIGTTIN, SIG_IGN); - signal(SIGTTOU, SIG_IGN); - signal(SIGCHLD, SIG_DFL); - signal(SIGWINCH, sigwinch_resize); - *shell_pgid = getpid(); - if (setpgid(*shell_pgid, *shell_pgid)) - { - ft_dprintf(2, "Couldnt put the shell in it's own process group"); - exit (1); - } - tcsetpgrp(STDIN, *shell_pgid); - tcgetattr(STDIN, &data->jobc.shell_tmodes); + ft_dprintf(2, "Couldnt put the shell in it's own process group"); + exit (1); } + tcsetpgrp(STDIN, *shell_pgid); + tcgetattr(STDIN, &data->jobc.shell_tmodes); + return (0); +} + +int shell_init(int ac, char **av) +{ + t_data *data; + + data = data_singleton(); + data_init(ac, av); + if (isatty(STDIN) && !*cliopts_getdata(av)) + { + DG("interactive"); + data->opts |= SH_INTERACTIVE; + data->opts |= SH_OPTS_JOBC; + } + else + DG("non interactive"); + if (cliopts_get(av, shell_opts, data)) + return (ft_perror()); + if (SH_IS_INTERACTIVE(data->opts)) + interactive_settings(); + else + DG("not interactive"); + return (0); }