Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / atf / dist / tools / atf-compile.cpp
blobf37605dc4a299ae6c9205f6119fca7705d419801
1 //
2 // Automated Testing Framework (atf)
3 //
4 // Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
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.
30 extern "C" {
31 #include <sys/types.h>
32 #include <sys/stat.h>
35 #include <cstdlib>
36 #include <fstream>
37 #include <iostream>
39 #include "atf-c++/application.hpp"
40 #include "atf-c++/config.hpp"
41 #include "atf-c++/sanity.hpp"
43 static
44 void
45 cat_file(std::ostream& os, const std::string& path)
47 std::ifstream is(path.c_str());
48 if (!is)
49 throw std::runtime_error("Cannot open " + path);
50 std::string line;
51 while (std::getline(is, line))
52 os << line << std::endl;
53 is.close();
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&);
67 public:
68 atf_compile(void);
70 int main(void);
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)")
82 std::string
83 atf_compile::specific_args(void)
84 const
86 return "<test-program>";
89 atf_compile::options_set
90 atf_compile::specific_options(void)
91 const
93 using atf::application::option;
94 options_set opts;
95 opts.insert(option('o', "out-file", "Name of the output file"));
96 return opts;
99 void
100 atf_compile::process_option(int ch, const char* arg)
102 switch (ch) {
103 case 'o':
104 m_outfile = arg;
105 break;
107 default:
108 UNREACHABLE;
112 void
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");
117 os << std::endl;
118 os << ". ${Atf_Pkgdatadir}/atf.header.subr" << std::endl;
119 os << std::endl;
120 for (int i = 0; i < m_argc; i++) {
121 cat_file(os, m_argv[i]);
122 os << std::endl;
124 os << ". ${Atf_Pkgdatadir}/atf.footer.subr" << std::endl;
125 os << std::endl;
126 os << "main \"${@}\"" << std::endl;
130 atf_compile::main(void)
132 if (m_argc < 1)
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());
139 if (!os)
140 throw std::runtime_error("Cannot open output file `" +
141 m_outfile + "'");
142 compile(os);
143 os.close();
145 mode_t umask = ::umask(S_IRWXU | S_IRWXG | S_IRWXO);
146 ::umask(umask);
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 + "'");
153 return EXIT_SUCCESS;
157 main(int argc, char* const* argv)
159 return atf_compile().run(argc, argv);