summaryrefslogtreecommitdiff
path: root/src/user/app/init/shell.c
diff options
context:
space:
mode:
authordzwdz2022-07-26 22:54:28 +0200
committerdzwdz2022-07-26 22:54:28 +0200
commitd7767be24bdcc0101c2ba08c03600b9f3c608663 (patch)
tree2ce6cb96fafad4814ad73c15ec32b702d3882280 /src/user/app/init/shell.c
parent4aee2f759ab7bf2ad9534941afef0195040ba5db (diff)
user/shell: automatically execute binaries in /bin/
Diffstat (limited to 'src/user/app/init/shell.c')
-rw-r--r--src/user/app/init/shell.c39
1 files changed, 28 insertions, 11 deletions
diff --git a/src/user/app/init/shell.c b/src/user/app/init/shell.c
index 85e56e1..048b713 100644
--- a/src/user/app/init/shell.c
+++ b/src/user/app/init/shell.c
@@ -3,6 +3,7 @@
#include <camellia/syscalls.h>
#include <stdbool.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <user/lib/elfload.h>
@@ -137,8 +138,12 @@ void shell_loop(void) {
readline(buf, 256);
if (feof(stdin))
exit(0);
- redir = strtrim(strsplit(buf, '>'));
+
cmd = strtrim(buf);
+ if (!*cmd) continue;
+
+ redir = strtrim(strsplit(cmd, '>'));
+ cmd = strtrim(cmd);
args = strtrim(strsplit(cmd, 0));
/* "special" commands that can't be handled in a subprocess */
@@ -162,15 +167,6 @@ void shell_loop(void) {
if (!strcmp(cmd, "echo")) {
printf("%s\n", args);
- } else if (!strcmp(cmd, "exec")) {
- FILE *file = fopen(args, "r");
- if (!file) {
- printf("couldn't open file\n");
- } else {
- elf_execf(file);
- fclose(file);
- printf("elf_execf failed\n");
- }
} else if (!strcmp(cmd, "cat")) {
cmd_cat_ls(args, false);
} else if (!strcmp(cmd, "ls")) {
@@ -194,7 +190,28 @@ void shell_loop(void) {
} else if (!strcmp(cmd, "stress")) {
stress_all();
} else {
- printf("unknown command :(\n");
+ char *binname = cmd;
+ if (*cmd != '/') {
+ size_t cmdlen = strlen(cmd);
+ binname = malloc(cmdlen);
+ if (!binname) {
+ printf("sh: out of memory.\n");
+ exit(1);
+ }
+ memcpy(binname, "/bin/", 5);
+ memcpy(binname + 5, cmd, cmdlen + 1);
+ }
+
+ FILE *file = fopen(binname, "r");
+ if (!file) {
+ printf("unknown command: %s\n", cmd);
+ } else {
+ elf_execf(file);
+ fclose(file);
+ printf("couldn't execute %s\n", binname);
+ }
+ if (binname != cmd)
+ free(binname);
}
exit(0);
} else {