diff --git a/Minishell.mk b/Minishell.mk index 8a1c416a..805bb440 100644 --- a/Minishell.mk +++ b/Minishell.mk @@ -6,7 +6,7 @@ # By: maiboyer +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/04/28 17:28:30 by maiboyer #+# #+# # -# Updated: 2024/07/31 17:18:34 by maiboyer ### ########.fr # +# Updated: 2024/08/02 18:58:21 by maiboyer ### ########.fr # # # # **************************************************************************** # @@ -100,4 +100,6 @@ build_filelist: @echo "" >> Filelist.$(ANAME).mk @echo -e '$(GREY) Populating $(GREEN) Filelist.$(ANAME).mk$(END)' +%.h: ; + -include $(DEP) diff --git a/allocator/Makefile b/allocator/Makefile index 9b42559b..aa4d14e5 100644 --- a/allocator/Makefile +++ b/allocator/Makefile @@ -6,7 +6,7 @@ # By: maiboyer +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/03 13:20:01 by maiboyer #+# #+# # -# Updated: 2024/07/23 21:52:17 by maiboyer ### ########.fr # +# Updated: 2024/08/02 18:57:11 by maiboyer ### ########.fr # # # # **************************************************************************** # @@ -17,7 +17,6 @@ SRC_DIR = src INCLUDE_DIR = include output/include ../stdme/include LIBS_DIR = . - BASE_PATH ?= $(shell pwd) NAME = lib$(ANAME).a LIB_NAME ?= @@ -87,4 +86,6 @@ build_filelist: @echo "" >> Filelist.$(ANAME).mk @echo -e '$(COL_GRAY) Populating $(COL_GREEN) Filelist.$(ANAME).mk$(END)' +%.h: ; + -include $(DEPS) diff --git a/ast/Makefile b/ast/Makefile index 7c9474d8..fb07427a 100644 --- a/ast/Makefile +++ b/ast/Makefile @@ -6,7 +6,7 @@ # By: maiboyer +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/03 13:20:01 by maiboyer #+# #+# # -# Updated: 2024/07/23 21:52:27 by maiboyer ### ########.fr # +# Updated: 2024/08/02 18:58:20 by maiboyer ### ########.fr # # # # **************************************************************************** # @@ -87,4 +87,6 @@ build_filelist: @echo "" >> Filelist.$(ANAME).mk @echo -e '$(COL_GRAY) Populating $(COL_GREEN) Filelist.$(ANAME).mk$(END)' +%.h: ; + -include $(DEPS) diff --git a/exec/Filelist.exec.mk b/exec/Filelist.exec.mk index 157c2eea..a7da6d03 100644 --- a/exec/Filelist.exec.mk +++ b/exec/Filelist.exec.mk @@ -1,5 +1,5 @@ SRC_FILES = \ -arith/arith \ -arith/arith_operation \ +run_arithmetic/arithmetic \ +run_arithmetic/arithmetic_operation \ run_ast \ diff --git a/exec/Makefile b/exec/Makefile index a688c082..08ee7d17 100644 --- a/exec/Makefile +++ b/exec/Makefile @@ -6,7 +6,7 @@ # By: maiboyer +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/03 13:20:01 by maiboyer #+# #+# # -# Updated: 2024/07/30 16:02:19 by maiboyer ### ########.fr # +# Updated: 2024/08/02 18:58:14 by maiboyer ### ########.fr # # # # **************************************************************************** # @@ -85,4 +85,6 @@ build_filelist: @echo "" >> Filelist.$(ANAME).mk @echo -e '$(COL_GRAY) Populating $(COL_GREEN) Filelist.$(ANAME).mk$(END)' +%.h: ; + -include $(DEPS) diff --git a/exec/include/exec/arith.h b/exec/include/exec/_run_arith.h similarity index 100% rename from exec/include/exec/arith.h rename to exec/include/exec/_run_arith.h diff --git a/exec/src/run_ast.h b/exec/include/exec/_run_ast.h similarity index 57% rename from exec/src/run_ast.h rename to exec/include/exec/_run_ast.h index d513b359..29ba2f15 100644 --- a/exec/src/run_ast.h +++ b/exec/include/exec/_run_ast.h @@ -1,33 +1,24 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* run_ast.h :+: :+: :+: */ +/* _run_ast.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 14:41:48 by rparodi #+# #+# */ -/* Updated: 2024/08/02 14:44:22 by rparodi ### ########.fr */ +/* Updated: 2024/08/02 18:51:16 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef RUN_AST_H -# define RUN_AST_H +#ifndef _RUN_AST_H +#define _RUN_AST_H #include "app/state.h" #include "ast/ast.h" -#include "exec/run.h" -#include "me/convert/numbers_to_str.h" #include "me/fs/fs.h" -#include "me/hashmap/hashmap_env.h" -#include "me/mem/mem.h" #include "me/os/os.h" -#include "me/str/str.h" -#include "me/string/string.h" #include "me/types.h" #include "me/vec/vec_estr.h" -#include "me/vec/vec_str.h" - -#include #define NOT_DONE \ { \ @@ -35,7 +26,6 @@ return (ERROR); \ } - #pragma GCC diagnostic ignored "-Wunused-parameter" #pragma GCC diagnostic ignored "-Wunused-variable" #pragma GCC diagnostic ignored "-Wunknown-pragmas" @@ -78,37 +68,36 @@ typedef struct s_cmd_pipe t_cmd_pipe; struct s_cmd_pipe { - t_fd *output; - bool create_input; + t_fd *input; + bool create_output; }; -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_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_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_command_substitution(t_ast_command_substitution *command_substitution, t_state *state, void *out); -t_error run_compound_statement(t_ast_compound_statement *compound_statement, t_state *state, void *out); -t_error run_elif(t_ast_elif *elif, t_state *state, void *out); -t_error run_else_(t_ast_else *else_, t_state *state, void *out); -t_error run_empty(t_ast_empty *empty, t_state *state, void *out); -t_error run_extglob(t_ast_extglob *extglob, t_state *state, void *out); -t_error run_file_redirection(t_ast_file_redirection *file_redirection, t_state *state, void *out); -t_error run_for_(t_ast_for *for_, t_state *state, void *out); -t_error run_function_definition(t_ast_function_definition *function_definition, t_state *state, void *out); -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_program(t_ast_program *program, 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); -t_error run_until(t_ast_until *until, t_state *state, void *out); -t_error run_variable_assignment(t_ast_variable_assignment *variable_assignment, t_state *state, void *out); -t_error run_while_(t_ast_while *while_, t_state *state, void *out); +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_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_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_command_substitution(t_ast_command_substitution *command_substitution, t_state *state, void *out); +t_error run_compound_statement(t_ast_compound_statement *compound_statement, t_state *state, void *out); +t_error run_elif(t_ast_elif *elif, t_state *state, void *out); +t_error run_else_(t_ast_else *else_, t_state *state, void *out); +t_error run_empty(t_ast_empty *empty, t_state *state, void *out); +t_error run_extglob(t_ast_extglob *extglob, t_state *state, void *out); +t_error run_file_redirection(t_ast_file_redirection *file_redirection, t_state *state, void *out); +t_error run_for_(t_ast_for *for_, t_state *state, void *out); +t_error run_function_definition(t_ast_function_definition *function_definition, t_state *state, void *out); +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_program(t_ast_program *program, 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); +t_error run_until(t_ast_until *until, t_state *state, void *out); +t_error run_variable_assignment(t_ast_variable_assignment *variable_assignment, t_state *state, void *out); +t_error run_while_(t_ast_while *while_, t_state *state, void *out); #endif diff --git a/exec/src/arith/arith.c b/exec/src/run_arithmetic/arithmetic.c similarity index 99% rename from exec/src/arith/arith.c rename to exec/src/run_arithmetic/arithmetic.c index 95cae72a..b308346f 100644 --- a/exec/src/arith/arith.c +++ b/exec/src/run_arithmetic/arithmetic.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "exec/arith.h" +#include "exec/_run_arith.h" #include "me/types.h" #include "me/convert/str_to_numbers.h" diff --git a/exec/src/arith/arith_operation.c b/exec/src/run_arithmetic/arithmetic_operation.c similarity index 99% rename from exec/src/arith/arith_operation.c rename to exec/src/run_arithmetic/arithmetic_operation.c index bf353c90..518c6599 100644 --- a/exec/src/arith/arith_operation.c +++ b/exec/src/run_arithmetic/arithmetic_operation.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "exec/arith.h" +#include "exec/_run_arith.h" t_error _binary_op_add(t_ast_node self, t_state *state, t_i64 *out) { diff --git a/exec/src/run_ast.c b/exec/src/run_ast.c index 6e9324a0..6e5769a5 100644 --- a/exec/src/run_ast.c +++ b/exec/src/run_ast.c @@ -6,15 +6,30 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/11 17:22:29 by maiboyer #+# #+# */ -/* Updated: 2024/08/02 14:45:19 by rparodi ### ########.fr */ +/* Updated: 2024/08/02 19:04:30 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ -#include "./run_ast.h" +#include "app/state.h" +#include "ast/ast.h" +#include "exec/_run_ast.h" +#include "exec/run.h" +#include "me/convert/numbers_to_str.h" +#include "me/fs/fs.h" +#include "me/hashmap/hashmap_env.h" +#include "me/mem/mem.h" +#include "me/os/os.h" +#include "me/str/str.h" +#include "me/string/string.h" +#include "me/types.h" +#include "me/vec/vec_estr.h" +#include "me/vec/vec_str.h" -bool _is_special_var(t_ast_expansion *self) +#include + +bool _is_special_var(t_ast_expansion *self) { - char name; + char name; if (self == NULL) return (true); @@ -23,18 +38,14 @@ bool _is_special_var(t_ast_expansion *self) if (str_len(self->var_name) != 1) return (false); name = self->var_name[0]; - if (name == '*' || name == '@' || \ - name == '?' || name == '!' || \ - name == '#' || name == '-' || \ - name == '$' || name == '0') + if (name == '*' || name == '@' || name == '?' || name == '!' || name == '#' || name == '-' || name == '$' || name == '0') return (true); return (false); } -t_error _run_expansion_special_var(\ - t_ast_expansion *self, t_state *state, t_expansion_result *out) +t_error _run_expansion_special_var(t_ast_expansion *self, t_state *state, t_expansion_result *out) { - char name; + char name; if (self == NULL || state == NULL || out == NULL) return (ERROR); @@ -100,19 +111,19 @@ t_error _handle_no_operator(t_ast_expansion *self, t_state *state, t_expansion_r return (NO_ERROR); }; -t_error _handle_assign_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; -t_error _handle_assign_colon_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; -t_error _handle_alternate_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; -t_error _handle_alternate_colon_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; -t_error _handle_default_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; -t_error _handle_default_colon_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; -t_error _handle_error_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; -t_error _handle_error_colon_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; +t_error _exp_assign_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; +t_error _exp_assign_colon_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; +t_error _exp_alternate_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; +t_error _exp_alternate_colon_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; +t_error _exp_default_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; +t_error _exp_default_colon_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; +t_error _exp_error_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; +t_error _exp_error_colon_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; -t_error _handle_suffix_pattern_smallest_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; -t_error _handle_suffix_pattern_longest_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; -t_error _handle_prefix_pattern_smallest_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; -t_error _handle_prefix_pattern_longest_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; +t_error _exp_suffix_pattern_smallest_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; +t_error _exp_suffix_pattern_largest_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; +t_error _exp_prefix_pattern_smallest_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; +t_error _exp_prefix_pattern_largest_operator(t_ast_expansion *self, t_state *state, t_expansion_result *value) NOT_DONE; t_error _get_op_func(t_ast_expansion *self, t_error (**op_func)(t_ast_expansion *self, t_state *state, t_expansion_result *value)) { @@ -121,29 +132,29 @@ t_error _get_op_func(t_ast_expansion *self, t_error (**op_func)(t_ast_expansion if (self->kind == E_OP_NONE) return (*op_func = _handle_no_operator, NO_ERROR); if (self->kind == E_OP_ERROR) - return (*op_func = _handle_error_operator, NO_ERROR); + return (*op_func = _exp_error_operator, NO_ERROR); if (self->kind == E_OP_ERROR_COLON) - return (*op_func = _handle_error_colon_operator, NO_ERROR); + return (*op_func = _exp_error_colon_operator, NO_ERROR); if (self->kind == E_OP_ASSIGN_DEFAULT) - return (*op_func = _handle_assign_operator, NO_ERROR); + return (*op_func = _exp_assign_operator, NO_ERROR); if (self->kind == E_OP_ASSIGN_DEFAULT_COLON) - return (*op_func = _handle_assign_colon_operator, NO_ERROR); + return (*op_func = _exp_assign_colon_operator, NO_ERROR); if (self->kind == E_OP_DEFAULT) - return (*op_func = _handle_default_operator, NO_ERROR); + return (*op_func = _exp_default_operator, NO_ERROR); if (self->kind == E_OP_DEFAULT_COLON) - return (*op_func = _handle_default_colon_operator, NO_ERROR); + return (*op_func = _exp_default_colon_operator, NO_ERROR); if (self->kind == E_OP_ALTERNATE) - return (*op_func = _handle_alternate_operator, NO_ERROR); + return (*op_func = _exp_alternate_operator, NO_ERROR); if (self->kind == E_OP_ALTERNATE_COLON) - return (*op_func = _handle_alternate_colon_operator, NO_ERROR); + return (*op_func = _exp_alternate_colon_operator, NO_ERROR); if (self->kind == E_OP_LARGEST_PREFIX) - return (*op_func = _handle_prefix_pattern_smallest_operator, NO_ERROR); + return (*op_func = _exp_prefix_pattern_largest_operator, NO_ERROR); if (self->kind == E_OP_SMALLEST_PREFIX) - return (*op_func = _handle_prefix_pattern_smallest_operator, NO_ERROR); + return (*op_func = _exp_prefix_pattern_smallest_operator, NO_ERROR); if (self->kind == E_OP_LARGEST_SUFFIX) - return (*op_func = _handle_suffix_pattern_smallest_operator, NO_ERROR); + return (*op_func = _exp_suffix_pattern_largest_operator, NO_ERROR); if (self->kind == E_OP_SMALLEST_SUFFIX) - return (*op_func = _handle_suffix_pattern_longest_operator, NO_ERROR); + return (*op_func = _exp_suffix_pattern_smallest_operator, NO_ERROR); return (ERROR); } @@ -511,8 +522,11 @@ t_error _spawn_cmd_and_run(t_vec_str args, t_vec_ast redirection, t_state *state t_vec_str filename_args; t_fd *red_fd; - if (cmd_pipe.output) - info.stdout = fd(cmd_pipe.output); + info = (t_spawn_info){}; + if (cmd_pipe.input) + info.stdin = fd(cmd_pipe.input); + if (cmd_pipe.create_output) + info.stdout = piped(); i = 0; filename_args = vec_str_new(16, str_free); while (i < redirection.len) @@ -533,7 +547,7 @@ t_error _spawn_cmd_and_run(t_vec_str args, t_vec_ast redirection, t_state *state return (ERROR); if (filename_args.len != 1) return (vec_str_free(filename_args), ERROR); - red_fd = open_fd(filename_args.buffer[i], FD_READ, 0 | O_CLOEXEC, 0); + 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 = fd(red_fd); diff --git a/line/Makefile b/line/Makefile index b1618388..f55fe80d 100644 --- a/line/Makefile +++ b/line/Makefile @@ -6,7 +6,7 @@ # By: maiboyer +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/03 13:20:01 by maiboyer #+# #+# # -# Updated: 2024/07/23 22:07:26 by maiboyer ### ########.fr # +# Updated: 2024/08/02 18:58:07 by maiboyer ### ########.fr # # # # **************************************************************************** # @@ -83,4 +83,6 @@ build_filelist: @echo "" >> Filelist.$(ANAME).mk @echo -e '$(COL_GRAY) Populating $(COL_GREEN) Filelist.$(ANAME).mk$(END)' +%.h: ; + -include $(DEPS) diff --git a/parser/Grammar.mk b/parser/Grammar.mk index 6b58a5a8..15529f45 100644 --- a/parser/Grammar.mk +++ b/parser/Grammar.mk @@ -6,7 +6,7 @@ # By: maiboyer +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/03 13:20:01 by maiboyer #+# #+# # -# Updated: 2024/07/30 14:52:58 by maiboyer ### ########.fr # +# Updated: 2024/08/02 18:57:50 by maiboyer ### ########.fr # # # # **************************************************************************** # @@ -77,4 +77,6 @@ build_filelist: @echo "" >> Filelist.$(ANAME).mk @echo -e '$(GREY) Populating $(GREEN) Filelist.$(ANAME).mk$(END)' +%.h: ; + -include $(DEPS) diff --git a/parser/Parser.mk b/parser/Parser.mk index 23997614..e8b934ae 100644 --- a/parser/Parser.mk +++ b/parser/Parser.mk @@ -6,7 +6,7 @@ # By: maiboyer +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/03 13:20:01 by maiboyer #+# #+# # -# Updated: 2024/07/23 21:53:53 by maiboyer ### ########.fr # +# Updated: 2024/08/02 18:57:55 by maiboyer ### ########.fr # # # # **************************************************************************** # @@ -91,4 +91,6 @@ build_filelist: @echo "" >> Filelist.$(ANAME).mk @echo -e '$(GREY) Populating $(GREEN) Filelist.$(ANAME).mk$(END)' +%.h: ; + -include $(DEPS) diff --git a/stdme/Filelist.me.mk b/stdme/Filelist.me.mk index a4486f61..8f53db6e 100644 --- a/stdme/Filelist.me.mk +++ b/stdme/Filelist.me.mk @@ -68,8 +68,8 @@ os/abort \ os/exit \ os/pipe \ os/process \ -os/process_inner \ -os/process_inner2 \ +os/process_iterator_function \ +os/process_redirection \ printf/callbacks \ printf/formatter/char \ printf/formatter/decimal \ diff --git a/stdme/Makefile b/stdme/Makefile index 42b8641f..586bf7f3 100644 --- a/stdme/Makefile +++ b/stdme/Makefile @@ -6,7 +6,7 @@ # By: maiboyer +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/03 13:20:01 by maiboyer #+# #+# # -# Updated: 2024/07/23 21:49:24 by maiboyer ### ########.fr # +# Updated: 2024/08/02 18:57:39 by maiboyer ### ########.fr # # # # **************************************************************************** # @@ -100,4 +100,6 @@ build_filelist: @tree $(GEN_DIR) -ifF | rg '$(GEN_DIR)/(.*)\.c$$' --replace '$$1' | sed -re 's/^(.*)_([0-9]+)$$/\1|\2/g' | sort -t'|' --key=1,1 --key=2,2n | sed -e's/|/_/' | xargs printf '%-78s\\\n' >> Filelist.$(ANAME).mk @echo "" >> Filelist.$(ANAME).mk +%.h: ; + -include $(DEPS) diff --git a/stdme/src/os/process.c b/stdme/src/os/process.c index 0726f6e1..5f5b9480 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/01 07:14:29 by maiboyer ### ########.fr */ +/* Updated: 2024/08/02 19:11:00 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,9 +21,8 @@ #include #include -bool find_path(const t_str *s); -bool find_null(const t_str *s); -bool str_start_with(t_const_str s, t_const_str prefix); +bool _find_path(const t_str *s); +bool _find_null(const t_str *s); t_error handle_redirections(t_spawn_info *info, t_process *process); t_error spawn_process_exec(t_spawn_info info, t_process *process) @@ -41,10 +40,10 @@ t_error spawn_process_exec(t_spawn_info info, t_process *process) close_fd(info.stdin.fd.fd); close_fd(info.stdout.fd.fd); close_fd(info.stderr.fd.fd); - if (!vec_str_any(&info.arguments, find_null, &res) && res) + if (!vec_str_any(&info.arguments, _find_null, &res) && res) vec_str_push(&info.arguments, NULL); res = false; - if (!vec_str_any(&info.environement, find_null, &res) && res) + if (!vec_str_any(&info.environement, _find_null, &res) && res) vec_str_push(&info.environement, NULL); execve(info.binary_path, info.arguments.buffer, info.environement.buffer); return (NO_ERROR); @@ -79,12 +78,11 @@ t_error find_binary(t_spawn_info *info, t_process *process) if (info->binary_path == NULL) return (ERROR); s = string_new(256); - if (str_start_with(info->binary_path, "/") - || str_find_chr(info->binary_path, '/') != NULL) + if (str_find_chr(info->binary_path, '/') != NULL) string_push(&s, info->binary_path); else { - if (vec_str_find(&info->environement, find_path, &p_idx)) + if (vec_str_find(&info->environement, _find_path, &p_idx)) return (string_free(s), ERROR); if (in_path(info, process, info->environement.buffer[p_idx], &s)) return (ERROR); @@ -98,7 +96,8 @@ t_error find_binary(t_spawn_info *info, t_process *process) return (string_free(s), ERROR); } -static void cleanup(t_spawn_info info, t_process *process, bool cleanup_process) +static void _process_cleanup(t_spawn_info info, t_process *process, \ + bool cleanup_process) { if (cleanup_process && process->stdin) close_fd(process->stdin); @@ -116,16 +115,18 @@ static void cleanup(t_spawn_info info, t_process *process, bool cleanup_process) t_error spawn_process(t_spawn_info info, t_process *process) { + if (process == NULL) + return (ERROR); if (handle_redirections(&info, process)) - return (cleanup(info, process, true), ERROR); + return (_process_cleanup(info, process, true), ERROR); if (find_binary(&info, process)) - return (cleanup(info, process, true), ERROR); + return (_process_cleanup(info, process, true), ERROR); process->pid = fork(); if (process->pid == 0) (spawn_process_exec(info, process), exit(1)); else { - cleanup(info, process, false); + _process_cleanup(info, process, false); if (process->pid == -1) return (ERROR); } diff --git a/stdme/src/os/process_inner.c b/stdme/src/os/process_iterator_function.c similarity index 100% rename from stdme/src/os/process_inner.c rename to stdme/src/os/process_iterator_function.c diff --git a/stdme/src/os/process_inner2.c b/stdme/src/os/process_redirection.c similarity index 78% rename from stdme/src/os/process_inner2.c rename to stdme/src/os/process_redirection.c index 978d0219..3915b090 100644 --- a/stdme/src/os/process_inner2.c +++ b/stdme/src/os/process_redirection.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* process_inner2.c :+: :+: :+: */ +/* process_redirection.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/04 22:27:00 by maiboyer #+# #+# */ -/* Updated: 2024/08/01 07:14:10 by maiboyer ### ########.fr */ +/* Updated: 2024/08/02 19:10:51 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ #include "me/os/os.h" #include "me/types.h" -void handle_redirections_inherited(t_spawn_info *info, t_process *process) +t_error _handle_redirections_inherited(t_spawn_info *info, t_process *process) { (void)(process); if (info->stdin.tag == R_INHERITED) @@ -32,9 +32,10 @@ void handle_redirections_inherited(t_spawn_info *info, t_process *process) info->stderr = fd(dup_fd(get_stderr())); process->stderr = dup_fd(get_stderr()); } + return (NO_ERROR); } -void handle_redirections_fds(t_spawn_info *info, t_process *process) +t_error _handle_redirections_fds(t_spawn_info *info, t_process *process) { if (info->stdin.tag == R_FD) { @@ -51,22 +52,13 @@ void handle_redirections_fds(t_spawn_info *info, t_process *process) info->stderr = fd(dup_fd(info->stderr.fd.fd)); process->stderr = dup_fd(info->stderr.fd.fd); } + return (NO_ERROR); } -static inline void redirection_inner(t_spawn_info *info, t_process *process) -{ - process->stderr = NULL; - process->stdout = NULL; - process->stdin = NULL; - handle_redirections_fds(info, process); - handle_redirections_inherited(info, process); -} - -t_error handle_redirections(t_spawn_info *info, t_process *process) +t_error _handle_redirections_pipe(t_spawn_info *info, t_process *process) { t_pipe pipe_fd; - redirection_inner(info, process); if (info->stdin.tag == R_PIPED) { if (create_pipe(&pipe_fd)) @@ -90,3 +82,17 @@ t_error handle_redirections(t_spawn_info *info, t_process *process) } return (NO_ERROR); } + +t_error handle_redirections(t_spawn_info *info, t_process *process) +{ + process->stderr = NULL; + process->stdout = NULL; + process->stdin = NULL; + if (_handle_redirections_fds(info, process)) + return (ERROR); + if (_handle_redirections_inherited(info, process)) + return (ERROR); + if (_handle_redirections_pipe(info, process)) + return (ERROR); + return (NO_ERROR); +}