xref: /minix3/external/bsd/kyua-cli/dist/engine/config.cpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc // Copyright 2010 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc //   without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc #include "engine/config.hpp"
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc #if defined(HAVE_CONFIG_H)
32*11be35a1SLionel Sambuc #   include "config.h"
33*11be35a1SLionel Sambuc #endif
34*11be35a1SLionel Sambuc 
35*11be35a1SLionel Sambuc #include <stdexcept>
36*11be35a1SLionel Sambuc 
37*11be35a1SLionel Sambuc #include "engine/exceptions.hpp"
38*11be35a1SLionel Sambuc #include "utils/config/exceptions.hpp"
39*11be35a1SLionel Sambuc #include "utils/config/parser.hpp"
40*11be35a1SLionel Sambuc #include "utils/config/tree.ipp"
41*11be35a1SLionel Sambuc #include "utils/passwd.hpp"
42*11be35a1SLionel Sambuc #include "utils/text/exceptions.hpp"
43*11be35a1SLionel Sambuc #include "utils/text/operations.ipp"
44*11be35a1SLionel Sambuc 
45*11be35a1SLionel Sambuc namespace config = utils::config;
46*11be35a1SLionel Sambuc namespace fs = utils::fs;
47*11be35a1SLionel Sambuc namespace passwd = utils::passwd;
48*11be35a1SLionel Sambuc namespace text = utils::text;
49*11be35a1SLionel Sambuc 
50*11be35a1SLionel Sambuc 
51*11be35a1SLionel Sambuc namespace {
52*11be35a1SLionel Sambuc 
53*11be35a1SLionel Sambuc 
54*11be35a1SLionel Sambuc /// Defines the schema of a configuration tree.
55*11be35a1SLionel Sambuc ///
56*11be35a1SLionel Sambuc /// \param [in,out] tree The tree to populate.  The tree should be empty on
57*11be35a1SLionel Sambuc ///     entry to prevent collisions with the keys defined in here.
58*11be35a1SLionel Sambuc static void
init_tree(config::tree & tree)59*11be35a1SLionel Sambuc init_tree(config::tree& tree)
60*11be35a1SLionel Sambuc {
61*11be35a1SLionel Sambuc     tree.define< config::string_node >("architecture");
62*11be35a1SLionel Sambuc     tree.define< config::string_node >("platform");
63*11be35a1SLionel Sambuc     tree.define< engine::user_node >("unprivileged_user");
64*11be35a1SLionel Sambuc     tree.define_dynamic("test_suites");
65*11be35a1SLionel Sambuc }
66*11be35a1SLionel Sambuc 
67*11be35a1SLionel Sambuc 
68*11be35a1SLionel Sambuc /// Fills in a configuration tree with default values.
69*11be35a1SLionel Sambuc ///
70*11be35a1SLionel Sambuc /// \param [in,out] tree The tree to populate.  init_tree() must have been
71*11be35a1SLionel Sambuc ///     called on it beforehand.
72*11be35a1SLionel Sambuc static void
set_defaults(config::tree & tree)73*11be35a1SLionel Sambuc set_defaults(config::tree& tree)
74*11be35a1SLionel Sambuc {
75*11be35a1SLionel Sambuc     tree.set< config::string_node >("architecture", KYUA_ARCHITECTURE);
76*11be35a1SLionel Sambuc     tree.set< config::string_node >("platform", KYUA_PLATFORM);
77*11be35a1SLionel Sambuc }
78*11be35a1SLionel Sambuc 
79*11be35a1SLionel Sambuc 
80*11be35a1SLionel Sambuc /// Configuration parser specialization for Kyua configuration files.
81*11be35a1SLionel Sambuc class config_parser : public config::parser {
82*11be35a1SLionel Sambuc     /// Initializes the configuration tree.
83*11be35a1SLionel Sambuc     ///
84*11be35a1SLionel Sambuc     /// This is a callback executed when the configuration script invokes the
85*11be35a1SLionel Sambuc     /// syntax() method.  We populate the configuration tree from here with the
86*11be35a1SLionel Sambuc     /// schema version requested by the file.
87*11be35a1SLionel Sambuc     ///
88*11be35a1SLionel Sambuc     /// \param [in,out] tree The tree to populate.
89*11be35a1SLionel Sambuc     /// \param syntax_version The version of the file format as specified in the
90*11be35a1SLionel Sambuc     ///     configuration file.
91*11be35a1SLionel Sambuc     ///
92*11be35a1SLionel Sambuc     /// \throw config::syntax_error If the syntax_format/syntax_version
93*11be35a1SLionel Sambuc     /// combination is not supported.
94*11be35a1SLionel Sambuc     void
setup(config::tree & tree,const int syntax_version)95*11be35a1SLionel Sambuc     setup(config::tree& tree, const int syntax_version)
96*11be35a1SLionel Sambuc     {
97*11be35a1SLionel Sambuc         if (syntax_version < 1 || syntax_version > 2)
98*11be35a1SLionel Sambuc             throw config::syntax_error(F("Unsupported config version %s") %
99*11be35a1SLionel Sambuc                                        syntax_version);
100*11be35a1SLionel Sambuc 
101*11be35a1SLionel Sambuc         init_tree(tree);
102*11be35a1SLionel Sambuc         set_defaults(tree);
103*11be35a1SLionel Sambuc     }
104*11be35a1SLionel Sambuc 
105*11be35a1SLionel Sambuc public:
106*11be35a1SLionel Sambuc     /// Initializes the parser.
107*11be35a1SLionel Sambuc     ///
108*11be35a1SLionel Sambuc     /// \param [out] tree_ The tree in which the results of the parsing will be
109*11be35a1SLionel Sambuc     ///     stored when parse() is called.  Should be empty on entry.  Because
110*11be35a1SLionel Sambuc     ///     we grab a reference to this object, the tree must remain valid for
111*11be35a1SLionel Sambuc     ///     the existence of the parser object.
config_parser(config::tree & tree_)112*11be35a1SLionel Sambuc     explicit config_parser(config::tree& tree_) :
113*11be35a1SLionel Sambuc         config::parser(tree_)
114*11be35a1SLionel Sambuc     {
115*11be35a1SLionel Sambuc     }
116*11be35a1SLionel Sambuc };
117*11be35a1SLionel Sambuc 
118*11be35a1SLionel Sambuc 
119*11be35a1SLionel Sambuc }  // anonymous namespace
120*11be35a1SLionel Sambuc 
121*11be35a1SLionel Sambuc 
122*11be35a1SLionel Sambuc /// Copies the node.
123*11be35a1SLionel Sambuc ///
124*11be35a1SLionel Sambuc /// \return A dynamically-allocated node.
125*11be35a1SLionel Sambuc config::detail::base_node*
deep_copy(void) const126*11be35a1SLionel Sambuc engine::user_node::deep_copy(void) const
127*11be35a1SLionel Sambuc {
128*11be35a1SLionel Sambuc     std::auto_ptr< user_node > new_node(new user_node());
129*11be35a1SLionel Sambuc     new_node->_value = _value;
130*11be35a1SLionel Sambuc     return new_node.release();
131*11be35a1SLionel Sambuc }
132*11be35a1SLionel Sambuc 
133*11be35a1SLionel Sambuc 
134*11be35a1SLionel Sambuc /// Pushes the node's value onto the Lua stack.
135*11be35a1SLionel Sambuc ///
136*11be35a1SLionel Sambuc /// \param state The Lua state onto which to push the value.
137*11be35a1SLionel Sambuc void
push_lua(lutok::state & state) const138*11be35a1SLionel Sambuc engine::user_node::push_lua(lutok::state& state) const
139*11be35a1SLionel Sambuc {
140*11be35a1SLionel Sambuc     state.push_string(value().name);
141*11be35a1SLionel Sambuc }
142*11be35a1SLionel Sambuc 
143*11be35a1SLionel Sambuc 
144*11be35a1SLionel Sambuc /// Sets the value of the node from an entry in the Lua stack.
145*11be35a1SLionel Sambuc ///
146*11be35a1SLionel Sambuc /// \param state The Lua state from which to get the value.
147*11be35a1SLionel Sambuc /// \param value_index The stack index in which the value resides.
148*11be35a1SLionel Sambuc ///
149*11be35a1SLionel Sambuc /// \throw value_error If the value in state(value_index) cannot be
150*11be35a1SLionel Sambuc ///     processed by this node.
151*11be35a1SLionel Sambuc void
set_lua(lutok::state & state,const int value_index)152*11be35a1SLionel Sambuc engine::user_node::set_lua(lutok::state& state, const int value_index)
153*11be35a1SLionel Sambuc {
154*11be35a1SLionel Sambuc     if (state.is_number(value_index)) {
155*11be35a1SLionel Sambuc         config::typed_leaf_node< passwd::user >::set(
156*11be35a1SLionel Sambuc             passwd::find_user_by_uid(state.to_integer(-1)));
157*11be35a1SLionel Sambuc     } else if (state.is_string(value_index)) {
158*11be35a1SLionel Sambuc         config::typed_leaf_node< passwd::user >::set(
159*11be35a1SLionel Sambuc             passwd::find_user_by_name(state.to_string(-1)));
160*11be35a1SLionel Sambuc     } else
161*11be35a1SLionel Sambuc         throw config::value_error("Invalid user identifier");
162*11be35a1SLionel Sambuc }
163*11be35a1SLionel Sambuc 
164*11be35a1SLionel Sambuc 
165*11be35a1SLionel Sambuc void
set_string(const std::string & raw_value)166*11be35a1SLionel Sambuc engine::user_node::set_string(const std::string& raw_value)
167*11be35a1SLionel Sambuc {
168*11be35a1SLionel Sambuc     try {
169*11be35a1SLionel Sambuc         config::typed_leaf_node< passwd::user >::set(
170*11be35a1SLionel Sambuc             passwd::find_user_by_name(raw_value));
171*11be35a1SLionel Sambuc     } catch (const std::runtime_error& e) {
172*11be35a1SLionel Sambuc         int uid;
173*11be35a1SLionel Sambuc         try {
174*11be35a1SLionel Sambuc             uid = text::to_type< int >(raw_value);
175*11be35a1SLionel Sambuc         } catch (const text::value_error& e2) {
176*11be35a1SLionel Sambuc             throw error(F("Cannot find user with name '%s'") % raw_value);
177*11be35a1SLionel Sambuc         }
178*11be35a1SLionel Sambuc 
179*11be35a1SLionel Sambuc         try {
180*11be35a1SLionel Sambuc             config::typed_leaf_node< passwd::user >::set(
181*11be35a1SLionel Sambuc                 passwd::find_user_by_uid(uid));
182*11be35a1SLionel Sambuc         } catch (const std::runtime_error& e2) {
183*11be35a1SLionel Sambuc             throw error(F("Cannot find user with UID %s") % uid);
184*11be35a1SLionel Sambuc         }
185*11be35a1SLionel Sambuc     }
186*11be35a1SLionel Sambuc }
187*11be35a1SLionel Sambuc 
188*11be35a1SLionel Sambuc 
189*11be35a1SLionel Sambuc std::string
to_string(void) const190*11be35a1SLionel Sambuc engine::user_node::to_string(void) const
191*11be35a1SLionel Sambuc {
192*11be35a1SLionel Sambuc     return config::typed_leaf_node< passwd::user >::value().name;
193*11be35a1SLionel Sambuc }
194*11be35a1SLionel Sambuc 
195*11be35a1SLionel Sambuc 
196*11be35a1SLionel Sambuc /// Constructs a config with the built-in settings.
197*11be35a1SLionel Sambuc config::tree
default_config(void)198*11be35a1SLionel Sambuc engine::default_config(void)
199*11be35a1SLionel Sambuc {
200*11be35a1SLionel Sambuc     config::tree tree;
201*11be35a1SLionel Sambuc     init_tree(tree);
202*11be35a1SLionel Sambuc     set_defaults(tree);
203*11be35a1SLionel Sambuc     return tree;
204*11be35a1SLionel Sambuc }
205*11be35a1SLionel Sambuc 
206*11be35a1SLionel Sambuc 
207*11be35a1SLionel Sambuc /// Constructs a config with the built-in settings.
208*11be35a1SLionel Sambuc config::tree
empty_config(void)209*11be35a1SLionel Sambuc engine::empty_config(void)
210*11be35a1SLionel Sambuc {
211*11be35a1SLionel Sambuc     config::tree tree;
212*11be35a1SLionel Sambuc     init_tree(tree);
213*11be35a1SLionel Sambuc     return tree;
214*11be35a1SLionel Sambuc }
215*11be35a1SLionel Sambuc 
216*11be35a1SLionel Sambuc 
217*11be35a1SLionel Sambuc /// Parses a test suite configuration file.
218*11be35a1SLionel Sambuc ///
219*11be35a1SLionel Sambuc /// \param file The file to parse.
220*11be35a1SLionel Sambuc ///
221*11be35a1SLionel Sambuc /// \return High-level representation of the configuration file.
222*11be35a1SLionel Sambuc ///
223*11be35a1SLionel Sambuc /// \throw load_error If there is any problem loading the file.  This includes
224*11be35a1SLionel Sambuc ///     file access errors and syntax errors.
225*11be35a1SLionel Sambuc config::tree
load_config(const utils::fs::path & file)226*11be35a1SLionel Sambuc engine::load_config(const utils::fs::path& file)
227*11be35a1SLionel Sambuc {
228*11be35a1SLionel Sambuc     config::tree tree;
229*11be35a1SLionel Sambuc     try {
230*11be35a1SLionel Sambuc         config_parser(tree).parse(file);
231*11be35a1SLionel Sambuc     } catch (const config::error& e) {
232*11be35a1SLionel Sambuc         throw load_error(file, e.what());
233*11be35a1SLionel Sambuc     }
234*11be35a1SLionel Sambuc     return tree;
235*11be35a1SLionel Sambuc }
236