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 <boost/preprocessor/stringize.hpp>
35 #include <senf/Utils/senfassert.hh>
36 #include <senf/Utils/Range.hh>
39 //#include "Executor.mpp"
41 ///////////////////////////////cc.p////////////////////////////////////////
45 struct TraversTokens {
46 typedef std::string const & result_type;
47 result_type operator()(senf::console::Token const & token) const {
54 ///////////////////////////////////////////////////////////////////////////
55 // senf::console::Executor
57 prefix_ senf::console::DirectoryNode & senf::console::Executor::cwd()
60 SENF_ASSERT( ! cwd_.empty() );
61 while (cwd_.size()>1 && (cwd_.back().expired() || ! cwd_.back().lock()->active()))
63 return * cwd_.back().lock();
66 prefix_ std::string senf::console::Executor::cwdPath()
71 (void) cwd(); // ensure, cwd is live.
72 return "/" + senf::stringJoin(
73 senf::make_transform_range(
74 boost::make_iterator_range(boost::next(cwd_.begin()), cwd_.end()),
75 boost::bind(&DirectoryNode::name, boost::bind(&DirectoryNode::weak_ptr::lock, _1))),
79 prefix_ void senf::console::Executor::execute(std::ostream & output,
80 ParseCommandInfo const & command)
82 SENF_LOG(( "Executing: " << command ));
85 (void) cwd(); // Prune the cwd path of expired entries
88 switch(command.builtin()) {
89 case ParseCommandInfo::NoBuiltin :
92 exec(output, command);
95 case ParseCommandInfo::BuiltinCD :
99 // The parser ensures, we have exactly one argument
100 cd(command.commandPath());
102 catch (IgnoreCommandException &) {
103 throw SyntaxErrorException(
104 "'cd' cannot be skipped (don't use 'cd' in conf-files)");
108 case ParseCommandInfo::BuiltinLS :
111 // The parser ensures, we have either one or no argument
112 ls( output, command.commandPath() );
115 case ParseCommandInfo::BuiltinLL :
118 // The parser ensures, we have either one or no argument
119 ll( output, command.commandPath() );
122 case ParseCommandInfo::BuiltinLR :
125 // The parser ensures, we have either one or no argument
126 lr( output, command.commandPath() );
129 case ParseCommandInfo::BuiltinPUSHD :
130 // The parser ensures, we have exactly one argument
132 pushd(command.commandPath());
134 exec(output, command);
137 case ParseCommandInfo::BuiltinPOPD :
138 // The parser ensures, we have no arguments
142 case ParseCommandInfo::BuiltinEXIT :
145 // The parser ensures, we have no arguments
149 case ParseCommandInfo::BuiltinHELP :
152 // The parser ensures, we have either one or no arguments
153 help( output, command.commandPath() );
158 catch (InvalidPathException & ex) {
159 throw SyntaxErrorException("invalid path") << " '" << ex.path << "'";
161 catch (InvalidDirectoryException & ex) {
162 throw SyntaxErrorException("invalid directory") << " '" << ex.path << "'";
164 catch (InvalidCommandException &) {
165 throw SyntaxErrorException("invalid command");
167 catch (IgnoreCommandException &) {}
170 prefix_ senf::console::GenericNode &
171 senf::console::Executor::getNode(ParseCommandInfo const & command)
174 return traverseNode(command.commandPath());
176 catch (InvalidPathException & ex) {
177 throw SyntaxErrorException("invalid path") << " '" << ex.path << "'";
179 catch (InvalidDirectoryException & ex) {
180 throw SyntaxErrorException("invalid directory") << " '" << ex.path << "'";
184 prefix_ void senf::console::Executor::exec(std::ostream & output,
185 ParseCommandInfo const & command)
188 GenericNode & node ( traverseNode(command.commandPath()) );
189 DirectoryNode * dir ( dynamic_cast<DirectoryNode*>(&node) );
191 if (! command.tokens().empty())
192 throw InvalidCommandException();
193 if (command.builtin() == ParseCommandInfo::BuiltinPUSHD)
194 pushd( command.commandPath() );
196 cd(command.commandPath());
199 throw InvalidCommandException();
202 dynamic_cast<CommandNode &>(node)(rv, output, command);
203 if (command.builtin() == ParseCommandInfo::BuiltinPUSHD) {
204 DirectoryNode::ptr rvdir;
206 rvdir = boost::any_cast<DirectoryNode::ptr>(rv);
208 catch (boost::bad_any_cast &) {
209 throw InvalidCommandException();
212 newDir.push_back(rvdir);
213 dirstack_.push_back(Path());
214 dirstack_.back().swap(cwd_);
219 catch (IgnoreCommandException &) {
220 if (command.builtin() == ParseCommandInfo::BuiltinPUSHD) {
221 dirstack_.push_back(Path());
222 dirstack_.back().swap(cwd_);
230 prefix_ void senf::console::Executor::cd(ParseCommandInfo::TokensRange dir)
232 if (dir.size() == 1 && *dir.begin() == WordToken("-")) {
234 (void) cwd(); // Prune any expired items
237 // We need to use a temporary so an error somewhere while traversing the dir does not cause
238 // the current directory to change.
240 traverseDirectory(dir, newDir);
246 prefix_ void senf::console::Executor::ls(std::ostream & output,
247 ParseCommandInfo::TokensRange path)
250 traverseDirectory(path, dir);
251 DirectoryNode & node (*dir.back().lock());
252 DirectoryNode::child_iterator i (node.children().begin());
253 DirectoryNode::child_iterator const i_end (node.children().end());
254 for (; i != i_end; ++i)
255 output << i->first << "\n";
258 prefix_ void senf::console::Executor::ll(std::ostream & output,
259 ParseCommandInfo::TokensRange path)
261 # define HELP_COLUMN 28
263 unsigned width (senf::console::Client::getWidth(output, 80u, 60u)-(HELP_COLUMN+1));
265 traverseDirectory(path, dir);
266 DirectoryNode & node (*dir.back().lock());
267 DirectoryNode::child_iterator i (node.children().begin());
268 DirectoryNode::child_iterator const i_end (node.children().end());
269 boost::format fmt ("%s%s %|" BOOST_PP_STRINGIZE(HELP_COLUMN) "t|%s\n");
270 for (; i != i_end; ++i)
273 % ( i->second->isDirectory()
275 : i->second->isLink()
278 % i->second->shorthelp().substr(0,width);
283 # define HELP_COLUMN 40
287 typedef std::map<senf::console::DirectoryNode*,std::string> NodesMap;
289 void dolr(std::ostream & output, unsigned width, NodesMap & nodes, std::string const & base,
290 unsigned level, senf::console::DirectoryNode & node)
292 boost::format fmt ("%s%s%s %|" BOOST_PP_STRINGIZE(HELP_COLUMN) "t|%s\n");
293 std::string pad (2*level, ' ');
294 senf::console::DirectoryNode::child_iterator i (node.children().begin());
295 senf::console::DirectoryNode::child_iterator const i_end (node.children().end());
296 for (; i != i_end; ++i) {
297 if (i->second->followLink().isDirectory()) {
298 senf::console::DirectoryNode & subnode (
299 static_cast<senf::console::DirectoryNode&>(i->second->followLink()));
300 NodesMap::iterator j (nodes.find(&subnode));
301 if (j == nodes.end()) {
304 % ( i->second->isDirectory() ? "/" : i->second->isLink() ? "@" : "" )
305 % i->second->shorthelp().substr(0,width);
306 std::string subbase (base);
307 if (! subbase.empty())
310 nodes.insert(std::make_pair(&subnode, subbase));
311 dolr(output, width, nodes, subbase, level+1, subnode);
313 output << pad << i->first
314 << ( i->second->isDirectory() ? "/" : i->second->isLink() ? "@" : "" )
315 << " -> " << j->second << "\n";
319 % ( i->second->isDirectory() ? "/" : i->second->isLink() ? "@" : "" )
320 % i->second->shorthelp().substr(0,width);
327 prefix_ void senf::console::Executor::lr(std::ostream & output,
328 ParseCommandInfo::TokensRange path)
331 traverseDirectory(path, dir);
332 DirectoryNode & node (*dir.back().lock());
334 dolr(output, senf::console::Client::getWidth(output, 80u, 60u)-(HELP_COLUMN+1),
340 prefix_ void senf::console::Executor::pushd(ParseCommandInfo::TokensRange dir)
345 traverseDirectory(dir, newDir);
347 catch (IgnoreCommandException &) {
351 dirstack_.push_back(Path());
352 dirstack_.back().swap(cwd_);
356 prefix_ void senf::console::Executor::popd()
358 if (! dirstack_.empty()) {
359 cwd_.swap(dirstack_.back());
360 dirstack_.pop_back();
364 prefix_ void senf::console::Executor::exit()
366 throw ExitException();
369 prefix_ void senf::console::Executor::help(std::ostream & output,
370 ParseCommandInfo::TokensRange path)
372 GenericNode const & node (traverseNode(path));
373 // output << prettyName(typeid(node)) << " at " << node.path() << "\n\n";
375 output << std::flush;
378 prefix_ senf::console::GenericNode &
379 senf::console::Executor::traverseNode(ParseCommandInfo::TokensRange const & path)
382 return *cwd_.back().lock();
385 traverseDirectory(boost::make_iterator_range(
387 boost::prior(path.end())),
389 // For auto-cd support we need to check against '.' and '..' here too
390 Token const & tok (*boost::prior(path.end()));
391 if (tok == WordToken("..")) {
394 return *dir.back().lock();
396 DirectoryNode & base (*dir.back().lock());
397 if (tok == WordToken(".") || tok == NoneToken())
399 std::string const & name (complete(base, tok.value()));
401 policy_( base, name );
402 return dir.back().lock()->get(name);
404 catch (UnknownNodeNameException &) {
405 throw InvalidPathException(
407 senf::make_transform_range(path, boost::bind(&Token::value, _1)),
413 senf::console::Executor::traverseDirectory(ParseCommandInfo::TokensRange const & path,
416 std::string errorPath;
418 ParseCommandInfo::TokensRange::const_iterator i (path.begin());
419 ParseCommandInfo::TokensRange::const_iterator const i_end (path.end());
420 for (; i != i_end; ++i) {
421 if (i != path.begin())
423 errorPath += i->value();
424 if (*i == NoneToken()) {
425 if (i == path.begin()) {
427 dir.push_back(root_);
430 else if (*i == WordToken("..")) {
434 else if (*i == WordToken("."))
437 DirectoryNode & base (*dir.back().lock());
438 std::string name (complete(base, i->value()));
440 policy_( base, name );
441 dir.push_back(base[name].thisptr());
445 catch (std::bad_cast &) {
446 throw InvalidDirectoryException(errorPath);
448 catch (UnknownNodeNameException &) {
449 throw InvalidDirectoryException(errorPath);
453 prefix_ std::string senf::console::Executor::complete(DirectoryNode & dir,
454 std::string const & name)
456 if (! dir.hasChild(name)) {
457 DirectoryNode::ChildrenRange completions (dir.completions(name));
458 if (has_one_elt(completions))
459 return completions.begin()->first;
464 prefix_ void senf::console::senf_console_format_value(DirectoryNode::ptr value,
468 os << "<Directory at '" << value->path() << "'>";
470 os << "<Null Directory>";
473 ///////////////////////////////cc.e////////////////////////////////////////
475 //#include "Executor.mpp"
481 // comment-column: 40
482 // c-file-style: "senf"
483 // indent-tabs-mode: nil
484 // ispell-local-dictionary: "american"
485 // compile-command: "scons -u test"