Socket: Fix dynamic_socket_cast() / check_socket_cast() to support crosscasts
[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_ void senf::console::Executor::execute(std::ostream & output,
50                                               ParseCommandInfo const & command)
51 {
52     SENF_LOG(( "Executing: " << command ));
53
54     if (cwd_.expired() || ! cwd().active())
55         cwd_ = root_;
56
57     try {
58         switch(command.builtin()) {
59         case ParseCommandInfo::NoBuiltin : {
60             GenericNode & node ( traverseCommand(command.commandPath()) );
61             DirectoryNode * dir ( dynamic_cast<DirectoryNode*>(&node) );
62             if ( dir ) {
63                 if (autocd_ && command.tokens().empty()) {
64                     oldCwd_ = cwd_;
65                     cwd_ = dir->thisptr();
66                 } else
67                     throw InvalidCommandException();
68             } else {
69                 dynamic_cast<CommandNode &>(node)(output, command);
70             }
71             break;
72         }
73
74         case ParseCommandInfo::BuiltinCD :
75             if ( command.arguments() ) {
76                 if (command.arguments().begin()->size() == 1 
77                     && command.arguments().begin()->begin()->value() == "-") {
78                     if (oldCwd_.expired() || ! oldCwd_.lock()->active()) {
79                         oldCwd_ = cwd_;
80                         cwd_ = root_;
81                     } else
82                         swap(cwd_, oldCwd_);
83                 }
84                 else {
85                     oldCwd_ = cwd_;
86                     cwd_ = traverseDirectory(*command.arguments().begin()).thisptr();
87                 }
88             }
89             break;
90             
91         case ParseCommandInfo::BuiltinLS : {
92             DirectoryNode const & dir ( command.arguments()
93                                         ? traverseDirectory(*command.arguments().begin())
94                                         : cwd() );
95             for (DirectoryNode::child_iterator i (dir.children().begin());
96                  i != dir.children().end(); ++i) {
97                 output << i->first;
98                 if (boost::dynamic_pointer_cast<DirectoryNode>(i->second))
99                     output << "/";
100                 output << "\n";
101             }
102             break;
103         }
104
105         case ParseCommandInfo::BuiltinPUSHD :
106             dirstack_.push_back(cwd_);
107             if ( command.arguments() )
108                 cwd_ = traverseDirectory(*command.arguments().begin()).thisptr();
109             break;
110             
111         case ParseCommandInfo::BuiltinPOPD :
112             if (! dirstack_.empty()) {
113                 cwd_ = dirstack_.back();
114                 dirstack_.pop_back();
115             }
116             break;
117             
118         case ParseCommandInfo::BuiltinEXIT :
119             throw ExitException();
120
121         case ParseCommandInfo::BuiltinHELP :
122             GenericNode const & node (command.arguments() 
123                                       ? traverseNode(*command.arguments().begin())
124                                       : cwd());
125             output << prettyName(typeid(node)) << " at " << node.path() << "\n\n";
126             node.help(output);
127             output << std::flush;
128             break;
129
130         }
131     }
132     catch (InvalidPathException &) {
133         output << "invalid path" << std::endl;
134     }
135     catch (InvalidDirectoryException &) {
136         output << "invalid directory" << std::endl;
137     }
138     catch (InvalidCommandException &) {
139         output << "invalid command" << std::endl;
140     }
141     catch (IgnoreCommandException &) {}
142 }
143
144 prefix_ senf::console::GenericNode &
145 senf::console::Executor::traverseNode(ParseCommandInfo::TokensRange const & path)
146 {
147     try {
148         return traverse(
149             cwd(),
150             boost::make_iterator_range(
151                 boost::make_transform_iterator(path.begin(), TraverseTokens()),
152                 boost::make_transform_iterator(path.end(), TraverseTokens())) );
153     }
154     catch (std::bad_cast &) {
155         throw InvalidPathException();
156     }
157     catch (UnknownNodeNameException &) {
158         throw InvalidPathException();
159     }
160 }
161
162 prefix_ senf::console::GenericNode &
163 senf::console::Executor::traverseCommand(ParseCommandInfo::CommandPathRange const & path)
164 {
165     try {
166         return traverse(cwd(), path);
167     }
168     catch (std::bad_cast &) {
169         throw InvalidPathException();
170     }
171     catch (UnknownNodeNameException &) {
172         throw InvalidPathException();
173     }
174 }
175
176 prefix_ senf::console::DirectoryNode &
177 senf::console::Executor::traverseDirectory(ParseCommandInfo::TokensRange const & path)
178 {
179     try {
180         return dynamic_cast<DirectoryNode&>( traverseNode(path) );
181     }
182     catch (std::bad_cast &) {
183         throw InvalidDirectoryException();
184     }
185     catch (InvalidPathException &) {
186         throw InvalidDirectoryException();
187     }
188 }
189
190 ///////////////////////////////cc.e////////////////////////////////////////
191 #undef prefix_
192 //#include "Executor.mpp"
193
194 \f
195 // Local Variables:
196 // mode: c++
197 // fill-column: 100
198 // comment-column: 40
199 // c-file-style: "senf"
200 // indent-tabs-mode: nil
201 // ispell-local-dictionary: "american"
202 // compile-command: "scons -u test"
203 // End: