diff options
Diffstat (limited to 'src/user/yosh/main.c')
-rw-r--r-- | src/user/yosh/main.c | 107 |
1 files changed, 61 insertions, 46 deletions
diff --git a/src/user/yosh/main.c b/src/user/yosh/main.c index 957060e..7586ab0 100644 --- a/src/user/yosh/main.c +++ b/src/user/yosh/main.c @@ -3,33 +3,33 @@ #include <stdlib.h> #include <stdio.h> -FILE out, cwd_f; +FILE cwd_f; char *cwd; void about() { - fprint(out, "Trivial/Computing Environment v0.1.0 - yosh 2.\n"); + print("Trivial/Computing Environment v0.1.0 - yosh 2.\n"); } void help(char *args[]) { if (args[1] == 0) { - fprint(out, "Available commands: about, help, exit, ls, cd, switch.\n"); + print("Available commands: about, help, exit, ls, cd, switch.\n"); } else if (strcmp(args[1], "about") == 0) { - fprint(out, "Usage:\tabout\nShows some info about yosh. Very usefull.\n"); + print("Usage:\tabout\nShows some info about yosh. Very usefull.\n"); } else if (strcmp(args[1], "help") == 0) { - fprint(out, "Usage:\thelp\n\thelp <command>\n"); - fprint(out, "Shows some info about the command you want, or commands in general.\n"); + print("Usage:\thelp\n\thelp <command>\n"); + print("Shows some info about the command you want, or commands in general.\n"); } else if (strcmp(args[1], "exit") == 0) { - fprint(out, "Usage:\texit\n"); - fprint(out, "Exits the shell.\nWill probably make your system panic if you only have a shell running.\n"); + print("Usage:\texit\n"); + print("Exits the shell.\nWill probably make your system panic if you only have a shell running.\n"); } else if (strcmp(args[1], "cd") == 0) { - fprint(out, "Usage:\tcd <location>\nGoes to the location specified.\n"); + print("Usage:\tcd <location>\nGoes to the location specified.\n"); } else if (strcmp(args[1], "ls") == 0) { - fprint(out, "Usage:\tls\n\tls <location>...\nShows the content of the current/specified directory.\n"); + print("Usage:\tls\n\tls <location>...\nShows the content of the current/specified directory.\n"); } else if (strcmp(args[1], "switch") == 0) { - fprint(out, "Usage:\tswitch <vt name>\nSwitchs focus to specified virtual terminal.\n"); - fprint(out, "Type `ls /.ui` to see available terminals.\n"); + print("Usage:\tswitch <vt name>\nSwitchs focus to specified virtual terminal.\n"); + print("Type `ls /.ui` to see available terminals.\n"); } else { - fprintf(out, "No such command: %s\n", args[1]); + printf("No such command: %s\n", args[1]); } } @@ -97,8 +97,12 @@ char* path_cat (char* a, char* b) { int la = strlen(a); int lb = strlen(b); - ret = (char*)malloc(la + lb + 2); + ret = (char*)malloc(la + lb + 3); memcpy(ret, a, la); + if (ret[la-1] != '/') { + ret[la] = '/'; + la++; + } memcpy(ret + la, b, lb); if (ret[la + lb - 1] != '/') { ret[la + lb] = '/'; @@ -112,7 +116,7 @@ char* path_cat (char* a, char* b) { void cd(char **args) { if (args[1] == 0) { - fprint(out, "Usage: cd <directory>\n"); + print("Usage: cd <directory>\n"); } if (strcmp(args[1], ".") == 0) return; @@ -121,18 +125,18 @@ void cd(char **args) { file_info info; int i = stat(newcwd, &info); if (i == E_NOT_FOUND) { - fprint(out, "No such file or directory.\n"); + print("No such file or directory.\n"); free(newcwd); } else if (i < 0) { - fprintf(out, "Error stating: %i\n", i); + printf("Error stating: %i\n", i); free(newcwd); } else if ((info.type & FT_DIR) == 0) { - fprint(out, "Not a directory.\n"); + print("Not a directory.\n"); free(newcwd); } else { FILE nf = open(newcwd, 0); if (nf <= 0) { - fprintf(out, "Error opening: %i\n", nf); + printf("Error opening: %i\n", nf); free(newcwd); } else { free(cwd); @@ -152,26 +156,26 @@ void ls_dir(FILE f) { if (strcmp(buf, ".") == 0 || strcmp(buf, "..") == 0) continue; - fprintf(out, " %s", buf); + printf(" %s", buf); stat_relative(f, buf, &info); - if (info.type & FT_DIR) fprint(out, "/"); - fprint(out, " \t"); + if (info.type & FT_DIR) print("/"); + print(" \t"); - if (info.type & FT_FILE) fprint(out, "file "); - if (info.type & FT_DIR) fprint(out, "dir "); - if (info.type & FT_SYMLINK) fprint(out, "symlink "); - if (info.type & FT_DEV) fprint(out, "dev "); - if (info.type & FT_TERMINAL) fprint(out, "term "); + 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 "); - fprint(out, "\t"); + print("\t"); if (info.type & FT_TERMINAL) { - fprintf(out, "%ix%i", info.size >> 16, info.size & 0xFFFF); + printf("%ix%i", info.size >> 16, info.size & 0xFFFF); } else if ((info.type & FT_DEV) == 0) { - fprintf(out, "%i", info.size); + printf("%i", info.size); } - fprint(out, "\n"); + print("\n"); } } @@ -181,18 +185,18 @@ void ls(char **args) { } else { int i; for (i = 1; args[i] != 0; i++) { - fprintf(out, "Contents of %s :\n", args[i]); + printf("Contents of %s :\n", args[i]); char* d = path_cat(cwd, args[i]); file_info info; int e = stat(d, &info); if (e == E_NOT_FOUND) { - fprint(out, " No such file or directory\n"); + print(" No such file or directory\n"); } else if (e < 0) { - fprintf(out, " Error stating: %i\n", e); + printf(" Error stating: %i\n", e); } else { FILE ff = open(d, 0); if (ff <= 0) { - fprintf(out, " Error opening: %i\n", ff); + printf(" Error opening: %i\n", ff); } else { ls_dir(ff); close(ff); @@ -205,7 +209,7 @@ void ls(char **args) { void t_switch(char** args) { if (args[1] == 0) { - fprint(out, "Usage: switch <vt_name>\n"); + print("Usage: switch <vt_name>\n"); return; } char* p = path_cat("/.ui/", args[1]); @@ -213,13 +217,13 @@ void t_switch(char** args) { if ((i = link(p, "/.dev/vgatxt", LM_OUTPUT_TO)) == 0) { link("/.dev/ps2kbd", p, LM_OUTPUT_TO); } else { - fprintf(out, "Error %i\n", i); + printf("Error %i\n", i); } } int main(char **sh_args) { - out = open("/.ui/home", 0); - if (out <= 0) { + term = open("/.ui/home", 0); + if (term <= 0) { return -1; } @@ -227,11 +231,13 @@ int main(char **sh_args) { cwd = strdup("/"); cwd_f = open(cwd, 0); + + char *path = path_cat(sh_args[0], ".."); while (1) { - fprintf(out, " %s ", cwd); + printf(" %s ", cwd); - char *s = freadln(out); + char *s = readln(); if (s == 0) return -1; int l = strlen(s); if (s[l-1] == '\n') { @@ -256,7 +262,7 @@ int main(char **sh_args) { } else if (strcmp(c_args[0], "help") == 0) { help(c_args); } else if (strcmp(c_args[0], "exit") == 0) { - fprint(out, "Exiting the shell. See you later!\n"); + print("Exiting the shell. See you later!\n"); break; } else if (strcmp(c_args[0], "cd") == 0) { cd(c_args); @@ -265,18 +271,27 @@ int main(char **sh_args) { } else if (strcmp(c_args[0], "switch") == 0) { t_switch(c_args); } else { - char *c = path_cat(cwd, c_args[0]); + char *c; + if (strchr(c_args[0], '/')) { + c = path_cat(cwd, c_args[0]); + } else { + c = path_cat(path, c_args[0]); + } int pid = run(c, c_args + 1); if (pid <= 0) { - fprintf(out, "Error %i\n", pid); + if (pid == E_NOT_FOUND) { + printf("Error: no such file %s\n", c); + } else { + printf("Error %i\n", pid); + } } else { int ret = waitpid(pid); - fprintf(out, "Exited with status %i\n", ret); + printf(" -> child (pid %i) exited with status %i\n", pid, ret); } free(c); } } else { - fprint(out, "[freadln fail, sorry.]\n"); + print("[freadln fail, sorry.]\n"); } free(s); } |