update: fixed heredoc finaly :D

This commit is contained in:
maix0 2024-09-18 14:46:19 +02:00
parent da223422b0
commit cc1e2b7fdc
7 changed files with 77 additions and 13 deletions

6
close.sh Normal file
View file

@ -0,0 +1,6 @@
for fd in $(/usr/bin/env ls /proc/$$/fd); do
if [ "$fd" -gt 10 ]; then
#echo "$fd"
exec {fd}>&-
fi;
done

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/14 12:30:09 by maiboyer #+# #+# */ /* Created: 2024/09/14 12:30:09 by maiboyer #+# #+# */
/* Updated: 2024/09/17 21:41:55 by rparodi ### ########.fr */ /* Updated: 2024/09/18 14:20:32 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -145,8 +145,8 @@ t_error _redirection_heredoc(\
(void)state; (void)state;
if (red->data.heredoc_redirection.op == AST_REDIR_HEREDOC) if (red->data.heredoc_redirection.op == AST_REDIR_HEREDOC)
{ {
(void)((info->stdout.tag == R_FD) && (close_fd(info->stdout.fd.fd), 1)); (void)((info->stdin.tag == R_FD) && (close_fd(info->stdin.fd.fd), 1));
info->stdout.tag = R_INHERITED; info->stdin.tag = R_INHERITED;
if (create_pipe(&heredoc_pipe)) if (create_pipe(&heredoc_pipe))
return (ERROR); return (ERROR);
if (_get_heredoc_input(heredoc_pipe.write, \ if (_get_heredoc_input(heredoc_pipe.write, \

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/14 12:32:37 by maiboyer #+# #+# */ /* Created: 2024/09/14 12:32:37 by maiboyer #+# #+# */
/* Updated: 2024/09/16 19:22:06 by maiboyer ### ########.fr */ /* Updated: 2024/09/17 17:09:30 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -48,7 +48,6 @@ t_error run_pipeline(t_ast_pipeline *pipeline, t_state *state,
else else
{ {
vec_pid_push(&pids, cmd_result.process.pid); vec_pid_push(&pids, cmd_result.process.pid);
close_fd(cmd_pipe.input);
if (cmd_result.process.stdout != NULL) if (cmd_result.process.stdout != NULL)
cmd_pipe.input = cmd_result.process.stdout; cmd_pipe.input = cmd_result.process.stdout;
if (cmd_result.process.stdin != NULL) if (cmd_result.process.stdin != NULL)
@ -56,6 +55,8 @@ t_error run_pipeline(t_ast_pipeline *pipeline, t_state *state,
if (cmd_result.process.stderr != NULL) if (cmd_result.process.stderr != NULL)
close_fd(cmd_result.process.stderr); close_fd(cmd_result.process.stderr);
} }
if (cmd_pipe.input != NULL)
printf("[%i]%s\n", cmd_pipe.input->fd, cmd_pipe.input->name);
} }
i++; i++;
} }
@ -73,7 +74,6 @@ t_error run_pipeline(t_ast_pipeline *pipeline, t_state *state,
else else
{ {
vec_pid_push(&pids, cmd_result.process.pid); vec_pid_push(&pids, cmd_result.process.pid);
close_fd(cmd_pipe.input);
if (cmd_result.process.stdout != NULL) if (cmd_result.process.stdout != NULL)
close_fd(cmd_result.process.stdout); close_fd(cmd_result.process.stdout);
if (cmd_result.process.stdin != NULL) if (cmd_result.process.stdin != NULL)
@ -85,8 +85,7 @@ t_error run_pipeline(t_ast_pipeline *pipeline, t_state *state,
} }
if (pids.len != 0) if (pids.len != 0)
{ {
if (!(kill(pids.buffer[pids.len - 1], 0) == -1 && errno == ESRCH)) while (waitpid(pids.buffer[pids.len - 1], &waitpid_status, 0) < 0 && errno != ESRCH)
while (waitpid(pids.buffer[pids.len - 1], &waitpid_status, 0) < 0)
; ;
while (waitpid(-1, NULL, 0) != -1) while (waitpid(-1, NULL, 0) != -1)
; ;

View file

@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* file_dup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/01 06:59:44 by maiboyer #+# #+# */
/* Updated: 2024/09/18 14:24:59 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "fcntl.h"
#include "me/fs/fs.h"
#include "me/printf/printf.h"
#include "me/str/str.h"
#include "me/string/string.h"
#include "me/types.h"
#include <unistd.h>
static int _get_flags(t_fd *fd)
{
int out;
out = 0;
if (fd->perms & FD_READ && fd->perms & FD_WRITE)
out = O_RDWR;
else if (fd->perms & FD_READ)
out = O_RDONLY;
else if (fd->perms & FD_WRITE)
out = O_WRONLY;
return (out);
}
t_fd *dup_fd(t_fd *fd)
{
struct s_file_slot *slot;
int tmp;
t_string path;
if (fd == NULL)
return (NULL);
slot = get_unused_fd_slot();
if (slot == NULL)
return (NULL);
path = string_new(32);
me_printf_str(&path, "/proc/self/fd/%i", fd->fd);
tmp = open(path.buf, O_CLOEXEC | _get_flags(fd));
string_free(path);
if (tmp == -1)
return (printf("There was an error while duping the "\
"fd %d `%s`\n", fd->fd, fd->name), NULL);
slot->ty = SLOT_FD;
slot->slot.fd.fd = tmp;
slot->slot.fd.perms = fd->perms;
slot->slot.fd.type = fd->type;
slot->slot.fd.name = str_clone(fd->name);
return (&slot->slot.fd);
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 17:59:48 by maiboyer #+# #+# */ /* Created: 2024/01/04 17:59:48 by maiboyer #+# #+# */
/* Updated: 2024/08/14 17:51:38 by maiboyer ### ########.fr */ /* Updated: 2024/09/18 14:24:44 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */ /* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */
/* Updated: 2024/08/11 12:00:25 by maiboyer ### ########.fr */ /* Updated: 2024/09/17 17:12:21 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -125,7 +125,7 @@ t_error spawn_process(t_spawn_info info, t_process *process)
(spawn_process_exec(info, process), exit(1)); (spawn_process_exec(info, process), exit(1));
else else
{ {
_process_cleanup(info, process, false); _process_cleanup(info, process, process->pid == -1);
if (process->pid == -1) if (process->pid == -1)
return (ERROR); return (ERROR);
} }

View file

@ -1,6 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
make && valgrind --leak-check=full --show-leak-kinds=none --track-origins=yes --track-fds=no --trace-children=yes --read-var-info=yes --read-inline-info=yes ./minishell <<<'cat <<EOF | xargs printf "\x1b[32m%s \x1b[0m\n" make && valgrind --leak-check=full --show-leak-kinds=none --track-origins=yes --track-fds=yes --trace-children=yes --read-var-info=yes --read-inline-info=yes --vgdb=no --vgdb-error=0 ./minishell <<<'cat <<EOF | xargs printf "\\x1b[32m%s\\x1b[0m\\n"
bonjour je suis un heredoc bonjour je suis un heredoc
le saviez tu je dois finir par EOF mais qui est sur la ligne le saviez tu je dois finir par EOF mais qui est sur la ligne
donc par example si j ai cette ligne qui fini avec EOF donc par example si j ai cette ligne qui fini avec EOF