diff options
author | Ian Moffett <ian@osmora.org> | 2025-05-31 17:14:55 -0400 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2025-05-31 17:14:55 -0400 |
commit | b86fde14a13edabcb6b9552910c89e64f68a6e8e (patch) | |
tree | b7a4bf2a8d3e7f50d039d1fedaf3b2990fc00f33 /usr.bin/osh | |
parent | 7609d17106b077595439008437b8e3845a930006 (diff) |
usr.bin: osh: Make kmsg its own program
- Give command table entries a path field to refer to
paths of executable files
- Make kmsg its own program in /usr/bin/kmsg
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'usr.bin/osh')
-rw-r--r-- | usr.bin/osh/osh.c | 52 |
1 files changed, 22 insertions, 30 deletions
diff --git a/usr.bin/osh/osh.c b/usr.bin/osh/osh.c index 9b4e9ab..4e16463 100644 --- a/usr.bin/osh/osh.c +++ b/usr.bin/osh/osh.c @@ -30,6 +30,7 @@ #include <sys/types.h> #include <sys/cdefs.h> #include <sys/reboot.h> +#include <sys/spawn.h> #include <fcntl.h> #include <stddef.h> #include <unistd.h> @@ -60,6 +61,7 @@ static int running; struct command { const char *name; + const char *path; void (*func)(int fd, int argc, char *argv[]); }; @@ -88,29 +90,6 @@ cmd_shutdown(int fd, int argc, char *argv[]) } void -cmd_kmsg(int fd, int argc, char *argv[]) -{ - int mfd; - ssize_t retval; - char linebuf[256]; - - if ((mfd = open("/dev/kmsg", O_RDONLY)) < 0) { - return; - } - - for (;;) { - retval = read(mfd, linebuf, sizeof(linebuf) - 1); - if (retval <= 0) { - break; - } - linebuf[retval] = '\0'; - prcons(fd, linebuf); - } - - close(mfd); -} - -void cmd_echo(int fd, int argc, char *argv[]) { for (i = 1; i < argc; i++) { @@ -189,13 +168,26 @@ getstr(int fd) } } +static void +command_run(struct command *cmd, int fd, int argc, char *argv[]) +{ + if (cmd->func != NULL) { + cmd->func(fd, argc, argv); + return; + } + + if (cmd->path != NULL) { + spawn(cmd->path, SPAWN_WAIT); + } +} + struct command cmds[] = { - {"help", cmd_help}, - {"echo", cmd_echo}, - {"exit", cmd_exit}, - {"reboot", cmd_reboot}, - {"shutdown", cmd_shutdown}, - {"kmsg", cmd_kmsg}, + {"help", NULL, cmd_help}, + {"echo", NULL, cmd_echo}, + {"exit", NULL, cmd_exit}, + {"reboot", NULL, cmd_reboot}, + {"shutdown", NULL, cmd_shutdown}, + {"kmsg", "/usr/bin/kmsg", NULL}, {NULL, NULL} }; @@ -230,7 +222,7 @@ main(void) for (i = 0; cmds[i].name != NULL; i++) { if (strcmp(input, cmds[i].name) == 0) { - cmds[i].func(fd, argc, argv); + command_run(&cmds[i], fd, argc, argv); found = 1; break; } |