diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-06-23 09:09:17 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-06-23 10:31:27 +0200 |
| commit | 11b258841f4a15c514c49af7d378b51cd6a8ab79 (patch) | |
| tree | 0df3f625f49fc1a8ca0e74f81272120f07a35feb /src | |
| parent | 58fc321ec43be4c3f7976769733116232361857a (diff) | |
| download | minishell-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
Diffstat (limited to 'src')
| -rw-r--r-- | src/builtin/builtin.c | 24 | ||||
| -rw-r--r-- | src/builtin/exit.c | 25 | ||||
| -rw-r--r-- | src/eval/cmd.c | 11 | ||||
| -rw-r--r-- | src/lexer/lexer.c | 46 | ||||
| -rw-r--r-- | src/lexer/token.c | 13 | ||||
| -rwxr-xr-x | src/parse/parse.c | 4 |
6 files changed, 77 insertions, 46 deletions
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) */ |
