pipes and redirection working

This commit is contained in:
Jack Halford 2016-11-29 20:30:36 +01:00
parent 1ac56a21e8
commit 54a6cdecb0
16 changed files with 95 additions and 45 deletions

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */
/* Updated: 2016/11/28 18:26:20 by jhalford ### ########.fr */
/* Updated: 2016/11/29 19:22:30 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -36,6 +36,9 @@ int exec_great(t_btree *ast, t_data *data);
int exec_dgreat(t_btree *ast, t_data *data);
int exec_command(t_btree *ast, t_data *data);
void fd_redirect(t_data *data);
void fd_reset(t_data *data);
int ft_cmd_process(char **argv, t_data *data);
int ft_cmd_exec(char *execpath, char **argv, t_data *data);

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
/* Updated: 2016/11/29 15:32:43 by jhalford ### ########.fr */
/* Updated: 2016/11/29 20:08:37 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -48,6 +48,8 @@ struct s_data
t_list *qstack;
int fdin;
int fdout;
int save_in;
int save_out;
};
extern t_stof g_builtins[];

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:21:34 by jhalford #+# #+# */
/* Updated: 2016/11/28 14:27:36 by jhalford ### ########.fr */
/* Updated: 2016/11/29 19:45:25 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -32,8 +32,10 @@ int ft_builtin(char **av, t_data *data)
{
if (ft_strcmp(g_builtin[i].name, *av) == 0)
{
fd_redirect(data);
ret = (g_builtin[i].f)(av, data);
builtin_setenv((char*[3]){"?", ft_itoa(ret)}, data);
fd_reset(data);
return (1);
}
i++;

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */
/* Updated: 2016/11/28 18:34:13 by jhalford ### ########.fr */
/* Updated: 2016/11/29 20:29:12 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 18:15:13 by jhalford #+# #+# */
/* Updated: 2016/11/28 18:36:11 by jhalford ### ########.fr */
/* Updated: 2016/11/29 20:29:10 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:27:51 by jhalford #+# #+# */
/* Updated: 2016/11/28 18:35:15 by jhalford ### ########.fr */
/* Updated: 2016/11/29 20:26:40 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:27:08 by jhalford #+# #+# */
/* Updated: 2016/11/28 18:19:26 by jhalford ### ########.fr */
/* Updated: 2016/11/29 20:29:01 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,8 +19,10 @@ int exec_less(t_btree *ast, t_data *data)
node = ast->item;
fd = open(node->u_data.redir.u_word.word, O_RDONLY);
dup2(fd, node->u_data.redir.n);
close(fd);
data->fdin = fd;
return (ft_exec(ast->left, data));
ft_exec(ast->left, data);
data->fdin = STDIN;
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 21:13:23 by jhalford #+# #+# */
/* Updated: 2016/11/28 18:10:26 by jhalford ### ########.fr */
/* Updated: 2016/11/29 19:19:50 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,6 +22,8 @@ int exec_pipe(t_btree *ast, t_data *data)
ft_exec(ast->left, data);
if (data->fdout != STDOUT)
close(data->fdout);
data->fdout = STDOUT;
data->fdin = fds[PIPE_READ];

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_redirect.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/29 16:04:18 by jhalford #+# #+# */
/* Updated: 2016/11/29 20:21:17 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void fd_redirect(t_data *data)
{
if (data->fdin != STDIN)
{
/* ft_dprintf(2, "redirecting input: %i->STDIN\n", data->fdin); */
dup2(data->fdin, STDIN);
close(data->fdin);
}
if (data->fdout != STDOUT)
{
/* ft_dprintf(2, "redirecting output: %i->STDOUT\n", data->fdout); */
dup2(data->fdout, STDOUT);
close(data->fdout);
}
}

