1 // Copyright (c) 2010 The NetBSD Foundation, Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions
7 // 1. Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // 2. Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
13 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "atf-c++/detail/application.hpp"
36 #include "atf-c++/detail/env.hpp"
37 #include "atf-c++/detail/fs.hpp"
38 #include "atf-c++/detail/sanity.hpp"
40 // ------------------------------------------------------------------------
41 // Auxiliary functions.
42 // ------------------------------------------------------------------------
48 fix_plain_name(const char *filename)
50 const atf::fs::path filepath(filename);
51 if (filepath.branch_path().str() == ".")
52 return std::string("./") + filename;
54 return std::string(filename);
59 construct_script(const char* filename)
61 const std::string libexecdir = atf::env::get(
62 "ATF_LIBEXECDIR", ATF_LIBEXECDIR);
63 const std::string pkgdatadir = atf::env::get(
64 "ATF_PKGDATADIR", ATF_PKGDATADIR);
65 const std::string shell = atf::env::get("ATF_SHELL", ATF_SHELL);
67 std::string* command = new std::string();
68 command->reserve(512);
69 (*command) += ("Atf_Check='" + libexecdir + "/atf-check' ; " +
70 "Atf_Shell='" + shell + "' ; " +
71 ". " + pkgdatadir + "/libatf-sh.subr ; " +
72 ". " + fix_plain_name(filename) + " ; " +
79 construct_argv(const std::string& shell, const int interpreter_argc,
80 const char* const* interpreter_argv)
82 PRE(interpreter_argc >= 1);
83 PRE(interpreter_argv[0] != NULL);
85 const std::string* script = construct_script(interpreter_argv[0]);
87 const int count = 4 + (interpreter_argc - 1) + 1;
88 const char** argv = new const char*[count];
89 argv[0] = shell.c_str();
91 argv[2] = script->c_str();
92 argv[3] = interpreter_argv[0];
94 for (int i = 1; i < interpreter_argc; i++)
95 argv[4 + i - 1] = interpreter_argv[i];
97 argv[count - 1] = NULL;
102 } // anonymous namespace
104 // ------------------------------------------------------------------------
105 // The "atf_sh" class.
106 // ------------------------------------------------------------------------
108 class atf_sh : public atf::application::app {
109 static const char* m_description;
111 atf::fs::path m_shell;
113 options_set specific_options(void) const;
114 void process_option(int, const char*);
122 const char* atf_sh::m_description =
123 "atf-sh is a shell interpreter that extends the functionality of the "
124 "system sh(1) with the atf-sh library.";
126 atf_sh::atf_sh(void) :
127 app(m_description, "atf-sh(1)"),
128 m_shell(atf::fs::path(atf::env::get("ATF_SHELL", ATF_SHELL)))
133 atf_sh::specific_options(void)
136 using atf::application::option;
139 INV(m_shell == atf::fs::path(atf::env::get("ATF_SHELL", ATF_SHELL)));
140 opts.insert(option('s', "shell", "Path to the shell interpreter to use; "
141 "default: " + m_shell.str()));
147 atf_sh::process_option(int ch, const char* arg)
151 m_shell = atf::fs::path(arg);
163 throw atf::application::usage_error("No test program provided");
165 const atf::fs::path script(m_argv[0]);
166 if (!atf::fs::exists(script))
167 throw std::runtime_error("The test program '" + script.str() + "' "
170 const char** argv = construct_argv(m_shell.str(), m_argc, m_argv);
171 // Don't bother keeping track of the memory allocated by construct_argv:
172 // we are going to exec or die immediately.
174 const int ret = execv(m_shell.c_str(), const_cast< char** >(argv));
176 std::cerr << "Failed to execute " << m_shell.str() << ": "
177 << std::strerror(errno) << "\n";
182 main(int argc, char* const* argv)
184 return atf_sh().run(argc, argv);