xref: /netbsd-src/external/bsd/atf/dist/atf-c++/config.cpp (revision 6dffe8d42bd46273f674d7ab834e7be9b1af990e)
1 //
2 // Automated Testing Framework (atf)
3 //
4 // Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 // 1. Redistributions of source code must retain the above copyright
11 //    notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 //    notice, this list of conditions and the following disclaimer in the
14 //    documentation and/or other materials provided with the distribution.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #include <map>
31 
32 extern "C" {
33 #include "atf-c/config.h"
34 }
35 
36 #include "atf-c++/config.hpp"
37 #include "atf-c++/env.hpp"
38 #include "atf-c++/sanity.hpp"
39 
40 static std::map< std::string, std::string > m_variables;
41 
42 //
43 // Adds all predefined standard build-time variables to the m_variables
44 // map, considering the values a user may have provided in the environment.
45 //
46 // Can only be called once during the program's lifetime.
47 //
48 static
49 void
50 init_variables(void)
51 {
52     PRE(m_variables.empty());
53 
54     m_variables["atf_arch"] = atf_config_get("atf_arch");
55     m_variables["atf_confdir"] = atf_config_get("atf_confdir");
56     m_variables["atf_libexecdir"] = atf_config_get("atf_libexecdir");
57     m_variables["atf_machine"] = atf_config_get("atf_machine");
58     m_variables["atf_pkgdatadir"] = atf_config_get("atf_pkgdatadir");
59     m_variables["atf_shell"] = atf_config_get("atf_shell");
60     m_variables["atf_workdir"] = atf_config_get("atf_workdir");
61 
62     POST(!m_variables.empty());
63 }
64 
65 const std::string&
66 atf::config::get(const std::string& varname)
67 {
68     if (m_variables.empty())
69         init_variables();
70 
71     PRE(has(varname));
72     return m_variables[varname];
73 }
74 
75 const std::map< std::string, std::string >&
76 atf::config::get_all(void)
77 {
78     if (m_variables.empty())
79         init_variables();
80 
81     return m_variables;
82 }
83 
84 bool
85 atf::config::has(const std::string& varname)
86 {
87     if (m_variables.empty())
88         init_variables();
89 
90     return m_variables.find(varname) != m_variables.end();
91 }
92 
93 extern "C" {
94 void __atf_config_reinit(void);
95 }
96 
97 namespace atf {
98 namespace config {
99 //
100 // Auxiliary function for the t_config test program so that it can
101 // revert the configuration's global status to an empty state and
102 // do new tests from there on.
103 //
104 // Ideally this shouldn't be part of the production library... but
105 // this is so small that it does not matter.
106 //
107 void
108 __reinit(void)
109 {
110     __atf_config_reinit();
111     m_variables.clear();
112 }
113 } // namespace config
114 } // namespace atf
115