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////////////////////////////////////////
35 ///////////////////////////////////////////////////////////////////////////
36 // senf::console::Executor
38 prefix_ bool senf::console::Executor::operator()(ParseCommandInfo const & command,
39 std::ostream & output)
41 SENF_LOG(( "Executing: " << command ));
44 cwd_ = boost::static_pointer_cast<DirectoryNode>(
45 root().shared_from_this());
47 switch(command.builtin()) {
48 case ParseCommandInfo::NoBuiltin :
51 case ParseCommandInfo::BuiltinCD :
52 if ( command.arguments() &&
53 ! chdir(command.arguments().begin()[0]) )
54 output << "invalid directory\n";
57 case ParseCommandInfo::BuiltinLS :
58 for (DirectoryNode::child_iterator i (cwd().children().begin());
59 i != cwd().children().end(); ++i) {
62 (void) cwd()(i->first);
64 catch (std::bad_cast &) {
71 case ParseCommandInfo::BuiltinPUSHD :
72 dirstack_.push_back(cwd_);
73 if ( command.arguments()
74 && ! chdir(command.arguments().begin()[0]) )
75 output << "invalid directory\n";
78 case ParseCommandInfo::BuiltinPOPD :
79 if (! dirstack_.empty()) {
80 cwd_ = dirstack_.back();
85 case ParseCommandInfo::BuiltinEXIT :
86 throw ExitException();
91 prefix_ bool senf::console::Executor::chdir(ParseCommandInfo::argument_value_type const & path)
93 if (path.size() == 1 && path.begin()->value() == "-") {
94 if (oldCwd_.expired()) {
96 cwd_ = boost::static_pointer_cast<DirectoryNode>(
97 root().shared_from_this());
103 DirectoryNode::ptr dir (cwd_.lock());
104 ParseCommandInfo::token_iterator i (path.begin());
105 ParseCommandInfo::token_iterator const i_end (path.end());
106 if (i != i_end && i->value().empty()) {
107 dir = boost::static_pointer_cast<DirectoryNode>(
108 root().shared_from_this());
111 for (; i != i_end; ++i) {
112 if (i->value() == "..") {
115 dir = boost::static_pointer_cast<DirectoryNode>(
116 root().shared_from_this());
118 else if (! i->value().empty() && i->value() != ".")
119 dir = boost::static_pointer_cast<DirectoryNode>(
120 (*dir)[i->value()].shared_from_this());
125 catch (std::bad_cast &) {
128 catch (UnknownNodeNameException &) {
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"