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_ void senf::console::Executor::execute(std::ostream & output,
50 ParseCommandInfo const & command)
52 SENF_LOG(( "Executing: " << command ));
54 if (cwd_.expired() || ! cwd().active())
55 cwd_ = root().thisptr();
58 switch(command.builtin()) {
59 case ParseCommandInfo::NoBuiltin : {
60 GenericNode & node ( traverseCommand(command.commandPath()) );
61 DirectoryNode * dir ( dynamic_cast<DirectoryNode*>(&node) );
64 cwd_ = dir->thisptr();
66 dynamic_cast<CommandNode &>(node)(output, command);
71 case ParseCommandInfo::BuiltinCD :
72 if ( command.arguments() ) {
73 if (command.arguments().begin()->size() == 1
74 && command.arguments().begin()->begin()->value() == "-") {
75 if (oldCwd_.expired() || ! oldCwd_.lock()->active()) {
77 cwd_ = root().thisptr();
83 cwd_ = traverseDirectory(command.arguments().begin()[0]).thisptr();
88 case ParseCommandInfo::BuiltinLS : {
89 DirectoryNode const & dir ( command.arguments()
90 ? traverseDirectory(command.arguments().begin()[0])
92 for (DirectoryNode::child_iterator i (dir.children().begin());
93 i != dir.children().end(); ++i) {
95 if (boost::dynamic_pointer_cast<DirectoryNode>(i->second))
102 case ParseCommandInfo::BuiltinPUSHD :
103 dirstack_.push_back(cwd_);
104 if ( command.arguments() )
105 cwd_ = traverseDirectory(command.arguments().begin()[0]).thisptr();
108 case ParseCommandInfo::BuiltinPOPD :
109 if (! dirstack_.empty()) {
110 cwd_ = dirstack_.back();
111 dirstack_.pop_back();
115 case ParseCommandInfo::BuiltinEXIT :
116 throw ExitException();
118 case ParseCommandInfo::BuiltinHELP :
119 GenericNode const & node (command.arguments()
120 ? traverseNode(command.arguments().begin()[0])
122 output << prettyName(typeid(node)) << " at " << node.path() << "\n\n";
124 output << std::flush;
129 catch (InvalidPathException &) {
130 output << "invalid path" << std::endl;
132 catch (InvalidDirectoryException &) {
133 output << "invalid directory" << std::endl;
135 catch (InvalidCommandException &) {
136 output << "invalid command" << std::endl;
140 prefix_ senf::console::GenericNode &
141 senf::console::Executor::traverseNode(ParseCommandInfo::argument_value_type const & path)
144 return cwd().traverse(
145 boost::make_iterator_range(
146 boost::make_transform_iterator(path.begin(), TraverseTokens()),
147 boost::make_transform_iterator(path.end(), TraverseTokens())));
149 catch (std::bad_cast &) {
150 throw InvalidPathException();
152 catch (UnknownNodeNameException &) {
153 throw InvalidPathException();
157 prefix_ senf::console::GenericNode &
158 senf::console::Executor::traverseCommand(ParseCommandInfo::CommandPathRange const & path)
161 return cwd().traverse(path);
163 catch (std::bad_cast &) {
164 throw InvalidPathException();
166 catch (UnknownNodeNameException &) {
167 throw InvalidPathException();
171 prefix_ senf::console::DirectoryNode &
172 senf::console::Executor::traverseDirectory(ParseCommandInfo::argument_value_type const & path)
175 return dynamic_cast<DirectoryNode&>( traverseNode(path) );
177 catch (std::bad_cast &) {
178 throw InvalidDirectoryException();
180 catch (InvalidPathException &) {
181 throw InvalidDirectoryException();
185 ///////////////////////////////cc.e////////////////////////////////////////
187 //#include "Executor.mpp"
193 // comment-column: 40
194 // c-file-style: "senf"
195 // indent-tabs-mode: nil
196 // ispell-local-dictionary: "american"
197 // compile-command: "scons -u test"