Environment

environment: rename functions
environment: add get_arr function
builtins: add export builtin
This commit is contained in:
whaffman 2025-02-08 14:43:53 +01:00
parent ef1a3f8a39
commit 869eb0d857
12 changed files with 130 additions and 40 deletions

View File

@ -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

View File

@ -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

View File

@ -1,7 +1,7 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* add_environment.c :+: :+: */
/* environment_add.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
@ -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;

View File

@ -1,7 +1,7 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* free_environment.c :+: :+: */
/* environment_free.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
@ -12,7 +12,7 @@
#include "minishell.h"
void free_environment(t_list *environment)
void environment_free(t_list *environment)
{
t_list *tmp;

View File

@ -1,7 +1,7 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* get_environment.c :+: :+: */
/* environment_get.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
@ -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);

View File

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* environment_get_arr.c :+: :+: */
/* +:+ */
/* By: willem <willem@student.codam.nl> +#+ */
/* +#+ */
/* 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);
}

View File

@ -1,7 +1,7 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* parse_environment.c :+: :+: */
/* environment_parse.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
@ -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++;
}

View File

@ -1,25 +1,33 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* print_environment.c :+: :+: */
/* environment_print.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* By: willem <willem@student.codam.nl> +#+ */
/* +#+ */
/* 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);
}

View File

@ -6,12 +6,13 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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);

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* prompt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* prompt.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/04 16:13:08 by whaffman #+# #+# */
/* Updated: 2025/02/06 16:12:02 by qmennen ### ########.fr */
/* 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");

View File

@ -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);
}

View File

@ -6,12 +6,42 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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);
}
}