diff options
author | Alexis211 <alexis211@gmail.com> | 2009-12-24 20:51:21 +0100 |
---|---|---|
committer | Alexis211 <alexis211@gmail.com> | 2009-12-24 20:51:21 +0100 |
commit | 714902e17c91200f53d0ad9a356466ee60b59d98 (patch) | |
tree | f28672f3384478b852d3319537f3860db04cc43e /Source/Applications/Shell/Applets | |
parent | 9455d1d0e91823f45906ae3f8b99cf9609991602 (diff) | |
download | Melon-714902e17c91200f53d0ad9a356466ee60b59d98.tar.gz Melon-714902e17c91200f53d0ad9a356466ee60b59d98.zip |
Changed some stuff about the Shell
Some applets are now externalized : cat free halt ls reboot uptime
Also added some colors
Diffstat (limited to 'Source/Applications/Shell/Applets')
-rw-r--r-- | Source/Applications/Shell/Applets/cat.cpp | 34 | ||||
-rw-r--r-- | Source/Applications/Shell/Applets/free.cpp | 12 | ||||
-rw-r--r-- | Source/Applications/Shell/Applets/halt.cpp | 13 | ||||
-rw-r--r-- | Source/Applications/Shell/Applets/ls.cpp | 95 | ||||
-rw-r--r-- | Source/Applications/Shell/Applets/reboot.cpp | 13 | ||||
-rw-r--r-- | Source/Applications/Shell/Applets/rot13.cpp | 30 | ||||
-rw-r--r-- | Source/Applications/Shell/Applets/uptime.cpp | 26 |
7 files changed, 193 insertions, 30 deletions
diff --git a/Source/Applications/Shell/Applets/cat.cpp b/Source/Applications/Shell/Applets/cat.cpp new file mode 100644 index 0000000..169226e --- /dev/null +++ b/Source/Applications/Shell/Applets/cat.cpp @@ -0,0 +1,34 @@ +#include <App/StreamApp.proto.h> + +class rot13 : public StreamApp { + public: + rot13() : StreamApp("cat", "Concatenate some input files") { + addFlag("r", "rot13", "Apply a ROT13 encryption to the output", FT_BOOL, ""); + } + int run(); +}; + +APP(rot13); + +int rot13::run() { + while (!in->eof()) { + String s = in->get(); + if (in->eof() && s.empty()) break; + if (bFlag("rot13")) { + for (u32int i = 0; i < s.size(); i++) { + WChar &c = s[i]; + if (c >= WChar('A') and c <= WChar('Z')) { + c += 13; + if (c > WChar('Z')) c -= 26; + } + if (c >= WChar('a') and c <= WChar('z')) { + c += 13; + if (c > WChar('z')) c -= 26; + } + } + } + *out << s << ENDL; + } + return 0; +} + diff --git a/Source/Applications/Shell/Applets/free.cpp b/Source/Applications/Shell/Applets/free.cpp new file mode 100644 index 0000000..3217574 --- /dev/null +++ b/Source/Applications/Shell/Applets/free.cpp @@ -0,0 +1,12 @@ +#include <App/ShellApp.proto.h> +#include <Binding/Sys.ns.h> + +class free : public ShellApp { + public: + free() : ShellApp("free", "Get the amount of system free RAM") {} + int run() { + outvt << "Free RAM: " << (s64int)Sys::freeRam() << " Kio of " << (s64int)Sys::totalRam() << " Kio\n"; + } +}; + +APP(free); diff --git a/Source/Applications/Shell/Applets/halt.cpp b/Source/Applications/Shell/Applets/halt.cpp new file mode 100644 index 0000000..d7dedef --- /dev/null +++ b/Source/Applications/Shell/Applets/halt.cpp @@ -0,0 +1,13 @@ +#include <App/ShellApp.proto.h> +#include <Binding/Sys.ns.h> + +class halt : public ShellApp { + public: + halt() : ShellApp("halt", "Halt the system") {} + int run() { + outvt << "Halting system...\n"; + Sys::halt(); + } +}; + +APP(halt); diff --git a/Source/Applications/Shell/Applets/ls.cpp b/Source/Applications/Shell/Applets/ls.cpp new file mode 100644 index 0000000..3d05de4 --- /dev/null +++ b/Source/Applications/Shell/Applets/ls.cpp @@ -0,0 +1,95 @@ +#include <App/ShellApp.proto.h> +#include <Binding/FSNode.class.h> + +#define DIR_COLOR 1 +#define FILE_COLOR 2 +#define NORMAL_COLOR 7 + +class ls : public ShellApp { + public: + ls() : ShellApp("ls", "List the content of a directory") { + addFlag("l", "long", "Use long output", FT_BOOL, ""); + } + int run(); + void doLs(FSNode n); +}; + +int ls::run() { + FSNode cwd = FS::cwdNode(); + if (args.size() == 0) { + doLs(cwd); + } else { + for (u32int i = 0; i < args.size(); i++) { + FSNode n = FS::find(args[i], cwd); + if (!n.valid()) { + outvt << "No such directory : " << args[1] << ENDL; + } else if (n.type() != NT_DIRECTORY) { + outvt << "Not a directory : " << args[1] << ENDL; + } else { + if (args.size() > 1) outvt << n.path() << ":\n"; + doLs(n); + if (args.size() > 1 and i != args.size() - 1) outvt << ENDL; + } + } + } + return 0; +} + +void ls::doLs(FSNode n) { + if (!n.valid()) { + outvt << "Invalid node...\n"; + return; + } + Vector<String> elements; int maxLength = 0; + for (u32int i = 0; i < n.getLength(); i++) { + FSNode c = n.getChild(i); + if (!n.valid()) { + outvt << "[inacessible file]\n"; + continue; + } + if (bFlag("long")) { + String perm = "rwxrwxrwx"; + u32int v = c.getPerm(); + for (u32int i = 0; i < 9; i++) { + if (((v >> i) & 1) == 0) perm[8 - i] = "-"; + } + if (c.type() == NT_FILE) { + outvt << " FILE " << perm << " " << c.getName(); + outvt << MVT::setcsrcol(35) << (s32int)c.getLength() << " bytes.\n"; + } else if (c.type() == NT_DIRECTORY) { + outvt << " DIR " << perm << " " << c.getName() << "/"; + outvt << MVT::setcsrcol(35) << (s32int)c.getLength() << " items.\n"; + } + } else { + String s = c.getName(); + if (c.type() == NT_DIRECTORY) s += "/"; + if (s.size() > maxLength) maxLength = s.size(); + elements.push(s); + } + } + if (!bFlag("long")) { + int nent; + bool boxed = outvt.isBoxed(); + if (!boxed) { + nent = 1; + } else { + nent = outvt.width() / (maxLength + 2); + } + for (int i = 0, e = 0; i < elements.size(); i++, e++) { + if (e >= nent) { + e = 0; + outvt << ENDL; + } + if (boxed) outvt << MVT::setcsrcol(e * (maxLength + 2)); + if (elements[i][elements[i].size() - 1] == WChar("/")) { + outvt << MVT::setfgcolor(DIR_COLOR); + } else { + outvt << MVT::setfgcolor(FILE_COLOR); + } + outvt << elements[i] << MVT::setfgcolor(NORMAL_COLOR); + } + outvt << ENDL; + } +} + +APP(ls); diff --git a/Source/Applications/Shell/Applets/reboot.cpp b/Source/Applications/Shell/Applets/reboot.cpp new file mode 100644 index 0000000..de97fe6 --- /dev/null +++ b/Source/Applications/Shell/Applets/reboot.cpp @@ -0,0 +1,13 @@ +#include <App/ShellApp.proto.h> +#include <Binding/Sys.ns.h> + +class reboot : public ShellApp { + public: + reboot() : ShellApp("reboot", "Reboot the system") {} + int run() { + outvt << "Reboot system...\n"; + Sys::reboot(); + } +}; + +APP(reboot); diff --git a/Source/Applications/Shell/Applets/rot13.cpp b/Source/Applications/Shell/Applets/rot13.cpp deleted file mode 100644 index e5638b8..0000000 --- a/Source/Applications/Shell/Applets/rot13.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include <App/StreamApp.proto.h> - -class rot13 : public StreamApp { - public: - rot13() : StreamApp("rot13", "Cat a file, but ROT13 it") {} - int run(); -}; - -APP(rot13); - -int rot13::run() { - while (!in->eof()) { - String s = in->get(); - if (in->eof() && s.empty()) break; - for (u32int i = 0; i < s.size(); i++) { - WChar &c = s[i]; - if (c >= WChar('A') and c <= WChar('Z')) { - c += 13; - if (c > WChar('Z')) c -= 26; - } - if (c >= WChar('a') and c <= WChar('z')) { - c += 13; - if (c > WChar('z')) c -= 26; - } - } - *out << s << ENDL; - } - return 0; -} - diff --git a/Source/Applications/Shell/Applets/uptime.cpp b/Source/Applications/Shell/Applets/uptime.cpp new file mode 100644 index 0000000..2264fdc --- /dev/null +++ b/Source/Applications/Shell/Applets/uptime.cpp @@ -0,0 +1,26 @@ +#include <App/ShellApp.proto.h> +#include <Binding/Sys.ns.h> + +class uptime : public ShellApp { + public: + uptime() : ShellApp("uptime", "Get the system's uptime") { + addFlag("u", "unformatted", "Show raw number of seconds", FT_BOOL, ""); + } + int run() { + if (bFlag("unformatted")) { + outvt << (s64int)Sys::uptime() << ENDL; + } else { + u64int secs = Sys::uptime(); + u64int mins = secs / 60; secs = secs % 60; + u64int hours = mins / 60; mins = mins % 60; + u64int days = hours / 24; hours = hours % 24; + outvt << "Uptime: "; + if (days > 0) outvt << (s64int)days << "d, "; + if (hours > 0) outvt << (s64int)hours << "h, "; + if (mins > 0) outvt << (s64int)mins << "m, "; + outvt << (s64int)secs << "s.\n"; + } + } +}; + +APP(uptime); |