From 7b9009b820818790407238c3b4f22faeeb44cbfc Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Mon, 14 Oct 2024 13:59:07 +0200 Subject: [PATCH] update: fixed env -i issue --- exec/src/run_ast/_ast_into_str.c | 22 +------------------ sources/main.c | 36 +++++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/exec/src/run_ast/_ast_into_str.c b/exec/src/run_ast/_ast_into_str.c index 73055a4b..3a445a51 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/13 17:30:06 by maiboyer ### ########.fr */ +/* Updated: 2024/10/14 12:34:25 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,26 +21,6 @@ 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_no_quote(t_state *state, t_word_result *res, t_vec_str *append) -{ - t_string tmp; - t_usize i; - - tmp = string_new(64); - i = 0; - while (i < res->value.len) - { - if (!res->value.buffer[i].do_expand) - string_push(&tmp, res->value.buffer[i].value); - else if (_word_into_str_inner((struct s_word_str_args){i, \ - state, &tmp, res, append, NULL, NULL, NULL})) - return (ERROR); - i++; - } - vec_str_push(append, tmp.buf); - return (NO_ERROR); -} - t_error _word_pass_quote(t_state *state, t_word_result *res, t_vec_str *append) { t_string tmp; diff --git a/sources/main.c b/sources/main.c index fa90194b..2828239e 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/13 17:55:56 by maiboyer ### ########.fr */ +/* Updated: 2024/10/14 13:57:51 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,8 @@ #include "parser/token.h" #include #include +#include "me/convert/str_to_numbers.h" +#include "me/convert/numbers_to_str.h" t_error get_user_input(t_state *state); void ast_print_node(t_ast_node self); @@ -35,6 +37,8 @@ void ft_exit(t_state *maiboyerlpb, t_u8 exit_status); void exec_shcat(t_state *state); void ft_take_args(t_state *state); +#define DFT_PATH "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" + // Foutre envp dans env // Chaque elemenet d'envp split au premier = // cle avant le = @@ -61,6 +65,28 @@ 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) +{ + 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), str_clone(tmp2)); + key = "PATH"; + 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[]) { t_usize i; @@ -68,17 +94,17 @@ t_error populate_env(t_hashmap_env *env, t_str envp[]) i = 0; if (envp == NULL || env == NULL) - return (printf("given a nullptr\n"), ERROR); + return (ERROR); while (envp[i] != NULL) { if (split_str_first(envp[i], '=', &temp[0], &temp[1])) return (ERROR); if (temp[0] == NULL || temp[1] == NULL) - return (printf("TEMP NULL\n"), ERROR); - if (hmap_env_insert(env, temp[0], temp[1])) - printf("'%s' was already in the hmap ?????\n", temp[0]); + return (ERROR); + (hmap_env_insert(env, temp[0], temp[1])); i++; } + append_default_env(env); return (NO_ERROR); }