diff --git a/42sh/Makefile b/42sh/Makefile index 4e41b851..5fa85765 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -137,6 +137,7 @@ job-control/sigtstp_handler.c\ job-control/sigttin_handler.c\ job-control/sigttou_handler.c\ lexer/command_getoutput.c\ +lexer/expand_bquotes.c\ lexer/get_state_global.c\ lexer/get_state_redir.c\ lexer/lexer_backslash.c\ @@ -157,12 +158,10 @@ lexer/lexer_paren.c\ lexer/lexer_quote.c\ lexer/lexer_sep.c\ lexer/lexer_word.c\ -lexer/reduce_bquotes.c\ lexer/reduce_parens.c\ lexer/stack_to_prompt.c\ lexer/token_append.c\ lexer/token_cmp_type.c\ -lexer/token_expand_var.c\ lexer/token_free.c\ lexer/token_init.c\ lexer/token_print.c\ diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 3385e5b8..3e9f37fd 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -30,14 +30,12 @@ # define TK_AMP (1 << 10) # define TK_PAREN_OPEN (1 << 11) # define TK_PAREN_CLOSE (1 << 12) -# define TK_BQUOTE (1 << 13) -# define TK_WORD (1 << 14) -# define TK_COMMAND (1 << 17) -# define TK_SUBSHELL (1 << 18) -# define TK_NEWLINE (1 << 19) +# define TK_WORD (1 << 13) +# define TK_COMMAND (1 << 14) +# define TK_SUBSHELL (1 << 15) +# define TK_NEWLINE (1 << 16) # define TK_REDIR (0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20) -# define TK_NON_FREEABLE (TK_BQUOTE) enum e_lexstate { @@ -89,10 +87,9 @@ int token_append(t_token *token, t_lexer *lexer, void token_free(void *data, size_t size); int token_cmp_type(t_token *token, t_type *ref); void token_print(t_list *lst); -void token_expand_var(t_token *token); int reduce_parens(t_list **alst, char *str); -int reduce_bquotes(t_list **alst, char **str); +int expand_bquotes(t_list **alst); char *command_getoutput(char *command); int ft_is_delim(char c); diff --git a/42sh/libft b/42sh/libft index acd7e54f..d0ac53c6 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit acd7e54fb8045b3958dd239d469f1476404468b7 +Subproject commit d0ac53c68648106b8bafcc5ee8334306f6b0bc7e diff --git a/42sh/src/lexer/command_getoutput.c b/42sh/src/lexer/command_getoutput.c index 7dbd1c46..114216c8 100644 --- a/42sh/src/lexer/command_getoutput.c +++ b/42sh/src/lexer/command_getoutput.c @@ -34,7 +34,7 @@ char *command_getoutput(char *command) ast = btree_create_node(&item, sizeof(item)); pipe(fds); exec->process.fdout = fds[PIPE_WRITE]; - ft_exec(&ast); + exec_command(&ast); exec->process.fdout = STDOUT; close(fds[PIPE_WRITE]); while ((ret = read(fds[PIPE_READ], buf, BUF_SIZE))) diff --git a/42sh/src/lexer/expand_bquotes.c b/42sh/src/lexer/expand_bquotes.c new file mode 100644 index 00000000..9336761e --- /dev/null +++ b/42sh/src/lexer/expand_bquotes.c @@ -0,0 +1,79 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* expand_bquotes.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/11 16:46:27 by jhalford #+# #+# */ +/* Updated: 2017/02/09 20:45:08 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "lexer.h" + +int expand_bquotes(t_list **alst) +{ + t_list *new_word; + t_list *after_word; + t_list *cur_word; + t_list *lst; + t_token *token; + t_token token_buf; + char *word; + char *output; + char *bq_start; + char *bq_end; + char *after_bq; + char **str; + t_flag tk; + + tk = TK_WORD; + cur_word = *alst; + while (cur_word && (lst = ft_lst_find(cur_word, &tk, token_cmp_type))) + { + cur_word = lst; + after_word = cur_word->next; + token = cur_word->content; + str = &token->data; + DG("found word=[%s]", *str); + if (!(bq_start = ft_strchr(*str, '`'))) + { + cur_word = cur_word->next; + continue ; + } + if (!(bq_end = ft_strchr(bq_start + 1, '`'))) + { + ft_dprintf(2, "{red}%s: parse error near '`'{eoc}\n", SHELL_NAME); + return (-1); + } + *bq_end = 0; + after_bq = ft_strdup(bq_end + 1); + output = command_getoutput(bq_start + 1); + word = ft_strtok(output, " \n\t"); + DG("strtok=[%s]", word); + DG("first_tok was [%s]", *str); + *bq_start = 0; + ft_strappend(str, word); + DG("first_tok=[%s]", *str); + while ((word = ft_strtok(NULL, " \n\t"))) + { + DG("strtok=[%s]", word); + token_buf.data = ft_strdup(word); + token_buf.type = TK_WORD; + new_word = ft_lstnew(&token_buf, sizeof(token_buf)); + cur_word->next = new_word; + new_word->next = after_word; + cur_word = new_word; + } + token = new_word->content; + ft_strappend(&token->data, after_bq); + ft_strdel(&after_bq); + ft_strdel(&output); + cur_word = after_word; + } + token_print(*alst); + DG("check end"); + return (0); +} + diff --git a/42sh/src/lexer/reduce_bquotes.c b/42sh/src/lexer/reduce_bquotes.c deleted file mode 100644 index f7c416de..00000000 --- a/42sh/src/lexer/reduce_bquotes.c +++ /dev/null @@ -1,57 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* reduce_bquotes.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/01/11 16:46:27 by jhalford #+# #+# */ -/* Updated: 2017/02/09 20:45:08 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "lexer.h" - -int reduce_bquotes(t_list **alst, char **str) -{ - t_list *start; - t_list **end; - t_token *token; - int tk; - char *new; - char *fit; - char *bq_start; - char *bq_end; - t_lexer lexer; - - tk = TK_BQUOTE; - if ((start = ft_lst_find(*alst, &tk, token_cmp_type))) - { - end = &start->next; - while (end && *end) - { - token = (*end)->content; - if (token->type == TK_BQUOTE) - break ; - end = &(*end)->next; - } - if (!*end) - return (-1); - bq_start = ((t_token*)start->content)->data; - bq_end = ((t_token*)(*end)->content)->data; - ft_lstdel(end, token_free); - fit = command_getoutput(ft_strbetween(bq_start + 1, bq_end)); - new = ft_strreplace(str, bq_start, bq_end, fit); - ft_strdel(str); - *str = new; - ft_lstdel(alst, token_free); - - lexer.pos = 0; - lexer.str = *str; - lexer.state = DEFAULT; - lexer_lex(alst, &lexer); - return (1); - } - return (0); -} - diff --git a/42sh/src/lexer/token_expand_var.c b/42sh/src/lexer/token_expand_var.c deleted file mode 100644 index 689dcb60..00000000 --- a/42sh/src/lexer/token_expand_var.c +++ /dev/null @@ -1,29 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* token_expand_var.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/01/10 14:57:53 by jhalford #+# #+# */ -/* Updated: 2017/02/07 13:16:04 by wescande ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "lexer.h" -/* -void token_expand_var(t_token *token) -{ - char *dollar; - char *val; - - dollar = ft_strchr(token->data, '$'); - if (!dollar[1]) - return ; - val = ft_getenv(data_singleton()->env, dollar + 1); - *dollar = 0; - if (val) - while (*val) - token_append(token, *val++, 1, 1); -} -*/ diff --git a/42sh/src/lexer/token_free.c b/42sh/src/lexer/token_free.c index a807db59..651b3611 100644 --- a/42sh/src/lexer/token_free.c +++ b/42sh/src/lexer/token_free.c @@ -18,11 +18,8 @@ void token_free(void *data, size_t size) (void)size; token = data; - if (!(token->type & TK_NON_FREEABLE)) - { - ft_strdel(&token->data); - ft_memdel((void **)&token->esc); - ft_memdel((void **)&token->esc2); - } + ft_strdel(&token->data); + ft_memdel((void **)&token->esc); + ft_memdel((void **)&token->esc2); free(token); } diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 3160048d..eeaf7f45 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -12,6 +12,35 @@ #include "minishell.h" +int non_interactive_shell(char *command) +{ + t_list *token; + t_lexer lexer; + t_btree *ast; + + lexer.pos = 0; + lexer.state = DEFAULT; + lexer.str = command; + lexer.stack = NULL; + token = NULL; + ast = NULL; + while (lexer.str[lexer.pos]) + { + if (lexer.stack && *(int*)lexer.stack->content == BACKSLASH) + pop(&lexer.stack); + do { + lexer_lex(&token, &lexer); + } while (lexer.str[lexer.pos] == '\n'); + if (expand_bquotes(&token)) + return (1); + token_print(token); + if (ft_parse(&ast, &token)) + return (1); + if (ft_exec(&ast)) + return (1); + } + return (0); +} int interactive_shell() { t_list *token; @@ -34,6 +63,11 @@ int interactive_shell() DG("[{mag}%s{eoc}] stack=[%i] state=[%i]", lexer.str, lexer.stack ? *(int*)lexer.stack->content : 0, lexer.state); token_print(token); } while (lexer.stack); + if (expand_bquotes(&token)) + return (1); + DG("check main 0"); + token_print(token); + DG("check main 1"); if (ft_parse(&ast, &token)) return (1); btree_print(STDBUG, ast, &ft_putast); @@ -55,5 +89,7 @@ int main(int ac, char **av) while (1) interactive_shell(); } + else + non_interactive_shell(shell_get_avdata()); return (0); } diff --git a/42sh/src/main/shell_exit.c b/42sh/src/main/shell_exit.c index 0983fa88..658ae832 100644 --- a/42sh/src/main/shell_exit.c +++ b/42sh/src/main/shell_exit.c @@ -16,6 +16,7 @@ void shell_exit(void) { DG("shell_exit()"); data_exit(); - job_kill_all(); + if (SH_HAS_JOBC(data_singleton()->opts)) + job_kill_all(); tcsetattr(STDIN, TCSANOW, &data_singleton()->jobc.shell_tmodes); }