From 933d68c2da364faaa29c50c349c6818a4c051de4 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Mon, 5 Aug 2024 17:17:20 +0200 Subject: [PATCH] Pipeline works with invalid free(TM) --- ast/src/from_node/from_node.c | 16 +-- exec/include/exec/_run_ast.h | 10 +- exec/src/run_ast.c | 193 ++++++++++++++++++---------------- stdme/src/fs/file_dup.c | 4 +- stdme/src/os/pipe.c | 4 +- stdme/src/os/process.c | 5 +- 6 files changed, 126 insertions(+), 106 deletions(-) diff --git a/ast/src/from_node/from_node.c b/ast/src/from_node/from_node.c index 32f4a133..2623f5d0 100644 --- a/ast/src/from_node/from_node.c +++ b/ast/src/from_node/from_node.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/26 10:55:52 by rparodi #+# #+# */ -/* Updated: 2024/08/04 12:43:56 by maiboyer ### ########.fr */ +/* Updated: 2024/08/05 16:37:43 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -127,22 +127,22 @@ void _append_redirection(t_ast_node node, t_ast_node redirection) return (ast_free(redirection)); if (node->kind == AST_CASE) vec = &node->data.case_.suffixes_redirections; - if (node->kind == AST_COMMAND) + else if (node->kind == AST_COMMAND) vec = &node->data.command.suffixes_redirections; - if (node->kind == AST_COMPOUND_STATEMENT) + else if (node->kind == AST_COMPOUND_STATEMENT) vec = &node->data.compound_statement.suffixes_redirections; - if (node->kind == AST_LIST) + else if (node->kind == AST_LIST) vec = &node->data.list.suffixes_redirections; - if (node->kind == AST_PIPELINE) + else if (node->kind == AST_PIPELINE) vec = &node->data.pipeline.suffixes_redirections; - if (node->kind == AST_SUBSHELL) + else if (node->kind == AST_SUBSHELL) vec = &node->data.subshell.suffixes_redirections; - if (vec == NULL) + else vec = _append_scripting(node); if (vec != NULL) vec_ast_push(vec, redirection); else - ast_free(redirection); + (ast_free(redirection)); } t_ast_terminator_kind _select_term(t_parse_node node) diff --git a/exec/include/exec/_run_ast.h b/exec/include/exec/_run_ast.h index 867b1125..2d4075f7 100644 --- a/exec/include/exec/_run_ast.h +++ b/exec/include/exec/_run_ast.h @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 14:41:48 by rparodi #+# #+# */ -/* Updated: 2024/08/03 15:39:10 by maiboyer ### ########.fr */ +/* Updated: 2024/08/05 16:07:00 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -78,11 +78,18 @@ struct s_program_result int exit; }; +typedef struct s_pipeline_result t_pipeline_result; +struct s_pipeline_result +{ + int exit; +}; + t_error run_arithmetic_expansion(t_ast_arithmetic_expansion *arithmetic_expansion, t_state *state, t_i64 *out); t_error run_command(t_ast_command *command, t_state *state, t_cmd_pipe cmd_pipe, t_command_result *out); t_error run_expansion(t_ast_expansion *self, t_state *state, t_expansion_result *out); 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_pipeline(t_ast_pipeline *pipeline, t_state *state, t_pipeline_result *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); @@ -98,7 +105,6 @@ t_error run_function_definition(t_ast_function_definition *function_definition, t_error run_heredoc_redirection(t_ast_heredoc_redirection *heredoc_redirection, t_state *state, void *out); t_error run_if_(t_ast_if *if_, t_state *state, void *out); t_error run_list(t_ast_list *list, t_state *state, void *out); -t_error run_pipeline(t_ast_pipeline *pipeline, t_state *state, void *out); t_error run_raw_string(t_ast_raw_string *raw_string, t_state *state, void *out); t_error run_regex(t_ast_regex *regex, t_state *state, void *out); t_error run_subshell(t_ast_subshell *subshell, t_state *state, void *out); diff --git a/exec/src/run_ast.c b/exec/src/run_ast.c index e4adf1d2..ac2565da 100644 --- a/exec/src/run_ast.c +++ b/exec/src/run_ast.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/11 17:22:29 by maiboyer #+# #+# */ -/* Updated: 2024/08/05 15:29:40 by maiboyer ### ########.fr */ +/* Updated: 2024/08/05 16:55:45 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,7 @@ #include "me/vec/vec_ast.h" #include "me/vec/vec_estr.h" #include "me/vec/vec_str.h" +#include #include #include @@ -108,11 +109,17 @@ t_error _handle_len_operator(t_ast_expansion *self, t_state *state, t_expansion_ t_error _handle_no_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) { - (void)(self); - (void)(state); - (void)(value); + t_str *val; + if (self == NULL || state == NULL || value == NULL) return (ERROR); + val = hmap_env_get(state->tmp_var, &self->var_name); + if (val == NULL) + val = hmap_env_get(state->env, &self->var_name); + if (val == NULL) + return (value->exists = false, NO_ERROR); + value->exists = true; + value->value = *val; return (NO_ERROR); }; @@ -275,8 +282,7 @@ t_error _ast_get_str__expansion(t_ast_node elem, t_word_iterator *state, t_vec_e return (ERROR); if (run_expansion(&elem->data.expansion, state->state, &exp_ret)) return (ERROR); - return (vec_estr_push(out, (t_expandable_str){.do_expand = state->res.kind == AST_WORD_NO_QUOTE, .value = str_clone(exp_ret.value)}), - NO_ERROR); + return (vec_estr_push(out, (t_expandable_str){.do_expand = state->res.kind == AST_WORD_NO_QUOTE, .value = exp_ret.value}), NO_ERROR); } t_error _ast_get_str__arimethic_expansion(t_ast_node elem, t_word_iterator *state, t_vec_estr *out) @@ -327,7 +333,7 @@ t_error _exp_into_str(t_ast_node self, t_state *state, t_vec_str *append) t_vec_str splitted; t_str tmp; - if (self == NULL || state == NULL || append == NULL || self->kind == AST_EXPANSION) + if (self == NULL || state == NULL || append == NULL || self->kind != AST_EXPANSION) return (ERROR); if (run_expansion(&self->data.expansion, state, &res)) return (ERROR); @@ -343,14 +349,14 @@ t_error _exp_into_str(t_ast_node self, t_state *state, t_vec_str *append) t_error _arith_into_str(t_ast_node self, t_state *state, t_vec_str *append) { - if (self == NULL || state == NULL || append == NULL || self->kind == AST_ARITHMETIC_EXPANSION) + if (self == NULL || state == NULL || append == NULL || self->kind != AST_ARITHMETIC_EXPANSION) return (ERROR); return (NO_ERROR); } t_error _cmd_into_str(t_ast_node self, t_state *state, t_vec_str *append) { - if (self == NULL || state == NULL || append == NULL || self->kind == AST_COMMAND_SUBSTITUTION) + if (self == NULL || state == NULL || append == NULL || self->kind != AST_COMMAND_SUBSTITUTION) return (ERROR); /* if (str_split(res.value, _get_ifs_value(state), &splitted)) @@ -459,9 +465,10 @@ t_error _ast_into_str(t_ast_node self, t_state *state, t_vec_str *append) // End Internals funcs -t_error run_expansion(t_ast_expansion *self, t_state *state, t_expansion_result *out); -t_error run_word(t_ast_word *word, t_state *state, t_word_result *out); -t_error run_arithmetic_expansion(t_ast_arithmetic_expansion *arithmetic_expansion, t_state *state, t_i64 *out); +// These are done externally +// t_error run_file_redirection(t_ast_file_redirection *file_redirection, t_state *state, void *out) NOT_DONE; +// t_error run_heredoc_redirection(t_ast_heredoc_redirection *heredoc_redirection, t_state *state, void *out) NOT_DONE; +// t_error run_raw_string(t_ast_raw_string *raw_string, t_state *state, void *out) NOT_DONE; t_error run_case_(t_ast_case *case_, t_state *state, void *out) NOT_DONE; t_error run_case_item(t_ast_case_item *case_item, t_state *state, void *out) NOT_DONE; @@ -471,26 +478,87 @@ t_error run_elif(t_ast_elif *elif, t_state *state, void *out) NOT_DONE; t_error run_else_(t_ast_else *else_, t_state *state, void *out) NOT_DONE; t_error run_empty(t_ast_empty *empty, t_state *state, void *out) NOT_DONE; t_error run_extglob(t_ast_extglob *extglob, t_state *state, void *out) NOT_DONE; -t_error run_file_redirection(t_ast_file_redirection *file_redirection, t_state *state, void *out) NOT_DONE; t_error run_for_(t_ast_for *for_, t_state *state, void *out) NOT_DONE; t_error run_function_definition(t_ast_function_definition *function_definition, t_state *state, void *out) NOT_DONE; -t_error run_heredoc_redirection(t_ast_heredoc_redirection *heredoc_redirection, t_state *state, void *out) NOT_DONE; t_error run_if_(t_ast_if *if_, t_state *state, void *out) NOT_DONE; t_error run_list(t_ast_list *list, t_state *state, void *out) NOT_DONE; -t_error run_pipeline(t_ast_pipeline *pipeline, t_state *state, void *out) NOT_DONE; -t_error run_raw_string(t_ast_raw_string *raw_string, t_state *state, void *out) NOT_DONE; t_error run_regex(t_ast_regex *regex, t_state *state, void *out) NOT_DONE; t_error run_subshell(t_ast_subshell *subshell, t_state *state, void *out) NOT_DONE; t_error run_until(t_ast_until *until, t_state *state, void *out) NOT_DONE; t_error run_variable_assignment(t_ast_variable_assignment *variable_assignment, t_state *state, bool is_temporary, void *out) NOT_DONE; t_error run_while_(t_ast_while *while_, t_state *state, void *out) NOT_DONE; +t_error run_pipeline(t_ast_pipeline *pipeline, t_state *state, t_pipeline_result *out) +{ + t_usize i; + t_cmd_pipe cmd_pipe; + t_ast_node child; + t_error ret; + t_command_result cmd_result; + t_ast_node tmp_ast; + + if (pipeline == NULL || state == NULL || out == NULL) + return (ERROR); + i = 0; + cmd_pipe.input = NULL; + cmd_pipe.create_output = true; + while (i < pipeline->statements.len - 1) + { + child = pipeline->statements.buffer[i]; + if (child->kind == AST_COMMAND) + { + if (run_command(&child->data.command, state, cmd_pipe, &cmd_result)) + { + cmd_pipe.input = NULL; + ret = ERROR; + } + else + { + if (cmd_result.process.stdout != NULL) + cmd_pipe.input = cmd_result.process.stdout; + else + (printf("WTF ???\n")); + if (cmd_result.process.stdin != NULL) + close_fd(cmd_result.process.stdin); + if (cmd_result.process.stderr != NULL) + close_fd(cmd_result.process.stderr); + } + } + i++; + } + { + cmd_pipe.create_output = false; + child = pipeline->statements.buffer[i]; + if (child->kind == AST_COMMAND) + { + while (!vec_ast_pop_front(&pipeline->suffixes_redirections, &tmp_ast)) + vec_ast_push(&child->data.command.suffixes_redirections, tmp_ast); + if (run_command(&child->data.command, state, cmd_pipe, &cmd_result)) + { + ret = ERROR; + } + else + { + out->exit = cmd_result.exit; + if (cmd_result.process.stdout != NULL) + close_fd(cmd_result.process.stdout); + if (cmd_result.process.stdin != NULL) + close_fd(cmd_result.process.stdin); + if (cmd_result.process.stderr != NULL) + close_fd(cmd_result.process.stderr); + } + } + } + return (ret); +} + t_error run_program(t_ast_program *self, t_state *state, t_program_result *out) { t_usize i; t_ast_node child; - t_command_result cmd_result; + t_command_result cmd_result; + t_pipeline_result pipeline_result; if (self == NULL || state == NULL || out == NULL) return (ERROR); @@ -510,8 +578,11 @@ t_error run_program(t_ast_program *self, t_state *state, t_program_result *out) if (cmd_result.process.stderr != NULL) close_fd(cmd_result.process.stderr); } - else if (child->kind == AST_IF) - ; + else if (child->kind == AST_PIPELINE) + { + if (run_pipeline(&child->data.pipeline, state, &pipeline_result)) + return (out->exit = 127, ERROR); + } else ; i++; @@ -567,14 +638,13 @@ void _ffree_func(struct s_ffree_state *state) } bool _is_builtin(t_const_str argv0); -t_error _handle_builtin(t_spawn_info info, t_state *state); +t_error _handle_builtin(t_spawn_info info, t_state *state); t_error _handle_builtin(t_spawn_info info, t_state *state) { return (ERROR); } - t_error _spawn_cmd_and_run(t_vec_str args, t_vec_ast redirection, t_state *state, t_cmd_pipe cmd_pipe, t_command_result *out) { t_spawn_info info; @@ -585,8 +655,10 @@ t_error _spawn_cmd_and_run(t_vec_str args, t_vec_ast redirection, t_state *state struct s_ffree_state ffree; info = (t_spawn_info){}; - if (cmd_pipe.input) + if (cmd_pipe.input != NULL) info.stdin = fd(cmd_pipe.input); + if (cmd_pipe.input != NULL && !(cmd_pipe.input->perms & FD_READ)) + printf("PERMISSION ERROR for %s !\n", cmd_pipe.input->name); if (cmd_pipe.create_output) info.stdout = piped(); i = 0; @@ -682,6 +754,8 @@ t_error _spawn_cmd_and_run(t_vec_str args, t_vec_ast redirection, t_state *state info.binary_path = str_clone(info.arguments.buffer[0]); info.forked_free_args = &ffree; info.forked_free = (void (*)(void *))_ffree_func; + signal(SIGINT, SIG_IGN); + signal(SIGQUIT, SIG_IGN); if (spawn_process(info, &out->process)) return (ERROR); int status; @@ -732,7 +806,7 @@ t_error run_command(t_ast_command *command, t_state *state, t_cmd_pipe cmd_pipe, if (tmp->kind == AST_VARIABLE_ASSIGNMENT) { if (run_variable_assignment(&tmp->data.variable_assignment, state, true, NULL)) - return (ERROR); + return (vec_str_free(args), vec_ast_free(redirection), ERROR); } i++; } @@ -740,19 +814,19 @@ t_error run_command(t_ast_command *command, t_state *state, t_cmd_pipe cmd_pipe, while (i < command->cmd_word.len) { if (_ast_into_str(command->cmd_word.buffer[i], state, &args)) - return (ERROR); + return (vec_str_free(args), vec_ast_free(redirection), ERROR); i++; } - while (i < command->prefixes.len) + i = 0; + while (i < command->suffixes_redirections.len) { - tmp = command->prefixes.buffer[i]; + tmp = command->suffixes_redirections.buffer[i]; if (tmp->kind == AST_FILE_REDIRECTION || tmp->kind == AST_HEREDOC_REDIRECTION) vec_ast_push(&redirection, tmp); i++; } - i = 0; if (_spawn_cmd_and_run(args, redirection, state, cmd_pipe, out)) - return (ERROR); + return (vec_str_free(args), vec_ast_free(redirection), ERROR); return (NO_ERROR); } @@ -774,66 +848,3 @@ t_error run_word(t_ast_word *word, t_state *state, t_word_result *out) return (vec_estr_free(iter_state.res.value), ERROR); return (*out = iter_state.res, NO_ERROR); } - -// FUNCTIONS - -/* - -t_error run_node(t_ast_node self, t_state *state, void *out) -{ - if (self->kind == AST_ARITHMETIC_EXPANSION) - return (run_arithmetic_expansion(&self->data.arithmetic_expansion, state, out)); - if (self->kind == AST_CASE) - return (run_case_(&self->data.case_, state, out)); - if (self->kind == AST_CASE_ITEM) - return (run_case_item(&self->data.case_item, state, out)); - if (self->kind == AST_COMMAND) - return (run_command(&self->data.command, state, out)); - if (self->kind == AST_COMMAND_SUBSTITUTION) - return (run_command_substitution(&self->data.command_substitution, state, out)); - if (self->kind == AST_COMPOUND_STATEMENT) - return (run_compound_statement(&self->data.compound_statement, state, out)); - if (self->kind == AST_ELIF) - return (run_elif(&self->data.elif, state, out)); - if (self->kind == AST_ELSE) - return (run_else_(&self->data.else_, state, out)); - if (self->kind == AST_EMPTY) - return (run_empty(&self->data.empty, state, out)); - if (self->kind == AST_EXPANSION) - return (run_expansion(&self->data.expansion, state, out)); - if (self->kind == AST_EXTGLOB) - return (run_extglob(&self->data.extglob, state, out)); - if (self->kind == AST_FILE_REDIRECTION) - return (run_file_redirection(&self->data.file_redirection, state, out)); - if (self->kind == AST_FOR) - return (run_for_(&self->data.for_, state, out)); - if (self->kind == AST_FUNCTION_DEFINITION) - return (run_function_definition(&self->data.function_definition, state, out)); - if (self->kind == AST_HEREDOC_REDIRECTION) - return (run_heredoc_redirection(&self->data.heredoc_redirection, state, out)); - if (self->kind == AST_IF) - return (run_if_(&self->data.if_, state, out)); - if (self->kind == AST_LIST) - return (run_list(&self->data.list, state, out)); - if (self->kind == AST_PIPELINE) - return (run_pipeline(&self->data.pipeline, state, out)); - if (self->kind == AST_PROGRAM) - return (run_program(&self->data.program, state, out)); - if (self->kind == AST_RAW_STRING) - return (run_raw_string(&self->data.raw_string, state, out)); - if (self->kind == AST_REGEX) - return (run_regex(&self->data.regex, state, out)); - if (self->kind == AST_SUBSHELL) - return (run_subshell(&self->data.subshell, state, out)); - if (self->kind == AST_UNTIL) - return (run_until(&self->data.until, state, out)); - if (self->kind == AST_VARIABLE_ASSIGNMENT) - return (run_variable_assignment(&self->data.variable_assignment, state, out)); - if (self->kind == AST_WHILE) - return (run_while_(&self->data.while_, state, out)); - if (self->kind == AST_WORD) - return (run_word(&self->data.word, state, out)); - return (ERROR); -} - -*/ diff --git a/stdme/src/fs/file_dup.c b/stdme/src/fs/file_dup.c index 7f59c8fe..40523a05 100644 --- a/stdme/src/fs/file_dup.c +++ b/stdme/src/fs/file_dup.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/01 06:59:44 by maiboyer #+# #+# */ -/* Updated: 2024/08/01 07:03:32 by maiboyer ### ########.fr */ +/* Updated: 2024/08/05 17:01:40 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,7 +27,7 @@ t_fd *dup_fd(t_fd *fd) return (NULL); tmp = dup(fd->fd); if (tmp == -1) - return (NULL); + 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; diff --git a/stdme/src/os/pipe.c b/stdme/src/os/pipe.c index 84ff6b51..98ad7c85 100644 --- a/stdme/src/os/pipe.c +++ b/stdme/src/os/pipe.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/04 17:59:48 by maiboyer #+# #+# */ -/* Updated: 2024/08/01 06:56:14 by maiboyer ### ########.fr */ +/* Updated: 2024/08/05 17:08:14 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,7 +43,7 @@ t_error create_pipe(t_pipe *out) return (ERROR); } read->slot.fd.fd = fd[0]; - write->slot.fd.fd = fd[0]; + write->slot.fd.fd = fd[1]; read->slot.fd.type = FD_PIPE; write->slot.fd.type = FD_PIPE; read->slot.fd.perms = FD_READ; diff --git a/stdme/src/os/process.c b/stdme/src/os/process.c index 9a7a02fb..2f14cf92 100644 --- a/stdme/src/os/process.c +++ b/stdme/src/os/process.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */ -/* Updated: 2024/08/03 16:34:46 by maiboyer ### ########.fr */ +/* Updated: 2024/08/05 15:46:09 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ #include "me/string/string.h" #include "me/types.h" #include "me/vec/vec_str.h" +#include #include #include #include @@ -29,6 +30,8 @@ t_error spawn_process_exec(t_spawn_info info, t_process *process) { if (info.forked_free) info.forked_free(info.forked_free_args); + signal(SIGINT, SIG_DFL); + signal(SIGQUIT, SIG_DFL); dup2(info.stdin.fd.fd->fd, 0); dup2(info.stdout.fd.fd->fd, 1); dup2(info.stderr.fd.fd->fd, 2);