~emersion/mrsh-dev

jobs(1): implement <current> marker 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/%3C20190831004432.8830-1-sir%40cmpwn.com%3E/mbox | git am -3
Learn more about email & git

[PATCH] jobs(1): implement <current> marker Export this patch

---
 builtin/bg.c        |  4 ++--
 builtin/fg.c        |  4 ++--
 builtin/jobs.c      |  7 +++++--
 builtin/wait.c      |  2 +-
 include/shell/job.h |  6 +++++-
 shell/job.c         | 26 ++++++++++++++++++++------
 6 files changed, 35 insertions(+), 14 deletions(-)

diff --git a/builtin/bg.c b/builtin/bg.c
index b0c2bee..f61fed0 100644
--- a/builtin/bg.c
@@ -20,7 +20,7 @@ int builtin_bg(struct mrsh_state *state, int argc, char *argv[]) {
 		}
 	}
 	if (mrsh_optind == argc) {
-		struct mrsh_job *job = job_by_id(state, "%%");
+		struct mrsh_job *job = job_by_id(state, "%%", true);
 		if (!job) {
 			return EXIT_FAILURE;
 		}
@@ -31,7 +31,7 @@ int builtin_bg(struct mrsh_state *state, int argc, char *argv[]) {
 	}
 
 	for (int i = mrsh_optind; i < argc; ++i) {
-		struct mrsh_job *job = job_by_id(state, argv[i]);
+		struct mrsh_job *job = job_by_id(state, argv[i], true);
 		if (!job) {
 			return EXIT_FAILURE;
 		}
diff --git a/builtin/fg.c b/builtin/fg.c
index b6e3f18..3bc5992 100644
--- a/builtin/fg.c
@@ -21,9 +21,9 @@ int builtin_fg(struct mrsh_state *state, int argc, char *argv[]) {
 
 	struct mrsh_job *job;
 	if (mrsh_optind == argc) {
-		job = job_by_id(state, "%%");
+		job = job_by_id(state, "%%", true);
 	} else if (mrsh_optind == argc - 1) {
-		job = job_by_id(state, argv[mrsh_optind]);
+		job = job_by_id(state, argv[mrsh_optind], true);
 	} else {
 		fprintf(stderr, fg_usage);
 		return EXIT_FAILURE;
diff --git a/builtin/jobs.c b/builtin/jobs.c
index f6be2f8..17dc6c2 100644
--- a/builtin/jobs.c
@@ -38,15 +38,18 @@ int builtin_jobs(struct mrsh_state *state, int argc, char *argv[]) {
 		}
 	}
 
+	struct mrsh_job *current = job_by_id(state, "%+", false);
+
 	for (size_t i = 0; i < state->jobs.len; i++) {
 		struct mrsh_job *job = state->jobs.data[i];
 		if (job_poll(job) >= 0) {
 			continue;
 		}
-		// TODO: <current>
 		char *cmd = mrsh_node_format(job->node);
-		printf("[%d] %c %s %s\n", job->job_id, ' ', job_state_str(job), cmd);
+		printf("[%d] %c %s %s\n", job->job_id, job == current ? '+' : ' ',
+				job_state_str(job), cmd);
 		free(cmd);
 	}
+
 	return EXIT_SUCCESS;
 }
diff --git a/builtin/wait.c b/builtin/wait.c
index d6c005f..7f793a7 100644
--- a/builtin/wait.c
@@ -41,7 +41,7 @@ int builtin_wait(struct mrsh_state *state, int argc, char *argv[]) {
 	} else {
 		for (int i = 1; i < argc; ++i) {
 			if (argv[i][0] == '%') {
-				struct mrsh_job *job = job_by_id(state, argv[i]);
+				struct mrsh_job *job = job_by_id(state, argv[i], true);
 				if (!job) {
 					goto failure;
 				}
diff --git a/include/shell/job.h b/include/shell/job.h
index 59e2efc..390cbb6 100644
--- a/include/shell/job.h
+++ b/include/shell/job.h
@@ -82,7 +82,11 @@ bool init_job_child_process(struct mrsh_state *state);
 void update_job(struct mrsh_state *state, pid_t pid, int stat);
 /**
  * Look up a job by its XBD Job Control Job ID.
+ *
+ * When using this to look up jobs internally, set interactive to false. This
+ * suppresses error reporting.
  */
-struct mrsh_job *job_by_id(struct mrsh_state *state, const char *id);
+struct mrsh_job *job_by_id(struct mrsh_state *state,
+		const char *id, bool interactive);
 
 #endif
diff --git a/shell/job.c b/shell/job.c
index 3156f5b..f75d71c 100644
--- a/shell/job.c
+++ b/shell/job.c
@@ -298,9 +298,12 @@ void update_job(struct mrsh_state *state, pid_t pid, int stat) {
 }
 
 // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_204
-struct mrsh_job *job_by_id(struct mrsh_state *state, const char *id) {
+struct mrsh_job *job_by_id(struct mrsh_state *state,
+		const char *id, bool interactive) {
 	if (id[0] != '%' || id[1] == '\0') {
-		fprintf(stderr, "Invalid job ID specifier\n");
+		if (interactive) {
+			fprintf(stderr, "Invalid job ID specifier\n");
+		}
 		return NULL;
 	}
 
@@ -321,7 +324,9 @@ struct mrsh_job *job_by_id(struct mrsh_state *state, const char *id) {
 					return job;
 				}
 			}
-			fprintf(stderr, "No current job\n");
+			if (interactive) {
+				fprintf(stderr, "No current job\n");
+			}
 			return NULL;
 		case '-':
 			// Previous job
@@ -344,7 +349,9 @@ struct mrsh_job *job_by_id(struct mrsh_state *state, const char *id) {
 					return job;
 				}
 			}
-			fprintf(stderr, "No previous job\n");
+			if (interactive) {
+				fprintf(stderr, "No previous job\n");
+			}
 			return NULL;
 		}
 	}
@@ -353,7 +360,9 @@ struct mrsh_job *job_by_id(struct mrsh_state *state, const char *id) {
 		char *endptr;
 		int n = strtol(&id[1], &endptr, 10);
 		if (endptr[0] != '\0') {
-			fprintf(stderr, "Invalid job number '%s'\n", id);
+			if (interactive) {
+				fprintf(stderr, "Invalid job number '%s'\n", id);
+			}
 			return NULL;
 		}
 		for (size_t i = 0; i < state->jobs.len; ++i) {
@@ -362,7 +371,9 @@ struct mrsh_job *job_by_id(struct mrsh_state *state, const char *id) {
 				return job;
 			}
 		}
-		fprintf(stderr, "No such job '%s' (%d)\n", id, n);
+		if (interactive) {
+			fprintf(stderr, "No such job '%s' (%d)\n", id, n);
+		}
 		return NULL;
 	}
 
@@ -384,5 +395,8 @@ struct mrsh_job *job_by_id(struct mrsh_state *state, const char *id) {
 		}
 	}
 
+	if (interactive) {
+		fprintf(stderr, "No such job '%s'\n", id);
+	}
 	return NULL;
 }
-- 
2.23.0
View this thread in the archives