summaryrefslogtreecommitdiff
path: root/src/user
diff options
context:
space:
mode:
authordzwdz2022-07-12 21:55:03 +0200
committerdzwdz2022-07-12 21:55:03 +0200
commit0505f08ea7e0f50cfaff0f883481679bd514b50d (patch)
treeed4cd028b7944e9df2aba7521db4e230b1bc3b34 /src/user
parent93af95b3419edfc127c6ea4245753540190c520e (diff)
user/shell: parse redirection syntax
Diffstat (limited to 'src/user')
-rw-r--r--src/user/app/shell.c37
1 files changed, 31 insertions, 6 deletions
diff --git a/src/user/app/shell.c b/src/user/app/shell.c
index 5bab2ab..09b0b82 100644
--- a/src/user/app/shell.c
+++ b/src/user/app/shell.c
@@ -4,9 +4,14 @@
#include <shared/syscalls.h>
#include <stdbool.h>
-static char *split(char *base) {
+static bool isspace(char c) {
+ return c == ' ' || c == '\t' || c == '\n';
+}
+
+static char *strsplit(char *base, char delim) {
+ if (!base) return NULL;
while (*base) {
- if (*base == ' ' || *base == '\t') {
+ if (delim ? *base == delim : isspace(*base)) {
*base++ = '\0';
return base;
}
@@ -15,6 +20,17 @@ static char *split(char *base) {
return NULL;
}
+static char *strtrim(char *s) {
+ char *end;
+ if (!s) return NULL;
+ while (isspace(*s)) s++;
+ end = s + strlen(s);
+ while (end > s && isspace(end[-1])) end--;
+ *end = '\0';
+ return s;
+}
+
+
static int readline(char *buf, size_t max) {
char c;
size_t pos = 0;
@@ -124,14 +140,23 @@ static void cmd_touch(const char *args) {
}
void shell_loop(void) {
- static char cmd[256];
+ static char buf[256];
int level = 0;
- char *args;
+ char *cmd, *args, *redir;
for (;;) {
printf("%x$ ", level);
- readline(cmd, 256);
- args = split(cmd);
+
+ readline(buf, 256);
+ redir = strtrim(strsplit(buf, '>'));
+ cmd = strtrim(buf);
+ args = strtrim(strsplit(cmd, 0));
+
+ if (redir) {
+ printf("redirections aren't supported just yet :(\n");
+ continue;
+ }
+
if (!strcmp(cmd, "echo")) {
printf("%s\n", args);
} else if (!strcmp(cmd, "cat")) {