Environment
environment: rename functions environment: add get_arr function builtins: add export builtin
This commit is contained in:
parent
ef1a3f8a39
commit
869eb0d857
@ -8,7 +8,9 @@ A lot of amazing shell stuff
|
|||||||
- Find absolute path for command input ('/', './', 'cmd')
|
- Find absolute path for command input ('/', './', 'cmd')
|
||||||
- Add heredoc to tokenizer
|
- Add heredoc to tokenizer
|
||||||
-[x] Environment to `t_list`
|
-[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
|
- Preliminary signals
|
||||||
- Define struct for commands, something like (
|
- Define struct for commands, something like (
|
||||||
```c
|
```c
|
||||||
|
|||||||
@ -13,10 +13,13 @@
|
|||||||
#ifndef environment_H
|
#ifndef environment_H
|
||||||
# define environment_H
|
# define environment_H
|
||||||
|
|
||||||
void add_environment(t_list **environment, char *name, char *value);
|
# include "minishell.h"
|
||||||
void print_environment(t_list *environment);
|
|
||||||
char *get_environment(t_list *environment, char *name);
|
void environment_add(t_list **environment, char *name, char *value);
|
||||||
void free_environment(t_list *environment);
|
void environment_print(t_list *environment);
|
||||||
int parse_environment(char **envp, 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
|
#endif // environment_H
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* :::::::: */
|
/* :::::::: */
|
||||||
/* add_environment.c :+: :+: */
|
/* environment_add.c :+: :+: */
|
||||||
/* +:+ */
|
/* +:+ */
|
||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
#include "minishell.h"
|
#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_environment *new_environment;
|
||||||
t_list *new_node;
|
t_list *new_node;
|
||||||
@ -1,7 +1,7 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* :::::::: */
|
/* :::::::: */
|
||||||
/* free_environment.c :+: :+: */
|
/* environment_free.c :+: :+: */
|
||||||
/* +:+ */
|
/* +:+ */
|
||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
void free_environment(t_list *environment)
|
void environment_free(t_list *environment)
|
||||||
{
|
{
|
||||||
t_list *tmp;
|
t_list *tmp;
|
||||||
|
|
||||||
@ -1,7 +1,7 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* :::::::: */
|
/* :::::::: */
|
||||||
/* get_environment.c :+: :+: */
|
/* environment_get.c :+: :+: */
|
||||||
/* +:+ */
|
/* +:+ */
|
||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
@ -12,14 +12,14 @@
|
|||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
char *get_environment(t_list *environment, char *name)
|
t_environment *environment_get(t_list *environment, char *name)
|
||||||
{
|
{
|
||||||
t_environment *env;
|
t_environment *env;
|
||||||
while (environment != NULL)
|
while (environment != NULL)
|
||||||
{
|
{
|
||||||
env = (t_environment *)environment->content;
|
env = (t_environment *)environment->content;
|
||||||
if (ft_strcmp(env->name, name) == 0)
|
if (ft_strcmp(env->name, name) == 0)
|
||||||
return (env->value);
|
return (env);
|
||||||
environment = environment->next;
|
environment = environment->next;
|
||||||
}
|
}
|
||||||
return (NULL);
|
return (NULL);
|
||||||
42
src/environment/environment_get_arr.c
Normal file
42
src/environment/environment_get_arr.c
Normal 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);
|
||||||
|
}
|
||||||
@ -1,7 +1,7 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* :::::::: */
|
/* :::::::: */
|
||||||
/* parse_environment.c :+: :+: */
|
/* environment_parse.c :+: :+: */
|
||||||
/* +:+ */
|
/* +:+ */
|
||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
int parse_environment(char **envp, t_list **environment)
|
int environment_parse(char **envp, t_list **environment)
|
||||||
{
|
{
|
||||||
char **env;
|
char **env;
|
||||||
|
|
||||||
@ -22,7 +22,7 @@ int parse_environment(char **envp, t_list **environment)
|
|||||||
while (*envp != NULL)
|
while (*envp != NULL)
|
||||||
{
|
{
|
||||||
env = ft_split(*envp, '=');
|
env = ft_split(*envp, '=');
|
||||||
add_environment(environment, env[0], env[1]);
|
environment_add(environment, env[0], env[1]);
|
||||||
ft_free_arr(env);
|
ft_free_arr(env);
|
||||||
envp++;
|
envp++;
|
||||||
}
|
}
|
||||||
@ -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 #+# #+# */
|
/* Created: 2025/02/08 13:52:08 by willem #+# #+# */
|
||||||
/* Updated: 2025/02/05 15:52:44 by whaffman ######## odam.nl */
|
/* Updated: 2025/02/08 14:24:20 by willem ######## odam.nl */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#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;
|
perror("malloc");
|
||||||
printf("%s=%s\n", env->name, env->value);
|
return ;
|
||||||
environment = environment->next;
|
|
||||||
}
|
}
|
||||||
|
i = 0;
|
||||||
|
while (arr[i] != NULL)
|
||||||
|
{
|
||||||
|
printf("%s\n", arr[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
ft_free_arr(arr);
|
||||||
}
|
}
|
||||||
@ -6,12 +6,13 @@
|
|||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/04 16:19:22 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"
|
#include "minishell.h"
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char **argv, char **envp)
|
int main(int argc, char **argv, char **envp)
|
||||||
{
|
{
|
||||||
t_minishell *minishell;
|
t_minishell *minishell;
|
||||||
@ -21,7 +22,7 @@ int main(int argc, char **argv, char **envp)
|
|||||||
print_banner();
|
print_banner();
|
||||||
ft_load_history();
|
ft_load_history();
|
||||||
minishell = init_minishell();
|
minishell = init_minishell();
|
||||||
parse_environment(envp, &(minishell->environment));
|
environment_parse(envp, &(minishell->environment));
|
||||||
while (TRUE)
|
while (TRUE)
|
||||||
{
|
{
|
||||||
minishell->line = ft_prompt(minishell);
|
minishell->line = ft_prompt(minishell);
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* :::::::: */
|
||||||
/* prompt.c :+: :+: :+: */
|
/* prompt.c :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ */
|
||||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/04 16:13:08 by whaffman #+# #+# */
|
/* 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 *str1;
|
||||||
char *str2;
|
char *str2;
|
||||||
|
|
||||||
str1 = ft_strdup(get_environment(environment, "USER"));
|
str1 = ft_strdup(environment_get(environment, "USER")->value);
|
||||||
if (str1 == NULL)
|
if (str1 == NULL)
|
||||||
str1 = ft_strdup("guest");
|
str1 = ft_strdup("guest");
|
||||||
if (str1 == NULL)
|
if (str1 == NULL)
|
||||||
@ -36,7 +36,7 @@ char *get_path(t_list *environment)
|
|||||||
char *cwd;
|
char *cwd;
|
||||||
|
|
||||||
cwd = getcwd(NULL, 0);
|
cwd = getcwd(NULL, 0);
|
||||||
home = get_environment(environment, "HOME");
|
home = environment_get(environment, "HOME")->value;
|
||||||
if (cwd == NULL)
|
if (cwd == NULL)
|
||||||
{
|
{
|
||||||
perror("getcwd");
|
perror("getcwd");
|
||||||
|
|||||||
@ -17,6 +17,6 @@ void free_minishell(t_minishell *minishell)
|
|||||||
if (minishell->line)
|
if (minishell->line)
|
||||||
free_minishell_line(minishell);
|
free_minishell_line(minishell);
|
||||||
if (minishell->environment)
|
if (minishell->environment)
|
||||||
free_environment(minishell->environment);
|
environment_free(minishell->environment);
|
||||||
free(minishell);
|
free(minishell);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,12 +6,42 @@
|
|||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/05 16:21:39 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"
|
#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)
|
static int cmp_value(t_list *list, char *str)
|
||||||
{
|
{
|
||||||
if (list != NULL
|
if (list != NULL
|
||||||
@ -28,10 +58,14 @@ void simple_builtins(t_minishell *minishell)
|
|||||||
if (cmp_value(minishell->tokens, "clear"))
|
if (cmp_value(minishell->tokens, "clear"))
|
||||||
printf("\033[2J\033[1;1H");
|
printf("\033[2J\033[1;1H");
|
||||||
else if (cmp_value(minishell->tokens, "env"))
|
else if (cmp_value(minishell->tokens, "env"))
|
||||||
print_environment(minishell->environment);
|
environment_print(minishell->environment);
|
||||||
else if (cmp_value(minishell->tokens, "exit"))
|
else if (cmp_value(minishell->tokens, "exit"))
|
||||||
{
|
{
|
||||||
free_minishell(minishell);
|
free_minishell(minishell);
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
else if (cmp_value(minishell->tokens, "export"))
|
||||||
|
{
|
||||||
|
builtin_export(minishell);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user