aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ast.c14
-rw-r--r--src/eval/cmd.c21
-rw-r--r--src/eval/eval.c82
-rw-r--r--src/eval/op.c49
-rw-r--r--src/eval/redir.c13
-rw-r--r--src/lexer/lexer.c6
-rw-r--r--src/lexer/lexer_utils.c11
-rw-r--r--src/main.c275
-rw-r--r--src/ms_glob.c27
-rwxr-xr-xsrc/parse/cmd_parse.c3
-rwxr-xr-xsrc/parse/parse.c143
-rwxr-xr-xsrc/parse/redir_parse.c1
-rw-r--r--src/utils.c2
13 files changed, 355 insertions, 292 deletions
diff --git a/src/ast.c b/src/ast.c
index 1a5f7eb..c878062 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
-/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:05:42 by charles #+# #+# */
-/* Updated: 2020/06/15 13:01:20 by charles ### ########.fr */
+/* Updated: 2020/06/18 13:39:30 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -31,8 +31,8 @@ t_ast *ast_new(enum e_ast_tag tag)
return (NULL);
ast->tag = tag;
ast->redirs = NULL;
- ast->line.left = NULL;
- ast->line.right = NULL;
+ ast->op.left = NULL;
+ ast->op.right = NULL;
ast->cmd_argv = NULL;
return (ast);
}
@@ -52,10 +52,10 @@ void ast_destroy(t_ast *ast)
ft_lstdestroy(&ast->cmd_argv, (void (*)(void*))token_destroy);
ft_lstdestroy(&ast->redirs, (void (*)(void*))token_destroy);
}
- else if (ast->tag == AST_LINE)
+ else if (ast->tag == AST_OP)
{
- ast_destroy(ast->line.left);
- ast_destroy(ast->line.right);
+ ast_destroy(ast->op.left);
+ ast_destroy(ast->op.right);
}
free(ast);
}
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/eval.c b/src/eval/eval.c
deleted file mode 100644
index c1b580f..0000000
--- a/src/eval/eval.c
+++ /dev/null
@@ -1,82 +0,0 @@
-/* ************************************************************************** */
-/* */
-/* ::: :::::::: */
-/* eval.c :+: :+: :+: */
-/* +:+ +:+ +:+ */
-/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
-/* Created: 2020/04/01 17:05:21 by charles #+# #+# */
-/* Updated: 2020/06/14 10:42:37 by charles ### ########.fr */
-/* */
-/* ************************************************************************** */
-
-/*
-** \file eval.c
-** \brief Evaluation of an AST
-*/
-
-/* #include "eval.h" */
-
-/* #<{(| */
-/* ** \brief Evaluate a line */
-/* ** \param state State of the evaluation */
-/* ** \param line Line to evaluate */
-/* ** \return Last Executed command status or -1 on error */
-/* |)}># */
-/* */
-/* static int eval_line(void *param) */
-/* { */
-/* int status; */
-/* t_eval_state *state; */
-/* t_line *line; */
-/* int fd_in; */
-/* int fd_out; */
-/* */
-/* state = ((t_fork_param_line*)param)->state; */
-/* line = ((t_fork_param_line*)param)->line; */
-/* fd_in = ((t_fork_param_line*)param)->fd_in; */
-/* fd_out = ((t_fork_param_line*)param)->fd_out; */
-/* */
-/* #<{(| if (line->right == NULL) |)}># */
-/* #<{(| return (eval(state, line->left)); |)}># */
-/* */
-/* #<{(| if (line->sep == SEP_PIPE) |)}># */
-/* #<{(| pipe(state->p); |)}># */
-/* */
-/* if (line->left->tag == AST_LINE) */
-/* { */
-/* return (fork_wrap(fd_in, fd_out, param, &eval_line)); */
-/* } */
-/* if ((status = eval(fd_in, fd_out, state, line->left)) == -1) */
-/* return (-1); */
-/* if ((line->sep == SEP_AND && status != 0) || */
-/* (line->sep == SEP_OR && status == 0)) */
-/* return (status); */
-/* */
-/* return (eval(fd_in, fd_out, state, line->right)); */
-/* } */
-/* */
-/* #<{(| */
-/* ** \brief Evaluate an AST */
-/* ** \param state State of the evaluation */
-/* ** \param ast Abstract syntax tree to evaluate */
-/* ** \return Last command status or -1 on error */
-/* |)}># */
-/* */
-/* int eval(int fd_in, int fd_out, t_eval_state *state, t_ast *ast) */
-/* { */
-/* t_fork_param_line param; */
-/* */
-/* errno = 0; */
-/* if (ast->tag == TAG_LINE) */
-/* { */
-/* param.state = state; */
-/* param.line = &ast->line; */
-/* param.fd_in = fd_in; */
-/* param.fd_out = fd_out; */
-/* return (eval_line(&param)); */
-/* } */
-/* if (ast->tag == TAG_CMD) */
-/* return (eval_cmd(fd_in, fd_out, state, &ast->cmd)); */
-/* return (-1); */
-/* } */
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/lexer/lexer.c b/src/lexer/lexer.c
index 2b1bdce..9b43616 100644
--- a/src/lexer/lexer.c
+++ b/src/lexer/lexer.c
@@ -9,9 +9,9 @@ int len_is_not_sep(char *input)
{
if (lexer_sep(input[i]))
{
- if (input[i + 1] == ' ')
- while(input[++i] == ' ')
- ;
+ //if (input[i + 1] == ' ')
+ // while(input[++i] == ' ')
+ // ;
return(i);
}
if (input[i] == '\'' || input[i] == '"')
diff --git a/src/lexer/lexer_utils.c b/src/lexer/lexer_utils.c
index 616c0d3..3ee41ff 100644
--- a/src/lexer/lexer_utils.c
+++ b/src/lexer/lexer_utils.c
@@ -1,5 +1,6 @@
#include "lexer.h"
+// check for append tag
enum e_token_tag ret_token_sep_redir_append(char *input, int i)
{
if (input[i + 1] == '>')
@@ -8,13 +9,14 @@ enum e_token_tag ret_token_sep_redir_append(char *input, int i)
}
+// return token tag corresponding to string id
enum e_token_tag ret_token(char *input, int i)
{
if (input[i] == ';')
- return(TAG_AND);
- if (input[i] == '&')
return(TAG_END);
- if (input[i] == '|' && input[i + 1] == '|')
+ if (input[i] == '&')
+ return(TAG_AND);
+ if (input[i] == '|' && input[i + 1] == '|')
return(TAG_OR);
if(input[i] == '|')
return(TAG_PIPE);
@@ -30,6 +32,8 @@ enum e_token_tag ret_token(char *input, int i)
}
+// check is char is separator
+// ft_strchr(";&|><()", input)
int lexer_sep(char input)
{
char *sep;
@@ -46,6 +50,7 @@ int lexer_sep(char input)
return (0);
}
+// skip spaces
int lexe_space(char *input)
{
int i;
diff --git a/src/main.c b/src/main.c
index dfceb09..060490a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
-/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* By: cacharle <cacharle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */
-/* Updated: 2020/06/17 12:46:04 by charles ### ########.fr */
+/* Updated: 2020/06/18 13:46:10 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,159 +21,170 @@
#include "parser.h"
#include "eval.h"
-void token_debug(void *v)
+/* 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)
{
t_token *t;
t= v;
- printf("[%4d %d] (%s)\n", t->tag, !!(t->tag & TAG_STICK), t->content);
+ printf("%s ", t->content);
+}
+
+void print_level(int level)
+{
+ while (level-- > 0)
+ printf(" ");
+}
+
+void ast_print(int level, t_ast *ast)
+{
+ if (ast->tag == AST_CMD)
+ {
+ print_level(level);
+ printf("cmd: [ ");
+ ft_lstiter(ast->cmd_argv, token_put);
+ printf(" ] redirs: [");
+ ft_lstiter(ast->redirs, token_put);
+ printf(" ]");
+ }
+ else
+ {
+ /* print_level(level); */
+ /* printf("SEP: %d\n", ast->op.sep); */
+ print_level(level);
+ printf("{\n");
+
+ print_level(level);
+ printf(" left:\n");
+ ast_print(level + 1, ast->op.left);
+
+ printf("\n");
+ print_level(level);
+ printf(" right:\n");
+ ast_print(level + 1, ast->op.right);
+
+ printf("\n");
+ print_level(level);
+ printf("}\n");
+ }
}
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]));
+ 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")));
- /* ft_lstiter(lex_out, token_debug); */
+ 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"))); */
- t_ret *parser_out = parse(lex_out);
+ /* 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"))); */
- /* printf("===cmd_argv===\n"); */
- /* ft_lstiter(parser_out->ast->cmd_argv, token_debug); */
- /* printf("===redirs===\n"); */
- /* ft_lstiter(parser_out->ast->redirs, token_debug); */
+ 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"))); */
- int eval_out = eval_cmd(env, path, parser_out->ast);
- (void)eval_out;
- }
+ 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/parse/cmd_parse.c b/src/parse/cmd_parse.c
index c655c5c..25802c1 100755
--- a/src/parse/cmd_parse.c
+++ b/src/parse/cmd_parse.c
@@ -2,7 +2,6 @@
#include "parser.h"
-
int parse_cmd_str_true_false(enum e_token_tag tag)
{
if (tag & TAG_STR || tag & TAG_STR_DOUBLE || tag & TAG_STR_SINGLE)
@@ -10,12 +9,10 @@ int parse_cmd_str_true_false(enum e_token_tag tag)
return(0);
}
-
t_ast *push_cmd(t_ast *ast, t_ftlst *rest)
{
t_ftlst *new;
- /* new = rest->data; */
if (ast == NULL)
{
ast = ast_new(AST_CMD);
diff --git a/src/parse/parse.c b/src/parse/parse.c
index 37192b0..198ac65 100755
--- a/src/parse/parse.c
+++ b/src/parse/parse.c
@@ -1,3 +1,15 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* parse.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/06/17 18:09:04 by nahaddac #+# #+# */
+/* Updated: 2020/06/18 12:48:20 by nahaddac ### ########.fr */
+/* */
+/* ************************************************************************** */
+
/*
** \file parse.c
** \brief Parser
@@ -5,61 +17,114 @@
#include "parser.h"
#include "lexer.h"
-// stdio.h est deja include dans minishell.h temporairement
-// (comme ca on doit le retirer a un seul endroit a la fin)
+t_ftlst *push_token(t_ftlst **tokens, t_token *pushed)
+{
+ t_ftlst *tmp;
+
+ if ((tmp = ft_lstnew(pushed)) == NULL)
+ return (NULL);
+ ft_lstpush_back(tokens, tmp);
+ return (tmp);
+}
-t_ret *parse(t_ftlst *input)
+t_ret *ret_wrap_ast(t_ast *ast, t_ftlst *rest)
{
- t_ret *ret;
- t_ret *first;
- enum e_token_tag tag;
-
- if(!(ret = malloc(sizeof(t_ret) * 1)))
- return(NULL);
- ret->rest = input;
- ret->ast = NULL;
+ t_ret *ret;
+
+ if ((ret = malloc(sizeof(t_ret))) == NULL)
+ return (NULL);
ret->unexpected = NULL;
- first = ret;
+ ret->rest = rest;
+ ret->ast = ast;
+ return ret;
+}
- while (ret->rest != NULL)
+t_ret *parse_cmd(t_ftlst *input)
+{
+ enum e_token_tag tag;
+ t_ast *ast;
+
+ ast = ast_new(AST_CMD);
+ while (input != NULL)
{
- tag = ((t_token *)ret->rest->data)->tag;
- if (parse_cmd_str_true_false(tag))
+ tag = ((t_token *)input->data)->tag;
+ if (tag & TAG_IS_STR)
{
- ret->ast = push_cmd(ret->ast, ret->rest);
+ push_token(&ast->cmd_argv, input->data);
}
- else if (parse_redir_true_false(tag))
+ else if (tag & TAG_IS_REDIR)
{
- while(ret->rest != NULL)
+ while(input != NULL)
{
- ret->ast = push_redir(ret->ast, ret->rest);
+ push_token(&ast->redirs, input->data);
if (tag & TAG_IS_STR && tag & TAG_STICK)
- ret->rest = ret->rest->next;
+ input = input->next;
else if (tag & TAG_IS_REDIR)
- ret->rest = ret->rest->next;
+ input = input->next;
else
- {
- //ret->rest = ret->rest->next;
break;
- }
- tag = ((t_token *)ret->rest->data)->tag;
+ tag = ((t_token *)input->data)->tag;
}
}
- ret->rest = ret->rest->next;
+ else
+ return ret_wrap_ast(ast, input);
+ input = input->next;
+ }
+ return ret_wrap_ast(ast, input);
+}
+
+// <cmd> ::= (<string> | <redir>)+
+// <op> ::= <expr> <sep> <op> | <expr>
+// <expr> ::= '(' <op> ')' | <cmd>
+
+t_ret *parse_op(t_ftlst *input)
+{
+ t_ast *ast;
+ t_ret *left_ret;
+ t_ret *right_ret;
+ enum e_token_tag sep_tag;
+
+ left_ret = parse_expr(input);
+ input = left_ret->rest;
+
+ if (input == NULL || ((t_token*)input->data)->tag == TAG_PARENT_CLOSE)
+ return ret_wrap_ast(left_ret->ast, input);
+
+ sep_tag = ((t_token*)input->data)->tag;
+ input = input->next;
+
+ right_ret = parse_op(input);
+
+ ast = ast_new(AST_OP);
+ ast->op.left = left_ret->ast;
+ ast->op.right = right_ret->ast;
+ ast->op.sep = sep_tag;
+ return ret_wrap_ast(ast, right_ret->rest);
+}
+
+t_ret *parse_expr(t_ftlst *input)
+{
+ t_ret *tmp;
+ enum e_token_tag tag;
+
+ tag = ((t_token*)input->data)->tag;
+ if (tag == TAG_PARENT_OPEN)
+ {
+ tmp = parse_op(input->next);
+ input = tmp->rest;
+ tag = ((t_token*)input->data)->tag;
+ if (tag != TAG_PARENT_CLOSE)
+ return (NULL);
+ input = input->next;
+ tmp->rest = input;
+ return tmp;