7e1676551ef2c34f1861e48ab5fa0e871ed229ab
[senf.git] / Console / Executor.cc
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief Executor non-inline non-template implementation */
25
26 #include "Executor.hh"
27 //#include "Executor.ih"
28
29 // Custom includes
30
31 //#include "Executor.mpp"
32 #define prefix_
33 ///////////////////////////////cc.p////////////////////////////////////////
34
35 namespace {
36
37     struct TraverseTokens {
38         typedef std::string const & result_type;
39         result_type operator()(senf::console::ArgumentToken const & token) const {
40             return token.value();
41         }
42     };
43     
44 }
45
46 ///////////////////////////////////////////////////////////////////////////
47 // senf::console::Executor
48
49 prefix_ bool senf::console::Executor::operator()(ParseCommandInfo const & command,
50                                                  std::ostream & output)
51 {
52     SENF_LOG(( "Executing: " << command ));
53
54     ///\fixme Whenever checking cwd_.expired(), we also need to check, wether
55     /// the node is still connected to the root.
56     if (cwd_.expired())
57         cwd_ = root().thisptr();
58
59     try {
60         switch(command.builtin()) {
61         case ParseCommandInfo::NoBuiltin :
62             traverseToCommand(command.commandPath())(output, command.arguments());
63             break;
64
65         case ParseCommandInfo::BuiltinCD :
66             if ( command.arguments() ) {
67                 if (command.arguments().begin()->size() == 1 
68                     && command.arguments().begin()->begin()->value() == "-") {
69                     if (oldCwd_.expired()) {
70                         oldCwd_ = cwd_;
71                         cwd_ = root().thisptr();
72                     } else
73                         swap(cwd_, oldCwd_);
74                 }
75                 else {
76                     oldCwd_ = cwd_;
77                     cwd_ = traverseTo(command.arguments().begin()[0]).thisptr();
78                 }
79             }
80             break;
81             
82         case ParseCommandInfo::BuiltinLS : {
83             DirectoryNode const & dir (
84                 command.arguments().empty() ? cwd() : traverseTo(command.arguments().begin()[0]));
85             for (DirectoryNode::child_iterator i (dir.children().begin());
86                  i != dir.children().end(); ++i) {
87                 output << i->first;
88                 if (boost::dynamic_pointer_cast<DirectoryNode>(i->second))
89                     output << "/";
90                 output << "\n";
91             }
92             break;
93         }
94
95         case ParseCommandInfo::BuiltinPUSHD :
96             dirstack_.push_back(cwd_);
97             if ( command.arguments() )
98                 cwd_ = traverseTo(command.arguments().begin()[0]).thisptr();
99             break;
100             
101         case ParseCommandInfo::BuiltinPOPD :
102             if (! dirstack_.empty()) {
103                 cwd_ = dirstack_.back();
104                 dirstack_.pop_back();
105             }
106             break;
107             
108         case ParseCommandInfo::BuiltinEXIT :
109             throw ExitException();
110         }
111     }
112     catch (InvalidDirectoryException &) {
113         output << "invalid directory" << std::endl;
114     }
115     catch (InvalidCommandException &) {
116         output << "invalid command" << std::endl;
117     }
118     return true;
119 }
120
121 prefix_ senf::console::DirectoryNode &
122 senf::console::Executor::traverseTo (ParseCommandInfo::argument_value_type const & path)
123 {
124     try {
125         return dynamic_cast<DirectoryNode&>(
126             cwd().traverse(
127                 boost::make_iterator_range(
128                     boost::make_transform_iterator(path.begin(), TraverseTokens()),
129                     boost::make_transform_iterator(path.end(), TraverseTokens()))));
130     }
131     catch (std::bad_cast &) {
132         throw InvalidDirectoryException();
133     }
134     catch (UnknownNodeNameException &) {
135         throw InvalidDirectoryException();
136     }
137 }
138
139 prefix_ senf::console::CommandNode &
140 senf::console::Executor::traverseToCommand(ParseCommandInfo::CommandPathRange const & path)
141 {
142     try {
143         return dynamic_cast<CommandNode &>( cwd().traverse(path) );
144     }
145     catch (std::bad_cast &) {
146         throw InvalidCommandException();
147     }
148     catch (UnknownNodeNameException &) {
149         throw InvalidCommandException();
150     }        
151 }
152         
153 ///////////////////////////////cc.e////////////////////////////////////////
154 #undef prefix_
155 //#include "Executor.mpp"
156
157 \f
158 // Local Variables:
159 // mode: c++
160 // fill-column: 100
161 // comment-column: 40
162 // c-file-style: "senf"
163 // indent-tabs-mode: nil
164 // ispell-local-dictionary: "american"
165 // compile-command: "scons -u test"
166 // End: