aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/builtin/cd.c3
-rw-r--r--src/builtin/export.c4
-rw-r--r--src/env.c41
-rw-r--r--src/main.c55
-rw-r--r--src/path.c30
-rw-r--r--src/preprocess.c74
-rw-r--r--src/utils.c36
7 files changed, 167 insertions, 76 deletions
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)
diff --git a/src/env.c b/src/env.c
index 631c2b4..d402a1b 100644
--- a/src/env.c
+++ b/src/env.c
@@ -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);
+}
diff --git a/src/main.c b/src/main.c
index f898bf8..2e42601 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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);
}
diff --git a/src/path.c b/src/path.c
index 1576a66..c9f184c 100644
--- a/src/path.c
+++ b/src/path.c
@@ -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 */