diff options
| -rw-r--r-- | include/minishell.h | 20 | ||||
| -rw-r--r-- | include/ms_glob.h | 8 | ||||
| -rw-r--r-- | include/utils.h | 31 | ||||
| m--------- | minishell_test | 0 | ||||
| -rw-r--r-- | src/builtin/export.c | 61 | ||||
| -rw-r--r-- | src/builtin/unset.c | 23 | ||||
| -rw-r--r-- | src/env.c | 34 | ||||
| -rw-r--r-- | src/error.c (renamed from src/eval/error.c) | 12 | ||||
| -rw-r--r-- | src/eval/cmd.c | 6 | ||||
| -rw-r--r-- | src/ms_glob.c | 3 | ||||
| -rw-r--r-- | src/path.c | 3 | ||||
| -rw-r--r-- | src/utils.c | 15 |
12 files changed, 129 insertions, 87 deletions
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 <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 <dirent.h> # include <unistd.h> # include <stddef.h> +# include <limits.h> + # include "libft_str.h" # include "libft_vec.h" -# include "utils.h" -# include <limits.h> + +# 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* 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 -Subproject cc041d1901daa8be9197a59d963466fdc7e2b40 +Subproject 9132220296cdf6ab29c570fe0534649cfcc1cd8 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 <charles@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 <charles@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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/eval/error.c b/src/error.c index 10f5740..13a1443 100644 --- a/src/eval/error.c +++ b/src/error.c @@ -6,7 +6,7 @@ /* By: charles <charles@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 11:02:52 by charles #+# #+# */ -/* Updated: 2020/07/14 11:10:12 by nahaddac ### ########.fr */ +/* Updated: 2020/07/15 13:11:13 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,3 +53,13 @@ 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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/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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 <limits.h> /* ** \brief Match vector start size @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 <cacharle@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); +} |
