Missing changes from last commit
[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_;
56
57     try {
58         switch(command.builtin()) {
59         case ParseCommandInfo::NoBuiltin : {
60             if (skipping_)
61                 break;
62             GenericNode & node ( traverseCommand(command.commandPath()) );
63             DirectoryNode * dir ( dynamic_cast<DirectoryNode*>(&node) );
64             if ( dir ) {
65                 if (autocd_ && command.tokens().empty()) {
66                     oldCwd_ = cwd_;
67                     cwd_ = dir->thisptr();
68                 } else
69                     throw InvalidCommandException();
70             } else {
71                 dynamic_cast<CommandNode &>(node)(output, command);
72             }
73             break;
74         }
75
76         case ParseCommandInfo::BuiltinCD :
77             if (skipping_)
78                 break;
79             try {
80                 if ( command.arguments() ) {
81                     if (command.arguments().begin()->size() == 1 
82                         && command.arguments().begin()->begin()->value() == "-") {
83                         if (oldCwd_.expired() || ! oldCwd_.lock()->active()) {
84                             oldCwd_ = cwd_;
85                             cwd_ = root_;
86                         } else
87                             swap(cwd_, oldCwd_);
88                     }
89                     else {
90                         oldCwd_ = cwd_;
91                     cwd_ = traverseDirectory(*command.arguments().begin()).thisptr();
92                     }
93                 }
94             }
95             catch (IgnoreCommandException &) {
96                 throw SyntaxErrorException(
97                     "'cd' cannot be skipped (don't use 'cd' in conf-files)");
98             }
99             break;
100             
101         case ParseCommandInfo::BuiltinLS : {
102             if (skipping_)
103                 break;
104             DirectoryNode const & dir ( command.arguments()
105                                         ? traverseDirectory(*command.arguments().begin())
106                                         : cwd() );
107             for (DirectoryNode::child_iterator i (dir.children().begin());
108                  i != dir.children().end(); ++i) {
109                 output << i->first;
110                 if (boost::dynamic_pointer_cast<DirectoryNode>(i->second))
111                     output << "/";
112                 output << "\n";
113             }
114             break;
115         }
116
117         case ParseCommandInfo::BuiltinPUSHD :
118             dirstack_.push_back(DirEntry(cwd_, skipping_));
119             if ( ! skipping_ && command.arguments() ) {
120                 try {
121                     cwd_ = traverseDirectory(*command.arguments().begin()).thisptr();
122                 }
123                 catch (IgnoreCommandException &) {
124                     cwd_.reset();
125                     skipping_ = true;
126                 }
127             }
128             break;
129             
130         case ParseCommandInfo::BuiltinPOPD :
131             if (! dirstack_.empty()) {
132                 cwd_ = dirstack_.back().dir;
133                 skipping_ = dirstack_.back().skip;
134                 dirstack_.pop_back();
135             }
136             break;
137             
138         case ParseCommandInfo::BuiltinEXIT :
139             if (skipping_)
140                 break;
141             throw ExitException();
142
143         case ParseCommandInfo::BuiltinHELP :
144             if (skipping_)
145                 break;
146             GenericNode const & node (command.arguments() 
147                                       ? traverseNode(*command.arguments().begin())
148                                       : cwd());
149             output << prettyName(typeid(node)) << " at " << node.path() << "\n\n";
150             node.help(output);
151             output << std::flush;
152             break;
153
154         }
155     }
156     catch (InvalidPathException &) {
157         output << "invalid path" << std::endl;
158     }
159     catch (InvalidDirectoryException &) {
160         output << "invalid directory" << std::endl;
161     }
162     catch (InvalidCommandException &) {
163         output << "invalid command" << std::endl;
164     }
165     catch (IgnoreCommandException &) {}
166 }
167
168 prefix_ senf::console::GenericNode &
169 senf::console::Executor::traverseNode(ParseCommandInfo::TokensRange const & path)
170 {
171     try {
172         return traverse(
173             cwd(),
174             boost::make_iterator_range(
175                 boost::make_transform_iterator(path.begin(), TraverseTokens()),
176                 boost::make_transform_iterator(path.end(), TraverseTokens())) );
177     }
178     catch (std::bad_cast &) {
179         throw InvalidPathException();
180     }
181     catch (UnknownNodeNameException &) {
182         throw InvalidPathException();
183     }
184 }
185
186 prefix_ senf::console::GenericNode &
187 senf::console::Executor::traverseCommand(ParseCommandInfo::CommandPathRange const & path)
188 {
189     try {
190         return traverse(cwd(), path);
191     }
192     catch (std::bad_cast &) {
193         throw InvalidPathException();
194     }
195     catch (UnknownNodeNameException &) {
196         throw InvalidPathException();
197     }
198 }
199
200 prefix_ senf::console::DirectoryNode &
201 senf::console::Executor::traverseDirectory(ParseCommandInfo::TokensRange const & path)
202 {
203     try {
204         return dynamic_cast<DirectoryNode&>( traverseNode(path) );
205     }
206     catch (std::bad_cast &) {
207         throw InvalidDirectoryException();
208     }
209     catch (InvalidPathException &) {
210         throw InvalidDirectoryException();
211     }
212 }
213
214 ///////////////////////////////cc.e////////////////////////////////////////
215 #undef prefix_
216 //#include "Executor.mpp"
217
218 \f
219 // Local Variables:
220 // mode: c++
221 // fill-column: 100
222 // comment-column: 40
223 // c-file-style: "senf"
224 // indent-tabs-mode: nil
225 // ispell-local-dictionary: "american"
226 // compile-command: "scons -u test"
227 // End: