new architecture for redirection inside of fork(), chained redirects dont work properly yet

This commit is contained in:
Jack Halford 2017-02-03 14:11:34 +01:00
parent 985745f881
commit 62be4bc470
20 changed files with 118 additions and 111 deletions

View file

@ -44,15 +44,14 @@ exec/ast_free.c\
exec/exec_ampersand.c\ exec/exec_ampersand.c\
exec/exec_and_if.c\ exec/exec_and_if.c\
exec/exec_command.c\ exec/exec_command.c\
exec/exec_dgreat.c\
exec/exec_great.c\
exec/exec_less.c\
exec/exec_or_if.c\ exec/exec_or_if.c\
exec/exec_pipe.c\ exec/exec_pipe.c\
exec/exec_redir.c\
exec/exec_semi.c\ exec/exec_semi.c\
exec/ft_exec.c\ exec/ft_exec.c\
exec/ft_findexec.c\ exec/ft_findexec.c\
exec/launch_process.c\ exec/launch_process.c\
exec/process_do_redirection.c\
exec/process_redirect.c\ exec/process_redirect.c\
exec/process_reset.c\ exec/process_reset.c\
exec/process_setexec.c\ exec/process_setexec.c\

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:29:56 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; pid_t pid;
int fdin; int fdin;
int fdout; int fdout;
t_list *redirs;
int toclose; int toclose;
int status; int status;
t_flag attributes; t_flag attributes;
@ -77,16 +78,14 @@ int exec_ampersand(t_btree **ast);
int exec_or_if(t_btree **ast); int exec_or_if(t_btree **ast);
int exec_and_if(t_btree **ast); int exec_and_if(t_btree **ast);
int exec_pipe(t_btree **ast); int exec_pipe(t_btree **ast);
int exec_redir(t_btree **ast);
int exec_less(t_btree **ast);
int exec_great(t_btree **ast);
int exec_dgreat(t_btree **ast);
int exec_command(t_btree **ast); int exec_command(t_btree **ast);
int launch_process(t_process *p); int launch_process(t_process *p);
int process_setexec(t_type type, t_process *p); int process_setexec(t_type type, t_process *p);
int process_setgroup(t_process *p, pid_t pid); int process_setgroup(t_process *p, pid_t pid);
int process_redirect(t_process *p); int process_redirect(t_process *p);
void process_do_redirection(t_redir *redir);
void process_setsig(void); void process_setsig(void);
void process_free(void *content, size_t content_size); void process_free(void *content, size_t content_size);
void process_reset(void); void process_reset(void);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:50 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:54 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" # 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 struct s_parser
{ {
t_type type; t_type type;
@ -37,9 +29,10 @@ union u_word
struct s_redir struct s_redir
{ {
t_flag type;
int n; int n;
int close;
t_word word; t_word word;
int close;
}; };
union u_astdata union u_astdata

View file

@ -6,29 +6,33 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 17:11:48 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 #ifndef TYPES_H
# define TYPES_H # define TYPES_H
typedef struct s_data t_data;
typedef long long t_type; typedef long long t_type;
typedef long long t_flag;
typedef struct s_line t_line; typedef struct s_line t_line;
typedef struct s_comp t_comp; typedef struct s_comp t_comp;
typedef struct s_exec t_exec; typedef struct s_exec t_exec;
typedef struct s_jobc t_jobc; typedef struct s_jobc t_jobc;
typedef enum e_mode t_mode; typedef enum e_mode t_mode;
typedef struct s_data t_data; typedef struct s_parser t_parser;
typedef enum e_qstate t_qstate; 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_job t_job;
typedef struct s_jobc t_jobc;
typedef struct s_execmap t_execmap; typedef struct s_execmap t_execmap;
typedef struct s_process t_process; typedef struct s_process t_process;
typedef long long t_type;
typedef long long t_flag;
typedef int (t_execf)(const char *path, char *const argv[], char *const envp[]); typedef int (t_execf)(const char *path, char *const argv[], char *const envp[]);
t_data *data_singleton(); t_data *data_singleton();

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:28:14 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->av = NULL;
p->pid = 0; p->pid = 0;
p->attributes &= ~(PROCESS_STATE_MASK | PROCESS_TYPE_MASK); p->attributes &= ~(PROCESS_STATE_MASK | PROCESS_TYPE_MASK);
p->redirs = NULL;
btree_delone(ast, &ast_free); btree_delone(ast, &ast_free);
return (0); return (0);
} }

