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 return "/" + senf::stringJoin(
71 senf::make_transform_range(
72 boost::make_iterator_range(boost::next(cwd_.begin()), cwd_.end()),
73 boost::bind(&DirectoryNode::name, boost::bind(&DirectoryNode::weak_ptr::lock, _1))),
77 prefix_ void senf::console::Executor::execute(std::ostream & output,
78 ParseCommandInfo const & command)
80 SENF_LOG(( "Executing: " << command ));
83 (void) cwd(); // Prune the cwd path of expired entries
86 switch(command.builtin()) {
87 case ParseCommandInfo::NoBuiltin :
90 exec(output, command);
93 case ParseCommandInfo::BuiltinCD :
97 // The parser ensures, we have exactly one argument
98 cd(*command.arguments().begin());
100 catch (IgnoreCommandException &) {
101 throw SyntaxErrorException(
102 "'cd' cannot be skipped (don't use 'cd' in conf-files)");
106 case ParseCommandInfo::BuiltinLS :
109 // The parser ensures, we have either one or no argument
111 command.tokens().empty() ? command.tokens() : *command.arguments().begin() );
114 case ParseCommandInfo::BuiltinPUSHD :
115 // The parser ensures, we have exactly one argument
116 pushd( *command.arguments().begin() );
119 case ParseCommandInfo::BuiltinPOPD :
120 // The parser ensures, we have no arguments
124 case ParseCommandInfo::BuiltinEXIT :
127 // The parser ensures, we have no arguments
131 case ParseCommandInfo::BuiltinHELP :
134 // The parser ensures, we have either one or no arguments
136 command.tokens().empty() ? command.tokens() : *command.arguments().begin() );
141 catch (InvalidPathException &) {
142 throw SyntaxErrorException("invalid path");
144 catch (InvalidDirectoryException &) {
145 throw SyntaxErrorException("invalid directory");
147 catch (InvalidCommandException &) {
148 throw SyntaxErrorException("invalid command");
150 catch (IgnoreCommandException &) {}
153 prefix_ void senf::console::Executor::exec(std::ostream & output,
154 ParseCommandInfo const & command)
156 GenericNode & node ( traverseNode(command.commandPath()) );
157 DirectoryNode * dir ( dynamic_cast<DirectoryNode*>(&node) );
159 if (autocd_ && command.tokens().empty()) {
160 cd( boost::make_iterator_range(
161 command.commandPath().begin(),
162 command.commandPath().end()) );
164 throw InvalidCommandException();
166 dynamic_cast<CommandNode &>(node)(output, command);
171 prefix_ void senf::console::Executor::cd(ParseCommandInfo::TokensRange dir)
173 if (dir.size() == 1 && *dir.begin() == WordToken("-")) {
175 (void) cwd(); // Prune any expired items
178 // We need to use a temporary so an error somewhere while traversing the dir does not cause
179 // the current directory to change.
181 traverseDirectory(dir, newDir);
187 prefix_ void senf::console::Executor::ls(std::ostream & output,
188 ParseCommandInfo::TokensRange path)
191 traverseDirectory(path, dir);
192 DirectoryNode & node (*dir.back().lock());
193 DirectoryNode::child_iterator i (node.children().begin());
194 DirectoryNode::child_iterator const i_end (node.children().end());
195 for (; i != i_end; ++i) {
197 if (i->second->isDirectory())
199 else if (i->second->isLink())
205 prefix_ void senf::console::Executor::pushd(ParseCommandInfo::TokensRange dir)
210 traverseDirectory(dir, newDir);
212 catch (IgnoreCommandException &) {
216 dirstack_.push_back(Path());
217 dirstack_.back().swap(cwd_);
221 prefix_ void senf::console::Executor::popd()
223 if (! dirstack_.empty()) {
224 cwd_.swap(dirstack_.back());
225 dirstack_.pop_back();
229 prefix_ void senf::console::Executor::exit()
231 throw ExitException();
234 prefix_ void senf::console::Executor::help(std::ostream & output,
235 ParseCommandInfo::TokensRange path)
237 GenericNode const & node (traverseNode(path));
238 // output << prettyName(typeid(node)) << " at " << node.path() << "\n\n";
240 output << std::flush;
243 prefix_ senf::console::GenericNode &
244 senf::console::Executor::traverseNode(ParseCommandInfo::TokensRange const & path)
247 return *cwd_.back().lock();
250 traverseDirectory(boost::make_iterator_range(
252 boost::prior(path.end())),
254 // For auto-cd support we need to check against '.' and '..' here too
255 Token const & tok (*boost::prior(path.end()));
256 if (tok == WordToken("..")) {
259 return *dir.back().lock();
261 DirectoryNode & base (*dir.back().lock());
262 if (tok == WordToken(".") || tok == NoneToken())
264 std::string const & name (complete(base, tok.value()));
266 policy_( base, name );
267 return dir.back().lock()->get(name);
269 catch (UnknownNodeNameException &) {
270 throw InvalidPathException();
275 senf::console::Executor::traverseDirectory(ParseCommandInfo::TokensRange const & path,
279 ParseCommandInfo::TokensRange::const_iterator i (path.begin());
280 ParseCommandInfo::TokensRange::const_iterator const i_end (path.end());
281 for (; i != i_end; ++i) {
282 if (*i == NoneToken()) {
283 if (i == path.begin()) {
285 dir.push_back(root_);
288 else if (*i == WordToken("..")) {
292 else if (*i == WordToken("."))
295 DirectoryNode & base (*dir.back().lock());
296 std::string name (complete(base, i->value()));
298 policy_( base, name );
299 dir.push_back(base[name].thisptr());
303 catch (std::bad_cast &) {
304 throw InvalidDirectoryException();
306 catch (UnknownNodeNameException &) {
307 throw InvalidDirectoryException();
311 prefix_ std::string senf::console::Executor::complete(DirectoryNode & dir,
312 std::string const & name)
314 if (! dir.hasChild(name)) {
315 DirectoryNode::ChildrenRange completions (dir.completions(name));
316 if (has_one_elt(completions))
317 return completions.begin()->first;
322 ///////////////////////////////cc.e////////////////////////////////////////
324 //#include "Executor.mpp"
330 // comment-column: 40
331 // c-file-style: "senf"
332 // indent-tabs-mode: nil
333 // ispell-local-dictionary: "american"
334 // compile-command: "scons -u test"