From baa9af918fc1bb5f3e73121a408ee8c33a75b80c Mon Sep 17 00:00:00 2001 From: maix0 Date: Fri, 11 Oct 2024 22:20:06 +0200 Subject: [PATCH] update: fixed error on pipe+redirection --- exec/src/run_ast/_run_exit_code.c | 3 +-- exec/src/run_ast/_spawn_cmd_redir_heredoc.c | 4 ++-- exec/src/run_ast/run_builtins.c | 3 +-- exec/src/run_ast/run_expansion_builtin.c | 3 +-- parser/src/passes.c | 6 +---- parser/src/yarn/ast_cmd.c | 4 ++-- stdme/src/os/abort.c | 2 +- stdme/src/os/process.c | 26 ++++++++++++--------- stdme/src/os/process_redirection.c | 20 ++++++++-------- 9 files changed, 34 insertions(+), 37 deletions(-) diff --git a/exec/src/run_ast/_run_exit_code.c b/exec/src/run_ast/_run_exit_code.c index 88fc0c20..1fac7581 100644 --- a/exec/src/run_ast/_run_exit_code.c +++ b/exec/src/run_ast/_run_exit_code.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/14 12:31:28 by maiboyer #+# #+# */ -/* Updated: 2024/10/10 18:54:14 by maiboyer ### ########.fr */ +/* Updated: 2024/10/11 22:16:18 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -56,7 +56,6 @@ t_error _run_other(t_ast_node self, t_state *state, int *out) *out = subshell_res.exit; } state->last_exit = *out; - printf("state->last_exit = %i\n", state->last_exit); return (NO_ERROR); } diff --git a/exec/src/run_ast/_spawn_cmd_redir_heredoc.c b/exec/src/run_ast/_spawn_cmd_redir_heredoc.c index 46365efc..9f7a4736 100644 --- a/exec/src/run_ast/_spawn_cmd_redir_heredoc.c +++ b/exec/src/run_ast/_spawn_cmd_redir_heredoc.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/14 12:30:09 by maiboyer #+# #+# */ -/* Updated: 2024/09/18 21:51:46 by maiboyer ### ########.fr */ +/* Updated: 2024/10/11 22:19:06 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,7 +42,7 @@ put_string_fd(lstate.output_fd, "^C\n"), line_refresh_line(&lstate), ERROR); } } line_edit_stop(&lstate); - if (line == NULL || str_compare(delim, line)) + if (line == NULL || (str_compare(delim, line) && (str_free(line), 1))) break ; (put_string_fd(write, line), put_char_fd(write, '\n'), str_free(line)); } diff --git a/exec/src/run_ast/run_builtins.c b/exec/src/run_ast/run_builtins.c index e18fdf15..928dcbc9 100644 --- a/exec/src/run_ast/run_builtins.c +++ b/exec/src/run_ast/run_builtins.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/14 12:24:49 by maiboyer #+# #+# */ -/* Updated: 2024/10/10 18:56:40 by maiboyer ### ########.fr */ +/* Updated: 2024/10/11 22:16:01 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -117,7 +117,6 @@ t_error _handle_builtin(t_spawn_info info, t_state *state, t_cmd_pipe cmd_pipe, return (ERROR); if (_setup_binfo(&info, &binfo, out)) return (ERROR); - printf("last_exit = %i\n", state->last_exit); _run_builtin(&binfo, actual_func, cmd_pipe, out); if (binfo.stdin) close_fd(binfo.stdin); diff --git a/exec/src/run_ast/run_expansion_builtin.c b/exec/src/run_ast/run_expansion_builtin.c index 94caca41..c4cb6225 100644 --- a/exec/src/run_ast/run_expansion_builtin.c +++ b/exec/src/run_ast/run_expansion_builtin.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/14 12:38:38 by maiboyer #+# #+# */ -/* Updated: 2024/10/10 18:58:52 by maiboyer ### ########.fr */ +/* Updated: 2024/10/11 22:16:10 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,7 +54,6 @@ t_error _run_expansion_special_var(t_ast_expansion *self, t_state *state, if (name == '?') { *out = (t_expansion_result){.exists = true, .value = NULL}; - printf("state->last_exit in exp = %i \n", state->last_exit); if (i32_to_str(state->last_exit, &out->value)) // TODO: fix this shit return (ERROR); } diff --git a/parser/src/passes.c b/parser/src/passes.c index a1a06440..4a5eca3d 100644 --- a/parser/src/passes.c +++ b/parser/src/passes.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/02 18:41:16 by maiboyer #+# #+# */ -/* Updated: 2024/10/10 17:25:40 by maiboyer ### ########.fr */ +/* Updated: 2024/10/11 21:33:34 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -67,8 +67,6 @@ t_error ts_apply_passes(t_vec_token ts, t_vec_token *out) if ((g_ts_passes[i].fn)(ts, &next)) return (me_eprintf("failed on %s token pass\n", \ g_ts_passes[i].name), ERROR); - else - me_printf("Applied '%s' pass\n", g_ts_passes[i].name); ts = next; i++; } @@ -94,8 +92,6 @@ t_error ts_dq_apply_passes(t_vec_token ts, t_vec_token *out) if ((g_ts_dq_passes[i].fn)(ts, &next)) return (me_eprintf(\ "failed on '%s' dq token pass\n", g_ts_dq_passes[i].name), ERROR); - else - me_printf("Applied '%s' dq_pass\n", g_ts_dq_passes[i].name); ts = next; i++; } diff --git a/parser/src/yarn/ast_cmd.c b/parser/src/yarn/ast_cmd.c index f9917410..d50fa80d 100644 --- a/parser/src/yarn/ast_cmd.c +++ b/parser/src/yarn/ast_cmd.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/09 12:44:53 by maiboyer #+# #+# */ -/* Updated: 2024/10/10 17:48:57 by maiboyer ### ########.fr */ +/* Updated: 2024/10/11 15:12:36 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,7 +34,7 @@ static t_error _create_ast_redir(enum e_token ty, t_ast_node *out) ret = ast_alloc(AST_HEREDOC_REDIRECTION); ret->data.heredoc_redirection.op = AST_REDIR_HEREDOC; } - else if (ty == TOK_DRCARRET || ty == TOK_LCARRET || ty == TOK_DRCARRET) + else if (ty == TOK_RCARRET || ty == TOK_LCARRET || ty == TOK_DRCARRET) ret = ast_alloc(AST_FILE_REDIRECTION); else return (ERROR); diff --git a/stdme/src/os/abort.c b/stdme/src/os/abort.c index fa3fb68d..03d9daa2 100644 --- a/stdme/src/os/abort.c +++ b/stdme/src/os/abort.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/07 11:08:03 by maiboyer #+# #+# */ -/* Updated: 2024/08/04 14:28:13 by maiboyer ### ########.fr */ +/* Updated: 2024/10/11 22:15:08 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/stdme/src/os/process.c b/stdme/src/os/process.c index f4454d67..a05af6a7 100644 --- a/stdme/src/os/process.c +++ b/stdme/src/os/process.c @@ -6,10 +6,11 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */ -/* Updated: 2024/10/11 11:47:20 by maiboyer ### ########.fr */ +/* Updated: 2024/10/11 22:19:32 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ +#include "fcntl.h" #include "me/fs/fs.h" #include "me/mem/mem.h" #include "me/os/os.h" @@ -29,6 +30,7 @@ t_error handle_redirections(t_spawn_info *info, t_process *process); t_error spawn_process_exec(t_spawn_info info, t_process *process) { + (void)(process); if (info.forked_free) info.forked_free(info.forked_free_args); signal(SIGINT, SIG_DFL); @@ -36,12 +38,12 @@ t_error spawn_process_exec(t_spawn_info info, t_process *process) dup2(info.stdin.fd.fd->fd, 0); dup2(info.stdout.fd.fd->fd, 1); dup2(info.stderr.fd.fd->fd, 2); - close_fd(process->stdin); - close_fd(process->stdout); - close_fd(process->stderr); - close_fd(info.stdin.fd.fd); - close_fd(info.stdout.fd.fd); - close_fd(info.stderr.fd.fd); + if (info.stdin.fd.fd->fd != 0) + close_fd(info.stdin.fd.fd); + if (info.stdout.fd.fd->fd != 1) + close_fd(info.stdout.fd.fd); + if (info.stderr.fd.fd->fd != 2) + close_fd(info.stderr.fd.fd); vec_str_push(&info.arguments, NULL); vec_str_push(&info.environement, NULL); execve(info.binary_path, info.arguments.buffer, info.environement.buffer); @@ -62,7 +64,6 @@ t_error in_path(t_spawn_info *info, t_process *process, t_const_str path_raw, { string_clear(s); me_printf_str(s, "%s/%s", path.buffer[idx++], info->binary_path); - printf("testing %s\n", s->buf); if (access(s->buf, X_OK) == 0) return (vec_str_free(path), NO_ERROR); } @@ -105,9 +106,12 @@ static void _process_cleanup(t_spawn_info info, t_process *process, \ close_fd(process->stdout); if (cleanup_process && process->stderr) close_fd(process->stderr); - close_fd(info.stdin.fd.fd); - close_fd(info.stdout.fd.fd); - close_fd(info.stderr.fd.fd); + if (info.stdin.fd.fd->fd != 0) + close_fd(info.stdin.fd.fd); + if (info.stdout.fd.fd->fd != 1) + close_fd(info.stdout.fd.fd); + if (info.stderr.fd.fd->fd != 2) + close_fd(info.stderr.fd.fd); vec_str_free(info.arguments); vec_str_free(info.environement); mem_free(info.binary_path); diff --git a/stdme/src/os/process_redirection.c b/stdme/src/os/process_redirection.c index 677845bc..bb3ab261 100644 --- a/stdme/src/os/process_redirection.c +++ b/stdme/src/os/process_redirection.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/04 22:27:00 by maiboyer #+# #+# */ -/* Updated: 2024/09/06 15:28:26 by rparodi ### ########.fr */ +/* Updated: 2024/10/11 22:10:05 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,18 +19,18 @@ t_error _handle_redirections_inherited(t_spawn_info *info, t_process *process) (void)(process); if (info->stdin.tag == R_INHERITED) { - info->stdin = fd(dup_fd(get_stdin())); - process->stdin = dup_fd(get_stdin()); + info->stdin = fd(get_stdin()); + process->stdin = NULL; } if (info->stdout.tag == R_INHERITED) { - info->stdout = fd(dup_fd(get_stdout())); - process->stdout = dup_fd(get_stdout()); + info->stdout = fd(get_stdout()); + process->stdout = NULL; } if (info->stderr.tag == R_INHERITED) { - info->stderr = fd(dup_fd(get_stderr())); - process->stderr = dup_fd(get_stderr()); + info->stderr = fd(get_stderr()); + process->stderr = NULL; } return (NO_ERROR); } @@ -40,17 +40,17 @@ t_error _handle_redirections_fds(t_spawn_info *info, t_process *process) if (info->stdin.tag == R_FD) { info->stdin = fd(dup_fd(info->stdin.fd.fd)); - process->stdin = dup_fd(info->stdin.fd.fd); + process->stdin = NULL; } if (info->stdout.tag == R_FD) { info->stdout = fd(dup_fd(info->stdout.fd.fd)); - process->stdout = dup_fd(info->stdout.fd.fd); + process->stdout = NULL; } if (info->stderr.tag == R_FD) { info->stderr = fd(dup_fd(info->stderr.fd.fd)); - process->stderr = dup_fd(info->stderr.fd.fd); + process->stderr = NULL; } return (NO_ERROR); }