xref: /minix3/external/bsd/atf/dist/atf-sh/atf-sh.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
111be35a1SLionel Sambuc //
211be35a1SLionel Sambuc // Automated Testing Framework (atf)
311be35a1SLionel Sambuc //
411be35a1SLionel Sambuc // Copyright (c) 2010 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc // All rights reserved.
611be35a1SLionel Sambuc //
711be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc // modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc // are met:
1011be35a1SLionel Sambuc // 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc //    notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc // 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc //    notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc //    documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc //
1611be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
1711be35a1SLionel Sambuc // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1811be35a1SLionel Sambuc // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1911be35a1SLionel Sambuc // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2011be35a1SLionel Sambuc // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
2111be35a1SLionel Sambuc // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2211be35a1SLionel Sambuc // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2311be35a1SLionel Sambuc // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2411be35a1SLionel Sambuc // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2511be35a1SLionel Sambuc // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2611be35a1SLionel Sambuc // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2711be35a1SLionel Sambuc // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2811be35a1SLionel Sambuc //
2911be35a1SLionel Sambuc 
3011be35a1SLionel Sambuc extern "C" {
3111be35a1SLionel Sambuc #include <unistd.h>
3211be35a1SLionel Sambuc }
3311be35a1SLionel Sambuc 
3411be35a1SLionel Sambuc #include <cerrno>
3511be35a1SLionel Sambuc #include <cstdlib>
3611be35a1SLionel Sambuc #include <cstring>
3711be35a1SLionel Sambuc #include <iostream>
3811be35a1SLionel Sambuc 
3911be35a1SLionel Sambuc #include "atf-c++/config.hpp"
4011be35a1SLionel Sambuc 
4111be35a1SLionel Sambuc #include "atf-c++/detail/application.hpp"
4211be35a1SLionel Sambuc #include "atf-c++/detail/fs.hpp"
4311be35a1SLionel Sambuc #include "atf-c++/detail/sanity.hpp"
4411be35a1SLionel Sambuc 
4511be35a1SLionel Sambuc // ------------------------------------------------------------------------
4611be35a1SLionel Sambuc // Auxiliary functions.
4711be35a1SLionel Sambuc // ------------------------------------------------------------------------
4811be35a1SLionel Sambuc 
4911be35a1SLionel Sambuc namespace {
5011be35a1SLionel Sambuc 
5111be35a1SLionel Sambuc static
5211be35a1SLionel Sambuc std::string
fix_plain_name(const char * filename)5311be35a1SLionel Sambuc fix_plain_name(const char *filename)
5411be35a1SLionel Sambuc {
5511be35a1SLionel Sambuc     const atf::fs::path filepath(filename);
5611be35a1SLionel Sambuc     if (filepath.branch_path().str() == ".")
5711be35a1SLionel Sambuc         return std::string("./") + filename;
5811be35a1SLionel Sambuc     else
5911be35a1SLionel Sambuc         return std::string(filename);
6011be35a1SLionel Sambuc }
6111be35a1SLionel Sambuc 
6211be35a1SLionel Sambuc static
6311be35a1SLionel Sambuc std::string*
construct_script(const char * filename)6411be35a1SLionel Sambuc construct_script(const char* filename)
6511be35a1SLionel Sambuc {
6611be35a1SLionel Sambuc     const std::string libexecdir = atf::config::get("atf_libexecdir");
6711be35a1SLionel Sambuc     const std::string pkgdatadir = atf::config::get("atf_pkgdatadir");
6811be35a1SLionel Sambuc     const std::string shell = atf::config::get("atf_shell");
6911be35a1SLionel Sambuc 
7011be35a1SLionel Sambuc     std::string* command = new std::string();
7111be35a1SLionel Sambuc     command->reserve(512);
7211be35a1SLionel Sambuc     (*command) += ("Atf_Check='" + libexecdir + "/atf-check' ; " +
7311be35a1SLionel Sambuc                    "Atf_Shell='" + shell + "' ; " +
7411be35a1SLionel Sambuc                    ". " + pkgdatadir + "/libatf-sh.subr ; " +
7511be35a1SLionel Sambuc                    ". " + fix_plain_name(filename) + " ; " +
7611be35a1SLionel Sambuc                    "main \"${@}\"");
7711be35a1SLionel Sambuc     return command;
7811be35a1SLionel Sambuc }
7911be35a1SLionel Sambuc 
8011be35a1SLionel Sambuc static
8111be35a1SLionel Sambuc const char**
construct_argv(const std::string & shell,const int interpreter_argc,const char * const * interpreter_argv)8211be35a1SLionel Sambuc construct_argv(const std::string& shell, const int interpreter_argc,
8311be35a1SLionel Sambuc                const char* const* interpreter_argv)
8411be35a1SLionel Sambuc {
8511be35a1SLionel Sambuc     PRE(interpreter_argc >= 1);
8611be35a1SLionel Sambuc     PRE(interpreter_argv[0] != NULL);
8711be35a1SLionel Sambuc 
8811be35a1SLionel Sambuc     const std::string* script = construct_script(interpreter_argv[0]);
8911be35a1SLionel Sambuc 
9011be35a1SLionel Sambuc     const int count = 4 + (interpreter_argc - 1) + 1;
9111be35a1SLionel Sambuc     const char** argv = new const char*[count];
9211be35a1SLionel Sambuc     argv[0] = shell.c_str();
9311be35a1SLionel Sambuc     argv[1] = "-c";
9411be35a1SLionel Sambuc     argv[2] = script->c_str();
9511be35a1SLionel Sambuc     argv[3] = interpreter_argv[0];
9611be35a1SLionel Sambuc 
9711be35a1SLionel Sambuc     for (int i = 1; i < interpreter_argc; i++)
9811be35a1SLionel Sambuc         argv[4 + i - 1] = interpreter_argv[i];
9911be35a1SLionel Sambuc 
10011be35a1SLionel Sambuc     argv[count - 1] = NULL;
10111be35a1SLionel Sambuc 
10211be35a1SLionel Sambuc     return argv;
10311be35a1SLionel Sambuc }
10411be35a1SLionel Sambuc 
10511be35a1SLionel Sambuc } // anonymous namespace
10611be35a1SLionel Sambuc 
10711be35a1SLionel Sambuc // ------------------------------------------------------------------------
10811be35a1SLionel Sambuc // The "atf_sh" class.
10911be35a1SLionel Sambuc // ------------------------------------------------------------------------
11011be35a1SLionel Sambuc 
11111be35a1SLionel Sambuc class atf_sh : public atf::application::app {
11211be35a1SLionel Sambuc     static const char* m_description;
11311be35a1SLionel Sambuc 
11411be35a1SLionel Sambuc public:
11511be35a1SLionel Sambuc     atf_sh(void);
11611be35a1SLionel Sambuc 
11711be35a1SLionel Sambuc     int main(void);
11811be35a1SLionel Sambuc };
11911be35a1SLionel Sambuc 
12011be35a1SLionel Sambuc const char* atf_sh::m_description =
12111be35a1SLionel Sambuc     "atf-sh is a shell interpreter that extends the functionality of the "
12211be35a1SLionel Sambuc     "system sh(1) with the atf-sh library.";
12311be35a1SLionel Sambuc 
atf_sh(void)12411be35a1SLionel Sambuc atf_sh::atf_sh(void) :
125*0a6a1f1dSLionel Sambuc     app(m_description, "atf-sh(1)")
12611be35a1SLionel Sambuc {
12711be35a1SLionel Sambuc }
12811be35a1SLionel Sambuc 
12911be35a1SLionel Sambuc int
main(void)13011be35a1SLionel Sambuc atf_sh::main(void)
13111be35a1SLionel Sambuc {
13211be35a1SLionel Sambuc     if (m_argc < 1)
13311be35a1SLionel Sambuc         throw atf::application::usage_error("No test program provided");
13411be35a1SLionel Sambuc 
13511be35a1SLionel Sambuc     const atf::fs::path script(m_argv[0]);
13611be35a1SLionel Sambuc     if (!atf::fs::exists(script))
13711be35a1SLionel Sambuc         throw std::runtime_error("The test program '" + script.str() + "' "
13811be35a1SLionel Sambuc                                  "does not exist");
13911be35a1SLionel Sambuc 
14011be35a1SLionel Sambuc     const std::string shell = atf::config::get("atf_shell");
14111be35a1SLionel Sambuc     const char** argv = construct_argv(shell, m_argc, m_argv);
14211be35a1SLionel Sambuc     // Don't bother keeping track of the memory allocated by construct_argv:
14311be35a1SLionel Sambuc     // we are going to exec or die immediately.
14411be35a1SLionel Sambuc 
1453260d16fSLionel Sambuc #if defined(__minix) && !defined(NDEBUG)
1463260d16fSLionel Sambuc     const int ret =
1473260d16fSLionel Sambuc #endif /* defined(__minix) && !defined(NDEBUG) */
1483260d16fSLionel Sambuc     execv(shell.c_str(), const_cast< char** >(argv));
14911be35a1SLionel Sambuc     INV(ret == -1);
15011be35a1SLionel Sambuc     std::cerr << "Failed to execute " << shell << ": " << std::strerror(errno)
15111be35a1SLionel Sambuc               << "\n";
15211be35a1SLionel Sambuc     return EXIT_FAILURE;
15311be35a1SLionel Sambuc }
15411be35a1SLionel Sambuc 
15511be35a1SLionel Sambuc int
main(int argc,char * const * argv)15611be35a1SLionel Sambuc main(int argc, char* const* argv)
15711be35a1SLionel Sambuc {
15811be35a1SLionel Sambuc     return atf_sh().run(argc, argv);
15911be35a1SLionel Sambuc }
160