summaryrefslogtreecommitdiff
path: root/Source/Library
diff options
context:
space:
mode:
Diffstat (limited to 'Source/Library')
-rw-r--r--Source/Library/Common/CMem.ns.cpp14
-rw-r--r--Source/Library/Common/CMem.ns.h2
-rw-r--r--Source/Library/Common/String.class.cpp1
-rw-r--r--Source/Library/Common/String.class.h1
-rw-r--r--Source/Library/Common/cppsupport.wtf.cpp2
-rw-r--r--Source/Library/Makefile1
-rw-r--r--Source/Library/Userland/App/Application.proto.h40
-rw-r--r--Source/Library/Userland/App/ShellApp.proto.cpp139
-rw-r--r--Source/Library/Userland/App/ShellApp.proto.h39
-rw-r--r--Source/Library/Userland/Binding/VirtualTerminal.class.h2
-rw-r--r--Source/Library/Userland/Start.cpp40
11 files changed, 249 insertions, 32 deletions
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<WChar> {
String(const char* string, u8int encoding = UE_UTF8);
String() : BasicString<WChar>() {}
+ String(WChar c, u32int n = 1) : BasicString<WChar>(c, n) {}
String(const String &other) : BasicString<WChar> (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 <types.h>
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
new file mode 100644
index 0000000..46b0975
--- /dev/null
+++ b/Source/Library/Userland/App/Application.proto.h
@@ -0,0 +1,40 @@
+#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;
+
+ 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() {
+ //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.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 << " <flags> [-] <arguments>\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
new file mode 100644
index 0000000..5575112
--- /dev/null
+++ b/Source/Library/Userland/App/ShellApp.proto.h
@@ -0,0 +1,39 @@
+#ifndef DEF_SHELLAPP_CLASS_H
+#define DEF_SHELLAPP_CLASS_H
+
+#include <App/Application.proto.h>
+#include <Binding/VirtualTerminal.class.h>
+
+#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;
+ Vector<String> args;
+ Vector<flag_t> 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/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..3f825e9 100644
--- a/Source/Library/Userland/Start.cpp
+++ b/Source/Library/Userland/Start.cpp
@@ -1,46 +1,36 @@
#include <types.h>
#include <Syscall/Syscall.wtf.h>
-#include <Binding/VirtualTerminal.class.h>
-#include <Heap.class.h>
+#include <App/Application.proto.h>
-extern u32int start_ctors, end_ctors, start_dtors, end_dtors;
+#include <Heap.class.h>
-Heap heap;
+extern u32int start_ctors, end_ctors;
-VirtualTerminal invt(0), outvt(0);
+Heap *heap;
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->init();
+ 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); }
}