summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-09-15 20:44:45 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-09-15 20:44:45 +0200
commit33a5806163fb91e4a72e188a9933ada20395f4e9 (patch)
tree24854a47dfc8fd77a0c4bf52c04f7b5c64e48cdc
parentInit fix (diff)
download42-minishell-33a5806163fb91e4a72e188a9933ada20395f4e9.tar.gz
42-minishell-33a5806163fb91e4a72e188a9933ada20395f4e9.tar.bz2
42-minishell-33a5806163fb91e4a72e188a9933ada20395f4e9.tar.xz
42-minishell-33a5806163fb91e4a72e188a9933ada20395f4e9.tar.zst
42-minishell-33a5806163fb91e4a72e188a9933ada20395f4e9.zip
Builtins ref toked
-rw-r--r--src/b_alias.c4
-rw-r--r--src/b_type.c17
-rw-r--r--src/d_define.h3
-rw-r--r--src/e_externs_pipes.c19
-rw-r--r--src/e_line.c17
-rw-r--r--src/s_destroy.c1
-rw-r--r--src/u_utils.c31
-rw-r--r--src/u_utils.h1
8 files changed, 42 insertions, 51 deletions
diff --git a/src/b_alias.c b/src/b_alias.c
index daa6e99..5ddc46f 100644
--- a/src/b_alias.c
+++ b/src/b_alias.c
@@ -99,9 +99,11 @@ uint8_t
ret = 0;
if (argc == 0)
{
+ if (msh->alias == NULL)
+ return (0);
b_print_alias_list(msh);
}
- if (argc > 0)
+ else if (argc > 0)
{
i = 0;
while (args[i] != NULL)
diff --git a/src/b_type.c b/src/b_type.c
index d8c6cef..c490aae 100644
--- a/src/b_type.c
+++ b/src/b_type.c
@@ -72,16 +72,19 @@ static uint8_t b_check_nonbuilt(char *ptr, uint8_t ret, t_msh *msh)
return (ret);
}
-static uint8_t b_check_builtins(char *ptr, t_msh *msh)
+static uint8_t b_check_builtins(char *ptr)
{
- char **p_bu;
+ char tmp[M_BUILTINS_REF_LEN];
+ char *tok_bu;
- p_bu = msh->bu_ref;
- while (*p_bu != NULL && ft_strncmp(ptr, *p_bu, ft_strlen(*p_bu) + 1) != 0)
+ ft_strlcpy(tmp, M_BUILTINS_REF, M_BUILTINS_REF_LEN);
+ tok_bu = ft_strtok(tmp, ":");
+ while (tok_bu != NULL
+ && ft_strncmp(ptr, tok_bu, ft_strlen(tok_bu) + 1) != 0)
{
- p_bu++;
+ tok_bu = ft_strtok(NULL, ":");
}
- if (*p_bu != NULL)
+ if (tok_bu != NULL)
{
ft_printf("%s is a shell builtin\n", ptr);
return (0);
@@ -110,7 +113,7 @@ uint8_t b_type(char *args[], t_msh *msh)
{
ft_printf("%s is aliased to `%s'\n", *ptr, p_alias->val);
}
- else if (b_check_builtins(*ptr, msh) == 1)
+ else if (b_check_builtins(*ptr) == 1)
ret = b_check_nonbuilt(*ptr, ret, msh);
ptr++;
}
diff --git a/src/d_define.h b/src/d_define.h
index b905d5f..f5dd32e 100644
--- a/src/d_define.h
+++ b/src/d_define.h
@@ -19,7 +19,8 @@
** ====== CLASSICS ======
*/
-#define FT_BUILTINS "echo:cd:pwd:export:unset:env:exit:type:[:alias"
+#define M_BUILTINS_REF "echo:cd:pwd:export:unset:env:exit:type:[:alias"
+#define M_BUILTINS_REF_LEN 48
/*
** ====== PSX ======
diff --git a/src/e_externs_pipes.c b/src/e_externs_pipes.c
index 05a2cf7..ca843d0 100644
--- a/src/e_externs_pipes.c
+++ b/src/e_externs_pipes.c
@@ -29,21 +29,6 @@
#include "u_path.h"
#include "u_utils.h"
-static uint8_t
- get_builtin_id(const char com[],
- t_msh *msh)
-{
- uint8_t i;
-
- i = 0;
- while (msh->bu_ref[i] && ft_strncmp(com, msh->bu_ref[i],
- ft_strlen(msh->bu_ref[i]) + 1) != 0)
- {
- i++;
- }
- return (i);
-}
-
static void
e_pipe_child(char *fullpath[],
uint8_t pipe_id,
@@ -56,7 +41,7 @@ static void
dup_redirs(ptr, msh);
if (ft_strncmp(fullpath[pipe_id], "builtin", 8) == 0)
{
- bu_id = get_builtin_id(ptr->bin, msh);
+ bu_id = u_get_builtin_id(ptr->bin);
ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh);
u_eof_fd(msh->fd);
s_lpipes_clear(&msh->pipes);
@@ -166,7 +151,7 @@ void
}
else
{
- if ((bu_id = get_builtin_id(rptr->com->bin, msh))
+ if ((bu_id = u_get_builtin_id(rptr->com->bin))
< FT_BUILTINS_COUNT)
{
if ((fullpath[i] = ft_strdup("builtin")) == NULL)
diff --git a/src/e_line.c b/src/e_line.c
index bb97556..caf3d75 100644
--- a/src/e_line.c
+++ b/src/e_line.c
@@ -20,20 +20,7 @@
#include "s_lpipes.h"
#include "s_com.h"
#include "s_struct.h"
-
-static uint8_t
- e_get_builtin_id(const char bin[], t_msh *msh)
-{
- uint8_t i;
-
- i = 0;
- while (msh->bu_ref[i] != NULL && ft_strncmp(bin, msh->bu_ref[i],
- ft_strlen(msh->bu_ref[i]) + 1) != 0)
- {
- i++;
- }
- return (i);
-}
+#include "u_utils.h"
void
e_line(t_msh *msh)
@@ -45,7 +32,7 @@ void
else if (msh->com != NULL)
{
if (msh->com->bin != NULL &&
- (bu_id = e_get_builtin_id(msh->com->bin, msh))
+ (bu_id = u_get_builtin_id(msh->com->bin))
< FT_BUILTINS_COUNT)
e_builtin(msh->com, bu_id, msh);
else if (msh->com->bin != NULL)
diff --git a/src/s_destroy.c b/src/s_destroy.c
index f9ce3c4..54f3d4a 100644
--- a/src/s_destroy.c
+++ b/src/s_destroy.c
@@ -21,7 +21,6 @@ void
{
ft_memdel((void*)&msh->shname);
ft_memdel((void*)&msh->cwd);
- ft_delwords(msh->bu_ref);
ft_delwords(msh->envp);
lvars_clear(&msh->vars);
s_lalias_clear(&msh->alias);
diff --git a/src/u_utils.c b/src/u_utils.c
index 4e332b3..d56d7c0 100644
--- a/src/u_utils.c
+++ b/src/u_utils.c
@@ -19,8 +19,7 @@
#include "f_fail.h"
#include "s_struct.h"
-t_bool
- u_is_not_escaped(const char *head, const char *ptr)
+t_bool u_is_not_escaped(const char *head, const char *ptr)
{
if (((ptr - head) == 0) ||
((ptr - head) >= 1 && *(ptr - 1) != C_BACKS) ||
@@ -29,8 +28,7 @@ t_bool
return (FALSE);
}
-void
- u_eof_fd(int32_t fd)
+void u_eof_fd(int32_t fd)
{
char *line;
@@ -39,11 +37,10 @@ void
ft_memdel((void*)&line);
}
-char
- **u_get_env_var_names(t_msh *msh)
+char **u_get_env_var_names(t_msh *msh)
{
- size_t i;
char **vars;
+ size_t i;
i = 0;
while (msh->envp[i] != NULL)
@@ -64,8 +61,7 @@ char
return (vars);
}
-uint64_t
- u_builtins_get_argc(const char *args[])
+uint64_t u_builtins_get_argc(const char *args[])
{
uint64_t argc;
@@ -76,3 +72,20 @@ uint64_t
}
return (argc);
}
+
+uint8_t u_get_builtin_id(const char bin[])
+{
+ char tmp[M_BUILTINS_REF_LEN];
+ char *tok;
+ uint8_t i;
+
+ i = 0;
+ ft_strlcpy(tmp, M_BUILTINS_REF, M_BUILTINS_REF_LEN);
+ tok = ft_strtok(tmp, ":");
+ while (tok != NULL && ft_strncmp(bin, tok, ft_strlen(tok) + 1) != 0)
+ {
+ tok = ft_strtok(NULL, ":");
+ i++;
+ }
+ return (i);
+}
diff --git a/src/u_utils.h b/src/u_utils.h
index 12606b7..496f50e 100644
--- a/src/u_utils.h
+++ b/src/u_utils.h
@@ -22,5 +22,6 @@ t_bool u_is_not_escaped(const char *head, const char *ptr);
void u_eof_fd(int32_t fd);
uint64_t u_builtins_get_argc(const char *args[]);
char **u_get_env_var_names(t_msh *msh);
+uint8_t u_get_builtin_id(const char bin[]);
#endif