21
42sh/src/exec/fd_reset.c Normal file
View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fd_reset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/29 19:20:05 by jhalford #+# #+# */
/* Updated: 2016/11/29 19:47:01 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void fd_reset(t_data *data)
{
dup2(data->save_in, STDIN);
dup2(data->save_out, STDOUT);
data->fdin = STDIN;
data->fdout = STDOUT;
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 21:13:18 by jhalford #+# #+# */
/* Updated: 2016/11/28 18:32:49 by jhalford ### ########.fr */
/* Updated: 2016/11/29 20:22:38 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -26,7 +26,7 @@ int ft_cmd_process(char **argv, t_data *data)
execpath = argv[0];
else if (!(execpath = ft_findexec(path, argv[0])))
{
ft_dprintf(2, "minishell: command not found: %s\n", argv[0]);
ft_dprintf(2, "%s: command not found: %s\n", SHELL_NAME, argv[0]);
return (-1);
}
return (ft_cmd_exec(execpath, argv, data));
@ -40,35 +40,22 @@ int ft_cmd_exec(char *execpath, char **argv, t_data *data)
if (access(execpath, X_OK) == -1)
{
ft_dprintf(2, "minishell: permission denied: %s\n", argv[0]);
ft_dprintf(2, "%s: permission denied: %s\n", SHELL_NAME, argv[0]);
return (-1);
}
ft_dprintf(2, "gonna fork, in=%i, out=%i\n", data->fdin, data->fdout);
/* ft_dprintf(2, "gonna fork, in=%i, out=%i\n", data->fdin, data->fdout); */
if ((pid = fork()) == -1)
return (-1);
else if (pid == 0)
{
if (data->fdin != STDIN)
{
dup2(data->fdin, STDIN);
close(data->fdin);
}
if (data->fdout != STDOUT)
{
dup2(data->fdout, STDOUT);
close(data->fdout);
}
fd_redirect(data);
environ = ft_sstrdup(data->env);
execve(execpath, argv, environ);
}
else
{
g_pid = pid;
if (data->fdin != STDIN)
close(data->fdin);
if (data->fdout != STDOUT)
close(data->fdout);
else
if (data->fdout == STDOUT)
{
waitpid(pid, &status, 0);
builtin_setenv((char*[3]){"?", ft_itoa(status)}, data);

View file

@ -5,8 +5,8 @@
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 16:56:11 by jhalford #+# #+# */
/* Updated: 2016/11/29 15:38:51 by jhalford ### ########.fr */
/* Created: 2016/11/29 15:56:40 by jhalford #+# #+# */
/* Updated: 2016/11/29 15:57:46 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ft_key_del.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:44:26 by jhalford #+# #+# */
/* Updated: 2016/11/29 13:46:30 by jhalford ### ########.fr */
/* Created: 2016/11/29 15:57:43 by jhalford #+# #+# */
/* Updated: 2016/11/29 15:57:44 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ft_key_enter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:44:30 by jhalford #+# #+# */
/* Updated: 2016/11/29 13:50:20 by jhalford ### ########.fr */
/* Created: 2016/11/29 15:56:36 by jhalford #+# #+# */
/* Updated: 2016/11/29 15:56:36 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */
/* Updated: 2016/11/28 19:28:22 by jhalford ### ########.fr */
/* Updated: 2016/11/29 19:46:07 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,6 +20,8 @@ int data_init(t_data *data)
data->history = NULL;
data->fdin = STDIN;
data->fdout = STDOUT;
data->save_in = dup(STDIN);
data->save_out = dup(STDOUT);
if (!(data->history = ft_dlstnew(NULL, 0)))
return (-1);
return (0);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 21:13:34 by jhalford #+# #+# */
/* Updated: 2016/11/28 19:28:03 by jhalford ### ########.fr */
/* Updated: 2016/11/29 20:08:54 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,18 +27,18 @@ int main(void)
{
if (ft_interactive_sh(&data))
return (1);
ft_dprintf(STDERR, "command='%s'\n", data.input);
/* ft_dprintf(STDERR, "command='%s'\n", data.input); */
if (ft_tokenize(&token, data.input, DEFAULT))
return (1);
if (!token)
continue ;
token_print(token);
/* token_print(token); */
ast = NULL;
if (ft_parse(&ast, &token))
return (1);
btree_print(STDERR, ast, &ft_putast);
ft_dprintf(STDERR, "\n--- INFIX BREAKDOWN ---\n");
btree_apply_infix(ast, &ft_putast2);
/* btree_print(STDERR, ast, &ft_putast); */
/* ft_dprintf(STDERR, "\n--- INFIX BREAKDOWN ---\n"); */
/* btree_apply_infix(ast, &ft_putast2); */
/* ft_lstdel(&token, &token_free); */
token = NULL;
if (ft_exec(ast, &data))