aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-02-28 09:41:52 +0100
committerCharles <sircharlesaze@gmail.com>2020-02-28 12:38:58 +0100
commitcc1b57c96cc8c0fdd53e781b54a83bb9c743179a (patch)
treeb58d43c6fc5b3b57cdeb28f9a21f421d3400196c /src
parent83e7d63bc9c2d4a246df3cc8555127f3b956f960 (diff)
downloadminishell-cc1b57c96cc8c0fdd53e781b54a83bb9c743179a.tar.gz
minishell-cc1b57c96cc8c0fdd53e781b54a83bb9c743179a.tar.bz2
minishell-cc1b57c96cc8c0fdd53e781b54a83bb9c743179a.zip
Added environment and state
Also dummy files eval.c parse.c
Diffstat (limited to 'src')
-rw-r--r--src/environment.c56
-rw-r--r--src/eval.c8
-rw-r--r--src/main.c41
-rw-r--r--src/path.c22
-rw-r--r--src/state.c31
-rw-r--r--src/util.c21
6 files changed, 148 insertions, 31 deletions
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 <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}