Console: Implement command execution
g0dil [Mon, 24 Mar 2008 10:10:00 +0000 (10:10 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@760 270642c3-0616-0410-b53a-bc976706d245

Console/Executor.cc
Console/Executor.hh
Console/Node.hh
Console/Server.cc
Console/testServer.cc

index 6070184..7e16765 100644 (file)
@@ -59,8 +59,9 @@ prefix_ bool senf::console::Executor::operator()(ParseCommandInfo const & comman
     try {
         switch(command.builtin()) {
         case ParseCommandInfo::NoBuiltin :
+            traverseToCommand(command.commandPath())(output, command.arguments());
             break;
-            
+
         case ParseCommandInfo::BuiltinCD :
             if ( command.arguments() ) {
                 if (command.arguments().begin()->size() == 1 
@@ -111,11 +112,14 @@ prefix_ bool senf::console::Executor::operator()(ParseCommandInfo const & comman
     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)
+senf::console::Executor::traverseTo (ParseCommandInfo::argument_value_type const & path)
 {
     try {
         return dynamic_cast<DirectoryNode&>(
@@ -131,6 +135,20 @@ senf::console::Executor::traverseTo(ParseCommandInfo::argument_value_type const
         throw InvalidDirectoryException();
     }
 }
+
+prefix_ senf::console::CommandNode &
+senf::console::Executor::traverseToCommand(ParseCommandInfo::CommandPathRange const & path)
+{
+    try {
+        return dynamic_cast<CommandNode &>( cwd().traverse(path) );
+    }
+    catch (std::bad_cast &) {
+        throw InvalidCommandException();
+    }
+    catch (UnknownNodeNameException &) {
+        throw InvalidCommandException();
+    }        
+}
         
 ///////////////////////////////cc.e////////////////////////////////////////
 #undef prefix_
index 658cc99..189eece 100644 (file)
@@ -69,8 +69,10 @@ namespace console {
 
     private:
         DirectoryNode & traverseTo(ParseCommandInfo::argument_value_type const & path);
+        CommandNode & traverseToCommand(ParseCommandInfo::CommandPathRange const & path);
 
         struct InvalidDirectoryException {};
+        struct InvalidCommandException {};
 
         DirectoryNode::weak_ptr cwd_;
         DirectoryNode::weak_ptr oldCwd_;
index 79e5c1b..d081ffd 100644 (file)
@@ -34,6 +34,7 @@
 #include <boost/utility.hpp>
 #include <boost/range/iterator_range.hpp>
 #include "../Utils/Exception.hh"
+#include "Parse.hh"
 
 //#include "Node.mpp"
 ///////////////////////////////hh.p////////////////////////////////////////
@@ -151,6 +152,9 @@ namespace console {
 
         ///////////////////////////////////////////////////////////////////////////
 
+        virtual void operator()(std::ostream & output, 
+                                ParseCommandInfo::ArgumentsRange const & arguments) = 0;
+
         ptr thisptr();
         cptr thisptr() const;
 
index 83f35b2..72fd25c 100644 (file)
@@ -143,7 +143,13 @@ prefix_ void senf::console::Client::clientData(ReadHelper<ClientHandle>::ptr hel
         // THIS COMMITS SUICIDE. THE INSTANCE IS GONE AFTER stopClient RETURNS
         stopClient();
         return;
-    }        
+    }
+    catch (std::exception & ex) {
+        out_ << ex.what() << std::endl;
+    }
+    catch (...) {
+        out_ << "unidentified error (unknown exception thrown)" << std::endl;
+    }
 
     showPrompt();
     ReadHelper<ClientHandle>::dispatch( handle_, 16384u, ReadUntil("\n"),
index 9e5a3c9..b55ca1b 100644 (file)
@@ -29,6 +29,7 @@
 // Custom includes
 #include <iostream>
 #include "Server.hh"
+#include "Node.hh"
 #include "../Scheduler/Scheduler.hh"
 #include "../Utils/Logger/SenfLog.hh"
 
 #define prefix_
 ///////////////////////////////cc.p////////////////////////////////////////
 
+namespace {
+    struct MyCommand : public senf::console::CommandNode
+    {
+        MyCommand(std::string name) : senf::console::CommandNode(name) {}
+        void operator()(std::ostream & output, 
+                        senf::console::ParseCommandInfo::ArgumentsRange const & arguments) {
+            senf::console::ParseCommandInfo::argument_iterator i (arguments.begin());
+            senf::console::ParseCommandInfo::argument_iterator i_end (arguments.end());
+            for (; i != i_end; ++i) {
+                senf::console::ParseCommandInfo::token_iterator j (i->begin());
+                senf::console::ParseCommandInfo::token_iterator j_end (i->end());
+                for (; j != j_end; ++j) 
+                    output << j->value() << ' ';
+            }
+            output << "\n";
+        }
+    };
+}
+
 int main(int, char const **)
 {
     senf::log::ConsoleTarget::instance().route< senf::SenfLog, senf::log::NOTICE >();
@@ -43,6 +63,9 @@ int main(int, char const **)
     senf::console::root().mkdir("network").mkdir("eth0");
     senf::console::root().mkdir("server");
 
+    senf::console::root()["network"].add(
+        std::auto_ptr<senf::console::GenericNode>(new MyCommand("route")));
+
     senf::console::Server::start( senf::INet4SocketAddress("127.0.0.1:23232") )
         .name("testServer");