summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-04-26 17:18:16 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-04-26 17:18:16 +0200
commit75492244a377c8d5d61459a162d73d60dd32759e (patch)
treec1d86b502143a97a7fde36db303bdab4a62ae2dd /src
parentTodo update (diff)
download42-minishell-75492244a377c8d5d61459a162d73d60dd32759e.tar.gz
42-minishell-75492244a377c8d5d61459a162d73d60dd32759e.tar.bz2
42-minishell-75492244a377c8d5d61459a162d73d60dd32759e.tar.xz
42-minishell-75492244a377c8d5d61459a162d73d60dd32759e.tar.zst
42-minishell-75492244a377c8d5d61459a162d73d60dd32759e.zip
Tryna subst vars
Diffstat (limited to 'src')
-rw-r--r--src/ft_d_define.h7
-rw-r--r--src/ft_p_lcom.c2
-rw-r--r--src/ft_p_lcom_next.c38
-rw-r--r--src/ft_p_lcom_next.h4
-rw-r--r--src/ft_p_line.c2
-rw-r--r--src/ft_s_lcom.c24
-rw-r--r--src/ft_s_lcom.h3
-rw-r--r--src/ft_u_utils.c61
-rw-r--r--src/ft_u_utils.h2
-rw-r--r--src/minishell.c1
10 files changed, 134 insertions, 10 deletions
diff --git a/src/ft_d_define.h b/src/ft_d_define.h
index 89e1106..b1e9a72 100644
--- a/src/ft_d_define.h
+++ b/src/ft_d_define.h
@@ -33,6 +33,13 @@
#define FT_OPT_COMMAND "-c"
/*
+** ====== VARS ======
+*/
+
+#define FT_ZER_VAR "$0"
+#define FT_RET_VAR "$?"
+
+/*
** ====== FAIL MSG ======
*/
diff --git a/src/ft_p_lcom.c b/src/ft_p_lcom.c
index a08e8c2..9a7a1b8 100644
--- a/src/ft_p_lcom.c
+++ b/src/ft_p_lcom.c
@@ -134,7 +134,7 @@ int8_t
return (-1);
while (i <= count)
{
- if (!(link = ft_lcom_new(words[i])))
+ if (!(link = ft_lcom_new(words[i], msh)))
{
return (-1);
}
diff --git a/src/ft_p_lcom_next.c b/src/ft_p_lcom_next.c
index df312bc..d1ab206 100644
--- a/src/ft_p_lcom_next.c
+++ b/src/ft_p_lcom_next.c
@@ -14,6 +14,44 @@
#include <stdlib.h>
#include <stdint.h>
+#include "ft_s_struct.h"
+#include "ft_u_utils.h"
+
+char
+ **ft_subst_vars(char *words[],
+ t_msh *msh)
+{
+ /* TODO: norme */
+ char **p_words;
+ char **p_env;
+ char *s_varname;
+ int64_t i;
+ size_t varlen;
+
+ p_words = words;
+ varlen = 0;
+ while (*p_words)
+ {
+ while (varlen < ft_strlen(*p_words) &&
+ (i = ft_strlchr((*p_words) + varlen, '$')))
+ {
+ if (i == -1)
+ break ;
+ varlen = i + 1;
+ while ((*p_words)[varlen] != '\0' && (*p_words)[varlen] != '$')
+ varlen += 1;
+ if (!(s_varname = ft_substr(*p_words, (uint32_t)i, varlen)))
+ return (NULL);
+ ft_printf("{%s} | {%s}\n", s_varname, ft_subst_var_value(s_varname, msh));
+ p_env = msh->envp;
+ ft_memdel((void*)&s_varname);
+ varlen += 1;
+ }
+ p_words += 1;
+ }
+ return (words);
+}
+
char
**ft_subst_args(const char word[],
int8_t redir)
diff --git a/src/ft_p_lcom_next.h b/src/ft_p_lcom_next.h
index 6f0c2a8..54eefd2 100644
--- a/src/ft_p_lcom_next.h
+++ b/src/ft_p_lcom_next.h
@@ -15,6 +15,10 @@
#include <stdint.h>
+#include "ft_s_struct.h"
+
+char **ft_subst_vars(char *words[],
+ t_msh *msh);
char **ft_subst_args(const char word[],
int8_t redir);
diff --git a/src/ft_p_line.c b/src/ft_p_line.c
index 07c5fac..b1ba6b2 100644
--- a/src/ft_p_line.c
+++ b/src/ft_p_line.c
@@ -17,6 +17,7 @@
#include "ft_f_fail.h"
#include "ft_p_lcom.h"
#include "ft_p_line.h"
+#include "ft_s_destroy.h"
#include "ft_s_struct.h"
void
@@ -42,6 +43,7 @@ void
}
if (ft_p_lcom(line, count, msh) < 0)
{
+ ft_s_destroy(msh);
ft_fail_alloc(msh);
}
}
diff --git a/src/ft_s_lcom.c b/src/ft_s_lcom.c
index 03b2f67..ab63414 100644
--- a/src/ft_s_lcom.c
+++ b/src/ft_s_lcom.c
@@ -106,12 +106,27 @@ void
*lcom = NULL;
}
+/* !!!!!!!!!! ATTENTION !!!!!!!!!!! */
+/* TODO: remove this after tests */
+/* !!!!!!!!!! ATTENTION !!!!!!!!!!! */
+static void
+ft_print_words(char *words[])
+{
+ while (*words)
+ {
+ ft_printf("[%s]\n", *words);
+ words++;
+ }
+}
+
t_lcom
- *ft_lcom_new(const char word[])
+ *ft_lcom_new(const char word[],
+ t_msh *msh)
{
t_lcom *link;
char **words;
+ (void)msh;
if (!(link = (t_lcom*)malloc(sizeof(t_lcom))))
return (NULL);
link->redir = 0;
@@ -123,6 +138,13 @@ t_lcom
return (NULL);
if (!(words = ft_subst_args(word, link->redir)))
return (NULL);
+ if (!(words = ft_subst_vars(words, msh)))
+ return (NULL);
+ /* !!!!!!!!!! ATTENTION !!!!!!!!!!! */
+ /* TODO: remove this after tests */
+ ft_print_words(words);
+ /* TODO: remove this after tests */
+ /* !!!!!!!!!! ATTENTION !!!!!!!!!!! */
if (ft_fill_lcom(words, &link) < 0)
{
ft_delwords(words);
diff --git a/src/ft_s_lcom.h b/src/ft_s_lcom.h
index eae0501..6db58d3 100644
--- a/src/ft_s_lcom.h
+++ b/src/ft_s_lcom.h
@@ -18,7 +18,8 @@
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_new(const char word[],
+ t_msh *msh);
t_lcom *ft_lcom_last(t_lcom *lcom);
#endif
diff --git a/src/ft_u_utils.c b/src/ft_u_utils.c
index cd9e53f..594f2c8 100644
--- a/src/ft_u_utils.c
+++ b/src/ft_u_utils.c
@@ -14,8 +14,24 @@
#include <stdlib.h>
#include <stdint.h>
+#include "ft_f_fail.h"
+#include "ft_s_destroy.h"
+#include "ft_s_lcom.h"
#include "ft_s_struct.h"
+uint64_t
+ ft_get_argc(const char *args[])
+{
+ uint64_t argc;
+
+ argc = 0;
+ while (args[argc])
+ {
+ argc++;
+ }
+ return (argc);
+}
+
char
*ft_get_home_dir(char **envp)
{
@@ -35,15 +51,46 @@ char
return (NULL);
}
-uint64_t
- ft_get_argc(const char *args[])
+/*
+** char *
+** ft_subst_var_value(const char varname[], const t_msh *msh);
+**
+** DESCRIPTION
+** The ft_subst_var_value() function returns
+** a heap-allocated, null-terminated string
+** that may later be free'd containing the
+** value of the variable varname[] including
+** the '$' prefix. NULL is returned if varname[]
+** wasn't found.
+*/
+
+char
+ *ft_subst_var_value(const char varname[],
+ t_msh *msh)
{
- uint64_t argc;
+ char **p_env;
+ char *varval;
- argc = 0;
- while (args[argc])
+ p_env = msh->envp;
+ varval = NULL;
+ while (*p_env)
{
- argc++;
+ if (!ft_strncmp(*p_env, varname + 1, ft_strlen(varname + 1) + 1))
+ {
+ ft_printf("(%s)\n", *p_env);
+ while (**p_env != '\0' && **p_env != '=')
+ **p_env += 1;
+ if (**p_env == '=')
+ **p_env += 1;
+ if (!(varval = ft_strdup(*p_env)))
+ {
+ ft_lcom_clear(&msh->curr);
+ ft_s_destroy(msh);
+ ft_fail_alloc(msh);
+ }
+ return (varval);
+ }
+ p_env += 1;
}
- return (argc);
+ return (NULL);
}
diff --git a/src/ft_u_utils.h b/src/ft_u_utils.h
index d025cb2..05b3ab3 100644
--- a/src/ft_u_utils.h
+++ b/src/ft_u_utils.h
@@ -17,5 +17,7 @@
uint64_t ft_get_argc(const char *args[]);
char *ft_get_home_dir(char *envp[]);
+char *ft_subst_var_value(const char varname[],
+ t_msh *msh);
#endif
diff --git a/src/minishell.c b/src/minishell.c
index 1df809b..36ba25a 100644
--- a/src/minishell.c
+++ b/src/minishell.c
@@ -30,6 +30,7 @@ int
int32_t ret;
/* TODO: increment $SHLVL */
+ /* TODO: handle general variables | $var */
if (!(msh = ft_init_msh(envp)))
{
ft_dprintf(2, "%s\n", strerror(errno));