summaryrefslogtreecommitdiff
path: root/Source
diff options
context:
space:
mode:
authorAlexis211 <alexis211@gmail.com>2009-12-16 18:22:58 +0100
committerAlexis211 <alexis211@gmail.com>2009-12-16 18:22:58 +0100
commit3d5aca66b9712758aecb2e80bc5b2fb3df6256a4 (patch)
tree817e945218dbca029244315f36b960dd288b3cff /Source
parent5f87c447bdcb82beacbfb930942fe9995dcdb60f (diff)
downloadMelon-3d5aca66b9712758aecb2e80bc5b2fb3df6256a4.tar.gz
Melon-3d5aca66b9712758aecb2e80bc5b2fb3df6256a4.zip
New model, object-oriented, for applications
Diffstat (limited to 'Source')
-rw-r--r--Source/Applications/Demos/GOL.cpp14
-rw-r--r--Source/Applications/Demos/cxxdemo.cpp14
-rw-r--r--Source/Applications/PaperWork/Makefile2
-rw-r--r--Source/Applications/PaperWork/PaperWork.cpp (renamed from Source/Applications/PaperWork/main.cpp)13
-rw-r--r--Source/Applications/Shell/Makefile5
-rw-r--r--Source/Applications/Shell/Shell-fs.class.cpp (renamed from Source/Applications/Shell/Shell-fs.ns.cpp)22
-rw-r--r--Source/Applications/Shell/Shell.class.cpp (renamed from Source/Applications/Shell/Shell.ns.cpp)32
-rw-r--r--Source/Applications/Shell/Shell.class.h28
-rw-r--r--Source/Applications/Shell/Shell.ns.h18
-rw-r--r--Source/Library/Userland/App/Application.proto.h41
-rw-r--r--Source/Library/Userland/App/ShellApp.proto.h18
-rw-r--r--Source/Library/Userland/Binding/VirtualTerminal.class.h2
-rw-r--r--Source/Library/Userland/Start.cpp37
13 files changed, 160 insertions, 86 deletions
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 <Binding/VirtualTerminal.class.h>
#include <Binding/Thread.class.h>
-#include <String.class.h>
#include <ByteArray.class.h>
#include <Rand.ns.h>
-int main(Vector<String> args) {
+#include <App/ShellApp.proto.h>
+
+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 <Syscall/Syscall.wtf.h>
-#include <String.class.h>
-#include <Binding/VirtualTerminal.class.h>
#include <Binding/Thread.class.h>
#include <Binding/File.class.h>
-int main(const Vector<String>& args) {
+#include <App/ShellApp.proto.h>
+
+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/main.cpp b/Source/Applications/PaperWork/PaperWork.cpp
index 2fb40df..0d6a821 100644
--- a/Source/Applications/PaperWork/main.cpp
+++ b/Source/Applications/PaperWork/PaperWork.cpp
@@ -1,9 +1,16 @@
-#include <Binding/Process.class.h>
-#include <String.class.h>
+#include <App/ShellApp.proto.h>
#define DEFAULT_SHELL "/Applications/Shell/Shell.app"
-int main(Vector<String> args) {
+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") {
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.ns.cpp b/Source/Applications/Shell/Shell-fs.class.cpp
index 0b51299..150b877 100644
--- a/Source/Applications/Shell/Shell-fs.ns.cpp
+++ b/Source/Applications/Shell/Shell-fs.class.cpp
@@ -1,10 +1,8 @@
-#include "Shell.ns.h"
+#include "Shell.class.h"
#include <TextFile.class.h>
#include <Binding/Process.class.h>
-namespace Shell {
-
-void ls(Vector<String>& args) {
+void Shell::ls(Vector<String>& args) {
FSNode d = cwd;
if (args.size() == 2) {
FSNode n = FS::find(args[1], cwd);
@@ -41,7 +39,7 @@ void ls(Vector<String>& args) {
}
}
-void cd(Vector<String>& args) {
+void Shell::cd(Vector<String>& args) {
if (args.size() != 2) {
outvt << "Invalid argument count.\n";
} else {
@@ -57,11 +55,11 @@ void cd(Vector<String>& args) {
}
}
-void pwd(Vector<String>& args) {
+void Shell::pwd(Vector<String>& args) {
outvt << "Current directory : " << cwd.path() << "\n";
}
-void rm(Vector<String>& args) {
+void Shell::rm(Vector<String>& 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()) {
@@ -70,7 +68,7 @@ void rm(Vector<String>& args) {
}
}
-void mkdir(Vector<String>& args) {
+void Shell::mkdir(Vector<String>& 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()) {
@@ -79,7 +77,7 @@ void mkdir(Vector<String>& args) {
}
}
-void cat(Vector<String>& args) {
+void Shell::cat(Vector<String>& args) {
if (args.size() == 1) outvt << "Meow.\n";
for (u32int i = 1; i < args.size(); i++) {
TextFile f(args[i], FM_READ, cwd);
@@ -94,7 +92,7 @@ void cat(Vector<String>& args) {
}
}
-void wf(Vector<String>& args) {
+void Shell::wf(Vector<String>& args) {
if (args.size() == 1) {
outvt << "No file to write !\n";
} else {
@@ -113,7 +111,7 @@ void wf(Vector<String>& args) {
}
}
-void run(Vector<String>& args) {
+void Shell::run(Vector<String>& args) {
if (args.size() == 1) {
outvt << "Nothing to run...\n";
} else {
@@ -132,5 +130,3 @@ void run(Vector<String>& args) {
}
}
}
-
-}
diff --git a/Source/Applications/Shell/Shell.ns.cpp b/Source/Applications/Shell/Shell.class.cpp
index 56a6b5f..42743e7 100644
--- a/Source/Applications/Shell/Shell.ns.cpp
+++ b/Source/Applications/Shell/Shell.class.cpp
@@ -1,25 +1,25 @@
-#include "Shell.ns.h"
+#include "Shell.class.h"
#include <Binding/Sys.ns.h>
#include <Binding/Process.class.h>
-namespace Shell {
-
-FSNode cwd(0);
+APP(Shell);
+Shell::Shell() : ShellApp(), cwd(FS::cwdNode()) {
+}
-u32int run() {
+int Shell::run() {
struct { //Command list
String name;
- void (*cmd)(Vector<String>&);
+ void (Shell::*cmd)(Vector<String>&);
} commands[] = {
- {"ls", ls},
- {"cd", cd},
- {"pwd", pwd},
- {"rm", rm},
- {"mkdir", mkdir},
- {"cat", cat},
- {"wf", wf},
- {"run", run},
+ {"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}
};
@@ -80,7 +80,7 @@ u32int run() {
if (commands[i].cmd == 0) {
outvt << "Not implemented yet.\n";
} else {
- commands[i].cmd(cmd);
+ (this->*(commands[i].cmd))(cmd);
}
break;
}
@@ -91,6 +91,4 @@ u32int run() {
}
}
-}
-
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 <Binding/VirtualTerminal.class.h>
+#include <Binding/FSNode.class.h>
+#include <String.class.h>
+
+#include <App/ShellApp.proto.h>
+
+class Shell : public ShellApp {
+ public:
+ Shell();
+
+ int run();
+
+ FSNode cwd;
+
+ void ls(Vector<String>& args);
+ void cd(Vector<String>& args);
+ void pwd(Vector<String>& args);
+ void rm(Vector<String>& args);
+ void mkdir(Vector<String>& args);
+ void cat(Vector<String>& args);
+ void wf(Vector<String>& args);
+ void run(Vector<String>& args);
+};
+
+#endif
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 <Binding/VirtualTerminal.class.h>
-#include <Binding/FSNode.class.h>
-#include <String.class.h>
-
-namespace Shell {
- u32int run();
-
- extern FSNode cwd;
-
- extern void ls(Vector<String>& args);
- extern void cd(Vector<String>& args);
- extern void pwd(Vector<String>& args);
- extern void rm(Vector<String>& args);
- extern void mkdir(Vector<String>& args);
- extern void cat(Vector<String>& args);
- extern void wf(Vector<String>& args);
- extern void run(Vector<String>& 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 <Binding/Process.class.h>
+#include <String.class.h>
+
+extern u32int start_dtors, end_dtors;
+
+#define APP(t) static t the_app; \
+ Application *app = &the_app;
+
+class Application {
+ public:
+ Process pr;
+ Vector<String> 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 <App/Application.proto.h>
+#include <Binding/VirtualTerminal.class.h>
+
+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 <types.h>
#include <Syscall/Syscall.wtf.h>
-#include <Binding/VirtualTerminal.class.h>
+
+#include <App/Application.proto.h>
#include <Heap.class.h>
-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<String>& 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<String> 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); }
}