#include #include #include #include FILE cwd_f; char *cwd; void about() { print("Trivial/Computing Environment v0.1.0 - yosh 2.\n"); } void help(char *args[]) { if (args[1] == 0) { print("Available commands: about, help, exit, ls, cd, switch.\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) { print("Usage:\thelp\n\thelp \n"); print("Shows some info about the command you want, or commands in general.\n"); } else if (strcmp(args[1], "exit") == 0) { 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) { print("Usage:\tcd \nGoes to the location specified.\n"); } else if (strcmp(args[1], "ls") == 0) { print("Usage:\tls\n\tls ...\nShows the content of the current/specified directory.\n"); } else if (strcmp(args[1], "switch") == 0) { print("Usage:\tswitch \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]); } } 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 (strcmp(member, ".") == 0) { char *i = member; while (1) { i[0] = i[2]; if (i[0] == 0) break; i++; } } else if (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) { char* ret; if (b[0] == '/') { int lb = strlen(b); ret = (char*)malloc(lb + 2); memcpy(ret, b, lb); if (ret[lb - 1] != '/') { ret[lb] = '/'; lb++; } ret[lb] = 0; } else { int la = strlen(a); int lb = strlen(b); 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] = '/'; lb++; } ret[la + lb] = 0; } simplify_path(ret); return ret; } void cd(char **args) { if (args[1] == 0) { print("Usage: cd \n"); } if (strcmp(args[1], ".") == 0) return; char* newcwd = path_cat(cwd, args[1]); file_info info; int i = stat(newcwd, &info); if (i == E_NOT_FOUND) { print("No such file or directory.\n"); free(newcwd); } else if (i < 0) { printf("Error stating: %i\n", i); free(newcwd); } else if ((info.type & FT_DIR) == 0) { print("Not a directory.\n"); free(newcwd); } else { FILE nf = open(newcwd, 0); if (nf <= 0) { printf("Error opening: %i\n", nf); free(newcwd); } else { free(cwd); cwd = newcwd; close(cwd_f); cwd_f = nf; } } } void ls_dir(FILE f) { file_info info; int i = 0; char buf[256]; while (read(f, i, 256, buf) > 0) { i++; if (strcmp(buf, ".") == 0 || strcmp(buf, "..") == 0) continue; printf(" %s", buf); stat_relative(f, buf, &info); if (info.type & FT_DIR) print("/"); print(" \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 "); print("\t"); if (info.type & FT_TERMINAL) { printf("%ix%i", info.size >> 16, info.size & 0xFFFF); } else if ((info.type & FT_DEV) == 0) { printf("%i", info.size); } print("\n"); } } void ls(char **args) { if (args[1] == 0) { ls_dir(cwd_f); } else { int i; for (i = 1; args[i] != 0; 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) { print(" No such file or directory\n"); } else if (e < 0) { printf(" Error stating: %i\n", e); } else { FILE ff = open(d, 0); if (ff <= 0) { printf(" Error opening: %i\n", ff); } else { ls_dir(ff); close(ff); } } free(d); } } } void t_switch(char** args) { if (args[1] == 0) { print("Usage: switch \n"); return; } char* p = path_cat("/.ui/", args[1]); int i; if ((i = link(p, "/.dev/vgatxt", LM_OUTPUT_TO)) == 0) { link("/.dev/ps2kbd", p, LM_OUTPUT_TO); } else { printf("Error %i\n", i); } } 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], ".."); while (1) { printf(" %s ", cwd); char *s = readln(); 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 (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); } else { 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) { if (pid == E_NOT_FOUND) { printf("Error: no such file %s\n", c); } else { printf("Error %i\n", pid); } } else { int ret = waitpid(pid); printf(" -> child (pid %i) exited with status %i\n", pid, ret); } free(c); } } else { print("[freadln fail, sorry.]\n"); } free(s); } return 0; }