From 869eb0d857c5857e9c9166f0c1d909b0dc08ce71 Mon Sep 17 00:00:00 2001 From: whaffman Date: Sat, 8 Feb 2025 14:43:53 +0100 Subject: [PATCH] Environment environment: rename functions environment: add get_arr function builtins: add export builtin --- README.md | 4 +- inc/environment.h | 13 +++--- .../{add_environment.c => environment_add.c} | 4 +- ...{free_environment.c => environment_free.c} | 4 +- .../{get_environment.c => environment_get.c} | 6 +-- src/environment/environment_get_arr.c | 42 +++++++++++++++++++ ...arse_environment.c => environment_parse.c} | 6 +-- ...rint_environment.c => environment_print.c} | 28 ++++++++----- src/main.c | 5 ++- src/prompt/prompt.c | 18 ++++---- src/utils/free_minishell.c | 2 +- src/utils/simple_builtins.c | 38 ++++++++++++++++- 12 files changed, 130 insertions(+), 40 deletions(-) rename src/environment/{add_environment.c => environment_add.c} (91%) rename src/environment/{free_environment.c => environment_free.c} (91%) rename src/environment/{get_environment.c => environment_get.c} (87%) create mode 100644 src/environment/environment_get_arr.c rename src/environment/{parse_environment.c => environment_parse.c} (86%) rename src/environment/{print_environment.c => environment_print.c} (55%) diff --git a/README.md b/README.md index 9467a3f..6ae1227 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,9 @@ A lot of amazing shell stuff - Find absolute path for command input ('/', './', 'cmd') - Add heredoc to tokenizer -[x] Environment to `t_list` -- Get environment array (export) +-[x] Get environment array (export) +-[x] Set environment variable (export) +-[x] Simple builtin export - Preliminary signals - Define struct for commands, something like ( ```c diff --git a/inc/environment.h b/inc/environment.h index 64cd4b0..7dd7beb 100644 --- a/inc/environment.h +++ b/inc/environment.h @@ -13,10 +13,13 @@ #ifndef environment_H # define environment_H -void add_environment(t_list **environment, char *name, char *value); -void print_environment(t_list *environment); -char *get_environment(t_list *environment, char *name); -void free_environment(t_list *environment); -int parse_environment(char **envp, t_list **environment); +# include "minishell.h" + +void environment_add(t_list **environment, char *name, char *value); +void environment_print(t_list *environment); +t_environment *environment_get(t_list *environment, char *name); +void environment_free(t_list *environment); +int environment_parse(char **envp, t_list **environment); +char **environment_get_arr(t_list *environment); #endif // environment_H diff --git a/src/environment/add_environment.c b/src/environment/environment_add.c similarity index 91% rename from src/environment/add_environment.c rename to src/environment/environment_add.c index 7cae6be..4ab1252 100644 --- a/src/environment/add_environment.c +++ b/src/environment/environment_add.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* add_environment.c :+: :+: */ +/* environment_add.c :+: :+: */ /* +:+ */ /* By: whaffman +#+ */ /* +#+ */ @@ -12,7 +12,7 @@ #include "minishell.h" -void add_environment(t_list **environment, char *name, char *value) +void environment_add(t_list **environment, char *name, char *value) { t_environment *new_environment; t_list *new_node; diff --git a/src/environment/free_environment.c b/src/environment/environment_free.c similarity index 91% rename from src/environment/free_environment.c rename to src/environment/environment_free.c index 182f217..09d8811 100644 --- a/src/environment/free_environment.c +++ b/src/environment/environment_free.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* free_environment.c :+: :+: */ +/* environment_free.c :+: :+: */ /* +:+ */ /* By: whaffman +#+ */ /* +#+ */ @@ -12,7 +12,7 @@ #include "minishell.h" -void free_environment(t_list *environment) +void environment_free(t_list *environment) { t_list *tmp; diff --git a/src/environment/get_environment.c b/src/environment/environment_get.c similarity index 87% rename from src/environment/get_environment.c rename to src/environment/environment_get.c index 607d045..be1f1de 100644 --- a/src/environment/get_environment.c +++ b/src/environment/environment_get.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* get_environment.c :+: :+: */ +/* environment_get.c :+: :+: */ /* +:+ */ /* By: whaffman +#+ */ /* +#+ */ @@ -12,14 +12,14 @@ #include "minishell.h" -char *get_environment(t_list *environment, char *name) +t_environment *environment_get(t_list *environment, char *name) { t_environment *env; while (environment != NULL) { env = (t_environment *)environment->content; if (ft_strcmp(env->name, name) == 0) - return (env->value); + return (env); environment = environment->next; } return (NULL); diff --git a/src/environment/environment_get_arr.c b/src/environment/environment_get_arr.c new file mode 100644 index 0000000..824a9c3 --- /dev/null +++ b/src/environment/environment_get_arr.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* environment_get_arr.c :+: :+: */ +/* +:+ */ +/* By: willem +#+ */ +/* +#+ */ +/* Created: 2025/02/08 13:59:48 by willem #+# #+# */ +/* Updated: 2025/02/08 13:59:49 by willem ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +# include "minishell.h" + +char **environment_get_arr(t_list *environment) +{ + char **arr; + t_environment *env; + int i; + + arr = malloc(sizeof(char *) * (ft_lstsize(environment) + 1)); + if (arr == NULL) + return (NULL); + i = 0; + while (environment != NULL) + { + env = (t_environment *)environment->content; + arr[i] = malloc(ft_strlen(env->name) + ft_strlen(env->value) + 2); + if (arr[i] == NULL) + { + ft_free_arr(arr); + return (NULL); + } + ft_strlcpy(arr[i], env->name, ft_strlen(env->name) + 1); + ft_strlcat(arr[i], "=", ft_strlen(env->name) + 2); + ft_strlcat(arr[i], env->value, ft_strlen(env->name) + ft_strlen(env->value) + 2); + environment = environment->next; + i++; + } + arr[i] = NULL; + return (arr); +} diff --git a/src/environment/parse_environment.c b/src/environment/environment_parse.c similarity index 86% rename from src/environment/parse_environment.c rename to src/environment/environment_parse.c index ad97d79..ffbfd9d 100644 --- a/src/environment/parse_environment.c +++ b/src/environment/environment_parse.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* parse_environment.c :+: :+: */ +/* environment_parse.c :+: :+: */ /* +:+ */ /* By: whaffman +#+ */ /* +#+ */ @@ -12,7 +12,7 @@ #include "minishell.h" -int parse_environment(char **envp, t_list **environment) +int environment_parse(char **envp, t_list **environment) { char **env; @@ -22,7 +22,7 @@ int parse_environment(char **envp, t_list **environment) while (*envp != NULL) { env = ft_split(*envp, '='); - add_environment(environment, env[0], env[1]); + environment_add(environment, env[0], env[1]); ft_free_arr(env); envp++; } diff --git a/src/environment/print_environment.c b/src/environment/environment_print.c similarity index 55% rename from src/environment/print_environment.c rename to src/environment/environment_print.c index e5edb88..77c513c 100644 --- a/src/environment/print_environment.c +++ b/src/environment/environment_print.c @@ -1,25 +1,33 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* print_environment.c :+: :+: */ +/* environment_print.c :+: :+: */ /* +:+ */ -/* By: whaffman +#+ */ +/* By: willem +#+ */ /* +#+ */ -/* Created: 2025/02/04 16:13:04 by whaffman #+# #+# */ -/* Updated: 2025/02/05 15:52:44 by whaffman ######## odam.nl */ +/* Created: 2025/02/08 13:52:08 by willem #+# #+# */ +/* Updated: 2025/02/08 14:24:20 by willem ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -void print_environment(t_list *environment) +void environment_print(t_list *environment) { - t_environment *env; + char **arr; + int i; - while (environment != NULL) + arr = environment_get_arr(environment); + if (arr == NULL) { - env = (t_environment *)environment->content; - printf("%s=%s\n", env->name, env->value); - environment = environment->next; + perror("malloc"); + return ; } + i = 0; + while (arr[i] != NULL) + { + printf("%s\n", arr[i]); + i++; + } + ft_free_arr(arr); } \ No newline at end of file diff --git a/src/main.c b/src/main.c index cc4a0bf..42e926c 100644 --- a/src/main.c +++ b/src/main.c @@ -6,12 +6,13 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/04 16:19:22 by whaffman #+# #+# */ -/* Updated: 2025/02/05 17:06:47 by whaffman ######## odam.nl */ +/* Updated: 2025/02/08 14:09:11 by willem ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" + int main(int argc, char **argv, char **envp) { t_minishell *minishell; @@ -21,7 +22,7 @@ int main(int argc, char **argv, char **envp) print_banner(); ft_load_history(); minishell = init_minishell(); - parse_environment(envp, &(minishell->environment)); + environment_parse(envp, &(minishell->environment)); while (TRUE) { minishell->line = ft_prompt(minishell); diff --git a/src/prompt/prompt.c b/src/prompt/prompt.c index 6c5c68b..0aa07bf 100644 --- a/src/prompt/prompt.c +++ b/src/prompt/prompt.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* prompt.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/02/04 16:13:08 by whaffman #+# #+# */ -/* Updated: 2025/02/06 16:12:02 by qmennen ### ########.fr */ +/* :::::::: */ +/* prompt.c :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/02/04 16:13:08 by whaffman #+# #+# */ +/* Updated: 2025/02/08 14:34:53 by willem ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ char *get_user(t_list *environment) char *str1; char *str2; - str1 = ft_strdup(get_environment(environment, "USER")); + str1 = ft_strdup(environment_get(environment, "USER")->value); if (str1 == NULL) str1 = ft_strdup("guest"); if (str1 == NULL) @@ -36,7 +36,7 @@ char *get_path(t_list *environment) char *cwd; cwd = getcwd(NULL, 0); - home = get_environment(environment, "HOME"); + home = environment_get(environment, "HOME")->value; if (cwd == NULL) { perror("getcwd"); diff --git a/src/utils/free_minishell.c b/src/utils/free_minishell.c index 3a0df96..eca8d94 100644 --- a/src/utils/free_minishell.c +++ b/src/utils/free_minishell.c @@ -17,6 +17,6 @@ void free_minishell(t_minishell *minishell) if (minishell->line) free_minishell_line(minishell); if (minishell->environment) - free_environment(minishell->environment); + environment_free(minishell->environment); free(minishell); } diff --git a/src/utils/simple_builtins.c b/src/utils/simple_builtins.c index 7817cfc..b979008 100644 --- a/src/utils/simple_builtins.c +++ b/src/utils/simple_builtins.c @@ -6,12 +6,42 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/05 16:21:39 by whaffman #+# #+# */ -/* Updated: 2025/02/05 16:22:47 by whaffman ######## odam.nl */ +/* Updated: 2025/02/08 14:41:19 by willem ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" +void builtin_export(t_minishell *minishell) +{ + t_list *tmp; + t_environment *env; + char **arr; + + tmp = minishell->tokens->next; + while (tmp != NULL) + { + arr = ft_split(((t_token *)tmp->content)->value, '='); + if (arr[1] == NULL) + { + ft_free_arr(arr); + tmp = tmp->next; + continue ; + } + env = environment_get(minishell->environment, arr[0]); + if (env != NULL) + { + free(env->value); + env->value = ft_strdup(arr[1]); + } + else + environment_add(&(minishell->environment), arr[0], arr[1]); + ft_free_arr(arr); + tmp = tmp->next; + } +} + + static int cmp_value(t_list *list, char *str) { if (list != NULL @@ -28,10 +58,14 @@ void simple_builtins(t_minishell *minishell) if (cmp_value(minishell->tokens, "clear")) printf("\033[2J\033[1;1H"); else if (cmp_value(minishell->tokens, "env")) - print_environment(minishell->environment); + environment_print(minishell->environment); else if (cmp_value(minishell->tokens, "exit")) { free_minishell(minishell); exit(EXIT_SUCCESS); } + else if (cmp_value(minishell->tokens, "export")) + { + builtin_export(minishell); + } }