From 73bb0e48640b842824a0b45fe4854df8ff7faf7b Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Sun, 6 Sep 2020 13:44:20 -0400 Subject: bugfixes, init program, hello world --- src/user/hello/hello.c | 5 +++++ src/user/include/knob/block.h | 8 -------- src/user/include/knob/file.h | 2 ++ src/user/include/knob/task.h | 9 +++++++++ src/user/include/pland/syscall.h | 5 +++-- src/user/init/main.c | 38 ++++++++++++++++++++------------------ src/user/knob/file.c | 16 +++++++--------- src/user/knob/task.c | 13 +++++++++++++ 8 files changed, 59 insertions(+), 37 deletions(-) create mode 100644 src/user/hello/hello.c delete mode 100644 src/user/include/knob/block.h create mode 100644 src/user/include/knob/task.h create mode 100644 src/user/knob/task.c (limited to 'src/user') diff --git a/src/user/hello/hello.c b/src/user/hello/hello.c new file mode 100644 index 0000000..65e23f0 --- /dev/null +++ b/src/user/hello/hello.c @@ -0,0 +1,5 @@ +#include + +void main() { + tell_user_sz("\nHello, world!\nThis is a userspace program that has been started by init."); +} \ No newline at end of file diff --git a/src/user/include/knob/block.h b/src/user/include/knob/block.h deleted file mode 100644 index 53b3deb..0000000 --- a/src/user/include/knob/block.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef KNOB_BLOCK_H -#define KNOB_BLOCK_H - -#include - -void blockcpy(void *to, const void *from, uint32_t count); - -#endif \ No newline at end of file diff --git a/src/user/include/knob/file.h b/src/user/include/knob/file.h index 6068077..4d0da87 100644 --- a/src/user/include/knob/file.h +++ b/src/user/include/knob/file.h @@ -5,6 +5,8 @@ struct file; +const char *remove_prefix(const char *path, uint8_t *dn_out); + struct file *open_file(const char *path); void close_file(struct file *f); diff --git a/src/user/include/knob/task.h b/src/user/include/knob/task.h new file mode 100644 index 0000000..ea5df62 --- /dev/null +++ b/src/user/include/knob/task.h @@ -0,0 +1,9 @@ +#ifndef KNOB_TASK_H +#define KNOB_TASK_H + +#include + +bool try_run_command(const char *path); +void yield_task(); + +#endif \ No newline at end of file diff --git a/src/user/include/pland/syscall.h b/src/user/include/pland/syscall.h index 01f7151..5858d16 100644 --- a/src/user/include/pland/syscall.h +++ b/src/user/include/pland/syscall.h @@ -2,6 +2,7 @@ #define PLAND_SYSCALL_H #include +#include typedef uint32_t _file_handle_t; typedef uint32_t _task_handle_t; @@ -120,8 +121,8 @@ static inline uint32_t _file_size(_file_handle_t handle) { return _sc1(_SCN_FILE_SIZE, handle); } -static inline void _start_task(_drive_number_t drive_number, char *path) { - _sc2(_SCN_START_TASK, drive_number, (uint32_t)path); +static inline bool _start_task(_drive_number_t drive_number, const char *path) { + return (bool)_sc2(_SCN_START_TASK, drive_number, (uint32_t)path); } static inline void _log_string(const char *sz) { diff --git a/src/user/init/main.c b/src/user/init/main.c index 9a411e7..c301a78 100644 --- a/src/user/init/main.c +++ b/src/user/init/main.c @@ -1,29 +1,31 @@ #include #include -#include +#include void main() { - tell_user_sz("\n\nThis is a userland program.\n"); - - tell_user_sz("Opening sd0:TEST.TXT.\n"); - struct file *f = open_file("sd0:TEST.TXT"); + struct file *f = open_file("SYS/STARTUP.RC"); if (!f) { - tell_user_sz("Failed to open.\n"); + tell_user_sz("\nCould not open SYS/STARTUP.RC"); return; } - tell_user_sz("Length: "); - uint32_t size = file_size(f); - tell_user_n(size); - tell_user_sz(" bytes\n\nContents:\n"); - - char *buf = get_block(size + 1); - read_from_file(f, size, buf); - buf[size] = '\0'; + char buf[1024]; + char *bufp = buf; + while (read_from_file(f, 1, bufp)) { + if (*bufp == '\n') { + if (bufp == buf) + continue; + *bufp = '\0'; + try_run_command(buf); + bufp = buf; + } + else + ++bufp; + } + if (bufp != buf) { + *bufp = '\0'; + try_run_command(buf); + } close_file(f); - - tell_user_sz(buf); - - tell_user_sz("\n\nGoodbye!\n"); } diff --git a/src/user/knob/file.c b/src/user/knob/file.c index 8ab7acd..0032cf2 100644 --- a/src/user/knob/file.c +++ b/src/user/knob/file.c @@ -9,9 +9,9 @@ struct file { uint32_t length; }; -static const char *try_remove_prefix(const char *path, uint8_t *dn_out) { +const char *remove_prefix(const char *path, uint8_t *dn_out) { if ((path[0] != 's') || (path[1] != 'd')) - return 0; + goto no_prefix; const char *num_part = path + 2; for (uint32_t i = 0; num_part[i]; ++i) @@ -19,22 +19,20 @@ static const char *try_remove_prefix(const char *path, uint8_t *dn_out) { uint32_t dn_large; if (!try_sntoi(num_part, i, &dn_large) || dn_large > 255) - return 0; + goto no_prefix; *dn_out = (uint8_t)dn_large; return num_part + i + 1; } - return 0; +no_prefix: + *dn_out = current_drive; + return path; } struct file *open_file(const char *path) { uint8_t dn; - const char *path_part = try_remove_prefix(path, &dn); - if (path_part) - path = path_part; - else - dn = current_drive; + path = remove_prefix(path, &dn); _file_handle_t h = _open_file(dn, path); if (!h) diff --git a/src/user/knob/task.c b/src/user/knob/task.c new file mode 100644 index 0000000..aa18eab --- /dev/null +++ b/src/user/knob/task.c @@ -0,0 +1,13 @@ +#include +#include +#include + +bool try_run_command(const char *path) { + uint8_t dn; + path = remove_prefix(path, &dn); + return _start_task(dn, path); +} + +void yield_task() { + _yield_task(); +} \ No newline at end of file -- cgit v1.2.3