View file

@ -1,27 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_dgreat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -1,30 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_less.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -1,27 +1,27 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* exec_great.c :+: :+: :+: */ /* exec_redir.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:27:51 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" #include "exec.h"
int exec_great(t_btree **ast) int exec_redir(t_btree **ast)
{ {
t_astnode *node; t_astnode *node;
int fd; t_process *p;
p = &data_singleton()->exec.process;
node = (*ast)->item; node = (*ast)->item;
fd = open(node->data.redir.word.word, O_WRONLY | O_TRUNC | O_CREAT, 0644); node->data.redir.type = node->type;
data_singleton()->exec.process.fdout = fd; ft_lsteadd(&p->redirs, ft_lstnew(&node->data.redir,sizeof(node->data.redir)));
ft_exec(&(*ast)->left); ft_exec(&(*ast)->left);
data_singleton()->exec.process.fdout = STDOUT;
btree_delone(ast, &ast_free); btree_delone(ast, &ast_free);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:30:32 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_SEMI, &exec_semi},
{TK_AMP, &exec_ampersand}, {TK_AMP, &exec_ampersand},
{TK_PIPE, &exec_pipe}, {TK_PIPE, &exec_pipe},
{TK_LESS, &exec_less}, {TK_REDIR, &exec_redir},
{TK_GREAT, &exec_great},
{TK_DGREAT, &exec_dgreat},
{TK_COMMAND | TK_SUBSHELL, &exec_command}, {TK_COMMAND | TK_SUBSHELL, &exec_command},
{0, 0}, {0, 0},
}; };

View file

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* process_do_redirection.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/29 16:04:18 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) 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) if (p->toclose != STDIN)
close(p->toclose); close(p->toclose);
if (p->fdin != STDIN) if (p->fdin != STDIN)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 12:41:11 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; p = content;
ft_strdel(&p->path); ft_strdel(&p->path);
ft_sstrfree(p->av); ft_sstrfree(p->av);
ft_lstdel(&p->redirs, ft_lst_cfree);
free(p); free(p);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:37:11 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 19:26:32 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.fdout = STDOUT;
data->exec.process.pid = 0; data->exec.process.pid = 0;
data->exec.process.attributes = PROCESS_PIPESTART | PROCESS_PIPEEND; data->exec.process.attributes = PROCESS_PIPESTART | PROCESS_PIPEEND;
data->exec.process.redirs = NULL;
data->exec.aol_status = NULL; data->exec.aol_status = NULL;
data->exec.aol_search = 0; data->exec.aol_search = 0;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 17:14:58 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 12:49:45 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 12:49:45 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; node->type = TK_GREATAND;
tok = (*lst)->content; tok = (*lst)->content;
and = ft_strchr(tok->data, '&'); 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.word.fd = ft_atoi(and + 1);
node->data.redir.close = node->data.redir.close =
tok->data[ft_strlen(tok->data) - 1] == '-' ? 1 : 0; tok->data[ft_strlen(tok->data) - 1] == '-' ? 1 : 0;

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 12:49:45 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; node->type = TK_LESSAND;
tok = (*lst)->content; tok = (*lst)->content;
and = ft_strchr(tok->data, '&'); 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.word.fd = ft_atoi(and + 1);
node->data.redir.close = node->data.redir.close =
tok->data[ft_strlen(tok->data) - 1] == '-' ? 1 : 0; tok->data[ft_strlen(tok->data) - 1] == '-' ? 1 : 0;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/02 18:58:27 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */