~emersion/mrsh-dev

jobs(1): implement -l, -p flags v1 SUPERSEDED

Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/mrsh-dev/%3C20190831005355.10593-1-sir%40cmpwn.com%3E/mbox | git am -3
Learn more about email & git

[PATCH] jobs(1): implement -l, -p flags Export this patch

This completes the jobs(1) implementation.
---
 builtin/jobs.c | 43 ++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 38 insertions(+), 5 deletions(-)

diff --git a/builtin/jobs.c b/builtin/jobs.c
index 17dc6c2..d7ab306 100644
--- a/builtin/jobs.c
@@ -1,9 +1,11 @@
 #include <assert.h>
+#include <limits.h>
 #include <mrsh/getopt.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include "builtin.h"
 #include "shell/job.h"
+#include "shell/process.h"
 #include "shell/shell.h"
 #include "shell/task.h"
 
@@ -27,10 +29,28 @@ static char *job_state_str(struct mrsh_job *job) {
 }
 
 int builtin_jobs(struct mrsh_state *state, int argc, char *argv[]) {
+	bool pids = false, pgids = false;
+
 	mrsh_optind = 0;
 	int opt;
-	while ((opt = mrsh_getopt(argc, argv, ":")) != -1) {
+	while ((opt = mrsh_getopt(argc, argv, ":lp")) != -1) {
 		switch (opt) {
+		case 'l':
+			if (pids) {
+				fprintf(stderr, "jobs: the -p and -l options are "
+						"mutually exclusive\n");
+				return EXIT_FAILURE;
+			}
+			pgids = true;
+			break;
+		case 'p':
+			if (pgids) {
+				fprintf(stderr, "jobs: the -p and -l options are "
+						"mutually exclusive\n");
+				return EXIT_FAILURE;
+			}
+			pids = true;
+			break;
 		default:
 			fprintf(stderr, "jobs: unknown option -- %c\n", mrsh_optopt);
 			fprintf(stderr, jobs_usage);
@@ -45,10 +65,23 @@ int builtin_jobs(struct mrsh_state *state, int argc, char *argv[]) {
 		if (job_poll(job) >= 0) {
 			continue;
 		}
-		char *cmd = mrsh_node_format(job->node);
-		printf("[%d] %c %s %s\n", job->job_id, job == current ? '+' : ' ',
-				job_state_str(job), cmd);
-		free(cmd);
+		if (pids) {
+			for (size_t j = 0; j < job->processes.len; ++j) {
+				struct process *proc = job->processes.data[j];
+				printf("%d\n", proc->pid);
+			}
+		} else if (pgids) {
+			char *cmd = mrsh_node_format(job->node);
+			printf("[%d] %c %d %s %s\n", job->job_id,
+					job == current ? '+' : ' ', job->pgid,
+					job_state_str(job), cmd);
+			free(cmd);
+		} else {
+			char *cmd = mrsh_node_format(job->node);
+			printf("[%d] %c %s %s\n", job->job_id, job == current ? '+' : ' ',
+					job_state_str(job), cmd);
+			free(cmd);
+		}
 	}
 
 	return EXIT_SUCCESS;
-- 
2.23.0
View this thread in the archives