1 // Copyright (c) 2007 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.
26 #include "atf-c++/detail/application.hpp"
28 #if defined(HAVE_CONFIG_H)
43 #include "atf-c/defs.h"
46 #include "atf-c++/detail/sanity.hpp"
48 #if !defined(HAVE_VSNPRINTF_IN_STD)
52 #endif // !defined(HAVE_VSNPRINTF_IN_STD)
54 namespace impl = atf::application;
55 #define IMPL_NAME "atf::application"
57 // ------------------------------------------------------------------------
58 // The "usage_error" class.
59 // ------------------------------------------------------------------------
61 impl::usage_error::usage_error(const char *fmt, ...)
63 std::runtime_error("usage_error; message unformatted")
68 std::vsnprintf(m_text, sizeof(m_text), fmt, ap);
72 impl::usage_error::~usage_error(void)
78 impl::usage_error::what(void)
84 // ------------------------------------------------------------------------
85 // The "application" class.
86 // ------------------------------------------------------------------------
88 impl::option::option(char ch,
90 const std::string& desc) :
98 impl::option::operator<(const impl::option& o)
101 return m_character < o.m_character;
104 impl::app::app(const std::string& description,
105 const std::string& manpage) :
109 m_description(description),
114 impl::app::~app(void)
119 impl::app::inited(void)
124 impl::app::options_set
125 impl::app::options(void)
127 return specific_options();
131 impl::app::specific_args(void)
137 impl::app::options_set
138 impl::app::specific_options(void)
141 return options_set();
145 impl::app::process_option(int ch ATF_DEFS_ATTRIBUTE_UNUSED,
146 const char* arg ATF_DEFS_ATTRIBUTE_UNUSED)
151 impl::app::process_options(void)
156 #if defined(HAVE_GNU_GETOPT)
157 optstr += '+'; // Turn on POSIX behavior.
161 options_set opts = options();
162 for (options_set::const_iterator iter = opts.begin();
163 iter != opts.end(); iter++) {
164 const option& opt = (*iter);
166 optstr += opt.m_character;
167 if (!opt.m_argument.empty())
173 const int old_opterr = ::opterr;
175 while ((ch = ::getopt(m_argc, m_argv, optstr.c_str())) != -1) {
178 throw usage_error("Option -%c requires an argument.",
182 throw usage_error("Unknown option -%c.", ::optopt);
185 process_option(ch, ::optarg);
191 // Clear getopt state just in case the test wants to use it.
194 #if defined(HAVE_OPTRESET)
200 impl::app::run(int argc, char* const* argv)
210 m_prog_name = std::strrchr(m_argv[0], '/');
211 if (m_prog_name == NULL)
212 m_prog_name = m_argv[0];
216 // Libtool workaround: if running from within the source tree (binaries
217 // that are not installed yet), skip the "lt-" prefix added to files in
218 // the ".libs" directory to show the real (not temporary) name.
219 if (std::strncmp(m_prog_name, "lt-", 3) == 0)
222 const std::string bug =
223 std::string("This is probably a bug in ") + m_prog_name +
224 " or one of the libraries it uses. Please report this problem to "
225 PACKAGE_BUGREPORT " and provide as many details as possible "
226 "describing how you got to this condition.";
232 } catch (const usage_error& e) {
233 std::cerr << m_prog_name << ": ERROR: " << e.what() << "\n";
234 std::cerr << m_prog_name << ": See " << m_manpage << " for usage "
236 errcode = EXIT_FAILURE;
237 } catch (const std::runtime_error& e) {
238 std::cerr << m_prog_name << ": ERROR: " << e.what() << "\n";
239 errcode = EXIT_FAILURE;
240 } catch (const std::exception& e) {
241 std::cerr << m_prog_name << ": ERROR: Caught unexpected error: "
243 errcode = EXIT_FAILURE;
245 std::cerr << m_prog_name << ": ERROR: Caught unknown error\n";
246 errcode = EXIT_FAILURE;