From: g0dil Date: Mon, 24 Mar 2008 10:10:00 +0000 (+0000) Subject: Console: Implement command execution X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=9ff18548815d10140e83fba6ed2d573fe556c346;p=senf.git Console: Implement command execution git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@760 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/Console/Executor.cc b/Console/Executor.cc index 6070184..7e16765 100644 --- a/Console/Executor.cc +++ b/Console/Executor.cc @@ -59,8 +59,9 @@ prefix_ bool senf::console::Executor::operator()(ParseCommandInfo const & comman try { switch(command.builtin()) { case ParseCommandInfo::NoBuiltin : + traverseToCommand(command.commandPath())(output, command.arguments()); break; - + case ParseCommandInfo::BuiltinCD : if ( command.arguments() ) { if (command.arguments().begin()->size() == 1 @@ -111,11 +112,14 @@ prefix_ bool senf::console::Executor::operator()(ParseCommandInfo const & comman catch (InvalidDirectoryException &) { output << "invalid directory" << std::endl; } + catch (InvalidCommandException &) { + output << "invalid command" << std::endl; + } return true; } prefix_ senf::console::DirectoryNode & -senf::console::Executor::traverseTo(ParseCommandInfo::argument_value_type const & path) +senf::console::Executor::traverseTo (ParseCommandInfo::argument_value_type const & path) { try { return dynamic_cast( @@ -131,6 +135,20 @@ senf::console::Executor::traverseTo(ParseCommandInfo::argument_value_type const throw InvalidDirectoryException(); } } + +prefix_ senf::console::CommandNode & +senf::console::Executor::traverseToCommand(ParseCommandInfo::CommandPathRange const & path) +{ + try { + return dynamic_cast( cwd().traverse(path) ); + } + catch (std::bad_cast &) { + throw InvalidCommandException(); + } + catch (UnknownNodeNameException &) { + throw InvalidCommandException(); + } +} ///////////////////////////////cc.e//////////////////////////////////////// #undef prefix_ diff --git a/Console/Executor.hh b/Console/Executor.hh index 658cc99..189eece 100644 --- a/Console/Executor.hh +++ b/Console/Executor.hh @@ -69,8 +69,10 @@ namespace console { private: DirectoryNode & traverseTo(ParseCommandInfo::argument_value_type const & path); + CommandNode & traverseToCommand(ParseCommandInfo::CommandPathRange const & path); struct InvalidDirectoryException {}; + struct InvalidCommandException {}; DirectoryNode::weak_ptr cwd_; DirectoryNode::weak_ptr oldCwd_; diff --git a/Console/Node.hh b/Console/Node.hh index 79e5c1b..d081ffd 100644 --- a/Console/Node.hh +++ b/Console/Node.hh @@ -34,6 +34,7 @@ #include #include #include "../Utils/Exception.hh" +#include "Parse.hh" //#include "Node.mpp" ///////////////////////////////hh.p//////////////////////////////////////// @@ -151,6 +152,9 @@ namespace console { /////////////////////////////////////////////////////////////////////////// + virtual void operator()(std::ostream & output, + ParseCommandInfo::ArgumentsRange const & arguments) = 0; + ptr thisptr(); cptr thisptr() const; diff --git a/Console/Server.cc b/Console/Server.cc index 83f35b2..72fd25c 100644 --- a/Console/Server.cc +++ b/Console/Server.cc @@ -143,7 +143,13 @@ prefix_ void senf::console::Client::clientData(ReadHelper::ptr hel // THIS COMMITS SUICIDE. THE INSTANCE IS GONE AFTER stopClient RETURNS stopClient(); return; - } + } + catch (std::exception & ex) { + out_ << ex.what() << std::endl; + } + catch (...) { + out_ << "unidentified error (unknown exception thrown)" << std::endl; + } showPrompt(); ReadHelper::dispatch( handle_, 16384u, ReadUntil("\n"), diff --git a/Console/testServer.cc b/Console/testServer.cc index 9e5a3c9..b55ca1b 100644 --- a/Console/testServer.cc +++ b/Console/testServer.cc @@ -29,6 +29,7 @@ // Custom includes #include #include "Server.hh" +#include "Node.hh" #include "../Scheduler/Scheduler.hh" #include "../Utils/Logger/SenfLog.hh" @@ -36,6 +37,25 @@ #define prefix_ ///////////////////////////////cc.p//////////////////////////////////////// +namespace { + struct MyCommand : public senf::console::CommandNode + { + MyCommand(std::string name) : senf::console::CommandNode(name) {} + void operator()(std::ostream & output, + senf::console::ParseCommandInfo::ArgumentsRange const & arguments) { + senf::console::ParseCommandInfo::argument_iterator i (arguments.begin()); + senf::console::ParseCommandInfo::argument_iterator i_end (arguments.end()); + for (; i != i_end; ++i) { + senf::console::ParseCommandInfo::token_iterator j (i->begin()); + senf::console::ParseCommandInfo::token_iterator j_end (i->end()); + for (; j != j_end; ++j) + output << j->value() << ' '; + } + output << "\n"; + } + }; +} + int main(int, char const **) { senf::log::ConsoleTarget::instance().route< senf::SenfLog, senf::log::NOTICE >(); @@ -43,6 +63,9 @@ int main(int, char const **) senf::console::root().mkdir("network").mkdir("eth0"); senf::console::root().mkdir("server"); + senf::console::root()["network"].add( + std::auto_ptr(new MyCommand("route"))); + senf::console::Server::start( senf::INet4SocketAddress("127.0.0.1:23232") ) .name("testServer");