Implement $! expansion v1 PROPOSED

Drew DeVault: 1
 Implement $! expansion

 1 files changed, 9 insertions(+), 1 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/%3C20190604234427.18585-1-sir%40cmpwn.com%3E/mbox | git am -3
Learn more about email & git

[PATCH] Implement $! expansion Export this patch

---
Note that due to issues with how mrsh spawns job groups (posted
separately to the ML), this will return the pid of the child shell
rather than the pid of the child process.

 shell/task/word.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/shell/task/word.c b/shell/task/word.c
index d3d1063..9390026 100644
--- a/shell/task/word.c
+++ b/shell/task/word.c
@@ -113,7 +113,15 @@ static const char *parameter_get_value(struct mrsh_state *state, char *name) {
 		sprintf(value, "%d", (int)getpid());
 		return value;
 	} else if (strcmp(name, "!") == 0) {
-		// TODO
+		if (state->jobs.len == 0) {
+			/* Standard is unclear on what to do in this case, mimic dash */
+			return "";
+		}
+		struct mrsh_job *job = state->jobs.data[state->jobs.len - 1];
+		struct process *process =
+			job->processes.data[job->processes.len - 1];
+		sprintf(value, "%zd %d", job->processes.len, process->pid);
+		return value;
 	} else if (end[0] == '\0') {
 		if (lvalue >= state->args->argc) {
 			return NULL;
-- 
2.21.0