summaryrefslogtreecommitdiff
path: root/Source/Kernel/Shell
diff options
context:
space:
mode:
authorAlexis211 <alexis211@gmail.com>2009-10-02 22:51:28 +0200
committerAlexis211 <alexis211@gmail.com>2009-10-02 22:51:28 +0200
commit92abedffec913fe7337117403c5e07185356c81b (patch)
tree1527937c1a1a7ab8168891f6465e198b88bbfea2 /Source/Kernel/Shell
parent021831ab981b9df22cd1ac5e5ac51f0f84ef49a7 (diff)
downloadMelon-92abedffec913fe7337117403c5e07185356c81b.tar.gz
Melon-92abedffec913fe7337117403c5e07185356c81b.zip
The kernel shell is now in an independent class, KernelShell::
Diffstat (limited to 'Source/Kernel/Shell')
-rw-r--r--Source/Kernel/Shell/KernelShell-fs.class.cpp104
-rw-r--r--Source/Kernel/Shell/KernelShell-sys.class.cpp60
-rw-r--r--Source/Kernel/Shell/KernelShell.class.cpp98
-rw-r--r--Source/Kernel/Shell/KernelShell.class.h43
4 files changed, 305 insertions, 0 deletions
diff --git a/Source/Kernel/Shell/KernelShell-fs.class.cpp b/Source/Kernel/Shell/KernelShell-fs.class.cpp
new file mode 100644
index 0000000..3ac7b20
--- /dev/null
+++ b/Source/Kernel/Shell/KernelShell-fs.class.cpp
@@ -0,0 +1,104 @@
+#include "KernelShell.class.h"
+#include <VFS/VFS.ns.h>
+#include <VFS/File.class.h>
+
+void KernelShell::ls(Vector<String>& args) {
+ DirectoryNode* d = m_cwd;
+ if (args.size() == 2) {
+ FSNode* n = VFS::find(args[1], m_cwd);
+ d = NULL;
+ if (n == NULL)
+ *m_vt << "No such directory : " << args[1] << "\n";
+ else if (n->type() != NT_DIRECTORY)
+ *m_vt << "Not a directory : " << args[1] << "\n";
+ else
+ d = (DirectoryNode*)n;
+ }
+ if (d != NULL) *m_vt << "Contents of directory " << VFS::path(d) << " :\n";
+ for (u32int i = 0; d != NULL && i < d->getLength(); i++) {
+ FSNode* n = d->getChild(i);
+ if (n->type() == NT_FILE) {
+ FileNode* f = (FileNode*)n;
+ *m_vt << " - FILE\t" << f->getName();
+ m_vt->setCursorCol(30);
+ *m_vt << (s32int)f->getLength() << " bytes.\n";
+ } else if (n->type() == NT_DIRECTORY) {
+ *m_vt << " - DIR\t" << n->getName() << "/";
+ m_vt->setCursorCol(30);
+ *m_vt << (s32int)n->getLength() << " items.\n";
+ }
+ }
+}
+
+void KernelShell::cd(Vector<String>& args) {
+ if (args.size() == 1) {
+ *m_vt << "Error : no argument given.\n";
+ } else {
+ FSNode* n = VFS::find(args[1], m_cwd);
+ if (n == NULL)
+ *m_vt << "No such directory : " << args[1] << "\n";
+ else if (n->type() != NT_DIRECTORY)
+ *m_vt << "Not a directory : " << args[1] << "\n";
+ else
+ m_cwd = (DirectoryNode*)n;
+ }
+}
+
+void KernelShell::pwd(Vector<String>& args) {
+ *m_vt << "Current location : " << VFS::path(m_cwd) << "\n";
+}
+
+void KernelShell::cat(Vector<String>& args) {
+ if (args.size() == 1) *m_vt << "Meow.\n";
+ for (u32int i = 1; i < args.size(); i++) {
+ File f(args[i], FM_READ, m_cwd);
+ if (f.valid()) {
+ u8int *buff = (u8int*)Mem::kalloc(f.length() + 1);
+ f.read(f.length(), buff);
+ buff[f.length()] = 0;
+ *m_vt << String((const char*) buff);
+ Mem::kfree(buff);
+ } else {
+ *m_vt << "Error reading from file " << args[i] << "\n";
+ }
+ }
+}
+
+void KernelShell::mkdir(Vector<String>& args) {
+ if (args.size() > 1) {
+ for (u32int i = 1; i < args.size(); i++) {
+ if (VFS::createDirectory(args[i], m_cwd) == NULL)
+ *m_vt << "Error while creating directory " << args[i] << "\n";
+ }
+ } else {
+ *m_vt << "No argument specified.\n";
+ }
+}
+
+void KernelShell::rm(Vector<String>& args) {
+ if (args.size() == 1) *m_vt << "Error : no argument specified.\n";
+ for (u32int i = 1; i < args.size(); i++) {
+ if (!VFS::remove(args[i], m_cwd)) {
+ *m_vt << "Error while removing file " << args[i] << "\n";
+ }
+ }
+}
+
+void KernelShell::wf(Vector<String>& args) {
+ if (args.size() == 1) {
+ *m_vt << "No file to write !\n";
+ } else {
+ File f(args[1], FM_TRUNCATE, m_cwd);
+ if (f.valid()) {
+ String t = m_vt->readLine();
+ while (t != ".") {
+ t += "\n";
+ ByteArray temp(t);
+ f.write(temp);
+ t = m_vt->readLine();
+ }
+ } else {
+ *m_vt << "Error openning file.\n";
+ }
+ }
+}
diff --git a/Source/Kernel/Shell/KernelShell-sys.class.cpp b/Source/Kernel/Shell/KernelShell-sys.class.cpp
new file mode 100644
index 0000000..17ff19c
--- /dev/null
+++ b/Source/Kernel/Shell/KernelShell-sys.class.cpp
@@ -0,0 +1,60 @@
+#include "KernelShell.class.h"
+#include <DeviceManager/Dev.ns.h>
+#include <DeviceManager/Time.ns.h>
+#include <MemoryManager/Mem.ns.h>
+#include <MemoryManager/PhysMem.ns.h>
+#include <VFS/Part.ns.h>
+
+void KernelShell::devices(Vector<String>& args) {
+ Vector<Device*> dev = Dev::findDevices();
+ *m_vt << " - Detected devices :\n";
+ for (u32int i = 0; i < dev.size(); i++) {
+ *m_vt << " - " << dev[i]->getClass();
+ m_vt->setCursorCol(25);
+ *m_vt << dev[i]->getName() << "\n";
+ }
+}
+
+void KernelShell::loadkeys(Vector<String>& args) {
+ if (args.size() == 1) {
+ *m_vt << "Error : no argument specified.\n";
+ } else {
+ if (!Kbd::loadKeymap(args[1])) {
+ *m_vt << "Error while loading keymap " << args[1] << ".\n";
+ }
+ }
+}
+
+void KernelShell::free(Vector<String>& args) {
+ u32int frames = PhysMem::total(), freef = PhysMem::free();
+ *m_vt << " - Free frames : " << (s32int)freef << " (" << (s32int)(freef * 4 / 1024) << "Mo) of "
+ << (s32int)frames << " (" << (s32int)(frames * 4 / 1024) << "Mo).\n";
+ u32int kh = Mem::kheapSize(), freek = Mem::kheapFree;
+ *m_vt << " - Kernel heap free : " << (s32int)(freek / 1024 / 1024) << "Mo (" << (s32int)(freek / 1024) <<
+ "Ko) of " << (s32int)(kh / 1024 / 1024) << "Mo (" << (s32int)(kh / 1024) << "Ko).\n";
+}
+
+void KernelShell::uptime(Vector<String>& args) {
+ *m_vt << " - Uptime : " << (s32int)(Time::uptime()) << "s.\n";
+}
+
+void KernelShell::part(Vector<String>& args) {
+ *m_vt << " * ID\tClass Name\n";
+ for (u32int i = 0; i < Part::devices.size(); i++) {
+ *m_vt << " - " << (s32int)i << "\t";
+ if (Part::devices[i] == 0) {
+ *m_vt << "[none]\n";
+ } else {
+ *m_vt << Part::devices[i]->getClass();
+ m_vt->setCursorCol(33);
+ *m_vt << Part::devices[i]->getName() << "\n";
+ for (u32int j = 0; j < Part::partitions.size(); j++) {
+ if (Part::partitions[j]->getDevice() == Part::devices[i]) {
+ *m_vt << "\t - Partition " << (s32int)Part::partitions[j]->getPartNumber() <<
+ ", start at " << (s32int)Part::partitions[j]->getStartBlock() <<
+ ", size " << (s32int)Part::partitions[j]->getBlockCount() << "\n";
+ }
+ }
+ }
+ }
+}
diff --git a/Source/Kernel/Shell/KernelShell.class.cpp b/Source/Kernel/Shell/KernelShell.class.cpp
new file mode 100644
index 0000000..5d78953
--- /dev/null
+++ b/Source/Kernel/Shell/KernelShell.class.cpp
@@ -0,0 +1,98 @@
+#include "KernelShell.class.h"
+#include <VTManager/ScrollableVT.class.h>
+#include <DeviceManager/Kbd.ns.h>
+
+u32int KernelShell::m_instances = 0;
+
+#define COMMAND(name, wat) {(void*)name, (void*)(&KernelShell::wat)},
+
+u32int shellRun(void* ks) {
+ KernelShell* sh = (KernelShell*)ks;
+ u32int ret = sh->run();
+ delete sh;
+ return ret;
+}
+
+KernelShell::KernelShell(DirectoryNode* cwd) {
+ m_vt = new ScrollableVT(10, 76, 200, SHELL_FGCOLOR, SHELL_BGCOLOR);
+ ((ScrollableVT*)m_vt)->map(9);
+ Kbd::setFocus(m_vt);
+ m_cwd = cwd;
+ *m_vt << "Welcome to Melon's kernel shell !\n";
+ m_thread = new Thread(shellRun, (void*)this, true);
+ m_instances++;
+}
+
+KernelShell::~KernelShell() {
+ delete m_vt;
+ m_instances--;
+}
+
+u32int KernelShell::run() {
+
+ struct {
+ const char* name;
+ void (KernelShell::*cmd)(Vector<String>&);
+ } commands[]= {
+ {"ls", &KernelShell::ls},
+ {"cd", &KernelShell::cd},
+ {"pwd", &KernelShell::pwd},
+ {"cat", &KernelShell::cat},
+ {"mkdir", &KernelShell::mkdir},
+ {"rm", &KernelShell::rm},
+ {"wf", &KernelShell::wf},
+
+ {"devices", &KernelShell::devices},
+ {"loadkeys", &KernelShell::loadkeys},
+ {"free", &KernelShell::free},
+ {"uptime", &KernelShell::uptime},
+ {"part", &KernelShell::part},
+
+ {0, 0}
+ };
+
+ while (1) {
+ m_vt->setColor(SHELL_LIGHTCOLOR);
+ *m_vt << "[" << m_cwd->getName() << "]# ";
+ m_vt->setColor(SHELL_ENTRYCOLOR);
+ Vector<String> tokens = m_vt->readLine().split(" ");
+ m_vt->setColor(SHELL_FGCOLOR);
+ if (tokens[0] == "help") {
+ *m_vt << " - Command list for integrated kernel shell:\n";
+ *m_vt << " - help shows this help screen\n";
+ *m_vt << " - reboot reboots your computer\n";
+ *m_vt << " - halt shuts down your computer\n";
+ *m_vt << " - panic causes a kernel panic\n";
+ *m_vt << " - devices shows all detected devices on your computer\n";
+ *m_vt << " - loadkeys loads specified kekymap\n";
+ *m_vt << " - free shows memory usage (frames and kheap)\n";
+ *m_vt << " - uptime shows seconds since boot\n";
+ *m_vt << " - part shows all detected block devs and partitions\n";
+ *m_vt << " - Standard UNIX commands : ls cd cat pwd rm mkdir wf\n";
+ *m_vt << " - Scroll up with shift+pgup !\n";
+ } else if (tokens[0] == "reboot") {
+ Sys::reboot();
+ } else if (tokens[0] == "halt") {
+ Sys::halt();
+ } else if (tokens[0] == "panic") {
+ PANIC("This is what happens when you say 'panic'.");
+ } else if (tokens[0] != "" or tokens.size() != 1) {
+ u32int i = 0;
+ bool found = false;
+ while (commands[i].name != 0) {
+ if (tokens[0] == (const char*)commands[i].name) {
+ found = true;
+ if (commands[i].name != 0) {
+ (this->*(commands[i].cmd))(tokens); //Call command
+ } else {
+ *m_vt << "This command isn't enabled... yet !\n";
+ }
+ break;
+ }
+ i++;
+ }
+ if (!found) *m_vt << "Unknown command : " << tokens[0] << "\n";
+ }
+ }
+ return 1337;
+}
diff --git a/Source/Kernel/Shell/KernelShell.class.h b/Source/Kernel/Shell/KernelShell.class.h
new file mode 100644
index 0000000..182aa66
--- /dev/null
+++ b/Source/Kernel/Shell/KernelShell.class.h
@@ -0,0 +1,43 @@
+#ifndef DEF_KERNELSHELL_CLASS_H
+#define DEF_KERNELSHELL_CLASS_H
+
+#include <VTManager/VirtualTerminal.proto.h>
+#include <VFS/DirectoryNode.class.h>
+#include <TaskManager/Thread.class.h>
+#include <Library/Vector.class.h>
+
+class KernelShell {
+ friend u32int shellRun(void* ks);
+
+ private:
+ VirtualTerminal *m_vt;
+ DirectoryNode *m_cwd;
+ Thread* m_thread;
+ static u32int m_instances;
+
+ ~KernelShell();
+ u32int run();
+
+ //in KernelShell-fs
+ void ls(Vector<String>& args);
+ void cd(Vector<String>& args);
+ void pwd(Vector<String>& args);
+ void cat(Vector<String>& args);
+ void mkdir(Vector<String>& args);
+ void rm(Vector<String>& args);
+ void wf(Vector<String>& args);
+
+ //in KernelShell-sys
+ void devices(Vector<String>& args);
+ void loadkeys(Vector<String>& args);
+ void free(Vector<String>& args);
+ void uptime(Vector<String>& args);
+ void part(Vector<String>& args);
+
+ public:
+ KernelShell(DirectoryNode* cwd);
+
+ static u32int getInstances() { return m_instances; }
+};
+
+#endif