summaryrefslogtreecommitdiff
path: root/Source/Library/Userland
diff options
context:
space:
mode:
authorAlexis211 <alexis211@gmail.com>2009-12-17 11:18:19 +0100
committerAlexis211 <alexis211@gmail.com>2009-12-17 11:18:19 +0100
commit4f9078c0f06e0cf0cb7bb164fc72fb9918c68e6a (patch)
tree34815307b54bf8639ba89f2efddf03e96ee4c0d0 /Source/Library/Userland
parent3d5aca66b9712758aecb2e80bc5b2fb3df6256a4 (diff)
downloadMelon-4f9078c0f06e0cf0cb7bb164fc72fb9918c68e6a.tar.gz
Melon-4f9078c0f06e0cf0cb7bb164fc72fb9918c68e6a.zip
Added some option handling featurs to the default ShellApp class
Diffstat (limited to 'Source/Library/Userland')
-rw-r--r--Source/Library/Userland/App/Application.proto.h5
-rw-r--r--Source/Library/Userland/App/ShellApp.proto.cpp139
-rw-r--r--Source/Library/Userland/App/ShellApp.proto.h31
-rw-r--r--Source/Library/Userland/Start.cpp3
4 files changed, 168 insertions, 10 deletions
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<String> 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 << " <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
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 <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;
-
- ShellApp() : Application(), invt(VirtualTerminal::getIn()), outvt(VirtualTerminal::getOut()) {
- if (!invt.valid()) exit(1);
- if (!outvt.valid()) exit(2);
- }
+ 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/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<String>& 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();