xref: /minix3/external/bsd/atf/dist/tools/atf-config.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc //
2*0a6a1f1dSLionel Sambuc // Automated Testing Framework (atf)
3*0a6a1f1dSLionel Sambuc //
4*0a6a1f1dSLionel Sambuc // Copyright (c) 2007 The NetBSD Foundation, Inc.
5*0a6a1f1dSLionel Sambuc // All rights reserved.
6*0a6a1f1dSLionel Sambuc //
7*0a6a1f1dSLionel Sambuc // Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc // modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc // are met:
10*0a6a1f1dSLionel Sambuc // 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc //    notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc // 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc //    notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc //    documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc //
16*0a6a1f1dSLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*0a6a1f1dSLionel Sambuc // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*0a6a1f1dSLionel Sambuc // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*0a6a1f1dSLionel Sambuc // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*0a6a1f1dSLionel Sambuc // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*0a6a1f1dSLionel Sambuc // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*0a6a1f1dSLionel Sambuc // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*0a6a1f1dSLionel Sambuc // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*0a6a1f1dSLionel Sambuc // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*0a6a1f1dSLionel Sambuc // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*0a6a1f1dSLionel Sambuc // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*0a6a1f1dSLionel Sambuc // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*0a6a1f1dSLionel Sambuc //
29*0a6a1f1dSLionel Sambuc 
30*0a6a1f1dSLionel Sambuc #include <cstdlib>
31*0a6a1f1dSLionel Sambuc #include <iostream>
32*0a6a1f1dSLionel Sambuc #include <map>
33*0a6a1f1dSLionel Sambuc #include <string>
34*0a6a1f1dSLionel Sambuc 
35*0a6a1f1dSLionel Sambuc #include "application.hpp"
36*0a6a1f1dSLionel Sambuc #include "config.hpp"
37*0a6a1f1dSLionel Sambuc 
38*0a6a1f1dSLionel Sambuc class atf_config : public tools::application::app {
39*0a6a1f1dSLionel Sambuc     static const char* m_description;
40*0a6a1f1dSLionel Sambuc 
41*0a6a1f1dSLionel Sambuc     bool m_tflag;
42*0a6a1f1dSLionel Sambuc 
43*0a6a1f1dSLionel Sambuc     void process_option(int, const char*);
44*0a6a1f1dSLionel Sambuc     std::string specific_args(void) const;
45*0a6a1f1dSLionel Sambuc     options_set specific_options(void) const;
46*0a6a1f1dSLionel Sambuc 
47*0a6a1f1dSLionel Sambuc     std::string format_var(const std::string&, const std::string&);
48*0a6a1f1dSLionel Sambuc 
49*0a6a1f1dSLionel Sambuc public:
50*0a6a1f1dSLionel Sambuc     atf_config(void);
51*0a6a1f1dSLionel Sambuc 
52*0a6a1f1dSLionel Sambuc     int main(void);
53*0a6a1f1dSLionel Sambuc };
54*0a6a1f1dSLionel Sambuc 
55*0a6a1f1dSLionel Sambuc const char* atf_config::m_description =
56*0a6a1f1dSLionel Sambuc     "atf-config is a tool that queries the value of several "
57*0a6a1f1dSLionel Sambuc     "installation-specific configuration values of the atf.  "
58*0a6a1f1dSLionel Sambuc     "It can be used by external tools to discover where specific "
59*0a6a1f1dSLionel Sambuc     "internal atf files are installed.";
60*0a6a1f1dSLionel Sambuc 
atf_config(void)61*0a6a1f1dSLionel Sambuc atf_config::atf_config(void) :
62*0a6a1f1dSLionel Sambuc     app(m_description, "atf-config(1)", "atf(7)"),
63*0a6a1f1dSLionel Sambuc     m_tflag(false)
64*0a6a1f1dSLionel Sambuc {
65*0a6a1f1dSLionel Sambuc }
66*0a6a1f1dSLionel Sambuc 
67*0a6a1f1dSLionel Sambuc void
process_option(int ch,const char * arg)68*0a6a1f1dSLionel Sambuc atf_config::process_option(int ch, const char* arg __attribute__((__unused__)))
69*0a6a1f1dSLionel Sambuc {
70*0a6a1f1dSLionel Sambuc     switch (ch) {
71*0a6a1f1dSLionel Sambuc     case 't':
72*0a6a1f1dSLionel Sambuc         m_tflag = true;
73*0a6a1f1dSLionel Sambuc         break;
74*0a6a1f1dSLionel Sambuc 
75*0a6a1f1dSLionel Sambuc     default:
76*0a6a1f1dSLionel Sambuc         std::abort();
77*0a6a1f1dSLionel Sambuc     }
78*0a6a1f1dSLionel Sambuc }
79*0a6a1f1dSLionel Sambuc 
80*0a6a1f1dSLionel Sambuc std::string
specific_args(void) const81*0a6a1f1dSLionel Sambuc atf_config::specific_args(void)
82*0a6a1f1dSLionel Sambuc     const
83*0a6a1f1dSLionel Sambuc {
84*0a6a1f1dSLionel Sambuc     return "[var1 [.. varN]]";
85*0a6a1f1dSLionel Sambuc }
86*0a6a1f1dSLionel Sambuc 
87*0a6a1f1dSLionel Sambuc atf_config::options_set
specific_options(void) const88*0a6a1f1dSLionel Sambuc atf_config::specific_options(void)
89*0a6a1f1dSLionel Sambuc     const
90*0a6a1f1dSLionel Sambuc {
91*0a6a1f1dSLionel Sambuc     using tools::application::option;
92*0a6a1f1dSLionel Sambuc     options_set opts;
93*0a6a1f1dSLionel Sambuc     opts.insert(option('t', "", "Terse output: show values only"));
94*0a6a1f1dSLionel Sambuc     return opts;
95*0a6a1f1dSLionel Sambuc }
96*0a6a1f1dSLionel Sambuc 
97*0a6a1f1dSLionel Sambuc std::string
format_var(const std::string & name,const std::string & val)98*0a6a1f1dSLionel Sambuc atf_config::format_var(const std::string& name, const std::string& val)
99*0a6a1f1dSLionel Sambuc {
100*0a6a1f1dSLionel Sambuc     std::string str;
101*0a6a1f1dSLionel Sambuc 
102*0a6a1f1dSLionel Sambuc     if (m_tflag)
103*0a6a1f1dSLionel Sambuc         str = val;
104*0a6a1f1dSLionel Sambuc     else
105*0a6a1f1dSLionel Sambuc         str = name + " : " + val;
106*0a6a1f1dSLionel Sambuc 
107*0a6a1f1dSLionel Sambuc     return str;
108*0a6a1f1dSLionel Sambuc }
109*0a6a1f1dSLionel Sambuc 
110*0a6a1f1dSLionel Sambuc int
main(void)111*0a6a1f1dSLionel Sambuc atf_config::main(void)
112*0a6a1f1dSLionel Sambuc {
113*0a6a1f1dSLionel Sambuc     if (m_argc < 1) {
114*0a6a1f1dSLionel Sambuc         std::map< std::string, std::string > cv = tools::config::get_all();
115*0a6a1f1dSLionel Sambuc 
116*0a6a1f1dSLionel Sambuc         for (std::map< std::string, std::string >::const_iterator iter =
117*0a6a1f1dSLionel Sambuc              cv.begin(); iter != cv.end(); iter++)
118*0a6a1f1dSLionel Sambuc             std::cout << format_var((*iter).first, (*iter).second) << "\n";
119*0a6a1f1dSLionel Sambuc     } else {
120*0a6a1f1dSLionel Sambuc         for (int i = 0; i < m_argc; i++) {
121*0a6a1f1dSLionel Sambuc             if (!tools::config::has(m_argv[i]))
122*0a6a1f1dSLionel Sambuc                 throw std::runtime_error(std::string("Unknown variable `") +
123*0a6a1f1dSLionel Sambuc                                          m_argv[i] + "'");
124*0a6a1f1dSLionel Sambuc         }
125*0a6a1f1dSLionel Sambuc 
126*0a6a1f1dSLionel Sambuc         for (int i = 0; i < m_argc; i++) {
127*0a6a1f1dSLionel Sambuc             std::cout << format_var(m_argv[i], tools::config::get(m_argv[i]))
128*0a6a1f1dSLionel Sambuc                       << "\n";
129*0a6a1f1dSLionel Sambuc         }
130*0a6a1f1dSLionel Sambuc     }
131*0a6a1f1dSLionel Sambuc 
132*0a6a1f1dSLionel Sambuc     return EXIT_SUCCESS;
133*0a6a1f1dSLionel Sambuc }
134*0a6a1f1dSLionel Sambuc 
135*0a6a1f1dSLionel Sambuc int
main(int argc,char * const * argv)136*0a6a1f1dSLionel Sambuc main(int argc, char* const* argv)
137*0a6a1f1dSLionel Sambuc {
138*0a6a1f1dSLionel Sambuc     return atf_config().run(argc, argv);
139*0a6a1f1dSLionel Sambuc }
140