diff --git a/Console/Executor.cc b/Console/Executor.cc index 6070184768a693af12e0048588e4ac6b75a26d44..7e1676551ef2c34f1861e48ab5fa0e871ed229ab 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<DirectoryNode&>( @@ -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<CommandNode &>( 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 658cc99b9987f6979d57510f1c26615c7720707b..189eece2c3702e6c7ac93b614fd86dff3db03681 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 79e5c1bcdf6e79cedbe2166121f094fca9e64f3b..d081ffd6476ce849a441ea64fd565d03879e4308 100644 --- a/Console/Node.hh +++ b/Console/Node.hh @@ -34,6 +34,7 @@ #include <boost/utility.hpp> #include <boost/range/iterator_range.hpp> #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 83f35b2aa6d15665ad2d12134aa386c2268c71b8..72fd25c36616f42e366a926b7515fd77f3a60ba6 100644 --- a/Console/Server.cc +++ b/Console/Server.cc @@ -143,7 +143,13 @@ prefix_ void senf::console::Client::clientData(ReadHelper<ClientHandle>::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<ClientHandle>::dispatch( handle_, 16384u, ReadUntil("\n"), diff --git a/Console/testServer.cc b/Console/testServer.cc index 9e5a3c9378f9a4c9730926ecb45246ab334b1f61..b55ca1b16ce3b5fcbfd969ffd3120ce973a592b9 100644 --- a/Console/testServer.cc +++ b/Console/testServer.cc @@ -29,6 +29,7 @@ // Custom includes #include <iostream> #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<senf::console::GenericNode>(new MyCommand("route"))); + senf::console::Server::start( senf::INet4SocketAddress("127.0.0.1:23232") ) .name("testServer");