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)
173 return traverseNode(command.commandPath());
176 prefix_ void senf::console::Executor::exec(std::ostream & output,
177 ParseCommandInfo const & command)
180 GenericNode & node ( traverseNode(command.commandPath()) );
181 DirectoryNode * dir ( dynamic_cast<DirectoryNode*>(&node) );
183 if (! command.tokens().empty())
184 throw InvalidCommandException();
185 if (command.builtin() == ParseCommandInfo::BuiltinPUSHD)
186 pushd( command.commandPath() );
188 cd(command.commandPath());
191 throw InvalidCommandException();
194 dynamic_cast<CommandNode &>(node)(rv, output, command);
195 if (command.builtin() == ParseCommandInfo::BuiltinPUSHD) {
196 DirectoryNode::ptr rvdir;
198 rvdir = boost::any_cast<DirectoryNode::ptr>(rv);
200 catch (boost::bad_any_cast &) {
201 throw InvalidCommandException();
204 newDir.push_back(rvdir);
205 dirstack_.push_back(Path());
206 dirstack_.back().swap(cwd_);
211 catch (IgnoreCommandException &) {
212 if (command.builtin() == ParseCommandInfo::BuiltinPUSHD) {
213 dirstack_.push_back(Path());
214 dirstack_.back().swap(cwd_);
222 prefix_ void senf::console::Executor::cd(ParseCommandInfo::TokensRange dir)
224 if (dir.size() == 1 && *dir.begin() == WordToken("-")) {
226 (void) cwd(); // Prune any expired items
229 // We need to use a temporary so an error somewhere while traversing the dir does not cause
230 // the current directory to change.
232 traverseDirectory(dir, newDir);
238 prefix_ void senf::console::Executor::ls(std::ostream & output,
239 ParseCommandInfo::TokensRange path)
242 traverseDirectory(path, dir);
243 DirectoryNode & node (*dir.back().lock());
244 DirectoryNode::child_iterator i (node.children().begin());
245 DirectoryNode::child_iterator const i_end (node.children().end());
246 for (; i != i_end; ++i)
247 output << i->first << "\n";
250 prefix_ void senf::console::Executor::ll(std::ostream & output,
251 ParseCommandInfo::TokensRange path)
253 # define HELP_COLUMN 28
255 unsigned width (senf::console::Client::getWidth(output, 80u, 60u)-(HELP_COLUMN+1));
257 traverseDirectory(path, dir);
258 DirectoryNode & node (*dir.back().lock());
259 DirectoryNode::child_iterator i (node.children().begin());
260 DirectoryNode::child_iterator const i_end (node.children().end());
261 boost::format fmt ("%s%s %|" BOOST_PP_STRINGIZE(HELP_COLUMN) "t|%s\n");
262 for (; i != i_end; ++i)
265 % ( i->second->isDirectory()
267 : i->second->isLink()
270 % i->second->shorthelp().substr(0,width);
275 # define HELP_COLUMN 40
279 typedef std::map<senf::console::DirectoryNode*,std::string> NodesMap;
281 void dolr(std::ostream & output, unsigned width, NodesMap & nodes, std::string const & base,
282 unsigned level, senf::console::DirectoryNode & node)
284 boost::format fmt ("%s%s%s %|" BOOST_PP_STRINGIZE(HELP_COLUMN) "t|%s\n");
285 std::string pad (2*level, ' ');
286 senf::console::DirectoryNode::child_iterator i (node.children().begin());
287 senf::console::DirectoryNode::child_iterator const i_end (node.children().end());
288 for (; i != i_end; ++i) {
289 if (i->second->followLink().isDirectory()) {
290 senf::console::DirectoryNode & subnode (
291 static_cast<senf::console::DirectoryNode&>(i->second->followLink()));
292 NodesMap::iterator j (nodes.find(&subnode));
293 if (j == nodes.end()) {
296 % ( i->second->isDirectory() ? "/" : i->second->isLink() ? "@" : "" )
297 % i->second->shorthelp().substr(0,width);
298 std::string subbase (base);
299 if (! subbase.empty())
302 nodes.insert(std::make_pair(&subnode, subbase));
303 dolr(output, width, nodes, subbase, level+1, subnode);
305 output << pad << i->first
306 << ( i->second->isDirectory() ? "/" : i->second->isLink() ? "@" : "" )
307 << " -> " << j->second << "\n";
311 % ( i->second->isDirectory() ? "/" : i->second->isLink() ? "@" : "" )
312 % i->second->shorthelp().substr(0,width);
319 prefix_ void senf::console::Executor::lr(std::ostream & output,
320 ParseCommandInfo::TokensRange path)
323 traverseDirectory(path, dir);
324 DirectoryNode & node (*dir.back().lock());
326 dolr(output, senf::console::Client::getWidth(output, 80u, 60u)-(HELP_COLUMN+1),
332 prefix_ void senf::console::Executor::pushd(ParseCommandInfo::TokensRange dir)
337 traverseDirectory(dir, newDir);
339 catch (IgnoreCommandException &) {
343 dirstack_.push_back(Path());
344 dirstack_.back().swap(cwd_);
348 prefix_ void senf::console::Executor::popd()
350 if (! dirstack_.empty()) {
351 cwd_.swap(dirstack_.back());
352 dirstack_.pop_back();
356 prefix_ void senf::console::Executor::exit()
358 throw ExitException();
361 prefix_ void senf::console::Executor::help(std::ostream & output,
362 ParseCommandInfo::TokensRange path)
364 GenericNode const & node (traverseNode(path));
365 // output << prettyName(typeid(node)) << " at " << node.path() << "\n\n";
367 output << std::flush;
370 prefix_ senf::console::GenericNode &
371 senf::console::Executor::traverseNode(ParseCommandInfo::TokensRange const & path)
374 return *cwd_.back().lock();
377 traverseDirectory(boost::make_iterator_range(
379 boost::prior(path.end())),
381 // For auto-cd support we need to check against '.' and '..' here too
382 Token const & tok (*boost::prior(path.end()));
383 if (tok == WordToken("..")) {
386 return *dir.back().lock();
388 DirectoryNode & base (*dir.back().lock());
389 if (tok == WordToken(".") || tok == NoneToken())
391 std::string const & name (complete(base, tok.value()));
393 policy_( base, name );
394 return dir.back().lock()->get(name);
396 catch (UnknownNodeNameException &) {
397 throw InvalidPathException(
399 senf::make_transform_range(path, boost::bind(&Token::value, _1)),
405 senf::console::Executor::traverseDirectory(ParseCommandInfo::TokensRange const & path,
408 std::string errorPath;
410 ParseCommandInfo::TokensRange::const_iterator i (path.begin());
411 ParseCommandInfo::TokensRange::const_iterator const i_end (path.end());
412 for (; i != i_end; ++i) {
413 if (i != path.begin())
415 errorPath += i->value();
416 if (*i == NoneToken()) {
417 if (i == path.begin()) {
419 dir.push_back(root_);
422 else if (*i == WordToken("..")) {
426 else if (*i == WordToken("."))
429 DirectoryNode & base (*dir.back().lock());
430 std::string name (complete(base, i->value()));
432 policy_( base, name );
433 dir.push_back(base[name].thisptr());
437 catch (std::bad_cast &) {
438 throw InvalidDirectoryException(errorPath);
440 catch (UnknownNodeNameException &) {
441 throw InvalidDirectoryException(errorPath);
445 prefix_ std::string senf::console::Executor::complete(DirectoryNode & dir,
446 std::string const & name)
448 if (! dir.hasChild(name)) {
449 DirectoryNode::ChildrenRange completions (dir.completions(name));
450 if (has_one_elt(completions))
451 return completions.begin()->first;
456 prefix_ void senf::console::senf_console_format_value(DirectoryNode::ptr value,
460 os << "<Directory at '" << value->path() << "'>";
462 os << "<Null Directory>";
465 ///////////////////////////////cc.e////////////////////////////////////////
467 //#include "Executor.mpp"
473 // comment-column: 40
474 // c-file-style: "senf"
475 // indent-tabs-mode: nil
476 // ispell-local-dictionary: "american"
477 // compile-command: "scons -u test"