From 4b22633cab257b80c2768e580fe5eb1af49494d7 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Wed, 18 Jan 2017 14:28:49 +0100 Subject: [PATCH] backquotes ok for simple, need to do furthur testing later, also check how bash handles nested backquotes --- 42sh/src/lexer/command_getoutput.c | 23 +++++++---------------- 42sh/src/lexer/reduce_bquotes.c | 1 + 42sh/src/lexer/token_print.c | 1 - 42sh/src/main/main.c | 11 +++++------ 4 files changed, 13 insertions(+), 23 deletions(-) diff --git a/42sh/src/lexer/command_getoutput.c b/42sh/src/lexer/command_getoutput.c index ec2d5349..44b6c2e4 100644 --- a/42sh/src/lexer/command_getoutput.c +++ b/42sh/src/lexer/command_getoutput.c @@ -11,7 +11,7 @@ /* ************************************************************************** */ #include "minishell.h" -#define BUF_SIZE 10 +#define BUF_SIZE 1024 char *command_getoutput(char *command) { @@ -19,11 +19,12 @@ char *command_getoutput(char *command) t_btree *ast; t_astnode item; char *output; - char *buf; + char buf[BUF_SIZE + 1]; int ret; + t_exec *exec; output = NULL; - buf = NULL; + exec = &data_singleton()->exec; item.type = TK_SUBSHELL; item.data.sstr = malloc(4 * sizeof(char *)); item.data.sstr[0] = ft_strdup(data_singleton()->argv[0]); @@ -32,25 +33,15 @@ char *command_getoutput(char *command) item.data.sstr[3] = NULL; ast = btree_create_node(&item, sizeof(item)); pipe(fds); - dup2(fds[PIPE_WRITE], 1); - close(fds[PIPE_WRITE]); + exec->process.fdout = fds[PIPE_WRITE]; ft_exec(&ast); - /* DG("gonna parse"); */ - /* token_print(token); */ - /* if (ft_parse(&ast, &token)) */ - /* return (NULL); */ - /* DG("gonna exec"); */ - /* if (ft_exec(&ast)) */ - /* return (NULL); */ - - DG("gonna read from pipe"); + exec->process.fdout = STDOUT; + close(fds[PIPE_WRITE]); while ((ret = read(fds[PIPE_READ], buf, BUF_SIZE))) { buf[ret] = 0; - DG("read '%s' from pipe", buf); ft_strappend(&output, buf); } - DG("finished reading from pipe"); close(fds[PIPE_READ]); return (output); } diff --git a/42sh/src/lexer/reduce_bquotes.c b/42sh/src/lexer/reduce_bquotes.c index c50e9613..5198a2f2 100644 --- a/42sh/src/lexer/reduce_bquotes.c +++ b/42sh/src/lexer/reduce_bquotes.c @@ -44,6 +44,7 @@ int reduce_bquotes(t_list **alst, char **str) ft_strdel(str); *str = new; ft_lstdel(alst, token_free); + ft_tokenize(alst, new, DEFAULT); return (1); } return (0); diff --git a/42sh/src/lexer/token_print.c b/42sh/src/lexer/token_print.c index d720e73d..97876ddb 100644 --- a/42sh/src/lexer/token_print.c +++ b/42sh/src/lexer/token_print.c @@ -21,7 +21,6 @@ void token_print(t_list *lst) while (lst) { i = -1; - DG("lst at %p", lst); token = lst->content; type = token->type; while (type >> (i++ + 2)) diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index e552f3c6..27e00034 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -31,21 +31,20 @@ int shell_single_command(char *command) return (1); DG("after post_tokenize"); token_print(token); - /* if (ft_parse(&ast, &token)) */ - /* return (1); */ + if (ft_parse(&ast, &token)) + return (1); /* btree_print(STDBUG, ast, &ft_putast); */ /* /1* ft_dprintf(STDBUG, "\n--- INFIX BREAKDOWN ---\n"); *1/ */ /* /1* btree_apply_infix(ast, &ft_putast2); *1/ */ - /* if (ft_exec(&ast)) */ - /* return (1); */ + if (ft_exec(&ast)) + return (1); return (0); } int main(int ac, char **av) { - DG("{inv}{bol}{gre}start of shell"); + DG("{inv}{bol}{gre}start of shell{eoc} job_control is %s", data_singleton()->opts & SHELL_OPTS_JOBC ? "ON" : "OFF"); shell_init(ac, av); - DG("job_control is %s", data_singleton()->opts & SHELL_OPTS_JOBC ? "ON" : "OFF"); if (data_singleton()->opts & SHELL_OPTS_LC) { shell_single_command(shell_get_avdata());