xref: /minix3/external/bsd/kyua-cli/dist/bootstrap/plain_helpers.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 <cstdlib>
30*11be35a1SLionel Sambuc #include <cstring>
31*11be35a1SLionel Sambuc #include <iostream>
32*11be35a1SLionel Sambuc 
33*11be35a1SLionel Sambuc #include "utils/defs.hpp"
34*11be35a1SLionel Sambuc 
35*11be35a1SLionel Sambuc 
36*11be35a1SLionel Sambuc namespace {
37*11be35a1SLionel Sambuc 
38*11be35a1SLionel Sambuc 
39*11be35a1SLionel Sambuc /// Prints a fake but valid test case list and then aborts.
40*11be35a1SLionel Sambuc ///
41*11be35a1SLionel Sambuc /// \param unused_argc The original argument count of the program.
42*11be35a1SLionel Sambuc /// \param argv The original arguments of the program.
43*11be35a1SLionel Sambuc ///
44*11be35a1SLionel Sambuc /// \return Nothing because this dies before returning.
45*11be35a1SLionel Sambuc static int
helper_abort_test_cases_list(int UTILS_UNUSED_PARAM (argc),char ** argv)46*11be35a1SLionel Sambuc helper_abort_test_cases_list(int UTILS_UNUSED_PARAM(argc), char** argv)
47*11be35a1SLionel Sambuc {
48*11be35a1SLionel Sambuc     for (const char* const* arg = argv; *arg != NULL; arg++) {
49*11be35a1SLionel Sambuc         if (std::strcmp(*arg, "-l") == 0) {
50*11be35a1SLionel Sambuc             std::cout << "Content-Type: application/X-atf-tp; "
51*11be35a1SLionel Sambuc                 "version=\"1\"\n\n";
52*11be35a1SLionel Sambuc             std::cout << "ident: foo\n";
53*11be35a1SLionel Sambuc         }
54*11be35a1SLionel Sambuc     }
55*11be35a1SLionel Sambuc     std::abort();
56*11be35a1SLionel Sambuc }
57*11be35a1SLionel Sambuc 
58*11be35a1SLionel Sambuc 
59*11be35a1SLionel Sambuc /// Just returns without printing anything as the test case list.
60*11be35a1SLionel Sambuc ///
61*11be35a1SLionel Sambuc /// \param unused_argc The original argument count of the program.
62*11be35a1SLionel Sambuc /// \param unused_argv The original arguments of the program.
63*11be35a1SLionel Sambuc ///
64*11be35a1SLionel Sambuc /// \return Always 0, as required for test programs.
65*11be35a1SLionel Sambuc static int
helper_empty_test_cases_list(int UTILS_UNUSED_PARAM (argc),char ** UTILS_UNUSED_PARAM (argv))66*11be35a1SLionel Sambuc helper_empty_test_cases_list(int UTILS_UNUSED_PARAM(argc),
67*11be35a1SLionel Sambuc                              char** UTILS_UNUSED_PARAM(argv))
68*11be35a1SLionel Sambuc {
69*11be35a1SLionel Sambuc     return EXIT_SUCCESS;
70*11be35a1SLionel Sambuc }
71*11be35a1SLionel Sambuc 
72*11be35a1SLionel Sambuc 
73*11be35a1SLionel Sambuc /// Prints a correctly-formatted test case list but empty.
74*11be35a1SLionel Sambuc ///
75*11be35a1SLionel Sambuc /// \param unused_argc The original argument count of the program.
76*11be35a1SLionel Sambuc /// \param argv The original arguments of the program.
77*11be35a1SLionel Sambuc ///
78*11be35a1SLionel Sambuc /// \return Always 0, as required for test programs.
79*11be35a1SLionel Sambuc static int
helper_zero_test_cases(int UTILS_UNUSED_PARAM (argc),char ** argv)80*11be35a1SLionel Sambuc helper_zero_test_cases(int UTILS_UNUSED_PARAM(argc), char** argv)
81*11be35a1SLionel Sambuc {
82*11be35a1SLionel Sambuc     for (const char* const* arg = argv; *arg != NULL; arg++) {
83*11be35a1SLionel Sambuc         if (std::strcmp(*arg, "-l") == 0)
84*11be35a1SLionel Sambuc             std::cout << "Content-Type: application/X-atf-tp; "
85*11be35a1SLionel Sambuc                 "version=\"1\"\n\n";
86*11be35a1SLionel Sambuc     }
87*11be35a1SLionel Sambuc     return EXIT_SUCCESS;
88*11be35a1SLionel Sambuc }
89*11be35a1SLionel Sambuc 
90*11be35a1SLionel Sambuc 
91*11be35a1SLionel Sambuc /// Mapping of the name of a helper to its implementation.
92*11be35a1SLionel Sambuc struct helper {
93*11be35a1SLionel Sambuc     /// The name of the helper, as will be provided by the user on the CLI.
94*11be35a1SLionel Sambuc     const char* name;
95*11be35a1SLionel Sambuc 
96*11be35a1SLionel Sambuc     /// A pointer to the function implementing the helper.
97*11be35a1SLionel Sambuc     int (*hook)(int, char**);
98*11be35a1SLionel Sambuc };
99*11be35a1SLionel Sambuc 
100*11be35a1SLionel Sambuc 
101*11be35a1SLionel Sambuc /// NULL-terminated table mapping helper names to their implementations.
102*11be35a1SLionel Sambuc static const helper helpers[] = {
103*11be35a1SLionel Sambuc     { "abort_test_cases_list", helper_abort_test_cases_list, },
104*11be35a1SLionel Sambuc     { "empty_test_cases_list", helper_empty_test_cases_list, },
105*11be35a1SLionel Sambuc     { "zero_test_cases", helper_zero_test_cases, },
106*11be35a1SLionel Sambuc     { NULL, NULL, },
107*11be35a1SLionel Sambuc };
108*11be35a1SLionel Sambuc 
109*11be35a1SLionel Sambuc 
110*11be35a1SLionel Sambuc }  // anonymous namespace
111*11be35a1SLionel Sambuc 
112*11be35a1SLionel Sambuc 
113*11be35a1SLionel Sambuc /// Entry point to the ATF-less helpers.
114*11be35a1SLionel Sambuc ///
115*11be35a1SLionel Sambuc /// The caller must select a helper to execute by defining the HELPER
116*11be35a1SLionel Sambuc /// environment variable to the name of the desired helper.  Think of this main
117*11be35a1SLionel Sambuc /// method as a subprogram dispatcher, to avoid having many individual helper
118*11be35a1SLionel Sambuc /// binaries.
119*11be35a1SLionel Sambuc ///
120*11be35a1SLionel Sambuc /// \todo Maybe we should really have individual helper binaries.  It would
121*11be35a1SLionel Sambuc /// avoid a significant amount of complexity here and in the tests, at the
122*11be35a1SLionel Sambuc /// expense of some extra files and extra build logic.
123*11be35a1SLionel Sambuc ///
124*11be35a1SLionel Sambuc /// \param argc The user argument count; delegated to the helper.
125*11be35a1SLionel Sambuc /// \param argv The user arguments; delegated to the helper.
126*11be35a1SLionel Sambuc ///
127*11be35a1SLionel Sambuc /// \return The exit code of the helper, which depends on the requested helper.
128*11be35a1SLionel Sambuc int
main(int argc,char ** argv)129*11be35a1SLionel Sambuc main(int argc, char** argv)
130*11be35a1SLionel Sambuc {
131*11be35a1SLionel Sambuc     const char* command = std::getenv("HELPER");
132*11be35a1SLionel Sambuc     if (command == NULL) {
133*11be35a1SLionel Sambuc         std::cerr << "Usage error: HELPER must be set to a helper name\n";
134*11be35a1SLionel Sambuc         std::exit(EXIT_FAILURE);
135*11be35a1SLionel Sambuc     }
136*11be35a1SLionel Sambuc 
137*11be35a1SLionel Sambuc     const struct helper* iter = helpers;
138*11be35a1SLionel Sambuc     for (; iter->name != NULL && std::strcmp(iter->name, command) != 0; iter++)
139*11be35a1SLionel Sambuc         ;
140*11be35a1SLionel Sambuc     if (iter->name == NULL) {
141*11be35a1SLionel Sambuc         std::cerr << "Usage error: unknown command " << command << "\n";
142*11be35a1SLionel Sambuc         std::exit(EXIT_FAILURE);
143*11be35a1SLionel Sambuc     }
144*11be35a1SLionel Sambuc 
145*11be35a1SLionel Sambuc     return iter->hook(argc, argv);
146*11be35a1SLionel Sambuc }
147