X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Console%2FExecutor.cc;h=5e4b6f21ee4b2f073a2fdffc9274cdacdcc05fef;hb=bf1d8ba5ce6fc6a169a938183f8d01c8bdbccf32;hp=7e1676551ef2c34f1861e48ab5fa0e871ed229ab;hpb=9ff18548815d10140e83fba6ed2d573fe556c346;p=senf.git diff --git a/Console/Executor.cc b/Console/Executor.cc index 7e16765..5e4b6f2 100644 --- a/Console/Executor.cc +++ b/Console/Executor.cc @@ -46,27 +46,25 @@ namespace { /////////////////////////////////////////////////////////////////////////// // senf::console::Executor -prefix_ bool senf::console::Executor::operator()(ParseCommandInfo const & command, - std::ostream & output) +prefix_ void senf::console::Executor::execute(std::ostream & output, + ParseCommandInfo const & command) { SENF_LOG(( "Executing: " << command )); - ///\fixme Whenever checking cwd_.expired(), we also need to check, wether - /// the node is still connected to the root. - if (cwd_.expired()) + if (cwd_.expired() || ! cwd().active()) cwd_ = root().thisptr(); try { switch(command.builtin()) { case ParseCommandInfo::NoBuiltin : - traverseToCommand(command.commandPath())(output, command.arguments()); + traverseCommand(command.commandPath())(output, command); break; case ParseCommandInfo::BuiltinCD : if ( command.arguments() ) { if (command.arguments().begin()->size() == 1 && command.arguments().begin()->begin()->value() == "-") { - if (oldCwd_.expired()) { + if (oldCwd_.expired() || ! oldCwd_.lock()->active()) { oldCwd_ = cwd_; cwd_ = root().thisptr(); } else @@ -74,14 +72,15 @@ prefix_ bool senf::console::Executor::operator()(ParseCommandInfo const & comman } else { oldCwd_ = cwd_; - cwd_ = traverseTo(command.arguments().begin()[0]).thisptr(); + cwd_ = traverseDirectory(command.arguments().begin()[0]).thisptr(); } } break; case ParseCommandInfo::BuiltinLS : { - DirectoryNode const & dir ( - command.arguments().empty() ? cwd() : traverseTo(command.arguments().begin()[0])); + DirectoryNode const & dir ( command.arguments() + ? traverseDirectory(command.arguments().begin()[0]) + : cwd() ); for (DirectoryNode::child_iterator i (dir.children().begin()); i != dir.children().end(); ++i) { output << i->first; @@ -95,7 +94,7 @@ prefix_ bool senf::console::Executor::operator()(ParseCommandInfo const & comman case ParseCommandInfo::BuiltinPUSHD : dirstack_.push_back(cwd_); if ( command.arguments() ) - cwd_ = traverseTo(command.arguments().begin()[0]).thisptr(); + cwd_ = traverseDirectory(command.arguments().begin()[0]).thisptr(); break; case ParseCommandInfo::BuiltinPOPD : @@ -107,37 +106,62 @@ prefix_ bool senf::console::Executor::operator()(ParseCommandInfo const & comman case ParseCommandInfo::BuiltinEXIT : throw ExitException(); + + case ParseCommandInfo::BuiltinHELP : + GenericNode const & node (command.arguments() + ? traverseNode(command.arguments().begin()[0]) + : cwd()); + output << prettyName(typeid(node)) << " at " << node.path() << "\n\n"; + node.help(output); + output << std::flush; + break; + } } + catch (InvalidPathException &) { + output << "invalid path" << std::endl; + } catch (InvalidDirectoryException &) { output << "invalid directory" << std::endl; } catch (InvalidCommandException &) { output << "invalid command" << std::endl; } - return true; +} + +prefix_ senf::console::GenericNode & +senf::console::Executor::traverseNode(ParseCommandInfo::argument_value_type const & path) +{ + try { + return cwd().traverse( + boost::make_iterator_range( + boost::make_transform_iterator(path.begin(), TraverseTokens()), + boost::make_transform_iterator(path.end(), TraverseTokens()))); + } + catch (std::bad_cast &) { + throw InvalidPathException(); + } + catch (UnknownNodeNameException &) { + throw InvalidPathException(); + } } prefix_ senf::console::DirectoryNode & -senf::console::Executor::traverseTo (ParseCommandInfo::argument_value_type const & path) +senf::console::Executor::traverseDirectory(ParseCommandInfo::argument_value_type const & path) { try { - return dynamic_cast( - cwd().traverse( - boost::make_iterator_range( - boost::make_transform_iterator(path.begin(), TraverseTokens()), - boost::make_transform_iterator(path.end(), TraverseTokens())))); + return dynamic_cast( traverseNode(path) ); } catch (std::bad_cast &) { throw InvalidDirectoryException(); } - catch (UnknownNodeNameException &) { + catch (InvalidPathException &) { throw InvalidDirectoryException(); } } prefix_ senf::console::CommandNode & -senf::console::Executor::traverseToCommand(ParseCommandInfo::CommandPathRange const & path) +senf::console::Executor::traverseCommand(ParseCommandInfo::CommandPathRange const & path) { try { return dynamic_cast( cwd().traverse(path) );