diff --git a/Minishell.mk b/Minishell.mk index 8955a6ab..07cb1176 100644 --- a/Minishell.mk +++ b/Minishell.mk @@ -6,7 +6,7 @@ # By: maiboyer +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/04/28 17:28:30 by maiboyer #+# #+# # -# Updated: 2024/10/03 21:41:55 by maiboyer ### ########.fr # +# Updated: 2024/10/06 14:33:24 by maiboyer ### ########.fr # # # # **************************************************************************** # @@ -18,6 +18,12 @@ link_group = -Wl,--start-group $(1) -Wl,--end-group ANAME = sh BUILD_DIR ?= $(shell realpath ./build/) +ifeq ($(MAKECMDGOALS), bonus) + CFLAGS_ADDITIONAL += -DBONUS=1 + BUILD_DIR := $(BUILD_DIR)/bonus + NAME := $(NAME)_bonus +endif + export CFLAGS_ADDITIONAL export CC export BASE_PATH @@ -57,7 +63,6 @@ LIBS_NAMES = me aq ast parser line exec sh LIBS_FILES = $(addprefix $(BUILD_DIR)/, $(addsuffix .a, $(addprefix lib, $(LIBS_NAMES)))) LIBS_FLAGS = $(addprefix -l, $(LIBS_NAMES)) - all: @$(MAKE) -C ./stdme/ "LIB_NAME=$(shell realpath ./stdme)/" libme.a @$(MAKE) -C ./allocator/ "LIB_NAME=$(shell realpath ./allocator)/" libaq.a @@ -66,16 +71,9 @@ all: @$(MAKE) -C ./line/ "LIB_NAME=$(shell realpath ./line)/" libline.a @$(MAKE) -C ./parser/ "LIB_NAME=$(shell realpath ./parser)/" libparser.a @$(MAKE) -f./Minishell.mk lib$(ANAME).a - @$(MAKE) -f./Minishell.mk $(NAME) - -# Bonus (make bonus) -bonus: $(OBJ) $(LIBS_FILES) - @mkdir -p $(BUILD_DIR) - @mkdir -p $(BUILD_DIR)/$(SRCDIRNAME) - @echo -e '$(GREY) Be Carefull ur in $(END)$(GREEN)Debug Mode$(END)' - @echo -e '$(GREY) Linking \t$(END)$(GOLD)$(NAME)$(END)' - @$(CC) $(CFLAGS) -DDEBUG=1 -o $(NAME) $(OBJ) -L$(BUILD_DIR) $(call link_group,$(LIBS_FLAGS)) + @$(MAKE) -f./Minishell.mk "NAME=$(NAME)" $(NAME) +bonus: all # Dependences for all $(NAME): $(LIBS_FILES) diff --git a/exec/Filelist.exec.mk b/exec/Filelist.exec.mk index 1dce545c..0e672409 100644 --- a/exec/Filelist.exec.mk +++ b/exec/Filelist.exec.mk @@ -20,6 +20,7 @@ run_ast/_ast_into_str3 \ run_ast/_ast_into_str4 \ run_ast/_ast_into_str5 \ run_ast/_ast_into_str6 \ +run_ast/_get_pid \ run_ast/_run_exit_code \ run_ast/_run_exp_operators \ run_ast/_spawn_cmd \ diff --git a/exec/src/run_ast/_get_pid.c b/exec/src/run_ast/_get_pid.c new file mode 100644 index 00000000..dec9dd8b --- /dev/null +++ b/exec/src/run_ast/_get_pid.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* _get_pid.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/06 14:25:20 by maiboyer #+# #+# */ +/* Updated: 2024/10/06 14:27:40 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/os/os.h" +#include "me/types.h" + +#define BONUS 1 + +#if BONUS +t_pid get_self_pid(void) +{ + return (getpid()); +} +#else +t_pid get_self_pid(void) +{ + return (1); +} + +#endif diff --git a/exec/src/run_ast/_run_exit_code.c b/exec/src/run_ast/_run_exit_code.c index 7a8cf842..ee0b734a 100644 --- a/exec/src/run_ast/_run_exit_code.c +++ b/exec/src/run_ast/_run_exit_code.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/14 12:31:28 by maiboyer #+# #+# */ -/* Updated: 2024/09/16 18:28:48 by maiboyer ### ########.fr */ +/* Updated: 2024/10/06 14:22:08 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,7 @@ t_error _run_cmd(t_ast_node self, t_state *state, int *out) if (cmd_res.process.stderr != NULL) close_fd(cmd_res.process.stderr); *out = cmd_res.exit; + state->last_exit = *out; return (NO_ERROR); } @@ -54,6 +55,7 @@ t_error _run_other(t_ast_node self, t_state *state, int *out) return (ERROR); *out = subshell_res.exit; } + state->last_exit = *out; return (NO_ERROR); } diff --git a/exec/src/run_ast/_spawn_cmd.c b/exec/src/run_ast/_spawn_cmd.c index 257fa050..c755b3a5 100644 --- a/exec/src/run_ast/_spawn_cmd.c +++ b/exec/src/run_ast/_spawn_cmd.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/14 12:30:09 by maiboyer #+# #+# */ -/* Updated: 2024/09/18 21:50:18 by maiboyer ### ########.fr */ +/* Updated: 2024/10/06 14:19:40 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -76,6 +76,7 @@ t_error _spawn_cmd_and_run_end(\ out->exit = WEXITSTATUS(status); if (WIFSIGNALED(status)) out->exit = WTERMSIG(status); + state->last_exit = out->exit; return (NO_ERROR); } diff --git a/exec/src/run_ast/run_expansion_builtin.c b/exec/src/run_ast/run_expansion_builtin.c index 01f407c3..62115ed6 100644 --- a/exec/src/run_ast/run_expansion_builtin.c +++ b/exec/src/run_ast/run_expansion_builtin.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/14 12:38:38 by maiboyer #+# #+# */ -/* Updated: 2024/09/14 12:42:53 by maiboyer ### ########.fr */ +/* Updated: 2024/10/06 14:28:10 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,9 @@ #include "me/str/str.h" #include "me/convert/numbers_to_str.h" +// non bonus only returns 1 +t_pid get_self_pid(void); + bool _is_special_var(t_ast_expansion *self) { char name; @@ -50,7 +53,27 @@ t_error _run_expansion_special_var(t_ast_expansion *self, t_state *state, *out = (t_expansion_result){.exists = false, .value = NULL}; if (name == '?') { - printf("PLEASE MAKE SURE TO FINISH THE SPECIAL VAR HANDLING !"); + *out = (t_expansion_result){.exists = true, .value = NULL}; + if (i32_to_str(state->last_exit, &out->value)) + return (ERROR); + } + if (name == '#') + { + *out = (t_expansion_result){.exists = true, .value = NULL}; + if (i32_to_str(1, &out->value)) + return (ERROR); + } + if (name == '!') + { + *out = (t_expansion_result){.exists = true, .value = NULL}; + if (i32_to_str(1, &out->value)) + return (ERROR); + } + if (name == '$') + { + *out = (t_expansion_result){.exists = true, .value = NULL}; + if (i32_to_str(get_self_pid(), &out->value)) + return (ERROR); } return (NO_ERROR); } diff --git a/exec/src/run_ast/run_list.c b/exec/src/run_ast/run_list.c index fb3fbc19..227db3b0 100644 --- a/exec/src/run_ast/run_list.c +++ b/exec/src/run_ast/run_list.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/14 12:34:33 by maiboyer #+# #+# */ -/* Updated: 2024/09/16 19:09:53 by maiboyer ### ########.fr */ +/* Updated: 2024/10/06 14:20:55 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -56,5 +56,6 @@ t_error run_list(t_ast_list *list, t_state *state, t_list_result *out) } else out->exit = left; + state->last_exit = out->exit; return (NO_ERROR); } diff --git a/exec/src/run_ast/run_pipeline.c b/exec/src/run_ast/run_pipeline.c index 8fadd697..1c64dd48 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/09/18 21:45:48 by maiboyer ### ########.fr */ +/* Updated: 2024/10/06 14:20:43 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,7 +41,7 @@ void _append_redir_to_pipeline(t_ast_pipeline *pipeline) vec_ast_push(append, tmp_ast); } -void _wait_pipeline(t_vec_pid pids, t_pipeline_result *out) +void _wait_pipeline(t_vec_pid pids, t_state *state, t_pipeline_result *out) { int waitpid_status; @@ -59,6 +59,7 @@ void _wait_pipeline(t_vec_pid pids, t_pipeline_result *out) } else out->exit = 127; + state->last_exit = out->exit; vec_pid_free(pids); } @@ -133,5 +134,5 @@ t_error run_pipeline(t_ast_pipeline *pipeline, t_state *state, ret |= ((void)(printf("List in pipelines are unsupported,"\ " use a subshell !\n")), ERROR); } - return (_wait_pipeline(pids, out), ret); + return (_wait_pipeline(pids, state, out), ret); } diff --git a/exec/src/run_ast/run_subshell.c b/exec/src/run_ast/run_subshell.c index 962cfd02..7293b121 100644 --- a/exec/src/run_ast/run_subshell.c +++ b/exec/src/run_ast/run_subshell.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/14 12:35:02 by maiboyer #+# #+# */ -/* Updated: 2024/09/16 19:02:01 by maiboyer ### ########.fr */ +/* Updated: 2024/10/06 14:21:41 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/includes/app/state.h b/includes/app/state.h index 8fb2e0ea..47ef71cf 100644 --- a/includes/app/state.h +++ b/includes/app/state.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/02 15:49:56 by maiboyer #+# #+# */ -/* Updated: 2024/09/26 18:10:23 by maiboyer ### ########.fr */ +/* Updated: 2024/10/06 14:20:09 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,7 @@ struct s_state t_hashmap_env *tmp_var; void *parser; t_ast_node ast; + t_i32 last_exit; }; #endif /* STATE_H */ diff --git a/parser/Makefile b/parser/Makefile index ad036000..54d55a8f 100644 --- a/parser/Makefile +++ b/parser/Makefile @@ -1,18 +1,18 @@ # **************************************************************************** # # # # ::: :::::::: # -# Parser.mk :+: :+: :+: # +# Makefile :+: :+: :+: # # +:+ +:+ +:+ # # By: maiboyer +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/03 13:20:01 by maiboyer #+# #+# # -# Updated: 2024/08/02 18:57:55 by maiboyer ### ########.fr # +# Updated: 2024/10/06 14:33:59 by maiboyer ### ########.fr # # # # **************************************************************************** # ANAME = parser -BUILD_DIR = ../build +BUILD_DIR ?= ../build SRC_DIR = ./src GEN_DIR = ./generic