Console: Add lots of documentation
[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::operator()(ParseCommandInfo const & command,
50                                                  std::ostream & output)
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             traverseCommand(command.commandPath())(output, command.arguments());
61             break;
62
63         case ParseCommandInfo::BuiltinCD :
64             if ( command.arguments() ) {
65                 if (command.arguments().begin()->size() == 1 
66                     && command.arguments().begin()->begin()->value() == "-") {
67                     if (oldCwd_.expired() || ! oldCwd_.lock()->active()) {
68                         oldCwd_ = cwd_;
69                         cwd_ = root().thisptr();
70                     } else
71                         swap(cwd_, oldCwd_);
72                 }
73                 else {
74                     oldCwd_ = cwd_;
75                     cwd_ = traverseDirectory(command.arguments().begin()[0]).thisptr();
76                 }
77             }
78             break;
79             
80         case ParseCommandInfo::BuiltinLS : {
81             DirectoryNode const & dir ( command.arguments()
82                                         ? traverseDirectory(command.arguments().begin()[0])
83                                         : cwd() );
84             for (DirectoryNode::child_iterator i (dir.children().begin());
85                  i != dir.children().end(); ++i) {
86                 output << i->first;
87                 if (boost::dynamic_pointer_cast<DirectoryNode>(i->second))
88                     output << "/";
89                 output << "\n";
90             }
91             break;
92         }
93
94         case ParseCommandInfo::BuiltinPUSHD :
95             dirstack_.push_back(cwd_);
96             if ( command.arguments() )
97                 cwd_ = traverseDirectory(command.arguments().begin()[0]).thisptr();
98             break;
99             
100         case ParseCommandInfo::BuiltinPOPD :
101             if (! dirstack_.empty()) {
102                 cwd_ = dirstack_.back();
103                 dirstack_.pop_back();
104             }
105             break;
106             
107         case ParseCommandInfo::BuiltinEXIT :
108             throw ExitException();
109
110         case ParseCommandInfo::BuiltinHELP :
111             GenericNode const & node (command.arguments() 
112                                       ? traverseNode(command.arguments().begin()[0])
113                                       : cwd());
114             output << prettyName(typeid(node)) << " at " << node.path() << "\n\n";
115             node.help(output);
116             output << std::flush;
117             break;
118
119         }
120     }
121     catch (InvalidPathException &) {
122         output << "invalid path" << std::endl;
123     }
124     catch (InvalidDirectoryException &) {
125         output << "invalid directory" << std::endl;
126     }
127     catch (InvalidCommandException &) {
128         output << "invalid command" << std::endl;
129     }
130 }
131
132 prefix_ senf::console::GenericNode &
133 senf::console::Executor::traverseNode(ParseCommandInfo::argument_value_type const & path)
134 {
135     try {
136         return cwd().traverse(
137             boost::make_iterator_range(
138                 boost::make_transform_iterator(path.begin(), TraverseTokens()),
139                 boost::make_transform_iterator(path.end(), TraverseTokens())));
140     }
141     catch (std::bad_cast &) {
142         throw InvalidPathException();
143     }
144     catch (UnknownNodeNameException &) {
145         throw InvalidPathException();
146     }
147 }
148
149 prefix_ senf::console::DirectoryNode &
150 senf::console::Executor::traverseDirectory(ParseCommandInfo::argument_value_type const & path)
151 {
152     try {
153         return dynamic_cast<DirectoryNode&>( traverseNode(path) );
154     }
155     catch (std::bad_cast &) {
156         throw InvalidDirectoryException();
157     }
158     catch (InvalidPathException &) {
159         throw InvalidDirectoryException();
160     }
161 }
162
163 prefix_ senf::console::CommandNode &
164 senf::console::Executor::traverseCommand(ParseCommandInfo::CommandPathRange const & path)
165 {
166     try {
167         return dynamic_cast<CommandNode &>( cwd().traverse(path) );
168     }
169     catch (std::bad_cast &) {
170         throw InvalidCommandException();
171     }
172     catch (UnknownNodeNameException &) {
173         throw InvalidCommandException();
174     }        
175 }
176         
177 ///////////////////////////////cc.e////////////////////////////////////////
178 #undef prefix_
179 //#include "Executor.mpp"
180
181 \f
182 // Local Variables:
183 // mode: c++
184 // fill-column: 100
185 // comment-column: 40
186 // c-file-style: "senf"
187 // indent-tabs-mode: nil
188 // ispell-local-dictionary: "american"
189 // compile-command: "scons -u test"
190 // End: