Console: Add console routing to testServer example
[senf.git] / Console / Executor.cc
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief Executor non-inline non-template implementation */
25
26 #include "Executor.hh"
27 //#include "Executor.ih"
28
29 // Custom includes
30
31 //#include "Executor.mpp"
32 #define prefix_
33 ///////////////////////////////cc.p////////////////////////////////////////
34
35 namespace {
36
37     struct TraverseTokens {
38         typedef std::string const & result_type;
39         result_type operator()(senf::console::ArgumentToken const & token) const {
40             return token.value();
41         }
42     };
43     
44 }
45
46 ///////////////////////////////////////////////////////////////////////////
47 // senf::console::Executor
48
49 prefix_ void senf::console::Executor::execute(std::ostream & output,
50                                               ParseCommandInfo const & command)
51 {
52     SENF_LOG(( "Executing: " << command ));
53
54     if (cwd_.expired() || ! cwd().active())
55         cwd_ = root().thisptr();
56
57     try {
58         switch(command.builtin()) {
59         case ParseCommandInfo::NoBuiltin : {
60             GenericNode & node ( traverseCommand(command.commandPath()) );
61             DirectoryNode * dir ( dynamic_cast<DirectoryNode*>(&node) );
62             if ( dir ) {
63                 oldCwd_ = cwd_;
64                 cwd_ = dir->thisptr();
65             } else {
66                 dynamic_cast<CommandNode &>(node)(output, command);
67             }
68             break;
69         }
70
71         case ParseCommandInfo::BuiltinCD :
72             if ( command.arguments() ) {
73                 if (command.arguments().begin()->size() == 1 
74                     && command.arguments().begin()->begin()->value() == "-") {
75                     if (oldCwd_.expired() || ! oldCwd_.lock()->active()) {
76                         oldCwd_ = cwd_;
77                         cwd_ = root().thisptr();
78                     } else
79                         swap(cwd_, oldCwd_);
80                 }
81                 else {
82                     oldCwd_ = cwd_;
83                     cwd_ = traverseDirectory(command.arguments().begin()[0]).thisptr();
84                 }
85             }
86             break;
87             
88         case ParseCommandInfo::BuiltinLS : {
89             DirectoryNode const & dir ( command.arguments()
90                                         ? traverseDirectory(command.arguments().begin()[0])
91                                         : cwd() );
92             for (DirectoryNode::child_iterator i (dir.children().begin());
93                  i != dir.children().end(); ++i) {
94                 output << i->first;
95                 if (boost::dynamic_pointer_cast<DirectoryNode>(i->second))
96                     output << "/";
97                 output << "\n";
98             }
99             break;
100         }
101
102         case ParseCommandInfo::BuiltinPUSHD :
103             dirstack_.push_back(cwd_);
104             if ( command.arguments() )
105                 cwd_ = traverseDirectory(command.arguments().begin()[0]).thisptr();
106             break;
107             
108         case ParseCommandInfo::BuiltinPOPD :
109             if (! dirstack_.empty()) {
110                 cwd_ = dirstack_.back();
111                 dirstack_.pop_back();
112             }
113             break;
114             
115         case ParseCommandInfo::BuiltinEXIT :
116             throw ExitException();
117
118         case ParseCommandInfo::BuiltinHELP :
119             GenericNode const & node (command.arguments() 
120                                       ? traverseNode(command.arguments().begin()[0])
121                                       : cwd());
122             output << prettyName(typeid(node)) << " at " << node.path() << "\n\n";
123             node.help(output);
124             output << std::flush;
125             break;
126
127         }
128     }
129     catch (InvalidPathException &) {
130         output << "invalid path" << std::endl;
131     }
132     catch (InvalidDirectoryException &) {
133         output << "invalid directory" << std::endl;
134     }
135     catch (InvalidCommandException &) {
136         output << "invalid command" << std::endl;
137     }
138 }
139
140 prefix_ senf::console::GenericNode &
141 senf::console::Executor::traverseNode(ParseCommandInfo::argument_value_type const & path)
142 {
143     try {
144         return cwd().traverse(
145             boost::make_iterator_range(
146                 boost::make_transform_iterator(path.begin(), TraverseTokens()),
147                 boost::make_transform_iterator(path.end(), TraverseTokens())));
148     }
149     catch (std::bad_cast &) {
150         throw InvalidPathException();
151     }
152     catch (UnknownNodeNameException &) {
153         throw InvalidPathException();
154     }
155 }
156
157 prefix_ senf::console::GenericNode &
158 senf::console::Executor::traverseCommand(ParseCommandInfo::CommandPathRange const & path)
159 {
160     try {
161         return cwd().traverse(path);
162     }
163     catch (std::bad_cast &) {
164         throw InvalidPathException();
165     }
166     catch (UnknownNodeNameException &) {
167         throw InvalidPathException();
168     }
169 }
170
171 prefix_ senf::console::DirectoryNode &
172 senf::console::Executor::traverseDirectory(ParseCommandInfo::argument_value_type const & path)
173 {
174     try {
175         return dynamic_cast<DirectoryNode&>( traverseNode(path) );
176     }
177     catch (std::bad_cast &) {
178         throw InvalidDirectoryException();
179     }
180     catch (InvalidPathException &) {
181         throw InvalidDirectoryException();
182     }
183 }
184
185 ///////////////////////////////cc.e////////////////////////////////////////
186 #undef prefix_
187 //#include "Executor.mpp"
188
189 \f
190 // Local Variables:
191 // mode: c++
192 // fill-column: 100
193 // comment-column: 40
194 // c-file-style: "senf"
195 // indent-tabs-mode: nil
196 // ispell-local-dictionary: "american"
197 // compile-command: "scons -u test"
198 // End: