From cc1b57c96cc8c0fdd53e781b54a83bb9c743179a Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 28 Feb 2020 09:41:52 +0100 Subject: Added environment and state Also dummy files eval.c parse.c --- include/minishell.h | 65 ++++++++++++++++++++++------------------------------- include/ms_parse.h | 58 +++++++++++++++++++++++++++++++++++++++++++++++ libft | 2 +- src/environment.c | 56 +++++++++++++++++++++++++++++++++++++++++++++ src/eval.c | 8 +++++++ src/main.c | 41 ++++++++++++++++++++------------- src/path.c | 22 ++++++------------ src/state.c | 31 +++++++++++++++++++++++++ src/util.c | 21 +++++++++++++++++ 9 files changed, 234 insertions(+), 70 deletions(-) create mode 100644 include/ms_parse.h create mode 100644 src/eval.c create mode 100644 src/state.c create mode 100644 src/util.c diff --git a/include/minishell.h b/include/minishell.h index cc28e10..5e27ffa 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/02/27 18:07:06 by cacharle ### ########.fr */ +/* Updated: 2020/02/28 12:34:21 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,58 +27,39 @@ # include "libft_lst.h" # include "libft_util.h" -typedef int t_status; +# include "ms_parse.h" -typedef enum -{ - REDIRECTION_OUT, - REDIRECTION_IN, - REDIRECTION_APPEND -} t_redirection_type; +# define MS_PATH_KEY "PATH" typedef struct { - char *filename; - t_redirection_type type; -} t_redirection; + t_ftht *commands; + t_ftlst *dirs; +} t_path; typedef struct { - char *name; - char **argv; - t_ftlst *redirections; -} t_command; + t_path *path; + t_ftht *environment; +} t_state; -typedef enum -{ - SEPARATOR_SEMICOLON, - SEPARATOR_PIPE, - SEPARATOR_AND, - SEPARATOR_OR, -} t_separator; +/* +** state.c +*/ -typedef struct -{ - t_ftlst *commands; - t_ftlst *separators; -} t_parsing; +int ms_state_init(t_state *state, const char **envp); +void ms_state_destroy(t_state *state); /* -** parse/.c +** eval.c */ -t_parsing *ms_parse(char *input); +int ms_eval(t_parsing *parsing); /* ** path.c */ -typedef struct -{ - t_ftht *commands; - t_ftlst *dirs; -} t_path; - t_path *ms_path_update(t_path *path, const char *path_str); void ms_path_destroy(t_path *path); @@ -86,15 +67,17 @@ void ms_path_destroy(t_path *path); ** environment.c */ -// t_ftht *ms_environment_update(t_ftht *environment, char **envp); -// char **ms_environment_(t_ftht *environment, char **envp); +t_ftht *ms_environment_from_array(const char **envp); +char **ms_environment_to_array(t_ftht *environment); +// probably bloat // void ms_environment_destroy(t_ftht *environment); - /* ** builtin*.c */ +typedef int t_status; + typedef t_status (*t_builtin_func)(int argc, char **argv, char **envp); t_builtin_func ms_echo; t_builtin_func ms_cd; @@ -104,4 +87,10 @@ t_builtin_func ms_unset; t_builtin_func ms_env; t_builtin_func ms_exit; +/* +** util.c +*/ + +void ms_ht_del_str_entry(t_ftht_content *content); + #endif diff --git a/include/ms_parse.h b/include/ms_parse.h new file mode 100644 index 0000000..043f065 --- /dev/null +++ b/include/ms_parse.h @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ms_parse.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/28 09:00:00 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:28:59 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MS_PARSE_H +# define MS_PARSE_H + +# include "minishell.h" + +typedef enum +{ + REDIRECTION_OUT, + REDIRECTION_IN, + REDIRECTION_APPEND +} t_redirection_type; + +typedef struct +{ + char *filename; + t_redirection_type type; +} t_redirection; + +typedef struct +{ + char *name; + char **argv; + t_ftlst *redirections; +} t_command; + +typedef enum +{ + SEPARATOR_SEMICOLON, + SEPARATOR_PIPE, + SEPARATOR_AND, + SEPARATOR_OR, +} t_separator; + +typedef struct +{ + t_ftlst *commands; + t_ftlst *separators; +} t_parsing; + +/* +** parse.c +*/ + +t_parsing *ms_parse(char *input); + +#endif diff --git a/libft b/libft index 8c25da5..915f1b8 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 8c25da5fc72aee70bca61130103416f1f68d3e70 +Subproject commit 915f1b888cf9c05e4b61321f84ac045eacd8ddd1 diff --git a/src/environment.c b/src/environment.c index e69de29..a39b246 100644 --- a/src/environment.c +++ b/src/environment.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* environment.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/28 09:21:24 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:30:55 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +#define MS_ENVIRONMENT_HT_SIZE 2048 + +t_ftht *ms_environment_from_array(const char **envp) +{ + t_ftht *environment; + int i; + char *key; + char *value; + /* int equal_pos; */ + + if (envp == NULL) + return (NULL); + if ((environment = ft_htnew(MS_ENVIRONMENT_HT_SIZE)) == NULL) + return (NULL); + i = -1; + while (envp[++i] != NULL) + { + // free stuff on error + if ((value = ft_strchr(envp[i], '=')) == NULL) + return (NULL); + if ((key = ft_strndup(envp[i], ft_strspn(envp[i], "="))) == NULL) + return (NULL); + if ((value = ft_strdup(value)) == NULL) + return (NULL); + if (ft_htset(environment, key, value, ms_ht_del_str_entry) == NULL) + return (NULL); + } + return (environment); +} + +char **ms_environment_to_array(t_ftht *environment) +{ + (void)environment; + // need ft_htlen + return (NULL); + +} + +/* void ms_environment_destroy(t_ftht *environment) */ +/* { */ +/* ft_htdestroy(environment, ms_ht_del_str_entry); */ +/* } */ diff --git a/src/eval.c b/src/eval.c new file mode 100644 index 0000000..2956662 --- /dev/null +++ b/src/eval.c @@ -0,0 +1,8 @@ + +#include "minishell.h" + +int ms_eval(t_parsing *parsing) +{ + (void)parsing; + return (0); +} diff --git a/src/main.c b/src/main.c index d4c6729..020b5e7 100644 --- a/src/main.c +++ b/src/main.c @@ -1,24 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ +/* Updated: 2020/02/28 11:50:29 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "minishell.h" int main(int argc, char **argv, char **envp) { + t_state state; + char *line; + int ret; - (void)argc; - (void)argv; - (void)envp; - // init - // find executable in PATH - - // user_loop - // get user input - // parse input - // if error: - // continue - // interpret command - // waiting for process to end + if (ms_state_init(&state, envp) == -1) + return (1); - // destroy - - + while ((ret = ft_next_line(STDIN_FILENO, &line)) == 1) + { + if (ms_eval(ms_parse(line)) == -1) + continue ; // and display error + free(line); + } + free(line); + ms_state_destroy(&state); return (0); } diff --git a/src/path.c b/src/path.c index 0a9dbd1..d4f4418 100644 --- a/src/path.c +++ b/src/path.c @@ -6,26 +6,18 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/27 15:51:01 by cacharle #+# #+# */ -/* Updated: 2020/02/27 18:07:16 by cacharle ### ########.fr */ +/* Updated: 2020/02/28 12:33:31 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static void st_path_command_entry_del(t_ftht_content *content) -{ - if (content == NULL) - return ; - free(content->key); - free(content->value); -} - static int st_in_dirs(char **dirs, char *dir) { while (*dirs != NULL) if (ft_strcmp(*dirs++, dir) == 0) - return (TRUE); // maybe change ft_lstremove_if func to cmp instead of bool - return (FALSE); + return (0); + return (-1); } static t_path *st_path_commands_update(t_path *path, char *dirname) @@ -40,7 +32,7 @@ static t_path *st_path_commands_update(t_path *path, char *dirname) { if ((tmp = ft_strjoin(dirname, entry->d_name)) == NULL) return (NULL); - if (ft_htset(path->commands, entry->d_name, st_path_command_entry_del) == NULL) + if (ft_htset(path->commands, entry->d_name, tmp, ms_ht_del_str_entry) == NULL) return (NULL); } if (closedir(dir) == -1) @@ -56,7 +48,7 @@ static t_path *st_path_dir_update(t_path *path, char *dirname) { t_ftlst *front; - if (ft_lstlfind(path->dirs, (int (*)(void*, void*))ft_strcmp, dirname) == NULL) + if (ft_lstlfind(path->dirs, (int (*)(const void*, const void*))ft_strcmp, dirname) == NULL) { if ((dirname = ft_strdup(dirname)) == NULL) return (NULL); @@ -85,7 +77,7 @@ t_path *ms_path_update(t_path *path, const char *path_str) return (NULL); if ((dirs = ft_split(path_str, ':')) == NULL) return (NULL); - ft_lstremove_if(&path->dirs, (t_ftbool (*)(void*, void*))st_in_dirs, free, (void*)dirs); // change t_ftbool to std bool + ft_lstremove_if(&path->dirs, (int (*)(const void*, const void*))st_in_dirs, free, (void*)dirs); i = -1; while (dirs[i] != NULL) if (st_path_dir_update(path, dirs[i]) == NULL) @@ -99,6 +91,6 @@ void ms_path_destroy(t_path *path) if (path == NULL) return ; ft_lstclear(&path->dirs, free); - ft_htdestroy(path->commands, st_path_command_entry_del); + ft_htdestroy(path->commands, ms_ht_del_str_entry); free(path); } diff --git a/src/state.c b/src/state.c new file mode 100644 index 0000000..61d8d41 --- /dev/null +++ b/src/state.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* state.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/28 11:51:54 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:36:10 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int ms_state_init(t_state *state, const char **envp) +{ + if ((state->environment = ms_environment_from_array(envp)) == NULL) + return (-1); + state->path = NULL; + if ((state->path = ms_path_update(state->path, ft_htget(state->environment, MS_PATH_KEY))) == NULL) + return (-1); + return (0); +} + +void ms_state_destroy(t_state *state) +{ + if (state == NULL) + return ; + ms_path_destroy(state->path); + ft_htdestroy(state->environment, ms_ht_del_str_entry); +} diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..d72f35a --- /dev/null +++ b/src/util.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* util.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/28 11:56:31 by cacharle #+# #+# */ +/* Updated: 2020/02/28 11:57:30 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void ms_ht_del_str_entry(t_ftht_content *content) +{ + if (content == NULL) + return ; + free(content->key); + free(content->value); +} -- cgit