summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorsalad <fmoenne-@student.le-101.fr>2020-04-27 18:11:59 +0200
committersalad <fmoenne-@student.le-101.fr>2020-04-27 18:11:59 +0200
commitf3c766105a7dc8a3f1791374c41542ea833c53ce (patch)
treeba1ecd86edf60952a85528c2e4184c7ac9ed226e /src
parentfecho full buffered, quotes bav (diff)
parentFinished and normed type (diff)
download42-minishell-f3c766105a7dc8a3f1791374c41542ea833c53ce.tar.gz
42-minishell-f3c766105a7dc8a3f1791374c41542ea833c53ce.tar.bz2
42-minishell-f3c766105a7dc8a3f1791374c41542ea833c53ce.tar.xz
42-minishell-f3c766105a7dc8a3f1791374c41542ea833c53ce.tar.zst
42-minishell-f3c766105a7dc8a3f1791374c41542ea833c53ce.zip
merge master into fmoenne
Diffstat (limited to 'src')
-rw-r--r--src/ft_b_echo.c3
-rw-r--r--src/ft_b_type.c72
-rw-r--r--src/ft_e_externs.c3
-rw-r--r--src/ft_e_externs_next.c1
-rw-r--r--src/ft_p_lcom.c2
-rw-r--r--src/ft_p_lcom_next.c49
-rw-r--r--src/minishell.c1
7 files changed, 89 insertions, 42 deletions
diff --git a/src/ft_b_echo.c b/src/ft_b_echo.c
index 8a2d0d7..6b410b3 100644
--- a/src/ft_b_echo.c
+++ b/src/ft_b_echo.c
@@ -18,8 +18,7 @@
#include "ft_s_struct.h"
#include "ft_u_utils.h"
-/* TODO: norme, echo $variables (variables are subst when parsing the line), */
-/* echo "quoted text", echo 'quoted text', */
+/* TODO: echo "quoted text", echo 'quoted text', */
/* echo kill\nbackslash\nbut\nnot\nn, echo "quoted\nnew\nlines" */
/* Might need to go full buffer */
diff --git a/src/ft_b_type.c b/src/ft_b_type.c
index f6601d8..4338b32 100644
--- a/src/ft_b_type.c
+++ b/src/ft_b_type.c
@@ -12,13 +12,37 @@
#include <libft.h>
#include <stdint.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include "ft_f_fail.h"
+#include "ft_s_lcom.h"
#include "ft_s_struct.h"
#include "ft_e_externs_next.h"
#include "ft_u_utils.h"
+static int8_t
+ ft_absolute_path_exists(char com[])
+{
+ int32_t fd;
+ DIR *dir;
+
+ if ((dir = opendir(com)) != NULL)
+ {
+ closedir(dir);
+ return (0);
+ }
+ if ((fd = open(com, O_RDONLY)) != -1)
+ {
+ close(fd);
+ return (1);
+ }
+ return (0);
+}
+
static char
- *ft_type_get_path(char *com,
+ *ft_type_get_path(char com[],
t_msh *msh)
{
char **envpath;
@@ -28,7 +52,16 @@ static char
fullpath = NULL;
if (ft_ischarset("/.", com[0]))
{
- /* TODO: ft_get_absolute_path(ptr->com); */
+ if (ft_absolute_path_exists(com))
+ {
+ if (!(fullpath = ft_strdup(com)))
+ {
+ ft_lcom_clear(&msh->curr);
+ ft_fail_alloc(msh);
+ }
+ return (fullpath);
+ }
+ return (NULL);
}
else if ((envpath = ft_get_env_path(msh)) != NULL)
{
@@ -38,19 +71,37 @@ static char
return (fullpath);
}
+static uint8_t
+ ft_chk_nonbuilt(char **ptr,
+ t_msh *msh)
+{
+ char *fullpath;
+ int32_t ret;
+
+ ret = 0;
+ fullpath = ft_type_get_path(*ptr, msh);
+ if (fullpath)
+ ft_printf("%s is %s\n", *ptr, fullpath);
+ else
+ {
+ ft_printf("minishell: type: %s: not found\n", *ptr);
+ ret = 1;
+ }
+ ft_memdel((void*)&fullpath);
+ return (ret);
+}
+
uint8_t
ft_b_type(char *args[],
t_msh *msh)
{
- /* TODO: norme */
char **ptr;
char **p_bu;
- char *fullpath;
int32_t ret;
ptr = args;
if (!*ptr)
- return (1);
+ return (0);
ret = 0;
while (*ptr)
{
@@ -61,18 +112,9 @@ uint8_t
ft_printf("%s is a shell builtin\n", *ptr);
else
{
- fullpath = ft_type_get_path(*ptr, msh);
- if (fullpath)
- ft_printf("%s is %s\n", *ptr, fullpath);
- else
- {
- ft_printf("minishell: type: %s: not found\n", *ptr);
- ret = 1;
- }
- ft_memdel((void*)&fullpath);
+ ret = ft_chk_nonbuilt(ptr, msh);
}
ptr++;
}
- /* TODO: finish type */
return (ret);
}
diff --git a/src/ft_e_externs.c b/src/ft_e_externs.c
index 4cdcc3c..07bd9e1 100644
--- a/src/ft_e_externs.c
+++ b/src/ft_e_externs.c
@@ -70,7 +70,8 @@ void
if (ft_ischarset("/.", ptr->com[0]))
{
- /* TODO: ft_get_physical_path(ptr->com); */
+ ft_exec_path(ptr->com, ptr, msh);
+ return ;
}
else if ((envpath = ft_get_env_path(msh)) != NULL)
{
diff --git a/src/ft_e_externs_next.c b/src/ft_e_externs_next.c
index ab5f150..36bf5e6 100644
--- a/src/ft_e_externs_next.c
+++ b/src/ft_e_externs_next.c
@@ -53,7 +53,6 @@ char
char *fullpath;
DIR *dir;
- (void)com;
p_path = envpath;
while (*p_path)
{
diff --git a/src/ft_p_lcom.c b/src/ft_p_lcom.c
index 9a7a1b8..9a678e6 100644
--- a/src/ft_p_lcom.c
+++ b/src/ft_p_lcom.c
@@ -132,7 +132,7 @@ int8_t
i = 0;
if (!(words = ft_split(line, ';')))
return (-1);
- while (i <= count)
+ while (i <= count && words[i])
{
if (!(link = ft_lcom_new(words[i], msh)))
{
diff --git a/src/ft_p_lcom_next.c b/src/ft_p_lcom_next.c
index 272931d..07fadbe 100644
--- a/src/ft_p_lcom_next.c
+++ b/src/ft_p_lcom_next.c
@@ -17,46 +17,54 @@
#include "ft_s_struct.h"
#include "ft_u_vars.h"
+static int8_t
+ ft_subst_those_vars(int64_t i,
+ char **p_words,
+ t_msh *msh)
+{
+ size_t varlen;
+ char *s_varname;
+ char *varval;
+
+ varval = NULL;
+ s_varname = NULL;
+ varlen = i + 1;
+ while ((*p_words)[varlen] != '\0' && (*p_words)[varlen] != '$')
+ varlen += 1;
+ if (!(s_varname = ft_substr(*p_words, (uint32_t)i, varlen - i)))
+ return (-1);
+ varval = ft_subst_var_value(s_varname, msh);
+ *p_words = ft_strsubst(*p_words, s_varname, varval);
+ ft_memdel((void*)&s_varname);
+ ft_memdel((void*)&varval);
+ return (0);
+}
+
char
**ft_subst_vars(char *words[],
t_msh *msh)
{
- /* TODO: norme */
char **p_words;
- char *s_varname;
- char *varval;
int64_t i;
- size_t varlen;
p_words = words;
- varlen = 0;
- varval = NULL;
- s_varname = NULL;
while (*p_words)
{
while ((i = ft_strlchr(*p_words, '$')) != -1)
{
- varlen = i + 1;
- while ((*p_words)[varlen] != '\0' && (*p_words)[varlen] != '$')
- varlen += 1;
- if (!(s_varname = ft_substr(*p_words, (uint32_t)i, varlen)))
+ if (ft_subst_those_vars(i, p_words, msh) != 0)
return (NULL);
- ft_printf("a\n");
- varval = ft_subst_var_value(s_varname, msh);
- *p_words = ft_strsubst(*p_words, s_varname, varval);
- ft_memdel((void*)&s_varname);
- ft_memdel((void*)&varval);
}
p_words += 1;
}
return (words);
}
+
char
**ft_subst_args(const char word[],
int8_t redir)
{
- /* TODO: norme */
char **words;
char *subst;
size_t i;
@@ -70,11 +78,8 @@ char
i = 0;
while (word[i] && !ft_ischarset("<>", word[i]))
i++;
- if (redir > 0)
- {
- while (ft_isdigit(word[i]))
- i--;
- }
+ while (redir > 0 && ft_isdigit(word[i]))
+ i--;
if (!(subst = ft_substr(word, 0, i)))
return (NULL);
if (!(words = ft_split(subst, ' ')))
diff --git a/src/minishell.c b/src/minishell.c
index e9f9c3f..ce5dd30 100644
--- a/src/minishell.c
+++ b/src/minishell.c
@@ -31,6 +31,7 @@ int
int32_t ret;
/* TODO: increment $SHLVL */
+ /* TODO: also set $SHELL */
/* TODO: handle general variables | $var */
if (!(msh = ft_init_msh(argv, envp)))
{