4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief Executor non-inline non-template implementation */
26 #include "Executor.hh"
27 //#include "Executor.ih"
31 //#include "Executor.mpp"
33 ///////////////////////////////cc.p////////////////////////////////////////
37 struct TraverseTokens {
38 typedef std::string const & result_type;
39 result_type operator()(senf::console::ArgumentToken const & token) const {
46 ///////////////////////////////////////////////////////////////////////////
47 // senf::console::Executor
49 prefix_ bool senf::console::Executor::operator()(ParseCommandInfo const & command,
50 std::ostream & output)
52 SENF_LOG(( "Executing: " << command ));
54 ///\fixme Whenever checking cwd_.expired(), we also need to check, wether
55 /// the node is still connected to the root.
57 cwd_ = root().thisptr();
60 switch(command.builtin()) {
61 case ParseCommandInfo::NoBuiltin :
64 case ParseCommandInfo::BuiltinCD :
65 if ( command.arguments() ) {
66 if (command.arguments().begin()->size() == 1
67 && command.arguments().begin()->begin()->value() == "-") {
68 if (oldCwd_.expired()) {
70 cwd_ = root().thisptr();
76 cwd_ = traverseTo(command.arguments().begin()[0]).thisptr();
81 case ParseCommandInfo::BuiltinLS : {
82 DirectoryNode const & dir (
83 command.arguments().empty() ? cwd() : traverseTo(command.arguments().begin()[0]));
84 for (DirectoryNode::child_iterator i (dir.children().begin());
85 i != dir.children().end(); ++i) {
87 if (boost::dynamic_pointer_cast<DirectoryNode>(i->second))
94 case ParseCommandInfo::BuiltinPUSHD :
95 dirstack_.push_back(cwd_);
96 if ( command.arguments() )
97 cwd_ = traverseTo(command.arguments().begin()[0]).thisptr();
100 case ParseCommandInfo::BuiltinPOPD :
101 if (! dirstack_.empty()) {
102 cwd_ = dirstack_.back();
103 dirstack_.pop_back();
107 case ParseCommandInfo::BuiltinEXIT :
108 throw ExitException();
111 catch (InvalidDirectoryException &) {
112 output << "invalid directory" << std::endl;
117 prefix_ senf::console::DirectoryNode &
118 senf::console::Executor::traverseTo(ParseCommandInfo::argument_value_type const & path)
121 return dynamic_cast<DirectoryNode&>(
123 boost::make_iterator_range(
124 boost::make_transform_iterator(path.begin(), TraverseTokens()),
125 boost::make_transform_iterator(path.end(), TraverseTokens()))));
127 catch (std::bad_cast &) {
128 throw InvalidDirectoryException();
130 catch (UnknownNodeNameException &) {
131 throw InvalidDirectoryException();
135 ///////////////////////////////cc.e////////////////////////////////////////
137 //#include "Executor.mpp"
143 // comment-column: 40
144 // c-file-style: "senf"
145 // indent-tabs-mode: nil
146 // ispell-local-dictionary: "american"
147 // compile-command: "scons -u test"