aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-09-12 17:12:41 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-09-12 17:12:41 +0200
commit65006d0c14d3efa647b3c866ab54bdb1749fa31d (patch)
tree716de58438e96bde6a5553a8dd2a44620f529617
parent9446bb78646bef4e3449a59a9e01118b025402bf (diff)
downloadminishell-65006d0c14d3efa647b3c866ab54bdb1749fa31d.tar.gz
minishell-65006d0c14d3efa647b3c866ab54bdb1749fa31d.tar.bz2
minishell-65006d0c14d3efa647b3c866ab54bdb1749fa31d.zip
Added concurrent pipeline (not working with minishell_test for some obscure reason)
-rw-r--r--include/eval.h33
m---------minishell_test0
-rw-r--r--src/builtin/cd.c8
-rw-r--r--src/builtin/echo.c2
-rw-r--r--src/builtin/export.c16
-rw-r--r--src/eval/cmd.c37
-rw-r--r--src/eval/op.c38
-rw-r--r--src/main.c5
-rw-r--r--src/utils.c2
9 files changed, 92 insertions, 49 deletions
diff --git a/include/eval.h b/include/eval.h
index b799c26..89f05ef 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/09/11 19:10:30 by charles ### ########.fr */
+/* Updated: 2020/09/12 12:28:36 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -24,11 +24,11 @@
typedef struct
{
- t_path path;
- t_env env;
- t_ast *ast;
- int fds[2];
-} t_fork_param_parent;
+ t_path path;
+ t_env env;
+ t_ast *ast;
+ int fds[2];
+} t_fork_param_args;
typedef struct
{
@@ -42,35 +42,36 @@ typedef struct
# define FD_WRITE 1
# define FD_READ 0
-extern pid_t g_child_pid;
+extern pid_t g_child_pid;
/*
** op.c
*/
-int eval_op(int fds[2], t_env env, t_path path, t_ast *ast);
-int eval(int fds[2], t_env env, t_path path, t_ast *ast);
+int eval_op(int fds[2], t_env env, t_path path, t_ast *ast);
+int eval(int fds[2], t_env env, t_path path, t_ast *ast);
+int eval_forked(int fds[2], t_env env, t_path path, t_ast *ast, pid_t *child_pid);
/*
** cmd.c
*/
-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);
-t_ftlst *split_token(t_ftlst **lst, enum e_tok);
+int fork_wrap(int fds[2], void *passed, int (*wrapped)(void *param), pid_t *child_pid);
+int fork_wrap_wait(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
*/
-int redir_extract(t_tok_lst **redirs, t_env env, int fds[2]);
+int redir_extract(t_tok_lst **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);
+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/minishell_test b/minishell_test
-Subproject 7e8af919fad194f69e52a7b7529cebba635d422
+Subproject 6026876cdfd8ffb1bde3c98e97bb7ba05ff4e4a
diff --git a/src/builtin/cd.c b/src/builtin/cd.c
index 5f7cdd8..de1eeb9 100644
--- a/src/builtin/cd.c
+++ b/src/builtin/cd.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:10:20 by charles #+# #+# */
-/* Updated: 2020/09/10 19:40:31 by charles ### ########.fr */
+/* Updated: 2020/09/12 11:09:49 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,7 +19,7 @@
int builtin_cd(char **argv, t_env env)
{
- char buf[PATH_MAX];
+ char buf[PATH_MAX + 1];
char *home;
(void)env;
@@ -36,9 +36,9 @@ int builtin_cd(char **argv, t_env env)
errno = 0;
if (chdir(argv[1]) == -1)
return (errorf_ret(1, "cd: %s: %s\n", argv[1], strerror(errno)));
- if (!(getcwd(buf, PATH_MAX)))
+ if (getcwd(buf, PATH_MAX) == NULL)
return (1);
if (env_export(env, "PWD", buf) == NULL)
- return (2); // FIXME malloc error recognition in builtins and cmd
+ return (EVAL_FATAL);
return (0);
}
diff --git a/src/builtin/echo.c b/src/builtin/echo.c
index 75a350c..9ad427a 100644
--- a/src/builtin/echo.c
+++ b/src/builtin/echo.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:10:47 by charles #+# #+# */
-/* Updated: 2020/06/15 14:26:30 by charles ### ########.fr */
+/* Updated: 2020/09/12 15:24:34 by charles ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/src/builtin/export.c b/src/builtin/export.c
index e19756d..d394af6 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/08/28 17:49:47 by charles ### ########.fr */
+/* Updated: 2020/09/12 11:06:47 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,16 +21,20 @@ static void st_put_declare_x(char *s)
{
char *equal_ptr;
- // could use errorf on stdout
if (s == NULL)
return ;
- ft_putstr("declare -x ");
+ ft_putstr("export ");
if ((equal_ptr = ft_strchr(s, '=')) == NULL)
equal_ptr = ft_strchr(s, '\0');
write(STDOUT_FILENO, s, equal_ptr - s);
ft_putchar('=');
ft_putchar('"');
- ft_putstr(equal_ptr + 1);
+ while (*++equal_ptr != '\0')
+ {
+ if (*equal_ptr == '"')
+ ft_putchar('\\');
+ ft_putchar(*equal_ptr);
+ }
ft_putchar('"');
ft_putchar('\n');
}
@@ -38,7 +42,7 @@ static void st_put_declare_x(char *s)
int builtin_export(char **argv, t_env env)
{
int status;
- size_t i;
+ size_t i;
char *equal_ptr;
if (argv[1] == NULL)
@@ -62,7 +66,7 @@ int builtin_export(char **argv, t_env env)
continue;
}
if (env_export(env, argv[i], equal_ptr == NULL ? "" : equal_ptr + 1) == NULL)
- return (127); // malloc error
+ return (EVAL_FATAL);
}
return (status);
}
diff --git a/src/eval/cmd.c b/src/eval/cmd.c
index 44f22b6..9745632 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/09/11 19:10:20 by charles ### ########.fr */
+/* Updated: 2020/09/12 17:04:23 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,6 +14,7 @@
pid_t g_child_pid = -1;
int g_last_status = 0;
+
void token_debug(void *v);
/*
@@ -25,16 +26,22 @@ void token_debug(void *v);
*/
int fork_wrap(
- int fds[2],
- void *passed,
- int (*wrapped)(void *param))
+ int fds[2],
+ void *passed,
+ int (*wrapped)(void *param),
+ pid_t *child_pid
+)
{
int status;
- pid_t child_pid;
+ bool waiting;
+ pid_t pid;
- if ((child_pid = fork()) == -1)
+ waiting = child_pid == NULL;
+ if (waiting)
+ child_pid = &pid;
+ if ((*child_pid = fork()) == -1)
return (EVAL_FATAL);
- if (child_pid == 0)
+ if (*child_pid == 0)
{
if ((fds[FD_READ] != FD_NONE && dup2(fds[FD_READ], STDIN_FILENO) == -1) ||
(fds[FD_WRITE] != FD_NONE && dup2(fds[FD_WRITE], STDOUT_FILENO) == -1))
@@ -43,10 +50,15 @@ int fork_wrap(
exit(EXIT_FAILURE);
exit(status);
}
- g_child_pid = child_pid;
- wait(&child_pid);
- close(fds[FD_WRITE]);
- return (WEXITSTATUS(child_pid));
+ g_child_pid = *child_pid;
+ if (waiting)
+ {
+ waitpid(*child_pid, child_pid, 0);
+ close(fds[FD_WRITE]);
+ /* close(fds[FD_READ]); */
+ return (WEXITSTATUS(*child_pid));
+ }
+ return (0);
}
int forked_cmd(void *void_param)
@@ -104,7 +116,6 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast)
param.builtin = builtin_search_func(argv[0]);
if (param.builtin == NULL)
{
- // check env local for PATH
param.exec_path = exec_search_path(path, env_search(env, "PATH"), argv[0]);
if (param.exec_path == NULL)
{
@@ -118,7 +129,7 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast)
param.argv = argv;
param.env = env;
- status = fork_wrap(fds, &param, &forked_cmd);
+ status = fork_wrap(fds, &param, &forked_cmd, NULL);
ft_split_destroy(argv);
g_last_status = status;
return (status);
diff --git a/src/eval/op.c b/src/eval/op.c
index 9a37736..a39e380 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/09/10 20:28:57 by charles ### ########.fr */
+/* Updated: 2020/09/12 17:00:11 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -28,6 +28,26 @@ int eval_op(int fds[2], t_env env, t_path path, t_ast *ast)
pipe(p);
left_fds[FD_WRITE] = p[FD_WRITE];
right_fds[FD_READ] = p[FD_READ];
+
+ pid_t left_pid;
+ pid_t right_pid;
+ eval_forked(left_fds, env, path, ast->op.left, &left_pid);
+ eval_forked(right_fds, env, path, ast->op.right, &right_pid);
+
+ pid_t finished;
+ finished = wait(NULL);
+ close(p[FD_READ]);
+ close(p[FD_WRITE]);
+ if (finished == left_pid)
+ {
+ /* waitpid(right_pid, &right_pid, 0); */
+ }
+ else if (finished == right_pid)
+ {
+ kill(left_pid, SIGKILL);
+ /* waitpid(left_pid, &left_pid, 0); */
+ }
+ return (0);
}
if ((status = eval(left_fds, env, path, ast->op.left)) == EVAL_FATAL)
return (EVAL_FATAL);
@@ -41,7 +61,7 @@ int eval_op(int fds[2], t_env env, t_path path, t_ast *ast)
int wrapped_eval(void *void_param)
{
- t_fork_param_parent *param;
+ t_fork_param_args *param;
param = void_param;
return (eval(param->fds, param->env, param->path, param->ast));
@@ -49,18 +69,24 @@ int wrapped_eval(void *void_param)
int eval_parent(int fds[2], t_env env, t_path path, t_ast *ast)
{
- t_fork_param_parent param;
int status;
if ((status = redir_extract(&ast->redirs, env, fds)) != 0)
return (status);
+ ast->tag ^= AST_PARENT;
+ return (eval_forked(fds, env, path, ast->parent_ast, NULL));
+}
+
+int eval_forked(int fds[2], t_env env, t_path path, t_ast *ast, pid_t *child_pid)
+{
+ t_fork_param_args param;
+
param.fds[0] = fds[0];
param.fds[1] = fds[1];
param.env = env;
param.path = path;
- ast->tag ^= AST_PARENT;
- param.ast = ast->parent_ast;
- return (fork_wrap(fds, &param, wrapped_eval));
+ param.ast = ast;
+ return (fork_wrap(fds, &param, wrapped_eval, child_pid));
}
int eval(int fds[2], t_env env, t_path path, t_ast *ast)
diff --git a/src/main.c b/src/main.c
index 5089a6f..62e1d2b 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/09/11 19:18:00 by charles ### ########.fr */
+/* Updated: 2020/09/12 17:04:06 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -108,7 +108,8 @@ int main(int argc, char **argv, char **envp)
if (status == EVAL_FATAL)
exit(1);
g_last_status = status;
- /* error_set_status(status); */
+ exit(status);
+ return (0);
}
else
{
diff --git a/src/utils.c b/src/utils.c
index 55c190b..9fd47c0 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/09/09 15:41:04 by charles ### ########.fr */
+/* Updated: 2020/09/12 16:30:43 by charles ### ########.fr */
/* */
/* ************************************************************************** */