1 1.1 jmmv // Copyright 2010 Google Inc. 2 1.1 jmmv // All rights reserved. 3 1.1 jmmv // 4 1.1 jmmv // Redistribution and use in source and binary forms, with or without 5 1.1 jmmv // modification, are permitted provided that the following conditions are 6 1.1 jmmv // met: 7 1.1 jmmv // 8 1.1 jmmv // * Redistributions of source code must retain the above copyright 9 1.1 jmmv // notice, this list of conditions and the following disclaimer. 10 1.1 jmmv // * Redistributions in binary form must reproduce the above copyright 11 1.1 jmmv // notice, this list of conditions and the following disclaimer in the 12 1.1 jmmv // documentation and/or other materials provided with the distribution. 13 1.1 jmmv // * Neither the name of Google Inc. nor the names of its contributors 14 1.1 jmmv // may be used to endorse or promote products derived from this software 15 1.1 jmmv // without specific prior written permission. 16 1.1 jmmv // 17 1.1 jmmv // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 1.1 jmmv // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 1.1 jmmv // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 1.1 jmmv // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 1.1 jmmv // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 1.1 jmmv // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 1.1 jmmv // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 1.1 jmmv // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 1.1 jmmv // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 1.1 jmmv // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 1.1 jmmv // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 1.1 jmmv 29 1.1 jmmv #include <cstdlib> 30 1.1 jmmv #include <cstring> 31 1.1 jmmv #include <iostream> 32 1.1 jmmv 33 1.1 jmmv #include "utils/defs.hpp" 34 1.1 jmmv 35 1.1 jmmv 36 1.1 jmmv namespace { 37 1.1 jmmv 38 1.1 jmmv 39 1.1 jmmv /// Prints a fake but valid test case list and then aborts. 40 1.1 jmmv /// 41 1.1 jmmv /// \param unused_argc The original argument count of the program. 42 1.1 jmmv /// \param argv The original arguments of the program. 43 1.1 jmmv /// 44 1.1 jmmv /// \return Nothing because this dies before returning. 45 1.1 jmmv static int 46 1.1 jmmv helper_abort_test_cases_list(int UTILS_UNUSED_PARAM(argc), char** argv) 47 1.1 jmmv { 48 1.1 jmmv for (const char* const* arg = argv; *arg != NULL; arg++) { 49 1.1 jmmv if (std::strcmp(*arg, "-l") == 0) { 50 1.1 jmmv std::cout << "Content-Type: application/X-atf-tp; " 51 1.1 jmmv "version=\"1\"\n\n"; 52 1.1 jmmv std::cout << "ident: foo\n"; 53 1.1 jmmv } 54 1.1 jmmv } 55 1.1 jmmv std::abort(); 56 1.1 jmmv } 57 1.1 jmmv 58 1.1 jmmv 59 1.1 jmmv /// Just returns without printing anything as the test case list. 60 1.1 jmmv /// 61 1.1 jmmv /// \param unused_argc The original argument count of the program. 62 1.1 jmmv /// \param unused_argv The original arguments of the program. 63 1.1 jmmv /// 64 1.1 jmmv /// \return Always 0, as required for test programs. 65 1.1 jmmv static int 66 1.1 jmmv helper_empty_test_cases_list(int UTILS_UNUSED_PARAM(argc), 67 1.1 jmmv char** UTILS_UNUSED_PARAM(argv)) 68 1.1 jmmv { 69 1.1 jmmv return EXIT_SUCCESS; 70 1.1 jmmv } 71 1.1 jmmv 72 1.1 jmmv 73 1.1 jmmv /// Prints a correctly-formatted test case list but empty. 74 1.1 jmmv /// 75 1.1 jmmv /// \param unused_argc The original argument count of the program. 76 1.1 jmmv /// \param argv The original arguments of the program. 77 1.1 jmmv /// 78 1.1 jmmv /// \return Always 0, as required for test programs. 79 1.1 jmmv static int 80 1.1 jmmv helper_zero_test_cases(int UTILS_UNUSED_PARAM(argc), char** argv) 81 1.1 jmmv { 82 1.1 jmmv for (const char* const* arg = argv; *arg != NULL; arg++) { 83 1.1 jmmv if (std::strcmp(*arg, "-l") == 0) 84 1.1 jmmv std::cout << "Content-Type: application/X-atf-tp; " 85 1.1 jmmv "version=\"1\"\n\n"; 86 1.1 jmmv } 87 1.1 jmmv return EXIT_SUCCESS; 88 1.1 jmmv } 89 1.1 jmmv 90 1.1 jmmv 91 1.1 jmmv /// Mapping of the name of a helper to its implementation. 92 1.1 jmmv struct helper { 93 1.1 jmmv /// The name of the helper, as will be provided by the user on the CLI. 94 1.1 jmmv const char* name; 95 1.1 jmmv 96 1.1 jmmv /// A pointer to the function implementing the helper. 97 1.1 jmmv int (*hook)(int, char**); 98 1.1 jmmv }; 99 1.1 jmmv 100 1.1 jmmv 101 1.1 jmmv /// NULL-terminated table mapping helper names to their implementations. 102 1.1 jmmv static const helper helpers[] = { 103 1.1 jmmv { "abort_test_cases_list", helper_abort_test_cases_list, }, 104 1.1 jmmv { "empty_test_cases_list", helper_empty_test_cases_list, }, 105 1.1 jmmv { "zero_test_cases", helper_zero_test_cases, }, 106 1.1 jmmv { NULL, NULL, }, 107 1.1 jmmv }; 108 1.1 jmmv 109 1.1 jmmv 110 1.1 jmmv } // anonymous namespace 111 1.1 jmmv 112 1.1 jmmv 113 1.1 jmmv /// Entry point to the ATF-less helpers. 114 1.1 jmmv /// 115 1.1 jmmv /// The caller must select a helper to execute by defining the HELPER 116 1.1 jmmv /// environment variable to the name of the desired helper. Think of this main 117 1.1 jmmv /// method as a subprogram dispatcher, to avoid having many individual helper 118 1.1 jmmv /// binaries. 119 1.1 jmmv /// 120 1.1 jmmv /// \todo Maybe we should really have individual helper binaries. It would 121 1.1 jmmv /// avoid a significant amount of complexity here and in the tests, at the 122 1.1 jmmv /// expense of some extra files and extra build logic. 123 1.1 jmmv /// 124 1.1 jmmv /// \param argc The user argument count; delegated to the helper. 125 1.1 jmmv /// \param argv The user arguments; delegated to the helper. 126 1.1 jmmv /// 127 1.1 jmmv /// \return The exit code of the helper, which depends on the requested helper. 128 1.1 jmmv int 129 1.1 jmmv main(int argc, char** argv) 130 1.1 jmmv { 131 1.1 jmmv const char* command = std::getenv("HELPER"); 132 1.1 jmmv if (command == NULL) { 133 1.1 jmmv std::cerr << "Usage error: HELPER must be set to a helper name\n"; 134 1.1 jmmv std::exit(EXIT_FAILURE); 135 1.1 jmmv } 136 1.1 jmmv 137 1.1 jmmv const struct helper* iter = helpers; 138 1.1 jmmv for (; iter->name != NULL && std::strcmp(iter->name, command) != 0; iter++) 139 1.1 jmmv ; 140 1.1 jmmv if (iter->name == NULL) { 141 1.1 jmmv std::cerr << "Usage error: unknown command " << command << "\n"; 142 1.1 jmmv std::exit(EXIT_FAILURE); 143 1.1 jmmv } 144 1.1 jmmv 145 1.1 jmmv return iter->hook(argc, argv); 146 1.1 jmmv } 147