diff --git a/Makefile b/Makefile index 6f5a96af..1ede6999 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2024/10/14 14:00:30 by maiboyer ### ########.fr # +# Updated: 2024/10/14 15:20:12 by maiboyer ### ########.fr # # # # **************************************************************************** # @@ -44,7 +44,7 @@ endif endif CFLAGS_ADDITIONAL ?= -#CFLAGS_ADDITIONAL += -DNVALGRIND +CFLAGS_ADDITIONAL += -DNVALGRIND # TODO: REMOVE THIS WHEN FINISHING THIS: # CFLAGS_ADDITIONAL += -fsanitize=memory -fno-omit-frame-pointer -fsanitize-memory-track-origins #-fuse-ld=lld -ffunction-sections -fdata-sections -Wl,--allow-multiple diff --git a/ast/src/print_ast/ast_print.c b/ast/src/print_ast/ast_print.c index 4ec8d3e3..0ddb1c7a 100644 --- a/ast/src/print_ast/ast_print.c +++ b/ast/src/print_ast/ast_print.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/03 20:38:29 by maiboyer #+# #+# */ -/* Updated: 2024/10/12 17:51:13 by rparodi ### ########.fr */ +/* Updated: 2024/10/14 14:12:42 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -87,7 +87,7 @@ static inline void ast_print_block2(t_ast_node self) if (self->kind == AST_FUNCTION_DEFINITION) return (ast_print_node_function_definition(self)); if (self->kind == AST_HEREDOC_REDIRECTION) - return (ast_print_notdone(self)); + return (ast_print_node_heredoc_redirection(self)); if (self->kind == AST_IF) return (ast_print_notdone(self)); if (self->kind == AST_LIST) diff --git a/ast/src/print_ast/ast_print_command.c b/ast/src/print_ast/ast_print_command.c index a47f4ee2..e4f081bf 100644 --- a/ast/src/print_ast/ast_print_command.c +++ b/ast/src/print_ast/ast_print_command.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/03 20:38:29 by maiboyer #+# #+# */ -/* Updated: 2024/10/12 17:51:13 by rparodi ### ########.fr */ +/* Updated: 2024/10/14 14:15:06 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,8 +61,9 @@ void ast_print_node_command(t_ast_node self) i = 0; while (i < self->data.command.prefixes.len) { + printf(" _"); ast_print(self->data.command.prefixes.buffer[i++]); - printf(" "); + printf("_ "); } return (ast_print_node_command_helper(self)); } diff --git a/ast/src/print_ast/ast_print_global.c b/ast/src/print_ast/ast_print_global.c index 75a7bccb..0fcde7fa 100644 --- a/ast/src/print_ast/ast_print_global.c +++ b/ast/src/print_ast/ast_print_global.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/26 13:34:09 by rparodi #+# #+# */ -/* Updated: 2024/10/12 17:51:14 by rparodi ### ########.fr */ +/* Updated: 2024/10/14 14:14:36 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ void ast_print_node_raw_string(t_ast_node self) return ; if (self->kind != AST_RAW_STRING) return ; - printf("%s", self->data.raw_string.str); + printf("{%s}", self->data.raw_string.str); } void ast_print_node_regex(t_ast_node self) diff --git a/ast/src/print_ast/ast_print_redirection.c b/ast/src/print_ast/ast_print_redirection.c index fbfa3e8b..3d480316 100644 --- a/ast/src/print_ast/ast_print_redirection.c +++ b/ast/src/print_ast/ast_print_redirection.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/26 13:07:12 by rparodi #+# #+# */ -/* Updated: 2024/10/12 17:51:15 by rparodi ### ########.fr */ +/* Updated: 2024/10/14 14:12:49 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,3 +49,8 @@ void ast_print_node_file_redirection(t_ast_node self) ast_print(self->data.file_redirection.output); return ((void) ast_print_node_file_redirection_heredoc(self)); } + +void ast_print_node_heredoc_redirection(t_ast_node self) +{ + printf("<<%s", self->data.heredoc_redirection.delimiter); +} diff --git a/ast/src/print_ast/ast_print_subshell.c b/ast/src/print_ast/ast_print_subshell.c index a3867aad..f82d3bd9 100644 --- a/ast/src/print_ast/ast_print_subshell.c +++ b/ast/src/print_ast/ast_print_subshell.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/26 13:27:30 by rparodi #+# #+# */ -/* Updated: 2024/10/12 17:51:15 by rparodi ### ########.fr */ +/* Updated: 2024/10/14 14:22:39 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -100,8 +100,12 @@ void ast_print_node_word(t_ast_node self) if (self->data.word.kind == AST_WORD_DOUBLE_QUOTE) quote_type = "\""; i = 0; + printf("<="); printf("%s", quote_type); while (i < self->data.word.inner.len) + { ast_print(self->data.word.inner.buffer[i++]); + } printf("%s", quote_type); + printf("=>"); } diff --git a/exec/src/run_ast/_ast_into_str.c b/exec/src/run_ast/_ast_into_str.c index 3a445a51..dfe7ef5c 100644 --- a/exec/src/run_ast/_ast_into_str.c +++ b/exec/src/run_ast/_ast_into_str.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/14 12:26:51 by maiboyer #+# #+# */ -/* Updated: 2024/10/14 12:34:25 by maiboyer ### ########.fr */ +/* Updated: 2024/10/14 15:02:01 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,32 +21,62 @@ t_error _word_handle_star(t_ast_word *word, t_state *state, t_vec_str *out); t_error list_files_in_current_directory(t_vec_str *out); t_error _word_into_str_inner(struct s_word_str_args args); -t_error _word_pass_quote(t_state *state, t_word_result *res, t_vec_str *append) +t_error _word_split_loop(\ + bool do_split, t_expandable_str val, t_vec_str *append, t_string *tmp) +{ + t_vec_str split; + t_str stmp; + + if (do_split) + { + if (val.do_expand) + { + if (str_split(val.value, " ", &split)) + return (ERROR); + while (!vec_str_pop_front(&split, &stmp)) + vec_str_push(append, stmp); + vec_str_free(split); + } + else + vec_str_push(append, str_clone(val.value)); + } + else + string_push(tmp, val.value); + return (NO_ERROR); +} + +t_error _word_split(\ + bool do_split, t_state *state, t_word_result *res, t_vec_str *append) { t_string tmp; t_usize i; - (void)(state); tmp = string_new(64); i = 0; while (i < res->value.len) - string_push(&tmp, res->value.buffer[i++].value); - vec_str_push(append, tmp.buf); - return (NO_ERROR); + { + if (_word_split_loop(do_split, res->value.buffer[i++], append, &tmp)) + return (string_free(tmp), ERROR); + } + if (!do_split) + vec_str_push(append, tmp.buf); + else + string_free(tmp); + return ((void)state, NO_ERROR); } -t_error _word_into_str(t_ast_node self, t_state *state, t_vec_str *append) +t_error _word_into_str(t_ast_node self, t_state *state, t_vec_str *ap) { t_word_result res; - if (self == NULL || state == NULL || append == NULL + if (self == NULL || state == NULL || ap == NULL || self->kind != AST_WORD) return (ERROR); if (_word_is_star(&self->data.word)) - return (_word_handle_star(&self->data.word, state, append)); + return (_word_handle_star(&self->data.word, state, ap)); if (run_word(&self->data.word, state, &res)) return (ERROR); - if (_word_pass_quote(state, &res, append)) + if (_word_split(self->data.word.kind == AST_WORD_NO_QUOTE, state, &res, ap)) return (ERROR); return (vec_estr_free(res.value), NO_ERROR); } diff --git a/exec/src/run_ast/_spawn_cmd.c b/exec/src/run_ast/_spawn_cmd.c index 17b00a96..e830e3af 100644 --- a/exec/src/run_ast/_spawn_cmd.c +++ b/exec/src/run_ast/_spawn_cmd.c @@ -6,21 +6,21 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/14 12:30:09 by maiboyer #+# #+# */ -/* Updated: 2024/10/13 17:59:35 by maiboyer ### ########.fr */ +/* Updated: 2024/10/14 15:07:42 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #include "app/env.h" #include "exec/_run_ast.h" +#include "line/line.h" #include "me/fs/fs.h" #include "me/os/os.h" +#include "me/printf/printf.h" #include "me/str/str.h" -#include -#include -#include -#include "line/line.h" #include "me/types.h" #include +#include +#include t_error _redirection_fd(\ t_spawn_info *info, t_state *state, t_ast_node red); @@ -55,12 +55,20 @@ t_error _setup_redirection(\ return (NO_ERROR); } +static t_error _err_cmd(t_str bpath) +{ + me_eprintf("failed to execute %s\n", bpath); + str_free(bpath); + return (ERROR); +} + t_error _spawn_cmd_and_run_end(\ t_spawn_info info, t_state *state, \ t_cmd_pipe cmd_pipe, t_command_result *out) { struct s_ffree_state ffree; int status; + t_str bpath; status = 0; ffree = (struct s_ffree_state){.state = state, .cmd_pipe = cmd_pipe}; @@ -68,8 +76,10 @@ t_error _spawn_cmd_and_run_end(\ info.forked_free = (void (*)(void *))_ffree_func; signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN); + bpath = str_clone(info.binary_path); if (spawn_process(info, &out->process)) - return (close_fd(cmd_pipe.input), out->exit = 127, ERROR); + return (close_fd(cmd_pipe.input), out->exit = 127, _err_cmd(bpath)); + str_free(bpath); if (cmd_pipe.create_output || cmd_pipe.input != NULL) return (out->exit = -1, NO_ERROR); if (waitpid(out->process.pid, &status, 0) == -1 && errno != ECHILD) @@ -88,8 +98,6 @@ t_error _spawn_cmd_and_run(t_vec_str args, t_redirections redirs, t_spawn_info info; info = (t_spawn_info){}; - for (t_usize i = 0; i < args.len; i++) - printf("args[%zu] = %s\n", i, args.buffer[i]); if (_setup_redirection(&info, state, redirs.cmd_pipe, &redirs.redirections)) return (ERROR); redirs.redirections.len = 0; diff --git a/exec/src/run_ast/_spawn_cmd_redir_fd.c b/exec/src/run_ast/_spawn_cmd_redir_fd.c index 41d38400..38dcf9a4 100644 --- a/exec/src/run_ast/_spawn_cmd_redir_fd.c +++ b/exec/src/run_ast/_spawn_cmd_redir_fd.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/14 12:30:09 by maiboyer #+# #+# */ -/* Updated: 2024/10/13 13:58:11 by maiboyer ### ########.fr */ +/* Updated: 2024/10/14 15:10:40 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ #include "exec/_run_ast.h" #include "me/fs/fs.h" #include "me/os/os.h" +#include "me/printf/printf.h" #include "me/str/str.h" #include #include @@ -21,8 +22,11 @@ #include "line/line.h" #include "me/types.h" +#define ARG "Error: too may args for redirection\n" +#define ERR "Error: failed to open %s\n" + t_error _redir_input(\ - t_spawn_info *info, t_state *state, t_vec_str *fname, t_ast_node red) + t_spawn_info *info, t_state *state, t_vec_str *fn, t_ast_node red) { t_fd *red_fd; @@ -30,20 +34,20 @@ t_error _redir_input(\ { (void)((info->stdin.tag == R_FD) && (close_fd(info->stdin.fd.fd), 1)); info->stdin.tag = R_INHERITED; - if (_ast_into_str(red->data.file_redirection.output, state, fname)) + if (_ast_into_str(red->data.file_redirection.output, state, fn)) return (ERROR); - if (fname->len != 1) - return (vec_str_free(*fname), ERROR); - red_fd = open_fd(fname->buffer[0], FD_READ, O_CLOEXEC, 0); + if (fn->len != 1) + return (me_eprintf(ARG), vec_str_free(*fn), ERROR); + red_fd = open_fd(fn->buffer[0], FD_READ, O_CLOEXEC, 0); if (red_fd == NULL) - return (vec_str_free(*fname), ERROR); + return (me_eprintf(ERR, fn->buffer[0]), vec_str_free(*fn), ERROR); info->stdin = fd(red_fd); } return (NO_ERROR); } t_error _redir_output(\ - t_spawn_info *info, t_state *state, t_vec_str *fname, t_ast_node red) + t_spawn_info *info, t_state *state, t_vec_str *fn, t_ast_node red) { t_fd *red_fd; @@ -51,21 +55,21 @@ t_error _redir_output(\ { (void)((info->stdout.tag == R_FD) && (close_fd(info->stdout.fd.fd), 1)); info->stdout.tag = R_INHERITED; - if (_ast_into_str(red->data.file_redirection.output, state, fname)) + if (_ast_into_str(red->data.file_redirection.output, state, fn)) return (ERROR); - if (fname->len != 1) - return (vec_str_free(*fname), ERROR); - red_fd = open_fd(fname->buffer[0], FD_WRITE, \ + if (fn->len != 1) + return (me_eprintf(ARG), vec_str_free(*fn), ERROR); + red_fd = open_fd(fn->buffer[0], FD_WRITE, \ O_TRUNC | O_CREAT | O_CLOEXEC, FP_ALL_READ | FP_ALL_WRITE); if (red_fd == NULL) - return (vec_str_free(*fname), ERROR); + return (me_eprintf(ERR, fn->buffer[0]), vec_str_free(*fn), ERROR); info->stdout = fd(red_fd); } return (NO_ERROR); } t_error _redir_output_append(\ - t_spawn_info *info, t_state *state, t_vec_str *fname, t_ast_node red) + t_spawn_info *info, t_state *state, t_vec_str *fn, t_ast_node red) { t_fd *red_fd; @@ -73,14 +77,14 @@ t_error _redir_output_append(\ { (void)((info->stdout.tag == R_FD) && (close_fd(info->stdout.fd.fd), 1)); info->stdout.tag = R_INHERITED; - if (_ast_into_str(red->data.file_redirection.output, state, fname)) + if (_ast_into_str(red->data.file_redirection.output, state, fn)) return (ERROR); - if (fname->len != 1) - return (vec_str_free(*fname), ERROR); - red_fd = open_fd(fname->buffer[0], FD_WRITE, \ + if (fn->len != 1) + return (me_eprintf(ARG), vec_str_free(*fn), ERROR); + red_fd = open_fd(fn->buffer[0], FD_WRITE, \ O_APPEND | O_CREAT | O_CLOEXEC, FP_ALL_READ | FP_ALL_WRITE); if (red_fd == NULL) - return (vec_str_free(*fname), ERROR); + return (me_eprintf(ERR, fn->buffer[0]), vec_str_free(*fn), ERROR); info->stdout = fd(red_fd); } return (NO_ERROR); diff --git a/exec/src/run_ast/run_pipeline.c b/exec/src/run_ast/run_pipeline.c index c90acf79..3154199f 100644 --- a/exec/src/run_ast/run_pipeline.c +++ b/exec/src/run_ast/run_pipeline.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/14 12:32:37 by maiboyer #+# #+# */ -/* Updated: 2024/10/13 18:01:06 by maiboyer ### ########.fr */ +/* Updated: 2024/10/14 15:02:28 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,6 +51,7 @@ void _wait_pipeline(t_vec_pid pids, t_state *state, t_pipeline_result *out) waitpid_status = 0; while (waitpid(pids.buffer[pids.len - 1], &waitpid_status, 0) < 0 \ && errno != ECHILD) + ; if (errno == ECHILD) waitpid_status = 0; while (waitpid(-1, NULL, 0) != -1) diff --git a/sources/_helper_main.c b/sources/_helper_main.c index 0da40c4c..b26ac60d 100644 --- a/sources/_helper_main.c +++ b/sources/_helper_main.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/06 16:31:41 by rparodi #+# #+# */ -/* Updated: 2024/10/12 17:52:01 by rparodi ### ########.fr */ +/* Updated: 2024/10/14 15:03:01 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -67,7 +67,7 @@ void exec_shcat(t_state *state) if (state->ast != NULL && run_program(&state->ast->data.program, state, &prog_res)) { - printf("Error when execting the Command \n"); + printf("Error when executing the cmd\n"); state->last_exit = 127; } ast_free(state->ast); diff --git a/sources/main.c b/sources/main.c index 67cfaae8..8d99ab8f 100644 --- a/sources/main.c +++ b/sources/main.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */ -/* Updated: 2024/10/14 14:01:19 by maiboyer ### ########.fr */ +/* Updated: 2024/10/14 15:13:06 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,18 +65,17 @@ t_error split_str_first(t_const_str s, char splitter, t_str *before, return (NO_ERROR); } -void append_default_env(t_hashmap_env *env) +void append_default_env(t_hashmap_env *env) { - t_str *tmp; - t_str key; - t_str tmp2; - t_u64 shlvl; - + t_str *tmp; + t_str key; + t_str tmp2; + t_u64 shlvl; + key = "SHLVL"; shlvl = 0; tmp = hmap_env_get(env, &key); (void)(tmp != NULL && str_to_u64(*tmp, 10, &shlvl)); - (void)((tmp != NULL) && (str_free(*tmp), 1)); if (u64_to_str(shlvl + 1, &tmp2)) me_abort("Failed to set SHLVL"); hmap_env_insert(env, str_clone(key), tmp2); @@ -84,7 +83,6 @@ void append_default_env(t_hashmap_env *env) tmp = hmap_env_get(env, &key); if (tmp == NULL) hmap_env_insert(env, str_clone(key), str_clone(DFT_PATH)); - } t_error populate_env(t_hashmap_env *env, t_str envp[]) diff --git a/sources/signal_handler.c b/sources/signal_handler.c index 12de04c9..7df98c7a 100644 --- a/sources/signal_handler.c +++ b/sources/signal_handler.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/02 13:22:14 by maiboyer #+# #+# */ -/* Updated: 2024/10/13 17:56:04 by maiboyer ### ########.fr */ +/* Updated: 2024/10/14 15:03:10 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,9 +16,9 @@ t_error install_signal(void) { - //if (signal(SIGINT, SIG_IGN)) - // return (ERROR); - //if (signal(SIGQUIT, SIG_IGN)) - // return (ERROR); + if (signal(SIGINT, SIG_IGN)) + return (ERROR); + if (signal(SIGQUIT, SIG_IGN)) + return (ERROR); return (NO_ERROR); } diff --git a/stdme/src/mem/allocator.c b/stdme/src/mem/allocator.c index f25a667b..a18a965e 100644 --- a/stdme/src/mem/allocator.c +++ b/stdme/src/mem/allocator.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/14 18:26:27 by maiboyer #+# #+# */ -/* Updated: 2024/10/12 17:52:32 by rparodi ### ########.fr */ +/* Updated: 2024/10/14 14:48:10 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ t_allocator *global_allocator(void) if (!init) { init = true; - global_alloc = m_init(); + global_alloc = lc_init(); } return (&global_alloc); }