e0a06c0b12ab438fa08a1e6b978a835e75398d04 — Simon Ser a month ago 8be67f9
Remove job_{terminated,stopped}, superseded by job_poll
4 files changed, 14 insertions(+), 41 deletions(-)

M builtin/bg.c
M include/shell/job.h
M shell/job.c
M shell/task/task.c
M builtin/bg.c => builtin/bg.c +2 -1
@@ 4,6 4,7 @@ #include "builtin.h"
  #include "shell/job.h"
  #include "shell/shell.h"
+ #include "shell/task.h"
  
  // TODO: bg [job_id]
  static const char bg_usage[] = "usage: bg\n";


@@ 27,7 28,7 @@ struct mrsh_job *stopped = NULL;
  	for (ssize_t i = state->jobs.len - 1; i >= 0; --i) {
  		struct mrsh_job *job = state->jobs.data[i];
- 		if (job_stopped(job)) {
+ 		if (job_poll(job) == TASK_STATUS_STOPPED) {
  			stopped = job;
  			break;
  		}

M include/shell/job.h => include/shell/job.h +6 -13
@@ 34,20 34,13 @@ void job_destroy(struct mrsh_job *job);
  void job_add_process(struct mrsh_job *job, struct process *proc);
  /**
-  * Check whether all child processes have terminated. If there are no child
-  * processes in this job, returns true.
-  */
- bool job_terminated(struct mrsh_job *job);
- /**
-  * Check whether there is at least one stopped child process and all others
-  * have terminated.
-  */
- bool job_stopped(struct mrsh_job *job);
- /**
   * Polls the job's current status without blocking. Returns:
-  * - An integer >= 0 if the job has terminated
-  * - TASK_STATUS_STOPPED if the job is stopped
-  * - TASK_STATUS_WAIT if the job is running
+  * - TASK_STATUS_WAIT if the job is running (ie. one or more processes are
+  *   running)
+  * - TASK_STATUS_STOPPED if the job is stopped (ie. one or more processes are
+  *   stopped, all the others are terminated)
+  * - An integer >= 0 if the job has terminated (ie. all processes have
+ *    terminated)
   */
  int job_poll(struct mrsh_job *job);
  /**

M shell/job.c => shell/job.c +2 -23
@@ 118,32 118,11 @@ mrsh_array_add(&job->processes, proc);
  }
  
- bool job_terminated(struct mrsh_job *job) {
- 	for (size_t j = 0; j < job->processes.len; ++j) {
- 		struct process *proc = job->processes.data[j];
- 		if (!proc->terminated) {
- 			return false;
- 		}
- 	}
- 	return true;
- }
- 
- bool job_stopped(struct mrsh_job *job) {
- 	bool stopped = false;
- 	for (size_t j = 0; j < job->processes.len; ++j) {
- 		struct process *proc = job->processes.data[j];
- 		if (!proc->terminated && !proc->stopped) {
- 			return false;
- 		}
- 		stopped |= proc->stopped;
- 	}
- 	return stopped;
- }
- 
  bool job_set_foreground(struct mrsh_job *job, bool foreground, bool cont) {
  	struct mrsh_state *state = job->state;
  
- 	if (!job_stopped(job)) {
+ 	// Don't try to continue the job if it's not stopped
+ 	if (job_poll(job) != TASK_STATUS_STOPPED) {
  		cont = false;
  	}
  

M shell/task/task.c => shell/task/task.c +4 -4
@@ 33,10 33,10 @@ return task->status;
  }
  
- static void destroy_finished_jobs(struct mrsh_state *state) {
+ static void destroy_terminated_jobs(struct mrsh_state *state) {
  	for (ssize_t i = 0; i < (ssize_t)state->jobs.len; ++i) {
  		struct mrsh_job *job = state->jobs.data[i];
- 		if (job_terminated(job)) {
+ 		if (job_poll(job) >= 0) {
  			job_destroy(job);
  			--i;
  		}


@@ 62,12 62,12 @@ // Either the job has terminated, either it's been stopped
  			assert(ctx->state->foreground_job == NULL);
  
- 			destroy_finished_jobs(ctx->state);
+ 			destroy_terminated_jobs(ctx->state);
  
  			return ret;
  		}
  
- 		destroy_finished_jobs(ctx->state);
+ 		destroy_terminated_jobs(ctx->state);
  
  		int stat;
  		pid_t pid = waitpid(-1, &stat, WUNTRACED);