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 TraverseTokens {
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.arguments().begin());
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
112 command.tokens().empty() ? command.tokens() : *command.arguments().begin() );
115 case ParseCommandInfo::BuiltinPUSHD :
116 // The parser ensures, we have exactly one argument
117 pushd( *command.arguments().begin() );
120 case ParseCommandInfo::BuiltinPOPD :
121 // The parser ensures, we have no arguments
125 case ParseCommandInfo::BuiltinEXIT :
128 // The parser ensures, we have no arguments
132 case ParseCommandInfo::BuiltinHELP :
135 // The parser ensures, we have either one or no arguments
137 command.tokens().empty() ? command.tokens() : *command.arguments().begin() );
142 catch (InvalidPathException &) {
143 throw SyntaxErrorException("invalid path");
145 catch (InvalidDirectoryException &) {
146 throw SyntaxErrorException("invalid directory");
148 catch (InvalidCommandException &) {
149 throw SyntaxErrorException("invalid command");
151 catch (IgnoreCommandException &) {}
154 prefix_ void senf::console::Executor::exec(std::ostream & output,
155 ParseCommandInfo const & command)
157 GenericNode & node ( traverseNode(command.commandPath()) );
158 DirectoryNode * dir ( dynamic_cast<DirectoryNode*>(&node) );
160 if (autocd_ && command.tokens().empty()) {
161 cd( boost::make_iterator_range(
162 command.commandPath().begin(),
163 command.commandPath().end()) );
165 throw InvalidCommandException();
167 dynamic_cast<CommandNode &>(node)(output, command);
172 prefix_ void senf::console::Executor::cd(ParseCommandInfo::TokensRange dir)
174 if (dir.size() == 1 && *dir.begin() == WordToken("-")) {
176 (void) cwd(); // Prune any expired items
179 // We need to use a temporary so an error somewhere while traversing the dir does not cause
180 // the current directory to change.
182 traverseDirectory(dir, newDir);
188 prefix_ void senf::console::Executor::ls(std::ostream & output,
189 ParseCommandInfo::TokensRange path)
192 traverseDirectory(path, dir);
193 DirectoryNode & node (*dir.back().lock());
194 DirectoryNode::child_iterator i (node.children().begin());
195 DirectoryNode::child_iterator const i_end (node.children().end());
196 for (; i != i_end; ++i) {
198 if (i->second->isDirectory())
200 else if (i->second->isLink())
206 prefix_ void senf::console::Executor::pushd(ParseCommandInfo::TokensRange dir)
211 traverseDirectory(dir, newDir);
213 catch (IgnoreCommandException &) {
217 dirstack_.push_back(Path());
218 dirstack_.back().swap(cwd_);
222 prefix_ void senf::console::Executor::popd()
224 if (! dirstack_.empty()) {
225 cwd_.swap(dirstack_.back());
226 dirstack_.pop_back();
230 prefix_ void senf::console::Executor::exit()
232 throw ExitException();
235 prefix_ void senf::console::Executor::help(std::ostream & output,
236 ParseCommandInfo::TokensRange path)
238 GenericNode const & node (traverseNode(path));
239 // output << prettyName(typeid(node)) << " at " << node.path() << "\n\n";
241 output << std::flush;
244 prefix_ senf::console::GenericNode &
245 senf::console::Executor::traverseNode(ParseCommandInfo::TokensRange const & path)
248 return *cwd_.back().lock();
251 traverseDirectory(boost::make_iterator_range(
253 boost::prior(path.end())),
255 // For auto-cd support we need to check against '.' and '..' here too
256 Token const & tok (*boost::prior(path.end()));
257 if (tok == WordToken("..")) {
260 return *dir.back().lock();
262 DirectoryNode & base (*dir.back().lock());
263 if (tok == WordToken(".") || tok == NoneToken())
265 std::string const & name (complete(base, tok.value()));
267 policy_( base, name );
268 return dir.back().lock()->get(name);
270 catch (UnknownNodeNameException &) {
271 throw InvalidPathException();
276 senf::console::Executor::traverseDirectory(ParseCommandInfo::TokensRange const & path,
280 ParseCommandInfo::TokensRange::const_iterator i (path.begin());
281 ParseCommandInfo::TokensRange::const_iterator const i_end (path.end());
282 for (; i != i_end; ++i) {
283 if (*i == NoneToken()) {
284 if (i == path.begin()) {
286 dir.push_back(root_);
289 else if (*i == WordToken("..")) {
293 else if (*i == WordToken("."))
296 DirectoryNode & base (*dir.back().lock());
297 std::string name (complete(base, i->value()));
299 policy_( base, name );
300 dir.push_back(base[name].thisptr());
304 catch (std::bad_cast &) {
305 throw InvalidDirectoryException();
307 catch (UnknownNodeNameException &) {
308 throw InvalidDirectoryException();
312 prefix_ std::string senf::console::Executor::complete(DirectoryNode & dir,
313 std::string const & name)
315 if (! dir.hasChild(name)) {
316 DirectoryNode::ChildrenRange completions (dir.completions(name));
317 if (has_one_elt(completions))
318 return completions.begin()->first;
323 ///////////////////////////////cc.e////////////////////////////////////////
325 //#include "Executor.mpp"
331 // comment-column: 40
332 // c-file-style: "senf"
333 // indent-tabs-mode: nil
334 // ispell-local-dictionary: "american"
335 // compile-command: "scons -u test"