Fix build script
[senf.git] / Console / Executor.cc
index d40f69d..5000104 100644 (file)
 
 // Custom includes
 #include <boost/utility.hpp>
+#include <boost/range/iterator_range.hpp>
+#include <boost/bind.hpp>
 #include "../Utils/senfassert.hh"
+#include "../Utils/Range.hh"
+#include "../Utils/String.hh"
 
 //#include "Executor.mpp"
 #define prefix_
@@ -57,6 +61,18 @@ prefix_ senf::console::DirectoryNode & senf::console::Executor::cwd()
     return * cwd_.back().lock();
 }
 
+prefix_ std::string senf::console::Executor::cwdPath()
+    const
+{
+    if (skipping())
+        return "";
+    return "/" + senf::stringJoin(
+        senf::make_transform_range(
+            boost::make_iterator_range(boost::next(cwd_.begin()), cwd_.end()),
+            boost::bind(&DirectoryNode::name, boost::bind(&DirectoryNode::weak_ptr::lock, _1))),
+        "/" );
+}
+
 prefix_ void senf::console::Executor::execute(std::ostream & output,
                                               ParseCommandInfo const & command)
 {
@@ -122,13 +138,13 @@ prefix_ void senf::console::Executor::execute(std::ostream & output,
         }
     }
     catch (InvalidPathException &) {
-        output << "invalid path" << std::endl;
+        throw SyntaxErrorException("invalid path");
     }
     catch (InvalidDirectoryException &) {
-        output << "invalid directory" << std::endl;
+        throw SyntaxErrorException("invalid directory");
     }
     catch (InvalidCommandException &) {
-        output << "invalid command" << std::endl;
+        throw SyntaxErrorException("invalid command");
     }
     catch (IgnoreCommandException &) {}
 }
@@ -233,7 +249,7 @@ senf::console::Executor::traverseNode(ParseCommandInfo::TokensRange const & path
                               boost::prior(path.end())),
                           dir);
         DirectoryNode & base (*dir.back().lock());
-        std::string const & name (boost::prior(path.end())->value());
+        std::string const & name (complete(base, boost::prior(path.end())->value()));
         if (policy_)
             policy_( base, name );
         return dir.back().lock()->get(name);
@@ -265,9 +281,10 @@ senf::console::Executor::traverseDirectory(ParseCommandInfo::TokensRange const &
                 ;
             else {
                 DirectoryNode & base (*dir.back().lock());
+                std::string name (complete(base, i->value()));
                 if (policy_)
-                    policy_( base, i->value() );
-                dir.push_back(base[i->value()].thisptr());
+                    policy_( base, name );
+                dir.push_back(base[name].thisptr());
             }
         }
     }
@@ -279,6 +296,17 @@ senf::console::Executor::traverseDirectory(ParseCommandInfo::TokensRange const &
     }
 }
 
+prefix_ std::string senf::console::Executor::complete(DirectoryNode & dir,
+                                                      std::string const & name)
+{
+    if (! dir.hasChild(name)) {
+        DirectoryNode::ChildrenRange completions (dir.completions(name));
+        if (completions.size() == 1)
+            return completions.begin()->first;
+    }
+    return name;
+}
+
 ///////////////////////////////cc.e////////////////////////////////////////
 #undef prefix_
 //#include "Executor.mpp"