diff options
author | Alex AUVOLAT <alexis211@gmail.com> | 2012-05-19 15:56:25 +0200 |
---|---|---|
committer | Alex AUVOLAT <alexis211@gmail.com> | 2012-05-19 15:56:25 +0200 |
commit | 8e07c1db6ba4bedd0f8fe537a6fb0ca80e5d25f4 (patch) | |
tree | 08df626a28c290f5cde43424dd62dd43fc441a35 /src/user/app | |
parent | 0d2f7645c3fb45d83497faf2a4b6fff8c3f175d1 (diff) | |
download | TCE-8e07c1db6ba4bedd0f8fe537a6fb0ca80e5d25f4.tar.gz TCE-8e07c1db6ba4bedd0f8fe537a6fb0ca80e5d25f4.zip |
Better sprintf, vsprintf, String::sprintf, etc.
Diffstat (limited to 'src/user/app')
-rw-r--r-- | src/user/app/yosh/main.cpp | 156 |
1 files changed, 31 insertions, 125 deletions
diff --git a/src/user/app/yosh/main.cpp b/src/user/app/yosh/main.cpp index 6277701..37b557d 100644 --- a/src/user/app/yosh/main.cpp +++ b/src/user/app/yosh/main.cpp @@ -6,7 +6,7 @@ #include <IO/IOStream.h> FILE cwd_f; -char *cwd; +String cwd; void about() { stdio << "Trivial/Computing Environment v0.1.0 - yosh 3.\n"; @@ -35,117 +35,27 @@ void help(String *args) { } } -void simplify_path(char* p) { - char *it = p; - char *member = it; - while (*it != 0) { - if (*it == '/') { - if (it == member && it != p) { - // two consecutive slashes - char *i = member; - while (1) { - i[0] = i[1]; - if (i[0] == 0) break; - i++; - } - } else { - *it = 0; - if (libc::strcmp(member, ".") == 0) { - char *i = member; - while (1) { - i[0] = i[2]; - if (i[0] == 0) break; - i++; - } - } else if (libc::strcmp(member, "..") == 0) { - *it = '/'; - char* start = member - 2; - char* next = member + 3; - while (start > p && *start != '/') { - start--; - } - start++; - it = member = start; - while (1) { - *start = *next; - if (*start == 0) break; - start++; - next++; - } - } else { - *it = '/'; - it++; - member = it; - } - } - } else { - it++; - } - } -} - -char* path_cat (char* a, char* b, bool trailing_slash = true) { - char* ret; - if (b[0] == '/') { - int lb = libc::strlen(b); - ret = (char*)malloc(lb + 2); - libc::memcpy(ret, b, lb); - if (ret[lb - 1] != '/') { - ret[lb] = '/'; - lb++; - } - ret[lb] = 0; - } else { - int la = libc::strlen(a); - int lb = libc::strlen(b); - - ret = (char*)malloc(la + lb + 3); - libc::memcpy(ret, a, la); - if (ret[la-1] != '/') { - ret[la] = '/'; - la++; - } - libc::memcpy(ret + la, b, lb); - if (ret[la + lb - 1] != '/') { - ret[la + lb] = '/'; - lb++; - } - ret[la + lb] = 0; - } - simplify_path(ret); - if (!trailing_slash) { - int l = libc::strlen(ret); - if (ret[l-1] == '/') ret[l-1] = 0; - } - return ret; -} - void cd(String *args) { if (!args[1]) { stdio << "Usage: cd <directory>\n"; } if (args[1] == ".") return; - char* newcwd = path_cat(cwd, args[1].c_str()); + String newcwd = path_cat(cwd, args[1]); file_info info; - int i = libc::stat(newcwd, &info); + int i = libc::stat(newcwd.c_str(), &info); if (i == E_NOT_FOUND) { 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) { stdio << "Not a directory.\n"; - free(newcwd); } else { - FILE nf = libc::open(newcwd, 0); + FILE nf = libc::open(newcwd.c_str(), 0); if (nf < 0) { stdio.printf("Error opening: %i\n", nf); - free(newcwd); } else { - free(cwd); cwd = newcwd; libc::close(cwd_f); cwd_f = nf; @@ -192,9 +102,9 @@ void ls(String *args) { int i; 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()); + String d = path_cat(cwd, args[i].c_str()); file_info info; - int e = libc::stat(d, &info); + int e = libc::stat(d.c_str(), &info); if (e == E_NOT_FOUND) { stdio << " No such file or directory\n"; } else if (e < 0) { @@ -202,7 +112,7 @@ void ls(String *args) { } else if ((info.type & FT_DIR) == 0) { stdio.printf(" Not a directory.\n"); } else { - FILE ff = libc::open(d, 0); + FILE ff = libc::open(d.c_str(), 0); if (ff < 0) { stdio.printf(" Error opening: %i\n", ff); } else { @@ -210,7 +120,6 @@ void ls(String *args) { libc::close(ff); } } - free(d); } } } @@ -218,17 +127,17 @@ void ls(String *args) { void cat(String *args) { int i; for (i = 1; args[i]; i++) { - char *d = path_cat(cwd, args[i].c_str(), false); + String d = path_cat(cwd, args[i], false); file_info info; - int e = libc::stat(d, &info); + int e = libc::stat(d.c_str(), &info); if (e == E_NOT_FOUND) { - stdio.printf("No such file: %s\n", d); + stdio.printf("No such file: %s\n", d.c_str()); } else if (e < 0) { - stdio.printf("Error stating %s : %i\n", d, e); + stdio.printf("Error stating %s : %i\n", d.c_str(), e); } else if ((info.type & FT_FILE) == 0) { - stdio.printf("Not a file: %s\n", d); + stdio.printf("Not a file: %s\n", d.c_str()); } else { - FILE ff = libc::open(d, 0); + FILE ff = libc::open(d.c_str(), 0); char* buff = (char*)malloc(info.size); libc::read(ff, 0, info.size, buff); libc::close(ff); @@ -243,10 +152,10 @@ void t_goto(String *args) { stdio << "Usage: goto <vt_name>\n"; return; } - char* p = path_cat("/.ui/", args[1].c_str(), false); + String p = path_cat("/.ui/", args[1], false); int i; - if ((i = libc::link(p, "/.dev/vgatxt", LM_OUTPUT_TO)) == 0) { - libc::link("/.dev/ps2kbd", p, LM_OUTPUT_TO); + if ((i = libc::link(p.c_str(), "/.dev/vgatxt", LM_OUTPUT_TO)) == 0) { + libc::link("/.dev/ps2kbd", p.c_str(), LM_OUTPUT_TO); } else { stdio.printf("Error %i\n", i); } @@ -255,10 +164,10 @@ void t_goto(String *args) { int Main(String *sh_args) { about(); - cwd = libc::strdup("/"); - cwd_f = libc::open(cwd, 0); + cwd = "/"; + cwd_f = libc::open(cwd.c_str(), 0); - char *path = path_cat(sh_args[0].c_str(), ".."); + String path = path_cat(sh_args[0], ".."); int bg_pr[128], bg_pr_c = 0; @@ -269,7 +178,7 @@ int Main(String *sh_args) { } while (1) { - stdio.printf("\x1b[33m %s \x1b[1m", cwd); + stdio.printf("\x1b[33m %s \x1b[1m", cwd.c_str()); String s = term->readline(); stdio.printf("\x1b[0m"); @@ -310,37 +219,36 @@ int Main(String *sh_args) { cat(c_args); } else { FILE vt = term->fd; - char *t = 0; String *first_arg = c_args + 1; + String t; if (c_args[0] == "on") { if (!c_args[1] || !c_args[2]) { stdio.printf("Usage:\ton <vt> <command>\n"); continue; } - t = path_cat("/.ui/", c_args[1].c_str(), false); - vt = libc::open(t, 0); + t = path_cat("/.ui/", c_args[1], false); + vt = libc::open(t.c_str(), 0); if (vt < 0 || vt == term->fd) { - stdio.printf("Error: cannot open terminal %s (%i)\n", t, vt); - free(t); + stdio.printf("Error: cannot open terminal %s (%i)\n", t.c_str(), vt); continue; } first_arg += 2; } - char *c; + String c; if (libc::strchr(c_args[0].c_str(), '/')) { - c = path_cat(cwd, c_args[0].c_str(), false); + c = path_cat(cwd, c_args[0], false); } else { - c = path_cat(path, c_args[0].c_str(), false); + c = path_cat(path, c_args[0], false); } - char *run_args[15] = {0}; + const 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); + int pid = libc::run(c.c_str(), run_args, vt); if (pid <= 0) { if (pid == E_NOT_FOUND) { - stdio.printf("Error: no such file %s\n", c); + stdio.printf("Error: no such file %s\n", c.c_str()); } else { stdio.printf("Error %i\n", pid); } @@ -349,12 +257,10 @@ int Main(String *sh_args) { 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); + stdio.printf("(yosh) running on terminal %s, pid:%i\n", t.c_str(), pid); bg_pr[bg_pr_c++] = pid; - free(t); } } - free(c); } int i = 0; |