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"
30 #include <boost/utility.hpp>
31 #include <boost/range/iterator_range.hpp>
32 #include <boost/bind.hpp>
33 #include "../../Utils/senfassert.hh"
34 #include "../../Utils/Range.hh"
35 #include "../../Utils/String.hh"
36 #include "../../Utils/range.hh"
38 //#include "Executor.mpp"
40 ///////////////////////////////cc.p////////////////////////////////////////
44 struct TraverseTokens {
45 typedef std::string const & result_type;
46 result_type operator()(senf::console::Token const & token) const {
53 ///////////////////////////////////////////////////////////////////////////
54 // senf::console::Executor
56 prefix_ senf::console::DirectoryNode & senf::console::Executor::cwd()
59 SENF_ASSERT( ! cwd_.empty() );
60 while (cwd_.size()>1 && (cwd_.back().expired() || ! cwd_.back().lock()->active()))
62 return * cwd_.back().lock();
65 prefix_ std::string senf::console::Executor::cwdPath()
70 return "/" + senf::stringJoin(
71 senf::make_transform_range(
72 boost::make_iterator_range(boost::next(cwd_.begin()), cwd_.end()),
73 boost::bind(&DirectoryNode::name, boost::bind(&DirectoryNode::weak_ptr::lock, _1))),
77 prefix_ void senf::console::Executor::execute(std::ostream & output,
78 ParseCommandInfo const & command)
80 SENF_LOG(( "Executing: " << command ));
83 (void) cwd(); // Prune the cwd path of expired entries
86 switch(command.builtin()) {
87 case ParseCommandInfo::NoBuiltin :
90 exec(output, command);
93 case ParseCommandInfo::BuiltinCD :
97 // The parser ensures, we have exactly one argument
98 cd(*command.arguments().begin());
100 catch (IgnoreCommandException &) {
101 throw SyntaxErrorException(
102 "'cd' cannot be skipped (don't use 'cd' in conf-files)");
106 case ParseCommandInfo::BuiltinLS :
109 // The parser ensures, we have either one or no argument
111 command.tokens().empty() ? command.tokens() : *command.arguments().begin() );
114 case ParseCommandInfo::BuiltinPUSHD :
115 // The parser ensures, we have exactly one argument
116 pushd( *command.arguments().begin() );
119 case ParseCommandInfo::BuiltinPOPD :
120 // The parser ensures, we have no arguments
124 case ParseCommandInfo::BuiltinEXIT :
127 // The parser ensures, we have no arguments
131 case ParseCommandInfo::BuiltinHELP :
134 // The parser ensures, we have either one or no arguments
136 command.tokens().empty() ? command.tokens() : *command.arguments().begin() );
141 catch (InvalidPathException &) {
142 throw SyntaxErrorException("invalid path");
144 catch (InvalidDirectoryException &) {
145 throw SyntaxErrorException("invalid directory");
147 catch (InvalidCommandException &) {
148 throw SyntaxErrorException("invalid command");
150 catch (IgnoreCommandException &) {}
153 prefix_ void senf::console::Executor::exec(std::ostream & output,
154 ParseCommandInfo const & command)
156 GenericNode & node ( traverseNode(command.commandPath()) );
157 DirectoryNode * dir ( dynamic_cast<DirectoryNode*>(&node) );
159 if (autocd_ && command.tokens().empty()) {
160 cd( boost::make_iterator_range(
161 command.commandPath().begin(),
162 command.commandPath().end()) );
164 throw InvalidCommandException();
166 dynamic_cast<CommandNode &>(node)(output, command);
171 prefix_ void senf::console::Executor::cd(ParseCommandInfo::TokensRange dir)
173 if (dir.size() == 1 && *dir.begin() == WordToken("-")) {
175 (void) cwd(); // Prune any expired items
178 // We need to use a temporary so an error somewhere while traversing the dir does not cause
179 // the current directory to change.
181 traverseDirectory(dir, newDir);
187 prefix_ void senf::console::Executor::ls(std::ostream & output,
188 ParseCommandInfo::TokensRange path)
191 traverseDirectory(path, dir);
192 DirectoryNode & node (*dir.back().lock());
193 DirectoryNode::child_iterator i (node.children().begin());
194 DirectoryNode::child_iterator const i_end (node.children().end());
195 for (; i != i_end; ++i) {
197 if (boost::dynamic_pointer_cast<DirectoryNode>(i->second))
203 prefix_ void senf::console::Executor::pushd(ParseCommandInfo::TokensRange dir)
208 traverseDirectory(dir, newDir);
210 catch (IgnoreCommandException &) {
214 dirstack_.push_back(Path());
215 dirstack_.back().swap(cwd_);
219 prefix_ void senf::console::Executor::popd()
221 if (! dirstack_.empty()) {
222 cwd_.swap(dirstack_.back());
223 dirstack_.pop_back();
227 prefix_ void senf::console::Executor::exit()
229 throw ExitException();
232 prefix_ void senf::console::Executor::help(std::ostream & output,
233 ParseCommandInfo::TokensRange path)
235 GenericNode const & node (traverseNode(path));
236 // output << prettyName(typeid(node)) << " at " << node.path() << "\n\n";
238 output << std::flush;
241 prefix_ senf::console::GenericNode &
242 senf::console::Executor::traverseNode(ParseCommandInfo::TokensRange const & path)
245 return *cwd_.back().lock();
248 traverseDirectory(boost::make_iterator_range(
250 boost::prior(path.end())),
252 // For auto-cd support we need to check against '.' and '..' here too
253 Token const & tok (*boost::prior(path.end()));
254 if (tok == WordToken("..")) {
257 return *dir.back().lock();
259 DirectoryNode & base (*dir.back().lock());
260 if (tok == WordToken("."))
262 std::string const & name (complete(base, tok.value()));
264 policy_( base, name );
265 return dir.back().lock()->get(name);
267 catch (UnknownNodeNameException &) {
268 throw InvalidPathException();
273 senf::console::Executor::traverseDirectory(ParseCommandInfo::TokensRange const & path,
277 ParseCommandInfo::TokensRange::const_iterator i (path.begin());
278 ParseCommandInfo::TokensRange::const_iterator const i_end (path.end());
279 for (; i != i_end; ++i) {
280 if (*i == NoneToken()) {
281 if (i == path.begin()) {
283 dir.push_back(root_);
286 else if (*i == WordToken("..")) {
290 else if (*i == WordToken("."))
293 DirectoryNode & base (*dir.back().lock());
294 std::string name (complete(base, i->value()));
296 policy_( base, name );
297 dir.push_back(base[name].thisptr());
301 catch (std::bad_cast &) {
302 throw InvalidDirectoryException();
304 catch (UnknownNodeNameException &) {
305 throw InvalidDirectoryException();
309 prefix_ std::string senf::console::Executor::complete(DirectoryNode & dir,
310 std::string const & name)
312 if (! dir.hasChild(name)) {
313 DirectoryNode::ChildrenRange completions (dir.completions(name));
314 if (has_one_elt(completions))
315 return completions.begin()->first;
320 ///////////////////////////////cc.e////////////////////////////////////////
322 //#include "Executor.mpp"
328 // comment-column: 40
329 // c-file-style: "senf"
330 // indent-tabs-mode: nil
331 // ispell-local-dictionary: "american"
332 // compile-command: "scons -u test"