From 4d65fcb9a8b6c7c6fd5a3390c46a96d11b6a80d4 Mon Sep 17 00:00:00 2001 From: Alex AUVOLAT Date: Sat, 8 Jun 2013 23:09:52 +0200 Subject: All FWIK is deleted. YOSH is now pure C. Not-working KBASIC included. --- src/user/app/yosh/Makefile | 2 +- src/user/app/yosh/main.c | 292 +++++++++++++++++++++++++++++++++++++++++++++ src/user/app/yosh/main.cpp | 271 ----------------------------------------- 3 files changed, 293 insertions(+), 272 deletions(-) create mode 100644 src/user/app/yosh/main.c delete mode 100644 src/user/app/yosh/main.cpp (limited to 'src/user/app/yosh') diff --git a/src/user/app/yosh/Makefile b/src/user/app/yosh/Makefile index bf55ce1..759531e 100644 --- a/src/user/app/yosh/Makefile +++ b/src/user/app/yosh/Makefile @@ -1,6 +1,6 @@ Obj = main.o Out = yosh.elf -include $(SrcPath)/user/app/fwik.make +include $(SrcPath)/user/app/common.make LDFLAGS += -Map yosh.map diff --git a/src/user/app/yosh/main.c b/src/user/app/yosh/main.c new file mode 100644 index 0000000..7c9b875 --- /dev/null +++ b/src/user/app/yosh/main.c @@ -0,0 +1,292 @@ +#include +#include +#include +#include +#include + +char *cwd; + +void about() { + printf("Trivial/Computing Environment v0.1.4 - yosh 4.\n"); +} + +void help(char **args) { + if (!args[1]) { + printf("Available commands: about, help, exit, ls, cd, goto.\n"); + } else if (!strcmp(args[1], "about")) { + printf("Usage:\tabout\nShows some info about yosh. Very usefull.\n"); + } else if (!strcmp(args[1], "help")) { + printf("Usage:\thelp\n\thelp \n"); + printf("Shows some info about the command you want, or commands in general.\n"); + } else if (!strcmp(args[1], "exit")) { + printf("Usage:\texit\n"); + printf("Exits the shell.\nWill probably make your system panic if you only have a shell running.\n"); + } else if (!strcmp(args[1], "cd")) { + printf("Usage:\tcd \nGoes to the location specified.\n"); + } else if (!strcmp(args[1], "ls")) { + printf("Usage:\tls\n\tls ...\nShows the content of the current/specified directory.\n"); + } else if (!strcmp(args[1], "goto")) { + printf("Usage:\tgoto \nSwitchs focus to specified virtual terminal.\n"); + printf("Type `ls /.ui` to see available terminals.\n"); + } else { + printf("No such command: %s\n", args[1]); + } +} + +void cd(char **args) { + if (!args[1]) { + printf("Usage: cd \n"); + } + if (!strcmp(args[1], ".")) return; + + char* newcwd = path_cat(cwd, args[1], 1); + + file_info f; + int r = stat(newcwd, &f); + if (r == E_NOT_FOUND) { + printf("No such file or directory.\n"); + } else if (r != 0) { + printf("Error stating: %i\n", r); + } else if (!(f.type & FT_DIR)) { + printf("Not a directory.\n"); + } else { + free(cwd); + cwd = newcwd; + return; + } + free(newcwd); +} + +void ls_dir(int fd) { + char buf[256]; + int pos = 0; + + while (read(fd, pos++, 256, buf) > 0) { + if ((!strcmp(buf, ".")) || (!strcmp(buf, ".."))) continue; + + printf(" %s", buf); + + file_info info; + stat_relative(fd, buf, &info); + if (info.type & FT_DIR) printf("/"); + printf(" \t"); + + if (info.type & FT_FILE) printf("file "); + if (info.type & FT_DIR) printf("dir "); + if (info.type & FT_SYMLINK) printf("symlink "); + if (info.type & FT_DEV) printf("dev "); + if (info.type & FT_TERMINAL) printf("term "); + + printf("\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); + } + + printf("\n"); + } +} + +void ls(char **args) { + if (!args[1]) { + int fd = open(cwd, FM_READ); + if (fd > 0) { + ls_dir(fd); + close(fd); + } else { + printf(" Could not open for read (%d).\n", fd); + } + } else { + int i; + for (i = 1; args[i]; i++) { + printf("Contents of %s :\n", args[i]); + char *d = path_cat(cwd, args[i], 1); + + file_info i; + int r = stat(d, &i); + + if (r == E_NOT_FOUND) { + printf(" No such file or directory\n"); + } else if (r != 0) { + printf(" Error stating: %i\n", r); + } else if (!(i.type & FT_DIR)) { + printf(" Not a directory.\n"); + } else { + int fd = open(d, FM_READ); + if (fd > 0) { + ls_dir(fd); + close(fd); + } else { + printf(" Could not open for read (%d).\n", fd); + } + } + free(d); + } + } +} + +void cat(char **args) { + int i; + for (i = 1; args[i]; 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 { + int ff = open(d, 0); + char* buff = (char*)malloc(info.size); + read(ff, 0, info.size, buff); + close(ff); + write(term.fd, 0, info.size, buff); + free(buff); + } + free(d); + } +} + +void t_goto(char **args) { + if (!args[1]) { + printf("Usage: goto \n"); + return; + } + 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); + } else { + printf("Error %i\n", i); + } + free(p); +} + +int main(int argc, char **sh_args) { + about(); + + cwd = strdup("/"); + + char *path = path_cat(sh_args[0], "..", 1); + + int bg_pr[128], bg_pr_c = 0; + + readline_history hist; + hist.str = 0; hist.max = 10; + + while (1) { + // check for background processes that may have finished + int p = 0; + while (p < bg_pr_c) { + int ret = waitpid(bg_pr[p], 0); + if (ret != E_NOT_FINISHED) { + printf("(yosh) child (pid %i) exited with status %i\n", bg_pr[p], ret); + bg_pr_c--; + bg_pr[p] = bg_pr[bg_pr_c]; + } else { + p++; + } + } + + // show prompt + printf("\x1b[33m %s \x1b[1m", cwd); + char *s = freadline(stdin, &hist); + printf("\x1b[0m"); + if (s == NULL) break; + if (strlen(s) == 0) continue; + + char *a_c = strdup(s); // duplicate because we're gonna add '\0'es + + char *c_args[16]; + char *start = a_c, *pos = a_c; + int argc = 0; + while (*pos) { + if (*pos == ' ') { + if (pos == start) { + start++; + } else { + *pos = 0; + c_args[argc] = start; + argc++; + start = pos + 1; + if (argc == 14) { + break; + } + } + } + pos++; + } + c_args[argc++] = start; + c_args[argc] = 0; + + if (!strcmp(c_args[0], "about")) { + about(); + } else if (!strcmp(c_args[0], "help")) { + help(c_args); + } else if (!strcmp(c_args[0], "exit")) { + printf("Exiting the shell. See you later!\n"); + break; + } else if (!strcmp(c_args[0], "cd")) { + cd(c_args); + } else if (!strcmp(c_args[0], "ls")) { + ls(c_args); + } else if (!strcmp(c_args[0], "goto")) { + t_goto(c_args); + } else if (!strcmp(c_args[0], "cat")) { + cat(c_args); + } else { + char **first_arg = c_args; + + char *term_s = NULL; + int vt_fd = term.fd; + + if (!strcmp(c_args[0], "on")) { + if (!c_args[1] || !c_args[2]) { + printf("Usage:\ton \n"); + continue; + } + term_s = path_cat("/.ui/", c_args[1], 1); + vt_fd = open(term_s, 0); + if (vt_fd < 0 || vt_fd == term.fd) { + printf("Error: cannot open terminal %s (%i)\n", term_s, vt_fd); + continue; + } + first_arg += 2; + } + + char *c; + if (strchr(*first_arg, '/')) { + c = path_cat(cwd, *first_arg, 0); + } else { + c = path_cat(path, *first_arg, 0); + } + first_arg++; + + int pid = run(c, (const char**)first_arg, vt_fd); + if (pid <= 0) { + if (pid == E_NOT_FOUND) { + printf("Error: no such file %s\n", c); + } else { + printf("Error %i\n", pid); + } + } else { + if (term_s == NULL) { + int ret = waitpid(pid, 1); + printf("(yosh) child (pid %i) exited with status %i\n", pid, ret); + } else { + printf("(yosh) running on terminal %s, pid:%i\n", term_s, pid); + bg_pr[bg_pr_c++] = pid; + } + } + free(c); + if (term_s) free(term_s); + } + + free(a_c); + } + + return 0; +} diff --git a/src/user/app/yosh/main.cpp b/src/user/app/yosh/main.cpp deleted file mode 100644 index 160e728..0000000 --- a/src/user/app/yosh/main.cpp +++ /dev/null @@ -1,271 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -Dir *cwd_f; -String cwd; - -void about() { - stdio << "Trivial/Computing Environment v0.1.0 - yosh 3.\n"; -} - -void help(String *args) { - if (!args[1]) { - stdio << "Available commands: about, help, exit, ls, cd, goto.\n"; - } else if (args[1] == "about") { - stdio << "Usage:\tabout\nShows some info about yosh. Very usefull.\n"; - } 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 (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 (args[1] == "cd") { - stdio << "Usage:\tcd \nGoes to the location specified.\n"; - } else if (args[1] == "ls") { - stdio << "Usage:\tls\n\tls ...\nShows the content of the current/specified directory.\n"; - } 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].c_str()); - } -} - -void cd(String *args) { - if (!args[1]) { - stdio << "Usage: cd \n"; - } - if (args[1] == ".") return; - - String newcwd = path_cat(cwd, args[1]); - - Dir *newdir = new Dir(newcwd.c_str(), 0); - if (newdir->error == E_NOT_FOUND) { - stdio << "No such file or directory.\n"; - } else if (newdir->error == E_INVALID_TYPE) { - stdio << "Not a directory.\n"; - } else if (newdir->error != 0) { - stdio.printf("Error stating: %i\n", newdir->error); - } else { - cwd_f->close(); - cwd = newcwd; - cwd_f = newdir; - } -} - -void ls_dir(Dir *d) { - if (d->error != 0) { - return; - } - d->pos = 0; - for (String name = d->read_ent(); name; name = d->read_ent()) { - if (name == "." || name == "..") continue; - - stdio.printf(" %s", name.c_str()); - - Node child(d->fd, name.c_str(), 0); - - if (child.info.type & FT_DIR) stdio << "/"; - stdio << " \t"; - - if (child.info.type & FT_FILE) stdio << "file "; - if (child.info.type & FT_DIR) stdio << "dir "; - if (child.info.type & FT_SYMLINK) stdio << "symlink "; - if (child.info.type & FT_DEV) stdio << "dev "; - if (child.info.type & FT_TERMINAL) stdio << "term "; - - stdio << "\t"; - if (child.info.type & FT_TERMINAL) { - stdio.printf("%ix%i", child.info.size >> 16, child.info.size & 0xFFFF); - } else if ((child.info.type & FT_DEV) == 0) { - stdio.printf("%i", child.info.size); - } - - stdio << "\n"; - } -} - -void ls(String *args) { - if (!args[1]) { - ls_dir(cwd_f); - } else { - int i; - for (i = 1; args[i]; i++) { - stdio.printf("Contents of %s :\n", args[i].c_str()); - String d = path_cat(cwd, args[i].c_str()); - Dir dir(d.c_str(), 0); - if (dir.error == E_NOT_FOUND) { - stdio << " No such file or directory\n"; - } else if (dir.error == E_INVALID_TYPE) { - stdio.printf(" Not a directory.\n"); - } else if (dir.error < 0) { - stdio.printf(" Error stating: %i\n", dir.error); - } else { - ls_dir(&dir); - dir.close(); - } - } - } -} - -void cat(String *args) { - int i; - for (i = 1; args[i]; i++) { - String d = path_cat(cwd, args[i], false); - file_info info; - int e = libc::stat(d.c_str(), &info); - if (e == E_NOT_FOUND) { - stdio.printf("No such file: %s\n", d.c_str()); - } else if (e < 0) { - 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.c_str()); - } else { - FILE ff = libc::open(d.c_str(), 0); - char* buff = (char*)malloc(info.size); - libc::read(ff, 0, info.size, buff); - libc::close(ff); - libc::write(stdio.term->fd, 0, info.size, buff); - free(buff); - } - } -} - -void t_goto(String *args) { - if (!args[1]) { - stdio << "Usage: goto \n"; - return; - } - String p = path_cat("/.ui/", args[1], false); - int i; - 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); - } -} - -int Main(String *sh_args) { - about(); - - cwd = "/"; - cwd_f = new Dir("/", 0); - - String path = path_cat(sh_args[0], ".."); - - int bg_pr[128], bg_pr_c = 0; - - Term *term = stdio.term; - if (term == 0) { - stdio << "Error: no terminal...\n"; - return -1; - } - - while (1) { - // check for background processes that may have finished - int p = 0; - while (p < bg_pr_c) { - int ret = libc::waitpid(bg_pr[p], 0); - if (ret != E_NOT_FINISHED) { - stdio.printf("(yosh) child (pid %i) exited with status %i\n", bg_pr[p], ret); - bg_pr_c--; - bg_pr[p] = bg_pr[bg_pr_c]; - } else { - p++; - } - } - - // show prompt - stdio.printf("\x1b[33m %s \x1b[1m", cwd.c_str()); - String s = term->readline(); - stdio.printf("\x1b[0m"); - 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++; - start = i + 1; - if (argc == 15) { - break; - } - } - } - } - c_args[argc++] = s.substr(start, s.size() - start); - - if (c_args[0] == "about") { - about(); - } else if (c_args[0] == "help") { - help(c_args); - } else if (c_args[0] == "exit") { - stdio << "Exiting the shell. See you later!\n"; - break; - } else if (c_args[0] == "cd") { - cd(c_args); - } else if (c_args[0] == "ls") { - ls(c_args); - } else if (c_args[0] == "goto") { - t_goto(c_args); - } else if (c_args[0] == "cat") { - cat(c_args); - } else { - FILE vt = term->fd; - String *first_arg = c_args ; - String t; - - 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], false); - vt = libc::open(t.c_str(), 0); - if (vt < 0 || vt == term->fd) { - stdio.printf("Error: cannot open terminal %s (%i)\n", t.c_str(), vt); - continue; - } - first_arg += 2; - } - - String c; - if (libc::strchr(first_arg->c_str(), '/')) { - c = path_cat(cwd, *first_arg, false); - } else { - c = path_cat(path, *first_arg, false); - } - first_arg++; - 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.c_str(), run_args, vt); - if (pid <= 0) { - if (pid == E_NOT_FOUND) { - stdio.printf("Error: no such file %s\n", c.c_str()); - } else { - stdio.printf("Error %i\n", pid); - } - } else { - if (vt == term->fd) { - 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.c_str(), pid); - bg_pr[bg_pr_c++] = pid; - } - } - } - } - - return 0; -} -- cgit v1.2.3