~emersion/mrsh-dev

Implement 'wait' builtin v2 PROPOSED

Drew DeVault: 2
 Implement 'wait' builtin
 Implement $! expansion

 5 files changed, 120 insertions(+), 1 deletions(-)
Both pushed:

To git.sr.ht:~emersion/mrsh
   27145de12e3d..7d1fc69ce60f  master -> master

Thanks!
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/patches/6048/mbox | git am -3
Learn more about email & git

[PATCH v2 1/2] Implement 'wait' builtin Export this patch

---
 builtin/builtin.c |   1 +
 builtin/wait.c    | 108 ++++++++++++++++++++++++++++++++++++++++++++++
 include/builtin.h |   1 +
 meson.build       |   1 +
 4 files changed, 111 insertions(+)
 create mode 100644 builtin/wait.c

diff --git a/builtin/builtin.c b/builtin/builtin.c
index aaeac2b..ce77af3 100644
--- a/builtin/builtin.c
@@ -38,6 +38,7 @@ static const struct builtin builtins[] = {
 	{ "umask", builtin_umask, false },
 	{ "unalias", builtin_unalias, false },
 	{ "unset", builtin_unset, true },
+	{ "wait", builtin_wait, false },
 };
 
 // The following commands are explicitly unspecified by POSIX
diff --git a/builtin/wait.c b/builtin/wait.c
new file mode 100644
index 0000000..7fc9dac
--- /dev/null
@@ -0,0 +1,108 @@
+#define _POSIX_C_SOURCE 200112L
+#include <errno.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/wait.h>
+#include "builtin.h"
+#include "shell/process.h"
+#include "shell/shell.h"
+
+struct wait_handle {
+	pid_t pid;
+	int status;
+};
+
+int builtin_wait(struct mrsh_state *state, int argc, char *argv[]) {
+	int npids = argc - 1;
+	if (npids == 0) {
+		npids = state->processes.len;
+	}
+	struct wait_handle *pids = malloc(npids * sizeof(struct wait_handle));
+	if (pids == NULL) {
+		fprintf(stderr, "wait: unable to allocate pid list");
+		return EXIT_FAILURE;
+	}
+
+	if (argc == 1) {
+		/* All known processes */
+		int _npids = 0;
+		for (size_t j = 0; j < state->processes.len; ++j) {
+			struct process *process = state->processes.data[j];
+			if (process->terminated) {
+				continue;
+			}
+			pids[_npids].pid = process->pid;
+			pids[_npids].status = -1;
+			++_npids;
+		}
+		npids = _npids;
+	} else {
+		for (int i = 1; i < argc; ++i) {
+			if (argv[i][0] == '%') {
+				// TODO
+				fprintf(stderr, "wait: job control IDs are unimplemented\n");
+				goto failure;
+			} else {
+				char *endptr;
+				pid_t pid = (pid_t)strtol(argv[i], &endptr, 10);
+				if (*endptr != '\0' || argv[i][0] == '\0') {
+					fprintf(stderr, "wait: error parsing pid '%s'", argv[i]);
+					goto failure;
+				}
+				if (pid <= 0) {
+					fprintf(stderr, "wait: invalid process ID\n");
+					goto failure;
+				}
+				pids[i - 1].pid = pid;
+				pids[i - 1].status = -1;
+				/* Check if this pid is known */
+				bool found = false;
+				for (size_t j = 0; j < state->processes.len; ++j) {
+					struct process *process = state->processes.data[j];
+					if (process->pid == pid) {
+						if (process->terminated) {
+							pids[i - 1].status = process->stat;
+						}
+						found = true;
+						break;
+					}
+				}
+				if (!found) {
+					/* Unknown pids are assumed to have exited 127 */
+					pids[i - 1].status = 127;
+				}
+			}
+		}
+	}
+
+	for (int i = 0; i < npids; ++i) {
+		int stat;
+		pid_t waited = waitpid(pids[i].pid, &stat, 0);
+		update_process(state, waited, stat);
+		if (waited == -1) {
+			if (errno == ECHILD) {
+				continue;
+			}
+			fprintf(stderr, "wait: %s\n", strerror(errno));
+			goto failure;
+		}
+		if (WIFEXITED(stat)) {
+			pids[i].status = WEXITSTATUS(stat);
+		} else {
+			pids[i].status = 129;
+		}
+	}
+
+	free(pids);
+	if (argc == 1) {
+		return EXIT_SUCCESS;
+	} else {
+		return pids[npids - 1].status;
+	}
+
+failure:
+	free(pids);
+	return EXIT_FAILURE;
+}
diff --git a/include/builtin.h b/include/builtin.h
index fb1933a..1f60125 100644
--- a/include/builtin.h
+++ b/include/builtin.h
@@ -31,6 +31,7 @@ int builtin_ulimit(struct mrsh_state *state, int argc, char *argv[]);
 int builtin_umask(struct mrsh_state *state, int argc, char *argv[]);
 int builtin_unalias(struct mrsh_state *state, int argc, char *argv[]);
 int builtin_unset(struct mrsh_state *state, int argc, char *argv[]);
+int builtin_wait(struct mrsh_state *state, int argc, char *argv[]);
 
 int builtin_unspecified(struct mrsh_state *state, int argc, char *argv[]);
 
diff --git a/meson.build b/meson.build
index e9b43e8..9c1f67a 100644
--- a/meson.build
+++ b/meson.build
@@ -97,6 +97,7 @@ lib_mrsh = library(
 		'builtin/unalias.c',
 		'builtin/unset.c',
 		'builtin/unspecified.c',
+		'builtin/wait.c',
 		'getopt.c',
 		'hashtable.c',
 		'parser/arithm.c',
-- 
2.21.0

[PATCH v2 2/2] Implement $! expansion Export this patch

---
 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..c456dc6 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, "%d", process->pid);
+		return value;
 	} else if (end[0] == '\0') {
 		if (lvalue >= state->args->argc) {
 			return NULL;
-- 
2.21.0
Both pushed:

To git.sr.ht:~emersion/mrsh
   27145de12e3d..7d1fc69ce60f  master -> master

Thanks!
View this thread in the archives