diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-04-03 15:13:08 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-04-03 15:13:08 +0200 |
| commit | c0e2ee28eedc1a9a886f9729a994d77738e2eb58 (patch) | |
| tree | f099ec9b9effe0db078a521c9405c5765b68fd8f | |
| parent | e5393671a265e1c301c6c303f21f938c4cf9ca75 (diff) | |
| download | minishell-c0e2ee28eedc1a9a886f9729a994d77738e2eb58.tar.gz minishell-c0e2ee28eedc1a9a886f9729a994d77738e2eb58.tar.bz2 minishell-c0e2ee28eedc1a9a886f9729a994d77738e2eb58.zip | |
Refactoring env, connecting pipes, preprocess draft
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | include/minishell.h | 15 | ||||
| m--------- | libft | 0 | ||||
| -rw-r--r-- | src/builtin/cd.c | 3 | ||||
| -rw-r--r-- | src/builtin/export.c | 4 | ||||
| -rw-r--r-- | src/env.c | 41 | ||||
| -rw-r--r-- | src/main.c | 55 | ||||
| -rw-r--r-- | src/path.c | 30 | ||||
| -rw-r--r-- | src/preprocess.c | 74 | ||||
| -rw-r--r-- | src/utils.c | 36 |
10 files changed, 181 insertions, 79 deletions
@@ -6,7 +6,7 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/02/03 04:14:24 by cacharle #+# #+# # -# Updated: 2020/04/01 18:08:25 by charles ### ########.fr # +# Updated: 2020/04/03 13:50:17 by charles ### ########.fr # # # # **************************************************************************** # diff --git a/include/minishell.h b/include/minishell.h index ac00875..bc404d4 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/04/01 22:13:47 by charles ### ########.fr */ +/* Updated: 2020/04/03 14:24:34 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,6 +34,7 @@ # include "libft_lst.h" # include "libft_util.h" # include "libft_vec.h" +# include "libft_dstr.h" /* ** \brief Value of pipe entry if closed @@ -104,9 +105,19 @@ int builtin_env(char **argv, t_env env); int builtin_exit(char **argv, t_env env); /* +** preprocess.c +*/ + +char *preprocess(char *input, t_env env); + +/* ** util.c - various utilitary functions */ -void ht_del_str_entry(t_ftht_entry *entry); +int utils_directory_iter( + char *dirname, + void *param, + int (*f)(char*, struct dirent*, void*) + ); #endif diff --git a/libft b/libft -Subproject d2feec1f97e9f8f201e56ad33662bb663c328a0 +Subproject bf263810b0a47a68fae0681e6e6d2229a488c06 diff --git a/src/builtin/cd.c b/src/builtin/cd.c index 1a7c70e..ed0b3cc 100644 --- a/src/builtin/cd.c +++ b/src/builtin/cd.c @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:10:20 by charles #+# #+# */ -/* Updated: 2020/04/02 11:33:15 by charles ### ########.fr */ +/* Updated: 2020/04/03 12:11:52 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ int builtin_cd(char **argv, t_env env) { + //change $PWD (void)env; if (argv[1] == NULL) return (1); diff --git a/src/builtin/export.c b/src/builtin/export.c index 3636407..1d362b3 100644 --- a/src/builtin/export.c +++ b/src/builtin/export.c @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:11:34 by charles #+# #+# */ -/* Updated: 2020/04/02 10:46:27 by charles ### ########.fr */ +/* Updated: 2020/04/03 12:11:38 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,8 @@ int builtin_export(char **argv, t_env env) { char *tmp; + // modify existing + // set with no string without '=' if (ft_strchr(argv[1], '=') == NULL) return (1); if ((tmp = ft_strdup(argv[1])) == NULL) @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 09:21:24 by cacharle #+# #+# */ -/* Updated: 2020/04/02 10:33:12 by charles ### ########.fr */ +/* Updated: 2020/04/03 14:25:31 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,8 @@ #include "minishell.h" +#define ENV_VEC_DEFAULT_SIZE 64 + /* ** \brief Convert array of string to environment hash table ** \param envp array of string (each in the format `name=value`) @@ -25,28 +27,22 @@ t_env env_from_array(char **envp) { + char *tmp; t_env env; - size_t i; - i = 0; - while (envp[i] != NULL) - if (ft_strchr(envp[i++], '=') == NULL) - return (NULL); - if ((env = ft_vecnew(i + 1)) == NULL) + if ((env = ft_vecnew(ENV_VEC_DEFAULT_SIZE)) == NULL) return (NULL); - env->size = i + 1; - i = 0; - while (envp[i] != NULL) + while (*envp != NULL) { - if ((env->data[i] = ft_strdup(envp[i])) == NULL) + if ((tmp = ft_strdup(*envp)) == NULL || + ft_vecpush(env, tmp) == NULL) { ft_vecdestroy(env, free); return (NULL); } - i++; + envp++; } - env->data[i] = NULL; - return (env); + return (ft_vecpush(env, NULL)); } /** @@ -69,3 +65,20 @@ char *env_search(t_env env, char *key) } return (NULL); } + +char *env_match_first(t_env env, const char *haystack) +{ + int len; + size_t i; + + len = -1; + while (ft_isalnum(haystack[len]) || haystack[len] == '_') + len++; + while (i < env->size) + { + if (ft_strncmp((char*)env->data[i], haystack, len) == 0) + return (ft_strchr((char*)env->data[i], '=') + 1); + i++; + } + return (NULL); +} @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ -/* Updated: 2020/04/03 08:56:46 by charles ### ########.fr */ +/* Updated: 2020/04/03 14:25:48 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,22 +38,22 @@ int main(int argc, char **argv, char **envp) /* int ret; */ env = env_from_array(envp); - path = path_update(NULL, env_search(env, "PATH")); - - t_ast *ast; - t_line line; - t_cmd cmd; - t_eval_state state; - - cmd.argv = ft_split("ls -l", ' '); - cmd.in = NULL; - cmd.out = NULL; - cmd.is_append = false; - - line.left = ast_new(TAG_CMD, &cmd); - line.right = NULL; - line.sep = SEP_END; - ast = ast_new(TAG_LINE, &line); + /* path = path_update(NULL, env_search(env, "PATH")); */ + /* */ + /* t_ast *ast; */ + /* t_line line; */ + /* t_cmd cmd; */ + /* t_eval_state state; */ + /* */ + /* cmd.argv = ft_split("ls -l", ' '); */ + /* cmd.in = NULL; */ + /* cmd.out = NULL; */ + /* cmd.is_append = false; */ + /* */ + /* line.left = ast_new(TAG_CMD, &cmd); */ + /* line.right = NULL; */ + /* line.sep = SEP_END; */ + /* ast = ast_new(TAG_LINE, &line); */ /* printf("%p\n", ast); */ /* printf("%d\n", ast->tag); */ @@ -64,15 +64,15 @@ int main(int argc, char **argv, char **envp) /* printf("%s\n", ast->data.line.left->data.cmd.argv[0]); */ /* printf("%s\n", ast->data.line.left->data.cmd.argv[1]); */ - state.pipe_in[0] = -1; - state.pipe_in[1] = -1; - state.pipe_out[0] = -1; - state.pipe_out[1] = -1; - state.path = path; - state.env = env; - t_io_frame frame; - io_frame_init(&frame); - printf("ret: %d %s\n", eval(&frame,&state, ast), strerror(errno)); + /* state.pipe_in[0] = -1; */ + /* state.pipe_in[1] = -1; */ + /* state.pipe_out[0] = -1; */ + /* state.pipe_out[1] = -1; */ + /* state.path = path; */ + /* state.env = env; */ + /* t_io_frame frame; */ + /* io_frame_init(&frame); */ + /* printf("ret: %d %s\n", eval(&frame,&state, ast), strerror(errno)); */ /* char buf[2048]; */ /* printf("%s\n", getcwd(buf, 2048)); */ @@ -85,7 +85,8 @@ int main(int argc, char **argv, char **envp) /* free(line); */ /* } */ /* free(line); */ - ft_htdestroy(path, free); + /* ft_htdestroy(path, free); */ + printf("%s\n", preprocess("*.c", env)); ft_vecdestroy(env, free); return (0); } @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/27 15:51:01 by cacharle #+# #+# */ -/* Updated: 2020/04/03 07:17:04 by charles ### ########.fr */ +/* Updated: 2020/04/03 13:53:01 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,27 +31,17 @@ ** \return Same path or NULL on error */ -static t_path st_path_dir_update(t_path path, char *dirname) +static int st_add_file(char *dirname, struct dirent *entry, void *path) { - DIR *dir; - struct dirent *entry; - char *filepath; + char *filepath; - if ((dir = opendir(dirname)) == NULL) - return (NULL); - while ((entry = readdir(dir)) != NULL) + if ((filepath = ft_strjoin3(dirname, "/", entry->d_name)) == NULL || + ft_htset((t_path)path, entry->d_name, filepath, free) == NULL) { - if ((filepath = ft_strjoin3(dirname, "/", entry->d_name)) == NULL || - ft_htset(path, entry->d_name, filepath, free) == NULL) - { - free(filepath); - closedir(dir); - return (NULL); - } + free(filepath); + return (-1); } - if (closedir(dir) == -1) - return (NULL); - return (path); + return (0); } /* @@ -74,8 +64,8 @@ t_path path_update(t_path path, char *path_var) if ((dirs = ft_split(path_var, ':')) == NULL) return (NULL); i = -1; - while (dirs[++i] != NULL) - if (st_path_dir_update(path, dirs[i]) == NULL) + while (dirs[++i] != NULL) // carefull with non existant dir error + if (utils_directory_iter(dirs[i], path, st_add_file) == -1) return (ft_split_destroy(dirs)); ft_split_destroy(dirs); return (path); diff --git a/src/preprocess.c b/src/preprocess.c new file mode 100644 index 0000000..30d2aa3 --- /dev/null +++ b/src/preprocess.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* preprocess.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 08:58:49 by charles #+# #+# */ +/* Updated: 2020/04/03 14:48:57 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +char *ms_glob(char *pattern) +{ + (void)pattern; + return (NULL); +} + +/* void iterpolate_iter_f(char **curr) */ +/* { */ +/* if (*curr != '$') */ +/* return (i + 1); */ +/* } */ + + +char *preprocess(char *input, t_env env) +{ + int i; + t_ftdstr *input_dstr; + char *glob_str; + /* char *tmp; */ + + glob_str = NULL; + if ((input_dstr = ft_dstrnew(input)) == NULL) + return (NULL); + i = 0; + while (input_dstr->str[i] != '\0') + { + if (input_dstr->str[i] == '*') + { + free(glob_str); + /* if ((glob_str = ms_glob()) = NULL) */ + /* { */ + /* ft_dstrdestroy(input_str); */ + /* return (NULL); */ + /* } */ + if (ft_dstrinsert(input_dstr, glob_str, 0) == NULL) + { + free(glob_str); + ft_dstrdestroy(input_dstr); + return (NULL); + } + i += strlen(glob_str); + } + /* else if (input_dstr->str[i] == '$') */ + /* { */ + /* if ((tmp = env_match_first(env, input_dstr->str + i + 1)) == NULL) */ + /* tmp = ""; */ + /* if (ft_dstrreplace(input_dstr, tmp, i, i + ft_strlen(tmp)) == NULL) */ + /* { */ + /* free(glob_str); */ + /* ft_dstrdestroy(input_dstr); */ + /* return (NULL); */ + /* } */ + /* i += ft_strlen(tmp); */ + /* } */ + /* else */ + i++; + } + printf("%d %d %s\n", input_dstr->length, input_dstr->capacity, input_dstr->str); + return (ft_dstrunwrap(input_dstr)); +} diff --git a/src/utils.c b/src/utils.c index 1649199..9ef99a6 100644 --- a/src/utils.c +++ b/src/utils.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:56:31 by cacharle #+# #+# */ -/* Updated: 2020/04/01 17:55:34 by charles ### ########.fr */ +/* Updated: 2020/04/03 14:58:17 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,17 +17,27 @@ #include "minishell.h" -/* -** \brief Delete function for a entry containing -** an allocated key and value -** \param entry Hash table entry -*/ - -void ht_del_str_entry(t_ftht_entry *entry) +int utils_directory_iter( + char *dirname, + void *param, + int (*f)(char*, struct dirent*, void*) +) { - if (entry == NULL) - return ; - free(entry->key); - free(entry->value); - free(entry); + DIR *dir; + struct dirent *entry; + + if ((dir = opendir(dirname)) == NULL) + return (-1); + while ((entry = readdir(dir)) != NULL) + if (f(dirname, entry, param) == -1) + { + closedir(dir); + return (-1); + } + if (closedir(dir) == -1) + return (-1); + return (0); + } + +/* int utils_not_alnum */ |
