Match job IDs against command strings v1 PROPOSED

Drew DeVault: 1
 Match job IDs against command strings

 1 files changed, 14 insertions(+), 3 deletions(-)
View this thread in the archives
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/%3C20190607202553.30669-1-sir%40cmpwn.com%3E/mbox | git am -3
Learn more about email & git

[PATCH] Match job IDs against command strings Export this patch

Note that this depends on the command string being valid, which is not
always the case at the moment.
---
 shell/job.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/shell/job.c b/shell/job.c
index 467726d..9841382 100644
--- a/shell/job.c
+++ b/shell/job.c
@@ -307,11 +307,22 @@ struct mrsh_job *job_by_id(struct mrsh_state *state, const char *id) {
 	}
 
 	if (id[1] == '?') {
-		// TODO
-		fprintf(stderr, "Job lookup by command string is unimplemented\n");
+		for (size_t i = 0; i < state->jobs.len; ++i) {
+			struct mrsh_job *job = state->jobs.data[i];
+			if (strstr(job->command, &id[2]) != NULL) {
+				return job;
+			}
+		}
+		fprintf(stderr, "No such job '%s'\n", id);
 		return NULL;
 	}
 
-	fprintf(stderr, "Job lookup by command string is unimplemented\n");
+	for (size_t i = 0; i < state->jobs.len; ++i) {
+		struct mrsh_job *job = state->jobs.data[i];
+		if (strstr(job->command, &id[1]) == job->command) {
+			return job;
+		}
+	}
+	fprintf(stderr, "No such job '%s'\n", id);
 	return NULL;
 }
-- 
2.21.0