From c9a39637da681f73a4e66efe75257e262688fd27 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Thu, 22 Dec 2016 11:44:44 +0100 Subject: [PATCH] pipeend and pipestart bits in process attribute, env problems solved --- 42sh/includes/exec.h | 12 +++++++++ 42sh/includes/job_control.h | 8 ------ 42sh/libft | 2 +- 42sh/src/builtin/builtin_setenv.c | 2 +- 42sh/src/exec/exec_command.c | 2 +- 42sh/src/exec/exec_pipe.c | 31 +++++++++++++++------- 42sh/src/exec/process_setexec.c | 2 -- 42sh/src/job-control/do_job_notification.c | 2 +- 42sh/src/job-control/job_addprocess.c | 4 +-- 9 files changed, 40 insertions(+), 25 deletions(-) diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 92324800..86048c03 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -16,6 +16,18 @@ # define PIPE_READ 0 # define PIPE_WRITE 1 +# define PROCESS_COMPLETED (1 << 0) +# define PROCESS_STOPPED (1 << 1) +# define PROCESS_BUILTIN (1 << 2) +# define PROCESS_BINARY (1 << 3) +# define PROCESS_SCRIPT (1 << 4) +# define PROCESS_UNKNOWN (1 << 5) +# define PROCESS_PIPESTART (1 << 6) +# define PROCESS_PIPEEND (1 << 7) + +# define IS_PIPESTART(p) (p & (PROCESS_PIPESTART)) +# define IS_PIPEEND(p) (p & (PROCESS_PIPEEND)) + # include "libft.h" # include "types.h" # include "job_control.h" diff --git a/42sh/includes/job_control.h b/42sh/includes/job_control.h index 5597f211..78fabb2c 100644 --- a/42sh/includes/job_control.h +++ b/42sh/includes/job_control.h @@ -19,19 +19,11 @@ # include "libft.h" # include "types.h" -# define PROCESS_COMPLETED (1 << 0) -# define PROCESS_STOPPED (1 << 1) -# define PROCESS_BUILTIN (1 << 2) -# define PROCESS_BINARY (1 << 3) -# define PROCESS_SCRIPT (1 << 4) -# define PROCESS_UNKNOWN (1 << 5) - # define JOB_NOTIFIED (1 << 0) # define JOB_BG (1 << 1) # define JOB_IS_BG(j) (j & JOB_BG) # define JOB_IS_FG(j) !(j & JOB_BG) - struct s_job { int id; diff --git a/42sh/libft b/42sh/libft index e7cece57..a26a4be4 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit e7cece5732986cdcd159b40f1d927137e2d6c9f3 +Subproject commit a26a4be4adbf4d6d4887af95a7307356d52ce85d diff --git a/42sh/src/builtin/builtin_setenv.c b/42sh/src/builtin/builtin_setenv.c index fc6ec20a..6cecb3d6 100644 --- a/42sh/src/builtin/builtin_setenv.c +++ b/42sh/src/builtin/builtin_setenv.c @@ -37,7 +37,7 @@ int builtin_setenv(const char *path, char *const av[], char *const envp[]) { if (ft_strcmp((*env)[i], av[0]) == '=') { - ft_strdel(*env); + ft_strdel(&(*env)[i]); (*env)[i] = str; return (0); } diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 471eda28..67db7916 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -34,7 +34,7 @@ int exec_command(t_btree **ast) DG("gonna launch_process now"); launch_process(p); job_addprocess(p); - if (p->fdout == STDOUT) + if (IS_PIPEEND(p->fdout == STDOUT)) { if (JOB_IS_FG(job->attributes)) put_job_in_foreground(job, 0); diff --git a/42sh/src/exec/exec_pipe.c b/42sh/src/exec/exec_pipe.c index 7b9025af..6bd51167 100644 --- a/42sh/src/exec/exec_pipe.c +++ b/42sh/src/exec/exec_pipe.c @@ -16,21 +16,34 @@ int exec_pipe(t_btree **ast) { int fds[2]; t_data *data; + t_process *p; data = data_singleton(); + p = &data_singleton()->exec.process; pipe(fds); DG("pipe %i->%i", fds[PIPE_WRITE], fds[PIPE_READ]); - data->exec.process.fdout = fds[PIPE_WRITE]; + p->fdout = fds[PIPE_WRITE]; + if (!IS_PIPEEND(p->attributes)) + { + p->attributes |= PROCESS_PIPESTART; + p->attributes &= ~PROCESS_PIPEEND; + } + ft_exec(&(*ast)->left); - if (data->exec.process.fdout != STDOUT) - close(data->exec.process.fdout); - data->exec.process.fdout = STDOUT; - data->exec.process.fdin = fds[PIPE_READ]; + if (p->fdout != STDOUT) + close(p->fdout); + p->fdout = STDOUT; + p->fdin = fds[PIPE_READ]; + + p->attributes ~= ~PROCESS_PIPESTART; + p->attributes |= PROCESS_PIPEEND; + ft_exec(&(*ast)->right); - close(fds[PIPE_WRITE]); - close(fds[PIPE_READ]); - data->exec.process.fdin = STDIN; - data->exec.process.fdout = STDOUT; + /* close(fds[PIPE_WRITE]); */ + /* close(fds[PIPE_READ]); */ + p->fdin = STDIN; + p->fdout = STDOUT; + p->attributes &= ~PROCESS_PIPEEND; btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/process_setexec.c b/42sh/src/exec/process_setexec.c index fd7d7c47..85571934 100644 --- a/42sh/src/exec/process_setexec.c +++ b/42sh/src/exec/process_setexec.c @@ -14,8 +14,6 @@ int process_setexec(t_process *p) { - DG("going to setexec:\nenv at %p\nav[0]=%s", data_singleton()->env, p->argv[0]); - DG("going to setexec:\nPATH=%s\nav[0]=%s", ft_getenv(data_singleton()->env, "PATH"), p->argv[0]); if ((p->execf = is_builtin(p))) { DG("process is a builtin"); diff --git a/42sh/src/job-control/do_job_notification.c b/42sh/src/job-control/do_job_notification.c index a9e4a40c..91c63e8a 100644 --- a/42sh/src/job-control/do_job_notification.c +++ b/42sh/src/job-control/do_job_notification.c @@ -31,7 +31,7 @@ int do_job_notification(void) { ret = 1; job_notify_change(j->id, 0); - /* job_remove(j->id); */ + job_remove(j->id); } else if (job_is_stopped(j) && !(j->attributes & JOB_NOTIFIED)) { diff --git a/42sh/src/job-control/job_addprocess.c b/42sh/src/job-control/job_addprocess.c index 03df4939..48d1930e 100644 --- a/42sh/src/job-control/job_addprocess.c +++ b/42sh/src/job-control/job_addprocess.c @@ -19,7 +19,7 @@ int job_addprocess(t_process *p) jobc = &data_singleton()->jobc; job = &data_singleton()->exec.job; - if (p->fdin == STDIN) + if (IS_PIPESTART(p->attributes)) { job_update_id(); job->id = jobc->current_id; @@ -28,7 +28,7 @@ int job_addprocess(t_process *p) } job = jobc->first_job->content; ft_lstadd(&job->first_process, ft_lstnew(p, sizeof(*p))); - if (JOB_IS_BG(job->attributes) && p->fdout == STDOUT) + if (JOB_IS_BG(job->attributes) && IS_PIPEEND(p->attributes)) job_notify_new(job); DG("added process to first_job : %i", p->pid); return(0);