From 9c9e5ac17efca1cc22fd8cf69fb75a1e4701efe7 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 15 Jul 2020 13:27:31 +0200 Subject: Added export/unset builtin error message and status code --- include/minishell.h | 20 +++++++++++++++- include/ms_glob.h | 8 ++++--- include/utils.h | 31 ------------------------- minishell_test | 2 +- src/builtin/export.c | 61 ++++++++++++++++++++++++++++++++---------------- src/builtin/unset.c | 23 +++++++++++-------- src/env.c | 34 ++++++++++++++++----------- src/error.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/eval/cmd.c | 6 ++--- src/eval/error.c | 55 -------------------------------------------- src/ms_glob.c | 3 +-- src/path.c | 3 +-- src/utils.c | 15 +++++++++++- 13 files changed, 184 insertions(+), 142 deletions(-) delete mode 100644 include/utils.h create mode 100644 src/error.c delete mode 100644 src/eval/error.c diff --git a/include/minishell.h b/include/minishell.h index 67c2266..de6e923 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/26 15:33:51 by cacharle #+# #+# */ -/* Updated: 2020/07/14 11:06:50 by charles ### ########.fr */ +/* Updated: 2020/07/15 13:11:38 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -60,9 +60,11 @@ t_path path_update(t_path path, char *path_var); ** env.c */ +// FIXME refactor env code t_env env_from_array(char **envp); int env_keycmp(char *var, char *key); char *env_search(t_env env, char *key); +int env_search_index(t_env env, char *key); char *env_search_first_match(t_env env, const char *haystack); char *env_export(t_env env, char *key, char *value); @@ -133,5 +135,21 @@ typedef struct } t_error; void error_eval_put(enum e_error id, char *content); +void error_put_invalid_identifier(char *prefix, char *identifier); + +/* +** utils.c +*/ + +typedef int (*t_directory_iter_func)(char*, struct dirent*, void*); + +int utils_directory_iter( + char *dirname, + void *param, + t_directory_iter_func f +); + +size_t utils_var_end(char *name); +bool utils_valid_identifier(char *name); #endif diff --git a/include/ms_glob.h b/include/ms_glob.h index 5b5c932..e340c45 100644 --- a/include/ms_glob.h +++ b/include/ms_glob.h @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/05 11:45:11 by charles #+# #+# */ -/* Updated: 2020/06/09 17:51:34 by charles ### ########.fr */ +/* Updated: 2020/07/15 12:12:22 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,10 +16,12 @@ # include # include # include +# include + # include "libft_str.h" # include "libft_vec.h" -# include "utils.h" -# include + +# include "minishell.h" struct s_glob_param { diff --git a/include/utils.h b/include/utils.h deleted file mode 100644 index 6f2fbc9..0000000 --- a/include/utils.h +++ /dev/null @@ -1,31 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* utils.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: charles +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/04/05 12:05:49 by charles #+# #+# */ -/* Updated: 2020/04/05 14:51:38 by charles ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef UTILS_H -# define UTILS_H - -/* -** \file utils.h -** \brief Various utilitary functions -*/ - -typedef int (*t_directory_iter_func)(char*, struct dirent*, void*); - -int utils_directory_iter( - char *dirname, - void *param, - t_directory_iter_func f -); - -size_t utils_var_end(char *name); - -#endif diff --git a/minishell_test b/minishell_test index cc041d1..9132220 160000 --- a/minishell_test +++ b/minishell_test @@ -1 +1 @@ -Subproject commit cc041d1901daa8be9197a59d963466fdc7e2b404 +Subproject commit 9132220296cdf6ab29c570fe0534649cfcc1cd8d diff --git a/src/builtin/export.c b/src/builtin/export.c index f41bcb0..809b809 100644 --- a/src/builtin/export.c +++ b/src/builtin/export.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:11:34 by charles #+# #+# */ -/* Updated: 2020/06/19 11:21:50 by nahaddac ### ########.fr */ +/* Updated: 2020/07/15 13:22:18 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,35 +15,56 @@ ** \brief `export` builtin */ -// modify existing -// set with no string without '=' -// TODO: multiple exported variable (e.g export A=a B=b C=c) - #include "minishell.h" -int builtin_export(char **argv, t_env env) +static void st_put_declare_x(char *s) { - char *temp; + char *equal_ptr; + + if (s == NULL) + return ; + ft_putstr("declare -x "); + if ((equal_ptr = ft_strchr(s, '=')) == NULL) + equal_ptr = ft_strchr(s, '\0'); + write(STDOUT_FILENO, s, equal_ptr - s); + ft_putchar('='); + ft_putchar('"'); + ft_putstr(equal_ptr + 1); + ft_putchar('"'); + ft_putchar('\n'); +} + +int builtin_export(char **argv, t_env env) +{ + int status; size_t i; + char *equal_ptr; + bool skip; - (void)env; if (argv[1] == NULL) + { + ft_veciter(env, (void (*)(void*))st_put_declare_x); return (0); - if(ft_isdigit(argv[1][0])) - return(0); + } + status = 0; i = 0; - temp = argv[1]; - while(temp[i] != '\0') + while (argv[++i] != NULL) { - if(temp[i] == ' ' || ft_isalnum(temp[i]) == 0) - return(0); - if (temp[i] == '=') + skip = (equal_ptr = ft_strchr(argv[i], '=')) == NULL; + if (!skip) + *equal_ptr = '\0'; + if (!utils_valid_identifier(argv[i])) { - temp[i] = '\0'; - env_export(env, temp, &argv[1][i + 1]); - return(0); + if (!skip) + *equal_ptr = '='; + error_put_invalid_identifier("export", argv[i]); + status = 1; + continue; } - i++; + if (skip) + continue; + if (env_export(env, argv[i], equal_ptr + 1) == NULL) + return (127); // malloc error } - return (0); + return (status); } diff --git a/src/builtin/unset.c b/src/builtin/unset.c index b1e2d5b..b7b6d58 100644 --- a/src/builtin/unset.c +++ b/src/builtin/unset.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:10:51 by charles #+# #+# */ -/* Updated: 2020/06/17 12:41:45 by nahaddac ### ########.fr */ +/* Updated: 2020/07/15 13:14:38 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,18 +20,23 @@ int builtin_unset(char **argv, t_env env) { size_t i; + int found_index; + int status; - if (argv[1] == NULL) - return (1); + status = 0; i = 0; - while (i < env->size) + while (argv[++i] != NULL) { - if (env_keycmp(env->data[i],argv[1]) == 0) + if (!utils_valid_identifier(argv[i])) { - ft_vecremove(env, i, free); - return (0); + error_put_invalid_identifier("unset", argv[i]); + status = 1; + continue; // put invalid identifier } - i++; + found_index = env_search_index(env, argv[i]); + if (found_index == -1) + continue; + ft_vecremove(env, found_index, free); } - return (1); + return (status); } diff --git a/src/env.c b/src/env.c index bdc2498..1ed5162 100644 --- a/src/env.c +++ b/src/env.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 09:21:24 by cacharle #+# #+# */ -/* Updated: 2020/07/13 11:09:39 by charles ### ########.fr */ +/* Updated: 2020/07/15 13:02:04 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,6 +77,20 @@ char *env_search(t_env env, char *key) return (NULL); } +int env_search_index(t_env env, char *key) +{ + size_t i; + + i = 0; + while (i < env->size - 1) + { + if (env_keycmp(env->data[i], key) == 0) + return (i); + i++; + } + return (-1); +} + char *env_search_first_match(t_env env, const char *haystack) { size_t len; @@ -107,28 +121,20 @@ char *env_search_first_match(t_env env, const char *haystack) char *env_export(t_env env, char *key, char *value) { char *joined; - size_t i; + int i; if ((joined = ft_strjoin3(key, "=", value)) == NULL) return (NULL); - if (env_search(env, key) == NULL) + i = env_search_index(env, key); + if (i == -1) { if (ft_vecinsert(env, env->size - 1, joined) == NULL) return (NULL); } else { - i = 0; - while (i < env->size - 1) - { - if (env_keycmp(env->data[i], key) == 0) - { - free(env->data[i]); - env->data[i] = joined; - break; - } - i++; - } + free(env->data[i]); + env->data[i] = joined; } return (joined); } diff --git a/src/error.c b/src/error.c new file mode 100644 index 0000000..13a1443 --- /dev/null +++ b/src/error.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* error.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/06/14 11:02:52 by charles #+# #+# */ +/* Updated: 2020/07/15 13:11:13 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "eval.h" + +static t_error g_errors[] = +{ + {ERROR_AMBIGUOUS_REDIR, 1, "ambiguous redirect"}, + {ERROR_OPEN, 1, NULL}, + {ERROR_CMD_NOT_FOUND, 127, "command not found"}, +}; + +t_error *st_error_get(enum e_error id) +{ + size_t i; + t_error *match; + + match = NULL; + i = 0; + while (i < sizeof(g_errors) / sizeof(t_error)) + { + if (g_errors[i].id == id) + match = &g_errors[i]; + i++; + } + return (match); +} + +void error_eval_put(enum e_error id, char *unexpected) +{ + t_error *err; + + err = st_error_get(id); + ft_putstr_fd("minishell: ", STDERR_FILENO); + ft_putstr_fd(unexpected, STDERR_FILENO); + ft_putstr_fd(": ", STDERR_FILENO); + if (err->msg == NULL) + ft_putendl_fd(strerror(errno), STDERR_FILENO); + else + ft_putendl_fd(err->msg, STDERR_FILENO); +} + +int error_status(enum e_error id) +{ + return (st_error_get(id)->status); +} + +void error_put_invalid_identifier(char *prefix, char *identifier) +{ + ft_putstr_fd(g_basename, STDERR_FILENO); + ft_putstr_fd(": ", STDERR_FILENO); + ft_putstr_fd(prefix, STDERR_FILENO); + ft_putstr_fd(": `", STDERR_FILENO); + ft_putstr_fd(identifier, STDERR_FILENO); + ft_putstr_fd("': not a valid identifier\n", STDERR_FILENO); +} diff --git a/src/eval/cmd.c b/src/eval/cmd.c index b3cdc15..196d810 100644 --- a/src/eval/cmd.c +++ b/src/eval/cmd.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 10:41:31 by charles #+# #+# */ -/* Updated: 2020/07/14 11:07:00 by charles ### ########.fr */ +/* Updated: 2020/07/15 13:03:20 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -92,8 +92,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) // solved with pipeline */ - /* return (param.builtin->func(argv, env)); */ + else// solved with pipeline + return (param.builtin->func(argv, env)); param.argv = argv; param.env = env; diff --git a/src/eval/error.c b/src/eval/error.c deleted file mode 100644 index 10f5740..0000000 --- a/src/eval/error.c +++ /dev/null @@ -1,55 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* error.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: charles +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/06/14 11:02:52 by charles #+# #+# */ -/* Updated: 2020/07/14 11:10:12 by nahaddac ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "eval.h" - -static t_error g_errors[] = -{ - {ERROR_AMBIGUOUS_REDIR, 1, "ambiguous redirect"}, - {ERROR_OPEN, 1, NULL}, - {ERROR_CMD_NOT_FOUND, 127, "command not found"}, -}; - -t_error *st_error_get(enum e_error id) -{ - size_t i; - t_error *match; - - match = NULL; - i = 0; - while (i < sizeof(g_errors) / sizeof(t_error)) - { - if (g_errors[i].id == id) - match = &g_errors[i]; - i++; - } - return (match); -} - -void error_eval_put(enum e_error id, char *unexpected) -{ - t_error *err; - - err = st_error_get(id); - ft_putstr_fd("minishell: ", STDERR_FILENO); - ft_putstr_fd(unexpected, STDERR_FILENO); - ft_putstr_fd(": ", STDERR_FILENO); - if (err->msg == NULL) - ft_putendl_fd(strerror(errno), STDERR_FILENO); - else - ft_putendl_fd(err->msg, STDERR_FILENO); -} - -int error_status(enum e_error id) -{ - return (st_error_get(id)->status); -} diff --git a/src/ms_glob.c b/src/ms_glob.c index dae2bd8..7603286 100644 --- a/src/ms_glob.c +++ b/src/ms_glob.c @@ -6,12 +6,11 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/05 11:44:07 by charles #+# #+# */ -/* Updated: 2020/06/17 14:39:52 by charles ### ########.fr */ +/* Updated: 2020/07/15 12:12:02 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "ms_glob.h" -#include /* ** \brief Match vector start size diff --git a/src/path.c b/src/path.c index d768d07..579fd81 100644 --- a/src/path.c +++ b/src/path.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/27 15:51:01 by cacharle #+# #+# */ -/* Updated: 2020/04/05 12:09:05 by charles ### ########.fr */ +/* Updated: 2020/07/15 12:11:48 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,6 @@ */ #include "minishell.h" -#include "utils.h" /* ** \brief Number of buckets of a path hash table diff --git a/src/utils.c b/src/utils.c index 4a9ecb8..b7d4404 100644 --- a/src/utils.c +++ b/src/utils.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:56:31 by cacharle #+# #+# */ -/* Updated: 2020/07/14 10:40:59 by nahaddac ### ########.fr */ +/* Updated: 2020/07/15 12:54:28 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,3 +63,16 @@ size_t utils_var_end(char *name) i++; return (i + 1); } + +bool utils_valid_identifier(char *name) +{ + if (ft_isdigit(*name) || *name == '\0') + return (false); + while (*name != '\0') + { + if (!ft_isalnum(*name) && *name != '_') + return (false); + name++; + } + return (true); +} -- cgit