From 48de0cd029b52f64f76345b6e1fdf3cde5c58de3 Mon Sep 17 00:00:00 2001 From: Alexis211 Date: Fri, 23 Oct 2009 19:40:08 +0200 Subject: More work on syscalls and shell --- Source/Applications/Shell/Makefile | 3 +- Source/Applications/Shell/Shell-fs.ns.cpp | 52 +++++++++++++++++++++++++++++++ Source/Applications/Shell/Shell.ns.cpp | 32 +++++++++++++++++-- Source/Applications/Shell/Shell.ns.h | 3 ++ 4 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 Source/Applications/Shell/Shell-fs.ns.cpp (limited to 'Source/Applications') diff --git a/Source/Applications/Shell/Makefile b/Source/Applications/Shell/Makefile index f93d80d..0eeb620 100644 --- a/Source/Applications/Shell/Makefile +++ b/Source/Applications/Shell/Makefile @@ -7,7 +7,8 @@ LD = ld LDFLAGS = -T ../../Library/Link.ld Objects = main.o \ - Shell.ns.o + Shell.ns.o \ + Shell-fs.ns.o OutFile = Shell all: $(OutFile) diff --git a/Source/Applications/Shell/Shell-fs.ns.cpp b/Source/Applications/Shell/Shell-fs.ns.cpp new file mode 100644 index 0000000..33bc94e --- /dev/null +++ b/Source/Applications/Shell/Shell-fs.ns.cpp @@ -0,0 +1,52 @@ +#include "Shell.ns.h" + +namespace Shell { + +void ls(Vector& args) { + FSNode d = cwd; + if (args.size() == 2) { + FSNode n = cwd.findFrom(args[1]); + d = FSNode(0); + if (!n.valid()) + outvt << "No such directory : " << args[1] << "\n"; + else if (n.type() != NT_DIRECTORY) + outvt << "Not a directory : " << args[1] << "\n"; + else + d = n; + } + if (d.valid()) outvt << "Contents of directory " << d.path() << " :\n"; + for (u32int i = 0; i < d.getLength(); i++) { + FSNode n = d.getChild(i); + if (n.type() == NT_FILE) { + outvt << " - FILE\t" << n.getName(); + outvt.setCsrCol(30); + outvt << (s32int)n.getLength() << " bytes.\n"; + } else if (n.type() == NT_DIRECTORY) { + outvt << " - DIR\t" << n.getName() << "/"; + outvt.setCsrCol(30); + outvt << (s32int)n.getLength() << " items.\n"; + } + } +} + +void cd(Vector& args) { + if (args.size() != 2) { + outvt << "Invalid argument count.\n"; + } else { + FSNode ncwd = cwd.findFrom(args[1]); + if (!ncwd.valid()) { + outvt << "No such directory : " << args[1] << "\n"; + } else if (ncwd.type() == NT_DIRECTORY) { + ncwd.setCwd(); + cwd = ncwd; + } else { + outvt << "Not a directory.\n"; + } + } +} + +void pwd(Vector& args) { + outvt << "Current directory : " << cwd.path() << "\n"; +} + +} diff --git a/Source/Applications/Shell/Shell.ns.cpp b/Source/Applications/Shell/Shell.ns.cpp index d34f516..4afc6b7 100644 --- a/Source/Applications/Shell/Shell.ns.cpp +++ b/Source/Applications/Shell/Shell.ns.cpp @@ -3,12 +3,23 @@ namespace Shell { -FSNode node(0); +FSNode cwd(0); + u32int run() { - node = FSNode::getRoot(); + struct { //Command list + String name; + void (*cmd)(Vector&); + } commands[] = { + {"ls", ls}, + {"cd", cd}, + {"pwd", pwd}, + {"", 0} + }; + + cwd = FSNode::getCwd(); while (1) { - outvt << node.getName() << " : "; + outvt << "{" << cwd.getName() << "}: "; String s = invt.readLine(); while (s[0] == WChar(" ") or s[0] == WChar("\t")) { s = s.substr(1, s.size() - 1); @@ -44,6 +55,21 @@ u32int run() { Sys::reboot(); outvt << "Something went wrong.\n"; } else { + u32int i = 0; + bool found = false; + while (!commands[i].name.empty()) { + if (commands[i].name == cmd[0]) { + found = true; + if (commands[i].cmd == 0) { + outvt << "Not implemented yet.\n"; + } else { + commands[i].cmd(cmd); + } + break; + } + i++; + } + if (!found) outvt << "Unknown command : " << cmd[0] << "\n"; } } } diff --git a/Source/Applications/Shell/Shell.ns.h b/Source/Applications/Shell/Shell.ns.h index 3be1e0d..8d7067a 100644 --- a/Source/Applications/Shell/Shell.ns.h +++ b/Source/Applications/Shell/Shell.ns.h @@ -7,4 +7,7 @@ namespace Shell { extern FSNode cwd; + extern void ls(Vector& args); + extern void cd(Vector& args); + extern void pwd(Vector& args); } -- cgit v1.2.3