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 ---- 12 files changed, 343 insertions(+), 317 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 (limited to 'Source/Applications') 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); -} -- cgit v1.2.3