From 5bc7fcc00507bbc5ff5bf957a1589209f8495534 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Fri, 13 Mar 2015 16:16:43 +0100 Subject: Shell begins to be usefull. --- src/kernel/config.h | 2 +- src/kernel/fs/iso9660.c | 2 +- src/kernel/user/process.c | 4 +-- src/lib/include/unistd.h | 10 +++++++ src/lib/libkogata/Makefile | 2 +- src/lib/libkogata/unistd.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++ src/sysbin/init/main.c | 6 ++--- src/sysbin/shell/main.c | 42 ++++++++++++++++++++++++++--- src/sysbin/terminal/main.c | 14 +++++----- 9 files changed, 131 insertions(+), 18 deletions(-) create mode 100644 src/lib/include/unistd.h create mode 100644 src/lib/libkogata/unistd.c (limited to 'src') diff --git a/src/kernel/config.h b/src/kernel/config.h index e7b9591..f50a4ff 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -24,6 +24,6 @@ // Comment to disable either form of debug log output #define DBGLOG_TO_SERIAL -#define DBGLOG_TO_SCREEN +//#define DBGLOG_TO_SCREEN /* vim: set ts=4 sw=4 tw=0 noet :*/ diff --git a/src/kernel/fs/iso9660.c b/src/kernel/fs/iso9660.c index 4de6f2b..6c67823 100644 --- a/src/kernel/fs/iso9660.c +++ b/src/kernel/fs/iso9660.c @@ -315,7 +315,7 @@ bool iso9660_dir_readdir(fs_handle_t *h, size_t ent_no, dirent_t *d) { if (!(dr->flags & ISO9660_DR_FLAG_DIR) && name[len] != ';') continue; if (name[len-1] == '.') len--; // file with no extension - if (idx == ent_no) { + if (idx - 2 == ent_no) { // Found the node we are interested in if (len >= DIR_MAX) len = DIR_MAX - 1; memcpy(d->name, name, len); diff --git a/src/kernel/user/process.c b/src/kernel/user/process.c index 52f1031..00a7e44 100644 --- a/src/kernel/user/process.c +++ b/src/kernel/user/process.c @@ -244,7 +244,7 @@ void process_exit(process_t *p, int status, int exit_code) { // notify parent process_t *par = p->parent; if (par->status == PS_RUNNING) { - resume_on(par->children); + resume_on(&par->children); resume_on(p); } @@ -408,7 +408,7 @@ void process_wait_any_child(process_t *par, proc_status_t *st, bool wait) { { int st = enter_critical(CL_NOSWITCH); mutex_unlock(&par->lock); - wait_ok = wait_on(par); + wait_ok = wait_on(&par->children); exit_critical(st); } diff --git a/src/lib/include/unistd.h b/src/lib/include/unistd.h new file mode 100644 index 0000000..5080d6c --- /dev/null +++ b/src/lib/include/unistd.h @@ -0,0 +1,10 @@ +#pragma once +#include + +int chdir(const char* path); + +char* getcwd(char* buf, size_t buf_len); + +char* pathncat(char* buf, const char* add, size_t buf_len); // path simplification + +/* vim: set ts=4 sw=4 tw=0 noet :*/ diff --git a/src/lib/libkogata/Makefile b/src/lib/libkogata/Makefile index 20c6dee..95de086 100644 --- a/src/lib/libkogata/Makefile +++ b/src/lib/libkogata/Makefile @@ -1,6 +1,6 @@ OBJ = start.o malloc.o debug.o syscall.o user_region.o \ mainloop.o gip.o draw.o keyboard.o \ - stdio.o + stdio.o unistd.o LIB = ../../common/libkogata/libkogata.lib ../../common/libalgo/libalgo.lib ../../common/libc/libc.lib diff --git a/src/lib/libkogata/unistd.c b/src/lib/libkogata/unistd.c new file mode 100644 index 0000000..80a32e4 --- /dev/null +++ b/src/lib/libkogata/unistd.c @@ -0,0 +1,67 @@ +#include + +#include + +#include + + +char cwd_buf[256]; + +char* getcwd(char* buf, size_t buf_len) { + if (buf_len > strlen(cwd_buf)) { + strcpy(buf, cwd_buf); + return buf; + } else { + return 0; + } +} + +int chdir(const char* path) { + char cwd_buf2[256]; + strcpy(cwd_buf2, cwd_buf); + + if (!pathncat(cwd_buf2, path, 256)) return -1; + + stat_t st; + if (!stat(cwd_buf2, &st)) return -1; + if (!st.type & FT_DIR) return -1; + + strcpy(cwd_buf, cwd_buf2); + return 0; +} + +char* pathncat(char* buf, const char* add, size_t buf_len) { + if (strchr(add, ':')) { + if (strlen(add) < buf_len) { + strcpy(buf, add); + return buf; + } else { + return 0; + } + } else { + char* sep_init = strchr(buf, ':'); + if (add[0] == '/') { + if (strlen(add) + (sep_init + 1 - buf) < buf_len) { + strcpy(sep_init + 1, add); + return buf; + } else { + return 0; + } + } else { + //TODO: simplify '..' + char* end = buf + strlen(buf) - 1; + if (*end != '/') end++; + if (end + 1 - buf + strlen(add) < buf_len) { + *end = '/'; + strcpy(end + 1, add); + return buf; + } else { + return 0; + } + } + return 0; + } +} + + +/* vim: set ts=4 sw=4 tw=0 noet :*/ diff --git a/src/sysbin/init/main.c b/src/sysbin/init/main.c index d7f3f36..6ac003b 100644 --- a/src/sysbin/init/main.c +++ b/src/sysbin/init/main.c @@ -80,10 +80,10 @@ void setup_sys() { char* sep = strchr(buf, ':'); if (sep == 0) { - ok = fs_subfs("sys", "root", buf, FM_READ | FM_MMAP); + ok = fs_subfs("sys", "root", buf, FM_READ | FM_MMAP | FM_READDIR); } else { *sep = 0; - ok = fs_subfs("sys", buf, sep +1, FM_READ | FM_MMAP); + ok = fs_subfs("sys", buf, sep +1, FM_READ | FM_MMAP | FM_READDIR); } if (!ok) PANIC("[init] Could not bind root:/sys to sys:/"); @@ -166,7 +166,7 @@ int main(int argc, char **argv) { char buf[50]; snprintf(buf, 50, "/config/%s", config); - bool ok = fs_subfs("config", "root", buf, FM_READ | FM_WRITE | FM_MMAP); + bool ok = fs_subfs("config", "root", buf, FM_READ | FM_WRITE | FM_MMAP | FM_READDIR); if (!ok) PANIC("[init] Could not setup config:"); } diff --git a/src/sysbin/shell/main.c b/src/sysbin/shell/main.c index 54cec06..1929025 100644 --- a/src/sysbin/shell/main.c +++ b/src/sysbin/shell/main.c @@ -4,22 +4,58 @@ #include #include +#include #include +void ls(char* dir) { + fd_t f = open(dir, FM_READDIR); + if (f) { + dirent_t i; + int ent_no = 0; + while (readdir(f, ent_no++, &i)) { + printf("%s\n", i.name); + } + close(f); + } else { + printf("Could not open directory '%s'\n", dir); + } +} + int main(int argc, char **argv) { dbg_printf("[shell] Starting\n"); /*fctl(stdio, FC_SET_BLOCKING, 0);*/ - puts("Hello, world!\n"); + puts("Kogata shell.\n"); + + chdir("sys:"); while(true) { - puts("> "); char buf[256]; + printf("\n%s> ", getcwd(buf, 256)); + getline(buf, 256); - printf("You said: '%s'. I don't understand a word of that.\n\n", buf); + if (!strncmp(buf, "cd ", 3)) { + chdir(buf + 3); + } else if (!strcmp(buf, "ls")) { + if (getcwd(buf, 256)) { + ls(buf); + } + } else if (!strncmp(buf, "ls ", 3)) { + char buf2[256]; + if (getcwd(buf2, 256)) { + if (pathncat(buf2, buf + 3, 256)) { + ls(buf2); + } + } + } else if (!strcmp(buf, "exit")) { + break; + } else { + printf("No such command.\n"); + } } + printf("Bye.\n"); return 0; } diff --git a/src/sysbin/terminal/main.c b/src/sysbin/terminal/main.c index 133736b..1174305 100644 --- a/src/sysbin/terminal/main.c +++ b/src/sysbin/terminal/main.c @@ -231,13 +231,7 @@ void c_buffer_info(gip_handler_t *s, gip_msg_header *p, gip_buffer_info_msg *m) void c_key_down(gip_handler_t *s, gip_msg_header *p) { term_t *c = (term_t*)s->data; - keyboard_press(c->kb, p->arg); -} - -void c_key_up(gip_handler_t *s, gip_msg_header *p) { - term_t *c = (term_t*)s->data; - - key_t k = keyboard_release(c->kb, p->arg); + key_t k = keyboard_press(c->kb, p->arg); c->wr_c_buf = 0; if (k.flags & KBD_CHAR) { @@ -250,6 +244,12 @@ void c_key_up(gip_handler_t *s, gip_msg_header *p) { mainloop_nonblocking_write(&c->app, &c->wr_c_buf, 1, false); } +void c_key_up(gip_handler_t *s, gip_msg_header *p) { + term_t *c = (term_t*)s->data; + + keyboard_release(c->kb, p->arg); +} + void c_unknown_msg(gip_handler_t *s, gip_msg_header *p) { // TODO } -- cgit v1.2.3