update: fixed error on pipe+redirection

This commit is contained in:
maix0 2024-10-11 22:20:06 +02:00
parent 1dfb7f7ef7
commit baa9af918f
9 changed files with 34 additions and 37 deletions

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/14 12:31:28 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; *out = subshell_res.exit;
} }
state->last_exit = *out; state->last_exit = *out;
printf("state->last_exit = %i\n", state->last_exit);
return (NO_ERROR); return (NO_ERROR);
} }

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/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); line_edit_stop(&lstate);
if (line == NULL || str_compare(delim, line)) if (line == NULL || (str_compare(delim, line) && (str_free(line), 1)))
break ; break ;
(put_string_fd(write, line), put_char_fd(write, '\n'), str_free(line)); (put_string_fd(write, line), put_char_fd(write, '\n'), str_free(line));
} }

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/14 12:24:49 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); return (ERROR);
if (_setup_binfo(&info, &binfo, out)) if (_setup_binfo(&info, &binfo, out))
return (ERROR); return (ERROR);
printf("last_exit = %i\n", state->last_exit);
_run_builtin(&binfo, actual_func, cmd_pipe, out); _run_builtin(&binfo, actual_func, cmd_pipe, out);
if (binfo.stdin) if (binfo.stdin)
close_fd(binfo.stdin); close_fd(binfo.stdin);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/14 12:38:38 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 == '?') if (name == '?')
{ {
*out = (t_expansion_result){.exists = true, .value = NULL}; *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 if (i32_to_str(state->last_exit, &out->value)) // TODO: fix this shit
return (ERROR); return (ERROR);
} }

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/02 18:41:16 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)) if ((g_ts_passes[i].fn)(ts, &next))
return (me_eprintf("failed on %s token pass\n", \ return (me_eprintf("failed on %s token pass\n", \
g_ts_passes[i].name), ERROR); g_ts_passes[i].name), ERROR);
else
me_printf("Applied '%s' pass\n", g_ts_passes[i].name);
ts = next; ts = next;
i++; 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)) if ((g_ts_dq_passes[i].fn)(ts, &next))
return (me_eprintf(\ return (me_eprintf(\
"failed on '%s' dq token pass\n", g_ts_dq_passes[i].name), ERROR); "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; ts = next;
i++; i++;
} }

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/09 12:44:53 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 = ast_alloc(AST_HEREDOC_REDIRECTION);
ret->data.heredoc_redirection.op = AST_REDIR_HEREDOC; 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); ret = ast_alloc(AST_FILE_REDIRECTION);
else else
return (ERROR); return (ERROR);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 11:08:03 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,10 +6,11 @@
/* 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/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/fs/fs.h"
#include "me/mem/mem.h" #include "me/mem/mem.h"
#include "me/os/os.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) t_error spawn_process_exec(t_spawn_info info, t_process *process)
{ {
(void)(process);
if (info.forked_free) if (info.forked_free)
info.forked_free(info.forked_free_args); info.forked_free(info.forked_free_args);
signal(SIGINT, SIG_DFL); signal(SIGINT, SIG_DFL);
@ -36,11 +38,11 @@ t_error spawn_process_exec(t_spawn_info info, t_process *process)
dup2(info.stdin.fd.fd->fd, 0); dup2(info.stdin.fd.fd->fd, 0);
dup2(info.stdout.fd.fd->fd, 1); dup2(info.stdout.fd.fd->fd, 1);
dup2(info.stderr.fd.fd->fd, 2); dup2(info.stderr.fd.fd->fd, 2);
close_fd(process->stdin); if (info.stdin.fd.fd->fd != 0)
close_fd(process->stdout);
close_fd(process->stderr);
close_fd(info.stdin.fd.fd); close_fd(info.stdin.fd.fd);
if (info.stdout.fd.fd->fd != 1)
close_fd(info.stdout.fd.fd); close_fd(info.stdout.fd.fd);
if (info.stderr.fd.fd->fd != 2)
close_fd(info.stderr.fd.fd); close_fd(info.stderr.fd.fd);
vec_str_push(&info.arguments, NULL); vec_str_push(&info.arguments, NULL);
vec_str_push(&info.environement, NULL); vec_str_push(&info.environement, NULL);
@ -62,7 +64,6 @@ t_error in_path(t_spawn_info *info, t_process *process, t_const_str path_raw,
{ {
string_clear(s); string_clear(s);
me_printf_str(s, "%s/%s", path.buffer[idx++], info->binary_path); 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) if (access(s->buf, X_OK) == 0)
return (vec_str_free(path), NO_ERROR); return (vec_str_free(path), NO_ERROR);
} }
@ -105,8 +106,11 @@ static void _process_cleanup(t_spawn_info info, t_process *process, \
close_fd(process->stdout); close_fd(process->stdout);
if (cleanup_process && process->stderr) if (cleanup_process && process->stderr)
close_fd(process->stderr); close_fd(process->stderr);
if (info.stdin.fd.fd->fd != 0)
close_fd(info.stdin.fd.fd); close_fd(info.stdin.fd.fd);
if (info.stdout.fd.fd->fd != 1)
close_fd(info.stdout.fd.fd); close_fd(info.stdout.fd.fd);
if (info.stderr.fd.fd->fd != 2)
close_fd(info.stderr.fd.fd); close_fd(info.stderr.fd.fd);
vec_str_free(info.arguments); vec_str_free(info.arguments);
vec_str_free(info.environement); vec_str_free(info.environement);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 22:27:00 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); (void)(process);
if (info->stdin.tag == R_INHERITED) if (info->stdin.tag == R_INHERITED)
{ {
info->stdin = fd(dup_fd(get_stdin())); info->stdin = fd(get_stdin());
process->stdin = dup_fd(get_stdin()); process->stdin = NULL;
} }
if (info->stdout.tag == R_INHERITED) if (info->stdout.tag == R_INHERITED)
{ {
info->stdout = fd(dup_fd(get_stdout())); info->stdout = fd(get_stdout());
process->stdout = dup_fd(get_stdout()); process->stdout = NULL;
} }
if (info->stderr.tag == R_INHERITED) if (info->stderr.tag == R_INHERITED)
{ {
info->stderr = fd(dup_fd(get_stderr())); info->stderr = fd(get_stderr());
process->stderr = dup_fd(get_stderr()); process->stderr = NULL;
} }
return (NO_ERROR); 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) if (info->stdin.tag == R_FD)
{ {
info->stdin = fd(dup_fd(info->stdin.fd.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) if (info->stdout.tag == R_FD)
{ {
info->stdout = fd(dup_fd(info->stdout.fd.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) if (info->stderr.tag == R_FD)
{ {
info->stderr = fd(dup_fd(info->stderr.fd.fd)); info->stderr = fd(dup_fd(info->stderr.fd.fd));
process->stderr = dup_fd(info->stderr.fd.fd); process->stderr = NULL;
} }
return (NO_ERROR); return (NO_ERROR);
} }