summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-04-21 16:09:25 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-04-21 16:09:25 +0200
commit23c53fec41ef1c9b62757c73b8ff8362139f8838 (patch)
treee2604b61531d9e6864c3b6038d8ea6848fc02903 /src
parentGood (diff)
download42-minishell-23c53fec41ef1c9b62757c73b8ff8362139f8838.tar.gz
42-minishell-23c53fec41ef1c9b62757c73b8ff8362139f8838.tar.bz2
42-minishell-23c53fec41ef1c9b62757c73b8ff8362139f8838.tar.xz
42-minishell-23c53fec41ef1c9b62757c73b8ff8362139f8838.tar.zst
42-minishell-23c53fec41ef1c9b62757c73b8ff8362139f8838.zip
Linked list works, not memdeling
Diffstat (limited to '')
-rw-r--r--src/ft_b_echo.c2
-rw-r--r--src/ft_b_env.c2
-rw-r--r--src/ft_e_lcom.c12
-rw-r--r--src/ft_m_loop.c2
-rw-r--r--src/ft_p_lcom.c15
-rw-r--r--src/ft_p_line.c2
-rw-r--r--src/ft_s_init.c2
-rw-r--r--src/ft_s_lcom.c93
-rw-r--r--src/ft_s_lcom.h4
-rw-r--r--src/ft_s_struct.h3
10 files changed, 109 insertions, 28 deletions
diff --git a/src/ft_b_echo.c b/src/ft_b_echo.c
index f7b83a2..d5e4a64 100644
--- a/src/ft_b_echo.c
+++ b/src/ft_b_echo.c
@@ -16,7 +16,7 @@
int32_t
ft_b_echo(char *args[],
- t_msh *msh)
+ t_msh *msh)
{
(void)msh;
while (*args)
diff --git a/src/ft_b_env.c b/src/ft_b_env.c
index 4f26788..eb3b107 100644
--- a/src/ft_b_env.c
+++ b/src/ft_b_env.c
@@ -17,7 +17,7 @@
int32_t
ft_b_env(char *args[],
- t_msh *msh)
+ t_msh *msh)
{
char **ptr;
diff --git a/src/ft_e_lcom.c b/src/ft_e_lcom.c
index c592f3c..b928004 100644
--- a/src/ft_e_lcom.c
+++ b/src/ft_e_lcom.c
@@ -17,7 +17,7 @@
static uint8_t
ft_get_builtin_id(const char com[],
- t_msh *msh)
+ t_msh *msh)
{
uint8_t i;
@@ -32,20 +32,22 @@ static uint8_t
int32_t
ft_e_lcom(t_msh *msh)
{
+ t_lcom *ptr;
uint8_t bu_id;
- while (msh->lcom_head != NULL)
+ ptr = msh->curr;
+ while (ptr != NULL)
{
- if ((bu_id = ft_get_builtin_id(msh->lcom_head->com, msh))
+ if ((bu_id = ft_get_builtin_id(ptr->com, msh))
< FT_BUILTINS_COUNT)
{
- msh->bu_ptr[bu_id](msh->lcom_head->args, msh);
+ msh->bu_ptr[bu_id](ptr->args, msh);
}
else
{
/* TODO: exec path stuff */
}
- msh->lcom_head = msh->lcom_head->next;
+ ptr = ptr->next;
}
return (0);
}
diff --git a/src/ft_m_loop.c b/src/ft_m_loop.c
index 4c22009..3a44d05 100644
--- a/src/ft_m_loop.c
+++ b/src/ft_m_loop.c
@@ -18,6 +18,7 @@
#include "ft_m_loop.h"
#include "ft_m_prompt.h"
#include "ft_p_line.h"
+#include "ft_s_lcom.h"
int32_t
ft_m_loop(t_msh *msh)
@@ -35,6 +36,7 @@ int32_t
ft_p_line(line, msh);
ft_memdel((void*)&line);
ft_e_lcom(msh);
+ ft_lcom_clear(&msh->curr);
/* TODO: memdel lcom - INSTALL VALGRIND */
}
else
diff --git a/src/ft_p_lcom.c b/src/ft_p_lcom.c
index 6711b85..f928f5d 100644
--- a/src/ft_p_lcom.c
+++ b/src/ft_p_lcom.c
@@ -17,26 +17,23 @@
int8_t
ft_p_lcom(const char line[],
- const uint64_t count,
- t_msh *msh)
+ const uint64_t count,
+ t_msh *msh)
{
uint64_t i;
- char **words;
+ t_lcom *link;
+ char **words;
i = 0;
if (!(words = ft_split(line, ';')))
return (-1);
while (i <= count)
{
- if (!(msh->lcom = ft_lcom_new(words[i])))
+ if (!(link = ft_lcom_new(words[i])))
{
return (-1);
}
- if (i < 1)
- {
- msh->lcom_head = msh->lcom;
- }
- msh->lcom = msh->lcom->next;
+ ft_lcom_add_back(&msh->curr, link);
i++;
}
return (0);
diff --git a/src/ft_p_line.c b/src/ft_p_line.c
index 606408b..f299f64 100644
--- a/src/ft_p_line.c
+++ b/src/ft_p_line.c
@@ -19,7 +19,7 @@
void
ft_p_line(char line[],
- t_msh *msh)
+ t_msh *msh)
{
uint64_t i;
uint64_t count;
diff --git a/src/ft_s_init.c b/src/ft_s_init.c
index 27075cf..63856c2 100644
--- a/src/ft_s_init.c
+++ b/src/ft_s_init.c
@@ -33,6 +33,6 @@ t_msh
msh->envp = envp;
msh->ret = 0;
ft_init_buptr(msh);
- msh->lcom = NULL;
+ msh->curr = NULL;
return (msh);
}
diff --git a/src/ft_s_lcom.c b/src/ft_s_lcom.c
index 737c6eb..8b61f69 100644
--- a/src/ft_s_lcom.c
+++ b/src/ft_s_lcom.c
@@ -12,24 +12,101 @@
#include <libft.h>
#include <stdlib.h>
+#include <stdint.h>
#include "ft_s_struct.h"
+static int8_t
+ ft_fill_lcom(char *words[],
+ t_lcom **lcom)
+{
+ int64_t i;
+ int64_t j;
+
+ i = 0;
+ if (!((*lcom)->com = (char*)malloc((ft_strlen(words[0]) + 1) *
+ sizeof(char))))
+ return (-1);
+ ft_strlcpy((*lcom)->com, words[0], ft_strlen(words[0]) + 1);
+ while(words[i + 1])
+ i++;
+ if (i > 0)
+ {
+ if (!((*lcom)->args = (char**)malloc((i + 1) * sizeof(char*))))
+ return (-1);
+ j = -1;
+ while (++j < i)
+ {
+ if (!((*lcom)->args[j] =
+ (char*)malloc((ft_strlen(words[j + 1]) + 1) * sizeof(char))))
+ return (-1);
+ ft_strlcpy((*lcom)->args[j], words[j + 1],
+ ft_strlen(words[j + 1]) + 1);
+ }
+ (*lcom)->args[j] = 0;
+ }
+ return (0);
+}
+
+t_lcom
+ *ft_lcom_last(t_lcom *lcom)
+{
+ while (lcom->next != NULL)
+ lcom = lcom->next;
+ return (lcom);
+}
+
+void
+ ft_lcom_add_back(t_lcom **alcom,
+ t_lcom *new)
+{
+ t_lcom *tmp;
+
+ if (!*alcom)
+ *alcom = new;
+ else
+ {
+ tmp = ft_lcom_last(*alcom);
+ tmp->next = new;
+ }
+}
+
+void
+ ft_lcom_clear(t_lcom **lcom)
+{
+ t_lcom *tmp;
+ t_lcom *renext;
+
+ if (!lcom)
+ return ;
+ tmp = *lcom;
+ while (tmp)
+ {
+ renext = tmp->next;
+ ft_memdel((void*)&tmp->com);
+ if (tmp->args)
+ ft_delwords(tmp->args);
+ ft_memdel((void*)&tmp);
+ tmp = renext;
+ }
+ *lcom = NULL;
+}
+
t_lcom
*ft_lcom_new(const char word[])
{
- t_lcom *nlcom;
+ t_lcom *link;
char **words;
- if (!(nlcom = (t_lcom*)malloc(sizeof(t_lcom))))
- {
+ if (!(link = (t_lcom*)malloc(sizeof(t_lcom))))
return (NULL);
- }
if (!(words = ft_split(word, ' ')))
+ return (NULL);
+ if (ft_fill_lcom(words, &link) < 0)
{
+ ft_delwords(words);
return (NULL);
}
- nlcom->com = words[0];
- nlcom->args = words + 1;
- nlcom->next = NULL;
- return (nlcom);
+ link->next = NULL;
+ ft_delwords(words);
+ return (link);
}
diff --git a/src/ft_s_lcom.h b/src/ft_s_lcom.h
index 14a0c39..eae0501 100644
--- a/src/ft_s_lcom.h
+++ b/src/ft_s_lcom.h
@@ -15,6 +15,10 @@
#include "ft_s_struct.h"
+void ft_lcom_add_back(t_lcom **alcom,
+ t_lcom *new);
+void ft_lcom_clear(t_lcom **lcom);
t_lcom *ft_lcom_new(const char word[]);
+t_lcom *ft_lcom_last(t_lcom *lcom);
#endif
diff --git a/src/ft_s_struct.h b/src/ft_s_struct.h
index 34a3182..9719a6f 100644
--- a/src/ft_s_struct.h
+++ b/src/ft_s_struct.h
@@ -30,8 +30,7 @@ typedef struct s_msh
int32_t ret;
char **bu_ref;
int32_t (*bu_ptr[FT_BUILTINS_COUNT])(char **, struct s_msh*);
- struct s_lcom *lcom_head;
- struct s_lcom *lcom;
+ struct s_lcom *curr;
} t_msh;
#endif