aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-06-09 17:57:57 +0200
committerCharles <sircharlesaze@gmail.com>2020-06-09 17:57:57 +0200
commit4e81fb861a79c9373b070176649bc0f4f8dfa850 (patch)
tree7586abc6d769edbba10d10bf4cc419ca85471862 /src
parent5ade92701836ce5ee1d39fc8d486b7709547058e (diff)
downloadminishell-4e81fb861a79c9373b070176649bc0f4f8dfa850.tar.gz
minishell-4e81fb861a79c9373b070176649bc0f4f8dfa850.tar.bz2
minishell-4e81fb861a79c9373b070176649bc0f4f8dfa850.zip
Fixing 2 leaks in preprocessor and norming it
Diffstat (limited to 'src')
-rw-r--r--src/lexer/token.c10
-rw-r--r--src/main.c10
-rw-r--r--src/ms_glob.c11
-rw-r--r--src/preprocess.c27
4 files changed, 37 insertions, 21 deletions
diff --git a/src/lexer/token.c b/src/lexer/token.c
index bf534a4..6c6a184 100644
--- a/src/lexer/token.c
+++ b/src/lexer/token.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/09 13:38:08 by charles #+# #+# */
-/* Updated: 2020/06/09 13:39:27 by charles ### ########.fr */
+/* Updated: 2020/06/09 17:55:23 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,7 +17,7 @@ t_token *token_new(enum e_token_tag tag, char *content)
t_token *token;
if (content == NULL
- || (token = malloc(sizeof(t_token))) == NULL)
+ || (token = (t_token*)malloc(sizeof(t_token))) == NULL)
return (NULL);
if ((token->content = ft_strdup(content)) == NULL)
{
@@ -27,3 +27,9 @@ t_token *token_new(enum e_token_tag tag, char *content)
token->tag = tag;
return token;
}
+
+void token_destroy(t_token *token)
+{
+ free(token->content);
+ free(token);
+}
diff --git a/src/main.c b/src/main.c
index 802d3d2..76c863d 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */
-/* Updated: 2020/06/09 16:53:45 by charles ### ########.fr */
+/* Updated: 2020/06/09 17:48:03 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -95,11 +95,11 @@ int main(int argc, char **argv, char **envp)
/* free(j); */
t_ftvec *v = ft_vecnew(32);
- /* ft_vecpush(v, token_new(LTAG_STR, "$TERM$LFS$TERM$TERM.")); */
- /* ft_vecpush(v, token_new(LTAG_STR, "$$LFS$TERM$TERM.")); */
+ ft_vecpush(v, token_new(LTAG_STR, "$TERM$LFS$TERM$TERM."));
+ ft_vecpush(v, token_new(LTAG_STR, "$$LFS$TERM$TERM."));
ft_vecpush(v, token_new(LTAG_STR, "*/*.c$TERM"));
- /* ft_vecpush(v, token_new(LTAG_STR, "src#<{(|.c include#<{(|.h")); */
- /* ft_vecpush(v, token_new(LTAG_STR, "$A$B")); */
+ ft_vecpush(v, token_new(LTAG_STR, "src/*.c include/*.h"));
+ ft_vecpush(v, token_new(LTAG_STR, "$A$B"));
char **as = preprocess_argv(v, env);
char **tmp = as;
diff --git a/src/ms_glob.c b/src/ms_glob.c
index 2ab62a3..a96c45f 100644
--- a/src/ms_glob.c
+++ b/src/ms_glob.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/05 11:44:07 by charles #+# #+# */
-/* Updated: 2020/06/09 16:20:22 by charles ### ########.fr */
+/* Updated: 2020/06/09 17:51:30 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -160,3 +160,12 @@ char *ms_glob(char *pattern)
}
return (join);
}
+
+char *ms_globf(char *pattern)
+{
+ char *ret;
+
+ ret = ms_glob(pattern);
+ free(pattern);
+ return (ret);
+}
diff --git a/src/preprocess.c b/src/preprocess.c
index 0d72a2c..c09ba71 100644
--- a/src/preprocess.c
+++ b/src/preprocess.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/03 08:58:49 by charles #+# #+# */
-/* Updated: 2020/06/09 17:03:37 by charles ### ########.fr */
+/* Updated: 2020/06/09 17:56:20 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,18 +14,16 @@
#include "ms_glob.h"
#include "lexer.h"
-static char *iterpolate(char *str, t_env env)
+static char *iterpolate(char *str, t_env env)
{
size_t i;
t_ftdstr *dstr;
char *match;
- if ((dstr = ft_dstrnew(str)) == NULL)
+ if ((dstr = ft_dstrwrap(str)) == NULL)
return (NULL);
- free(str);
- i = 0;
- while (i < dstr->length)
- {
+ i = -1;
+ while (++i < dstr->length)
if (dstr->str[i] == '$')
{
if ((match = env_search_first_match(env, dstr->str + i + 1)) == NULL)
@@ -43,8 +41,6 @@ static char *iterpolate(char *str, t_env env)
i += ft_strlen(match) - 1;
}
}
- i++;
- }
return (ft_dstrunwrap(dstr));
}
@@ -53,13 +49,13 @@ static char *iterpolate_globs(char *str)
char **strs;
int i;
- if ((strs = ft_split(str, ' ')) == NULL)
+ if ((strs = ft_splitf(str, ' ')) == NULL)
return (NULL);
i = 0;
while (strs[i] != NULL)
{
if (ft_strchr(strs[i], '*') != NULL
- && (strs[i] = ms_glob(strs[i])) == NULL)
+ && (strs[i] = ms_globf(strs[i])) == NULL)
{
ft_split_destroy(strs);
return (NULL);
@@ -69,7 +65,7 @@ static char *iterpolate_globs(char *str)
return (ft_strsjoinf(strs, " "));
}
-static int splat_arg(t_ftvec *argv, int i)
+static int splat_arg(t_ftvec *argv, int i)
{
t_token *splated;
char **strs;
@@ -77,19 +73,24 @@ static int splat_arg(t_ftvec *argv, int i)
if ((splated = ft_vectake(argv, i)) == NULL
|| (strs = ft_split(splated->content, ' ')) == NULL)
+ {
+ token_destroy(splated);
return (-1);
+ }
j = 0;
while (strs[j] != NULL)
{
if (ft_vecinsert_safe(argv, i + j, token_new(LTAG_STR, strs[j])) == NULL)
{
+ token_destroy(splated);
ft_split_destroy(strs);
return (-1);
}
j++;
}
+ token_destroy(splated);
ft_split_destroy(strs);
- return i + j - 1;
+ return (i + j - 1);
}
void iter_func_unwrap_token(void **addr)