Packets: extended description of bad_cast exception in Packet.as()
[senf.git] / senf / Utils / Console / Config.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 Config non-inline non-template implementation */
25
26 #include "Config.hh"
27 #include "Config.ih"
28
29 // Custom includes
30 #include <senf/Utils/membind.hh>
31
32 //#include "Config.mpp"
33 #define prefix_
34 //-/////////////////////////////////////////////////////////////////////////////////////////////////
35
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37 // senf::console::detail::RestrictedExecutor
38
39 prefix_ senf::console::detail::RestrictedExecutor::RestrictedExecutor(DirectoryNode & root)
40 {
41     executor_
42         .chroot(root)
43         .policy(senf::membind(&RestrictedExecutor::policyCallback, this));
44 }
45
46 prefix_ void
47 senf::console::detail::RestrictedExecutor::execute(std::ostream & output,
48                                                    ParseCommandInfo const & command)
49 {
50     executor_.execute(output, command);
51 }
52
53 prefix_ void
54 senf::console::detail::RestrictedExecutor::operator()(std::ostream & output,
55                                                       ParseCommandInfo const & command)
56 {
57     execute(output, command);
58 }
59
60 prefix_ senf::console::GenericNode &
61 senf::console::detail::RestrictedExecutor::getNode(ParseCommandInfo const & command)
62 {
63     return executor_.getNode(command);
64 }
65
66 prefix_ bool senf::console::detail::RestrictedExecutor::parsed(GenericNode & node)
67     const
68 {
69     ParsedNodes::const_iterator i (parsedNodes_.begin());
70     ParsedNodes::const_iterator const i_end (parsedNodes_.end());
71     for (; i != i_end; ++i)
72         if ( ! i->expired() && node.isChildOf(*(i->lock())) )
73             return true;
74     return false;
75 }
76
77 prefix_ void senf::console::detail::RestrictedExecutor::policyCallback(DirectoryNode & dir,
78                                                                        std::string const & name)
79 {
80     if (dir.hasChild(name)) {
81         GenericNode & item (dir.get(name));
82         if (restrict_ && ! item.isChildOf(*restrict_) && ! item.isDirectory())
83             throw Executor::IgnoreCommandException();
84         if (parsed(item))
85             throw Executor::IgnoreCommandException();
86     }
87     else if (restrict_ && ! dir.isChildOf(*restrict_))
88         throw Executor::IgnoreCommandException();
89 }
90
91 namespace {
92     struct RemoveNodesFn
93     {
94         RemoveNodesFn(senf::console::DirectoryNode::ptr newNode) : newNode_ (newNode) {}
95
96         bool operator()(senf::console::DirectoryNode::weak_ptr node) const
97             { return node.expired() || node.lock()->isChildOf(*newNode_); }
98
99         senf::console::DirectoryNode::ptr newNode_;
100     };
101 }
102
103 prefix_ void
104 senf::console::detail::RestrictedExecutor::insertParsedNode(DirectoryNode & node)
105 {
106     parsedNodes_.erase(
107         std::remove_if(parsedNodes_.begin(), parsedNodes_.end(), RemoveNodesFn(node.thisptr())),
108         parsedNodes_.end());
109     parsedNodes_.push_back(node.thisptr());
110 }
111
112 //-/////////////////////////////////////////////////////////////////////////////////////////////////
113 // senf::console::ConfigBundle
114
115 prefix_ void senf::console::ConfigBundle::parse()
116 {
117     detail::RestrictedExecutor::RestrictGuard guard (executor_);
118     parseInternal();
119 }
120
121 prefix_ void senf::console::ConfigBundle::parse(DirectoryNode & restrict)
122 {
123     detail::RestrictedExecutor::RestrictGuard guard (executor_, restrict);
124     parseInternal();
125 }
126
127 prefix_ void senf::console::ConfigBundle::parseInternal()
128 {
129     // It is valid to add additional sources at the end while parsing ...
130     for (Sources::const_iterator i (sources_.begin()); i != sources_.end(); ++i)
131         (*i)->parse(executor_);
132 }
133
134 //-/////////////////////////////////////////////////////////////////////////////////////////////////
135 // senf::console::detail::RestrictedExecutor::RestrictGuard
136
137 prefix_ senf::console::detail::RestrictedExecutor::RestrictGuard::
138 RestrictGuard(RestrictedExecutor & executor)
139     : executor_ (executor)
140 {
141     // This MUST BE root() not chroot() since restriction does NOT follow symlinks.
142     // Therefore, if chroot() is a directory of symlinks, restricting to it will
143     // execute NOTHING.
144     executor_.restrict_ = console::root().thisptr();
145 }
146
147 prefix_ senf::console::detail::RestrictedExecutor::RestrictGuard::
148 RestrictGuard(RestrictedExecutor & executor, DirectoryNode & restrict)
149     : executor_ (executor)
150 {
151     executor_.restrict_ = restrict.thisptr();
152 }
153
154 prefix_ senf::console::detail::RestrictedExecutor::RestrictGuard::~RestrictGuard()
155 {
156     if (! std::uncaught_exception())
157         executor_.insertParsedNode( *executor_.restrict_ );
158     executor_.restrict_ = console::root().thisptr();
159 }
160
161 //-/////////////////////////////////////////////////////////////////////////////////////////////////
162 // senf::console::detail::ConfigSource
163
164 prefix_ senf::console::detail::ConfigSource::~ConfigSource()
165 {}
166
167 //-/////////////////////////////////////////////////////////////////////////////////////////////////
168 #undef prefix_
169 //#include "Config.mpp"
170
171 \f
172 // Local Variables:
173 // mode: c++
174 // fill-column: 100
175 // comment-column: 40
176 // c-file-style: "senf"
177 // indent-tabs-mode: nil
178 // ispell-local-dictionary: "american"
179 // compile-command: "scons -u test"
180 // End: