2 // Automated Testing Framework (atf)
4 // Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
10 // 1. Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/types.h>
39 #include "atf-c++/application.hpp"
40 #include "atf-c++/config.hpp"
41 #include "atf-c++/sanity.hpp"
45 cat_file(std::ostream
& os
, const std::string
& path
)
47 std::ifstream
is(path
.c_str());
49 throw std::runtime_error("Cannot open " + path
);
51 while (std::getline(is
, line
))
52 os
<< line
<< std::endl
;
56 class atf_compile
: public atf::application::app
{
57 static const char* m_description
;
59 std::string m_outfile
;
61 std::string
specific_args(void) const;
62 options_set
specific_options(void) const;
63 void process_option(int ch
, const char*);
65 void compile(std::ostream
&);
73 const char* atf_compile::m_description
=
74 "atf-compile is a tool that ``compiles'' test cases written in the "
75 "POSIX shell language, generating an executable.";
77 atf_compile::atf_compile(void) :
78 app(m_description
, "atf-compile(1)", "atf(7)")
83 atf_compile::specific_args(void)
86 return "<test-program>";
89 atf_compile::options_set
90 atf_compile::specific_options(void)
93 using atf::application::option
;
95 opts
.insert(option('o', "out-file", "Name of the output file"));
100 atf_compile::process_option(int ch
, const char* arg
)
113 atf_compile::compile(std::ostream
& os
)
115 os
<< "#! " << atf::config::get("atf_shell") << std::endl
;
116 cat_file(os
, atf::config::get("atf_pkgdatadir") + "/atf.init.subr");
118 os
<< ". ${Atf_Pkgdatadir}/atf.header.subr" << std::endl
;
120 for (int i
= 0; i
< m_argc
; i
++) {
121 cat_file(os
, m_argv
[i
]);
124 os
<< ". ${Atf_Pkgdatadir}/atf.footer.subr" << std::endl
;
126 os
<< "main \"${@}\"" << std::endl
;
130 atf_compile::main(void)
133 throw atf::application::usage_error("No test program specified");
135 if (m_outfile
.empty())
136 throw atf::application::usage_error("No output file specified");
138 std::ofstream
os(m_outfile
.c_str());
140 throw std::runtime_error("Cannot open output file `" +
145 mode_t umask
= ::umask(S_IRWXU
| S_IRWXG
| S_IRWXO
);
148 if (::chmod(m_outfile
.c_str(),
149 (S_IRWXU
| S_IRWXG
| S_IRWXO
) & ~umask
) == -1)
150 throw std::runtime_error("Cannot set executable permissions "
151 "on `" + m_outfile
+ "'");
157 main(int argc
, char* const* argv
)
159 return atf_compile().run(argc
, argv
);