From 3d5aca66b9712758aecb2e80bc5b2fb3df6256a4 Mon Sep 17 00:00:00 2001 From: Alexis211 Date: Wed, 16 Dec 2009 18:22:58 +0100 Subject: New model, object-oriented, for applications --- Source/Applications/Demos/GOL.cpp | 14 ++- Source/Applications/Demos/cxxdemo.cpp | 14 ++- Source/Applications/PaperWork/Makefile | 2 +- Source/Applications/PaperWork/PaperWork.cpp | 64 ++++++++++ Source/Applications/PaperWork/main.cpp | 57 --------- Source/Applications/Shell/Makefile | 5 +- Source/Applications/Shell/Shell-fs.class.cpp | 132 ++++++++++++++++++++ Source/Applications/Shell/Shell-fs.ns.cpp | 136 --------------------- Source/Applications/Shell/Shell.class.cpp | 94 ++++++++++++++ Source/Applications/Shell/Shell.class.h | 28 +++++ Source/Applications/Shell/Shell.ns.cpp | 96 --------------- Source/Applications/Shell/Shell.ns.h | 18 --- Source/Library/Userland/App/Application.proto.h | 41 +++++++ Source/Library/Userland/App/ShellApp.proto.h | 18 +++ .../Userland/Binding/VirtualTerminal.class.h | 2 - Source/Library/Userland/Start.cpp | 37 +++--- 16 files changed, 416 insertions(+), 342 deletions(-) create mode 100644 Source/Applications/PaperWork/PaperWork.cpp delete mode 100644 Source/Applications/PaperWork/main.cpp create mode 100644 Source/Applications/Shell/Shell-fs.class.cpp delete mode 100644 Source/Applications/Shell/Shell-fs.ns.cpp create mode 100644 Source/Applications/Shell/Shell.class.cpp create mode 100644 Source/Applications/Shell/Shell.class.h delete mode 100644 Source/Applications/Shell/Shell.ns.cpp delete mode 100644 Source/Applications/Shell/Shell.ns.h create mode 100644 Source/Library/Userland/App/Application.proto.h create mode 100644 Source/Library/Userland/App/ShellApp.proto.h (limited to 'Source') diff --git a/Source/Applications/Demos/GOL.cpp b/Source/Applications/Demos/GOL.cpp index e6da3a6..cd6e719 100644 --- a/Source/Applications/Demos/GOL.cpp +++ b/Source/Applications/Demos/GOL.cpp @@ -1,10 +1,18 @@ -#include #include -#include #include #include -int main(Vector args) { +#include + +class GOL : public ShellApp { + public: + GOL() : ShellApp() {} + int run(); +}; + +APP(GOL); + +int GOL::run() { if (!outvt.isBoxed()) { outvt << "Error : cannot display GOL on a non-boxed terminal.\n"; return 1; diff --git a/Source/Applications/Demos/cxxdemo.cpp b/Source/Applications/Demos/cxxdemo.cpp index 3d452e7..372e69f 100644 --- a/Source/Applications/Demos/cxxdemo.cpp +++ b/Source/Applications/Demos/cxxdemo.cpp @@ -1,10 +1,18 @@ #include -#include -#include #include #include -int main(const Vector& args) { +#include + +class CPPDemo : public ShellApp { + public: + CPPDemo() : ShellApp() {} + int run(); +}; + +APP(CPPDemo); + +int CPPDemo::run() { outvt << "Enter some text plz : "; String s = invt.readLine(); outvt << s; diff --git a/Source/Applications/PaperWork/Makefile b/Source/Applications/PaperWork/Makefile index fe8d564..d3203cf 100644 --- a/Source/Applications/PaperWork/Makefile +++ b/Source/Applications/PaperWork/Makefile @@ -6,7 +6,7 @@ CXXFLAGS = -nostartfiles -nostdlib -ffreestanding -fno-exceptions -fno-rtti -I . LD = ld LDFLAGS = -T ../../Library/Link.ld -L ../../Library -Objects = main.o +Objects = PaperWork.o OutFile = PaperWork all: $(OutFile) diff --git a/Source/Applications/PaperWork/PaperWork.cpp b/Source/Applications/PaperWork/PaperWork.cpp new file mode 100644 index 0000000..0d6a821 --- /dev/null +++ b/Source/Applications/PaperWork/PaperWork.cpp @@ -0,0 +1,64 @@ +#include + +#define DEFAULT_SHELL "/Applications/Shell/Shell.app" + +class PaperWork : public ShellApp { + public: + PaperWork() : ShellApp() {} + int run(); +}; + +APP(PaperWork); + +int PaperWork::run() { + String act = "init"; + if (args.size() == 2) { + if (args[1] == "login") { + act = "login"; + } else if (args[1] == "init") { + act = "init"; + } + } + + if (act == "init") { + while (1) { + Process p = Process::run("/System/Applications/PaperWork.app"); + if (p.valid()) { + p.setInVT(invt); + p.setOutVT(outvt); + p.pushArg("login"); + p.start(); + p.wait(); + } else { + return 1; + } + } + } else if (act == "login") { + outvt << "Logging in to Melon\n"; + String user, pw; + while (1) { + outvt << "Username: "; + user = invt.readLine(); + outvt << "Password: "; + pw = invt.readLine(false); + if (!Process::get().authenticatePW(user, pw)) { + outvt << "Authentication failed.\n\n"; + continue; + } + outvt << "What shell to run [" << DEFAULT_SHELL << "]? "; + String sh = invt.readLine(); + if (sh == "") sh = DEFAULT_SHELL; + Process p = Process::run(sh); + if (p.valid()) { + p.setInVT(invt); + p.setOutVT(outvt); + p.start(); + p.wait(); + outvt << "\n\n"; + } else { + return 1; + } + return 0; + } + } +} diff --git a/Source/Applications/PaperWork/main.cpp b/Source/Applications/PaperWork/main.cpp deleted file mode 100644 index 2fb40df..0000000 --- a/Source/Applications/PaperWork/main.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include -#include - -#define DEFAULT_SHELL "/Applications/Shell/Shell.app" - -int main(Vector args) { - String act = "init"; - if (args.size() == 2) { - if (args[1] == "login") { - act = "login"; - } else if (args[1] == "init") { - act = "init"; - } - } - - if (act == "init") { - while (1) { - Process p = Process::run("/System/Applications/PaperWork.app"); - if (p.valid()) { - p.setInVT(invt); - p.setOutVT(outvt); - p.pushArg("login"); - p.start(); - p.wait(); - } else { - return 1; - } - } - } else if (act == "login") { - outvt << "Logging in to Melon\n"; - String user, pw; - while (1) { - outvt << "Username: "; - user = invt.readLine(); - outvt << "Password: "; - pw = invt.readLine(false); - if (!Process::get().authenticatePW(user, pw)) { - outvt << "Authentication failed.\n\n"; - continue; - } - outvt << "What shell to run [" << DEFAULT_SHELL << "]? "; - String sh = invt.readLine(); - if (sh == "") sh = DEFAULT_SHELL; - Process p = Process::run(sh); - if (p.valid()) { - p.setInVT(invt); - p.setOutVT(outvt); - p.start(); - p.wait(); - outvt << "\n\n"; - } else { - return 1; - } - return 0; - } - } -} diff --git a/Source/Applications/Shell/Makefile b/Source/Applications/Shell/Makefile index bf81af6..d546a15 100644 --- a/Source/Applications/Shell/Makefile +++ b/Source/Applications/Shell/Makefile @@ -6,9 +6,8 @@ CXXFLAGS = -nostartfiles -nostdlib -ffreestanding -fno-exceptions -fno-rtti -I . LD = ld LDFLAGS = -T ../../Library/Link.ld -L ../../Library -Objects = main.o \ - Shell.ns.o \ - Shell-fs.ns.o +Objects = Shell.class.o \ + Shell-fs.class.o OutFile = Shell all: $(OutFile) diff --git a/Source/Applications/Shell/Shell-fs.class.cpp b/Source/Applications/Shell/Shell-fs.class.cpp new file mode 100644 index 0000000..150b877 --- /dev/null +++ b/Source/Applications/Shell/Shell-fs.class.cpp @@ -0,0 +1,132 @@ +#include "Shell.class.h" +#include +#include + +void Shell::ls(Vector& args) { + FSNode d = cwd; + if (args.size() == 2) { + FSNode n = FS::find(args[1], cwd); + d = FSNode(0); + if (!n.valid()) + outvt << "No such directory : " << args[1] << "\n"; + else if (n.type() != NT_DIRECTORY) + outvt << "Not a directory : " << args[1] << "\n"; + else + d = n; + } + if (d.valid()) outvt << "Contents of directory " << d.path() << " :\n"; + if (!d.valid()) return; + for (u32int i = 0; i < d.getLength(); i++) { + FSNode n = d.getChild(i); + if (!n.valid()) { + outvt << " [inacessible file]\n"; //This is a file we are not supposed to be able to read + continue; + } + String perm = "rwxrwxrwx"; + u32int p = n.getPerm(); + for (u32int i = 0; i < 9; i++) { + if (((p >> i) & 1) == 0) perm[8 - i] = "-"; + } + if (n.type() == NT_FILE) { + outvt << " FILE " << perm << " " << n.getName(); + outvt.setCsrCol(30); + outvt << (s32int)n.getLength() << " bytes.\n"; + } else if (n.type() == NT_DIRECTORY) { + outvt << " DIR " << perm << " " << n.getName() << "/"; + outvt.setCsrCol(30); + outvt << (s32int)n.getLength() << " items.\n"; + } + } +} + +void Shell::cd(Vector& args) { + if (args.size() != 2) { + outvt << "Invalid argument count.\n"; + } else { + FSNode ncwd = FS::find(args[1], cwd); + if (!ncwd.valid()) { + outvt << "No such directory : " << args[1] << "\n"; + } else if (ncwd.type() == NT_DIRECTORY) { + ncwd.setCwd(); + cwd = ncwd; + } else { + outvt << "Not a directory.\n"; + } + } +} + +void Shell::pwd(Vector& args) { + outvt << "Current directory : " << cwd.path() << "\n"; +} + +void Shell::rm(Vector& args) { + if (args.size() == 1) outvt << "No file to remove.\n"; + for (u32int i = 1; i < args.size(); i++) { + if (!FS::find(args[i], cwd).remove()) { + outvt << "Error while removing file " << args[i] << "\n"; + } + } +} + +void Shell::mkdir(Vector& args) { + if (args.size() == 1) outvt << "No directory to create.\n"; + for (u32int i = 1; i < args.size(); i++) { + if (!FS::mkdir(args[i], cwd).valid()) { + outvt << "Error while creating directory " << args[i] << "\n"; + } + } +} + +void Shell::cat(Vector& args) { + if (args.size() == 1) outvt << "Meow.\n"; + for (u32int i = 1; i < args.size(); i++) { + TextFile f(args[i], FM_READ, cwd); + if (f.valid() && f.validOpened()) { + while (!f.eof()) { + outvt << f.readLine() << "\n"; + } + f.close(); + } else { + outvt << "Error while reading from file " << args[i] << "\n"; + } + } +} + +void Shell::wf(Vector& args) { + if (args.size() == 1) { + outvt << "No file to write !\n"; + } else { + TextFile f(args[1], FM_TRUNCATE, cwd); + if (f.valid() && f.validOpened()) { + outvt << "Enter contents for file " << args[1] << " and end your entry with .\n"; + String t = invt.readLine(); + while (t != ".") { + f.write(t, true); + t = invt.readLine(); + } + f.close(); + } else { + outvt << "Error opening file.\n"; + } + } +} + +void Shell::run(Vector& args) { + if (args.size() == 1) { + outvt << "Nothing to run...\n"; + } else { + Process p = Process::run(args[1]); + if (p.valid()) { + p.setInVT(invt); + p.setOutVT(outvt); + for (u32int i = 2; i < args.size(); i++) { + p.pushArg(args[i]); + } + p.start(); + s32int v = p.wait(); + outvt << "Return value : " << (s64int)v << "\n"; + } else { + outvt << "Error while launching process.\n"; + } + } +} diff --git a/Source/Applications/Shell/Shell-fs.ns.cpp b/Source/Applications/Shell/Shell-fs.ns.cpp deleted file mode 100644 index 0b51299..0000000 --- a/Source/Applications/Shell/Shell-fs.ns.cpp +++ /dev/null @@ -1,136 +0,0 @@ -#include "Shell.ns.h" -#include -#include - -namespace Shell { - -void ls(Vector& args) { - FSNode d = cwd; - if (args.size() == 2) { - FSNode n = FS::find(args[1], cwd); - d = FSNode(0); - if (!n.valid()) - outvt << "No such directory : " << args[1] << "\n"; - else if (n.type() != NT_DIRECTORY) - outvt << "Not a directory : " << args[1] << "\n"; - else - d = n; - } - if (d.valid()) outvt << "Contents of directory " << d.path() << " :\n"; - if (!d.valid()) return; - for (u32int i = 0; i < d.getLength(); i++) { - FSNode n = d.getChild(i); - if (!n.valid()) { - outvt << " [inacessible file]\n"; //This is a file we are not supposed to be able to read - continue; - } - String perm = "rwxrwxrwx"; - u32int p = n.getPerm(); - for (u32int i = 0; i < 9; i++) { - if (((p >> i) & 1) == 0) perm[8 - i] = "-"; - } - if (n.type() == NT_FILE) { - outvt << " FILE " << perm << " " << n.getName(); - outvt.setCsrCol(30); - outvt << (s32int)n.getLength() << " bytes.\n"; - } else if (n.type() == NT_DIRECTORY) { - outvt << " DIR " << perm << " " << n.getName() << "/"; - outvt.setCsrCol(30); - outvt << (s32int)n.getLength() << " items.\n"; - } - } -} - -void cd(Vector& args) { - if (args.size() != 2) { - outvt << "Invalid argument count.\n"; - } else { - FSNode ncwd = FS::find(args[1], cwd); - if (!ncwd.valid()) { - outvt << "No such directory : " << args[1] << "\n"; - } else if (ncwd.type() == NT_DIRECTORY) { - ncwd.setCwd(); - cwd = ncwd; - } else { - outvt << "Not a directory.\n"; - } - } -} - -void pwd(Vector& args) { - outvt << "Current directory : " << cwd.path() << "\n"; -} - -void rm(Vector& args) { - if (args.size() == 1) outvt << "No file to remove.\n"; - for (u32int i = 1; i < args.size(); i++) { - if (!FS::find(args[i], cwd).remove()) { - outvt << "Error while removing file " << args[i] << "\n"; - } - } -} - -void mkdir(Vector& args) { - if (args.size() == 1) outvt << "No directory to create.\n"; - for (u32int i = 1; i < args.size(); i++) { - if (!FS::mkdir(args[i], cwd).valid()) { - outvt << "Error while creating directory " << args[i] << "\n"; - } - } -} - -void cat(Vector& args) { - if (args.size() == 1) outvt << "Meow.\n"; - for (u32int i = 1; i < args.size(); i++) { - TextFile f(args[i], FM_READ, cwd); - if (f.valid() && f.validOpened()) { - while (!f.eof()) { - outvt << f.readLine() << "\n"; - } - f.close(); - } else { - outvt << "Error while reading from file " << args[i] << "\n"; - } - } -} - -void wf(Vector& args) { - if (args.size() == 1) { - outvt << "No file to write !\n"; - } else { - TextFile f(args[1], FM_TRUNCATE, cwd); - if (f.valid() && f.validOpened()) { - outvt << "Enter contents for file " << args[1] << " and end your entry with .\n"; - String t = invt.readLine(); - while (t != ".") { - f.write(t, true); - t = invt.readLine(); - } - f.close(); - } else { - outvt << "Error opening file.\n"; - } - } -} - -void run(Vector& args) { - if (args.size() == 1) { - outvt << "Nothing to run...\n"; - } else { - Process p = Process::run(args[1]); - if (p.valid()) { - p.setInVT(invt); - p.setOutVT(outvt); - for (u32int i = 2; i < args.size(); i++) { - p.pushArg(args[i]); - } - p.start(); - s32int v = p.wait(); - outvt << "Return value : " << (s64int)v << "\n"; - } else { - outvt << "Error while launching process.\n"; - } - } -} - -} diff --git a/Source/Applications/Shell/Shell.class.cpp b/Source/Applications/Shell/Shell.class.cpp new file mode 100644 index 0000000..42743e7 --- /dev/null +++ b/Source/Applications/Shell/Shell.class.cpp @@ -0,0 +1,94 @@ +#include "Shell.class.h" +#include +#include + +APP(Shell); + +Shell::Shell() : ShellApp(), cwd(FS::cwdNode()) { +} + +int Shell::run() { + struct { //Command list + String name; + void (Shell::*cmd)(Vector&); + } commands[] = { + {"ls", &Shell::ls}, + {"cd", &Shell::cd}, + {"pwd", &Shell::pwd}, + {"rm", &Shell::rm}, + {"mkdir", &Shell::mkdir}, + {"cat", &Shell::cat}, + {"wf", &Shell::wf}, + {"run", &Shell::run}, + {"", 0} + }; + + cwd = FS::cwdNode(); + while (1) { + outvt << "{" << cwd.getName() << "}: "; + String s = invt.readLine(); + if (s.empty()) continue; + while (s[0] == WChar(" ") or s[0] == WChar("\t")) { + s = s.substr(1, s.size() - 1); + } + if (s.empty()) continue; + if (s[0] == WChar("#")) continue; + + //Parse string + Vector cmd; + cmd.push(String()); + bool inQuote = false; + for (u32int i = 0; i < s.size(); i++) { + if (s[i] == WChar("'")) { + inQuote = !inQuote; + } else if (s[i] == WChar("\\")) { + i++; + cmd.back() += s[i]; + } else if (s[i] == WChar(" ") and !inQuote) { + cmd.push(String()); + } else { + cmd.back() += s[i]; + } + } + + //Run command + if (cmd[0] == "exit") { + if (cmd.size() == 1) return 0; + return cmd[1].toInt(); + } else if (cmd[0] == "halt") { + Sys::halt(); + outvt << "Something went wrong.\n"; + } else if (cmd[0] == "reboot") { + Sys::reboot(); + outvt << "Something went wrong.\n"; + } else if (cmd[0] == "uptime") { + outvt << "Uptime : " << (s64int)Sys::uptime() << "s\n"; + } else if (cmd[0] == "free") { + outvt << "Free RAM : " << (s64int)Sys::freeRam() << " Kio of " << (s64int)Sys::totalRam() << " Kio\n"; + } else if (cmd[0] == "uid") { + outvt << "User ID : " << (s64int)(Process::get().getUid()) << "\n"; + } else if (cmd[0] == "help") { + while (cmd.size() > 1) cmd.pop(); + cmd.push("/Applications/Shell/Help.txt"); + cat(cmd); + } else { + u32int i = 0; + bool found = false; + while (!commands[i].name.empty()) { + if (commands[i].name == cmd[0]) { + found = true; + if (commands[i].cmd == 0) { + outvt << "Not implemented yet.\n"; + } else { + (this->*(commands[i].cmd))(cmd); + } + break; + } + i++; + } + if (!found) outvt << "Unknown command : " << cmd[0] << "\n"; + } + } +} + + diff --git a/Source/Applications/Shell/Shell.class.h b/Source/Applications/Shell/Shell.class.h new file mode 100644 index 0000000..7c0c324 --- /dev/null +++ b/Source/Applications/Shell/Shell.class.h @@ -0,0 +1,28 @@ +#ifndef DEF_SHELL_CLASS_H +#define DEF_SHELL_CLASS_H + +#include +#include +#include + +#include + +class Shell : public ShellApp { + public: + Shell(); + + int run(); + + FSNode cwd; + + void ls(Vector& args); + void cd(Vector& args); + void pwd(Vector& args); + void rm(Vector& args); + void mkdir(Vector& args); + void cat(Vector& args); + void wf(Vector& args); + void run(Vector& args); +}; + +#endif diff --git a/Source/Applications/Shell/Shell.ns.cpp b/Source/Applications/Shell/Shell.ns.cpp deleted file mode 100644 index 56a6b5f..0000000 --- a/Source/Applications/Shell/Shell.ns.cpp +++ /dev/null @@ -1,96 +0,0 @@ -#include "Shell.ns.h" -#include -#include - -namespace Shell { - -FSNode cwd(0); - - -u32int run() { - struct { //Command list - String name; - void (*cmd)(Vector&); - } commands[] = { - {"ls", ls}, - {"cd", cd}, - {"pwd", pwd}, - {"rm", rm}, - {"mkdir", mkdir}, - {"cat", cat}, - {"wf", wf}, - {"run", run}, - {"", 0} - }; - - cwd = FS::cwdNode(); - while (1) { - outvt << "{" << cwd.getName() << "}: "; - String s = invt.readLine(); - if (s.empty()) continue; - while (s[0] == WChar(" ") or s[0] == WChar("\t")) { - s = s.substr(1, s.size() - 1); - } - if (s.empty()) continue; - if (s[0] == WChar("#")) continue; - - //Parse string - Vector cmd; - cmd.push(String()); - bool inQuote = false; - for (u32int i = 0; i < s.size(); i++) { - if (s[i] == WChar("'")) { - inQuote = !inQuote; - } else if (s[i] == WChar("\\")) { - i++; - cmd.back() += s[i]; - } else if (s[i] == WChar(" ") and !inQuote) { - cmd.push(String()); - } else { - cmd.back() += s[i]; - } - } - - //Run command - if (cmd[0] == "exit") { - if (cmd.size() == 1) return 0; - return cmd[1].toInt(); - } else if (cmd[0] == "halt") { - Sys::halt(); - outvt << "Something went wrong.\n"; - } else if (cmd[0] == "reboot") { - Sys::reboot(); - outvt << "Something went wrong.\n"; - } else if (cmd[0] == "uptime") { - outvt << "Uptime : " << (s64int)Sys::uptime() << "s\n"; - } else if (cmd[0] == "free") { - outvt << "Free RAM : " << (s64int)Sys::freeRam() << " Kio of " << (s64int)Sys::totalRam() << " Kio\n"; - } else if (cmd[0] == "uid") { - outvt << "User ID : " << (s64int)(Process::get().getUid()) << "\n"; - } else if (cmd[0] == "help") { - while (cmd.size() > 1) cmd.pop(); - cmd.push("/Applications/Shell/Help.txt"); - cat(cmd); - } else { - u32int i = 0; - bool found = false; - while (!commands[i].name.empty()) { - if (commands[i].name == cmd[0]) { - found = true; - if (commands[i].cmd == 0) { - outvt << "Not implemented yet.\n"; - } else { - commands[i].cmd(cmd); - } - break; - } - i++; - } - if (!found) outvt << "Unknown command : " << cmd[0] << "\n"; - } - } -} - -} - - diff --git a/Source/Applications/Shell/Shell.ns.h b/Source/Applications/Shell/Shell.ns.h deleted file mode 100644 index b2dd587..0000000 --- a/Source/Applications/Shell/Shell.ns.h +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include -#include - -namespace Shell { - u32int run(); - - extern FSNode cwd; - - extern void ls(Vector& args); - extern void cd(Vector& args); - extern void pwd(Vector& args); - extern void rm(Vector& args); - extern void mkdir(Vector& args); - extern void cat(Vector& args); - extern void wf(Vector& args); - extern void run(Vector& args); -} diff --git a/Source/Library/Userland/App/Application.proto.h b/Source/Library/Userland/App/Application.proto.h new file mode 100644 index 0000000..1f3a963 --- /dev/null +++ b/Source/Library/Userland/App/Application.proto.h @@ -0,0 +1,41 @@ +#ifndef DEF_APPLICATION_PROTO_H +#define DEF_APPLICATION_PROTO_H + +#include +#include + +extern u32int start_dtors, end_dtors; + +#define APP(t) static t the_app; \ + Application *app = &the_app; + +class Application { + public: + Process pr; + Vector args; + + Application() : pr(Process::get()), args(pr.argc()) { + for (u32int i = 0; i < args.size(); i++) args[i] = pr.argv(i); + //TODO : add default signal handlers + } + virtual ~Application() {} + + virtual int run() = 0; //Application's main loop + + virtual void doEvents() { + //TODO : handle signals (IPC featurs to come) + } + + void exit(u32int ret) { + //Call static destructors + for(u32int * call = &start_dtors; call < &end_dtors; call++) { + ((void (*)(void))*call)(); + } + threadFinishedSyscall(ret); + } +}; + +extern Application *app; + +#endif + diff --git a/Source/Library/Userland/App/ShellApp.proto.h b/Source/Library/Userland/App/ShellApp.proto.h new file mode 100644 index 0000000..846f2b1 --- /dev/null +++ b/Source/Library/Userland/App/ShellApp.proto.h @@ -0,0 +1,18 @@ +#ifndef DEF_SHELLAPP_CLASS_H +#define DEF_SHELLAPP_CLASS_H + +#include +#include + +class ShellApp : public Application { + public: + VirtualTerminal invt, outvt; + + ShellApp() : Application(), invt(VirtualTerminal::getIn()), outvt(VirtualTerminal::getOut()) { + if (!invt.valid()) exit(1); + if (!outvt.valid()) exit(2); + } +}; + +#endif + diff --git a/Source/Library/Userland/Binding/VirtualTerminal.class.h b/Source/Library/Userland/Binding/VirtualTerminal.class.h index c8a4123..70c6b23 100644 --- a/Source/Library/Userland/Binding/VirtualTerminal.class.h +++ b/Source/Library/Userland/Binding/VirtualTerminal.class.h @@ -71,6 +71,4 @@ class VirtualTerminal : public RessourceCaller { inline VirtualTerminal& operator<<(u32int i) { writeHex(i); return *this; } }; -extern VirtualTerminal invt, outvt; - #endif diff --git a/Source/Library/Userland/Start.cpp b/Source/Library/Userland/Start.cpp index f1d1771..cb9faf9 100644 --- a/Source/Library/Userland/Start.cpp +++ b/Source/Library/Userland/Start.cpp @@ -1,46 +1,37 @@ #include #include -#include + +#include #include -extern u32int start_ctors, end_ctors, start_dtors, end_dtors; +extern u32int start_ctors, end_ctors; -Heap heap; +Heap *heap; VirtualTerminal invt(0), outvt(0); int main(const Vector& args); extern "C" void start() { - heap.create(0x40000000, 0x00040000, 0x00004000); //Initially create a 256ko heap with 16ko index + Heap h; + h.create(0x40000000, 0x00040000, 0x00004000); //Initially create a 256ko heap with 16ko index + heap = &h; - //Call static constructors + //Call static constructors (this will construct the Application object and get some stuff (arguments, ...)) u32int i = 0; for(u32int * call = &start_ctors; call < &end_ctors; call++) { ((void (*)(void))*call)(); } - invt = VirtualTerminal::getIn(); outvt = VirtualTerminal::getOut(); - if (!invt.valid()) threadFinishedSyscall(1); - if (!outvt.valid()) threadFinishedSyscall(2); - - u32int argc = Process::get().argc(); - Vector args(argc); - for (u32int i = 0; i < argc; i++) args[i] = Process::get().argv(i); - - u32int r = main(args); - - //Call static destructors - for(u32int * call = &start_dtors; call < &end_dtors; call++) { - ((void (*)(void))*call)(); - } - - threadFinishedSyscall(r); + app->doEvents(); + u32int r = app->run(); + app->doEvents(); + app->exit(r); //Will call static destructors } namespace Mem { - void* alloc (size_t sz) { return heap.alloc(sz); } - void free(void* ptr) { heap.free(ptr); } + void* alloc (size_t sz) { return heap->alloc(sz); } + void free(void* ptr) { heap->free(ptr); } void* mkXchgSpace (size_t sz) { return alloc(sz); } } -- cgit v1.2.3 From 4f9078c0f06e0cf0cb7bb164fc72fb9918c68e6a Mon Sep 17 00:00:00 2001 From: Alexis211 Date: Thu, 17 Dec 2009 11:18:19 +0100 Subject: Added some option handling featurs to the default ShellApp class --- Source/Applications/Demos/GOL.cpp | 2 +- Source/Applications/Demos/cxxdemo.cpp | 2 +- Source/Applications/PaperWork/PaperWork.cpp | 20 ++-- Source/Applications/Shell/Shell.class.cpp | 2 +- Source/Kernel/Shell/KernelShell-sys.class.cpp | 2 +- Source/Library/Common/CMem.ns.cpp | 14 ++- Source/Library/Common/CMem.ns.h | 2 +- Source/Library/Common/String.class.cpp | 1 + Source/Library/Common/String.class.h | 1 + Source/Library/Common/cppsupport.wtf.cpp | 2 +- Source/Library/Makefile | 1 + Source/Library/Userland/App/Application.proto.h | 5 +- Source/Library/Userland/App/ShellApp.proto.cpp | 139 ++++++++++++++++++++++++ Source/Library/Userland/App/ShellApp.proto.h | 31 +++++- Source/Library/Userland/Start.cpp | 3 +- 15 files changed, 197 insertions(+), 30 deletions(-) create mode 100644 Source/Library/Userland/App/ShellApp.proto.cpp (limited to 'Source') diff --git a/Source/Applications/Demos/GOL.cpp b/Source/Applications/Demos/GOL.cpp index cd6e719..675c159 100644 --- a/Source/Applications/Demos/GOL.cpp +++ b/Source/Applications/Demos/GOL.cpp @@ -6,7 +6,7 @@ class GOL : public ShellApp { public: - GOL() : ShellApp() {} + GOL() : ShellApp("GOL.app", "Melon's Game of Life simulator \\o/") {} int run(); }; diff --git a/Source/Applications/Demos/cxxdemo.cpp b/Source/Applications/Demos/cxxdemo.cpp index 372e69f..36cfd27 100644 --- a/Source/Applications/Demos/cxxdemo.cpp +++ b/Source/Applications/Demos/cxxdemo.cpp @@ -6,7 +6,7 @@ class CPPDemo : public ShellApp { public: - CPPDemo() : ShellApp() {} + CPPDemo() : ShellApp("CPPDemo.app", "A demo application using the C++ Melon framework") {} int run(); }; diff --git a/Source/Applications/PaperWork/PaperWork.cpp b/Source/Applications/PaperWork/PaperWork.cpp index 0d6a821..4f9cb0e 100644 --- a/Source/Applications/PaperWork/PaperWork.cpp +++ b/Source/Applications/PaperWork/PaperWork.cpp @@ -4,7 +4,11 @@ class PaperWork : public ShellApp { public: - PaperWork() : ShellApp() {} + PaperWork() : ShellApp("PaperWork.app", "Melon's init/login manager") { + addFlag("s", "shell", "Define the default shell to launch", FT_STR, DEFAULT_SHELL); + addFlag("l", "login", "Act as a login manager"); + addFlag("i", "init", "Act as a init manager", FT_BOOL, "on"); + } int run(); }; @@ -12,13 +16,7 @@ APP(PaperWork); int PaperWork::run() { String act = "init"; - if (args.size() == 2) { - if (args[1] == "login") { - act = "login"; - } else if (args[1] == "init") { - act = "init"; - } - } + if (bFlag("login")) act = "login"; if (act == "init") { while (1) { @@ -26,7 +24,7 @@ int PaperWork::run() { if (p.valid()) { p.setInVT(invt); p.setOutVT(outvt); - p.pushArg("login"); + p.pushArg("--login"); p.start(); p.wait(); } else { @@ -45,9 +43,9 @@ int PaperWork::run() { outvt << "Authentication failed.\n\n"; continue; } - outvt << "What shell to run [" << DEFAULT_SHELL << "]? "; + outvt << "What shell to run [" << sFlag("shell") << "]? "; String sh = invt.readLine(); - if (sh == "") sh = DEFAULT_SHELL; + if (sh == "") sh = sFlag("shell"); Process p = Process::run(sh); if (p.valid()) { p.setInVT(invt); diff --git a/Source/Applications/Shell/Shell.class.cpp b/Source/Applications/Shell/Shell.class.cpp index 42743e7..4e0f18b 100644 --- a/Source/Applications/Shell/Shell.class.cpp +++ b/Source/Applications/Shell/Shell.class.cpp @@ -4,7 +4,7 @@ APP(Shell); -Shell::Shell() : ShellApp(), cwd(FS::cwdNode()) { +Shell::Shell() : ShellApp("Shell.app", "The Melon command line interpreter"), cwd(FS::cwdNode()) { } int Shell::run() { diff --git a/Source/Kernel/Shell/KernelShell-sys.class.cpp b/Source/Kernel/Shell/KernelShell-sys.class.cpp index da5378b..85ba207 100644 --- a/Source/Kernel/Shell/KernelShell-sys.class.cpp +++ b/Source/Kernel/Shell/KernelShell-sys.class.cpp @@ -77,7 +77,7 @@ void KernelShell::mount(Vector& args) { if (VFS::mount(args[1], m_vt)) *m_vt << "Ok, filesystem mounted.\n"; } } else { - *m_vt << "Usage: mount [ \n"; + *m_vt << "Wrong options. Type 'mount help' for more info.\n"; } } diff --git a/Source/Library/Common/CMem.ns.cpp b/Source/Library/Common/CMem.ns.cpp index 37cdf0c..a4da4d2 100644 --- a/Source/Library/Common/CMem.ns.cpp +++ b/Source/Library/Common/CMem.ns.cpp @@ -3,9 +3,17 @@ namespace CMem { //Standard C functions -u8int *memcpy(u8int *dest, const u8int *src, int count) { - for (int i = 0; i < count; i++) { - dest[i] = src[i]; +u8int *memcpy(u8int *dest, const u8int *src, u32int count) { + u32int f = count % 4, n = count / 4; + const u32int* s = (u32int*)src; + u32int* d = (u32int*)dest; + for (u32int i = 0; i < n; i++) { + d[i] = s[i]; + } + if (f != 0) { + for (u32int i = count - f; i < count; i++) { + dest[i] = src[i]; + } } return dest; } diff --git a/Source/Library/Common/CMem.ns.h b/Source/Library/Common/CMem.ns.h index f0c15da..aecddc9 100644 --- a/Source/Library/Common/CMem.ns.h +++ b/Source/Library/Common/CMem.ns.h @@ -6,7 +6,7 @@ //This namespace contains basic memory managment functions namespace CMem { - u8int *memcpy(u8int *dest, const u8int *src, int count); + u8int *memcpy(u8int *dest, const u8int *src, u32int count); u8int *memset(u8int *dest, u8int val, int count); u16int *memsetw(u16int *dest, u16int val, int count); u32int strlen(const char *str); diff --git a/Source/Library/Common/String.class.cpp b/Source/Library/Common/String.class.cpp index fe851bd..8e958e0 100644 --- a/Source/Library/Common/String.class.cpp +++ b/Source/Library/Common/String.class.cpp @@ -195,6 +195,7 @@ String String::substr(s32int start, s32int size) { if (size == 0) return String(); if (start < 0) start = m_length - start; if (size == -1) size = m_length - start; + if (start + size >= (int)m_length) size = m_length - start; String ret; ret.m_string = new WChar[size + 1]; ret.m_length = size; diff --git a/Source/Library/Common/String.class.h b/Source/Library/Common/String.class.h index b623fb2..14f07f0 100644 --- a/Source/Library/Common/String.class.h +++ b/Source/Library/Common/String.class.h @@ -14,6 +14,7 @@ class String : public BasicString { String(const char* string, u8int encoding = UE_UTF8); String() : BasicString() {} + String(WChar c, u32int n = 1) : BasicString(c, n) {} String(const String &other) : BasicString (other) {} void affect(const char* string, u8int encoding = UE_UTF8); diff --git a/Source/Library/Common/cppsupport.wtf.cpp b/Source/Library/Common/cppsupport.wtf.cpp index 06ef1b9..59a78fc 100644 --- a/Source/Library/Common/cppsupport.wtf.cpp +++ b/Source/Library/Common/cppsupport.wtf.cpp @@ -2,7 +2,7 @@ #include namespace CMem { - u8int* memcpy(u8int*, const u8int*, int); + u8int* memcpy(u8int*, const u8int*, u32int); }; using namespace CMem; diff --git a/Source/Library/Makefile b/Source/Library/Makefile index 5c77c84..b3040d4 100644 --- a/Source/Library/Makefile +++ b/Source/Library/Makefile @@ -20,6 +20,7 @@ Objects = Common/WChar.class.uo \ Common/ByteArray.class.uo \ Common/Rand.ns.uo \ Common/cppsupport.wtf.uo \ + Userland/App/ShellApp.proto.uo \ Userland/Syscall/Syscall.wtf.uo \ Userland/Syscall/RessourceCaller.class.uo \ Userland/Start.uo diff --git a/Source/Library/Userland/App/Application.proto.h b/Source/Library/Userland/App/Application.proto.h index 1f3a963..46b0975 100644 --- a/Source/Library/Userland/App/Application.proto.h +++ b/Source/Library/Userland/App/Application.proto.h @@ -12,14 +12,13 @@ extern u32int start_dtors, end_dtors; class Application { public: Process pr; - Vector args; - Application() : pr(Process::get()), args(pr.argc()) { - for (u32int i = 0; i < args.size(); i++) args[i] = pr.argv(i); + Application() : pr(Process::get()) { //TODO : add default signal handlers } virtual ~Application() {} + virtual void init() {} //Do anything that can't be done in the constructor virtual int run() = 0; //Application's main loop virtual void doEvents() { diff --git a/Source/Library/Userland/App/ShellApp.proto.cpp b/Source/Library/Userland/App/ShellApp.proto.cpp new file mode 100644 index 0000000..a3d9737 --- /dev/null +++ b/Source/Library/Userland/App/ShellApp.proto.cpp @@ -0,0 +1,139 @@ +#include "ShellApp.proto.h" + +ShellApp::ShellApp(String name, String desc) + : Application(), invt(VirtualTerminal::getIn()), outvt(VirtualTerminal::getOut()) { + appName = name, appDesc = desc; + if (!invt.valid()) exit(1); + if (!outvt.valid()) exit(2); + + addFlag("h", "help", "Show this help screen"); +} + +void ShellApp::init() { + //Parse flags + u32int argc = pr.argc(); + for (u32int i = 0; i < argc; i++) { + String arg = pr.argv(i); + if (arg == "-") { + i++; + if (i == argc) { + args.push("-"); + } else { + for (; i < argc; i++) args.push(pr.argv(i)); + } + } else if (arg[0] == WChar("-")) { + if (arg.substr(0, 5) == "--no-") { + bool found = false; + for (u32int i = 0; i < flags.size(); i++) { + if (flags[i].type == FT_BOOL) { + if (arg == String("--no-") + flags[i].lName) { + flags[i].boolVal = false; + found = true; + } + } + } + if (!found) outvt << "Unknown option : " << arg << "\n"; + } else if (arg.substr(0, 2) == "--") { + bool found = false; + for (u32int i = 0; i < flags.size(); i++) { + if (flags[i].type == FT_BOOL) { + if (arg == String("--") + flags[i].lName) { + flags[i].boolVal = true; + found = true; + } + } else { + if (arg.substr(2, 1 + flags[i].lName.size()) == flags[i].lName + "=") { + found = true; + flags[i].strVal = arg.substr(3 + flags[i].lName.size(), arg.size()); + if (flags[i].type == FT_INT) flags[i].intVal = flags[i].strVal.toInt(); + } + } + } + if (!found) outvt << "Unknown option : " << arg << "\n"; + } else { + for (u32int j = 1; j < arg.size(); j++) { + bool found = false; + for (u32int k = 0; k < flags.size(); k++) { + if (flags[k].sName == arg[j]) { + found = true; + if (flags[k].type == FT_BOOL) flags[k].boolVal = true; + if (flags[k].type == FT_INT) flags[k].intVal = pr.argv(++i).toInt(); + if (flags[k].type == FT_STR) flags[k].strVal = pr.argv(++i); + break; + } + } + if (!found) { + outvt << "Unknown option : -" << String(arg[j]) << "\n"; + exit(-1); + } + } + } + } else { + args.push(arg); + } + } + + //Eventually show help screen + if (bFlag("help")) { + outvt << appName << ": " << appDesc << "\n"; + outvt << "Usage: \t" << appName << " [-] \n\n"; + outvt << "Possible flags :\n"; + for (u32int i = 0; i < flags.size(); i++) { + outvt << " --" << flags[i].lName << "\t" << (flags[i].sName != 0 ? "-" : "") << String(flags[i].sName) << "\t"; + if (flags[i].type == FT_STR) outvt << "Default: " << flags[i].strVal << "\n"; + if (flags[i].type == FT_INT) outvt << "Default: " << flags[i].intVal << "\n"; + if (flags[i].type == FT_BOOL) outvt << "Default: " << (flags[i].boolVal && flags[i].lName != "help" ? "on" : "off") << "\n"; + if (!flags[i].desc.empty()) outvt << "\t" << flags[i].desc << "\n"; + outvt << "\n"; + } + exit(0); + } +} + +//******************** FLAG HANDLING ***************** + +flag_t *ShellApp::getFlag(String name) { + for (u32int i = 0; i < flags.size(); i++) { + if (flags[i].lName == name) return &flags[i]; + } + return NULL; +} + +void ShellApp::addFlag(WChar sName, String lName, String desc, int type, String deflt) { + if (lName.empty()) return; + if (getFlag(lName) != false) return; + flag_t temp; + temp.sName = sName; + temp.lName = lName; + temp.type = type; + temp.desc = desc; + if (type == FT_BOOL) { + temp.boolVal = !deflt.empty(); + } else if (type == FT_STR) { + temp.strVal = deflt; + } else if (type == FT_INT) { + temp.intVal = deflt.toInt(); + } + flags.push(temp); +} + +String ShellApp::sFlag(String name) { + flag_t* f = getFlag(name); + if (f == 0) return ""; + if (f->type != FT_STR) return ""; + return f->strVal; +} + +int ShellApp::iFlag(String name) { + flag_t* f = getFlag(name); + if (f == 0) return 0; + if (f->type != FT_INT) return 0; + return f->intVal; +} + +bool ShellApp::bFlag(String name) { + flag_t *f = getFlag(name); + if (f == 0) return false; + if (f->type != FT_BOOL) return false; + return f->boolVal; +} diff --git a/Source/Library/Userland/App/ShellApp.proto.h b/Source/Library/Userland/App/ShellApp.proto.h index 846f2b1..5575112 100644 --- a/Source/Library/Userland/App/ShellApp.proto.h +++ b/Source/Library/Userland/App/ShellApp.proto.h @@ -4,14 +4,35 @@ #include #include +#define FT_BOOL 1 +#define FT_STR 2 +#define FT_INT 3 + +struct flag_t { + WChar sName; + String lName, desc; + int type; + String strVal; + int intVal; + bool boolVal; +}; + class ShellApp : public Application { public: VirtualTerminal invt, outvt; - - ShellApp() : Application(), invt(VirtualTerminal::getIn()), outvt(VirtualTerminal::getOut()) { - if (!invt.valid()) exit(1); - if (!outvt.valid()) exit(2); - } + Vector args; + Vector flags; + String appName, appDesc; + ShellApp(String name, String desc); + + virtual void init(); + + //Flag handling + flag_t* getFlag(String name); + void addFlag(WChar sName, String lName, String desc, int type = FT_BOOL, String deflt = ""); + String sFlag(String name); + int iFlag(String name); + bool bFlag(String name); }; #endif diff --git a/Source/Library/Userland/Start.cpp b/Source/Library/Userland/Start.cpp index cb9faf9..3f825e9 100644 --- a/Source/Library/Userland/Start.cpp +++ b/Source/Library/Userland/Start.cpp @@ -9,8 +9,6 @@ extern u32int start_ctors, end_ctors; Heap *heap; -VirtualTerminal invt(0), outvt(0); - int main(const Vector& args); extern "C" void start() { @@ -24,6 +22,7 @@ extern "C" void start() { ((void (*)(void))*call)(); } + app->init(); app->doEvents(); u32int r = app->run(); app->doEvents(); -- cgit v1.2.3 From 66f21576d9f9dee507995c520463c5fa94b66a49 Mon Sep 17 00:00:00 2001 From: Alexis211 Date: Fri, 18 Dec 2009 15:42:27 +0100 Subject: Nothing, really --- Source/Applications/Shell/Shell.class.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source') diff --git a/Source/Applications/Shell/Shell.class.cpp b/Source/Applications/Shell/Shell.class.cpp index 4e0f18b..3d406c8 100644 --- a/Source/Applications/Shell/Shell.class.cpp +++ b/Source/Applications/Shell/Shell.class.cpp @@ -66,7 +66,7 @@ int Shell::run() { } else if (cmd[0] == "free") { outvt << "Free RAM : " << (s64int)Sys::freeRam() << " Kio of " << (s64int)Sys::totalRam() << " Kio\n"; } else if (cmd[0] == "uid") { - outvt << "User ID : " << (s64int)(Process::get().getUid()) << "\n"; + outvt << "User ID : " << (s64int)(pr.getUid()) << "\n"; } else if (cmd[0] == "help") { while (cmd.size() > 1) cmd.pop(); cmd.push("/Applications/Shell/Help.txt"); -- cgit v1.2.3 From 6dc28352073c473a35a9d51796b02361c6c6486c Mon Sep 17 00:00:00 2001 From: Alexis211 Date: Sat, 19 Dec 2009 12:46:55 +0100 Subject: Nothing, really --- Source/Kernel/Devices/Display/VESADisplay.class.cpp | 19 ++++++++++++++++++- Source/Kernel/Makefile | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) (limited to 'Source') diff --git a/Source/Kernel/Devices/Display/VESADisplay.class.cpp b/Source/Kernel/Devices/Display/VESADisplay.class.cpp index f882d5b..d4aee07 100644 --- a/Source/Kernel/Devices/Display/VESADisplay.class.cpp +++ b/Source/Kernel/Devices/Display/VESADisplay.class.cpp @@ -245,7 +245,24 @@ void VESADisplay::drawChar(u16int line, u16int col, WChar c, u8int color) { fgcolor = color & 0xF; bgcolor = (color >> 4) & 0xF; } - + + if (c == WChar(" ")) { + u8int* p = memPos(sx, sy); + for (int y = 0; y < C_FONT_HEIGHT; y++) { + if (m_pixWidth == 1) memset(p, bgcolor, 9); + if (m_pixWidth == 2) memsetw((u16int*)p, bgcolor, 9); + if (m_pixWidth == 3) { + for (int i = 0; i < 9; i++) { + p[0] = (bgcolor >> 16); + p[1] = (bgcolor >> 8); + p[2] = (bgcolor); + p += 3; + } + } + p += m_currMode.pitch; + } + return; + } int y = 0; for (u8int* p = memPos(sx, sy); p < memPos(sx, sy + C_FONT_HEIGHT); p += m_currMode.pitch) { diff --git a/Source/Kernel/Makefile b/Source/Kernel/Makefile index 660017a..0f244d5 100644 --- a/Source/Kernel/Makefile +++ b/Source/Kernel/Makefile @@ -5,7 +5,7 @@ CXX = g++ LD = ld LDFLAGS = -T Link.ld -Map Map.txt --oformat=elf32-i386 CFLAGS = -nostdlib -nostartfiles -nodefaultlibs -fno-builtin -fno-stack-protector -Wall -Wextra -Werror -I . -CXXFLAGS = -nostartfiles -nostdlib -fno-exceptions -fno-rtti -I . -I ../Library/Common -I ../Library/Interface -Wall -Werror -Wno-write-strings -funsigned-char -D THIS_IS_MELON_KERNEL -D RANDOM_SEED=1`date +%N`LL +CXXFLAGS = -nostartfiles -nostdlib -fno-exceptions -fno-rtti -I . -I ../Library/Common -I ../Library/Interface -Wall -Werror -Wno-write-strings -funsigned-char -D THIS_IS_MELON_KERNEL -D RANDOM_SEED=1`date +%N`LL -O2 ASM = nasm ASMFLAGS = -f elf -- cgit v1.2.3