redirection now works with builtins!

This commit is contained in:
Jack Halford 2017-02-07 17:57:52 +01:00
parent 060800769b
commit a01b7b76ab
20 changed files with 128 additions and 69 deletions

View file

@ -6,14 +6,14 @@
# By: wescande <wescande@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2016/08/29 21:32:58 by wescande #+# #+# #
# Updated: 2017/02/06 21:49:51 by jhalford ### ########.fr #
# Updated: 2017/02/07 16:11:13 by jhalford ### ########.fr #
# #
# **************************************************************************** #
NAME = 42sh
CC = gcc
FLAGS = -Wall -Wextra -Werror -g
FLAGS = -Wall -Wextra -Werror
D_FLAGS = -g
DELTA = $$(echo "$$(tput cols)-47"|bc)
@ -54,6 +54,7 @@ completion/c_terminal.c\
completion/completion.c\
exec/ast_free.c\
exec/bad_fd.c\
exec/close_fdsave.c\
exec/exec_ampersand.c\
exec/exec_and_if.c\
exec/exec_command.c\
@ -67,6 +68,7 @@ exec/ft_findexec.c\
exec/launch_process.c\
exec/process_redirect.c\
exec/process_reset.c\
exec/process_resetfds.c\
exec/process_setexec.c\
exec/process_setgroup.c\
exec/process_setsig.c\

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */
/* Updated: 2017/02/07 12:09:05 by jhalford ### ########.fr */
/* Updated: 2017/02/07 17:49:55 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -59,6 +59,9 @@ struct s_exec
int aol_search;
t_job job;
t_process process;
int fd0save;
int fd1save;
int fd2save;
};
struct s_execmap
@ -93,10 +96,12 @@ int process_setexec(t_type type, t_process *p);
int process_setgroup(t_process *p, pid_t pid);
void process_setsig(void);
void process_free(void *content, size_t content_size);
void process_reset(void);
void process_reset(t_process *p);
void process_resetfds(void);
void close_fdsave(void);
int fd_is_valid(int fd);
void bad_fd(int fd);
int bad_fd(int fd);
int process_redirect(t_process *p);
int redirect_great(t_redir *redir);
int redirect_less(t_redir *redir);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */
/* Updated: 2017/02/07 12:56:46 by jhalford ### ########.fr */
/* Updated: 2017/02/07 15:25:04 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

@ -1 +1 @@
Subproject commit 92902c1c6539152d9daadaf2a018b2d344f1224e
Subproject commit 6437a0545bf0eec54bc3ac4fa4bc24d83efcf384

View file

@ -6,15 +6,15 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/06 22:32:43 by jhalford #+# #+# */
/* Updated: 2017/02/06 22:34:21 by jhalford ### ########.fr */
/* Updated: 2017/02/07 16:02:12 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void bad_fd(int fd)
int bad_fd(int fd)
{
ft_dprintf(2, "{red}%s: %i: Bad file descriptor{eoc}\n",
SHELL_NAME, fd);
exit(1);
return (1);
}

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* close_fdsave.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/07 17:45:23 by jhalford #+# #+# */
/* Updated: 2017/02/07 17:54:07 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
void close_fdsave(void)
{
t_exec *exec;
exec = &data_singleton()->exec;
close(exec->fd0save);
close(exec->fd1save);
close(exec->fd2save);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */
/* Updated: 2017/02/07 12:14:31 by jhalford ### ########.fr */
/* Updated: 2017/02/07 17:44:40 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -65,10 +65,7 @@ int exec_command(t_btree **ast)
job->pgid = 0;
}
}
p->av = NULL;
p->pid = 0;
p->attributes &= ~(PROCESS_STATE_MASK | PROCESS_TYPE_MASK);
p->redirs = NULL;
process_reset(p);
btree_delone(ast, &ast_free);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:27:51 by jhalford #+# #+# */
/* Updated: 2017/02/06 16:12:14 by jhalford ### ########.fr */
/* Updated: 2017/02/07 15:24:11 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 14:20:45 by jhalford #+# #+# */
/* Updated: 2017/02/07 12:11:34 by jhalford ### ########.fr */
/* Updated: 2017/02/07 17:54:12 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,7 +24,11 @@ int launch_process(t_process *p)
set_exitstatus(127, 1);
}
else if (p->attributes & PROCESS_BUILTIN && IS_PIPESINGLE(p->attributes))
{
if (process_redirect(p))
return (1);
set_exitstatus((*p->execf)(p->path, p->av, data_singleton()->env), 1);
}
else
{
p->attributes &= ~PROCESS_STATE_MASK;
@ -39,9 +43,11 @@ int launch_process(t_process *p)
pid = fork();
if (pid == 0)
{
close_fdsave();
process_setgroup(p, 0);
process_setsig();
process_redirect(p);
if (process_redirect(p))
exit (1);
(*p->execf)(p->path, p->av, data_singleton()->env);
exit(43);
}
@ -52,7 +58,7 @@ int launch_process(t_process *p)
return (0);
}
else if (pid == -1)
ft_dprintf(2, "{red}internal fork error{eoc}\n");
ft_dprintf(2, "{red}%s: internal fork error{eoc}\n", SHELL_NAME);
}
return (1);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/29 16:04:18 by jhalford #+# #+# */
/* Updated: 2017/02/07 12:01:06 by jhalford ### ########.fr */
/* Updated: 2017/02/07 17:36:46 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -32,18 +32,19 @@ int process_redirect(t_process *p)
redirs = p->redirs;
while (redirs)
{
i = 0;
redir = redirs->content;
if (redir->n > 9)
{
bad_fd(redir->n);
exit(1);
}
i = 0;
while (g_redirmap[i].type)
{
if (g_redirmap[i].type == redir->type)
{
(g_redirmap[i].f)(redir);
if ((g_redirmap[i].f)(redir))
return (1);
break ;
}
i++;
@ -53,14 +54,8 @@ int process_redirect(t_process *p)
if (p->toclose != STDIN)
close(p->toclose);
if (p->fdin != STDIN)
{
dup2(p->fdin, STDIN);
close(p->fdin);
}
dup2_close(p->fdin, STDIN);
if (p->fdout != STDOUT)
{
dup2(p->fdout, STDOUT);
close(p->fdout);
}
dup2_close(p->fdout, STDOUT);
return (0);
}

View file

@ -1,8 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* process_reset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/07 17:44:22 by jhalford #+# #+# */
/* Updated: 2017/02/07 17:48:22 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
void process_reset(void)
void process_reset(t_process *p)
{
t_data *data;
data = data_singleton();
process_resetfds();
p->av = NULL;
p->pid = 0;
p->redirs = NULL;
p->attributes &= ~(PROCESS_STATE_MASK | PROCESS_TYPE_MASK);
}

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* process_resetfds.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/07 17:39:14 by jhalford #+# #+# */
/* Updated: 2017/02/07 17:50:52 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void process_resetfds(void)
{
t_exec *exec;
exec = &data_singleton()->exec;
dup2(exec->fd0save, 0);
dup2(exec->fd1save, 1);
dup2(exec->fd2save, 2);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/06 22:12:31 by jhalford #+# #+# */
/* Updated: 2017/02/07 12:01:45 by jhalford ### ########.fr */
/* Updated: 2017/02/07 17:54:40 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,26 +17,20 @@ int redirect_greatand(t_redir *redir)
int fdold;
int fdnew;
if (redir->word.fd == redir->n)
return (0);
if (redir->word.fd > 9)
bad_fd(redir->word.fd);
if (redir->close)
{
close(redir->n);
return (1);
}
else
{
fdold = redir->word.fd;
fdnew = redir->n;
return (0);
}
if (redir->word.fd == redir->n)
return (0);
if (redir->word.fd > 9)
return (bad_fd(redir->word.fd));
fdold = redir->word.fd;
fdnew = redir->n;
if (fd_is_valid(fdold))
{
dup2(fdold, fdnew);
close(fdold);
}
dup2_close(fdold, fdnew);
else
bad_fd(fdold);
return (bad_fd(fdold));
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/06 22:09:53 by jhalford #+# #+# */
/* Updated: 2017/02/06 22:38:46 by jhalford ### ########.fr */
/* Updated: 2017/02/07 16:05:09 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/06 22:11:18 by jhalford #+# #+# */
/* Updated: 2017/02/07 12:01:56 by jhalford ### ########.fr */
/* Updated: 2017/02/07 17:54:57 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,26 +17,23 @@ int redirect_lessand(t_redir *redir)
int fdold;
int fdnew;
if (redir->word.fd == redir->n)
return (0);
if (redir->word.fd > 9)
bad_fd(redir->word.fd);
if (redir->close)
{
close(redir->n);
return (1);
}
else
{
fdold = redir->word.fd;
fdnew = redir->n;
return (0);
}
if (redir->word.fd == redir->n)
return (0);
if (redir->word.fd > 9)
return (bad_fd(redir->word.fd));
fdold = redir->word.fd;
fdnew = redir->n;
if (fd_is_valid(fdold))
{
dup2(fdold, fdnew);
close(fdold);
}
else
bad_fd(fdold);
return (bad_fd(fdold));
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */
/* Updated: 2017/01/08 15:58:20 by jhalford ### ########.fr */
/* Updated: 2017/02/07 16:04:57 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,7 +27,7 @@ void set_exitstatus(int status, int override)
exitval = 128 + WTERMSIG(status);
else
{
DG("fail: process was not exited nor signaled.");
DG("%s: process was not exited nor signaled.", SHELL_NAME);
return ;
}
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */
/* Updated: 2017/02/03 15:25:55 by gwojda ### ########.fr */
/* Updated: 2017/02/07 17:23:02 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -31,6 +31,9 @@ int data_init(void)
data->exec.process.pid = 0;
data->exec.process.attributes = PROCESS_PIPESTART | PROCESS_PIPEEND;
data->exec.process.redirs = NULL;
data->exec.fd0save = dup(0);
data->exec.fd1save = dup(1);
data->exec.fd2save = dup(2);
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/06 16:46:48 by jhalford ### ########.fr */
/* Updated: 2017/02/07 17:18:21 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/02/06 16:29:54 by jhalford ### ########.fr */
/* Updated: 2017/02/07 15:26:25 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/02/06 16:30:41 by jhalford ### ########.fr */
/* Updated: 2017/02/07 15:32:44 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */