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 --- CopyToFDD.sh | 14 +-- 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 +++--- 17 files changed, 423 insertions(+), 349 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 diff --git a/CopyToFDD.sh b/CopyToFDD.sh index f5d93d6..7b8459a 100755 --- a/CopyToFDD.sh +++ b/CopyToFDD.sh @@ -7,10 +7,14 @@ cp Grub-menu-fdd.cfg Mount/boot/menu.cfg # Create directories mkdir Mount/{System,Applications,Volumes} -mkdir Mount/Applications/Shell +mkdir Mount/Applications/{Shell,Demos} mkdir Mount/System/{Applications,Logs,Configuration} mkdir Mount/Volumes/{HDD,InitRFS} +# Copy kernel and ramfs +cp Source/Kernel/Melon.ke Mount/System +cp Init.rfs Mount/System + # Copy system files cp Source/Kernel/Ressources/Configuration/* Mount/System/Configuration cp Source/Applications/PaperWork/PaperWork Mount/System/Applications/PaperWork.app @@ -21,12 +25,8 @@ echo "/Volumes/InitRFS:ramfs:0" > Mount/System/Configuration/Mount echo "/Volumes/HDD:block.ata:0:1" >> Mount/System/Configuration/Mount # Copy demo apps -cp Source/Applications/Demos/GOL Mount/Apps/GOL.app -cp Source/Applications/Demos/asmdemo Mount/Apps/ASM.dem - -# Copy kernel and ramfs -cp Source/Kernel/Melon.ke Mount/System -cp Init.rfs Mount/System +cp Source/Applications/Demos/GOL Mount/Applications/Demos/GOL.app +cp Source/Applications/Demos/asmdemo Mount/Applications/Demos/ASM.dem #echo "*** Launching a BASH shell, if you want to do any maintenance ***" #bash || exit 0 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