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>
38 #include "senf/Utils/IgnoreValue.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 senf::IGNORE( 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 senf::IGNORE( 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::BuiltinLL :
119 // The parser ensures, we have either one or no argument
120 ll( output, command.commandPath() );
123 case ParseCommandInfo::BuiltinLR :
126 // The parser ensures, we have either one or no argument
127 lr( output, command.commandPath() );
130 case ParseCommandInfo::BuiltinPUSHD :
131 // The parser ensures, we have exactly one argument
133 pushd(command.commandPath());
135 exec(output, command);
138 case ParseCommandInfo::BuiltinPOPD :
139 // The parser ensures, we have no arguments
143 case ParseCommandInfo::BuiltinEXIT :
146 // The parser ensures, we have no arguments
150 case ParseCommandInfo::BuiltinHELP :
153 // The parser ensures, we have either one or no arguments
154 help( output, command.commandPath() );
159 catch (InvalidPathException & ex) {
160 throw SyntaxErrorException("invalid path") << " '" << ex.path << "'";
162 catch (InvalidDirectoryException & ex) {
163 throw SyntaxErrorException("invalid directory") << " '" << ex.path << "'";
165 catch (InvalidCommandException &) {
166 throw SyntaxErrorException("invalid command");
168 catch (IgnoreCommandException &) {}
171 prefix_ senf::console::GenericNode &
172 senf::console::Executor::getNode(ParseCommandInfo const & command)
175 return traverseNode(command.commandPath());
177 catch (InvalidPathException & ex) {
178 throw SyntaxErrorException("invalid path") << " '" << ex.path << "'";
180 catch (InvalidDirectoryException & ex) {
181 throw SyntaxErrorException("invalid directory") << " '" << ex.path << "'";
185 prefix_ void senf::console::Executor::exec(std::ostream & output,
186 ParseCommandInfo const & command)
189 GenericNode & node ( traverseNode(command.commandPath()) );
190 DirectoryNode * dir ( dynamic_cast<DirectoryNode*>(&node) );
192 if (! command.tokens().empty())
193 throw InvalidCommandException();
194 if (command.builtin() == ParseCommandInfo::BuiltinPUSHD)
195 pushd( command.commandPath() );
197 cd(command.commandPath());
200 throw InvalidCommandException();
203 dynamic_cast<CommandNode &>(node)(rv, output, command);
204 if (command.builtin() == ParseCommandInfo::BuiltinPUSHD) {
205 DirectoryNode::ptr rvdir;
207 rvdir = boost::any_cast<DirectoryNode::ptr>(rv);
209 catch (boost::bad_any_cast &) {
210 throw InvalidCommandException();
213 newDir.push_back(rvdir);
214 dirstack_.push_back(Path());
215 dirstack_.back().swap(cwd_);
220 catch (IgnoreCommandException &) {
221 if (command.builtin() == ParseCommandInfo::BuiltinPUSHD) {
222 dirstack_.push_back(Path());
223 dirstack_.back().swap(cwd_);
231 prefix_ void senf::console::Executor::cd(ParseCommandInfo::TokensRange dir)
233 if (dir.size() == 1 && *dir.begin() == WordToken("-")) {
235 senf::IGNORE( cwd() ); // Prune any expired items
238 // We need to use a temporary so an error somewhere while traversing the dir does not cause
239 // the current directory to change.
241 traverseDirectory(dir, newDir);
247 prefix_ void senf::console::Executor::ls(std::ostream & output,
248 ParseCommandInfo::TokensRange path)
251 traverseDirectory(path, dir);
252 DirectoryNode & node (*dir.back().lock());
253 DirectoryNode::child_iterator i (node.children().begin());
254 DirectoryNode::child_iterator const i_end (node.children().end());
255 for (; i != i_end; ++i)
256 output << i->first << "\n";
259 prefix_ void senf::console::Executor::ll(std::ostream & output,
260 ParseCommandInfo::TokensRange path)
262 # define HELP_COLUMN 28
264 unsigned width (senf::console::Client::getWidth(output, 80u, 60u)-(HELP_COLUMN+1));
266 traverseDirectory(path, dir);
267 DirectoryNode & node (*dir.back().lock());
268 DirectoryNode::child_iterator i (node.children().begin());
269 DirectoryNode::child_iterator const i_end (node.children().end());
270 boost::format fmt ("%s%s %|" BOOST_PP_STRINGIZE(HELP_COLUMN) "t|%s\n");
271 for (; i != i_end; ++i)
274 % ( i->second->isDirectory()
276 : i->second->isLink()
279 % i->second->shorthelp().substr(0,width);
284 # define HELP_COLUMN 40
288 typedef std::map<senf::console::DirectoryNode*,std::string> NodesMap;
290 void dolr(std::ostream & output, unsigned width, NodesMap & nodes, std::string const & base,
291 unsigned level, senf::console::DirectoryNode & node)
293 boost::format fmt ("%s%s%s %|" BOOST_PP_STRINGIZE(HELP_COLUMN) "t|%s\n");
294 std::string pad (2*level, ' ');
295 senf::console::DirectoryNode::child_iterator i (node.children().begin());
296 senf::console::DirectoryNode::child_iterator const i_end (node.children().end());
297 for (; i != i_end; ++i) {
298 if (i->second->followLink().isDirectory()) {
299 senf::console::DirectoryNode & subnode (
300 static_cast<senf::console::DirectoryNode&>(i->second->followLink()));
301 NodesMap::iterator j (nodes.find(&subnode));
302 if (j == nodes.end()) {
305 % ( i->second->isDirectory() ? "/" : i->second->isLink() ? "@" : "" )
306 % i->second->shorthelp().substr(0,width);
307 std::string subbase (base);
308 if (! subbase.empty())
311 nodes.insert(std::make_pair(&subnode, subbase));
312 dolr(output, width, nodes, subbase, level+1, subnode);
314 output << pad << i->first
315 << ( i->second->isDirectory() ? "/" : i->second->isLink() ? "@" : "" )
316 << " -> " << j->second << "\n";
320 % ( i->second->isDirectory() ? "/" : i->second->isLink() ? "@" : "" )
321 % i->second->shorthelp().substr(0,width);
328 prefix_ void senf::console::Executor::lr(std::ostream & output,
329 ParseCommandInfo::TokensRange path)
332 traverseDirectory(path, dir);
333 DirectoryNode & node (*dir.back().lock());
335 dolr(output, senf::console::Client::getWidth(output, 80u, 60u)-(HELP_COLUMN+1),
341 prefix_ void senf::console::Executor::pushd(ParseCommandInfo::TokensRange dir)
346 traverseDirectory(dir, newDir);
348 catch (IgnoreCommandException &) {
352 dirstack_.push_back(Path());
353 dirstack_.back().swap(cwd_);
357 prefix_ void senf::console::Executor::popd()
359 if (! dirstack_.empty()) {
360 cwd_.swap(dirstack_.back());
361 dirstack_.pop_back();
365 prefix_ void senf::console::Executor::exit()
367 throw ExitException();
370 prefix_ void senf::console::Executor::help(std::ostream & output,
371 ParseCommandInfo::TokensRange path)
373 GenericNode const & node (traverseNode(path));
374 // output << prettyName(typeid(node)) << " at " << node.path() << "\n\n";
376 output << std::flush;
379 prefix_ senf::console::GenericNode &
380 senf::console::Executor::traverseNode(ParseCommandInfo::TokensRange const & path)
383 return *cwd_.back().lock();
386 traverseDirectory(boost::make_iterator_range(
388 boost::prior(path.end())),
390 // For auto-cd support we need to check against '.' and '..' here too
391 Token const & tok (*boost::prior(path.end()));
392 if (tok == WordToken("..")) {
395 return *dir.back().lock();
397 DirectoryNode & base (*dir.back().lock());
398 if (tok == WordToken(".") || tok == NoneToken())
400 std::string const & name (complete(base, tok.value()));
402 policy_( base, name );
403 return dir.back().lock()->get(name);
405 catch (UnknownNodeNameException &) {
406 throw InvalidPathException(
408 senf::make_transform_range(path, boost::bind(&Token::value, _1)),
414 senf::console::Executor::traverseDirectory(ParseCommandInfo::TokensRange const & path,
417 std::string errorPath;
419 ParseCommandInfo::TokensRange::const_iterator i (path.begin());
420 ParseCommandInfo::TokensRange::const_iterator const i_end (path.end());
421 for (; i != i_end; ++i) {
422 if (i != path.begin())
424 errorPath += i->value();
425 if (*i == NoneToken()) {
426 if (i == path.begin()) {
428 dir.push_back(root_);
431 else if (*i == WordToken("..")) {
435 else if (*i == WordToken("."))
438 DirectoryNode & base (*dir.back().lock());
439 std::string name (complete(base, i->value()));
441 policy_( base, name );
442 dir.push_back(base[name].thisptr());
446 catch (std::bad_cast &) {
447 throw InvalidDirectoryException(errorPath);
449 catch (UnknownNodeNameException &) {
450 throw InvalidDirectoryException(errorPath);
454 prefix_ std::string senf::console::Executor::complete(DirectoryNode & dir,
455 std::string const & name)
457 if (! dir.hasChild(name)) {
458 DirectoryNode::ChildrenRange completions (dir.completions(name));
459 if (has_one_elt(completions))
460 return completions.begin()->first;
465 prefix_ void senf::console::senf_console_format_value(DirectoryNode::ptr value,
469 os << "<Directory at '" << value->path() << "'>";
471 os << "<Null Directory>";
474 ///////////////////////////////cc.e////////////////////////////////////////
476 //#include "Executor.mpp"
482 // comment-column: 40
483 // c-file-style: "senf"
484 // indent-tabs-mode: nil
485 // ispell-local-dictionary: "american"
486 // compile-command: "scons -u test"