aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-06-23 09:09:17 +0200
committerCharles <sircharlesaze@gmail.com>2020-06-23 10:31:27 +0200
commit11b258841f4a15c514c49af7d378b51cd6a8ab79 (patch)
tree0df3f625f49fc1a8ca0e74f81272120f07a35feb
parent58fc321ec43be4c3f7976769733116232361857a (diff)
downloadminishell-11b258841f4a15c514c49af7d378b51cd6a8ab79.tar.gz
minishell-11b258841f4a15c514c49af7d378b51cd6a8ab79.tar.bz2
minishell-11b258841f4a15c514c49af7d378b51cd6a8ab79.zip
Fixing builtin which needed to not be run in a child process, Added exit builtin
-rw-r--r--include/eval.h4
-rw-r--r--include/lexer.h3
-rw-r--r--include/minishell.h9
-rw-r--r--memo4
m---------minishell_test0
-rw-r--r--src/builtin/builtin.c24
-rw-r--r--src/builtin/exit.c25
-rw-r--r--src/eval/cmd.c11
-rw-r--r--src/lexer/lexer.c46
-rw-r--r--src/lexer/token.c13
-rwxr-xr-xsrc/parse/parse.c4
11 files changed, 86 insertions, 57 deletions
diff --git a/include/eval.h b/include/eval.h
index 97c1ce8..e300644 100644
--- a/include/eval.h
+++ b/include/eval.h
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:05:30 by charles #+# #+# */
-/* Updated: 2020/06/19 12:18:42 by charles ### ########.fr */
+/* Updated: 2020/06/23 08:33:02 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -35,7 +35,7 @@ typedef struct
char *exec_path;
char **argv;
t_env env;
- t_builtin_func builtin;
+ t_builtin_entry *builtin;
} t_fork_param_cmd;
# define MS_NO_FD -2
diff --git a/include/lexer.h b/include/lexer.h
index df05c20..b5df79f 100644
--- a/include/lexer.h
+++ b/include/lexer.h
@@ -6,7 +6,7 @@
/* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/19 10:51:26 by nahaddac #+# #+# */
-/* Updated: 2020/06/19 10:51:30 by nahaddac ### ########.fr */
+/* Updated: 2020/06/23 08:55:53 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -54,6 +54,7 @@ int lexer_verif_entre_cote(char *input, int i);
int lexe_space(char *input);
t_token *token_new(enum e_token_tag tag, char *content);
+t_token *token_new_until(enum e_token_tag tag, char *content, int n);
void token_destroy(t_token *token);
void token_destroy_lst(t_ftlst *tokens);
void token_destroy_lst2(t_ftlst *tokens1, t_ftlst *tokens2);
diff --git a/include/minishell.h b/include/minishell.h
index 5f4f56a..f2b237f 100644
--- a/include/minishell.h
+++ b/include/minishell.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/26 15:33:51 by cacharle #+# #+# */
-/* Updated: 2020/06/18 13:45:27 by charles ### ########.fr */
+/* Updated: 2020/06/23 08:32:46 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -79,13 +79,14 @@ typedef int (*t_builtin_func)(char **argv, t_env env);
** \param func Associated function
*/
-struct s_builtin_entry
+typedef struct
{
char *name;
t_builtin_func func;
-};
+ bool child_process;
+} t_builtin_entry;
-t_builtin_func builtin_search_func(char *name);
+t_builtin_entry *builtin_search_func(char *name);
int builtin_echo(char **argv, t_env env);
int builtin_cd(char **argv, t_env env);
diff --git a/memo b/memo
deleted file mode 100644
index 6085a41..0000000
--- a/memo
+++ /dev/null
@@ -1,4 +0,0 @@
-export A=a B=b C=c
-
-
-parenthèse dans parser_error.c
diff --git a/minishell_test b/minishell_test
-Subproject b463bfa34471c1d3e65dfd4e22a99f4c84d7c5c
+Subproject 3f2db95d5563c9c2b92a0531ddee28f79f43870
diff --git a/src/builtin/builtin.c b/src/builtin/builtin.c
index ae47a60..80b263e 100644
--- a/src/builtin/builtin.c
+++ b/src/builtin/builtin.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:11:01 by charles #+# #+# */
-/* Updated: 2020/06/14 12:52:12 by charles ### ########.fr */
+/* Updated: 2020/06/23 08:35:15 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,25 +21,25 @@
** \brief Array storing builtin executable name and associated functions
*/
-static struct s_builtin_entry g_builtin_lookup[] = {
- {"echo", builtin_echo},
- {"cd", builtin_cd},
- {"pwd", builtin_pwd},
- {"export", builtin_export},
- {"unset", builtin_unset},
- {"env", builtin_env},
- {"exit", builtin_exit},
+static t_builtin_entry g_builtin_lookup[] = {
+ {"echo", builtin_echo, true},
+ {"cd", builtin_cd, false},
+ {"pwd", builtin_pwd, true},
+ {"export", builtin_export, false},
+ {"unset", builtin_unset, false},
+ {"env", builtin_env, true},
+ {"exit", builtin_exit, false},
};
-t_builtin_func builtin_search_func(char *name)
+t_builtin_entry *builtin_search_func(char *name)
{
size_t i;
i = 0;
- while (i < sizeof(g_builtin_lookup) / sizeof(struct s_builtin_entry))
+ while (i < sizeof(g_builtin_lookup) / sizeof(t_builtin_entry))
{
if (ft_strcmp(g_builtin_lookup[i].name, name) == 0)
- return (g_builtin_lookup[i].func);
+ return (&g_builtin_lookup[i]);
i++;
}
return (NULL);
diff --git a/src/builtin/exit.c b/src/builtin/exit.c
index 1e6ec45..aa5d10b 100644
--- a/src/builtin/exit.c
+++ b/src/builtin/exit.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:10:16 by charles #+# #+# */
-/* Updated: 2020/04/01 17:10:17 by charles ### ########.fr */
+/* Updated: 2020/06/23 10:03:39 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,7 +19,28 @@
int builtin_exit(char **argv, t_env env)
{
- (void)argv;
+ int status;
+
(void)env;
+ if (argv[1] == NULL)
+ status = 0;
+ else if (argv[2] != NULL)
+ {
+ // replace with minishell error system
+ ft_putendl_fd("minishell: exit: too many arguments", STDERR_FILENO);
+ return 1;
+ }
+ else
+ {
+ errno = 0;
+ status = ft_atoi_strict(argv[1]);
+ if (errno != 0)
+ {
+ // replace with minishell error system
+ ft_putendl_fd("minishell: exit: ---argv[1]---: numeric argument required", STDERR_FILENO);
+ return 2;
+ }
+ }
+ exit(status % 256);
return (0);
}
diff --git a/src/eval/cmd.c b/src/eval/cmd.c
index 4405cdd..57f9899 100644
--- a/src/eval/cmd.c
+++ b/src/eval/cmd.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/14 10:41:31 by charles #+# #+# */
-/* Updated: 2020/06/19 12:03:13 by charles ### ########.fr */
+/* Updated: 2020/06/23 09:04:38 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -50,14 +50,13 @@ int forked_cmd(void *void_param)
t_fork_param_cmd *param;
param = void_param;
-
if (param->builtin != NULL)
- return (param->builtin(param->argv, param->env));
+ return (param->builtin->func(param->argv, param->env));
else
return (execve(param->exec_path, param->argv, (char**)param->env->data));
}
-int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast)
+int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast)
{
t_fork_param_cmd param;
char **argv;
@@ -76,6 +75,8 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast)
}
// can have no command (e.g `< file`)
+ if (argv[0] == NULL)
+ return (0);
param.builtin = builtin_search_func(argv[0]);
if (param.builtin == NULL)
{
@@ -87,6 +88,8 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast)
return (-1); // return error status
}
}
+ else if (!param.builtin->child_process)
+ return (param.builtin->func(argv, env));
param.argv = argv;
param.env = env;
diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c
index 9b43616..dd34654 100644
--- a/src/lexer/lexer.c
+++ b/src/lexer/lexer.c
@@ -69,24 +69,24 @@ int check_input_out(char *input)
return(0);
}
-t_token *lexer_lst_token_str(char *input, int i, int j)
-{
- t_token *lst_token;
-
- if (!(lst_token = malloc(sizeof(t_token) * 1)))
- return (NULL);
- lst_token->tag = 0;
- lst_token->content = NULL;
- if (!(lst_token->content = malloc(sizeof(char) * j + 1)))
- return(0);
- if (!(ft_strlcpy(lst_token->content, &input[i], j + 1)))
- {
- free(lst_token);
- return(0);
- }
-
- return (lst_token);
-}
+/* t_token *lexer_lst_token_str(char *input, int i, int j) */
+/* { */
+/* t_token *lst_token; */
+/* */
+/* if (!(lst_token = malloc(sizeof(t_token) * 1))) */
+/* return (NULL); */
+/* lst_token->tag = 0; */
+/* lst_token->content = NULL; */
+/* if (!(lst_token->content = malloc(sizeof(char) * j + 1))) */
+/* return(0); */
+/* if (!(ft_strlcpy(lst_token->content, &input[i], j + 1))) */
+/* { */
+/* free(lst_token); */
+/* return(0); */
+/* } */
+/* */
+/* return (lst_token); */
+/* } */
enum e_token_tag token_verif_stick(t_token *lst_token)
{
@@ -151,9 +151,10 @@ static t_ftlst *create_token_list(char *input, t_ftlst **lst)
{
j = 0;
j += check_input(&input[i]);
- lst_token = lexer_lst_token_str(input,i,j);
+ /* lst_token = lexer_lst_token_str(input,i,j); */
+ lst_token = token_new_until(0, input + i, j);
lst_token = push_token_enum(lst_token);
- new = ft_lstnew((void *) lst_token);
+ new = ft_lstnew(lst_token);
ft_lstpush_back(lst, new);
i += j;
}
@@ -165,10 +166,7 @@ t_ftlst *lexer(char *input)
t_ftlst *lst;
if (!input)
- return (0);
- lst = malloc(sizeof(t_ftlst ) * 1);
- if (!lst)
- return(0);
+ return (NULL);
lst = NULL;
lst = create_token_list(input, &lst);
lst = lexe_trim_out(lst);
diff --git a/src/lexer/token.c b/src/lexer/token.c
index 966a443..5465b6b 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/15 11:38:24 by charles ### ########.fr */
+/* Updated: 2020/06/23 08:55:32 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,13 +14,22 @@
t_token *token_new(enum e_token_tag tag, char *content)
{
+ size_t len;
+
+ if (content != NULL)
+ len = ft_strlen(content);
+ return (token_new_until(tag, content, len));
+}
+
+t_token *token_new_until(enum e_token_tag tag, char *content, int n)
+{
t_token *token;
if ((token = (t_token*)malloc(sizeof(t_token))) == NULL)
return (NULL);
if (content == NULL)
token->content = NULL;
- else if ((token->content = ft_strdup(content)) == NULL)
+ else if ((token->content = ft_strndup(content, n)) == NULL)
{
free(token);
return (NULL);
diff --git a/src/parse/parse.c b/src/parse/parse.c
index 8115d77..740726c 100755
--- a/src/parse/parse.c
+++ b/src/parse/parse.c
@@ -6,7 +6,7 @@
/* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/17 18:09:04 by nahaddac #+# #+# */
-/* Updated: 2020/06/19 19:12:25 by charles ### ########.fr */
+/* Updated: 2020/06/23 08:35:29 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -169,7 +169,7 @@ t_ret *parse_expr(t_ftlst *input)
t_ret *parse(t_ftlst *input)
{
t_ret *ret;
- t_ftlst *in_f;
+ /* t_ftlst *in_f; */
/* in_f = input; */
/* if (input == NULL) */