~emersion/mrsh

65b57c2747f87357a65548169e9c0258c3c69875 — Drew DeVault 2 years ago f593609
jobs(1): job_state_str: add parenthesized info

This completes the jobs(1) implementation.
3 files changed, 22 insertions(+), 1 deletions(-)

M builtin/jobs.c
M include/shell/process.h
M shell/process.c
M builtin/jobs.c => builtin/jobs.c +20 -1
@@ 3,6 3,7 @@
#include <mrsh/getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include "builtin.h"
#include "shell/job.h"
#include "shell/process.h"


@@ 13,15 14,33 @@ static const char jobs_usage[] = "usage: jobs\n";

static char *job_state_str(struct mrsh_job *job) {
	int status = job_poll(job);
	// TODO code in parentheses for Done and Stopped
	switch (status) {
	case TASK_STATUS_WAIT:
		return "Running";
	case TASK_STATUS_ERROR:
		return "Error";
	case TASK_STATUS_STOPPED:
		if (job->processes.len > 0) {
			struct process *proc = job->processes.data[0];
			switch (proc->signal) {
			case SIGSTOP:
				return "Stopped (SIGSTOP)";
			case SIGTTIN:
				return "Stopped (SIGTTIN)";
			case SIGTTOU:
				return "Stopped (SIGTTOU)";
			}
		}
		return "Stopped";
	default:
		if (job->processes.len > 0) {
			struct process *proc = job->processes.data[0];
			if (proc->stat != 0) {
				static char stat[128];
				snprintf(stat, sizeof(stat), "Done(%d)", proc->stat);
				return stat;
			}
		}
		assert(status >= 0);
		return "Done";
	}

M include/shell/process.h => include/shell/process.h +1 -0
@@ 18,6 18,7 @@ struct process {
	bool stopped;
	bool terminated;
	int stat; // only valid if terminated
	int signal; // only valid if stopped is true
};

/**

M shell/process.c => shell/process.c +1 -0
@@ 68,6 68,7 @@ void update_process(struct mrsh_state *state, pid_t pid, int stat) {
		proc->stat = stat;
	} else if (WIFSTOPPED(stat)) {
		proc->stopped = true;
		proc->signal = WSTOPSIG(stat);
	} else {
		assert(false);
	}