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 <boost/format.hpp>
34 #include "../../Utils/senfassert.hh"
35 #include "../../Utils/Range.hh"
36 #include "../../Utils/String.hh"
37 #include "../../Utils/range.hh"
40 //#include "Executor.mpp"
42 ///////////////////////////////cc.p////////////////////////////////////////
46 struct TraversTokens {
47 typedef std::string const & result_type;
48 result_type operator()(senf::console::Token const & token) const {
55 ///////////////////////////////////////////////////////////////////////////
56 // senf::console::Executor
58 prefix_ senf::console::DirectoryNode & senf::console::Executor::cwd()
61 SENF_ASSERT( ! cwd_.empty() );
62 while (cwd_.size()>1 && (cwd_.back().expired() || ! cwd_.back().lock()->active()))
64 return * cwd_.back().lock();
67 prefix_ std::string senf::console::Executor::cwdPath()
72 (void) cwd(); // ensure, cwd is live.
73 return "/" + senf::stringJoin(
74 senf::make_transform_range(
75 boost::make_iterator_range(boost::next(cwd_.begin()), cwd_.end()),
76 boost::bind(&DirectoryNode::name, boost::bind(&DirectoryNode::weak_ptr::lock, _1))),
80 prefix_ void senf::console::Executor::execute(std::ostream & output,
81 ParseCommandInfo const & command)
83 SENF_LOG(( "Executing: " << command ));
86 (void) cwd(); // Prune the cwd path of expired entries
89 switch(command.builtin()) {
90 case ParseCommandInfo::NoBuiltin :
93 exec(output, command);
96 case ParseCommandInfo::BuiltinCD :
100 // The parser ensures, we have exactly one argument
101 cd(command.commandPath());
103 catch (IgnoreCommandException &) {
104 throw SyntaxErrorException(
105 "'cd' cannot be skipped (don't use 'cd' in conf-files)");
109 case ParseCommandInfo::BuiltinLS :
112 // The parser ensures, we have either one or no argument
113 ls( output, command.commandPath() );
116 case ParseCommandInfo::BuiltinLR :
119 // The parser ensures, we have either one or no argument
120 lr( output, command.commandPath() );
123 case ParseCommandInfo::BuiltinPUSHD :
124 // The parser ensures, we have exactly one argument
126 pushd(command.commandPath());
128 exec(output, command);
131 case ParseCommandInfo::BuiltinPOPD :
132 // The parser ensures, we have no arguments
136 case ParseCommandInfo::BuiltinEXIT :
139 // The parser ensures, we have no arguments
143 case ParseCommandInfo::BuiltinHELP :
146 // The parser ensures, we have either one or no arguments
147 help( output, command.commandPath() );
152 catch (InvalidPathException & ex) {
153 throw SyntaxErrorException("invalid path") << " '" << ex.path << "'";
155 catch (InvalidDirectoryException & ex) {
156 throw SyntaxErrorException("invalid directory") << " '" << ex.path << "'";
158 catch (InvalidCommandException &) {
159 throw SyntaxErrorException("invalid command");
161 catch (IgnoreCommandException &) {}
164 prefix_ void senf::console::Executor::exec(std::ostream & output,
165 ParseCommandInfo const & command)
168 GenericNode & node ( traverseNode(command.commandPath()) );
169 DirectoryNode * dir ( dynamic_cast<DirectoryNode*>(&node) );
171 if (! command.tokens().empty())
172 throw InvalidCommandException();
173 if (command.builtin() == ParseCommandInfo::BuiltinPUSHD)
174 pushd( command.commandPath() );
176 cd(command.commandPath());
179 throw InvalidCommandException();
182 dynamic_cast<CommandNode &>(node)(rv, output, command);
183 if (command.builtin() == ParseCommandInfo::BuiltinPUSHD) {
184 DirectoryNode::ptr rvdir;
186 rvdir = boost::any_cast<DirectoryNode::ptr>(rv);
188 catch (boost::bad_any_cast &) {
189 throw InvalidCommandException();
192 newDir.push_back(rvdir);
193 dirstack_.push_back(Path());
194 dirstack_.back().swap(cwd_);
199 catch (IgnoreCommandException &) {
200 if (command.builtin() == ParseCommandInfo::BuiltinPUSHD) {
201 dirstack_.push_back(Path());
202 dirstack_.back().swap(cwd_);
210 prefix_ void senf::console::Executor::cd(ParseCommandInfo::TokensRange dir)
212 if (dir.size() == 1 && *dir.begin() == WordToken("-")) {
214 (void) cwd(); // Prune any expired items
217 // We need to use a temporary so an error somewhere while traversing the dir does not cause
218 // the current directory to change.
220 traverseDirectory(dir, newDir);
226 prefix_ void senf::console::Executor::ls(std::ostream & output,
227 ParseCommandInfo::TokensRange path)
231 width = senf::console::Client::get(output).width();
233 catch (std::bad_cast &)
239 traverseDirectory(path, dir);
240 DirectoryNode & node (*dir.back().lock());
241 DirectoryNode::child_iterator i (node.children().begin());
242 DirectoryNode::child_iterator const i_end (node.children().end());
243 boost::format fmt ("%s%s %|28t|%s\n");
244 for (; i != i_end; ++i)
247 % ( i->second->isDirectory()
249 : i->second->isLink()
252 % i->second->shorthelp().substr(0,width);
257 typedef std::map<senf::console::DirectoryNode*,std::string> NodesMap;
259 void dolr(std::ostream & output, unsigned width, NodesMap & nodes, std::string const & base,
260 unsigned level, senf::console::DirectoryNode & node)
262 boost::format fmt ("%s%s%s %|40t|%s\n");
263 std::string pad (2*level, ' ');
264 senf::console::DirectoryNode::child_iterator i (node.children().begin());
265 senf::console::DirectoryNode::child_iterator const i_end (node.children().end());
266 for (; i != i_end; ++i) {
270 % ( i->second->isDirectory()
272 : i->second->isLink()
275 % i->second->shorthelp().substr(0,width);
276 if (i->second->followLink().isDirectory()) {
277 senf::console::DirectoryNode & subnode (
278 static_cast<senf::console::DirectoryNode&>(i->second->followLink()));
279 NodesMap::iterator j (nodes.find(&subnode));
280 if (j == nodes.end()) {
281 std::string subbase (base);
282 if (! subbase.empty())
285 nodes.insert(std::make_pair(&subnode, subbase));
286 dolr(output, width, nodes, subbase, level+1, subnode);
288 output << pad << " -> " << j->second << "\n";
295 prefix_ void senf::console::Executor::lr(std::ostream & output,
296 ParseCommandInfo::TokensRange path)
300 width = senf::console::Client::get(output).width();
302 catch (std::bad_cast &)
308 traverseDirectory(path, dir);
309 DirectoryNode & node (*dir.back().lock());
311 dolr(output, width, nodes, "", 0, node);
314 prefix_ void senf::console::Executor::pushd(ParseCommandInfo::TokensRange dir)
319 traverseDirectory(dir, newDir);
321 catch (IgnoreCommandException &) {
325 dirstack_.push_back(Path());
326 dirstack_.back().swap(cwd_);
330 prefix_ void senf::console::Executor::popd()
332 if (! dirstack_.empty()) {
333 cwd_.swap(dirstack_.back());
334 dirstack_.pop_back();
338 prefix_ void senf::console::Executor::exit()
340 throw ExitException();
343 prefix_ void senf::console::Executor::help(std::ostream & output,
344 ParseCommandInfo::TokensRange path)
346 GenericNode const & node (traverseNode(path));
347 // output << prettyName(typeid(node)) << " at " << node.path() << "\n\n";
349 output << std::flush;
352 prefix_ senf::console::GenericNode &
353 senf::console::Executor::traverseNode(ParseCommandInfo::TokensRange const & path)
356 return *cwd_.back().lock();
359 traverseDirectory(boost::make_iterator_range(
361 boost::prior(path.end())),
363 // For auto-cd support we need to check against '.' and '..' here too
364 Token const & tok (*boost::prior(path.end()));
365 if (tok == WordToken("..")) {
368 return *dir.back().lock();
370 DirectoryNode & base (*dir.back().lock());
371 if (tok == WordToken(".") || tok == NoneToken())
373 std::string const & name (complete(base, tok.value()));
375 policy_( base, name );
376 return dir.back().lock()->get(name);
378 catch (UnknownNodeNameException &) {
379 throw InvalidPathException(
381 senf::make_transform_range(path, boost::bind(&Token::value, _1)),
387 senf::console::Executor::traverseDirectory(ParseCommandInfo::TokensRange const & path,
390 std::string errorPath;
392 ParseCommandInfo::TokensRange::const_iterator i (path.begin());
393 ParseCommandInfo::TokensRange::const_iterator const i_end (path.end());
394 for (; i != i_end; ++i) {
395 if (i != path.begin())
397 errorPath += i->value();
398 if (*i == NoneToken()) {
399 if (i == path.begin()) {
401 dir.push_back(root_);
404 else if (*i == WordToken("..")) {
408 else if (*i == WordToken("."))
411 DirectoryNode & base (*dir.back().lock());
412 std::string name (complete(base, i->value()));
414 policy_( base, name );
415 dir.push_back(base[name].thisptr());
419 catch (std::bad_cast &) {
420 throw InvalidDirectoryException(errorPath);
422 catch (UnknownNodeNameException &) {
423 throw InvalidDirectoryException(errorPath);
427 prefix_ std::string senf::console::Executor::complete(DirectoryNode & dir,
428 std::string const & name)
430 if (! dir.hasChild(name)) {
431 DirectoryNode::ChildrenRange completions (dir.completions(name));
432 if (has_one_elt(completions))
433 return completions.begin()->first;
438 prefix_ void senf::console::senf_console_format_value(DirectoryNode::ptr value,
442 os << "<Directory at '" << value->path() << "'>";
444 os << "<Null Directory>";
447 ///////////////////////////////cc.e////////////////////////////////////////
449 //#include "Executor.mpp"
455 // comment-column: 40
456 // c-file-style: "senf"
457 // indent-tabs-mode: nil
458 // ispell-local-dictionary: "american"
459 // compile-command: "scons -u test"