update: should handle built-in variables

This commit is contained in:
maix0 2024-10-06 14:37:54 +02:00
parent b058348d35
commit d459a8b8dc
11 changed files with 81 additions and 24 deletions

View file

@ -6,7 +6,7 @@
# By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ # # By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/04/28 17:28:30 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 ANAME = sh
BUILD_DIR ?= $(shell realpath ./build/) 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 CFLAGS_ADDITIONAL
export CC export CC
export BASE_PATH 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_FILES = $(addprefix $(BUILD_DIR)/, $(addsuffix .a, $(addprefix lib, $(LIBS_NAMES))))
LIBS_FLAGS = $(addprefix -l, $(LIBS_NAMES)) LIBS_FLAGS = $(addprefix -l, $(LIBS_NAMES))
all: all:
@$(MAKE) -C ./stdme/ "LIB_NAME=$(shell realpath ./stdme)/" libme.a @$(MAKE) -C ./stdme/ "LIB_NAME=$(shell realpath ./stdme)/" libme.a
@$(MAKE) -C ./allocator/ "LIB_NAME=$(shell realpath ./allocator)/" libaq.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 ./line/ "LIB_NAME=$(shell realpath ./line)/" libline.a
@$(MAKE) -C ./parser/ "LIB_NAME=$(shell realpath ./parser)/" libparser.a @$(MAKE) -C ./parser/ "LIB_NAME=$(shell realpath ./parser)/" libparser.a
@$(MAKE) -f./Minishell.mk lib$(ANAME).a @$(MAKE) -f./Minishell.mk lib$(ANAME).a
@$(MAKE) -f./Minishell.mk $(NAME) @$(MAKE) -f./Minishell.mk "NAME=$(NAME)" $(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))
bonus: all
# Dependences for all # Dependences for all
$(NAME): $(LIBS_FILES) $(NAME): $(LIBS_FILES)

View file

@ -20,6 +20,7 @@ run_ast/_ast_into_str3 \
run_ast/_ast_into_str4 \ run_ast/_ast_into_str4 \
run_ast/_ast_into_str5 \ run_ast/_ast_into_str5 \
run_ast/_ast_into_str6 \ run_ast/_ast_into_str6 \
run_ast/_get_pid \
run_ast/_run_exit_code \ run_ast/_run_exit_code \
run_ast/_run_exp_operators \ run_ast/_run_exp_operators \
run_ast/_spawn_cmd \ run_ast/_spawn_cmd \

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* _get_pid.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/14 12:31:28 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) if (cmd_res.process.stderr != NULL)
close_fd(cmd_res.process.stderr); close_fd(cmd_res.process.stderr);
*out = cmd_res.exit; *out = cmd_res.exit;
state->last_exit = *out;
return (NO_ERROR); return (NO_ERROR);
} }
@ -54,6 +55,7 @@ t_error _run_other(t_ast_node self, t_state *state, int *out)
return (ERROR); return (ERROR);
*out = subshell_res.exit; *out = subshell_res.exit;
} }
state->last_exit = *out;
return (NO_ERROR); return (NO_ERROR);
} }

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/14 12:30:09 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); out->exit = WEXITSTATUS(status);
if (WIFSIGNALED(status)) if (WIFSIGNALED(status))
out->exit = WTERMSIG(status); out->exit = WTERMSIG(status);
state->last_exit = out->exit;
return (NO_ERROR); return (NO_ERROR);
} }

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/14 12:38:38 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/str/str.h"
#include "me/convert/numbers_to_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) bool _is_special_var(t_ast_expansion *self)
{ {
char name; 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}; *out = (t_expansion_result){.exists = false, .value = NULL};
if (name == '?') 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); return (NO_ERROR);
} }

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/14 12:34:33 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 else
out->exit = left; out->exit = left;
state->last_exit = out->exit;
return (NO_ERROR); return (NO_ERROR);
} }

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/14 12:32:37 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); 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; int waitpid_status;
@ -59,6 +59,7 @@ void _wait_pipeline(t_vec_pid pids, t_pipeline_result *out)
} }
else else
out->exit = 127; out->exit = 127;
state->last_exit = out->exit;
vec_pid_free(pids); 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,"\ ret |= ((void)(printf("List in pipelines are unsupported,"\
" use a subshell !\n")), ERROR); " use a subshell !\n")), ERROR);
} }
return (_wait_pipeline(pids, out), ret); return (_wait_pipeline(pids, state, out), ret);
} }

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/14 12:35:02 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 15:49:56 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; t_hashmap_env *tmp_var;
void *parser; void *parser;
t_ast_node ast; t_ast_node ast;
t_i32 last_exit;
}; };
#endif /* STATE_H */ #endif /* STATE_H */

View file

@ -1,18 +1,18 @@
# **************************************************************************** # # **************************************************************************** #
# # # #
# ::: :::::::: # # ::: :::::::: #
# Parser.mk :+: :+: :+: # # Makefile :+: :+: :+: #
# +:+ +:+ +:+ # # +:+ +:+ +:+ #
# By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ # # By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/11/03 13:20:01 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 ANAME = parser
BUILD_DIR = ../build BUILD_DIR ?= ../build
SRC_DIR = ./src SRC_DIR = ./src
GEN_DIR = ./generic GEN_DIR = ./generic