summaryrefslogtreecommitdiff
path: root/Source/Library/Userland
diff options
context:
space:
mode:
authorAlexis211 <alexis211@gmail.com>2009-12-20 20:03:22 +0100
committerAlexis211 <alexis211@gmail.com>2009-12-20 20:03:22 +0100
commit09353da6e91a0968ae24d4b4d97ed434520e6217 (patch)
tree016694d12e9dfebc773cef629eb56556d0024b22 /Source/Library/Userland
parent247070cc7e5ae117fd0d1b551fafdf5c13f0dd6b (diff)
parent18454dc8be12827a84c2ebc58aa5d31bb44e1e6a (diff)
downloadMelon-09353da6e91a0968ae24d4b4d97ed434520e6217.tar.gz
Melon-09353da6e91a0968ae24d4b4d97ed434520e6217.zip
Merge branch 'framework'
Conflicts: .gitignore
Diffstat (limited to 'Source/Library/Userland')
-rw-r--r--Source/Library/Userland/App/ShellApp.proto.cpp34
-rw-r--r--Source/Library/Userland/App/ShellApp.proto.h3
-rw-r--r--Source/Library/Userland/App/StreamApp.proto.cpp40
-rw-r--r--Source/Library/Userland/App/StreamApp.proto.h27
-rw-r--r--Source/Library/Userland/Binding/Process.class.h4
-rw-r--r--Source/Library/Userland/Binding/VirtualTerminal.class.h32
-rw-r--r--Source/Library/Userland/Syscall/RessourceCaller.class.h4
7 files changed, 118 insertions, 26 deletions
diff --git a/Source/Library/Userland/App/ShellApp.proto.cpp b/Source/Library/Userland/App/ShellApp.proto.cpp
index a3d9737..123eb73 100644
--- a/Source/Library/Userland/App/ShellApp.proto.cpp
+++ b/Source/Library/Userland/App/ShellApp.proto.cpp
@@ -1,6 +1,6 @@
#include "ShellApp.proto.h"
-ShellApp::ShellApp(String name, String desc)
+ShellApp::ShellApp(const String &name, const String &desc)
: Application(), invt(VirtualTerminal::getIn()), outvt(VirtualTerminal::getOut()) {
appName = name, appDesc = desc;
if (!invt.valid()) exit(1);
@@ -9,11 +9,17 @@ ShellApp::ShellApp(String name, String desc)
addFlag("h", "help", "Show this help screen");
}
+ShellApp::~ShellApp() {
+ outvt << END;
+}
+
void ShellApp::init() {
//Parse flags
u32int argc = pr.argc();
- for (u32int i = 0; i < argc; i++) {
+ args.clear();
+ for (u32int i = 1; i < argc; i++) {
String arg = pr.argv(i);
+ if (arg.empty()) continue;
if (arg == "-") {
i++;
if (i == argc) {
@@ -26,13 +32,13 @@ void ShellApp::init() {
bool found = false;
for (u32int i = 0; i < flags.size(); i++) {
if (flags[i].type == FT_BOOL) {
- if (arg == String("--no-") + flags[i].lName) {
+ if (arg == (String("--no-") += flags[i].lName)) {
flags[i].boolVal = false;
found = true;
}
}
}
- if (!found) outvt << "Unknown option : " << arg << "\n";
+ if (!found) outvt << "Unknown option : " << arg << ENDL;
} else if (arg.substr(0, 2) == "--") {
bool found = false;
for (u32int i = 0; i < flags.size(); i++) {
@@ -49,21 +55,29 @@ void ShellApp::init() {
}
}
}
- if (!found) outvt << "Unknown option : " << arg << "\n";
+ if (!found) outvt << "Unknown option : " << arg << ENDL;
} 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);
+ if (flags[k].type == FT_BOOL) {
+ flags[k].boolVal = true;
+ } else {
+ i++;
+ if (i >= argc) {
+ outvt << "Missing argument for flag : -" << String(arg[j], 1) << ENDL;
+ } else {
+ flags[k].strVal = pr.argv(i);
+ if (flags[k].type == FT_INT) flags[k].intVal = flags[k].strVal.toInt();
+ }
+ }
break;
}
}
if (!found) {
- outvt << "Unknown option : -" << String(arg[j]) << "\n";
+ outvt << "Unknown option : -" << String(arg[j], 1) << ENDL;
exit(-1);
}
}
@@ -76,7 +90,7 @@ void ShellApp::init() {
//Eventually show help screen
if (bFlag("help")) {
outvt << appName << ": " << appDesc << "\n";
- outvt << "Usage: \t" << appName << " <flags> [-] <arguments>\n\n";
+ outvt << "Usage: \t" << pr.argv(0) << " <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";
diff --git a/Source/Library/Userland/App/ShellApp.proto.h b/Source/Library/Userland/App/ShellApp.proto.h
index 5575112..bc57cd0 100644
--- a/Source/Library/Userland/App/ShellApp.proto.h
+++ b/Source/Library/Userland/App/ShellApp.proto.h
@@ -23,7 +23,8 @@ class ShellApp : public Application {
Vector<String> args;
Vector<flag_t> flags;
String appName, appDesc;
- ShellApp(String name, String desc);
+ ShellApp(const String &name, const String &desc);
+ ~ShellApp();
virtual void init();
diff --git a/Source/Library/Userland/App/StreamApp.proto.cpp b/Source/Library/Userland/App/StreamApp.proto.cpp
new file mode 100644
index 0000000..b1dc7dd
--- /dev/null
+++ b/Source/Library/Userland/App/StreamApp.proto.cpp
@@ -0,0 +1,40 @@
+#include "StreamApp.proto.h"
+
+#include <FileStream.class.h>
+
+StreamApp::StreamApp(const String& name, const String& desc)
+ : ShellApp(name, desc) {
+ addFlag("o", "output", "Set the output to a file instead of the text output", FT_STR, "");
+ addFlag("e", "encoding", "Set the encoding for files (input and output)", FT_STR, "utf8");
+ addFlag("a", "append", "When writing to a file, append instead of truncating", FT_BOOL, "");
+}
+
+StreamApp::~StreamApp() {
+}
+
+void StreamApp::init() {
+ ShellApp::init();
+
+ u8int encoding = UE_UTF8;
+ if (sFlag("encoding") == "utf8") encoding = UE_UTF8;
+ if (sFlag("encoding") == "utf16be") encoding = UE_UTF16_BE;
+ if (sFlag("encoding") == "utf16le") encoding = UE_UTF16_LE;
+ if (sFlag("encoding") == "utf32be") encoding = UE_UTF32_BE;
+ if (sFlag("encoding") == "utf32le") encoding = UE_UTF32_LE;
+
+ if (sFlag("output") == "") {
+ out = &outvt;
+ } else {
+ out = new FileOStream(sFlag("output"), (bFlag("append") ? FM_APPEND : FM_TRUNCATE), encoding, FS::cwdNode());
+ }
+
+ if (args.size() == 0) {
+ in = &invt;
+ } else {
+ FileIStream *f = new FileIStream(encoding, FS::cwdNode());
+ for (u32int i = 0; i < args.size(); i++) {
+ f->appendFile(args[i]);
+ }
+ in = f;
+ }
+}
diff --git a/Source/Library/Userland/App/StreamApp.proto.h b/Source/Library/Userland/App/StreamApp.proto.h
new file mode 100644
index 0000000..462b1f3
--- /dev/null
+++ b/Source/Library/Userland/App/StreamApp.proto.h
@@ -0,0 +1,27 @@
+#ifndef DEF_STREAMAPP_PROTO_H
+#define DEF_STREAMAPP_PROTO_H
+
+#include "ShellApp.proto.h"
+
+#include <IStream.proto.h>
+#include <OStream.proto.h>
+
+/*
+ * This class implements basic utilities for apps that simply take some input, process it and output something.
+ * Examples : cat, grep, ...
+ */
+
+class StreamApp : public ShellApp {
+ protected:
+
+ IStream *in;
+ OStream *out;
+
+ public:
+ StreamApp(const String& name, const String& desc);
+ ~StreamApp();
+
+ virtual void init();
+};
+
+#endif
diff --git a/Source/Library/Userland/Binding/Process.class.h b/Source/Library/Userland/Binding/Process.class.h
index 2200f59..e94084b 100644
--- a/Source/Library/Userland/Binding/Process.class.h
+++ b/Source/Library/Userland/Binding/Process.class.h
@@ -54,10 +54,10 @@ class Process : public RessourceCaller {
void pushArg(const String& arg) {
doCall(PRIF_PUSHARG, (u32int)&arg);
}
- void setInVT(VirtualTerminal vt) {
+ void setInVT(const VirtualTerminal& vt) {
doCall(PRIF_SETINVT, vt.resId());
}
- void setOutVT(VirtualTerminal vt) {
+ void setOutVT(const VirtualTerminal& vt) {
doCall(PRIF_SETOUTVT, vt.resId());
}
bool authenticatePW(String user, String pw) {
diff --git a/Source/Library/Userland/Binding/VirtualTerminal.class.h b/Source/Library/Userland/Binding/VirtualTerminal.class.h
index 70c6b23..2a38abd 100644
--- a/Source/Library/Userland/Binding/VirtualTerminal.class.h
+++ b/Source/Library/Userland/Binding/VirtualTerminal.class.h
@@ -6,10 +6,14 @@
#include <VirtualTerminal.iface.h>
#include <Kbd.iface.h>
-#include <String.class.h>
+#include <OStream.proto.h>
+#include <IStream.proto.h>
#include <WChar.class.h>
-class VirtualTerminal : public RessourceCaller {
+class VirtualTerminal : public RessourceCaller, public OStream, public IStream {
+ private:
+ bool m_eof;
+
public:
static VirtualTerminal getIn() {
u32int id = RessourceCaller::sCall(VTIF_OBJTYPE, VTIF_SGETPRINVT);
@@ -19,22 +23,33 @@ class VirtualTerminal : public RessourceCaller {
u32int id = RessourceCaller::sCall(VTIF_OBJTYPE, VTIF_SGETPROUTVT);
return VirtualTerminal(id);
}
- VirtualTerminal(u32int id) : RessourceCaller(id, VTIF_OBJTYPE) {}
+ VirtualTerminal(u32int id) : RessourceCaller(id, VTIF_OBJTYPE) { m_eof = false; }
- void writeHex(u32int number) {
+ /*void writeHex(u32int number) {
doCall(VTIF_WRITEHEX, number);
}
void writeDec(s64int number) {
doCall(VTIF_WRITEDEC, (number >> 32), number);
- }
- void write(String s) {
+ }*/
+ void write(const String &s) {
doCall(VTIF_WRITE, (u32int)&s);
}
+ String read() {
+ if (m_eof) return "";
+ String ret = String::unserialize(doCall(VTIF_READLINE, 1));
+ if (ret[ret.size() - 1] == WChar(EOF)) {
+ ret = ret.substr(0, ret.size() - 1);
+ if (ret.empty()) return "";
+ m_eof = true;
+ }
+ return ret += "\n";
+ }
keypress_t getKeypress(bool show = true, bool block = true) {
keypress_t* ptr = (keypress_t*)doCall(VTIF_GETKEYPRESS, (show ? 1 : 0) | (block ? 2 : 0));
return *ptr;
}
String readLine(bool show = true) {
+ flush();
return String::unserialize(doCall(VTIF_READLINE, (show ? 1 : 0)));
}
void setColor(u8int fg, u8int bg = 0xFF) {
@@ -64,11 +79,6 @@ class VirtualTerminal : public RessourceCaller {
void put(u8int line, u8int col, WChar c) {
moveCursor(line, col); put(c);
}
-
- inline VirtualTerminal& operator<<(const String& s) { write(s); return *this; }
- inline VirtualTerminal& operator<<(s32int i) { writeDec(i); return *this; }
- inline VirtualTerminal& operator<<(s64int i) { writeDec(i); return *this; }
- inline VirtualTerminal& operator<<(u32int i) { writeHex(i); return *this; }
};
#endif
diff --git a/Source/Library/Userland/Syscall/RessourceCaller.class.h b/Source/Library/Userland/Syscall/RessourceCaller.class.h
index f26216d..39091ed 100644
--- a/Source/Library/Userland/Syscall/RessourceCaller.class.h
+++ b/Source/Library/Userland/Syscall/RessourceCaller.class.h
@@ -28,8 +28,8 @@ class RessourceCaller {
public:
static u32int sCall(u32int type, u8int wat, u32int a = 0, u32int b = 0, u32int c = 0, u32int d = 0);
- u32int resId() { return m_id; }
- u32int resType() { return m_type; }
+ u32int resId() const { return m_id; }
+ u32int resType() const { return m_type; }
bool valid() { return m_type != 0; }
};