diff options
author | Alex AUVOLAT <alexis211@gmail.com> | 2012-05-19 09:23:48 +0200 |
---|---|---|
committer | Alex AUVOLAT <alexis211@gmail.com> | 2012-05-19 09:23:48 +0200 |
commit | 7b466345af0d3a7dc5622617ce443a90c64e34a4 (patch) | |
tree | f276b6bf391ccfe8ec3a2cb62a7f70964249621f /src/user/yosh | |
parent | ac10c1a29c44b0cb29960cd0f792c7361bc430ce (diff) | |
download | TCE-7b466345af0d3a7dc5622617ce443a90c64e34a4.tar.gz TCE-7b466345af0d3a7dc5622617ce443a90c64e34a4.zip |
Added ANSI support, minimal readline-like library.
Diffstat (limited to 'src/user/yosh')
-rw-r--r-- | src/user/yosh/main.c | 200 |
1 files changed, 134 insertions, 66 deletions
diff --git a/src/user/yosh/main.c b/src/user/yosh/main.c index 7586ab0..d334d1e 100644 --- a/src/user/yosh/main.c +++ b/src/user/yosh/main.c @@ -2,17 +2,18 @@ #include <tce/vfs.h> #include <stdlib.h> #include <stdio.h> +#include <readline.h> FILE cwd_f; char *cwd; void about() { - print("Trivial/Computing Environment v0.1.0 - yosh 2.\n"); + print("Trivial/Computing Environment v0.1.0 - yosh 3.\n"); } void help(char *args[]) { if (args[1] == 0) { - print("Available commands: about, help, exit, ls, cd, switch.\n"); + print("Available commands: about, help, exit, ls, cd, goto.\n"); } else if (strcmp(args[1], "about") == 0) { print("Usage:\tabout\nShows some info about yosh. Very usefull.\n"); } else if (strcmp(args[1], "help") == 0) { @@ -25,8 +26,8 @@ void help(char *args[]) { print("Usage:\tcd <location>\nGoes to the location specified.\n"); } else if (strcmp(args[1], "ls") == 0) { print("Usage:\tls\n\tls <location>...\nShows the content of the current/specified directory.\n"); - } else if (strcmp(args[1], "switch") == 0) { - print("Usage:\tswitch <vt name>\nSwitchs focus to specified virtual terminal.\n"); + } else if (strcmp(args[1], "goto") == 0) { + print("Usage:\tgoto <vt name>\nSwitchs focus to specified virtual terminal.\n"); print("Type `ls /.ui` to see available terminals.\n"); } else { printf("No such command: %s\n", args[1]); @@ -82,7 +83,7 @@ void simplify_path(char* p) { } } -char* path_cat (char* a, char* b) { +char* path_cat (char* a, char* b, int trailing_slash) { char* ret; if (b[0] == '/') { int lb = strlen(b); @@ -111,6 +112,10 @@ char* path_cat (char* a, char* b) { ret[la + lb] = 0; } simplify_path(ret); + if (!trailing_slash) { + int l = strlen(ret); + if (ret[l-1] == '/') ret[l-1] = 0; + } return ret; } @@ -120,7 +125,7 @@ void cd(char **args) { } if (strcmp(args[1], ".") == 0) return; - char* newcwd = path_cat(cwd, args[1]); + char* newcwd = path_cat(cwd, args[1], 1); file_info info; int i = stat(newcwd, &info); @@ -135,7 +140,7 @@ void cd(char **args) { free(newcwd); } else { FILE nf = open(newcwd, 0); - if (nf <= 0) { + if (nf < 0) { printf("Error opening: %i\n", nf); free(newcwd); } else { @@ -186,16 +191,18 @@ void ls(char **args) { int i; for (i = 1; args[i] != 0; i++) { printf("Contents of %s :\n", args[i]); - char* d = path_cat(cwd, args[i]); + char* d = path_cat(cwd, args[i], 1); file_info info; int e = stat(d, &info); if (e == E_NOT_FOUND) { print(" No such file or directory\n"); } else if (e < 0) { printf(" Error stating: %i\n", e); + } else if ((info.type & FT_DIR) == 0) { + printf(" Not a directory.\n"); } else { FILE ff = open(d, 0); - if (ff <= 0) { + if (ff < 0) { printf(" Error opening: %i\n", ff); } else { ls_dir(ff); @@ -207,12 +214,35 @@ void ls(char **args) { } } -void t_switch(char** args) { +void cat(char **args) { + int i; + for (i = 1; args[i] != 0; i++) { + char *d = path_cat(cwd, args[i], 0); + file_info info; + int e = stat(d, &info); + if (e == E_NOT_FOUND) { + printf("No such file: %s\n", d); + } else if (e < 0) { + printf("Error stating %s : %i\n", d, e); + } else if ((info.type & FT_FILE) == 0) { + printf("Not a file: %s\n", d); + } else { + FILE ff = open(d, 0); + char* buff = (char*)malloc(info.size); + read(ff, 0, info.size, buff); + close(ff); + write(term, 0, info.size, buff); + free(buff); + } + } +} + +void t_goto(char** args) { if (args[1] == 0) { - print("Usage: switch <vt_name>\n"); + print("Usage: goto <vt_name>\n"); return; } - char* p = path_cat("/.ui/", args[1]); + char* p = path_cat("/.ui/", args[1], 0); int i; if ((i = link(p, "/.dev/vgatxt", LM_OUTPUT_TO)) == 0) { link("/.dev/ps2kbd", p, LM_OUTPUT_TO); @@ -222,78 +252,116 @@ void t_switch(char** args) { } int main(char **sh_args) { - term = open("/.ui/home", 0); - if (term <= 0) { - return -1; - } - about(); cwd = strdup("/"); cwd_f = open(cwd, 0); - char *path = path_cat(sh_args[0], ".."); - + char *path = path_cat(sh_args[0], "..", 1); + + int bg_pr[128], bg_pr_c = 0; + + readline_history h; h.str = 0; h.max = 4; + while (1) { - printf(" %s ", cwd); + printf("\x1b[33m %s \x1b[1m", cwd); - char *s = readln(); + char *s = readline(term, &h); + printf("\x1b[0m"); if (s == 0) return -1; - int l = strlen(s); - if (s[l-1] == '\n') { - s[l-1] = 0; l--; - - 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; - argc++; - if (argc == 15) break; - } + 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; + argc++; + if (argc == 15) break; + } + } + } + + if (strcmp(c_args[0], "about") == 0) { + about(); + } else if (strcmp(c_args[0], "help") == 0) { + help(c_args); + } else if (strcmp(c_args[0], "exit") == 0) { + print("Exiting the shell. See you later!\n"); + break; + } else if (strcmp(c_args[0], "cd") == 0) { + cd(c_args); + } else if (strcmp(c_args[0], "ls") == 0) { + ls(c_args); + } else if (strcmp(c_args[0], "goto") == 0) { + t_goto(c_args); + } else if (strcmp(c_args[0], "cat") == 0) { + cat(c_args); + } else { + FILE vt = term; + char *t = 0; + if (strcmp(c_args[0], "on") == 0) { + if (c_args[1] == 0 || c_args[2] == 0) { + printf("Usage:\ton <vt> <command>\n"); + continue; + } + t = path_cat("/.ui/", c_args[1], 0); + vt = open(t, 0); + if (vt < 0 || vt == term) { + 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++; } } - if (strcmp(c_args[0], "about") == 0) { - about(); - } else if (strcmp(c_args[0], "help") == 0) { - help(c_args); - } else if (strcmp(c_args[0], "exit") == 0) { - print("Exiting the shell. See you later!\n"); - break; - } else if (strcmp(c_args[0], "cd") == 0) { - cd(c_args); - } else if (strcmp(c_args[0], "ls") == 0) { - ls(c_args); - } else if (strcmp(c_args[0], "switch") == 0) { - t_switch(c_args); + char *c; + if (strchr(c_args[0], '/')) { + c = path_cat(cwd, c_args[0], 0); } else { - char *c; - if (strchr(c_args[0], '/')) { - c = path_cat(cwd, c_args[0]); + c = path_cat(path, c_args[0], 0); + } + int pid = run(c, c_args + 1, vt); + if (pid <= 0) { + if (pid == E_NOT_FOUND) { + printf("Error: no such file %s\n", c); } else { - c = path_cat(path, c_args[0]); + printf("Error %i\n", pid); } - int pid = run(c, c_args + 1); - if (pid <= 0) { - if (pid == E_NOT_FOUND) { - printf("Error: no such file %s\n", c); - } else { - printf("Error %i\n", pid); - } - } else { - int ret = waitpid(pid); + } else { + if (vt == term) { + int ret = waitpid(pid, 1); printf(" -> child (pid %i) exited with status %i\n", pid, ret); + } else { + printf(" -> running on terminal %s, pid:%i\n", t, pid); + bg_pr[bg_pr_c++] = pid; + free(t); } - free(c); } - } else { - print("[freadln fail, sorry.]\n"); + 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); + if (ret != E_NOT_FINISHED) { + printf(" -> child (pid %i) exited with status %i\n", bg_pr[i], ret); + bg_pr_c--; + bg_pr[i] = bg_pr[bg_pr_c]; + } else { + i++; + } } - free(s); } return 0; |