Update exec to support subshells
This commit is contained in:
parent
42e1b718a2
commit
0ba9a1a1b5
3 changed files with 169 additions and 11 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/08/02 14:41:48 by rparodi #+# #+# */
|
/* Created: 2024/08/02 14:41:48 by rparodi #+# #+# */
|
||||||
/* Updated: 2024/08/13 16:52:12 by maiboyer ### ########.fr */
|
/* Updated: 2024/08/14 17:41:41 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -26,10 +26,10 @@
|
||||||
return (ERROR); \
|
return (ERROR); \
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
||||||
#pragma GCC diagnostic ignored "-Wunused-variable"
|
#pragma GCC diagnostic ignored "-Wunused-variable"
|
||||||
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
|
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
|
||||||
#pragma GCC diagnostic ignored "-Wempty-body"
|
#pragma GCC diagnostic ignored "-Wempty-body"
|
||||||
#pragma clang diagnostic ignored "-Wunknown-pragmas"
|
#pragma clang diagnostic ignored "-Wunknown-pragmas"
|
||||||
#pragma clang diagnostic ignored "-Wunused-parameter"
|
#pragma clang diagnostic ignored "-Wunused-parameter"
|
||||||
#pragma clang diagnostic ignored "-Wunused-variable"
|
#pragma clang diagnostic ignored "-Wunused-variable"
|
||||||
|
|
@ -93,7 +93,11 @@ struct s_list_result
|
||||||
typedef struct s_subshell_result t_subshell_result;
|
typedef struct s_subshell_result t_subshell_result;
|
||||||
struct s_subshell_result
|
struct s_subshell_result
|
||||||
{
|
{
|
||||||
int exit;
|
t_pid pid;
|
||||||
|
int exit;
|
||||||
|
t_fd *stdin;
|
||||||
|
t_fd *stdout;
|
||||||
|
t_fd *stderr;
|
||||||
};
|
};
|
||||||
|
|
||||||
t_error run_arithmetic_expansion(t_ast_arithmetic_expansion *arithmetic_expansion, t_state *state, t_i64 *out);
|
t_error run_arithmetic_expansion(t_ast_arithmetic_expansion *arithmetic_expansion, t_state *state, t_i64 *out);
|
||||||
|
|
@ -103,7 +107,7 @@ t_error run_word(t_ast_word *word, t_state *state, t_word_result *out);
|
||||||
t_error run_program(t_ast_program *program, t_state *state, t_program_result *out);
|
t_error run_program(t_ast_program *program, t_state *state, t_program_result *out);
|
||||||
t_error run_pipeline(t_ast_pipeline *pipeline, t_state *state, t_pipeline_result *out);
|
t_error run_pipeline(t_ast_pipeline *pipeline, t_state *state, t_pipeline_result *out);
|
||||||
t_error run_list(t_ast_list *list, t_state *state, t_list_result *out);
|
t_error run_list(t_ast_list *list, t_state *state, t_list_result *out);
|
||||||
t_error run_subshell(t_ast_subshell *subshell, t_state *state, t_subshell_result *out);
|
t_error run_subshell(t_ast_subshell *subshell, t_state *state, t_cmd_pipe cmd_pipe, t_subshell_result *out);
|
||||||
|
|
||||||
t_error run_case_(t_ast_case *case_, t_state *state, void *out);
|
t_error run_case_(t_ast_case *case_, t_state *state, void *out);
|
||||||
t_error run_case_item(t_ast_case_item *case_item, t_state *state, void *out);
|
t_error run_case_item(t_ast_case_item *case_item, t_state *state, void *out);
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/07/11 17:22:29 by maiboyer #+# #+# */
|
/* Created: 2024/07/11 17:22:29 by maiboyer #+# #+# */
|
||||||
/* Updated: 2024/08/14 17:39:16 by maiboyer ### ########.fr */
|
/* Updated: 2024/08/14 18:07:51 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -28,6 +28,7 @@
|
||||||
#include "me/vec/vec_estr.h"
|
#include "me/vec/vec_estr.h"
|
||||||
#include "me/vec/vec_pid.h"
|
#include "me/vec/vec_pid.h"
|
||||||
#include "me/vec/vec_str.h"
|
#include "me/vec/vec_str.h"
|
||||||
|
#include "unistd.h"
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
|
|
||||||
|
|
@ -526,7 +527,13 @@ t_error run_while_(t_ast_while *while_, t_state *state, void *out) NOT_DONE;
|
||||||
|
|
||||||
void mem_free(void *free);
|
void mem_free(void *free);
|
||||||
|
|
||||||
t_error run_subshell(t_ast_subshell *subshell, t_state *state, t_subshell_result *out) NOT_DONE;
|
struct s_subshell_info
|
||||||
|
{
|
||||||
|
t_fd *stdin;
|
||||||
|
t_fd *stderr;
|
||||||
|
t_fd *stdout;
|
||||||
|
t_fd *ret_stdout;
|
||||||
|
};
|
||||||
|
|
||||||
t_error _run_get_exit_code(t_ast_node self, t_state *state, int *out)
|
t_error _run_get_exit_code(t_ast_node self, t_state *state, int *out)
|
||||||
{
|
{
|
||||||
|
|
@ -561,13 +568,160 @@ t_error _run_get_exit_code(t_ast_node self, t_state *state, int *out)
|
||||||
}
|
}
|
||||||
if (self->kind == AST_SUBSHELL)
|
if (self->kind == AST_SUBSHELL)
|
||||||
{
|
{
|
||||||
if (run_subshell(&self->data.subshell, state, &subshell_res))
|
if (run_subshell(&self->data.subshell, state, (t_cmd_pipe){NULL, false}, &subshell_res))
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
*out = subshell_res.exit;
|
*out = subshell_res.exit;
|
||||||
}
|
}
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t_error run_subshell(t_ast_subshell *subshell, t_state *state, t_cmd_pipe cmd_pipe, t_subshell_result *out)
|
||||||
|
{
|
||||||
|
struct s_subshell_info info;
|
||||||
|
t_pipe spipe;
|
||||||
|
t_usize i;
|
||||||
|
t_vec_str filename_args;
|
||||||
|
t_ast_node red;
|
||||||
|
t_vec_ast redirection;
|
||||||
|
t_fd *red_fd;
|
||||||
|
|
||||||
|
if (subshell == NULL || state == NULL || out == NULL)
|
||||||
|
return (ERROR);
|
||||||
|
|
||||||
|
mem_set_zero(&info, sizeof(info));
|
||||||
|
info.stdin = cmd_pipe.input;
|
||||||
|
if (cmd_pipe.create_output)
|
||||||
|
{
|
||||||
|
if (create_pipe(&spipe))
|
||||||
|
return (ERROR);
|
||||||
|
info.stdout = spipe.write;
|
||||||
|
info.ret_stdout = spipe.read;
|
||||||
|
}
|
||||||
|
i = 0;
|
||||||
|
filename_args = vec_str_new(16, str_free);
|
||||||
|
redirection = subshell->suffixes_redirections;
|
||||||
|
while (i < redirection.len)
|
||||||
|
{
|
||||||
|
red = redirection.buffer[i];
|
||||||
|
if (red == NULL)
|
||||||
|
continue;
|
||||||
|
vec_str_free(filename_args);
|
||||||
|
filename_args = vec_str_new(16, str_free);
|
||||||
|
if (red->kind == AST_FILE_REDIRECTION)
|
||||||
|
{
|
||||||
|
if (red->data.file_redirection.op == AST_REDIR_INPUT)
|
||||||
|
{
|
||||||
|
if (info.stdin != NULL)
|
||||||
|
close_fd(info.stdin);
|
||||||
|
info.stdin = NULL;
|
||||||
|
if (_ast_into_str(red->data.file_redirection.output, state, &filename_args))
|
||||||
|
return (ERROR);
|
||||||
|
if (filename_args.len != 1)
|
||||||
|
return (vec_str_free(filename_args), ERROR);
|
||||||
|
red_fd = open_fd(filename_args.buffer[i], FD_READ, O_CLOEXEC, 0);
|
||||||
|
if (red_fd == NULL)
|
||||||
|
return (vec_str_free(filename_args), ERROR);
|
||||||
|
info.stdin = red_fd;
|
||||||
|
};
|
||||||
|
if (red->data.file_redirection.op == AST_REDIR_OUTPUT)
|
||||||
|
{
|
||||||
|
if (info.stdout != NULL)
|
||||||
|
close_fd(info.stdout);
|
||||||
|
info.stdout = NULL;
|
||||||
|
if (_ast_into_str(red->data.file_redirection.output, state, &filename_args))
|
||||||
|
return (ERROR);
|
||||||
|
if (filename_args.len != 1)
|
||||||
|
return (vec_str_free(filename_args), ERROR);
|
||||||
|
red_fd = open_fd(filename_args.buffer[i], FD_WRITE, O_TRUNC | O_CREAT | O_CLOEXEC, FP_ALL_READ | FP_ALL_WRITE);
|
||||||
|
if (red_fd == NULL)
|
||||||
|
return (vec_str_free(filename_args), ERROR);
|
||||||
|
info.stdout = red_fd;
|
||||||
|
};
|
||||||
|
if (red->data.file_redirection.op == AST_REDIR_APPEND)
|
||||||
|
{
|
||||||
|
if (info.stdout != NULL)
|
||||||
|
close_fd(info.stdout);
|
||||||
|
info.stdout = NULL;
|
||||||
|
if (_ast_into_str(red->data.file_redirection.output, state, &filename_args))
|
||||||
|
return (ERROR);
|
||||||
|
if (filename_args.len != 1)
|
||||||
|
return (vec_str_free(filename_args), ERROR);
|
||||||
|
red_fd = open_fd(filename_args.buffer[i], FD_WRITE, O_TRUNC | O_CREAT | O_CLOEXEC, FP_ALL_READ | FP_ALL_WRITE);
|
||||||
|
if (red_fd == NULL)
|
||||||
|
return (vec_str_free(filename_args), ERROR);
|
||||||
|
info.stdout = red_fd;
|
||||||
|
};
|
||||||
|
if (red->data.file_redirection.op == AST_REDIR_DUP_INPUT || red->data.file_redirection.op == AST_REDIR_DUP_OUTPUT ||
|
||||||
|
red->data.file_redirection.op == AST_REDIR_CLOSE_INPUT || red->data.file_redirection.op == AST_REDIR_CLOSE_OUTPUT ||
|
||||||
|
red->data.file_redirection.op == AST_REDIR_INPUT_OUTPUT || red->data.file_redirection.op == AST_REDIR_OUTPUT_CLOBBER)
|
||||||
|
return (ERROR);
|
||||||
|
}
|
||||||
|
if (red->kind == AST_HEREDOC_REDIRECTION)
|
||||||
|
{
|
||||||
|
if (red->data.heredoc_redirection.op == AST_REDIR_HEREDOC)
|
||||||
|
{
|
||||||
|
t_pipe heredoc_pipe;
|
||||||
|
|
||||||
|
if (info.stdout != NULL)
|
||||||
|
close_fd(info.stdout);
|
||||||
|
info.stdout = NULL;
|
||||||
|
if (create_pipe(&heredoc_pipe))
|
||||||
|
return (ERROR);
|
||||||
|
put_string_fd(heredoc_pipe.write, red->data.heredoc_redirection.content);
|
||||||
|
close_fd(heredoc_pipe.write);
|
||||||
|
info.stdin = heredoc_pipe.read;
|
||||||
|
}
|
||||||
|
if (red->data.heredoc_redirection.op == AST_REDIR_HEREDOC_INDENT)
|
||||||
|
return (ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
vec_str_free(filename_args);
|
||||||
|
|
||||||
|
t_pid forked;
|
||||||
|
forked = fork();
|
||||||
|
if (forked == 0)
|
||||||
|
{
|
||||||
|
int code;
|
||||||
|
|
||||||
|
if (info.ret_stdout != NULL)
|
||||||
|
close_fd(info.ret_stdout);
|
||||||
|
if (info.stdin != NULL)
|
||||||
|
(dup2(info.stdin->fd, 0), close_fd(info.stdin));
|
||||||
|
if (info.stdout != NULL)
|
||||||
|
(dup2(info.stdout->fd, 1), close_fd(info.stdout));
|
||||||
|
if (info.stderr != NULL)
|
||||||
|
(dup2(info.stderr->fd, 2), close_fd(info.stderr));
|
||||||
|
i = 0;
|
||||||
|
while (i < subshell->body.len)
|
||||||
|
{
|
||||||
|
if (_run_get_exit_code(subshell->body.buffer[i], state, &code))
|
||||||
|
me_exit(127);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
me_exit(code);
|
||||||
|
}
|
||||||
|
if (forked == -1)
|
||||||
|
return (ERROR);
|
||||||
|
if (info.stdin != NULL)
|
||||||
|
(dup2(info.stdin->fd, 0), close_fd(info.stdin));
|
||||||
|
if (info.stdout != NULL)
|
||||||
|
(dup2(info.stdout->fd, 1), close_fd(info.stdout));
|
||||||
|
if (info.stderr != NULL)
|
||||||
|
(dup2(info.stderr->fd, 2), close_fd(info.stderr));
|
||||||
|
if (cmd_pipe.create_output || cmd_pipe.input != NULL)
|
||||||
|
return (out->pid = forked, out->stdout = info.ret_stdout, NO_ERROR);
|
||||||
|
int status;
|
||||||
|
if (waitpid(forked, &status, 0) == -1)
|
||||||
|
return (ERROR);
|
||||||
|
if (WIFEXITED(status))
|
||||||
|
out->exit = WEXITSTATUS(status);
|
||||||
|
if (WIFSIGNALED(status))
|
||||||
|
out->exit = WTERMSIG(status);
|
||||||
|
return (NO_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
t_error run_list(t_ast_list *list, t_state *state, t_list_result *out)
|
t_error run_list(t_ast_list *list, t_state *state, t_list_result *out)
|
||||||
{
|
{
|
||||||
t_ast_node tmp;
|
t_ast_node tmp;
|
||||||
|
|
|
||||||
|
|
@ -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/05 17:08:14 by maiboyer ### ########.fr */
|
/* Updated: 2024/08/14 17:51:38 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue