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 if (cwd_.expired() || ! cwd().active())
55 cwd_ = root().thisptr();
58 switch(command.builtin()) {
59 case ParseCommandInfo::NoBuiltin :
60 traverseToCommand(command.commandPath())(output, command.arguments());
63 case ParseCommandInfo::BuiltinCD :
64 if ( command.arguments() ) {
65 if (command.arguments().begin()->size() == 1
66 && command.arguments().begin()->begin()->value() == "-") {
67 if (oldCwd_.expired() || ! oldCwd_.lock()->active()) {
69 cwd_ = root().thisptr();
75 cwd_ = traverseTo(command.arguments().begin()[0]).thisptr();
80 case ParseCommandInfo::BuiltinLS : {
81 DirectoryNode const & dir (
82 command.arguments().empty() ? cwd() : traverseTo(command.arguments().begin()[0]));
83 for (DirectoryNode::child_iterator i (dir.children().begin());
84 i != dir.children().end(); ++i) {
86 if (boost::dynamic_pointer_cast<DirectoryNode>(i->second))
93 case ParseCommandInfo::BuiltinPUSHD :
94 dirstack_.push_back(cwd_);
95 if ( command.arguments() )
96 cwd_ = traverseTo(command.arguments().begin()[0]).thisptr();
99 case ParseCommandInfo::BuiltinPOPD :
100 if (! dirstack_.empty()) {
101 cwd_ = dirstack_.back();
102 dirstack_.pop_back();
106 case ParseCommandInfo::BuiltinEXIT :
107 throw ExitException();
109 case ParseCommandInfo::BuiltinHELP :
114 boost::make_iterator_range(
115 boost::make_transform_iterator(command.arguments().begin()[0].begin(), TraverseTokens()),
116 boost::make_transform_iterator(command.arguments().begin()[0].end(), TraverseTokens())))
119 output << std::flush;
121 catch (UnknownNodeNameException &) {
122 output << "invalid path" << std::endl;
127 catch (InvalidDirectoryException &) {
128 output << "invalid directory" << std::endl;
130 catch (InvalidCommandException &) {
131 output << "invalid command" << std::endl;
136 prefix_ senf::console::DirectoryNode &
137 senf::console::Executor::traverseTo (ParseCommandInfo::argument_value_type const & path)
140 return dynamic_cast<DirectoryNode&>(
142 boost::make_iterator_range(
143 boost::make_transform_iterator(path.begin(), TraverseTokens()),
144 boost::make_transform_iterator(path.end(), TraverseTokens()))));
146 catch (std::bad_cast &) {
147 throw InvalidDirectoryException();
149 catch (UnknownNodeNameException &) {
150 throw InvalidDirectoryException();
154 prefix_ senf::console::CommandNode &
155 senf::console::Executor::traverseToCommand(ParseCommandInfo::CommandPathRange const & path)
158 return dynamic_cast<CommandNode &>( cwd().traverse(path) );
160 catch (std::bad_cast &) {
161 throw InvalidCommandException();
163 catch (UnknownNodeNameException &) {
164 throw InvalidCommandException();
168 ///////////////////////////////cc.e////////////////////////////////////////
170 //#include "Executor.mpp"
176 // comment-column: 40
177 // c-file-style: "senf"
178 // indent-tabs-mode: nil
179 // ispell-local-dictionary: "american"
180 // compile-command: "scons -u test"