From 85910da2fd666667b3d339b4a01cdb6095b7e4dd Mon Sep 17 00:00:00 2001 From: Maix0 Date: Sat, 10 Aug 2024 20:02:12 +0200 Subject: [PATCH] did env, exit and export (only env printing) --- exec/src/builtins/env.c | 32 ++++++++++++++++++++++++++++++-- exec/src/builtins/exit.c | 16 ++++++++++------ exec/src/builtins/export.c | 11 +++-------- 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/exec/src/builtins/env.c b/exec/src/builtins/env.c index 4f58deae..9e1866c6 100644 --- a/exec/src/builtins/env.c +++ b/exec/src/builtins/env.c @@ -6,14 +6,42 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/07 14:22:50 by rparodi #+# #+# */ -/* Updated: 2024/08/10 19:44:43 by maiboyer ### ########.fr */ +/* Updated: 2024/08/10 19:57:32 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ -#include "me/types.h" #include "exec/builtins.h" +#include "me/hashmap/hashmap_env.h" +#include "me/printf/printf.h" +#include "me/types.h" + +struct s_print_env_state +{ + t_state *state; + t_spawn_info *info; +}; + +static t_error _print_env(t_usize _idx, const t_str *key, t_str *value, void *vctx) +{ + const struct s_print_env_state *ctx = vctx; + t_str *val; + + val = hmap_env_get(ctx->state->tmp_var, (t_str *)key); + if (val == NULL) + val = value; + if (val == NULL || *val == NULL) + return (NO_ERROR); + // TODO: Fix this to handle the corrrect output + me_printf_fd(NULL, "%s=%s\n", *key, *val); + return (NO_ERROR); +} t_error builtin_env___(t_state *state, t_spawn_info info) { + struct s_print_env_state ctx; + + ctx.info = &info; + ctx.state = state; + hmap_env_iter(state->env, _print_env, &ctx); return (NO_ERROR); } diff --git a/exec/src/builtins/exit.c b/exec/src/builtins/exit.c index 37722bb0..66870ed7 100644 --- a/exec/src/builtins/exit.c +++ b/exec/src/builtins/exit.c @@ -6,18 +6,22 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/10 18:43:18 by maiboyer #+# #+# */ -/* Updated: 2024/08/10 19:49:14 by maiboyer ### ########.fr */ +/* Updated: 2024/08/10 20:01:36 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec/builtins.h" -#include "me/printf/printf.h" -#include "me/str/str.h" -#include "me/string/string.h" +#include "me/convert/str_to_numbers.h" #include "me/types.h" t_error builtin_exit__(t_state *state, t_spawn_info info) { - return (ERROR); -} + t_i32 exit_code; + if (info.arguments.len < 2) + exit_code = 0; + else if (str_to_i32(info.arguments.buffer[1], 10, &exit_code)) + return (ERROR); + me_exit(exit_code); + return (NO_ERROR); +} diff --git a/exec/src/builtins/export.c b/exec/src/builtins/export.c index 6341c5d5..5beaf67c 100644 --- a/exec/src/builtins/export.c +++ b/exec/src/builtins/export.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/07 14:13:41 by rparodi #+# #+# */ -/* Updated: 2024/08/10 19:43:58 by maiboyer ### ########.fr */ +/* Updated: 2024/08/10 19:58:12 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,12 +16,7 @@ t_error builtin_export(t_state *state, t_spawn_info info) { if (info.arguments.len == 1) - { - // print env - } - else - { - // assign variable - } + return (builtin_env___(state, info)); + return (NO_ERROR); }