redirections

This commit is contained in:
Loic Deridder
2025-02-06 10:54:55 +01:00
parent 362a63a836
commit 2bfdc3814f

View File

@@ -6,12 +6,13 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */ /* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/27 11:22:33 by lderidde #+# #+# */ /* Created: 2025/01/27 11:22:33 by lderidde #+# #+# */
/* Updated: 2025/02/05 15:39:07 by lderidde ### ########.fr */ /* Updated: 2025/02/06 10:51:20 by lderidde ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../../includes/minishell.h" #include "../../includes/minishell.h"
#include <unistd.h> // #include <fcntl.h>
// #include <unistd.h>
// #include <stdlib.h> // #include <stdlib.h>
// #include <fcntl.h> // #include <fcntl.h>
// #include <sys/wait.h> // #include <sys/wait.h>
@@ -54,7 +55,7 @@ void read_input(t_ast_n *node, int j)
{ {
if (c != '\n' && c != '\0') if (c != '\n' && c != '\0')
buf[i++] = c; buf[i++] = c;
r = read(0, &c, 1); r = read(STDIN_FILENO, &c, 1);
} }
buf[i] = '\0'; buf[i] = '\0';
if (ft_strncmp(buf, node->files[j], ft_strlen(node->files[j])) == 0) if (ft_strncmp(buf, node->files[j], ft_strlen(node->files[j])) == 0)
@@ -70,11 +71,15 @@ void read_input(t_ast_n *node, int j)
void here_doc(t_ast_n *node, int i) void here_doc(t_ast_n *node, int i)
{ {
pid_t pid; pid_t pid;
int fd;
pipe(node->fds); pipe(node->fds);
pid = fork(); pid = fork();
if (pid == 0) if (pid == 0)
{ {
fd = open("/dev/tty", O_RDONLY);
dup2(fd, STDIN_FILENO);
close(fd);
close(node->fds[0]); close(node->fds[0]);
while (1) while (1)
read_input(node, i); read_input(node, i);
@@ -92,7 +97,6 @@ void here_doc(t_ast_n *node, int i)
void save_stds(t_ast_n *node) void save_stds(t_ast_n *node)
{ {
ft_fprintf(2, "save fds\nstate: %d", node->state);
node->save_stdo = dup(STDOUT_FILENO); node->save_stdo = dup(STDOUT_FILENO);
node->save_stdi = dup(STDIN_FILENO); node->save_stdi = dup(STDIN_FILENO);
} }
@@ -127,7 +131,6 @@ void handle_redir(t_ast_n *node)
} }
} }
int is_builtin(char *str) int is_builtin(char *str)
{ {
if (ft_strncmp(str, "exit", 4) == 0) if (ft_strncmp(str, "exit", 4) == 0)
@@ -199,7 +202,7 @@ char *find_path(char *cmd, char **env)
char **paths; char **paths;
int i; int i;
if (access(cmd, F_OK) == 0) if (access(cmd, F_OK) == 0 && access(cmd, X_OK) == 0)
return (cmd); return (cmd);
i = 0; i = 0;
while (ft_strnstr(env[i], "PATH=", 5) == NULL) while (ft_strnstr(env[i], "PATH=", 5) == NULL)
@@ -246,7 +249,6 @@ int exec_scmd(t_ast_n *node)
pid_t pid; pid_t pid;
int status; int status;
// handle_redir(node);
if (is_builtin(node->cmd)) if (is_builtin(node->cmd))
return (exec_builtin(node)); return (exec_builtin(node));
else else
@@ -280,7 +282,6 @@ void exec_pcmd(t_ast_n *pcmd)
{ {
int ret; int ret;
handle_redir(pcmd);
if (is_builtin(pcmd->cmd)) if (is_builtin(pcmd->cmd))
{ {
ret = exec_builtin(pcmd); ret = exec_builtin(pcmd);
@@ -299,8 +300,7 @@ void exec_pchild(int *pipes, int index, t_ast_n *pcmd, int cmds)
dup2(pipes[1], STDOUT_FILENO); dup2(pipes[1], STDOUT_FILENO);
close(pipes[0]); close(pipes[0]);
close(pipes[1]); close(pipes[1]);
// if (pcmd->state == _CMD) handle_redir(pcmd);
// handle_redir(pcmd);
if (pcmd->state == _CMD) if (pcmd->state == _CMD)
exec_pcmd(pcmd); exec_pcmd(pcmd);
else if (pcmd->state == _SUBSH) else if (pcmd->state == _SUBSH)
@@ -328,7 +328,6 @@ int exec_pline(t_ast_n **pline)
int i; int i;
pid_t pid; pid_t pid;
pid_t last_pid; pid_t last_pid;
// int pipes[2];
i = -1; i = -1;
while (pline[++i]) while (pline[++i])
@@ -344,7 +343,7 @@ int exec_pline(t_ast_n **pline)
dup2(pline[i]->fds[0], STDIN_FILENO); dup2(pline[i]->fds[0], STDIN_FILENO);
close(pline[i]->fds[0]); close(pline[i]->fds[0]);
close(pline[i]->fds[1]); close(pline[i]->fds[1]);
if(i == count_cmds(pline) - 1) if (i == count_cmds(pline) - 1)
last_pid = pid; last_pid = pid;
} }
else if (pid < 0) else if (pid < 0)
@@ -358,7 +357,6 @@ int exec_shcmd(t_ast_n *node)
pid_t pid; pid_t pid;
int status; int status;
// handle_redir(node);
if (is_builtin(node->cmd)) if (is_builtin(node->cmd))
return (exec_builtin(node)); return (exec_builtin(node));
else else
@@ -384,8 +382,6 @@ int exec_subsh(t_ast_n *node);
int execute_shcommand(t_ast_n *node) int execute_shcommand(t_ast_n *node)
{ {
// int status;
if (node->state == _CMD) if (node->state == _CMD)
handle_redir(node); handle_redir(node);
if (node->state == _CMD) if (node->state == _CMD)
@@ -395,14 +391,12 @@ int execute_shcommand(t_ast_n *node)
node->msh->ex_code = execute_shcommand(node->left); node->msh->ex_code = execute_shcommand(node->left);
if (node->msh->ex_code == 0) if (node->msh->ex_code == 0)
node->msh->ex_code = execute_shcommand(node->right); node->msh->ex_code = execute_shcommand(node->right);
// return (node->msh->ex_code);
} }
else if (node->state == _OR) else if (node->state == _OR)
{ {
node->msh->ex_code = execute_shcommand(node->left); node->msh->ex_code = execute_shcommand(node->left);
if (node->msh->ex_code != 0) if (node->msh->ex_code != 0)
node->msh->ex_code = execute_shcommand(node->right); node->msh->ex_code = execute_shcommand(node->right);
// return (node->msh->ex_code);
} }
else if (node->state == _PLINE) else if (node->state == _PLINE)
node->msh->ex_code = exec_pline(node->pline); node->msh->ex_code = exec_pline(node->pline);
@@ -423,14 +417,12 @@ int exec_subsh(t_ast_n *node)
if (pid == 0) if (pid == 0)
{ {
handle_redir(node->parent); handle_redir(node->parent);
// node->save_std = dup(STDOUT_FILENO);
ret = execute_shcommand(node); ret = execute_shcommand(node);
reset_redir(node->parent); reset_redir(node->parent);
exit(ret); exit(ret);
} }
else if (pid > 0) else if (pid > 0)
{ {
// reset_redir(node->parent);
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
if (WIFEXITED(status) && node->parent->state != _PLINE) if (WIFEXITED(status) && node->parent->state != _PLINE)
return (WEXITSTATUS(status)); return (WEXITSTATUS(status));
@@ -446,8 +438,6 @@ int exec_subsh(t_ast_n *node)
int execute_command(t_ast_n *node) int execute_command(t_ast_n *node)
{ {
// int status;
if (node->state == _CMD) if (node->state == _CMD)
handle_redir(node); handle_redir(node);
if (node->state == _CMD) if (node->state == _CMD)
@@ -457,14 +447,12 @@ int execute_command(t_ast_n *node)
node->msh->ex_code = execute_command(node->left); node->msh->ex_code = execute_command(node->left);
if (node->msh->ex_code == 0) if (node->msh->ex_code == 0)
node->msh->ex_code = execute_command(node->right); node->msh->ex_code = execute_command(node->right);
// return (node->msh->ex_code);
} }
else if (node->state == _OR) else if (node->state == _OR)
{ {
node->msh->ex_code = execute_command(node->left); node->msh->ex_code = execute_command(node->left);
if (node->msh->ex_code != 0) if (node->msh->ex_code != 0)
node->msh->ex_code = execute_command(node->right); node->msh->ex_code = execute_command(node->right);
// return (node->msh->ex_code);
} }
else if (node->state == _PLINE) else if (node->state == _PLINE)
node->msh->ex_code = exec_pline(node->pline); node->msh->ex_code = exec_pline(node->pline);