From c0c5582d5bbadca30b2fbae8d12ceb5ae18ffd4d Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 28 Apr 2020 18:42:06 +0200 Subject: The variable list works --- Makefile | 1 + libft/Makefile | 6 +-- src/ft_s_destroy.c | 3 ++ src/ft_s_lvars.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ft_s_lvars.h | 29 ++++++++++++ src/ft_s_struct.h | 8 ++-- src/minishell.c | 2 + 7 files changed, 166 insertions(+), 9 deletions(-) create mode 100644 src/ft_s_lvars.c create mode 100644 src/ft_s_lvars.h diff --git a/Makefile b/Makefile index 4fe2140..77e748f 100644 --- a/Makefile +++ b/Makefile @@ -40,6 +40,7 @@ SRCS_NAME += ft_m_redirs.c SRCS_NAME += ft_s_destroy.c SRCS_NAME += ft_s_init.c SRCS_NAME += ft_s_lcom.c +SRCS_NAME += ft_s_lvars.c SRCS_NAME += ft_p_line.c SRCS_NAME += ft_p_lcom.c SRCS_NAME += ft_p_lcom_next.c diff --git a/libft/Makefile b/libft/Makefile index 858e90b..6803f66 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -120,11 +120,7 @@ OS = $(shell uname) #==============================================================================# #-------------------------------- Compiler ------------------------------------# #==============================================================================# -ifeq (${OS}, Linux) - CC = gcc -else - CC = clang -endif +CC = clang #------------------------------------------------------------------------------# CFLAGS = -std=c89 CFLAGS += -Wall 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 + #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 +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#include + +#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 +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 */ -- cgit v1.2.3 From d59bd533a9043abd0ba2a78686f823ed6ffe4b5b Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 30 Apr 2020 16:23:45 +0200 Subject: BSD magick test --- src/minishell.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/minishell.c b/src/minishell.c index 3b8e043..7ddaa42 100644 --- a/src/minishell.c +++ b/src/minishell.c @@ -28,8 +28,13 @@ int { t_msh *msh; int32_t ret; - int32_t i; + char *str; + str = ft_strdup("qwe\'"); + ft_printf("%s\n", str); + str = ft_strsubst(str, "\'", ""); + ft_printf("%s\n", str); + return (0); /* TODO: increment $SHLVL */ /* TODO: also set $SHELL */ /* TODO: handle general variables | $var */ -- cgit v1.2.3