aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-07-20 18:29:03 +0200
committerCharles <sircharlesaze@gmail.com>2020-07-20 18:29:03 +0200
commite77b1667e23a05f2874f80f5e47e634c58180c37 (patch)
treee76c06188cf2081f92c37621f619bbb624f761d6 /src
parent52bb7cc0f234776cd19c7a608b06578150d4695a (diff)
downloadminishell-e77b1667e23a05f2874f80f5e47e634c58180c37.tar.gz
minishell-e77b1667e23a05f2874f80f5e47e634c58180c37.tar.bz2
minishell-e77b1667e23a05f2874f80f5e47e634c58180c37.zip
Fixing glob by removing chdir, Changed cmd variables
Diffstat (limited to 'src')
-rw-r--r--src/eval/cmd.c43
-rw-r--r--src/eval/utils_eval.c34
-rw-r--r--src/main.c6
-rw-r--r--src/ms_glob.c77
-rw-r--r--src/preprocess.c2
-rw-r--r--src/utils.c7
6 files changed, 99 insertions, 70 deletions
diff --git a/src/eval/cmd.c b/src/eval/cmd.c
index c81c349..951b845 100644
--- a/src/eval/cmd.c
+++ b/src/eval/cmd.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/14 10:41:31 by charles #+# #+# */
-/* Updated: 2020/07/20 14:53:40 by nahaddac ### ########.fr */
+/* Updated: 2020/07/20 18:22:14 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -93,7 +93,7 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast)
t_fork_param_cmd param;
char **argv;
char *id;
- t_ftlst *tmp;
+ /* t_ftlst *tmp; */
if (!redir_extract(ast->redirs, env, fds))
{
@@ -103,21 +103,48 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast)
ast->redirs = NULL;
if ((param.env_local = env_from_array((char*[]){NULL})) == NULL)
return (-1);
- // TODO generate token list after `=` for variable value preprocessing
+
while (ast->cmd_argv != NULL
&& ((t_token*)ast->cmd_argv->data)->tag & TAG_IS_STR
&& utils_start_with_valid_identifier(((t_token*)ast->cmd_argv->data)->content))
{
+ t_ftlst *start;
+
id = ((t_token*)ast->cmd_argv->data)->content;
*ft_strchr(id, '=') = '\0';
((t_token*)ast->cmd_argv->data)->content = ft_strchr(id, '\0') + 1;
- tmp = split_token(&ast->cmd_argv, TAG_STICK);
- preprocess(tmp, env);
- ft_lstiter(tmp, token_debug);
+ if (*((t_token*)ast->cmd_argv->data)->content == '\0')
+ ft_lstpop_front(&ast->cmd_argv, NULL);
+ else
+ {
+ t_ftlst *curr = ast->cmd_argv;
+ t_ftlst *prev = curr;
+
+ while (curr != NULL
+ && ((t_token*)curr->data)->tag & TAG_STICK && ((t_token*)curr->data)->tag & TAG_IS_STR)
+ {
+ prev = curr;
+ curr = curr->next;
+ }
+ if (curr != NULL && ((t_token*)curr->data)->tag & TAG_IS_STR)
+ {
+ prev = curr;
+ curr = curr->next;
+ }
+
+ start = ast->cmd_argv;
+ ast->cmd_argv = prev->next;
+ prev->next = NULL;
+ }
+
+ /* ft_lstiter(start, token_debug); */
+ /* puts(""); */
+ /* ft_lstiter(ast->cmd_argv, token_debug); */
+
+ char **strs = preprocess(&start, env);
- if (env_export(param.env_local,id, ((t_token*)ast->cmd_argv->data)->content) == NULL)
+ if (env_export(param.env_local, id, strs[0]) == NULL)
return (-1);
- ft_lstpop_front(&ast->cmd_argv, (void (*)(void*))token_destroy);
}
if (ast->cmd_argv == NULL) // FIXME special env not passed to child processes
{
diff --git a/src/eval/utils_eval.c b/src/eval/utils_eval.c
index 374411e..5fefe70 100644
--- a/src/eval/utils_eval.c
+++ b/src/eval/utils_eval.c
@@ -6,7 +6,7 @@
/* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/07/20 13:07:25 by nahaddac #+# #+# */
-/* Updated: 2020/07/20 14:51:40 by nahaddac ### ########.fr */
+/* Updated: 2020/07/20 17:53:18 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,18 +17,38 @@ t_ftlst *split_token(t_ftlst **lst, enum e_token_tag tag)
{
t_ftlst *curr;
t_ftlst *start;
+ enum e_token_tag curr_tag;
start = *lst;
curr = *lst;
- while (curr != NULL || ((t_token *)curr->data)->tag & tag)
+ t_ftlst *prev = curr;
+ while (curr != NULL)
{
- if (curr->next == NULL || (!(((t_token *)curr->next->data)->tag & tag)))
+ curr_tag = ((t_token *)curr->data)->tag;
+ if (!(curr_tag & TAG_STICK) || !(curr_tag & TAG_IS_STR))
{
- *lst = curr->next;
- curr->next = NULL;
- return start;
+ *lst = prev->next;
+ prev->next = NULL;
+ return start;
}
+ prev = curr;
curr = curr->next;
}
- return start;
+ return start;
+
+
+
+ /* if (curr != NULL) */
+ /* curr_tag = ((t_token *)curr->data)->tag; */
+ /* while (curr != NULL && curr_tag & TAG_STICK && curr_tag & TAG_IS_STR) */
+ /* { */
+ /* curr = curr->next; */
+ /* curr_tag = ((t_token *)curr->data)->tag; */
+ /* if (curr == NULL || !(curr_tag & TAG_STICK) || !(curr_tag & TAG_IS_STR)) */
+ /* { */
+ /* *lst = curr->next; */
+ /* curr->next = NULL; */
+ /* } */
+ /* } */
+ /* return start; */
}
diff --git a/src/main.c b/src/main.c
index e1b956a..3e23206 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <cacharle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */
-/* Updated: 2020/07/20 11:21:23 by nahaddac ### ########.fr */
+/* Updated: 2020/07/20 17:35:51 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -66,7 +66,7 @@ int main(int argc, char **argv, char **envp)
path = path_update(NULL, env_search(env, "PATH"));
- char *env_exec_path;
+ /* char *env_exec_path; */
// if ((env_exec_path = ft_htget(path, "env")) == NULL)
// {
// errorf("env: command not found\n");
@@ -104,7 +104,7 @@ int main(int argc, char **argv, char **envp)
if (parser_out == NULL || parser_out->syntax_error)
return (1);
- ast_print(0, parser_out->ast);
+ /* ast_print(0, parser_out->ast); */
/* printf("===cmd_argv===\n"); */
/* ft_lstiter(parser_out->ast->cmd_argv, token_debug); */
diff --git a/src/ms_glob.c b/src/ms_glob.c
index 19be81c..059ff1c 100644
--- a/src/ms_glob.c
+++ b/src/ms_glob.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/05 11:44:07 by charles #+# #+# */
-/* Updated: 2020/07/19 15:21:08 by charles ### ########.fr */
+/* Updated: 2020/07/20 17:14:04 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -20,6 +20,8 @@
#define MATCHES_VEC_START_SIZE 16
+static char g_glob_path[PATH_MAX];
+
/*
** \brief Glob directory iteration function
** for subdirectory matches
@@ -31,23 +33,15 @@
*/
static int glob_iter_subdir(
- char *dirname,
struct dirent *entry,
struct s_glob_param *param,
char *subdir_pattern
)
{
- char subdir_name[PATH_MAX];
t_ftvec *subdir_matches;
size_t i;
- ft_strcat3(ft_strcpy(subdir_name, dirname), "/", entry->d_name);
- errno = 0;
- chdir(subdir_name);
- if (errno == EACCES)
- return (0);
subdir_matches = glob_matches(subdir_pattern);
- chdir(dirname);
if (subdir_matches == NULL)
return (-1);
i = 0;
@@ -78,7 +72,9 @@ static int glob_iter(
)
{
char *subdir_pattern;
+ int ret;
+ (void)dirname;
if (param->pattern[0] != '.' && entry->d_name[0] == '.')
return (0);
if ((subdir_pattern = ft_strchr(param->pattern, '/')) != NULL)
@@ -89,7 +85,6 @@ static int glob_iter(
subdir_pattern[-1] = '/';
return (0);
}
- /* printf("%s %s\n", dirname, entry->d_name); */
if (subdir_pattern != NULL)
{
if (entry->d_type != DT_DIR && entry->d_type != DT_LNK)
@@ -97,7 +92,10 @@ static int glob_iter(
subdir_pattern[-1] = '/';
return (0);
}
- return (glob_iter_subdir(dirname, entry, param, subdir_pattern));
+ ft_strcat3(g_glob_path, "/", entry->d_name);
+ ret = glob_iter_subdir(entry, param, subdir_pattern);
+ *ft_strrchr(g_glob_path, '/') = '\0';
+ return (ret);
}
if (ft_vecpush_safe(param->matches, ft_strdup(entry->d_name)) == NULL)
return (-1);
@@ -112,48 +110,16 @@ static int glob_iter(
t_ftvec *glob_matches(char *pattern)
{
- char dirname[PATH_MAX];
struct s_glob_param param;
- bool absolute;
- size_t i;
- if (getcwd(dirname, PATH_MAX) == NULL)
+ param.pattern = pattern;
+ if ((param.matches = ft_vecnew(MATCHES_VEC_START_SIZE)) == NULL)
return (NULL);
- absolute = *pattern == '/';
- if (*pattern == '/')
- pattern++;
- if ((param.pattern = ft_strdup(pattern)) == NULL ||
- (param.matches = ft_vecnew(MATCHES_VEC_START_SIZE)) == NULL)
+ if (utils_directory_iter(g_glob_path, &param, (t_directory_iter_func)glob_iter) == -1)
{
- free(param.pattern);
- return (NULL);
- }
- if (absolute)
- chdir("/");
- if (utils_directory_iter(absolute ? "/" : dirname, &param,
- (t_directory_iter_func)glob_iter) == -1)
- {
- chdir(dirname);
- free(param.pattern);
ft_vecdestroy(param.matches, free);
return (NULL);
}
- chdir(dirname);
- free(param.pattern);
- if (absolute)
- {
- i = 0;
- while (i < param.matches->size)
- {
- param.matches->data[i] = ft_strjoinf("/", param.matches->data[i], FT_STRJOINF_SND);
- if (param.matches->data[i] == NULL)
- {
- ft_vecdestroy(param.matches, free);
- return (NULL);
- }
- i++;
- }
- }
return (param.matches);
}
@@ -169,9 +135,26 @@ char *ms_glob(char *pattern)
{
char *join;
t_ftvec *matches;
+ bool absolute;
+ size_t i;
- if ((matches = glob_matches(pattern)) == NULL)
+ absolute = *pattern == '/';
+ if (absolute)
+ ft_strcpy(g_glob_path, "/");
+ else if (getcwd(g_glob_path, PATH_MAX) == NULL)
+ return (NULL);
+ if ((matches = glob_matches(absolute ? pattern + 1 : pattern)) == NULL)
return (NULL);
+ if (absolute)
+ {
+ i = -1;
+ while (++i < matches->size)
+ if ((matches->data[i] = ft_strjoinf("/", matches->data[i], FT_STRJOINF_SND)) == NULL)
+ {
+ ft_vecdestroy(matches, free);
+ return (NULL);
+ }
+ }
ft_vecsort(matches, ft_compar_str);
if (ft_vecpush(matches, NULL) == NULL ||
(join = ft_strsjoin((char**)matches->data, " ")) == NULL)
diff --git a/src/preprocess.c b/src/preprocess.c
index a834624..c9fbd8a 100644
--- a/src/preprocess.c
+++ b/src/preprocess.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/03 08:58:49 by charles #+# #+# */
-/* Updated: 2020/07/20 14:12:41 by nahaddac ### ########.fr */
+/* Updated: 2020/07/20 17:36:09 by charles ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/src/utils.c b/src/utils.c
index 3a27290..b301751 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/19 14:53:27 by charles ### ########.fr */
+/* Updated: 2020/07/20 17:06:21 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -26,9 +26,8 @@ int utils_directory_iter(
DIR *dir;
struct dirent *entry;
- errno = 0;
- if ((dir = opendir(dirname)) == NULL) // add fail safe
- return (errno == EACCES ? -2 : -1);
+ if ((dir = opendir(dirname)) == NULL) // EACCES and Not a directory with glob are ignored by bash
+ return (-2);
while ((entry = readdir(dir)) != NULL)
if (f(dirname, entry, param) == -1)
{