err stdin

This commit is contained in:
Loic Deridder
2025-01-29 13:34:27 +01:00
parent 9bce0bf9cb
commit b56f1a7d3c
3 changed files with 138 additions and 47 deletions

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */ /* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 08:22:16 by lderidde #+# #+# */ /* Created: 2025/01/24 08:22:16 by lderidde #+# #+# */
/* Updated: 2025/01/28 13:31:27 by lderidde ### ########.fr */ /* Updated: 2025/01/29 13:32:50 by lderidde ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -84,20 +84,22 @@ t_ast_n *return_hardcode_ast(char **envp)
head = created_ast_n(_AND, NULL, NULL); head = created_ast_n(_AND, NULL, NULL);
head->env = init_env(envp); head->env = init_env(envp);
head->left = created_ast_n(_CMD, head, head); head->left = created_ast_n(_CMD, head, head);
setup_cmd(head->left, "cd", "cd srcs/../.."); setup_cmd(head->left, "ls", "ls");
head->right = created_ast_n(_AND, head, head); // head->right = created_ast_n(_AND, head, head);
head->right->left = created_ast_n(_CMD, head->right, head); // head->right->left = created_ast_n(_CMD, head->right, head);
setup_cmd(head->right->left, "echo", "echo $PWD"); // setup_cmd(head->right->left, "echo", "echo $PWD");
head->right->right = created_ast_n(_CMD, head->right, head); // head->right->right = created_ast_n(_CMD, head->right, head);
setup_cmd(head->right->right, "ls", "ls -l"); // setup_cmd(head->right->right, "ls", "ls -l");
// head->right = created_ast_n(_PLINE, head, head); head->right = created_ast_n(_PLINE, head, head);
// head->right->pline = malloc(sizeof(t_ast_n *) * 4); head->right->pline = malloc(sizeof(t_ast_n *) * 5);
// head->right->pline[0] = created_ast_n(_CMD, head->right, head); head->right->pline[0] = created_ast_n(_CMD, head->right, head);
// setup_cmd(head->right->pline[0], "ls", "ls"); setup_cmd(head->right->pline[0], "ls", "ls");
// head->right->pline[1] = created_ast_n(_CMD, head->right, head); head->right->pline[1] = created_ast_n(_CMD, head->right, head);
// setup_cmd(head->right->pline[1], "cat", "cat -e"); setup_cmd(head->right->pline[1], "echo", "echo test test2");
// head->right->pline[2] = created_ast_n(_CMD, head->right, head); head->right->pline[2] = created_ast_n(_CMD, head->right, head);
// setup_cmd(head->right->pline[2], "wc", "wc -l"); setup_cmd(head->right->pline[2], "grep", "grep test");
// head->right->pline[3] = NULL; head->right->pline[3] = created_ast_n(_CMD, head->right, head);
setup_cmd(head->right->pline[3], "cat", "cat -e");
head->right->pline[4] = NULL;
return (head); return (head);
} }

View File

@@ -6,7 +6,7 @@
/* 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/01/28 15:39:37 by lderidde ### ########.fr */ /* Updated: 2025/01/29 13:33:57 by lderidde ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -95,6 +95,18 @@ int exec_builtin(t_ast_n *node)
return (builtin_export(node->args, node->head)); return (builtin_export(node->args, node->head));
} }
int count_cmds(t_ast_n **pline)
{
int i;
i = 0;
if (!pline || !*pline)
return (0);
while (pline[i])
i++;
return (i);
}
char *find_path(char *cmd, char **env) char *find_path(char *cmd, char **env)
{ {
char *tmp; char *tmp;
@@ -132,7 +144,7 @@ int exec(t_ast_n *node)
{ {
char *path; char *path;
handle_redir(node); // handle_redir(node);
path = find_path(node->cmd, node->head->env); path = find_path(node->cmd, node->head->env);
if (!path) if (!path)
return_error(node->cmd, "command not found", 127); return_error(node->cmd, "command not found", 127);
@@ -165,37 +177,114 @@ int exec_scmd(t_ast_n *node)
if (WIFEXITED(status)) if (WIFEXITED(status))
return (WEXITSTATUS(status)); return (WEXITSTATUS(status));
else else
return (-1); return (1);
} }
return (1); return (1);
} }
} }
// int exec_pipe(t_ast_n **pline) int *create_pipes(t_ast_n **pline)
// { {
// int i; int i;
// pid_t pid; int arg;
// int *pipes;
// i = -1;
// while (pline[++i]) arg = count_cmds(pline);
// { pipes = malloc(2 * (arg - 1) * sizeof(int));
// pipe(pline[i]->fds); if (!pipes)
// pid = fork(); return (NULL);
// if (pid == 0) i = -1;
// { while (++i < arg - 1)
// close(pline[i]->fds[0]); {
// dup2(pline[i]->fds[1], STDOUT_FILENO); pipe(&pipes[2 * i]);
// close(pline[i]->fds[1]); }
// execute_command(pline[i]); return (pipes);
// } }
// else
// { void close_pipes(int *pipes, int count, bool flag)
// close(pline[i]->fds[1]); {
// dup2(pline[i]->fds[0], STDIN_FILENO); int i;
// close(pline[i]->fds[0]);
// } i = -1;
// } while (++i < count)
// } {
close(pipes[2 * i]);
close(pipes[(2 * i) + 1]);
}
if (flag)
free(pipes);
}
int err_fork_pline(int *pipes, int count)
{
close_pipes(pipes, count, true);
perror("fork");
return (1);
}
void exec_pchild(int *pipes, int index, t_ast_n *pcmd, int cmds)
{
int ret;
if (index > 0)
dup2(pipes[2 * (index - 1)], STDIN_FILENO);
if (index < cmds - 1)
dup2(pipes[(2 * index) + 1], STDOUT_FILENO);
ft_fprintf(2, "cmd %d: STDIN = %d | STDOUT = %d\n", index, pipes[2 * (index - 1)], pipes[(2* index) + 1]);
fflush(stderr);
// close_pipes(pipes, cmds, false);
if (is_builtin(pcmd->cmd))
{
ret = exec_builtin(pcmd);
exit(ret);
}
else
exec(pcmd);
}
int end_pline(pid_t last_pid, t_ast_n **pline)
{
int status;
int i;
i = -1;
waitpid(last_pid, &status, 0);
while (++i < count_cmds(pline) - 1)
wait(NULL);
if (WIFEXITED(status))
return (WEXITSTATUS(status));
else
return (1);
}
int exec_pipe(t_ast_n **pline)
{
int i;
pid_t pid;
pid_t last_pid;
int *pipes;
pipes = create_pipes(pline);
if (!pipes)
return (1);
i = -1;
while (pline[++i])
{
pid = fork();
if (pid == 0)
exec_pchild(pipes, i, pline[i], count_cmds(pline));
else if (pid > 0)
{
wait(NULL);
if(i == count_cmds(pline) - 1)
last_pid = pid;
}
else if (pid < 0)
return (err_fork_pline(pipes,count_cmds(pline)));
}
// close_pipes(pipes, count_cmds(pline), true);
return (end_pline(last_pid, pline));
}
int execute_command(t_ast_n *node) int execute_command(t_ast_n *node)
{ {
@@ -217,7 +306,7 @@ int execute_command(t_ast_n *node)
return (execute_command(node->right)); return (execute_command(node->right));
return (status); return (status);
} }
// else if (node->state == _PLINE) else if (node->state == _PLINE)
// return (exec_pipe(node->pline)); return (exec_pipe(node->pline));
return (0); return (0);
} }

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */ /* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/27 14:16:52 by lderidde #+# #+# */ /* Created: 2025/01/27 14:16:52 by lderidde #+# #+# */
/* Updated: 2025/01/28 08:46:01 by lderidde ### ########.fr */ /* Updated: 2025/01/29 13:28:35 by lderidde ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */