1//
2// Automated Testing Framework (atf)
3//
4// Copyright (c) 2007 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 <cassert>
31#include <map>
32
33#include "config.hpp"
34#include "env.hpp"
35#include "text.hpp"
36
37static std::map< std::string, std::string > m_variables;
38
39static struct var {
40    const char *name;
41    const char *default_value;
42    bool can_be_empty;
43} vars[] = {
44    { "ATF_ARCH",           ATF_ARCH,           false, },
45    { "ATF_BUILD_CC",       ATF_BUILD_CC,       false, },
46    { "ATF_BUILD_CFLAGS",   ATF_BUILD_CFLAGS,   true,  },
47    { "ATF_BUILD_CPP",      ATF_BUILD_CPP,      false, },
48    { "ATF_BUILD_CPPFLAGS", ATF_BUILD_CPPFLAGS, true,  },
49    { "ATF_BUILD_CXX",      ATF_BUILD_CXX,      false, },
50    { "ATF_BUILD_CXXFLAGS", ATF_BUILD_CXXFLAGS, true,  },
51    { "ATF_CONFDIR",        ATF_CONFDIR,        false, },
52    { "ATF_INCLUDEDIR",     ATF_INCLUDEDIR,     false, },
53    { "ATF_LIBDIR",         ATF_LIBDIR,         false, },
54    { "ATF_LIBEXECDIR",     ATF_LIBEXECDIR,     false, },
55    { "ATF_MACHINE",        ATF_MACHINE,        false, },
56    { "ATF_PKGDATADIR",     ATF_PKGDATADIR,     false, },
57    { "ATF_SHELL",          ATF_SHELL,          false, },
58    { "ATF_WORKDIR",        ATF_WORKDIR,        false, },
59    { NULL,                 NULL,               false, },
60};
61
62//
63// Adds all predefined standard build-time variables to the m_variables
64// map, considering the values a user may have provided in the environment.
65//
66// Can only be called once during the program's lifetime.
67//
68static
69void
70init_variables(void)
71{
72    assert(m_variables.empty());
73
74    for (struct var* v = vars; v->name != NULL; v++) {
75        const std::string varname = tools::text::to_lower(v->name);
76
77        if (tools::env::has(v->name)) {
78            const std::string envval = tools::env::get(v->name);
79            if (envval.empty() && !v->can_be_empty)
80                m_variables[varname] = v->default_value;
81            else
82                m_variables[varname] = envval;
83        } else {
84            m_variables[varname] = v->default_value;
85        }
86    }
87
88    assert(!m_variables.empty());
89}
90
91const std::string&
92tools::config::get(const std::string& varname)
93{
94    if (m_variables.empty())
95        init_variables();
96
97    assert(has(varname));
98    return m_variables[varname];
99}
100
101const std::map< std::string, std::string >&
102tools::config::get_all(void)
103{
104    if (m_variables.empty())
105        init_variables();
106
107    return m_variables;
108}
109
110bool
111tools::config::has(const std::string& varname)
112{
113    if (m_variables.empty())
114        init_variables();
115
116    return m_variables.find(varname) != m_variables.end();
117}
118
119namespace tools {
120namespace config {
121//
122// Auxiliary function for the t_config test program so that it can
123// revert the configuration's global status to an empty state and
124// do new tests from there on.
125//
126// Ideally this shouldn't be part of the production library... but
127// this is so small that it does not matter.
128//
129void
130__reinit(void)
131{
132    m_variables.clear();
133}
134} // namespace config
135} // namespace tools
136