From 0d2f7645c3fb45d83497faf2a4b6fff8c3f175d1 Mon Sep 17 00:00:00 2001 From: Alex AUVOLAT Date: Sat, 19 May 2012 14:07:01 +0200 Subject: Added string class for FWIK. --- src/kernel/config.h | 4 +- src/user/app/init/main.cpp | 9 +- src/user/app/prime/main.cpp | 3 +- src/user/app/test/main.c | 8 +- src/user/app/yosh/main.cpp | 217 ++++++++++++++++---------------- src/user/lib/fwik/String.cpp | 157 +++++++++++++++++++++++ src/user/lib/fwik/include/IO/IOStream.h | 4 +- src/user/lib/fwik/include/IO/Term.h | 5 +- src/user/lib/fwik/include/String.h | 35 ++++++ src/user/lib/fwik/io/IOStream.cpp | 4 +- src/user/lib/fwik/io/Term.cpp | 11 +- src/user/lib/fwik/main.cpp | 11 +- src/user/lib/libc/include/string.h | 3 + src/user/lib/libc/std/stdio.c | 40 ------ src/user/lib/libc/std/string.c | 40 ++++++ 15 files changed, 380 insertions(+), 171 deletions(-) diff --git a/src/kernel/config.h b/src/kernel/config.h index 5a6c629..74d0933 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -2,7 +2,7 @@ #define DEF_CONFIG_H #define K_OS_NAME "T/CE" -#define K_OS_VER "0.1.2" -#define K_OS_CODENAME "Make Your Terminal ANSI" +#define K_OS_VER "0.1.3" +#define K_OS_CODENAME "FWIcKet" #endif diff --git a/src/user/app/init/main.cpp b/src/user/app/init/main.cpp index 4bb12da..759d804 100644 --- a/src/user/app/init/main.cpp +++ b/src/user/app/init/main.cpp @@ -1,16 +1,17 @@ #include #include +#include -int Main(char** args) { +int Main(String *args) { FILE home_term = libc::open("/.ui/home", 0); if (home_term < 0) return -1; stdio << "(init) Trivial/Computing Environment says hello. Press super to go home.\n"; - for (int i = 0; args[i] != 0; i++) { + for (int i = 0; args[i]; i++) { if (i == 0) continue; - stdio.printf("(init) Spawning %s...\n", args[i]); - int pid = libc::run(args[i], 0, home_term); + stdio.printf("(init) Spawning %s...\n", args[i].c_str()); + int pid = libc::run(args[i].c_str(), 0, home_term); if (pid < 0) { stdio << "(init) Error. Sorry.\n"; } else { diff --git a/src/user/app/prime/main.cpp b/src/user/app/prime/main.cpp index b2b08a8..be8780f 100644 --- a/src/user/app/prime/main.cpp +++ b/src/user/app/prime/main.cpp @@ -1,4 +1,5 @@ #include +#include bool is_prime(int i) { for (int j = 2; j*j <= i; j++) { @@ -7,7 +8,7 @@ bool is_prime(int i) { return true; } -int Main(char** args) { +int Main(String *args) { for (int i = 2; i < 1000000; i++) { if (is_prime(i)) stdio.printf("%d\t", i); } diff --git a/src/user/app/test/main.c b/src/user/app/test/main.c index 5940cae..e691d6f 100644 --- a/src/user/app/test/main.c +++ b/src/user/app/test/main.c @@ -35,7 +35,7 @@ void thread_cascade(void* d) { int main(char** args) { char**a; if (args != 0) { - printk("args"); + printk("(test) args"); for (a = args; *a != 0; a++) { printk(" - "); printk(*a); @@ -43,14 +43,14 @@ int main(char** args) { printk("\n"); } - printk(" -> Creating thread cascade (total 2**4 = 16 threads)\n"); - thread_new(thread_cascade, (void*)4); + printk("(test) Creating thread cascade (total 2**6 = 64 threads)\n"); + thread_new(thread_cascade, (void*)6); while (1) { thread_sleep(100); if (threads == 0) break; } - printk("\n -> Test process exiting. Press the super key to go to the home terminal.\n"); + printk("\n(test) Test process exiting. Press the super key to go to the home terminal.\n"); return 0; } diff --git a/src/user/app/yosh/main.cpp b/src/user/app/yosh/main.cpp index fe3eeed..6277701 100644 --- a/src/user/app/yosh/main.cpp +++ b/src/user/app/yosh/main.cpp @@ -5,8 +5,6 @@ #include #include -using namespace libc; - FILE cwd_f; char *cwd; @@ -14,26 +12,26 @@ void about() { stdio << "Trivial/Computing Environment v0.1.0 - yosh 3.\n"; } -void help(char *args[]) { - if (args[1] == 0) { +void help(String *args) { + if (!args[1]) { stdio << "Available commands: about, help, exit, ls, cd, goto.\n"; - } else if (strcmp(args[1], "about") == 0) { + } else if (args[1] == "about") { stdio << "Usage:\tabout\nShows some info about yosh. Very usefull.\n"; - } else if (strcmp(args[1], "help") == 0) { + } else if (args[1] == "help") { stdio << "Usage:\thelp\n\thelp \n" << "Shows some info about the command you want, or commands in general.\n"; - } else if (strcmp(args[1], "exit") == 0) { + } else if (args[1] == "exit") { stdio << "Usage:\texit\n" << "Exits the shell.\nWill probably make your system panic if you only have a shell running.\n"; - } else if (strcmp(args[1], "cd") == 0) { + } else if (args[1] == "cd") { stdio << "Usage:\tcd \nGoes to the location specified.\n"; - } else if (strcmp(args[1], "ls") == 0) { + } else if (args[1] == "ls") { stdio << "Usage:\tls\n\tls ...\nShows the content of the current/specified directory.\n"; - } else if (strcmp(args[1], "goto") == 0) { + } else if (args[1] == "goto") { stdio << "Usage:\tgoto \nSwitchs focus to specified virtual terminal.\n" << "Type `ls /.ui` to see available terminals.\n"; } else { - stdio.printf("No such command: %s\n", args[1]); + stdio.printf("No such command: %s\n", args[1].c_str()); } } @@ -52,14 +50,14 @@ void simplify_path(char* p) { } } else { *it = 0; - if (strcmp(member, ".") == 0) { + if (libc::strcmp(member, ".") == 0) { char *i = member; while (1) { i[0] = i[2]; if (i[0] == 0) break; i++; } - } else if (strcmp(member, "..") == 0) { + } else if (libc::strcmp(member, "..") == 0) { *it = '/'; char* start = member - 2; char* next = member + 3; @@ -86,28 +84,28 @@ void simplify_path(char* p) { } } -char* path_cat (char* a, char* b, int trailing_slash) { +char* path_cat (char* a, char* b, bool trailing_slash = true) { char* ret; if (b[0] == '/') { - int lb = strlen(b); + int lb = libc::strlen(b); ret = (char*)malloc(lb + 2); - memcpy(ret, b, lb); + libc::memcpy(ret, b, lb); if (ret[lb - 1] != '/') { ret[lb] = '/'; lb++; } ret[lb] = 0; } else { - int la = strlen(a); - int lb = strlen(b); + int la = libc::strlen(a); + int lb = libc::strlen(b); ret = (char*)malloc(la + lb + 3); - memcpy(ret, a, la); + libc::memcpy(ret, a, la); if (ret[la-1] != '/') { ret[la] = '/'; la++; } - memcpy(ret + la, b, lb); + libc::memcpy(ret + la, b, lb); if (ret[la + lb - 1] != '/') { ret[la + lb] = '/'; lb++; @@ -116,40 +114,40 @@ char* path_cat (char* a, char* b, int trailing_slash) { } simplify_path(ret); if (!trailing_slash) { - int l = strlen(ret); + int l = libc::strlen(ret); if (ret[l-1] == '/') ret[l-1] = 0; } return ret; } -void cd(char **args) { - if (args[1] == 0) { - print("Usage: cd \n"); +void cd(String *args) { + if (!args[1]) { + stdio << "Usage: cd \n"; } - if (strcmp(args[1], ".") == 0) return; + if (args[1] == ".") return; - char* newcwd = path_cat(cwd, args[1], 1); + char* newcwd = path_cat(cwd, args[1].c_str()); file_info info; - int i = stat(newcwd, &info); + int i = libc::stat(newcwd, &info); if (i == E_NOT_FOUND) { - print("No such file or directory.\n"); + stdio << "No such file or directory.\n"; free(newcwd); } else if (i < 0) { stdio.printf("Error stating: %i\n", i); free(newcwd); } else if ((info.type & FT_DIR) == 0) { - print("Not a directory.\n"); + stdio << "Not a directory.\n"; free(newcwd); } else { - FILE nf = open(newcwd, 0); + FILE nf = libc::open(newcwd, 0); if (nf < 0) { stdio.printf("Error opening: %i\n", nf); free(newcwd); } else { free(cwd); cwd = newcwd; - close(cwd_f); + libc::close(cwd_f); cwd_f = nf; } } @@ -159,57 +157,57 @@ void ls_dir(FILE f) { file_info info; int i = 0; char buf[256]; - while (read(f, i, 256, buf) > 0) { + while (libc::read(f, i, 256, buf) > 0) { i++; - if (strcmp(buf, ".") == 0 || strcmp(buf, "..") == 0) continue; + if (libc::strcmp(buf, ".") == 0 || libc::strcmp(buf, "..") == 0) continue; stdio.printf(" %s", buf); - stat_relative(f, buf, &info); - if (info.type & FT_DIR) print("/"); - print(" \t"); + libc::stat_relative(f, buf, &info); + if (info.type & FT_DIR) stdio << "/"; + stdio << " \t"; - if (info.type & FT_FILE) print("file "); - if (info.type & FT_DIR) print("dir "); - if (info.type & FT_SYMLINK) print("symlink "); - if (info.type & FT_DEV) print("dev "); - if (info.type & FT_TERMINAL) print("term "); + if (info.type & FT_FILE) stdio << "file "; + if (info.type & FT_DIR) stdio << "dir "; + if (info.type & FT_SYMLINK) stdio << "symlink "; + if (info.type & FT_DEV) stdio << "dev "; + if (info.type & FT_TERMINAL) stdio << "term "; - print("\t"); + stdio << "\t"; if (info.type & FT_TERMINAL) { stdio.printf("%ix%i", info.size >> 16, info.size & 0xFFFF); } else if ((info.type & FT_DEV) == 0) { stdio.printf("%i", info.size); } - print("\n"); + stdio << "\n"; } } -void ls(char **args) { - if (args[1] == 0) { +void ls(String *args) { + if (!args[1]) { ls_dir(cwd_f); } else { int i; - for (i = 1; args[i] != 0; i++) { - stdio.printf("Contents of %s :\n", args[i]); - char* d = path_cat(cwd, args[i], 1); + for (i = 1; args[i]; i++) { + stdio.printf("Contents of %s :\n", args[i].c_str()); + char* d = path_cat(cwd, args[i].c_str()); file_info info; - int e = stat(d, &info); + int e = libc::stat(d, &info); if (e == E_NOT_FOUND) { - print(" No such file or directory\n"); + stdio << " No such file or directory\n"; } else if (e < 0) { stdio.printf(" Error stating: %i\n", e); } else if ((info.type & FT_DIR) == 0) { stdio.printf(" Not a directory.\n"); } else { - FILE ff = open(d, 0); + FILE ff = libc::open(d, 0); if (ff < 0) { stdio.printf(" Error opening: %i\n", ff); } else { ls_dir(ff); - close(ff); + libc::close(ff); } } free(d); @@ -217,12 +215,12 @@ void ls(char **args) { } } -void cat(char **args) { +void cat(String *args) { int i; - for (i = 1; args[i] != 0; i++) { - char *d = path_cat(cwd, args[i], 0); + for (i = 1; args[i]; i++) { + char *d = path_cat(cwd, args[i].c_str(), false); file_info info; - int e = stat(d, &info); + int e = libc::stat(d, &info); if (e == E_NOT_FOUND) { stdio.printf("No such file: %s\n", d); } else if (e < 0) { @@ -230,37 +228,37 @@ void cat(char **args) { } else if ((info.type & FT_FILE) == 0) { stdio.printf("Not a file: %s\n", d); } else { - FILE ff = open(d, 0); + FILE ff = libc::open(d, 0); char* buff = (char*)malloc(info.size); - read(ff, 0, info.size, buff); - close(ff); - write(term, 0, info.size, buff); + libc::read(ff, 0, info.size, buff); + libc::close(ff); + libc::write(stdio.term->fd, 0, info.size, buff); free(buff); } } } -void t_goto(char** args) { - if (args[1] == 0) { - print("Usage: goto \n"); +void t_goto(String *args) { + if (!args[1]) { + stdio << "Usage: goto \n"; return; } - char* p = path_cat("/.ui/", args[1], 0); + char* p = path_cat("/.ui/", args[1].c_str(), false); int i; - if ((i = link(p, "/.dev/vgatxt", LM_OUTPUT_TO)) == 0) { - link("/.dev/ps2kbd", p, LM_OUTPUT_TO); + if ((i = libc::link(p, "/.dev/vgatxt", LM_OUTPUT_TO)) == 0) { + libc::link("/.dev/ps2kbd", p, LM_OUTPUT_TO); } else { stdio.printf("Error %i\n", i); } } -int Main(char **sh_args) { +int Main(String *sh_args) { about(); - cwd = strdup("/"); - cwd_f = open(cwd, 0); + cwd = libc::strdup("/"); + cwd_f = libc::open(cwd, 0); - char *path = path_cat(sh_args[0], "..", 1); + char *path = path_cat(sh_args[0].c_str(), ".."); int bg_pr[128], bg_pr_c = 0; @@ -273,70 +271,73 @@ int Main(char **sh_args) { while (1) { stdio.printf("\x1b[33m %s \x1b[1m", cwd); - char *s = term->readline(); + String s = term->readline(); stdio.printf("\x1b[0m"); - if (s == 0) return -1; - if (*s == 0) continue; - - char *c_args[16] = {0}, *cmd; - c_args[0] = s; - int argc = 1; - for (cmd = s; *cmd != 0; cmd++) { - if (*cmd == ' ') { - *cmd = 0; - if (cmd[1] != ' ' && cmd[1] != 0) { - c_args[argc] = cmd + 1; + if (s.size() == 0) continue; + + String c_args[16]; + int argc = 0, start = 0; + for (int i = 0; i < s.size(); i++) { + if (s[i] == ' ') { + if (start == i) { + start++; + } else { + c_args[argc] = s.substr(start, i - start); argc++; - if (argc == 15) break; + start = i + 1; + if (argc == 15) { + break; + } } } } + c_args[argc++] = s.substr(start, s.size() - start); - if (strcmp(c_args[0], "about") == 0) { + if (c_args[0] == "about") { about(); - } else if (strcmp(c_args[0], "help") == 0) { + } else if (c_args[0] == "help") { help(c_args); - } else if (strcmp(c_args[0], "exit") == 0) { - print("Exiting the shell. See you later!\n"); + } else if (c_args[0] == "exit") { + stdio << "Exiting the shell. See you later!\n"; break; - } else if (strcmp(c_args[0], "cd") == 0) { + } else if (c_args[0] == "cd") { cd(c_args); - } else if (strcmp(c_args[0], "ls") == 0) { + } else if (c_args[0] == "ls") { ls(c_args); - } else if (strcmp(c_args[0], "goto") == 0) { + } else if (c_args[0] == "goto") { t_goto(c_args); - } else if (strcmp(c_args[0], "cat") == 0) { + } else if (c_args[0] == "cat") { cat(c_args); } else { FILE vt = term->fd; char *t = 0; - if (strcmp(c_args[0], "on") == 0) { - if (c_args[1] == 0 || c_args[2] == 0) { + String *first_arg = c_args + 1; + + if (c_args[0] == "on") { + if (!c_args[1] || !c_args[2]) { stdio.printf("Usage:\ton \n"); continue; } - t = path_cat("/.ui/", c_args[1], 0); - vt = open(t, 0); + t = path_cat("/.ui/", c_args[1].c_str(), false); + vt = libc::open(t, 0); if (vt < 0 || vt == term->fd) { stdio.printf("Error: cannot open terminal %s (%i)\n", t, vt); free(t); continue; } - int i = 0; - while (1) { - c_args[i] = c_args[i+2]; - if (c_args[i] == 0) break; - i++; - } + first_arg += 2; } char *c; - if (strchr(c_args[0], '/')) { - c = path_cat(cwd, c_args[0], 0); + if (libc::strchr(c_args[0].c_str(), '/')) { + c = path_cat(cwd, c_args[0].c_str(), false); } else { - c = path_cat(path, c_args[0], 0); + c = path_cat(path, c_args[0].c_str(), false); } - int pid = run(c, c_args + 1, vt); + char *run_args[15] = {0}; + for (int i = 0; first_arg[i]; i++) run_args[i] = first_arg[i].c_str(); + + int pid = libc::run(c, run_args, vt); if (pid <= 0) { if (pid == E_NOT_FOUND) { stdio.printf("Error: no such file %s\n", c); @@ -345,7 +346,7 @@ int Main(char **sh_args) { } } else { if (vt == term->fd) { - int ret = waitpid(pid, 1); + int ret = libc::waitpid(pid, 1); stdio.printf("(yosh) child (pid %i) exited with status %i\n", pid, ret); } else { stdio.printf("(yosh) running on terminal %s, pid:%i\n", t, pid); @@ -356,11 +357,9 @@ int Main(char **sh_args) { free(c); } - for (; s < cmd; s++) if (*s == 0) *s = ' '; - int i = 0; while (i < bg_pr_c) { - int ret = waitpid(bg_pr[i], 0); + int ret = libc::waitpid(bg_pr[i], 0); if (ret != E_NOT_FINISHED) { stdio.printf("(yosh) child (pid %i) exited with status %i\n", bg_pr[i], ret); bg_pr_c--; diff --git a/src/user/lib/fwik/String.cpp b/src/user/lib/fwik/String.cpp index 75114c2..982ce4e 100644 --- a/src/user/lib/fwik/String.cpp +++ b/src/user/lib/fwik/String.cpp @@ -1 +1,158 @@ #include +#include + +String::String() { + ptr = 0; + len = 0; +} + +String::String(const String &other) { + len = other.len; + if (len == 0) { + ptr = 0; + } else { + ptr = (char*)malloc(len + 1); + libc::memcpy(ptr, other.ptr, len + 1); + } +} + +String::String(char* from) { + len = (from == 0 ? 0 : libc::strlen(from)); + if (len == 0) { + ptr = 0; + } else { + ptr = (char*)malloc(len + 1); + libc::memcpy(ptr, from, len + 1); + } +} + +String::String(char* from, int l) { + len = l; + if (len < 0) len = 0; + if (len == 0) { + ptr = 0; + } else { + ptr = (char*)malloc(len + 1); + libc::memcpy(ptr, from, len); + ptr[len] = 0; + } +} + +String::String(char c, int count) { + len = count; + if (len == 0) { + ptr = 0; + } else { + ptr = (char*)malloc(len + 1); + libc::memset(ptr, c, len); + ptr[len] = 0; + } +} + +String::~String() { + if (ptr != 0) free(ptr); +} + +void String::operator=(const String &other) { + if (ptr != 0) free(ptr); + len = other.len; + if (len == 0) { + ptr = 0; + } else { + ptr = (char*)malloc(len + 1); + libc::memcpy(ptr, other.ptr, len + 1); + } +} + +void String::operator=(char* from) { + if (ptr != 0) free(ptr); + len = (from == 0 ? 0 : libc::strlen(from)); + if (len == 0) { + ptr = 0; + } else { + ptr = (char*)malloc(len + 1); + libc::memcpy(ptr, from, len + 1); + } +} + +char* String::c_str() { + if (ptr == 0) return ""; + return ptr; +} + +bool String::operator==(const String& other) { + if (len != other.len) return false; + for (int i = 0; i < len; i++) if (ptr[i] != other.ptr[i]) return false; + return true; +} + +bool String::operator==(char* other) { + if (other == 0) return (len == 0); + if (len != libc::strlen(other)) return false; + for (int i = 0; i < len; i++) if (ptr[i] != other[i]) return false; + return true; +} + +bool String::operator<(const String& other) { + for (int i = 0; i < len && i < other.len; i++) { + if (ptr[i] > other.ptr[i]) return false; + if (ptr[i] < other.ptr[i]) return true; + } + if (len < other.len) return true; + return false; +} + +static char crap; +char &String::operator[](int pos) { + if (pos >= 0 && pos < len) return ptr[pos]; + crap = 0; + return crap; +} + +String String::substr(int start, int count) { + if (start + count > len) count = len - start; + return String(ptr + start, count); +} + +String String::operator+(const String& other) { + String ret(' ', len + other.len); + libc::memcpy(ret.ptr, ptr, len); + libc::memcpy(ret.ptr + len, other.ptr, other.len); + return ret; +} + +void String::operator+=(const String& other) { + if (other.len == 0) return; + int newlen = len + other.len; + char* newptr = (char*)malloc(newlen + 1); + libc::memcpy(newptr, ptr, len); + libc::memcpy(newptr+len, other.ptr, other.len); + newptr[newlen] = 0; + free(ptr); + ptr = newptr; + len = newlen; +} + +void String::operator+=(char c) { + char* newptr = (char*)malloc(len + 2); + libc::memcpy(newptr, ptr, len); + newptr[len] = c; + len++; + newptr[len] = 0; + free(ptr); + ptr = newptr; +} + +String String::dec(int i) { + char b[16]; + const char *e = libc::format_int(b, i); + return String(b, e - b); +} + +String String::hex(uint32_t v) { + char b[16]; + const char *e = libc::format_hex(b, v); + return String(b, e - b); +} + + diff --git a/src/user/lib/fwik/include/IO/IOStream.h b/src/user/lib/fwik/include/IO/IOStream.h index e9ae246..e381cf5 100644 --- a/src/user/lib/fwik/include/IO/IOStream.h +++ b/src/user/lib/fwik/include/IO/IOStream.h @@ -3,6 +3,8 @@ #include "Term.h" +#include + class IOStream { public: Term *term; @@ -12,7 +14,7 @@ class IOStream { void print(char* str); void printf(char* fmt, ...); - char* readln(); + String readln(); IOStream &operator<<(char* s) { print(s); diff --git a/src/user/lib/fwik/include/IO/Term.h b/src/user/lib/fwik/include/IO/Term.h index cf90789..4fd9306 100644 --- a/src/user/lib/fwik/include/IO/Term.h +++ b/src/user/lib/fwik/include/IO/Term.h @@ -3,6 +3,7 @@ #include #include "Node.h" +#include #include @@ -22,8 +23,8 @@ class Term : public Node { virtual void print(char *s); virtual void printf(char* fmt, ...); virtual void vprintf(char* fmt, va_list ap); - virtual char* readln(); - char* readline(); + virtual String readln(); + String readline(); virtual Term* as_term() { return this; } }; diff --git a/src/user/lib/fwik/include/String.h b/src/user/lib/fwik/include/String.h index 2a44046..672220d 100644 --- a/src/user/lib/fwik/include/String.h +++ b/src/user/lib/fwik/include/String.h @@ -1,5 +1,40 @@ #ifndef DEF_FWIK_STRING_H #define DEF_FWIK_STRING_H +#include + +class String { + private: + char *ptr; // zero-terminated for internal purposes. + int len; + + public: + String(); + String(const String& other); + String(char* ptr); + String(char* ptr, int len); + String(char c, int count); + ~String(); + void operator=(const String &string); + void operator=(char* ptr); + + char* c_str(); + + bool operator==(const String& other); + bool operator==(char* other); + bool operator<(const String& other); + char &operator[](int pos); + + int size() { return len; } + operator bool() { return len != 0; } + String substr(int start, int count); + + String operator+(const String& other); + void operator+=(const String& other); + void operator+=(char c); + + static String dec(int i); + static String hex(uint32_t v); +}; #endif diff --git a/src/user/lib/fwik/io/IOStream.cpp b/src/user/lib/fwik/io/IOStream.cpp index bcaa581..ae495c0 100644 --- a/src/user/lib/fwik/io/IOStream.cpp +++ b/src/user/lib/fwik/io/IOStream.cpp @@ -13,7 +13,7 @@ void IOStream::printf(char* fmt, ...) { va_end(ap); } -char* IOStream::readln() { - if (term == 0) return 0; +String IOStream::readln() { + if (term == 0) return ""; return term->readln(); } diff --git a/src/user/lib/fwik/io/Term.cpp b/src/user/lib/fwik/io/Term.cpp index 1c6db66..f7f28ec 100644 --- a/src/user/lib/fwik/io/Term.cpp +++ b/src/user/lib/fwik/io/Term.cpp @@ -42,10 +42,13 @@ void Term::vprintf(char* fmt, va_list ap) { libc::vfprintf(fd, fmt, ap); } -char* Term::readln() { - return libc::freadln(fd); +String Term::readln() { + char *s = libc::freadln(fd); + String ret(s); + free(s); + return ret; } -char *Term::readline() { - return libc::freadline(fd, &hist); +String Term::readline() { + return String(libc::freadline(fd, &hist)); } diff --git a/src/user/lib/fwik/main.cpp b/src/user/lib/fwik/main.cpp index 8e2e289..e26cb46 100644 --- a/src/user/lib/fwik/main.cpp +++ b/src/user/lib/fwik/main.cpp @@ -1,10 +1,11 @@ #include #include #include +#include IOStream stdio; -int Main(char **args); // FWIK app main +int Main(String *args); // FWIK app main extern "C" int main(char **args) { stdio.term = 0; @@ -13,7 +14,13 @@ extern "C" int main(char **args) { if (zero.info.type & FT_TERMINAL) { stdio.term = new Term(zero); } - Main(args); + + int argc = 0; + while (args[argc] != 0) argc++; + String s_args[argc+1]; + for (int i = 0; i < argc; i++) s_args[i] = args[i]; + + Main(s_args); } diff --git a/src/user/lib/libc/include/string.h b/src/user/lib/libc/include/string.h index fbe3ade..54fdf7a 100644 --- a/src/user/lib/libc/include/string.h +++ b/src/user/lib/libc/include/string.h @@ -18,6 +18,9 @@ char *strchr(const char *str, int c); char *strcat(char *dest, const char *src); int strcmp(const char *s1, const char *s2); +char* format_int(char* buf, int number); +char* format_hex(char *buf, unsigned v); + #ifdef __cplusplus } } #endif diff --git a/src/user/lib/libc/std/stdio.c b/src/user/lib/libc/std/stdio.c index 3595622..da2f094 100644 --- a/src/user/lib/libc/std/stdio.c +++ b/src/user/lib/libc/std/stdio.c @@ -20,46 +20,6 @@ void fprintf(FILE f, char* format, ...) { va_end(ap); } -// INTERNAL, FOR FORMATTING - -static char* format_int(char* buf, int number) { - if (number == 0) { - *(buf++) = '0'; - return buf; - } - if (number < 0) { - *(buf++) = '-'; - number = 0 - number; - } - - int order = 0, temp = number, i; - char numbers[] = "0123456789"; - while (temp > 0) { - order++; - temp /= 10; - } - - for (i = order; i > 0; i--) { - buf[i - 1] = numbers[number % 10]; - number /= 10; - } - return buf + order; -} - -static char* format_hex(char *buf, unsigned v) { - *(buf++) = '0'; - *(buf++) = 'x'; - - int i; - char hexdigits[] = "0123456789ABCDEF"; - for (i = 0; i < 8; i++) { - *(buf++) = hexdigits[v >> 28]; - v = v << 4; - } - return buf; -} - - // FUNCTIONS void fprint(FILE f, char *s) { diff --git a/src/user/lib/libc/std/string.c b/src/user/lib/libc/std/string.c index 5a41b7e..d8e4a48 100644 --- a/src/user/lib/libc/std/string.c +++ b/src/user/lib/libc/std/string.c @@ -79,3 +79,43 @@ uint16_t *memsetw(uint16_t *dest, uint16_t val, int count) { } return dest; } + + +// Formatting + +char* format_int(char* buf, int number) { + if (number == 0) { + *(buf++) = '0'; + return buf; + } + if (number < 0) { + *(buf++) = '-'; + number = 0 - number; + } + + int order = 0, temp = number, i; + char numbers[] = "0123456789"; + while (temp > 0) { + order++; + temp /= 10; + } + + for (i = order; i > 0; i--) { + buf[i - 1] = numbers[number % 10]; + number /= 10; + } + return buf + order; +} + +char* format_hex(char *buf, unsigned v) { + *(buf++) = '0'; + *(buf++) = 'x'; + + int i; + char hexdigits[] = "0123456789ABCDEF"; + for (i = 0; i < 8; i++) { + *(buf++) = hexdigits[v >> 28]; + v = v << 4; + } + return buf; +} -- cgit v1.2.3