diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ft_s_destroy.c | 3 | ||||
-rw-r--r-- | src/ft_s_lvars.c | 126 | ||||
-rw-r--r-- | src/ft_s_lvars.h | 29 | ||||
-rw-r--r-- | src/ft_s_struct.h | 8 | ||||
-rw-r--r-- | src/minishell.c | 2 |
5 files changed, 164 insertions, 4 deletions
diff --git a/src/ft_s_destroy.c b/src/ft_s_destroy.c index f339e8d..08980b5 100644 --- a/src/ft_s_destroy.c +++ b/src/ft_s_destroy.c @@ -11,7 +11,9 @@ /* ************************************************************************** */ #include <libft.h> + #include "ft_s_destroy.h" +#include "ft_s_lvars.h" void ft_s_destroy(t_msh *msh) @@ -20,5 +22,6 @@ void ft_memdel((void*)&msh->cwd); ft_memdel((void*)&msh->shname); ft_delwords(msh->bu_ref); + ft_lvars_clear(&msh->vars); ft_memdel((void*)&msh); } diff --git a/src/ft_s_lvars.c b/src/ft_s_lvars.c new file mode 100644 index 0000000..fb3a0f4 --- /dev/null +++ b/src/ft_s_lvars.c @@ -0,0 +1,126 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_s_lvars.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <string.h> +#include <stdlib.h> +#include <errno.h> +#include <unistd.h> + +#include "ft_s_struct.h" + +void + ft_lvars_rebind(t_lvars **lvars, + const char name[], + const char newval[]) +{ + t_lvars *tmp; + + tmp = *lvars; + while (tmp && ft_strncmp(tmp->name, name, ft_strlen(name))) + { + tmp = tmp->next; + } + if (tmp == NULL) + { + return ; + } + ft_memdel((void*)&tmp->val); + if (!(tmp->val = ft_strdup(newval))) + { + ft_dprintf(STDERR_FILENO, "%s\n", strerror(errno)); + exit(FT_RET_ALLOC); + } +} + +void + ft_lvars_delone(t_lvars **lvars, + const char name[]) +{ + t_lvars *tmp; + t_lvars *prev; + + tmp = *lvars; + if (tmp != NULL && !ft_strncmp(tmp->name, name, ft_strlen(name))) + { + *lvars = tmp->next; + ft_memdel((void*)&tmp->name); + ft_memdel((void*)&tmp->val); + ft_memdel((void*)&tmp); + return ; + } + while (tmp && ft_strncmp(tmp->name, name, ft_strlen(name))) + { + prev = tmp; + tmp = tmp->next; + } + if (tmp == NULL) + return ; + prev->next = tmp->next; + ft_memdel((void*)&tmp->name); + ft_memdel((void*)&tmp->val); + ft_memdel((void*)&tmp); +} + +void + ft_lvars_add_front(t_lvars **alvars, + t_lvars *new) +{ + if (!alvars || !new) + { + return ; + } + new->next = *alvars; + *alvars = new; +} + +void + ft_lvars_clear(t_lvars **lvars) +{ + t_lvars *tmp; + t_lvars *renext; + + if (!lvars) + return ; + tmp = *lvars; + while (tmp) + { + renext = tmp->next; + ft_memdel((void*)&tmp->name); + ft_memdel((void*)&tmp->val); + ft_memdel((void*)&tmp); + tmp = renext; + } + *lvars = NULL; +} + +t_lvars + *ft_lvars_new(const char name[], + const char val[]) +{ + t_lvars *link; + + if (!(link = (t_lvars*)malloc(sizeof(t_lvars)))) + { + return (NULL); + } + if (!(link->name = ft_strdup(name))) + { + return (NULL); + } + if (!(link->val = ft_strdup(val))) + { + return (NULL); + } + link->next = NULL; + return (link); +} diff --git a/src/ft_s_lvars.h b/src/ft_s_lvars.h new file mode 100644 index 0000000..0f46d7d --- /dev/null +++ b/src/ft_s_lvars.h @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_s_lvars.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_S_LVARS_H +#define FT_S_LVARS_H + +#include "ft_s_struct.h" + +void ft_lvars_rebind(t_lvars **lvars, + const char name[], + const char newval[]); +void ft_lvars_delone(t_lvars **lvars, + const char name[]); +void ft_lvars_add_front(t_lvars **alvars, + t_lvars *new); +void ft_lvars_clear(t_lvars **lvars); +t_lvars *ft_lvars_new(const char name[], + const char val[]); + +#endif diff --git a/src/ft_s_struct.h b/src/ft_s_struct.h index 5b99acb..b6804bf 100644 --- a/src/ft_s_struct.h +++ b/src/ft_s_struct.h @@ -26,12 +26,12 @@ ** 0: means no redirection */ -typedef struct s_vars +typedef struct s_lvars { char *name; char *val; - struct s_vars *next; -} t_vars; + struct s_lvars *next; +} t_lvars; typedef struct s_lcom { @@ -53,7 +53,7 @@ typedef struct s_msh char **bu_ref; uint8_t (*bu_ptr[FT_BUILTINS_COUNT])(char **, struct s_msh*); struct s_lcom *curr; - struct s_vars *vars; + struct s_lvars *vars; } t_msh; #endif diff --git a/src/minishell.c b/src/minishell.c index 96b51c8..3b8e043 100644 --- a/src/minishell.c +++ b/src/minishell.c @@ -28,6 +28,7 @@ int { t_msh *msh; int32_t ret; + int32_t i; /* TODO: increment $SHLVL */ /* TODO: also set $SHELL */ @@ -55,4 +56,5 @@ int ** p_ -> parse related ** s_ -> structs related ** u_ -> utils related +** v_ -> general purpose variables related */ |