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 "../../Utils/senfassert.hh"
36 #include "../../Utils/Range.hh"
37 #include "../../Utils/String.hh"
38 #include "../../Utils/range.hh"
41 //#include "Executor.mpp"
43 ///////////////////////////////cc.p////////////////////////////////////////
47 struct TraversTokens {
48 typedef std::string const & result_type;
49 result_type operator()(senf::console::Token const & token) const {
56 ///////////////////////////////////////////////////////////////////////////
57 // senf::console::Executor
59 prefix_ senf::console::DirectoryNode & senf::console::Executor::cwd()
62 SENF_ASSERT( ! cwd_.empty() );
63 while (cwd_.size()>1 && (cwd_.back().expired() || ! cwd_.back().lock()->active()))
65 return * cwd_.back().lock();
68 prefix_ std::string senf::console::Executor::cwdPath()
73 (void) cwd(); // ensure, cwd is live.
74 return "/" + senf::stringJoin(
75 senf::make_transform_range(
76 boost::make_iterator_range(boost::next(cwd_.begin()), cwd_.end()),
77 boost::bind(&DirectoryNode::name, boost::bind(&DirectoryNode::weak_ptr::lock, _1))),
81 prefix_ void senf::console::Executor::execute(std::ostream & output,
82 ParseCommandInfo const & command)
84 SENF_LOG(( "Executing: " << command ));
87 (void) cwd(); // Prune the cwd path of expired entries
90 switch(command.builtin()) {
91 case ParseCommandInfo::NoBuiltin :
94 exec(output, command);
97 case ParseCommandInfo::BuiltinCD :
101 // The parser ensures, we have exactly one argument
102 cd(command.commandPath());
104 catch (IgnoreCommandException &) {
105 throw SyntaxErrorException(
106 "'cd' cannot be skipped (don't use 'cd' in conf-files)");
110 case ParseCommandInfo::BuiltinLS :
113 // The parser ensures, we have either one or no argument
114 ls( output, command.commandPath() );
117 case ParseCommandInfo::BuiltinLR :
120 // The parser ensures, we have either one or no argument
121 lr( output, command.commandPath() );
124 case ParseCommandInfo::BuiltinPUSHD :
125 // The parser ensures, we have exactly one argument
127 pushd(command.commandPath());
129 exec(output, command);
132 case ParseCommandInfo::BuiltinPOPD :
133 // The parser ensures, we have no arguments
137 case ParseCommandInfo::BuiltinEXIT :
140 // The parser ensures, we have no arguments
144 case ParseCommandInfo::BuiltinHELP :
147 // The parser ensures, we have either one or no arguments
148 help( output, command.commandPath() );
153 catch (InvalidPathException & ex) {
154 throw SyntaxErrorException("invalid path") << " '" << ex.path << "'";
156 catch (InvalidDirectoryException & ex) {
157 throw SyntaxErrorException("invalid directory") << " '" << ex.path << "'";
159 catch (InvalidCommandException &) {
160 throw SyntaxErrorException("invalid command");
162 catch (IgnoreCommandException &) {}
165 prefix_ senf::console::GenericNode &
166 senf::console::Executor::getNode(ParseCommandInfo const & command)
168 return traverseNode(command.commandPath());
171 prefix_ void senf::console::Executor::exec(std::ostream & output,
172 ParseCommandInfo const & command)
175 GenericNode & node ( traverseNode(command.commandPath()) );
176 DirectoryNode * dir ( dynamic_cast<DirectoryNode*>(&node) );
178 if (! command.tokens().empty())
179 throw InvalidCommandException();
180 if (command.builtin() == ParseCommandInfo::BuiltinPUSHD)
181 pushd( command.commandPath() );
183 cd(command.commandPath());
186 throw InvalidCommandException();
189 dynamic_cast<CommandNode &>(node)(rv, output, command);
190 if (command.builtin() == ParseCommandInfo::BuiltinPUSHD) {
191 DirectoryNode::ptr rvdir;
193 rvdir = boost::any_cast<DirectoryNode::ptr>(rv);
195 catch (boost::bad_any_cast &) {
196 throw InvalidCommandException();
199 newDir.push_back(rvdir);
200 dirstack_.push_back(Path());
201 dirstack_.back().swap(cwd_);
206 catch (IgnoreCommandException &) {
207 if (command.builtin() == ParseCommandInfo::BuiltinPUSHD) {
208 dirstack_.push_back(Path());
209 dirstack_.back().swap(cwd_);
217 prefix_ void senf::console::Executor::cd(ParseCommandInfo::TokensRange dir)
219 if (dir.size() == 1 && *dir.begin() == WordToken("-")) {
221 (void) cwd(); // Prune any expired items
224 // We need to use a temporary so an error somewhere while traversing the dir does not cause
225 // the current directory to change.
227 traverseDirectory(dir, newDir);
233 prefix_ void senf::console::Executor::ls(std::ostream & output,
234 ParseCommandInfo::TokensRange path)
236 # define HELP_COLUMN 28
238 unsigned width (senf::console::Client::getWidth(output, 80u, 60u)-(HELP_COLUMN+1));
240 traverseDirectory(path, dir);
241 DirectoryNode & node (*dir.back().lock());
242 DirectoryNode::child_iterator i (node.children().begin());
243 DirectoryNode::child_iterator const i_end (node.children().end());
244 boost::format fmt ("%s%s %|" BOOST_PP_STRINGIZE(HELP_COLUMN) "t|%s\n");
245 for (; i != i_end; ++i)
248 % ( i->second->isDirectory()
250 : i->second->isLink()
253 % i->second->shorthelp().substr(0,width);
258 # define HELP_COLUMN 40
262 typedef std::map<senf::console::DirectoryNode*,std::string> NodesMap;
264 void dolr(std::ostream & output, unsigned width, NodesMap & nodes, std::string const & base,
265 unsigned level, senf::console::DirectoryNode & node)
267 boost::format fmt ("%s%s%s %|" BOOST_PP_STRINGIZE(HELP_COLUMN) "t|%s\n");
268 std::string pad (2*level, ' ');
269 senf::console::DirectoryNode::child_iterator i (node.children().begin());
270 senf::console::DirectoryNode::child_iterator const i_end (node.children().end());
271 for (; i != i_end; ++i) {
272 if (i->second->followLink().isDirectory()) {
273 senf::console::DirectoryNode & subnode (
274 static_cast<senf::console::DirectoryNode&>(i->second->followLink()));
275 NodesMap::iterator j (nodes.find(&subnode));
276 if (j == nodes.end()) {
279 % ( i->second->isDirectory() ? "/" : i->second->isLink() ? "@" : "" )
280 % i->second->shorthelp().substr(0,width);
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 << i->first
289 << ( i->second->isDirectory() ? "/" : i->second->isLink() ? "@" : "" )
290 << " -> " << j->second << "\n";
294 % ( i->second->isDirectory() ? "/" : i->second->isLink() ? "@" : "" )
295 % i->second->shorthelp().substr(0,width);
302 prefix_ void senf::console::Executor::lr(std::ostream & output,
303 ParseCommandInfo::TokensRange path)
306 traverseDirectory(path, dir);
307 DirectoryNode & node (*dir.back().lock());
309 dolr(output, senf::console::Client::getWidth(output, 80u, 60u)-(HELP_COLUMN+1),
315 prefix_ void senf::console::Executor::pushd(ParseCommandInfo::TokensRange dir)
320 traverseDirectory(dir, newDir);
322 catch (IgnoreCommandException &) {
326 dirstack_.push_back(Path());
327 dirstack_.back().swap(cwd_);
331 prefix_ void senf::console::Executor::popd()
333 if (! dirstack_.empty()) {
334 cwd_.swap(dirstack_.back());
335 dirstack_.pop_back();
339 prefix_ void senf::console::Executor::exit()
341 throw ExitException();
344 prefix_ void senf::console::Executor::help(std::ostream & output,
345 ParseCommandInfo::TokensRange path)
347 GenericNode const & node (traverseNode(path));
348 // output << prettyName(typeid(node)) << " at " << node.path() << "\n\n";
350 output << std::flush;
353 prefix_ senf::console::GenericNode &
354 senf::console::Executor::traverseNode(ParseCommandInfo::TokensRange const & path)
357 return *cwd_.back().lock();
360 traverseDirectory(boost::make_iterator_range(
362 boost::prior(path.end())),
364 // For auto-cd support we need to check against '.' and '..' here too
365 Token const & tok (*boost::prior(path.end()));
366 if (tok == WordToken("..")) {
369 return *dir.back().lock();
371 DirectoryNode & base (*dir.back().lock());
372 if (tok == WordToken(".") || tok == NoneToken())
374 std::string const & name (complete(base, tok.value()));
376 policy_( base, name );
377 return dir.back().lock()->get(name);
379 catch (UnknownNodeNameException &) {
380 throw InvalidPathException(
382 senf::make_transform_range(path, boost::bind(&Token::value, _1)),
388 senf::console::Executor::traverseDirectory(ParseCommandInfo::TokensRange const & path,
391 std::string errorPath;
393 ParseCommandInfo::TokensRange::const_iterator i (path.begin());
394 ParseCommandInfo::TokensRange::const_iterator const i_end (path.end());
395 for (; i != i_end; ++i) {
396 if (i != path.begin())
398 errorPath += i->value();
399 if (*i == NoneToken()) {
400 if (i == path.begin()) {
402 dir.push_back(root_);
405 else if (*i == WordToken("..")) {
409 else if (*i == WordToken("."))
412 DirectoryNode & base (*dir.back().lock());
413 std::string name (complete(base, i->value()));
415 policy_( base, name );
416 dir.push_back(base[name].thisptr());
420 catch (std::bad_cast &) {
421 throw InvalidDirectoryException(errorPath);
423 catch (UnknownNodeNameException &) {
424 throw InvalidDirectoryException(errorPath);
428 prefix_ std::string senf::console::Executor::complete(DirectoryNode & dir,
429 std::string const & name)
431 if (! dir.hasChild(name)) {
432 DirectoryNode::ChildrenRange completions (dir.completions(name));
433 if (has_one_elt(completions))
434 return completions.begin()->first;
439 prefix_ void senf::console::senf_console_format_value(DirectoryNode::ptr value,
443 os << "<Directory at '" << value->path() << "'>";
445 os << "<Null Directory>";
448 ///////////////////////////////cc.e////////////////////////////////////////
450 //#include "Executor.mpp"
456 // comment-column: 40
457 // c-file-style: "senf"
458 // indent-tabs-mode: nil
459 // ispell-local-dictionary: "american"
460 // compile-command: "scons -u test"