aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--Makefile2
-rw-r--r--include/ast.h8
-rw-r--r--include/eval.h35
-rw-r--r--include/minishell.h16
-rw-r--r--include/parser.h4
-rw-r--r--print_argv_env_main.c18
-rw-r--r--src/ast.c2
-rw-r--r--src/builtin/cd.c15
-rw-r--r--src/builtin/export.c40
-rw-r--r--src/builtin/unset.c6
-rw-r--r--src/env.c50
-rw-r--r--src/eval/cmd.c21
-rw-r--r--src/eval/op.c49
-rw-r--r--src/eval/redir.c13
-rw-r--r--src/main.c247
-rw-r--r--src/ms_glob.c27
-rw-r--r--src/utils.c4
18 files changed, 326 insertions, 232 deletions
diff --git a/.gitignore b/.gitignore
index b30589d..afd864a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,3 +8,4 @@ tmp/*
.DS_Store
a.out
vgcore.*
+*.orig
diff --git a/Makefile b/Makefile
index c8a6df4..d0496bf 100644
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/02/03 04:14:24 by cacharle #+# #+# #
-# Updated: 2020/06/12 09:17:28 by charles ### ########.fr #
+# Updated: 2020/06/17 16:26:19 by charles ### ########.fr #
# #
# **************************************************************************** #
diff --git a/include/ast.h b/include/ast.h
index 317e1b8..53221fa 100644
--- a/include/ast.h
+++ b/include/ast.h
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:05:38 by charles #+# #+# */
-/* Updated: 2020/06/17 16:43:12 by nahaddac ### ########.fr */
+/* Updated: 2020/06/18 13:35:38 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -26,7 +26,7 @@
struct s_ast;
/*
-** \brief Line struct
+** \brief Operation struct
** \param left AST to the left of separator
** \param right AST to the right of separator
** \param sep Type of separator
@@ -43,7 +43,7 @@ typedef struct s_op
/*
** \brief AST node tag (type)
** \param TAG_CMD Command AST node
-** \param TAG_LINE Line AST node
+** \param TAG_OP Operation AST node
*/
enum e_ast_tag
@@ -56,7 +56,7 @@ enum e_ast_tag
/*
** \brief AST node struct
** \param tag Node tag
-** \param line Line struct
+** \param op Operation struct
** \param cmd_argv Array of string tokens
** \param in STDIN redirection string tokens
** \param out STDOUT redirection string tokens
diff --git a/include/eval.h b/include/eval.h
index bde0cd7..60d6f23 100644
--- a/include/eval.h
+++ b/include/eval.h
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:05:30 by charles #+# #+# */
-/* Updated: 2020/06/17 15:49:49 by nahaddac ### ########.fr */
+/* Updated: 2020/06/18 13:38:53 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,6 +19,7 @@
*/
# include "minishell.h"
+# include "lexer.h"
# include "ast.h"
/*
@@ -47,7 +48,7 @@ typedef struct
t_op *op;
int fd_in;
int fd_out;
-} t_fork_param_line;
+} t_fork_param_op;
typedef struct
{
@@ -57,21 +58,16 @@ typedef struct
t_builtin_func builtin;
} t_fork_param_cmd;
-#define MS_NO_FD -2
+# define MS_NO_FD -2
+# define FDS_WRITE 1
+# define FDS_READ 0
/*
-** eval.c
+** op.c
*/
-int eval(int fd_in, int fd_out, t_eval_state *state, t_ast *ast);
-
-/*
-** exec.c
-*/
-
-bool exec_is_path(char *path_str);
-bool exec_is_valid(char *exec_path);
-char *exec_search_path(t_path path, char *path_var, char *exec_name);
+int eval_op(int fds[2], t_env env, t_path path, t_op *op);
+int eval(int fds[2], t_env env, t_path path, t_ast *ast);
enum e_error
{
@@ -99,12 +95,21 @@ void error_eval_put(enum e_error id, char *unexpected);
** cmd.c
*/
-int eval_cmd(t_env env, t_path path, t_ast *ast);
+int fork_wrap(int fds[2], void *passed, int (*wrapped)(void *param));
+int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast);
/*
** redir.c
*/
-bool redir_extract(t_ftlst *redirs, t_env env, int *fd_in, int *fd_out);
+bool redir_extract(t_ftlst *redirs, t_env env, int fds[2]);
+
+/*
+** exec.c
+*/
+
+bool exec_is_path(char *exec_name);
+bool exec_is_valid(char *exec_path);
+char *exec_search_path(t_path path, char *path_var, char *exec_name);
#endif
diff --git a/include/minishell.h b/include/minishell.h
index 97b12e4..5f4f56a 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/06/15 09:47:20 by charles ### ########.fr */
+/* Updated: 2020/06/18 13:45:27 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -42,18 +42,6 @@
# define PIPE_CLOSED -1
-/*
-** \brief Pipe write index
-*/
-
-# define PIPE_WRITE 1
-
-/*
-** \brief Pipe read index
-*/
-
-# define PIPE_READ 0
-
# define BUILTIN_NOT_FOUND -2
typedef t_ftht* t_path;
@@ -70,8 +58,10 @@ t_path path_update(t_path path, char *path_var);
*/
t_env env_from_array(char **envp);
+int env_keycmp(char *var, char *key);
char *env_search(t_env env, char *key);
char *env_search_first_match(t_env env, const char *haystack);
+char *env_export(t_env env, char *key, char *value);
/*
** builtin*.c - directory with all builtin commands
diff --git a/include/parser.h b/include/parser.h
index 0dfeb18..f3a425e 100644
--- a/include/parser.h
+++ b/include/parser.h
@@ -6,7 +6,7 @@
/* By: cacharle <cacharle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 09:00:00 by cacharle #+# #+# */
-/* Updated: 2020/06/18 12:40:22 by nahaddac ### ########.fr */
+/* Updated: 2020/06/18 14:04:07 by nahaddac ### ########.fr */
/* */
/* ************************************************************************** */
@@ -39,7 +39,7 @@
t_ret *parse(t_ftlst *input);
-t_ret *parse_op(t_ftlst *input)
+t_ret *parse_op(t_ftlst *input);
t_ret *parse_expr(t_ftlst *input);
t_ret *parse_cmd(t_ftlst *input);
diff --git a/print_argv_env_main.c b/print_argv_env_main.c
index dcc6e0e..4011275 100644
--- a/print_argv_env_main.c
+++ b/print_argv_env_main.c
@@ -1,12 +1,18 @@
#include <stdio.h>
+#include <unistd.h>
+#include <limits.h>
int main(int argc, char **argv, char **envp)
{
- printf("ARGV:\n");
- for (int i = 0; i < argc; i++)
- printf("[%d] %s\n", i, argv[i]);
- printf("\nENV:\n");
- for (int i = 0; envp[i] != NULL && i < 10; i++)
- printf("[%d] %s\n", i, envp[i]);
+ char buf[PATH_MAX];
+
+ getcwd(buf, PATH_MAX);
+ printf("%s\n", buf);
+ /* printf("ARGV:\n"); */
+ /* for (int i = 0; i < argc; i++) */
+ /* printf("[%d] %s\n", i, argv[i]); */
+ /* printf("\nENV:\n"); */
+ /* for (int i = 0; envp[i] != NULL && i < 10; i++) */
+ /* printf("[%d] %s\n", i, envp[i]); */
return 0;
}
diff --git a/src/ast.c b/src/ast.c
index 8a551ef..c878062 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:05:42 by charles #+# #+# */
-/* Updated: 2020/06/17 15:48:56 by nahaddac ### ########.fr */
+/* Updated: 2020/06/18 13:39:30 by charles ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/src/builtin/cd.c b/src/builtin/cd.c
index ed0b3cc..cdcdaa4 100644
--- a/src/builtin/cd.c
+++ b/src/builtin/cd.c
@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
-/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:10:20 by charles #+# #+# */
-/* Updated: 2020/04/03 12:11:52 by charles ### ########.fr */
+/* Updated: 2020/06/17 12:36:29 by nahaddac ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,11 +19,16 @@
int builtin_cd(char **argv, t_env env)
{
- //change $PWD
+ char buf[PATH_MAX];
+
(void)env;
if (argv[1] == NULL)
return (1);
- if (chdir(argv[1]) == -1)
- return (1);
+ if (chdir(argv[1]) != -1)
+ {
+ if (!(getcwd(buf, PATH_MAX)))
+ return(1);
+ env_export(env, "PWD", buf);
+ }
return (0);
}
diff --git a/src/builtin/export.c b/src/builtin/export.c
index 1d362b3..c60d8f0 100644
--- a/src/builtin/export.c
+++ b/src/builtin/export.c
@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* export.c :+: :+: :+: */
/* +:+ +:+ +:+ */
-/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:11:34 by charles #+# #+# */
-/* Updated: 2020/04/03 12:11:38 by charles ### ########.fr */
+/* Updated: 2020/06/18 13:50:47 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,19 +15,35 @@
** \brief `export` builtin
*/
+// modify existing
+// set with no string without '='
+// TODO: multiple exported variable (e.g export A=a B=b C=c)
+
#include "minishell.h"
int builtin_export(char **argv, t_env env)
{
- char *tmp;
+ char *temp;
+ size_t i;
- // modify existing
- // set with no string without '='
- if (ft_strchr(argv[1], '=') == NULL)
- return (1);
- if ((tmp = ft_strdup(argv[1])) == NULL)
- return (2);
- if (ft_vecinsert(env, env->size - 1, tmp) == NULL)
- return (2); // internal error code
- return (0);
+ (void)env;
+ if (argv[1] == NULL)
+ return (4);
+ if(ft_isdigit(argv[1][0]))
+ return(0);
+ i = 0;
+ temp = argv[1];
+ while(temp[i] != '\0')
+ {
+ if(temp[i] == ' ' || ft_isalnum(temp[i]) == 0)
+ return(2);
+ if (temp[i] == '=')
+ {
+ temp[i] = '\0';
+ env_export(env, temp, argv[1][i + 1]);
+ return(0);
+ }
+ i++;
+ }
+ return (3);
}
diff --git a/src/builtin/unset.c b/src/builtin/unset.c
index 146b755..b1e2d5b 100644
--- a/src/builtin/unset.c
+++ b/src/builtin/unset.c
@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* unset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
-/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:10:51 by charles #+# #+# */
-/* Updated: 2020/04/02 11:17:03 by charles ### ########.fr */
+/* Updated: 2020/06/17 12:41:45 by nahaddac ### ########.fr */
/* */
/* ************************************************************************** */
@@ -26,7 +26,7 @@ int builtin_unset(char **argv, t_env env)
i = 0;
while (i < env->size)
{
- if (ft_strncmp(env->data[i], argv[1], ft_strlen(argv[1])) == 0)
+ if (env_keycmp(env->data[i],argv[1]) == 0)
{
ft_vecremove(env, i, free);
return (0);
diff --git a/src/env.c b/src/env.c
index 2557e35..9e7aed5 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/06/12 10:51:10 by charles ### ########.fr */
+/* Updated: 2020/06/17 12:50:26 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -43,6 +43,18 @@ t_env env_from_array(char **envp)
return (ft_vecpush(env, NULL));
}
+int env_keycmp(char *var, char *key)
+{
+ size_t key_len;
+
+ key_len = ft_strlen(key);
+ if (ft_strncmp(var, key, key_len) == 0
+ && ft_strlen(var) > key_len
+ && var[key_len] == '=')
+ return (0);
+ return (1);
+}
+
/**
** \brief Search a key in environment
** \param env Search environment
@@ -50,16 +62,15 @@ t_env env_from_array(char **envp)
** \return Value after '=' in environment variable array or NULL if not found
*/
-// could be a wrapper around ft_lfind
char *env_search(t_env env, char *key)
{
size_t i;
i = 0;
- while (i < env->size)
+ while (i < env->size - 1)
{
- if (ft_strncmp((char*)env->data[i], key, ft_strlen(key)) == 0)
- return (ft_strchr((char*)env->data[i], '=') + 1);
+ if (env_keycmp(env->data[i], key))
+ return (ft_strchr(env->data[i], '=') + 1);
i++;
}
return (NULL);
@@ -89,3 +100,32 @@ char *env_search_first_match(t_env env, const char *haystack)
}
return (NULL);
}
+
+char *env_export(t_env env, char *key, char *value)
+{
+ char *joined;
+ size_t i;
+
+ if ((joined = ft_strjoin3(key, "=", value)) == NULL)
+ return (NULL);
+ if (env_search(env, key) == NULL)
+ {
+ if (ft_vecinsert(env, env->size - 1, joined) == NULL)
+ return (NULL);
+ }
+ else
+ {
+ i = 0;
+ while (i < env->size - 1)
+ {
+ if (env_keycmp(env->data[i], key) == 0)
+ {
+ free(env->data[i]);
+ env->data[i] = joined;
+ break;
+ }
+ i++;
+ }
+ }
+ return (joined);
+}
diff --git a/src/eval/cmd.c b/src/eval/cmd.c
index 9468cb2..f502984 100644
--- a/src/eval/cmd.c
+++ b/src/eval/cmd.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/14 10:41:31 by charles #+# #+# */
-/* Updated: 2020/06/15 11:09:38 by charles ### ########.fr */
+/* Updated: 2020/06/17 17:02:07 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,8 +21,7 @@
*/
int fork_wrap(
- int fd_in,
- int fd_out,
+ int fds[2],
void *passed,
int (*wrapped)(void *param))
{
@@ -33,14 +32,16 @@ int fork_wrap(
return (-1);
if (child_pid == 0)
{
- if ((fd_in != MS_NO_FD && dup2(fd_in, STDIN_FILENO) == -1) ||
- (fd_out != MS_NO_FD && dup2(fd_out, STDOUT_FILENO) == -1))
+ if ((fds[FDS_READ] != MS_NO_FD && dup2(fds[FDS_READ], STDIN_FILENO) == -1) ||
+ (fds[FDS_WRITE] != MS_NO_FD && dup2(fds[FDS_WRITE], STDOUT_FILENO) == -1))
exit(EXIT_FAILURE);
if ((status = wrapped(passed)) == -1)
exit(EXIT_FAILURE);
exit(status);
}
wait(&child_pid);
+ close(fds[FDS_WRITE]);
+ // also read end?
return (WEXITSTATUS(child_pid));
}
@@ -56,16 +57,12 @@ int forked_cmd(void *void_param)
return (execve(param->exec_path, param->argv, (char**)param->env->data));
}
-int eval_cmd(t_env env, t_path path, t_ast *ast)
+int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast)
{
t_fork_param_cmd param;
- int fd_in;
- int fd_out;
char **argv;
- fd_in = MS_NO_FD;
- fd_out = MS_NO_FD;
- if (!redir_extract(ast->redirs, env, &fd_in, &fd_out))
+ if (!redir_extract(ast->redirs, env, fds))
{
ast->redirs = NULL;
return (-1);
@@ -92,7 +89,7 @@ int eval_cmd(t_env env, t_path path, t_ast *ast)
param.argv = argv;
param.env = env;
- int ret = fork_wrap(fd_in, fd_out, &param, &forked_cmd);
+ int ret = fork_wrap(fds, &param, &forked_cmd);
ft_split_destroy(argv);
return (ret);
}
diff --git a/src/eval/op.c b/src/eval/op.c
new file mode 100644
index 0000000..e47698b
--- /dev/null
+++ b/src/eval/op.c
@@ -0,0 +1,49 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* op.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/06/17 15:27:22 by charles #+# #+# */
+/* Updated: 2020/06/17 17:03:48 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "eval.h"
+
+// TODO: add parent tag on first operation of parent to fork
+int eval_op(int fds[2], t_env env, t_path path, t_op *op)
+{
+ int status;
+ int left_fds[2];
+ int right_fds[2];
+ int p[2];
+
+ left_fds[FDS_READ] = fds[FDS_READ];
+ left_fds[FDS_WRITE] = MS_NO_FD;
+ right_fds[FDS_READ] = MS_NO_FD;
+ right_fds[FDS_WRITE] = fds[FDS_WRITE];
+ if (op->sep == TAG_PIPE)
+ {
+ pipe(p);
+ left_fds[FDS_WRITE] = p[FDS_WRITE];
+ right_fds[FDS_READ] = p[FDS_READ];
+ }
+ if ((status = eval(left_fds, env, path, op->left)) == -1)
+ return (-1);
+ if ((op->sep == TAG_AND && status != 0) ||
+ (op->sep == TAG_PIPE && status != 0) ||
+ (op->sep == TAG_OR && status == 0))
+ return (status);
+ return (eval(right_fds, env, path, op->right));
+}
+
+int eval(int fds[2], t_env env, t_path path, t_ast *ast)
+{
+ if (ast->tag == AST_OP)
+ return (eval_op(fds, env, path, &ast->op));
+ if (ast->tag == AST_CMD)
+ return (eval_cmd(fds, env, path, ast));
+ return (-1);
+}
diff --git a/src/eval/redir.c b/src/eval/redir.c
index 5a9d074..7249ad5 100644
--- a/src/eval/redir.c
+++ b/src/eval/redir.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/15 11:05:34 by charles #+# #+# */
-/* Updated: 2020/06/15 16:00:40 by charles ### ########.fr */
+/* Updated: 2020/06/17 16:17:43 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -37,8 +37,7 @@ static bool st_open_replace(int *fd, char *filename, int oflag)
bool redir_extract(
t_ftlst *redirs,
t_env env,
- int *fd_in,
- int *fd_out)
+ int fds[2])
{
t_ftlst *after;
t_ftlst *curr;
@@ -66,16 +65,16 @@ bool redir_extract(
return (false);
}
if ((st_lst_tag(redirs) == TAG_REDIR_IN
- && !st_open_replace(fd_in, filename, O_RDONLY))
+ && !st_open_replace(&fds[FDS_READ], filename, O_RDONLY))
|| (st_lst_tag(redirs) == TAG_REDIR_OUT
- && !st_open_replace(fd_out, filename, O_WRONLY | O_CREAT | O_TRUNC))
+ && !st_open_replace(&fds[FDS_WRITE], filename, O_WRONLY | O_CREAT | O_TRUNC))
|| (st_lst_tag(redirs) == TAG_REDIR_APPEND
- && !st_open_replace(fd_out, filename, O_WRONLY | O_CREAT | O_APPEND)))
+ && !st_open_replace(&fds[FDS_WRITE], filename, O_WRONLY | O_CREAT | O_APPEND)))
{
token_destroy_lst2(redirs, after);
return (false);
}
token_destroy_lst(redirs);
free(filename);
- return (redir_extract(after, env, fd_in, fd_out));
+ return (redir_extract(after, env, fds));
}
diff --git a/src/main.c b/src/main.c
index e3391bf..060490a 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/06/18 11:26:02 by charles ### ########.fr */
+/* Updated: 2020/06/18 13:46:10 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,13 +21,46 @@
#include "parser.h"
#include "eval.h"
-void token_debug(void *v)
-{
- t_token *t;
-
- t= v;
- printf("[%4d %d] (%s)\n", t->tag, !!(t->tag & TAG_STICK), t->content);
-}
+/* void token_debug(void *v) */
+/* { */
+/* t_token *t; */
+/* */
+/* t= v; */
+/* printf("[%4d %d] (%s)\n", t->tag, !!(t->tag & TAG_STICK), t->content); */
+/* } */
+/* */
+/* int main(int argc, char **argv, char **envp) */
+/* { */
+/* t_path path; */
+/* t_env env; */
+/* */
+/* env = env_from_array(envp); */
+/* path = path_update(NULL, env_search(env, "PATH")); */
+/* #<{(| printf("%s\n", argv[2]); |)}># */
+/* */
+/* if (argc == 3 && ft_strcmp(argv[1], "-c") == 0) */
+/* { */
+/* //printf("%s\n", argv[2]); */
+/* t_ftlst *lex_out = lexer(ft_strdup(argv[2])); */
+/* */
+/* //ft_lstiter(lex_out, token_debug); */
+/* */
+/* t_ret *parser_out = parse(lex_out); */
+/* */
+/* #<{(| printf("===cmd_argv===\n"); |)}># */
+/* #<{(| ft_lstiter(parser_out->ast->cmd_argv, token_debug); |)}># */
+/* #<{(| printf("===redirs===\n"); |)}># */
+/* #<{(| ft_lstiter(parser_out->ast->redirs, token_debug); |)}># */
+/* */
+/* int fds[2] = {MS_NO_FD, MS_NO_FD}; */
+/* int eval_out = eval_cmd(fds, env, path, parser_out->ast); */
+/* (void)eval_out; */
+/* } */
+/* */
+/* ft_htdestroy(path, free); */
+/* ft_vecdestroy(env, free); */
+/* return (0); */
+/* } */
void token_put(void *v)
{
@@ -78,150 +111,80 @@ void ast_print(int level, t_ast *ast)
int main(int argc, char **argv, char **envp)
{
+ (void)argc;
+ (void)argv;
+ /* (void)envp; */
t_path path;
t_env env;
-
+ /* char *line; */
+ /* int ret; */
env = env_from_array(envp);
path = path_update(NULL, env_search(env, "PATH"));
- /* printf("%s\n", argv[2]); */
-
- if (argc == 3 && ft_strcmp(argv[1], "-c") == 0)
- {
- //printf("%s\n", argv[2]);
- t_ftlst *lex_out = lexer(ft_strdup(argv[2]));
-
- /* ft_lstiter(lex_out, token_debug); */
-
- t_ret *parser_out = parse(lex_out);
- ast_print(0, parser_out->ast);
-
- /* printf("===cmd_argv===\n"); */
- /* ft_lstiter(parser_out->ast->cmd_argv, token_debug); */
- /* printf("===redirs===\n"); */
- /* ft_lstiter(parser_out->ast->redirs, token_debug); */
-
- /* int eval_out = eval_cmd(env, path, parser_out->ast); */
- /* (void)eval_out; */
- }
+ t_ftlst *args1 = NULL;
+ ft_lstpush_back(&args1, ft_lstnew(token_new(TAG_STR, "ls")));
+ ft_lstpush_back(&args1, ft_lstnew(token_new(TAG_STR, "-l")));
+
+ t_ftlst *args2 = NULL;
+ ft_lstpush_back(&args2, ft_lstnew(token_new(TAG_STR, "cat")));
+ ft_lstpush_back(&args2, ft_lstnew(token_new(TAG_STR, "-e")));
+ /* ft_lstpush_back(&args2, ft_lstnew(token_new(TAG_STR, "je"))); */
+
+ /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "ls"))); */
+ /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "-a"))); */
+ /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "-l"))); */
+ /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "$$LFS$TERM$TERM."))); */
+ /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "*.c"))); */
+ /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "src.c include*.h"))); */
+ /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "$A$B"))); */
+ /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "\\$TERM"))); */
+ /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "$TER\\M"))); */
+ /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "\\\\"))); */
+ /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_SINGLE, "''''$TEST\\TEST"))); */
+ /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_DOUBLE, ",$TEST,$B,"))); */
+ /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_DOUBLE | TAG_STICK, "$TEST"))); */
+ /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_DOUBLE | TAG_STICK, "$TEST"))); */
+ /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_DOUBLE , "$TEST"))); */
+ /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_DOUBLE | TAG_STICK, "$TEST"))); */
+ /* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_SINGLE, "$TEST"))); */
+
+ t_ftlst *redirs = NULL;
+ ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_REDIR_OUT, NULL)));
+ ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_STR, "bonjour")));
+ /* ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_REDIR_APPEND, NULL))); */
+ /* ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_STR, "yo"))); */
+ /* ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_REDIR_OUT, NULL))); */
+ /* ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_STR, "yo1"))); */
+
+ t_ast *cmd1 = ast_new(AST_CMD);
+ cmd1->cmd_argv = args1;
+ cmd1->redirs = NULL;
+
+ t_ast *cmd2 = ast_new(AST_CMD);
+ cmd2->cmd_argv = args2;
+ cmd2->redirs = NULL;
+
+ t_ast *op_ast = ast_new(AST_OP);
+ op_ast->op.left = cmd1;
+ op_ast->op.right = cmd2;
+ op_ast->op.sep = TAG_PIPE;
+
+
+ int fds[2] = {MS_NO_FD, MS_NO_FD};
+ printf("eval %d\n", eval(fds, env, path, op_ast));
+
+ /* char **as = preprocess(l, env); */
+ /* printf("%p\n", as); */
+ /* printf("%p\n", *as); */
+ /* char **tmp = as; */
+ /* while (*as != NULL) */
+ /* puts(*as++); */
+ /* ft_split_destroy(tmp); */
ft_htdestroy(path, free);
ft_vecdestroy(env, free);
return (0);
}
-/* int main(int argc, char **argv, char **envp) */
-/* { */
-/* (void)argc; */
-/* (void)argv; */
-/* #<{(| (void)envp; |)}># */
-/* t_path path; */
-/* t_env env; */
-/* #<{(| char *line; |)}># */
-/* #<{(| 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); |)}># */
-/* #<{(| printf("%p\n", ast); |)}># */
-/* #<{(| printf("%d\n", ast->tag); |)}># */
-/* #<{(| printf("%p\n", ast->data.line.left); |)}># */
-/* #<{(| printf("%p\n", ast->data.line.right); |)}># */
-/* #<{(| printf("%d\n", ast->data.line.left->tag); |)}># */
-/* #<{(| printf("%p\n", ast->data.line.left->data.cmd.argv); |)}># */
-/* #<{(| 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)); |)}># */
-/* #<{(| char buf[2048]; |)}># */
-/* #<{(| printf("%s\n", getcwd(buf, 2048)); |)}># */
-/* #<{(| builtin_env(NULL, state.env); |)}># */
-/* #<{(| ast_destroy(ast); |)}># */
-/* #<{(| while ((ret = ft_next_line(STDIN_FILENO, &line)) == 1) |)}># */
-/* #<{(| { |)}># */
-/* #<{(| if (eval(parse(line)) == -1) |)}># */
-/* #<{(| continue ; // and display error |)}># */
-/* #<{(| free(line); |)}># */
-/* #<{(| } |)}># */
-/* #<{(| free(line); |)}># */
-/* #<{(| ms_glob("src#<{(|"); |)}># */
-/* #<{(| char *j = ms_glob("|)}>#*.c"); |)}># */
-/* #<{(| printf("%s\n", j); |)}># */
-/* #<{(| free(j); |)}># */
-/* */
-/* t_ftlst *args = NULL; */
-/* #<{(| ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "echo"))); |)}># */
-/* #<{(| ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "bonjour"))); |)}># */
-/* #<{(| ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "je"))); |)}># */
-/* */
-/* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "cat"))); */
-/* ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "-e"))); */
-/* */
-/* #<{(| ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "ls"))); |)}># */
-/* #<{(| ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "-a"))); |)}># */
-/* #<{(| ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "-l"))); |)}># */
-/* #<{(| ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "$$LFS$TERM$TERM."))); |)}># */
-/* #<{(| ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "|)}>#*.c"))); |)}># */
-/* #<{(| ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "src#<{(|.c include#<{(|.h"))); |)}># */
-/* #<{(| ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "$A$B"))); |)}># */
-/* #<{(| ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "\\$TERM"))); |)}># */
-/* #<{(| ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "$TER\\M"))); |)}># */
-/* #<{(| ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR, "\\\\"))); |)}># */
-/* #<{(| ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_SINGLE, "''''$TEST\\TEST"))); |)}># */
-/* #<{(| ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_DOUBLE, ",$TEST,$B,"))); |)}># */
-/* #<{(| ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_DOUBLE | TAG_STICK, "$TEST"))); |)}># */
-/* #<{(| ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_DOUBLE | TAG_STICK, "$TEST"))); |)}># */
-/* #<{(| ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_DOUBLE , "$TEST"))); |)}># */
-/* #<{(| ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_DOUBLE | TAG_STICK, "$TEST"))); |)}># */
-/* #<{(| ft_lstpush_back(&args, ft_lstnew(token_new(TAG_STR_SINGLE, "$TEST"))); |)}># */
-/* */
-/* t_ftlst *redirs = NULL; */
-/* ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_REDIR_IN, NULL))); */
-/* ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_STR, "bonjour"))); */
-/* ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_REDIR_APPEND, NULL))); */
-/* ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_STR, "yo"))); */
-/* ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_REDIR_OUT, NULL))); */
-/* ft_lstpush_back(&redirs, ft_lstnew(token_new(TAG_STR, "yo1"))); */
-/* */
-/* t_ast *ast = ast_new(AST_CMD); */
-/* ast->cmd_argv = args; */
-/* ast->redirs = redirs; */
-/* */
-/* printf("eval %d\n", eval_cmd(env, path, ast)); */
-/* ast_destroy(ast); */
-/* */
-/* */
-/* #<{(| char **as = preprocess(l, env); |)}># */
-/* #<{(| printf("%p\n", as); |)}># */
-/* #<{(| printf("%p\n", *as); |)}># */
-/* #<{(| char **tmp = as; |)}># */
-/* #<{(| while (*as != NULL) |)}># */
-/* #<{(| puts(*as++); |)}># */
-/* #<{(| ft_split_destroy(tmp); |)}># */
-/* ft_htdestroy(path, free); */
-/* ft_vecdestroy(env, free); */
-/* return (0); */
-/* } */
-
/////////////////////////////////////////////////////////////////////////////////////////
// lexer main
/////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/ms_glob.c b/src/ms_glob.c
index 39c537c..dae2bd8 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/06/12 11:51:41 by charles ### ########.fr */
+/* Updated: 2020/06/17 14:39:52 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -111,23 +111,46 @@ 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)
return (NULL);
+ absolute = *pattern == '/';
+ if (*pattern == '/')
+ pattern++;
if ((param.pattern = ft_strdup(pattern)) == NULL ||
(param.matches = ft_vecnew(MATCHES_VEC_START_SIZE)) == NULL)
{
free(param.pattern);
return (NULL);
}
- if (utils_directory_iter(dirname, &param,
+ 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);
}
diff --git a/src/utils.c b/src/utils.c
index 3449c25..2eaac3e 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/06/09 15:49:39 by charles ### ########.fr */
+/* Updated: 2020/06/18 13:46:26 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -26,7 +26,7 @@ int utils_directory_iter(
DIR *dir;
struct dirent *entry;
- if ((dir = opendir(dirname)) == NULL)
+ if ((dir = opendir(dirname)) == NULL) // add fail safe
return (-1);
while ((entry = readdir(dir)) != NULL)
if (f(dirname, entry, param) == -1)