From cba548d0ae307186d495d963a4bb6245b8dedefa Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Sun, 5 Mar 2017 18:17:51 +0100 Subject: [PATCH] redir io number seems good --- 42sh/Makefile | 1 + 42sh/includes/parser.h | 10 +++----- 42sh/src/exec/ast_free.c | 4 ++-- 42sh/src/exec/redir_free.c | 6 ++--- 42sh/src/exec/redirect_dgreat.c | 4 ++-- 42sh/src/exec/redirect_dless.c | 2 +- 42sh/src/exec/redirect_great.c | 4 ++-- 42sh/src/exec/redirect_greatand.c | 7 +++++- 42sh/src/exec/redirect_less.c | 6 ++--- 42sh/src/exec/redirect_lessand.c | 7 +++++- 42sh/src/lexer/lexer_number.c | 3 +-- 42sh/src/parser/add_case.c | 2 +- 42sh/src/parser/add_number.c | 40 +++++++++++++++++++++++++++++++ 42sh/src/parser/add_redir.c | 34 +++++++++----------------- 14 files changed, 81 insertions(+), 49 deletions(-) create mode 100644 42sh/src/parser/add_number.c diff --git a/42sh/Makefile b/42sh/Makefile index e0bfe3a8..34a6b874 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -239,6 +239,7 @@ parser/add_redir.c\ parser/add_sep.c\ parser/add_subshell.c\ parser/add_var.c\ +parser/add_number.c\ parser/aggregate_sym.c\ parser/build_tree.c\ parser/error_syntax.c\ diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index d654f75b..a6fbd771 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -125,6 +125,7 @@ int add_one_func(t_btree **ast, t_list **lst); int add_pipe(t_btree **ast, t_list **lst); int add_var(t_btree **ast, t_list **lst); int add_null(t_btree **ast, t_list **lst); +int add_ionumbr(t_btree **ast, t_list **lst); int isloop(t_btree **ast, t_list **lst); int isloop_condition(t_btree **ast, t_list **lst); @@ -141,6 +142,7 @@ int isdir_sep(t_btree **ast, t_list **list); int isdir_word(t_btree **ast, t_list **list); int isvar(t_btree **ast, t_list **list); int isnull(t_btree **ast, t_list **list); +int isionumber(t_btree **ast, t_list **lst); int join_ast(t_btree **ast, t_btree **new_node); int gen_node(t_btree **ast); @@ -154,17 +156,11 @@ struct s_distrostree extern t_distrostree g_distrostree[]; -union u_word -{ - char *word; - int fd; -}; - struct s_redir { t_flag type; int n; - t_word word; + char *word; int close; }; diff --git a/42sh/src/exec/ast_free.c b/42sh/src/exec/ast_free.c index 93baaec6..4975673e 100644 --- a/42sh/src/exec/ast_free.c +++ b/42sh/src/exec/ast_free.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/05 11:50:51 by jhalford #+# #+# */ -/* Updated: 2017/03/05 17:02:27 by ariard ### ########.fr */ +/* Updated: 2017/03/05 18:05:43 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ void read_redir(void *data) t_redir *redir; redir = data; - DG("file : [%s]", redir->word.word); + DG("file : [%s]", redir->word); } void ast_free(void *data, size_t content_size) diff --git a/42sh/src/exec/redir_free.c b/42sh/src/exec/redir_free.c index 41ed42d2..5befbe63 100644 --- a/42sh/src/exec/redir_free.c +++ b/42sh/src/exec/redir_free.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/03 18:12:57 by ariard #+# #+# */ -/* Updated: 2017/03/05 15:58:13 by jhalford ### ########.fr */ +/* Updated: 2017/03/05 18:06:24 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,11 +20,9 @@ void redir_free(void *data, size_t content_size) redir = data; if (redir->type == TK_GREAT || redir->type == TK_LESS || redir->type == TK_DGREAT) { - /* DG("word.word: %s", redir->word.word); */ - ft_strdel(&redir->word.word); + ft_strdel(&redir->word); } /* else */ - /* redir->word.fd = 0; */ /* redir->type = 0; */ /* redir->n = 0; */ /* redir->close = 1; */ diff --git a/42sh/src/exec/redirect_dgreat.c b/42sh/src/exec/redirect_dgreat.c index 62e01058..f890172b 100644 --- a/42sh/src/exec/redirect_dgreat.c +++ b/42sh/src/exec/redirect_dgreat.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:07:37 by jhalford #+# #+# */ -/* Updated: 2017/02/06 22:27:10 by jhalford ### ########.fr */ +/* Updated: 2017/03/05 18:10:36 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ int redirect_dgreat(t_redir *redir) int fdnew; fdnew = redir->n; - if ((fdold = open(redir->word.word, + if ((fdold = open(redir->word, O_WRONLY | O_CREAT | O_APPEND, 0644)) < 0) { DG("open errno=%i", errno); diff --git a/42sh/src/exec/redirect_dless.c b/42sh/src/exec/redirect_dless.c index 36f4a25a..0244098d 100644 --- a/42sh/src/exec/redirect_dless.c +++ b/42sh/src/exec/redirect_dless.c @@ -17,7 +17,7 @@ int redirect_dless(t_redir *redir) char *str; pipe(fds); - str = redir->word.word; + str = redir->word; write(fds[PIPE_WRITE], str, ft_strlen(str)); close(fds[PIPE_WRITE]); dup2(fds[PIPE_READ], 0); diff --git a/42sh/src/exec/redirect_great.c b/42sh/src/exec/redirect_great.c index bd6e170a..4ec227d8 100644 --- a/42sh/src/exec/redirect_great.c +++ b/42sh/src/exec/redirect_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:03:53 by jhalford #+# #+# */ -/* Updated: 2017/02/06 22:42:05 by jhalford ### ########.fr */ +/* Updated: 2017/03/05 18:07:25 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ int redirect_great(t_redir* redir) int fdnew; fdnew = redir->n; - if ((fdold = open(redir->word.word, + if ((fdold = open(redir->word, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) { DG("open errno=%i", errno); diff --git a/42sh/src/exec/redirect_greatand.c b/42sh/src/exec/redirect_greatand.c index 70658424..34803f72 100644 --- a/42sh/src/exec/redirect_greatand.c +++ b/42sh/src/exec/redirect_greatand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:12:31 by jhalford #+# #+# */ -/* Updated: 2017/02/07 17:54:40 by jhalford ### ########.fr */ +/* Updated: 2017/03/05 18:12:26 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,11 @@ int redirect_greatand(t_redir *redir) int fdold; int fdnew; + (void)redir; + (void)fdold; + (void)fdnew; DG("redir greatand"); + /* if (redir->close) { close(redir->n); @@ -33,5 +37,6 @@ int redirect_greatand(t_redir *redir) dup2_close(fdold, fdnew); else return (bad_fd(fdold)); + */ return (0); } diff --git a/42sh/src/exec/redirect_less.c b/42sh/src/exec/redirect_less.c index 8619db29..7942ce1a 100644 --- a/42sh/src/exec/redirect_less.c +++ b/42sh/src/exec/redirect_less.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:09:53 by jhalford #+# #+# */ -/* Updated: 2017/02/20 20:42:53 by ariard ### ########.fr */ +/* Updated: 2017/03/05 18:11:13 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,10 +18,10 @@ int redirect_less(t_redir *redir) int fdnew; fdnew = redir->n; - if ((fdold = open(redir->word.word, O_RDONLY)) < 0) + if ((fdold = open(redir->word, O_RDONLY)) < 0) { ft_dprintf(2, "{red}%s: no such file or directory: %s{eoc}\n", - SHELL_NAME, redir->word.word); + SHELL_NAME, redir->word); exit (1); } dup2(fdold, fdnew); diff --git a/42sh/src/exec/redirect_lessand.c b/42sh/src/exec/redirect_lessand.c index 15da4123..0442941d 100644 --- a/42sh/src/exec/redirect_lessand.c +++ b/42sh/src/exec/redirect_lessand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:11:18 by jhalford #+# #+# */ -/* Updated: 2017/02/07 17:54:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/05 18:13:11 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,10 @@ int redirect_lessand(t_redir *redir) int fdold; int fdnew; + (void)redir; + (void)fdold; + (void)fdnew; + /* if (redir->close) { close(redir->n); @@ -35,5 +39,6 @@ int redirect_lessand(t_redir *redir) } else return (bad_fd(fdold)); + */ return (0); } diff --git a/42sh/src/lexer/lexer_number.c b/42sh/src/lexer/lexer_number.c index 172700d5..b7eb03b4 100644 --- a/42sh/src/lexer/lexer_number.c +++ b/42sh/src/lexer/lexer_number.c @@ -6,8 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:45 by jhalford #+# #+# */ -/* Updated: 2017/03/05 17:21:51 by ariard ### ########.fr */ ->>>>>>> origin/pda_execution +/* Updated: 2017/03/05 18:03:34 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_case.c b/42sh/src/parser/add_case.c index 6b4661ed..74ab538e 100644 --- a/42sh/src/parser/add_case.c +++ b/42sh/src/parser/add_case.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/04 20:42:13 by ariard #+# #+# */ -/* Updated: 2017/03/05 16:47:15 by ariard ### ########.fr */ +/* Updated: 2017/03/05 17:30:19 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_number.c b/42sh/src/parser/add_number.c new file mode 100644 index 00000000..84ffe8ca --- /dev/null +++ b/42sh/src/parser/add_number.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* add_number.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/05 17:28:31 by ariard #+# #+# */ +/* Updated: 2017/03/05 18:15:40 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" + +int isionumber(t_btree **ast, t_list **lst) +{ + t_token *token; + + (void)ast; + token = (*lst)->content; + if (token->type == TK_IO_NUMBER) + return (1); + return (0); +} + +int add_ionumber(t_btree **ast, t_list **lst) +{ + t_astnode *node; + t_token *token; + t_redir redir; + + if (!*ast) + gen_node(ast); + token = (*lst)->content; + node = (*ast)->item; + node->type = token->type; + redir.n = ft_atoi(token->data); + ft_lsteadd(&node->data.cmd.redir, ft_lstnew(&redir, sizeof(redir))); + return (0); +} diff --git a/42sh/src/parser/add_redir.c b/42sh/src/parser/add_redir.c index 10f66350..46d89b71 100644 --- a/42sh/src/parser/add_redir.c +++ b/42sh/src/parser/add_redir.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 16:39:05 by ariard #+# #+# */ -/* Updated: 2017/03/05 17:22:24 by ariard ### ########.fr */ +/* Updated: 2017/03/05 18:14:00 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,26 +14,14 @@ int isdir_sep(t_btree **ast, t_list **list) { - t_astnode *node; t_token *token; + (void)ast; token = (*list)->content; - if (*ast) - { - node = (*ast)->item; - if (node->type == CMD - && (token->type == TK_LESS || token->type == TK_GREAT - || token->type == TK_GREATAND || token->type == TK_LESSAND - || token->type == TK_DLESS || token->type == TK_DGREAT)) - return (1); - } - if (!*ast) - { - if (token->type == TK_LESS || token->type == TK_GREAT - || token->type == TK_GREATAND || token->type == TK_LESSAND - || token->type == TK_DLESS || token->type == TK_DGREAT) - return (1); - } + if (token->type == TK_LESS || token->type == TK_GREAT + || token->type == TK_GREATAND || token->type == TK_LESSAND + || token->type == TK_DLESS || token->type == TK_DGREAT) + return (1); return (0); } @@ -68,11 +56,9 @@ int add_redir_word(t_btree **ast, t_list **lst) DG("add file"); redir = (ft_lstlast(node->data.cmd.redir))->content; if (redir->type == TK_DLESS) - redir->word.word = NULL; - else if (ft_stris((char *)token->data, &ft_isdigit)) - redir->word.fd = ft_atoi(token->data); + redir->word = NULL; else - redir->word.word = ft_strdup(token->data); + redir->word = ft_strdup(token->data); } return (0); } @@ -88,10 +74,12 @@ int add_redir_type(t_btree **ast, t_list **lst) token = (*lst)->content; node = (*ast)->item; if (!(node->type == TK_IO_NUMBER)) + { redir.n = (token->type == TK_LESS || token->type == TK_DLESS || token->type == TK_LESSAND) ? STDIN : STDOUT; + ft_lsteadd(&node->data.cmd.redir, ft_lstnew(&redir, sizeof(redir))); + } node->type = REDIR; redir.type = token->type; - ft_lsteadd(&node->data.cmd.redir, ft_lstnew(&redir, sizeof(redir))); return (0); }