From 62be4bc4707158fe3a0e6a8a140722e2a8c810c6 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 3 Feb 2017 14:11:34 +0100 Subject: [PATCH] new architecture for redirection inside of fork(), chained redirects dont work properly yet --- 42sh/Makefile | 5 +- 42sh/includes/exec.h | 9 ++- 42sh/includes/lexer.h | 2 +- 42sh/includes/parser.h | 13 +---- 42sh/includes/types.h | 32 ++++++----- 42sh/src/exec/exec_command.c | 3 +- 42sh/src/exec/exec_dgreat.c | 27 --------- 42sh/src/exec/exec_less.c | 30 ---------- 42sh/src/exec/{exec_great.c => exec_redir.c} | 14 ++--- 42sh/src/exec/ft_exec.c | 6 +- 42sh/src/exec/process_do_redirection.c | 58 ++++++++++++++++++++ 42sh/src/exec/process_redirect.c | 10 +++- 42sh/src/job-control/process_free.c | 3 +- 42sh/src/lexer/ft_tokenize.c | 2 +- 42sh/src/main/data_init.c | 3 +- 42sh/src/parser/ft_parse.c | 2 +- 42sh/src/parser/parse_great.c | 2 +- 42sh/src/parser/parse_greatand.c | 3 +- 42sh/src/parser/parse_lessand.c | 3 +- 42sh/src/parser/parse_redir.c | 2 +- 20 files changed, 118 insertions(+), 111 deletions(-) delete mode 100644 42sh/src/exec/exec_dgreat.c delete mode 100644 42sh/src/exec/exec_less.c rename 42sh/src/exec/{exec_great.c => exec_redir.c} (72%) create mode 100644 42sh/src/exec/process_do_redirection.c diff --git a/42sh/Makefile b/42sh/Makefile index 8a4eda07..48bc5be4 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -44,15 +44,14 @@ exec/ast_free.c\ exec/exec_ampersand.c\ exec/exec_and_if.c\ exec/exec_command.c\ -exec/exec_dgreat.c\ -exec/exec_great.c\ -exec/exec_less.c\ exec/exec_or_if.c\ exec/exec_pipe.c\ +exec/exec_redir.c\ exec/exec_semi.c\ exec/ft_exec.c\ exec/ft_findexec.c\ exec/launch_process.c\ +exec/process_do_redirection.c\ exec/process_redirect.c\ exec/process_reset.c\ exec/process_setexec.c\ diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index aaee3466..0616b685 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/01/12 13:18:27 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 14:01:10 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,6 +47,7 @@ struct s_process pid_t pid; int fdin; int fdout; + t_list *redirs; int toclose; int status; t_flag attributes; @@ -77,16 +78,14 @@ int exec_ampersand(t_btree **ast); int exec_or_if(t_btree **ast); int exec_and_if(t_btree **ast); int exec_pipe(t_btree **ast); - -int exec_less(t_btree **ast); -int exec_great(t_btree **ast); -int exec_dgreat(t_btree **ast); +int exec_redir(t_btree **ast); int exec_command(t_btree **ast); int launch_process(t_process *p); int process_setexec(t_type type, t_process *p); int process_setgroup(t_process *p, pid_t pid); int process_redirect(t_process *p); +void process_do_redirection(t_redir *redir); void process_setsig(void); void process_free(void *content, size_t content_size); void process_reset(void); diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 0b4f3424..a3508bc6 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ -/* Updated: 2017/02/02 15:36:09 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 13:25:50 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 0bbc3553..f5eae463 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */ -/* Updated: 2017/02/02 19:03:31 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 14:01:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,14 +15,6 @@ # include "minishell.h" -typedef struct s_parser t_parser; -typedef struct s_ld t_ld; -typedef struct s_astnode t_astnode; -typedef struct s_redir t_redir; -typedef union u_astdata t_astdata; -typedef union u_word t_word; -typedef long long t_type; - struct s_parser { t_type type; @@ -37,9 +29,10 @@ union u_word struct s_redir { + t_flag type; int n; - int close; t_word word; + int close; }; union u_astdata diff --git a/42sh/includes/types.h b/42sh/includes/types.h index af8071d0..c2aea9ff 100644 --- a/42sh/includes/types.h +++ b/42sh/includes/types.h @@ -6,29 +6,33 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */ -/* Updated: 2017/01/10 12:45:35 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 14:02:56 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef TYPES_H # define TYPES_H -typedef long long t_type; -typedef struct s_line t_line; -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 t_data; -typedef struct s_data t_data; -typedef enum e_qstate t_qstate; - -typedef struct s_job t_job; -typedef struct s_jobc t_jobc; -typedef struct s_execmap t_execmap; -typedef struct s_process t_process; typedef long long t_type; typedef long long t_flag; +typedef struct s_line t_line; +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_parser t_parser; +typedef struct s_ld t_ld; +typedef struct s_astnode t_astnode; +typedef struct s_redir t_redir; +typedef union u_astdata t_astdata; +typedef union u_word t_word; + +typedef struct s_job t_job; +typedef struct s_execmap t_execmap; +typedef struct s_process t_process; typedef int (t_execf)(const char *path, char *const argv[], char *const envp[]); t_data *data_singleton(); diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index e0f5e71b..b9db339a 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ -/* Updated: 2017/02/02 15:45:32 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 13:45:08 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -66,6 +66,7 @@ int exec_command(t_btree **ast) p->av = NULL; p->pid = 0; p->attributes &= ~(PROCESS_STATE_MASK | PROCESS_TYPE_MASK); + p->redirs = NULL; btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_dgreat.c b/42sh/src/exec/exec_dgreat.c deleted file mode 100644 index 9897f54d..00000000 --- a/42sh/src/exec/exec_dgreat.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* exec_dgreat.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/28 18:15:13 by jhalford #+# #+# */ -/* Updated: 2016/12/13 17:13:58 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "exec.h" - -int exec_dgreat(t_btree **ast) -{ - t_astnode *node; - int fd; - - node = (*ast)->item; - fd = open(node->data.redir.word.word, O_WRONLY | O_APPEND | O_CREAT, 0644); - data_singleton()->exec.process.fdout = fd; - ft_exec(&(*ast)->left); - data_singleton()->exec.process.fdout = STDOUT; - btree_delone(ast, &ast_free); - return (0); -} diff --git a/42sh/src/exec/exec_less.c b/42sh/src/exec/exec_less.c deleted file mode 100644 index f27be538..00000000 --- a/42sh/src/exec/exec_less.c +++ /dev/null @@ -1,30 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* exec_less.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/14 17:27:08 by jhalford #+# #+# */ -/* Updated: 2016/12/13 17:14:46 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "exec.h" - -int exec_less(t_btree **ast) -{ - t_astnode *node; - int fd; - - node = (*ast)->item; - fd = open(node->data.redir.word.word, O_RDONLY); - data_singleton()->exec.process.fdin = fd; - /* ft_strappend(&data->exec.process.command, "<"); */ - /* ft_strappend(&data->exec.process.command, node->data.redir.word.word); */ - ft_exec(&(*ast)->left); - data_singleton()->exec.process.fdin = STDIN; - /* data->exec.process.command = NULL; */ - btree_delone(ast, &ast_free); - return (0); -} diff --git a/42sh/src/exec/exec_great.c b/42sh/src/exec/exec_redir.c similarity index 72% rename from 42sh/src/exec/exec_great.c rename to 42sh/src/exec/exec_redir.c index fa307617..d7becf4b 100644 --- a/42sh/src/exec/exec_great.c +++ b/42sh/src/exec/exec_redir.c @@ -1,27 +1,27 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* exec_great.c :+: :+: :+: */ +/* exec_redir.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:27:51 by jhalford #+# #+# */ -/* Updated: 2017/02/02 19:06:38 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 13:58:38 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" -int exec_great(t_btree **ast) +int exec_redir(t_btree **ast) { t_astnode *node; - int fd; + t_process *p; + p = &data_singleton()->exec.process; node = (*ast)->item; - fd = open(node->data.redir.word.word, O_WRONLY | O_TRUNC | O_CREAT, 0644); - data_singleton()->exec.process.fdout = fd; + node->data.redir.type = node->type; + ft_lsteadd(&p->redirs, ft_lstnew(&node->data.redir,sizeof(node->data.redir))); ft_exec(&(*ast)->left); - data_singleton()->exec.process.fdout = STDOUT; btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 1b5f4c59..6bf0e751 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: 2017/02/02 19:06:47 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 13:45:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,9 +19,7 @@ t_execmap g_execmap[] = {TK_SEMI, &exec_semi}, {TK_AMP, &exec_ampersand}, {TK_PIPE, &exec_pipe}, - {TK_LESS, &exec_less}, - {TK_GREAT, &exec_great}, - {TK_DGREAT, &exec_dgreat}, + {TK_REDIR, &exec_redir}, {TK_COMMAND | TK_SUBSHELL, &exec_command}, {0, 0}, }; diff --git a/42sh/src/exec/process_do_redirection.c b/42sh/src/exec/process_do_redirection.c new file mode 100644 index 00000000..b0ce8954 --- /dev/null +++ b/42sh/src/exec/process_do_redirection.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* process_do_redirection.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/03 13:46:40 by jhalford #+# #+# */ +/* Updated: 2017/02/03 14:10:01 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void process_do_redirection(t_redir *redir) +{ + int fdin; + int fdout; + + if (redir->type & TK_GREAT) + { + fdin = redir->n; + fdout = open(redir->word.word, O_WRONLY | O_TRUNC | O_CREAT, 0644); + DG("opened [%s] in fd[%i]", redir->word.word, fdout); + } + else if (redir->type & TK_DGREAT) + { + fdin = redir->n; + fdout = open(redir->word.word, O_WRONLY | O_APPEND | O_CREAT, 0644); + } + else if (redir->type & TK_LESS) + { + fdin = open(redir->word.word, O_WRONLY | O_TRUNC | O_CREAT, 0644); + fdout = redir->n; + } + else if (redir->type & (TK_LESSAND | TK_GREATAND)) + { + if (redir->close) + { + DG("gonna close(%i)", redir->n); + close(redir->n); + return ; + } + else + { + fdin = redir->type & TK_LESSAND ? redir->word.fd : redir->n; + fdout = redir->type & TK_LESSAND ? redir->n : redir->word.fd; + } + } + else + { + DG("redir->type not well set !"); + return ; + } + DG("gonna dup2(%i,%i)", fdout, fdin); + dup2(fdout, fdin); + close(fdout); +} diff --git a/42sh/src/exec/process_redirect.c b/42sh/src/exec/process_redirect.c index 77830bb0..4f3396ca 100644 --- a/42sh/src/exec/process_redirect.c +++ b/42sh/src/exec/process_redirect.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/29 16:04:18 by jhalford #+# #+# */ -/* Updated: 2017/01/10 14:31:15 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 13:46:30 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,14 @@ int process_redirect(t_process *p) { + t_list *redirs; + + redirs = p->redirs; + while (redirs) + { + process_do_redirection(redirs->content); + redirs = redirs->next; + } if (p->toclose != STDIN) close(p->toclose); if (p->fdin != STDIN) diff --git a/42sh/src/job-control/process_free.c b/42sh/src/job-control/process_free.c index f0f77c0a..ce23bf71 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/02/02 15:45:25 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 13:59:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,5 +20,6 @@ void process_free(void *content, size_t content_size) p = content; ft_strdel(&p->path); ft_sstrfree(p->av); + ft_lstdel(&p->redirs, ft_lst_cfree); free(p); } diff --git a/42sh/src/lexer/ft_tokenize.c b/42sh/src/lexer/ft_tokenize.c index a2334864..cfdfe43d 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: 2017/02/02 15:34:45 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 13:26:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 5a0791ae..5b9a0fd9 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/01/19 16:26:35 by gwojda ### ########.fr */ +/* Updated: 2017/02/03 13:44:30 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,6 +29,7 @@ int data_init(void) data->exec.process.fdout = STDOUT; data->exec.process.pid = 0; data->exec.process.attributes = PROCESS_PIPESTART | PROCESS_PIPEEND; + data->exec.process.redirs = NULL; data->exec.aol_status = NULL; data->exec.aol_search = 0; diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index fbcb39a2..ea512479 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */ -/* Updated: 2017/02/02 19:02:32 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 13:43:00 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_great.c b/42sh/src/parser/parse_great.c index f81c0de4..dffaf1a7 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: 2017/02/02 19:05:52 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 14:07:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_greatand.c b/42sh/src/parser/parse_greatand.c index b4d6f657..7751fb83 100644 --- a/42sh/src/parser/parse_greatand.c +++ b/42sh/src/parser/parse_greatand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2016/12/01 16:37:58 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 14:09:50 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,7 @@ int parse_greatand(t_btree **ast, t_list **start, t_list **lst) node->type = TK_GREATAND; tok = (*lst)->content; and = ft_strchr(tok->data, '&'); + node->data.redir.n = *tok->data == '>' ? 1 : ft_atoi(tok->data); node->data.redir.word.fd = ft_atoi(and + 1); node->data.redir.close = tok->data[ft_strlen(tok->data) - 1] == '-' ? 1 : 0; diff --git a/42sh/src/parser/parse_lessand.c b/42sh/src/parser/parse_lessand.c index 8f5b0b9b..2a047610 100644 --- a/42sh/src/parser/parse_lessand.c +++ b/42sh/src/parser/parse_lessand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2016/12/01 16:37:54 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 14:07:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,7 @@ int parse_lessand(t_btree **ast, t_list **start, t_list **lst) node->type = TK_LESSAND; tok = (*lst)->content; and = ft_strchr(tok->data, '&'); + node->data.redir.n = *tok->data == '<' ? 1 : ft_atoi(tok->data); node->data.redir.word.fd = ft_atoi(and + 1); node->data.redir.close = tok->data[ft_strlen(tok->data) - 1] == '-' ? 1 : 0; diff --git a/42sh/src/parser/parse_redir.c b/42sh/src/parser/parse_redir.c index e937b1a3..95625fd0 100644 --- a/42sh/src/parser/parse_redir.c +++ b/42sh/src/parser/parse_redir.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/02 18:58:27 by jhalford #+# #+# */ -/* Updated: 2017/02/02 19:04:32 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 13:58:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */