aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/eval.h4
m---------minishell_test0
-rw-r--r--src/builtin/export.c13
-rw-r--r--src/eval/cmd.c64
-rw-r--r--src/eval/eval.c82
-rw-r--r--src/eval/local.c68
-rw-r--r--src/eval/op.c7
-rw-r--r--src/eval/redir.c33
-rw-r--r--src/main.c10
-rw-r--r--src/preprocess.c72
10 files changed, 140 insertions, 213 deletions
diff --git a/include/eval.h b/include/eval.h
index 761c152..9b5d00a 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/08/27 20:31:35 by charles ### ########.fr */
+/* Updated: 2020/08/28 16:56:37 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -64,7 +64,7 @@ t_ftlst *split_token(t_ftlst **lst, enum e_tok);
** redir.c
*/
-bool redir_extract(t_tok_lst *redirs, t_env env, int fds[2]);
+bool redir_extract(t_tok_lst **redirs, t_env env, int fds[2]);
/*
** exec.c
diff --git a/minishell_test b/minishell_test
-Subproject cbc2c50efaf06f5b925bb87c445994e9db6e171
+Subproject fb3df181b71d3bed23f43ea4a6fcd8111d4c869
diff --git a/src/builtin/export.c b/src/builtin/export.c
index 6fbb88f..e19756d 100644
--- a/src/builtin/export.c
+++ b/src/builtin/export.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:11:34 by charles #+# #+# */
-/* Updated: 2020/07/19 18:46:48 by charles ### ########.fr */
+/* Updated: 2020/08/28 17:49:47 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -40,7 +40,6 @@ int builtin_export(char **argv, t_env env)
int status;
size_t i;
char *equal_ptr;
- bool skip;
if (argv[1] == NULL)
{
@@ -51,20 +50,18 @@ int builtin_export(char **argv, t_env env)
i = 0;
while (argv[++i] != NULL)
{
- skip = (equal_ptr = ft_strchr(argv[i], '=')) == NULL;
- if (!skip)
+ equal_ptr = ft_strchr(argv[i], '=');
+ if (equal_ptr != NULL)
*equal_ptr = '\0';
if (!utils_valid_identifier(argv[i]))
{
- if (!skip)
+ if (equal_ptr != NULL)
*equal_ptr = '=';
errorf("export: `%s': not a valid identifier\n", argv[i]);
status = 1;
continue;
}
- if (skip)
- continue;
- if (env_export(env, argv[i], equal_ptr + 1) == NULL)
+ if (env_export(env, argv[i], equal_ptr == NULL ? "" : equal_ptr + 1) == NULL)
return (127); // malloc error
}
return (status);
diff --git a/src/eval/cmd.c b/src/eval/cmd.c
index b76666c..ccb4cc2 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/08/27 17:19:14 by charles ### ########.fr */
+/* Updated: 2020/08/28 16:54:48 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -92,72 +92,12 @@ 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; */
- if (!redir_extract(ast->redirs, env, fds))
- {
- ast->redirs = NULL;
+ if (!redir_extract(&ast->redirs, env, fds))
return (-1);
- }
- ast->redirs = NULL;
if ((param.env_local = env_from_array((char*[]){NULL})) == NULL)
return (-1);
- /* 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; */
- /* 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, strs[0]) == NULL) */
- /* return (-1); */
- /* } */
- /* if (ast->cmd_argv == NULL) // FIXME special env not passed to child processes */
- /* { */
- /* ft_vecpop(param.env_local, NULL); */
- /* if (ft_vecswallow_at(env, env->size - 1, param.env_local) == NULL) */
- /* { */
- /* ft_vecdestroy(param.env_local, free); */
- /* return (-1); */
- /* } */
- /* g_last_status_code = 0; */
- /* return (0); */
- /* } */
-
if ((argv = preprocess(&ast->cmd_argv, env)) == NULL)
{
ast->cmd_argv = NULL;
diff --git a/src/eval/eval.c b/src/eval/eval.c
deleted file mode 100644
index a831237..0000000
--- a/src/eval/eval.c
+++ /dev/null
@@ -1,82 +0,0 @@
-/* ************************************************************************** */
-/* */
-/* ::: :::::::: */
-/* eval.c :+: :+: :+: */
-/* +:+ +:+ +:+ */
-/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
-/* Created: 2020/04/01 17:05:21 by charles #+# #+# */
-/* Updated: 2020/06/18 13:16:34 by nahaddac ### ########.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/local.c b/src/eval/local.c
new file mode 100644
index 0000000..13719cd
--- /dev/null
+++ b/src/eval/local.c
@@ -0,0 +1,68 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* local.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/08/28 16:52:54 by charles #+# #+# */
+/* Updated: 2020/08/28 16:53:04 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "eval.h"
+
+ /* 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; */
+ /* 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, strs[0]) == NULL) */
+ /* return (-1); */
+ /* } */
+ /* if (ast->cmd_argv == NULL) // FIXME special env not passed to child processes */
+ /* { */
+ /* ft_vecpop(param.env_local, NULL); */
+ /* if (ft_vecswallow_at(env, env->size - 1, param.env_local) == NULL) */
+ /* { */
+ /* ft_vecdestroy(param.env_local, free); */
+ /* return (-1); */
+ /* } */
+ /* g_last_status_code = 0; */
+ /* return (0); */
+ /* } */
+
diff --git a/src/eval/op.c b/src/eval/op.c
index 1f85bc1..36340be 100644
--- a/src/eval/op.c
+++ b/src/eval/op.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/17 15:27:22 by charles #+# #+# */
-/* Updated: 2020/06/19 17:32:43 by charles ### ########.fr */
+/* Updated: 2020/08/28 16:54:39 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -50,11 +50,8 @@ int eval_parent(int fds[2], t_env env, t_path path, t_ast *ast)
{
t_fork_param_parent param;
- if (!redir_extract(ast->redirs, env, fds))
- {
- ast->redirs = NULL;
+ if (!redir_extract(&ast->redirs, env, fds))
return (-1);
- }
param.fds[0] = fds[0];
param.fds[1] = fds[1];
param.env = env;
diff --git a/src/eval/redir.c b/src/eval/redir.c
index 8ab214d..e8796a5 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/08/28 10:22:02 by charles ### ########.fr */
+/* Updated: 2020/08/28 17:14:19 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,6 +22,7 @@ static bool st_open_replace(int *fd, char *filename, int oflag)
*fd = open(filename, oflag);
if (*fd == -1)
{
+ g_last_status_code = 1;
errorf("%s: %s\n", filename, strerror(errno));
free(filename);
return (false);
@@ -30,7 +31,7 @@ static bool st_open_replace(int *fd, char *filename, int oflag)
}
bool redir_extract(
- t_tok_lst *redirs,
+ t_tok_lst **redirs,
t_env env,
int fds[2])
{
@@ -38,12 +39,12 @@ bool redir_extract(
t_tok_lst *curr;
char *filename;
- if (redirs == NULL)
+ if (*redirs == NULL)
return (true);
- if (!(redirs->tag & TAG_IS_REDIR) || redirs->next == NULL
- || !(redirs->next->tag & TAG_IS_STR))
+ if (!((*redirs)->tag & TAG_IS_REDIR) || (*redirs)->next == NULL
+ || !((*redirs)->next->tag & TAG_IS_STR))
return (false);
- curr = redirs->next;
+ curr = (*redirs)->next;
after = NULL;
while (curr != NULL && curr->tag & TAG_IS_STR)
{
@@ -54,24 +55,24 @@ bool redir_extract(
}
curr = curr->next;
}
- if ((filename = preprocess_filename(&redirs->next, env)) == NULL)
+ if ((filename = preprocess_filename(&(*redirs)->next, env)) == NULL)
{
- /* ft_lstdestroy((t_ftlst**)&redirs, free); */
- /* ft_lstdestroy((t_ftlst**)&after, free); */
+ tok_lst_destroy(redirs, free);
+ tok_lst_destroy(&after, free);
return (false);
}
- if ((redirs->tag == TAG_REDIR_IN
+ if (((*redirs)->tag == TAG_REDIR_IN
&& !st_open_replace(&fds[FDS_READ], filename, O_RDONLY))
- || (redirs->tag == TAG_REDIR_OUT
+ || ((*redirs)->tag == TAG_REDIR_OUT
&& !st_open_replace(&fds[FDS_WRITE], filename, O_WRONLY | O_CREAT | O_TRUNC))
- || (redirs->tag == TAG_REDIR_APPEND
+ || ((*redirs)->tag == TAG_REDIR_APPEND
&& !st_open_replace(&fds[FDS_WRITE], filename, O_WRONLY | O_CREAT | O_APPEND)))
{
- /* ft_lstdestroy((t_ftlst**)&redirs, free); */
- /* ft_lstdestroy((t_ftlst**)&after, free); */
+ tok_lst_destroy(redirs, free);
+ tok_lst_destroy(&after, free);
return (false);
}
- /* ft_lstdestroy((t_ftlst**)&redirs, free); */
+ tok_lst_destroy(redirs, free);
free(filename);
- return (redir_extract(after, env, fds));
+ return (redir_extract(&after, env, fds));
}
diff --git a/src/main.c b/src/main.c
index e7326de..b3ef218 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/08/28 10:45:33 by charles ### ########.fr */
+/* Updated: 2020/08/28 17:16:18 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -118,12 +118,12 @@ int main(int argc, char **argv, char **envp)
int ret;
char *line;
- print_prompt();
+ ft_putstr_fd("> ", STDERR_FILENO);
while ((ret = ft_getline(STDOUT_FILENO, &line)) == FTGL_OK)
{
if (*line == '\0')
{
- print_prompt();
+ ft_putstr_fd("> ", STDERR_FILENO);
continue;
}
t_tok_lst *lex_out = lexer(line);
@@ -135,14 +135,14 @@ int main(int argc, char **argv, char **envp)
return (1);
if (parser_out->syntax_error)
{
- print_prompt();
+ ft_putstr_fd("> ", STDERR_FILENO);
continue;
}
int fds[2] = {MS_NO_FD, MS_NO_FD};
int eval_out = eval(fds, env, path, parser_out->ast);
(void)eval_out;
- print_prompt();
+ ft_putstr_fd("> ", STDERR_FILENO);
}
if (ret != FTGL_EOF)
return (1);
diff --git a/src/preprocess.c b/src/preprocess.c
index f4eb104..f58cd37 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/08/27 20:43:22 by charles ### ########.fr */
+/* Updated: 2020/08/28 17:42:02 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -67,7 +67,8 @@ char **st_tokens_to_argv(t_tok_lst *tokens)
char **ret;
size_t i;
- if ((ret = ft_calloc(ft_lstsize((t_ftlst*)tokens) + 1, sizeof(char*))) == NULL)
+ ret = ft_calloc(ft_lstsize((t_ftlst*)tokens) + 1, sizeof(char*));
+ if (ret == NULL)
return (NULL);
i = 0;
while (tokens != NULL)
@@ -75,7 +76,7 @@ char **st_tokens_to_argv(t_tok_lst *tokens)
ret[i++] = tokens->content;
tokens = tokens->next;
}
- tok_lst_destroy(&tokens, free);
+ tok_lst_destroy(&tokens, NULL);
return (ret);
}
@@ -128,36 +129,41 @@ char **preprocess(t_tok_lst **tokens, t_env env)
if (curr->tag & TAG_STR)
{
t_tok_lst *fields = st_field_split(match);
-
- len = ft_strlen(tok_lst_last(fields)->content);
-
- if (!(prev_tag & TAG_STICK) && *before == '\0' && *fields->content == '\0')
- ft_lstpop_front((t_ftlst**)&fields, free);
- if (!(curr->tag & TAG_STICK) && *after == '\0'
- && *tok_lst_last(fields)->content == '\0')
- ft_lstpop_back((t_ftlst**)&fields, free);
-
- if (fields == NULL)
- // delete curr?
- ;
- else if (fields->next == NULL)
+ if (fields != NULL)
{
- curr->content = ft_strjoin3(before, fields->content, after);
- i += len - 1;
- }
- else
- {
- curr->content = ft_strjoin(before, fields->content);
- tok_lst_last(fields)->content =
- ft_strjoin(tok_lst_last(fields)->content, after);
-
- t_tok_lst *tmp = curr->next;
- curr->next = fields->next;
- curr = tok_lst_last(fields);
- curr->next = tmp;
- i = len - 1;
- str = curr->content;
+ len = ft_strlen(tok_lst_last(fields)->content);
+
+ if (!(prev_tag & TAG_STICK) && *before == '\0' && *fields->content == '\0')
+ ft_lstpop_front((t_ftlst**)&fields, free);
+ if (!(curr->tag & TAG_STICK) && *after == '\0'
+ && *tok_lst_last(fields)->content == '\0')
+ ft_lstpop_back((t_ftlst**)&fields, free);
+
+ if (fields == NULL)
+ // delete curr?
+ ;
+ else if (fields->next == NULL)
+ {
+ curr->content = ft_strjoin3(before, fields->content, after);
+ i += len - 1;
+ }
+ else
+ {
+ curr->content = ft_strjoin(before, fields->content);
+ tok_lst_last(fields)->content =
+ ft_strjoin(tok_lst_last(fields)->content, after);
+
+ t_tok_lst *tmp = curr->next;
+ curr->next = fields->next;
+ curr = tok_lst_last(fields);
+ curr->next = tmp;
+ i = len - 1;
+ str = curr->content;
+ }
}
+ /* else */
+ /* printf("yo\n"); */
+ /* i+=10; */
}
else if (curr->tag & TAG_STR_DOUBLE)
{
@@ -175,7 +181,7 @@ char **preprocess(t_tok_lst **tokens, t_env env)
return (st_tokens_to_argv(*tokens));
}
-// need to free tokens
+// TODO malloc error vs shell error
char *preprocess_filename(t_tok_lst **tokens, t_env env)
{
char **strs;
@@ -196,5 +202,5 @@ char *preprocess_filename(t_tok_lst **tokens, t_env env)
}
ret = strs[0];
free(strs);
- return (ret);
+ return (ft_strdup(ret));
}