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 "../../Utils/senfassert.hh"
34 #include "../../Utils/Range.hh"
35 #include "../../Utils/String.hh"
36 #include "../../Utils/range.hh"
38 //#include "Executor.mpp"
40 ///////////////////////////////cc.p////////////////////////////////////////
44 struct TraversTokens {
45 typedef std::string const & result_type;
46 result_type operator()(senf::console::Token const & token) const {
53 ///////////////////////////////////////////////////////////////////////////
54 // senf::console::Executor
56 prefix_ senf::console::DirectoryNode & senf::console::Executor::cwd()
59 SENF_ASSERT( ! cwd_.empty() );
60 while (cwd_.size()>1 && (cwd_.back().expired() || ! cwd_.back().lock()->active()))
62 return * cwd_.back().lock();
65 prefix_ std::string senf::console::Executor::cwdPath()
70 (void) cwd(); // ensure, cwd is live.
71 return "/" + senf::stringJoin(
72 senf::make_transform_range(
73 boost::make_iterator_range(boost::next(cwd_.begin()), cwd_.end()),
74 boost::bind(&DirectoryNode::name, boost::bind(&DirectoryNode::weak_ptr::lock, _1))),
78 prefix_ void senf::console::Executor::execute(std::ostream & output,
79 ParseCommandInfo const & command)
81 SENF_LOG(( "Executing: " << command ));
84 (void) cwd(); // Prune the cwd path of expired entries
87 switch(command.builtin()) {
88 case ParseCommandInfo::NoBuiltin :
91 exec(output, command);
94 case ParseCommandInfo::BuiltinCD :
98 // The parser ensures, we have exactly one argument
99 cd(command.commandPath());
101 catch (IgnoreCommandException &) {
102 throw SyntaxErrorException(
103 "'cd' cannot be skipped (don't use 'cd' in conf-files)");
107 case ParseCommandInfo::BuiltinLS :
110 // The parser ensures, we have either one or no argument
111 ls( output, command.commandPath() );
114 case ParseCommandInfo::BuiltinPUSHD :
115 // The parser ensures, we have exactly one argument
117 pushd(command.commandPath());
119 exec(output, command);
122 case ParseCommandInfo::BuiltinPOPD :
123 // The parser ensures, we have no arguments
127 case ParseCommandInfo::BuiltinEXIT :
130 // The parser ensures, we have no arguments
134 case ParseCommandInfo::BuiltinHELP :
137 // The parser ensures, we have either one or no arguments
138 help( output, command.commandPath() );
143 catch (InvalidPathException & ex) {
144 throw SyntaxErrorException("invalid path") << " '" << ex.path << "'";
146 catch (InvalidDirectoryException & ex) {
147 throw SyntaxErrorException("invalid directory") << " '" << ex.path << "'";
149 catch (InvalidCommandException &) {
150 throw SyntaxErrorException("invalid command");
152 catch (IgnoreCommandException &) {}
155 prefix_ void senf::console::Executor::exec(std::ostream & output,
156 ParseCommandInfo const & command)
159 GenericNode & node ( traverseNode(command.commandPath()) );
160 DirectoryNode * dir ( dynamic_cast<DirectoryNode*>(&node) );
162 if (! command.tokens().empty())
163 throw InvalidCommandException();
164 if (command.builtin() == ParseCommandInfo::BuiltinPUSHD)
165 pushd( command.commandPath() );
167 cd(command.commandPath());
170 throw InvalidCommandException();
173 dynamic_cast<CommandNode &>(node)(rv, output, command);
174 if (command.builtin() == ParseCommandInfo::BuiltinPUSHD) {
175 DirectoryNode::ptr rvdir;
177 rvdir = boost::any_cast<DirectoryNode::ptr>(rv);
179 catch (boost::bad_any_cast &) {
180 throw InvalidCommandException();
183 newDir.push_back(rvdir);
184 dirstack_.push_back(Path());
185 dirstack_.back().swap(cwd_);
190 catch (IgnoreCommandException &) {
191 if (command.builtin() == ParseCommandInfo::BuiltinPUSHD) {
192 dirstack_.push_back(Path());
193 dirstack_.back().swap(cwd_);
201 prefix_ void senf::console::Executor::cd(ParseCommandInfo::TokensRange dir)
203 if (dir.size() == 1 && *dir.begin() == WordToken("-")) {
205 (void) cwd(); // Prune any expired items
208 // We need to use a temporary so an error somewhere while traversing the dir does not cause
209 // the current directory to change.
211 traverseDirectory(dir, newDir);
217 prefix_ void senf::console::Executor::ls(std::ostream & output,
218 ParseCommandInfo::TokensRange path)
221 traverseDirectory(path, dir);
222 DirectoryNode & node (*dir.back().lock());
223 DirectoryNode::child_iterator i (node.children().begin());
224 DirectoryNode::child_iterator const i_end (node.children().end());
225 for (; i != i_end; ++i) {
227 if (i->second->isDirectory())
229 else if (i->second->isLink())
235 prefix_ void senf::console::Executor::pushd(ParseCommandInfo::TokensRange dir)
240 traverseDirectory(dir, newDir);
242 catch (IgnoreCommandException &) {
246 dirstack_.push_back(Path());
247 dirstack_.back().swap(cwd_);
251 prefix_ void senf::console::Executor::popd()
253 if (! dirstack_.empty()) {
254 cwd_.swap(dirstack_.back());
255 dirstack_.pop_back();
259 prefix_ void senf::console::Executor::exit()
261 throw ExitException();
264 prefix_ void senf::console::Executor::help(std::ostream & output,
265 ParseCommandInfo::TokensRange path)
267 GenericNode const & node (traverseNode(path));
268 // output << prettyName(typeid(node)) << " at " << node.path() << "\n\n";
270 output << std::flush;
273 prefix_ senf::console::GenericNode &
274 senf::console::Executor::traverseNode(ParseCommandInfo::TokensRange const & path)
277 return *cwd_.back().lock();
280 traverseDirectory(boost::make_iterator_range(
282 boost::prior(path.end())),
284 // For auto-cd support we need to check against '.' and '..' here too
285 Token const & tok (*boost::prior(path.end()));
286 if (tok == WordToken("..")) {
289 return *dir.back().lock();
291 DirectoryNode & base (*dir.back().lock());
292 if (tok == WordToken(".") || tok == NoneToken())
294 std::string const & name (complete(base, tok.value()));
296 policy_( base, name );
297 return dir.back().lock()->get(name);
299 catch (UnknownNodeNameException &) {
300 throw InvalidPathException(
302 senf::make_transform_range(
303 boost::make_iterator_range(path.begin(), path.end()),
304 boost::bind(&Token::value, _1)),
310 senf::console::Executor::traverseDirectory(ParseCommandInfo::TokensRange const & path,
313 std::string errorPath;
315 ParseCommandInfo::TokensRange::const_iterator i (path.begin());
316 ParseCommandInfo::TokensRange::const_iterator const i_end (path.end());
317 for (; i != i_end; ++i) {
318 if (i != path.begin())
320 errorPath += i->value();
321 if (*i == NoneToken()) {
322 if (i == path.begin()) {
324 dir.push_back(root_);
327 else if (*i == WordToken("..")) {
331 else if (*i == WordToken("."))
334 DirectoryNode & base (*dir.back().lock());
335 std::string name (complete(base, i->value()));
337 policy_( base, name );
338 dir.push_back(base[name].thisptr());
342 catch (std::bad_cast &) {
343 throw InvalidDirectoryException(errorPath);
345 catch (UnknownNodeNameException &) {
346 throw InvalidDirectoryException(errorPath);
350 prefix_ std::string senf::console::Executor::complete(DirectoryNode & dir,
351 std::string const & name)
353 if (! dir.hasChild(name)) {
354 DirectoryNode::ChildrenRange completions (dir.completions(name));
355 if (has_one_elt(completions))
356 return completions.begin()->first;
361 prefix_ void senf::console::senf_console_format_value(DirectoryNode::ptr value,
365 os << "<Directory at '" << value->path() << "'>";
367 os << "<Null Directory>";
370 ///////////////////////////////cc.e////////////////////////////////////////
372 //#include "Executor.mpp"
378 // comment-column: 40
379 // c-file-style: "senf"
380 // indent-tabs-mode: nil
381 // ispell-local-dictionary: "american"
382 // compile-command: "scons -u test"