From 30556cdba2e6d85ce96b634163a3c2f23d3dd5aa Mon Sep 17 00:00:00 2001 From: wescande Date: Tue, 14 Mar 2017 20:20:19 +0100 Subject: [PATCH 1/4] le retour des BQUOTE --- 42sh/src/glob/command_getoutput.c | 106 +++++++++------------------- 42sh/src/glob/expand_bquote.c | 22 +++--- 42sh/src/job-control/process_free.c | 5 +- 3 files changed, 50 insertions(+), 83 deletions(-) diff --git a/42sh/src/glob/command_getoutput.c b/42sh/src/glob/command_getoutput.c index 2588d318..bfce6064 100644 --- a/42sh/src/glob/command_getoutput.c +++ b/42sh/src/glob/command_getoutput.c @@ -3,93 +3,53 @@ /* ::: :::::::: */ /* command_getoutput.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/01/12 14:01:59 by jhalford #+# #+# */ -/* Updated: 2017/03/13 23:32:52 by jhalford ### ########.fr */ +/* Created: 2017/03/14 19:44:25 by wescande #+# #+# */ +/* Updated: 2017/03/14 19:44:37 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" #define BUF_SIZE 1024 -/*static char *manage_output(int *fds) - { - int ret; - char buf[BUF_SIZE + 1]; - char *output; - - output = NULL; - while ((ret = read(fds[PIPE_READ], buf, BUF_SIZE)) > 0) - { - buf[ret] = 0; - ft_strappend(&output, buf); - } - close(fds[PIPE_READ]); - return (output); - } - */ - -static t_btree *gen_t_btree(const char *command) +static char *manage_output(int *fds) { - t_btree *ast; - t_astnode item; - int len; - unsigned char *esc; - - ft_bzero(&item, sizeof(t_astnode)); - item.type = SUBSHELL; - ast = btree_create_node(&item, sizeof(t_astnode)); - ft_bzero(&item, sizeof(t_astnode)); - item.type = CMD; - len = ft_strlen(command); - esc = ft_memalloc((len >> 3) + 1); - ft_ld_pushfront(&item.data.cmd.token, gen_tab(command, esc, esc, 1)); - ast->right = btree_create_node(&item, sizeof(t_astnode)); - return (ast); -} - -char *command_getoutput(char *command) -{ - t_btree *ast; - char *output; - char buf[BUF_SIZE + 1]; - t_exec *exec; - t_process p; int ret; - int pid; + char buf[BUF_SIZE + 1]; + char *output; - return (NULL); output = NULL; - exec = &data_singleton()->exec; - DG("%p exec ?", exec); - DG("\033[38;5;46mbefore"); - ast = gen_t_btree(command); - DG(); - if (process_set(&p, ast)) - return (NULL); - DG(); - if (!(pid = plaunch_subshell(&p))) - return (NULL); - waitpid(pid, &ret, WUNTRACED); - DG(); -// exec_leaf(&ast); - /* - DG(); - set_process(&p, ast); - DG(); - launch_process(&p); - DG(); - close(p.fdout);*/ - DG(); - while ((ret = read(exec->fdin, buf, BUF_SIZE)) > 0) + close(fds[PIPE_WRITE]); + while ((ret = read(fds[PIPE_READ], buf, BUF_SIZE)) > 0) { buf[ret] = 0; ft_strappend(&output, buf); } - DG(); - close(exec->fdin); - DG("done with %s\033[0m", output); - + close(fds[PIPE_READ]); + return (output); +} + +char *command_getoutput(char *command) +{ + char *output; + int ret; + int pid; + int fds[2]; + + if (!command) + return (NULL); + pipe(fds); + if (!(pid = fork())) + { + close(fds[PIPE_READ]); + dup2_close(fds[PIPE_WRITE], STDOUT); + execle(data_singleton()->argv[0],data_singleton()->argv[0], + "-c", command, NULL, + data_singleton()->env); + exit(1); + } + waitpid(pid, &ret, WUNTRACED); + output = manage_output(fds); return (output); } diff --git a/42sh/src/glob/expand_bquote.c b/42sh/src/glob/expand_bquote.c index cc5dfcb4..8e4e8bf9 100644 --- a/42sh/src/glob/expand_bquote.c +++ b/42sh/src/glob/expand_bquote.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 17:47:53 by wescande #+# #+# */ -/* Updated: 2017/02/24 22:04:59 by ariard ### ########.fr */ +/* Updated: 2017/03/14 20:07:47 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -75,7 +75,7 @@ static char *get_output(char *command) int len; if (!(output = command_getoutput(command))) - return (ft_strnew(0)); + return (NULL);//ft_strnew(0)); len = ft_strlen(output); while (output[--len] == '\n') output[len] = '\0'; @@ -98,10 +98,8 @@ static int search_bquote(t_bquote *me) me->mid = ft_strsub(sta, 1, me->str - sta - 1); me->s1 = ft_strsub(CH(*me->wk)[0], 0, sta - CH(*me->wk)[0]); me->s2 = ft_strdup(me->str + 1); - content = get_output(me->mid); - DG(); - init_expand(me, content, is_char_esc(me->esc, CH(*me->wk)[0], sta)); - DG(); + if ((content = get_output(me->mid))) + init_expand(me, content, is_char_esc(me->esc, CH(*me->wk)[0], sta)); ft_strdel(&me->mid); ft_strdel(&me->s1); ft_strdel(&me->s2); @@ -113,6 +111,13 @@ static int search_bquote(t_bquote *me) return (0); } +void delete(t_ld **tmp, t_ld **src) +{ + if (*tmp == *src) + ft_ld_del(src, &ft_tabdel); + else + ft_ld_del(tmp, &ft_tabdel); +} void expand_bquote(t_glob *gl) { t_ld *tmp; @@ -133,8 +138,9 @@ void expand_bquote(t_glob *gl) me.str = CH(gl->m_pat)[0]; if ((tmp = gl->m_pat) && (do_it = search_bquote(&me)) == 1) - ft_ld_del(&tmp, &ft_tabdel); - if (!gl->m_pat->next) + delete(&tmp, &gl->m_pat); +// ft_ld_del(&tmp, &ft_tabdel); + if (!gl->m_pat || !gl->m_pat->next) break ; gl->m_pat = gl->m_pat->next; } diff --git a/42sh/src/job-control/process_free.c b/42sh/src/job-control/process_free.c index 928ac258..f7c364f3 100644 --- a/42sh/src/job-control/process_free.c +++ b/42sh/src/job-control/process_free.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 12:41:11 by jhalford #+# #+# */ -/* Updated: 2017/03/13 22:19:53 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 20:19:22 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,7 +36,8 @@ void process_free(void *content, size_t content_size) /* return ; */ /* if (g_freemap[p->type].f) */ /* (g_freemap[p->type].f)(p); */ - (p->map.free)(p); + if (p->map.free) + (p->map.free)(p); ft_lstdel(&p->redirs, redir_free); free(p); } From 6799dc9c27ee96f18bab62efb0120be99fc7d707 Mon Sep 17 00:00:00 2001 From: wescande Date: Tue, 14 Mar 2017 20:22:09 +0100 Subject: [PATCH 2/4] protection des prints --- 42sh/src/job-control/process_format.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/42sh/src/job-control/process_format.c b/42sh/src/job-control/process_format.c index 1dc3388c..32aa8272 100644 --- a/42sh/src/job-control/process_format.c +++ b/42sh/src/job-control/process_format.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 13:05:55 by jhalford #+# #+# */ -/* Updated: 2017/03/13 22:29:29 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 20:21:46 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,7 +42,8 @@ static void process_format_com_long(t_list **plist) t_process *p; p = (*plist)->content; - (p->map.print)(p); + if (p->map.print) + (p->map.print)(p); // faudrait printer les redirections (p->redir) ici genre avec ft_lstiter je pense if ((*plist)->next) ft_putstr(" |"); @@ -64,7 +65,8 @@ static void process_format_com_short(t_list **plist, t_flag state) p->attrs &= ~PROCESS_STATE_MASK; p->attrs &= ~PROCESS_RUNNING; } - (p->map.print)(p); + if (p->map.print) + (p->map.print)(p); // faudrait printer les redirections (p->redir) ici genre avec ft_lstiter je pense if ((*plist)->next) ft_putstr(" | "); From 4572a81c5a6beabd4604326f776ed9f048176cc6 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 14 Mar 2017 21:11:28 +0100 Subject: [PATCH 3/4] export finished, added cliopts --- 42sh/Makefile | 1 - 42sh/includes/builtin.h | 2 ++ 42sh/includes/builtin_read.h | 2 +- 42sh/includes/exec.h | 2 +- 42sh/includes/minishell.h | 2 +- 42sh/includes/types.h | 5 +-- 42sh/libft | 2 +- 42sh/src/builtin/bt_read_get.c | 2 +- 42sh/src/builtin/builtin_export.c | 53 ++++++++++++++++++++++++++--- 42sh/src/builtin/builtin_read.c | 2 +- 42sh/src/builtin/builtin_setenv.c | 8 ++--- 42sh/src/builtin/builtin_unsetenv.c | 5 +-- 42sh/src/exec/exec_leaf.c | 2 +- 42sh/src/exec/exec_var.c | 4 +-- 42sh/src/main/main.c | 15 ++++---- 42sh/src/main/shell_get_avdata.c | 34 ------------------ 42sh/src/main/shell_init.c | 2 +- 17 files changed, 77 insertions(+), 66 deletions(-) delete mode 100644 42sh/src/main/shell_get_avdata.c diff --git a/42sh/Makefile b/42sh/Makefile index 1f8b85d0..dbbdd149 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_avdata.c\ main/shell_get_opts.c\ main/shell_init.c\ parser/add_bang.c\ diff --git a/42sh/includes/builtin.h b/42sh/includes/builtin.h index 5f27f108..69999869 100644 --- a/42sh/includes/builtin.h +++ b/42sh/includes/builtin.h @@ -16,6 +16,8 @@ # include "libft.h" # include "builtin_read.h" +# define BT_EXPORT_LP (1 << 0) + t_execf *is_builtin(t_process *p); int builtin_export(const char *path, char *const av[], char *const envp[]); int builtin_unset(const char *path, char *const av[], char *const envp[]); diff --git a/42sh/includes/builtin_read.h b/42sh/includes/builtin_read.h index e0ebfc9f..1b656655 100644 --- a/42sh/includes/builtin_read.h +++ b/42sh/includes/builtin_read.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/20 15:02:39 by jhalford #+# #+# */ -/* Updated: 2017/03/13 20:39:49 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 19:57:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index b6564b2b..83104812 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/13 23:02:23 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 20:34:31 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 21c50f1b..9a445751 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/12 01:01:44 by ariard ### ########.fr */ +/* Updated: 2017/03/14 19:58:37 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/types.h b/42sh/includes/types.h index e6f8d640..e35c78d9 100644 --- a/42sh/includes/types.h +++ b/42sh/includes/types.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */ -/* Updated: 2017/03/14 00:49:40 by ariard ### ########.fr */ +/* Updated: 2017/03/14 20:25:53 by jhalford ### ########.fr */ /* Updated: 2017/03/07 18:35:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,9 +38,10 @@ typedef struct s_comp t_comp; typedef struct s_exec t_exec; typedef struct s_jobc t_jobc; typedef enum e_mode t_mode; +typedef struct s_data_template t_btexport; /* -** Execution types +** Lexer types */ typedef struct s_lexer t_lexer; diff --git a/42sh/libft b/42sh/libft index b209bb1f..2713f7ab 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit b209bb1fb718a68256253d5ab5ff69a46a90d5d6 +Subproject commit 2713f7ab295d895824b22ac39524f85a7e5b7985 diff --git a/42sh/src/builtin/bt_read_get.c b/42sh/src/builtin/bt_read_get.c index 575d694e..24b79c3c 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/07 11:27:49 by ariard ### ########.fr */ +/* Updated: 2017/03/14 18:11:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_export.c b/42sh/src/builtin/builtin_export.c index bdf78140..03bea7ec 100644 --- a/42sh/src/builtin/builtin_export.c +++ b/42sh/src/builtin/builtin_export.c @@ -6,20 +6,63 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 11:39:37 by gwojda #+# #+# */ -/* Updated: 2017/03/13 19:00:58 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 21:10:37 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int builtin_export(const char *path, char *const av[], char *const envp[]) +t_cliopts opts[] = { + {'p', NULL, BT_EXPORT_LP, 0, NULL}, + {0, NULL, 0, 0, NULL}, +}; + +int bt_export_print(void) +{ + char **env; char *equal; - if ((equal = ft_strchr(av[1], '='))) + env = data_singleton()->env; + while (*env) { - *equal = 0; - builtin_setenv(path, (char*[]){"setenv", av[1], equal + 1}, envp); + if ((equal = ft_strchr(*env, '='))) + ft_printf("export %s\n", *env); + else + ft_printf("export %s\n", *env); + env++; + } + return (0); +} + +int builtin_export(const char *path, char *const av[], char *const envp[]) +{ + char *equal; + t_btexport data; + int i; + + i = 0; + (void)envp; + (void)path; + if (cliopts_get((char**)av, opts, &data)) + ft_perror(); + if (data.flag & BT_EXPORT_LP) + return (bt_export_print()); + av = cliopts_getdata((char**)av); + while (*av) + { + if ((equal = ft_strchr(*av, '='))) + { + *equal = 0; + builtin_setenv("internal", (char*[]){"global", *av, equal + 1}, NULL); + } + else + { + equal = ft_getenv(data_singleton()->local_var, *av); + builtin_setenv("internal", (char*[]){"global", *av, equal}, NULL); + } + builtin_unsetenv("internal", (char*[]){"local", *av}, NULL); + av++; } return (0); } diff --git a/42sh/src/builtin/builtin_read.c b/42sh/src/builtin/builtin_read.c index 264f2b3b..8fdc0361 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/02/06 14:02:17 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 18:08:35 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_setenv.c b/42sh/src/builtin/builtin_setenv.c index 95316f18..b145cb2c 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 00:44:32 by ariard ### ########.fr */ +/* Updated: 2017/03/14 21:10:39 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,13 +21,9 @@ int builtin_setenv(const char *path, char *const av[], char *const envp[]) (void)envp; (void)path; i = 0; - DG("in setenv"); env = ft_strcmp(av[0], "local") == 0 ? &data_singleton()->local_var : &data_singleton()->env; - if (ft_strcmp(av[0], "setenv") == 0 - || ft_strcmp(av[0], "export") == 0 - || ft_strcmp(av[0], "local") == 0) - av++; + av++; if (!av[0]) { ft_sstrprint(*env, '\n'); diff --git a/42sh/src/builtin/builtin_unsetenv.c b/42sh/src/builtin/builtin_unsetenv.c index d684462a..2081f600 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: 2016/12/03 11:55:53 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 21:08:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,8 @@ int builtin_unsetenv(const char *path, char *const av[], char *const envp[]) (void)envp; (void)path; - env = &data_singleton()->env; + env = ft_strcmp(av[0], "local") == 0 ? + &data_singleton()->local_var : &data_singleton()->env; i = 1; while (av[i]) { diff --git a/42sh/src/exec/exec_leaf.c b/42sh/src/exec/exec_leaf.c index 16b4dd82..1404e6a9 100644 --- a/42sh/src/exec/exec_leaf.c +++ b/42sh/src/exec/exec_leaf.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 15:47:30 by wescande #+# #+# */ -/* Updated: 2017/03/13 23:56:53 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 20:26:49 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_var.c b/42sh/src/exec/exec_var.c index 578422d2..194309ae 100644 --- a/42sh/src/exec/exec_var.c +++ b/42sh/src/exec/exec_var.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 11:12:05 by ariard #+# #+# */ -/* Updated: 2017/03/07 18:37:38 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 21:09:45 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,6 +30,6 @@ int exec_var(t_btree **ast) node = (*ast)->item; av = token_to_argv(node->data.cmd.token, 1); set_var(av[0], &var, &value); - builtin_setenv("setenv", (char*[]){var, value, 0}, data_singleton()->local_var); + builtin_setenv("internal", (char*[]){"local", var, value, 0}, NULL); return (0); } diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 4da9ed74..b93491e2 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/13 23:58:56 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 20:49:16 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -82,7 +82,7 @@ int handle_instruction(int fd) return (0); } -int get_input_fd() +int get_input_fd(char **av) { t_data *data; char *file; @@ -96,13 +96,15 @@ int get_input_fd() { pipe(fds); fd = fds[PIPE_READ]; - file = shell_get_avdata(); + /* 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 = shell_get_avdata())) */ + else if ((file = *cliopts_getdata(av))) { if ((fd = open(file, O_RDONLY | O_CLOEXEC)) < 0) return (-1); @@ -116,12 +118,13 @@ int main(int ac, char **av) { int fd; + g_argv = av; setlocale(LC_ALL, ""); DG("{inv}{bol}{gre}start of shell{eoc}"); shell_init(ac, av); - if ((fd = get_input_fd()) < 0) + if ((fd = get_input_fd(av)) < 0) { - ft_printf("{red}%s: %s: No such file or directory\n{eoc}", SHELL_NAME, shell_get_avdata()); + ft_printf("{red}%s: %s: No such file or directory\n{eoc}", SHELL_NAME, *cliopts_getdata(av)); return (1); } DG("JOBC is %s, fd=[%i]", SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF", fd); diff --git a/42sh/src/main/shell_get_avdata.c b/42sh/src/main/shell_get_avdata.c deleted file mode 100644 index 16a34d13..00000000 --- a/42sh/src/main/shell_get_avdata.c +++ /dev/null @@ -1,34 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* shell_get_avdata.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/01/11 17:14:52 by jhalford #+# #+# */ -/* Updated: 2017/03/03 17:33:33 by wescande ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -char *shell_get_avdata() -{ - t_data *data; - char **av; - int i; - - data = data_singleton(); - av = data->argv; - i = 1; - while (av[i] && av[i][0] == '-') - { - if (ft_strcmp(av[i], "--") == 0) - { - i++; - break ; - } - i++; - } - return(av[i]); -} diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index 1844fe2e..d198c59a 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/14 11:09:31 by gwojda ### ########.fr */ +/* Updated: 2017/03/14 21:10:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ From c6a23fb2ac754cd1a235fba552ebd6ab88620e95 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 14 Mar 2017 21:51:40 +0100 Subject: [PATCH 4/4] 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); }