Reformat detailed member documentation
[senf.git] / Console / Executor.cc
index b808a09..4d9b2f9 100644 (file)
@@ -46,8 +46,8 @@ namespace {
 ///////////////////////////////////////////////////////////////////////////
 // senf::console::Executor
 
-prefix_ bool senf::console::Executor::operator()(ParseCommandInfo const & command,
-                                                 std::ostream & output)
+prefix_ void senf::console::Executor::execute(std::ostream & output,
+                                              ParseCommandInfo const & command)
 {
     SENF_LOG(( "Executing: " << command ));
 
@@ -56,9 +56,20 @@ prefix_ bool senf::console::Executor::operator()(ParseCommandInfo const & comman
 
     try {
         switch(command.builtin()) {
-        case ParseCommandInfo::NoBuiltin :
-            traverseToCommand(command.commandPath())(output, command.arguments());
+        case ParseCommandInfo::NoBuiltin : {
+            GenericNode & node ( traverseCommand(command.commandPath()) );
+            DirectoryNode * dir ( dynamic_cast<DirectoryNode*>(&node) );
+            if ( dir ) {
+                if (autocd_ && command.arguments().empty()) {
+                    oldCwd_ = cwd_;
+                    cwd_ = dir->thisptr();
+                } else
+                    throw InvalidCommandException();
+            } else {
+                dynamic_cast<CommandNode &>(node)(output, command);
+            }
             break;
+        }
 
         case ParseCommandInfo::BuiltinCD :
             if ( command.arguments() ) {
@@ -72,14 +83,15 @@ prefix_ bool senf::console::Executor::operator()(ParseCommandInfo const & comman
                 }
                 else {
                     oldCwd_ = cwd_;
-                    cwd_ = traverseTo(command.arguments().begin()[0]).thisptr();
+                    cwd_ = traverseDirectory(command.arguments().begin()[0]).thisptr();
                 }
             }
             break;
             
         case ParseCommandInfo::BuiltinLS : {
-            DirectoryNode const & dir (
-                command.arguments().empty() ? cwd() : traverseTo(command.arguments().begin()[0]));
+            DirectoryNode const & dir ( command.arguments()
+                                        ? traverseDirectory(command.arguments().begin()[0])
+                                        : cwd() );
             for (DirectoryNode::child_iterator i (dir.children().begin());
                  i != dir.children().end(); ++i) {
                 output << i->first;
@@ -93,7 +105,7 @@ prefix_ bool senf::console::Executor::operator()(ParseCommandInfo const & comman
         case ParseCommandInfo::BuiltinPUSHD :
             dirstack_.push_back(cwd_);
             if ( command.arguments() )
-                cwd_ = traverseTo(command.arguments().begin()[0]).thisptr();
+                cwd_ = traverseDirectory(command.arguments().begin()[0]).thisptr();
             break;
             
         case ParseCommandInfo::BuiltinPOPD :
@@ -107,64 +119,73 @@ prefix_ bool senf::console::Executor::operator()(ParseCommandInfo const & comman
             throw ExitException();
 
         case ParseCommandInfo::BuiltinHELP :
-            try {
-                GenericNode & node (
-                    command.arguments() 
-                    ? cwd().traverse(
-                        boost::make_iterator_range(
-                            boost::make_transform_iterator(command.arguments().begin()[0].begin(), TraverseTokens()),
-                            boost::make_transform_iterator(command.arguments().begin()[0].end(), TraverseTokens())))
-                    : cwd() );
-                node.help(output);
-                output << std::flush;
-            }
-            catch (UnknownNodeNameException &) {
-                output << "invalid path" << std::endl;
-            }
+            GenericNode const & node (command.arguments() 
+                                      ? traverseNode(command.arguments().begin()[0])
+                                      : cwd());
+            output << prettyName(typeid(node)) << " at " << node.path() << "\n\n";
+            node.help(output);
+            output << std::flush;
             break;
+
         }
     }
+    catch (InvalidPathException &) {
+        output << "invalid path" << std::endl;
+    }
     catch (InvalidDirectoryException &) {
         output << "invalid directory" << std::endl;
     }
     catch (InvalidCommandException &) {
         output << "invalid command" << std::endl;
     }
-    return true;
 }
 
-prefix_ senf::console::DirectoryNode &
-senf::console::Executor::traverseTo (ParseCommandInfo::argument_value_type const & path)
+prefix_ senf::console::GenericNode &
+senf::console::Executor::traverseNode(ParseCommandInfo::argument_value_type const & path)
 {
     try {
-        return dynamic_cast<DirectoryNode&>(
-            cwd().traverse(
-                boost::make_iterator_range(
-                    boost::make_transform_iterator(path.begin(), TraverseTokens()),
-                    boost::make_transform_iterator(path.end(), TraverseTokens()))));
+        return cwd().traverse(
+            boost::make_iterator_range(
+                boost::make_transform_iterator(path.begin(), TraverseTokens()),
+                boost::make_transform_iterator(path.end(), TraverseTokens())),
+            autocomplete_);
     }
     catch (std::bad_cast &) {
-        throw InvalidDirectoryException();
+        throw InvalidPathException();
     }
     catch (UnknownNodeNameException &) {
-        throw InvalidDirectoryException();
+        throw InvalidPathException();
     }
 }
 
-prefix_ senf::console::CommandNode &
-senf::console::Executor::traverseToCommand(ParseCommandInfo::CommandPathRange const & path)
+prefix_ senf::console::GenericNode &
+senf::console::Executor::traverseCommand(ParseCommandInfo::CommandPathRange const & path)
 {
     try {
-        return dynamic_cast<CommandNode &>( cwd().traverse(path) );
+        return cwd().traverse(path, autocomplete_);
     }
     catch (std::bad_cast &) {
-        throw InvalidCommandException();
+        throw InvalidPathException();
     }
     catch (UnknownNodeNameException &) {
-        throw InvalidCommandException();
-    }        
+        throw InvalidPathException();
+    }
 }
-        
+
+prefix_ senf::console::DirectoryNode &
+senf::console::Executor::traverseDirectory(ParseCommandInfo::argument_value_type const & path)
+{
+    try {
+        return dynamic_cast<DirectoryNode&>( traverseNode(path) );
+    }
+    catch (std::bad_cast &) {
+        throw InvalidDirectoryException();
+    }
+    catch (InvalidPathException &) {
+        throw InvalidDirectoryException();
+    }
+}
+
 ///////////////////////////////cc.e////////////////////////////////////////
 #undef prefix_
 //#include "Executor.mpp"