From 695b402e92e6f2b79295ccbc6439afcc39a7c5d4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Luk=C3=A1=C5=A1=20Ji=C5=99i=C5=A1t=C4=9B?= Date: Tue, 27 Aug 2024 15:09:13 +0200 Subject: [PATCH] Add exit codes, make exit a "normal" built-in --- Makefile | 1 + inc/builtins.h | 3 ++- inc/minishell_structs.h | 4 +++- src/builtins/exit.c | 50 +++++++++++++++++++++++++++++++++++++++++ src/env.c | 3 ++- src/execution.c | 8 ++++++- src/main.c | 14 +++++++----- 7 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 src/builtins/exit.c diff --git a/Makefile b/Makefile index 71683a0..b100a97 100644 --- a/Makefile +++ b/Makefile @@ -40,6 +40,7 @@ SOURCES := main.c \ builtins/env.c \ builtins/export.c \ builtins/unset.c \ + builtins/exit.c \ SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES)) diff --git a/inc/builtins.h b/inc/builtins.h index 0afbd1d..30ee6b6 100644 --- a/inc/builtins.h +++ b/inc/builtins.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/26 11:39:54 by ljiriste #+# #+# */ -/* Updated: 2024/08/26 11:51:32 by ljiriste ### ########.fr */ +/* Updated: 2024/08/27 15:02:28 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,7 @@ int pwd(void); int ft_env(int argc, t_execution_env *env); int export(int argc, char **argv, t_execution_env *env); int unset(int argc, char **argv, t_execution_env *env); +int execute_exit(int argc, char **argv, t_execution_env *env); void unset_single(const char *var_name, t_execution_env *env); diff --git a/inc/minishell_structs.h b/inc/minishell_structs.h index 4b8b980..20b2085 100644 --- a/inc/minishell_structs.h +++ b/inc/minishell_structs.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/26 09:08:46 by ljiriste #+# #+# */ -/* Updated: 2024/08/27 13:37:07 by ljiriste ### ########.fr */ +/* Updated: 2024/08/27 15:03:52 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,6 +31,8 @@ typedef struct s_execution_env int stdin_fd; int stdout_fd; int ret_val; + int may_exit; + int exit; int last_was_builtin; int last_builtin_ret_val; t_vars *vars; diff --git a/src/builtins/exit.c b/src/builtins/exit.c new file mode 100644 index 0000000..7a978a2 --- /dev/null +++ b/src/builtins/exit.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/27 14:49:17 by ljiriste #+# #+# */ +/* Updated: 2024/08/27 15:05:25 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "builtins.h" +#include "minishell_structs.h" +#include + +int is_numeric(const char *str) +{ + size_t i; + + i = 0; + if (str[0] == '+' || str[0] == '-') + ++i; + while (str[i]) + { + if (!ft_isdigit(str[i])) + return (0); + ++i; + } + return (1); +} + +int execute_exit(int argc, char **argv, t_execution_env *env) +{ + if (env->may_exit) + env->exit = 1; + if (argc == 1) + return (0); + if (argc > 2) + { + ft_dprintf(STDERR_FILENO, "exit: too many arguments\n"); + return (1); + } + if (!is_numeric(argv[1])) + { + ft_dprintf(STDERR_FILENO, "exit: %s: numeric argument required\n", argv[1]); + return (2); + } + return (ft_atoi(argv[1])); +} diff --git a/src/env.c b/src/env.c index 936d7ba..dca713f 100644 --- a/src/env.c +++ b/src/env.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/21 21:24:52 by ljiriste #+# #+# */ -/* Updated: 2024/08/27 10:34:14 by ljiriste ### ########.fr */ +/* Updated: 2024/08/27 15:02:02 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,7 @@ int init_env(t_execution_env *env, char **envp) env->stdin_fd = STDIN_FILENO; env->stdout_fd = STDOUT_FILENO; env->ret_val = 0; + env->exit = 0; env->vars = malloc(sizeof(t_vars)); if (!env->vars) return (1); diff --git a/src/execution.c b/src/execution.c index 94df96d..a60991e 100644 --- a/src/execution.c +++ b/src/execution.c @@ -6,7 +6,7 @@ /* By: lnikolov last_builtin_ret_val = export(count_fields(fields), fields, env); else if (!ft_strcmp(fields[0], "unset")) env->last_builtin_ret_val = unset(count_fields(fields), fields, env); + else if (!ft_strcmp(fields[0], "exit")) + env->last_builtin_ret_val = execute_exit(count_fields(fields), fields, env); else return (1); env->last_was_builtin = 1; @@ -985,8 +989,10 @@ int ex_pipeline(t_parse_tree_node *pipeline, t_execution_env *env, int depth) if (pipeline->children.size == 1) { + env->may_exit = (depth == 0); if (ex_command(ft_vec_access(&pipeline->children, 0), env)) return (1); + env->may_exit = 0; } else { diff --git a/src/main.c b/src/main.c index 8d7e2db..7231928 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/26 13:11:47 by ljiriste #+# #+# */ -/* Updated: 2024/08/27 14:31:19 by ljiriste ### ########.fr */ +/* Updated: 2024/08/27 15:08:06 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,6 +31,7 @@ int main(int argc, char **argv) int main(int argc, __attribute__((unused)) char **argv, char **envp) { + int res; char *line; t_execution_env env; @@ -44,18 +45,21 @@ int main(int argc, __attribute__((unused)) char **argv, char **envp) clean_env(&env); return (2); } - while (1) + while (env.exit == 0) { line = get_line(); - if (!line || !ft_strncmp(line, "exit", 4)) + if (!line) + { + env.ret_val = 0; break ; + } handle_input(&line, &env); free(line); } + res = env.ret_val; clean_env(&env); parse(NULL, NULL); terminate_input(); - free(line); ft_printf("exit\n"); - return (0); + return (res); } -- 2.30.2