diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 345b5afb..136d9db8 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: 2016/11/29 19:22:30 by jhalford ### ########.fr */ +/* Updated: 2016/11/30 21:07:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,7 +30,10 @@ extern t_exec g_exec[]; int ft_exec(t_btree *ast, t_data *data); int exec_semi(t_btree *ast, t_data *data); +int exec_or_if(t_btree *ast, t_data *data); +int exec_and_if(t_btree *ast, t_data *data); int exec_pipe(t_btree *ast, t_data *data); + int exec_less(t_btree *ast, t_data *data); int exec_great(t_btree *ast, t_data *data); int exec_dgreat(t_btree *ast, t_data *data); diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index cc077777..73085b71 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 15:31:42 by jhalford #+# #+# */ -/* Updated: 2016/11/28 18:36:29 by jhalford ### ########.fr */ +/* Updated: 2016/11/30 21:07:42 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,16 +15,19 @@ # include "minishell.h" -# define TK_LESS 0x0001 -# define TK_GREAT 0x0002 -# define TK_DLESS 0x0004 -# define TK_DGREAT 0x0008 -# define TK_LESSAND 0x0010 -# define TK_GREATAND 0x0020 -# define TK_SEMI 0x0040 -# define TK_PIPE 0x0080 -# define TK_WORD 0x0100 -# define TK_COMMAND 0x0200 +# define TK_LESS 0x00000001 +# define TK_GREAT 0x00000002 +# define TK_DLESS 0x00000004 +# define TK_DGREAT 0x00000008 +# define TK_LESSAND 0x00000010 +# define TK_GREATAND 0x00000020 +# define TK_SEMI 0x00000040 +# define TK_PIPE 0x00000080 +# define TK_AND_IF 0x00000100 +# define TK_OR_IF 0x00000200 +# define TK_AMP 0x00000400 +# define TK_WORD 0x00100000 +# define TK_COMMAND 0x00200000 # define TK_REDIR (0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20) diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 783df9d3..cc9214b5 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: 2016/11/29 20:30:49 by jhalford ### ########.fr */ +/* Updated: 2016/11/30 21:48:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_and_if.c b/42sh/src/exec/exec_and_if.c new file mode 100644 index 00000000..34bb5ebb --- /dev/null +++ b/42sh/src/exec/exec_and_if.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_and_if.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/30 20:52:28 by jhalford #+# #+# */ +/* Updated: 2016/11/30 21:06:05 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +int exec_and_if(t_btree *ast, t_data *data) +{ + ft_exec(ast->left, data); + ft_exec(ast->right, data); + return (0); +} diff --git a/42sh/src/exec/exec_or_if.c b/42sh/src/exec/exec_or_if.c new file mode 100644 index 00000000..d28a347b --- /dev/null +++ b/42sh/src/exec/exec_or_if.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_or_if.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/30 21:06:17 by jhalford #+# #+# */ +/* Updated: 2016/11/30 21:45:53 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +int exec_or_if(t_btree *ast, t_data *data) +{ + ft_exec(ast->left, data); + ft_exec(ast->right, data); + return (0); +} diff --git a/42sh/src/exec/exec_semi.c b/42sh/src/exec/exec_semi.c index 6569181e..e75061ba 100644 --- a/42sh/src/exec/exec_semi.c +++ b/42sh/src/exec/exec_semi.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_semi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/30 20:52:05 by jhalford #+# #+# */ +/* Updated: 2016/11/30 20:52:09 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "exec.h" int exec_semi(t_btree *ast, t_data *data) diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index dcefd761..ec42031d 100644 --- a/42sh/src/exec/ft_exec.c +++ b/42sh/src/exec/ft_exec.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */ -/* Updated: 2016/11/28 18:16:13 by jhalford ### ########.fr */ +/* Updated: 2016/11/30 20:52:07 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,8 @@ t_exec g_exec[] = { + {TK_AND_IF, &exec_and_if}, + {TK_OR_IF, &exec_or_if}, {TK_SEMI, &exec_semi}, {TK_PIPE, &exec_pipe}, {TK_LESS, &exec_less}, diff --git a/42sh/src/lexer/ft_tokenize.c b/42sh/src/lexer/ft_tokenize.c index 92dd69ba..0079ad13 100644 --- a/42sh/src/lexer/ft_tokenize.c +++ b/42sh/src/lexer/ft_tokenize.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:37:11 by jhalford #+# #+# */ -/* Updated: 2016/11/28 16:46:16 by jhalford ### ########.fr */ +/* Updated: 2016/11/30 16:57:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,7 +44,7 @@ int ft_tokenize(t_list **alst, char *str, t_lexstate state) *alst = ft_lstnew(token, sizeof(*token)); if (ft_is_delim(*str)) state = DELIM; - if (*str == ';' || *str == '|') + if (*str == '&' || *str == ';' || *str == '|') state = SEP; else if (*str == '\'') { diff --git a/42sh/src/lexer/lexer_sep.c b/42sh/src/lexer/lexer_sep.c index 2ba0b429..fa511fb5 100644 --- a/42sh/src/lexer/lexer_sep.c +++ b/42sh/src/lexer/lexer_sep.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lexer_sep.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/30 16:29:57 by jhalford #+# #+# */ +/* Updated: 2016/11/30 16:56:58 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "lexer.h" int lexer_sep(t_list **alst, char *str) @@ -16,6 +28,13 @@ int lexer_sep(t_list **alst, char *str) *alst = ft_lstnew(token, sizeof(*token)); } token = (*alst)->content; - token->type = (*str == ';') ? TK_SEMI : TK_PIPE; - return (ft_tokenize(&(*alst)->next, str + 1, DEFAULT)); + if (str[0] == '&') + token->type = str[1] == '&' ? TK_AND_IF : TK_AMP; + if (str[0] == '|') + token->type = str[1] == '|' ? TK_OR_IF : TK_PIPE; + token->type = (*str == ';') ? TK_SEMI : token->type; + return (ft_tokenize(&(*alst)->next, + str + 1 + + (token->type & (TK_AND_IF | TK_OR_IF) ? 1 : 0), + DEFAULT)); } diff --git a/42sh/src/lexer/token_cmp_type.c b/42sh/src/lexer/token_cmp_type.c index 910f073b..43f80c0d 100644 --- a/42sh/src/lexer/token_cmp_type.c +++ b/42sh/src/lexer/token_cmp_type.c @@ -1,6 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* token_cmp_type.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/30 17:08:55 by jhalford #+# #+# */ +/* Updated: 2016/11/30 17:52:00 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "lexer.h" int token_cmp_type(t_token *token, t_type *ref) { - return (token->type - *ref); + return (!(token->type & *ref)); } diff --git a/42sh/src/lexer/token_print.c b/42sh/src/lexer/token_print.c index 90871dbe..6ab2b601 100644 --- a/42sh/src/lexer/token_print.c +++ b/42sh/src/lexer/token_print.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:39:01 by jhalford #+# #+# */ -/* Updated: 2016/11/28 14:39:08 by jhalford ### ########.fr */ +/* Updated: 2016/11/30 20:50:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ void token_print(t_list *lst) while (lst) { token = lst->content; - ft_dprintf(2, "%#06llx: '%s'\n", token->type, token->data); + ft_dprintf(2, "%#010llx: '%s' (at %p next at %p)\n", token->type, token->data, lst, lst->next); lst = lst->next; } } diff --git a/42sh/src/main/ft_putast.c b/42sh/src/main/ft_putast.c index cfdfe8a0..431c651c 100644 --- a/42sh/src/main/ft_putast.c +++ b/42sh/src/main/ft_putast.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */ -/* Updated: 2016/11/28 14:47:40 by jhalford ### ########.fr */ +/* Updated: 2016/11/30 21:48:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,8 +17,14 @@ char *ft_putast(void *nodein) t_astnode *node; node = nodein; - if (node->type == TK_SEMI) + if (node->type == TK_AMP) + return (" & "); + else if (node->type == TK_SEMI) return (" ; "); + else if (node->type == TK_AND_IF) + return (" && "); + else if (node->type == TK_OR_IF) + return (" || "); else if (node->type == TK_PIPE) return (" | "); else if (node->type == TK_COMMAND) diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 169473be..ee7018ff 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 21:13:34 by jhalford #+# #+# */ -/* Updated: 2016/11/29 20:08:54 by jhalford ### ########.fr */ +/* Updated: 2016/11/30 21:48:53 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,21 +28,21 @@ int main(void) if (ft_interactive_sh(&data)) return (1); /* ft_dprintf(STDERR, "command='%s'\n", data.input); */ + token = NULL; if (ft_tokenize(&token, data.input, DEFAULT)) return (1); if (!token) continue ; - /* token_print(token); */ + token_print(token); ast = NULL; if (ft_parse(&ast, &token)) return (1); - /* btree_print(STDERR, ast, &ft_putast); */ + btree_print(STDERR, ast, &ft_putast); /* ft_dprintf(STDERR, "\n--- INFIX BREAKDOWN ---\n"); */ /* btree_apply_infix(ast, &ft_putast2); */ /* ft_lstdel(&token, &token_free); */ - token = NULL; - if (ft_exec(ast, &data)) - return (1); + /* if (ft_exec(ast, &data)) */ + /* return (1); */ } return (0); } diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index 0c64b9f8..71f0e9c8 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_parse.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/14 11:30:08 by jhalford #+# #+# */ -/* Updated: 2016/11/28 16:38:17 by jhalford ### ########.fr */ +/* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */ +/* Updated: 2016/11/30 21:05:53 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,8 @@ t_parser g_parser[] = { - {TK_SEMI, &parse_separator}, + {TK_SEMI | TK_AMP, &parse_separator}, + {TK_AND_IF | TK_OR_IF, &parse_separator}, {TK_PIPE, &parse_separator}, {TK_LESS, &parse_less}, {TK_GREAT, &parse_great}, @@ -44,14 +45,11 @@ int ft_parse(t_btree **ast, t_list **start) { if ((lst = ft_lst_find(*start, &g_parser[i].type, &token_cmp_type))) { - /* ft_printf("found token:%#06llx\n", g_parser[i].type); */ - item.type = g_parser[i].type; if (g_parser[i].f) (*g_parser[i].f)(ast, start, &lst); return (0); } - else - i++; + i++; } return (0); } diff --git a/42sh/src/parser/parse_great.c b/42sh/src/parser/parse_great.c index d13a6652..af9bb33c 100644 --- a/42sh/src/parser/parse_great.c +++ b/42sh/src/parser/parse_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2016/11/14 17:47:35 by jhalford ### ########.fr */ +/* Updated: 2016/11/30 21:45:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,8 +28,10 @@ int parse_great(t_btree **ast, t_list **start, t_list **lst) return (1); node->u_data.redir.n = ft_atoi(tok->data); node->u_data.redir.u_word.word = ft_strdup(next_tok->data); + ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); ft_lst_delif(start, (*lst)->next->content, &ft_addrcmp, &token_free); + ft_parse(&(*ast)->left, start); return (0); } diff --git a/42sh/src/parser/parse_less.c b/42sh/src/parser/parse_less.c index a0eb6aed..b94b9129 100644 --- a/42sh/src/parser/parse_less.c +++ b/42sh/src/parser/parse_less.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2016/11/28 18:12:39 by jhalford ### ########.fr */ +/* Updated: 2016/11/30 18:03:37 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_separator.c b/42sh/src/parser/parse_separator.c index 2b926da6..fcea3ea7 100644 --- a/42sh/src/parser/parse_separator.c +++ b/42sh/src/parser/parse_separator.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 16:21:51 by jhalford #+# #+# */ -/* Updated: 2016/11/28 16:21:56 by jhalford ### ########.fr */ +/* Updated: 2016/11/30 21:45:49 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,10 +19,11 @@ int parse_separator(t_btree **ast, t_list **start, t_list **lst) token = (*lst)->content; node = (*ast)->item; + node->type = token->type; + ft_parse(&(*ast)->right, &(*lst)->next); - node->type = token->type; - ft_lstdelone(lst, &token_free); + ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); ft_parse(&(*ast)->left, start); return (0); diff --git a/42sh/src/parser/parse_word.c b/42sh/src/parser/parse_word.c index b432c49c..87d48e48 100644 --- a/42sh/src/parser/parse_word.c +++ b/42sh/src/parser/parse_word.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2016/11/14 17:48:50 by jhalford ### ########.fr */ +/* Updated: 2016/11/30 21:19:16 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,7 @@ int parse_word(t_btree **ast, t_list **start, t_list **lst) node->type = TK_COMMAND; node->u_data.sstr = ft_sstradd(node->u_data.sstr, token->data); ft_parse(ast, &(*lst)->next); - ft_lstdelone(lst, &token_free); + + ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); return (0); }