Merge branch 'redirection-fix'

This commit is contained in:
Jack Halford 2017-02-03 14:42:41 +01:00
commit da66e7bd79
22 changed files with 151 additions and 120 deletions

View file

@ -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\
@ -189,6 +188,7 @@ parser/parse_great.c\
parser/parse_greatand.c\
parser/parse_less.c\
parser/parse_lessand.c\
parser/parse_redir.c\
parser/parse_separator.c\
parser/parse_subshell.c\
parser/parse_word.c

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */
/* Updated: 2017/02/02 14:03:15 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
@ -60,6 +53,7 @@ extern t_parser g_parser[];
int ft_parse(t_btree **ast, t_list **token);
int parse_separator(t_btree **ast, t_list **start, t_list **lst);
int parse_redir(t_btree **ast, t_list **start, t_list **lst);
int parse_less(t_btree **ast, t_list **start, t_list **lst);
int parse_great(t_btree **ast, t_list **start, t_list **lst);
int parse_dless(t_btree **ast, t_list **start, t_list **lst);

View file

@ -6,29 +6,33 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 struct s_data t_data;
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_data t_data;
typedef enum e_qstate t_qstate;
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_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 int (t_execf)(const char *path, char *const argv[], char *const envp[]);
t_data *data_singleton();

View file

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

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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:27:51 by jhalford #+# #+# */
/* Updated: 2016/12/13 17:14:19 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);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */
/* Updated: 2017/02/02 15:04:50 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},
};

View file

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* process_do_redirection.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/03 13:46:40 by jhalford #+# #+# */
/* Updated: 2017/02/03 14:41:01 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int fd_is_valid(int fd)
{
return (fcntl(fd, F_GETFD) != -1 || errno != EBADF);
}
void process_do_redirection(t_redir *redir)
{
int fdin;
int fdout;
if (redir->type & (TK_GREAT | TK_DGREAT))
{
fdin = redir->n;
fdout = open(redir->word.word, O_WRONLY | O_CREAT
(redir->type & TK_GREAT) ? O_TRUNC : O_APPEND,
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)
{
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;
}
}
fd_is_valid(fdout) ? dup2(fdout, fdin) : close(fdin);
close(fdout);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)

View file

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

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
/* Updated: 2017/02/02 15:45:17 by jhalford ### ########.fr */
/* Updated: 2017/02/02 18:57:56 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */
/* Updated: 2017/02/02 15:45:33 by jhalford ### ########.fr */
/* Updated: 2017/02/03 13:43:00 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,12 +18,7 @@ t_parser g_parser[] =
{TK_AND_IF | TK_OR_IF, &parse_separator},
{TK_AMP, &parse_separator},
{TK_PIPE, &parse_separator},
{TK_LESS, &parse_less},
{TK_GREAT, &parse_great},
{TK_DLESS, &parse_dless},
{TK_DGREAT, &parse_dgreat},
{TK_LESSAND, &parse_lessand},
{TK_GREATAND, &parse_greatand},
{TK_REDIR, &parse_redir},
{TK_SUBSHELL, &parse_subshell},
{TK_WORD, &parse_word},
{0, 0},
@ -48,8 +43,7 @@ int ft_parse(t_btree **ast, t_list **start)
if ((lst = ft_lst_find(*start, &g_parser[i].type, &token_cmp_type)))
{
if (g_parser[i].f)
(*g_parser[i].f)(ast, start, &lst);
return (0);
return ((*g_parser[i].f)(ast, start, &lst));
}
i++;
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */
/* Updated: 2017/01/10 14:44:12 by jhalford ### ########.fr */
/* Updated: 2017/02/02 19:02:52 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */
/* Updated: 2017/01/10 14:45:50 by jhalford ### ########.fr */
/* Updated: 2017/02/03 14:07:03 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;

View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_redir.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/02 18:58:27 by jhalford #+# #+# */
/* Updated: 2017/02/03 13:58:57 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
int parse_redir(t_btree **ast, t_list **start, t_list **lst)
{
t_token *tok;
tok = (*lst)->content;
if (tok->type == TK_LESS)
return (parse_less(ast, start, lst));
else if (tok->type == TK_GREAT)
return (parse_great(ast, start, lst));
else if (tok->type == TK_DLESS)
return (parse_dless(ast, start, lst));
else if (tok->type == TK_DGREAT)
return (parse_dgreat(ast, start, lst));
else if (tok->type == TK_LESSAND)
return (parse_lessand(ast, start, lst));
else if (tok->type == TK_GREATAND)
return (parse_greatand(ast, start, lst));
else
return (-42);
}