111be35a1SLionel Sambuc //
211be35a1SLionel Sambuc // Automated Testing Framework (atf)
311be35a1SLionel Sambuc //
411be35a1SLionel Sambuc // Copyright (c) 2007 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 #if defined(HAVE_CONFIG_H)
3111be35a1SLionel Sambuc #include "bconfig.h"
3211be35a1SLionel Sambuc #endif
3311be35a1SLionel Sambuc
3411be35a1SLionel Sambuc extern "C" {
3511be35a1SLionel Sambuc #include <unistd.h>
3611be35a1SLionel Sambuc }
3711be35a1SLionel Sambuc
3811be35a1SLionel Sambuc #include <cstdarg>
3911be35a1SLionel Sambuc #include <cstdio>
4011be35a1SLionel Sambuc #include <cstdlib>
4111be35a1SLionel Sambuc #include <cstring>
4211be35a1SLionel Sambuc #include <iostream>
4311be35a1SLionel Sambuc
4411be35a1SLionel Sambuc extern "C" {
4511be35a1SLionel Sambuc #include "atf-c/defs.h"
4611be35a1SLionel Sambuc }
4711be35a1SLionel Sambuc
4811be35a1SLionel Sambuc #include "application.hpp"
4911be35a1SLionel Sambuc #include "sanity.hpp"
5011be35a1SLionel Sambuc
5111be35a1SLionel Sambuc #if !defined(HAVE_VSNPRINTF_IN_STD)
5211be35a1SLionel Sambuc namespace std {
5311be35a1SLionel Sambuc using ::vsnprintf;
5411be35a1SLionel Sambuc }
5511be35a1SLionel Sambuc #endif // !defined(HAVE_VSNPRINTF_IN_STD)
5611be35a1SLionel Sambuc
5711be35a1SLionel Sambuc namespace impl = atf::application;
5811be35a1SLionel Sambuc #define IMPL_NAME "atf::application"
5911be35a1SLionel Sambuc
6011be35a1SLionel Sambuc // ------------------------------------------------------------------------
6111be35a1SLionel Sambuc // The "usage_error" class.
6211be35a1SLionel Sambuc // ------------------------------------------------------------------------
6311be35a1SLionel Sambuc
usage_error(const char * fmt,...)6411be35a1SLionel Sambuc impl::usage_error::usage_error(const char *fmt, ...)
6511be35a1SLionel Sambuc throw() :
6611be35a1SLionel Sambuc std::runtime_error("usage_error; message unformatted")
6711be35a1SLionel Sambuc {
6811be35a1SLionel Sambuc va_list ap;
6911be35a1SLionel Sambuc
7011be35a1SLionel Sambuc va_start(ap, fmt);
7111be35a1SLionel Sambuc std::vsnprintf(m_text, sizeof(m_text), fmt, ap);
7211be35a1SLionel Sambuc va_end(ap);
7311be35a1SLionel Sambuc }
7411be35a1SLionel Sambuc
~usage_error(void)7511be35a1SLionel Sambuc impl::usage_error::~usage_error(void)
7611be35a1SLionel Sambuc throw()
7711be35a1SLionel Sambuc {
7811be35a1SLionel Sambuc }
7911be35a1SLionel Sambuc
8011be35a1SLionel Sambuc const char*
what(void) const8111be35a1SLionel Sambuc impl::usage_error::what(void)
8211be35a1SLionel Sambuc const throw()
8311be35a1SLionel Sambuc {
8411be35a1SLionel Sambuc return m_text;
8511be35a1SLionel Sambuc }
8611be35a1SLionel Sambuc
8711be35a1SLionel Sambuc // ------------------------------------------------------------------------
8811be35a1SLionel Sambuc // The "application" class.
8911be35a1SLionel Sambuc // ------------------------------------------------------------------------
9011be35a1SLionel Sambuc
option(char ch,const std::string & a,const std::string & desc)9111be35a1SLionel Sambuc impl::option::option(char ch,
9211be35a1SLionel Sambuc const std::string& a,
9311be35a1SLionel Sambuc const std::string& desc) :
9411be35a1SLionel Sambuc m_character(ch),
9511be35a1SLionel Sambuc m_argument(a),
9611be35a1SLionel Sambuc m_description(desc)
9711be35a1SLionel Sambuc {
9811be35a1SLionel Sambuc }
9911be35a1SLionel Sambuc
10011be35a1SLionel Sambuc bool
operator <(const impl::option & o) const10111be35a1SLionel Sambuc impl::option::operator<(const impl::option& o)
10211be35a1SLionel Sambuc const
10311be35a1SLionel Sambuc {
10411be35a1SLionel Sambuc return m_character < o.m_character;
10511be35a1SLionel Sambuc }
10611be35a1SLionel Sambuc
app(const std::string & description,const std::string & manpage)10711be35a1SLionel Sambuc impl::app::app(const std::string& description,
108*0a6a1f1dSLionel Sambuc const std::string& manpage) :
10911be35a1SLionel Sambuc m_argc(-1),
11011be35a1SLionel Sambuc m_argv(NULL),
11111be35a1SLionel Sambuc m_prog_name(NULL),
11211be35a1SLionel Sambuc m_description(description),
113*0a6a1f1dSLionel Sambuc m_manpage(manpage)
11411be35a1SLionel Sambuc {
11511be35a1SLionel Sambuc }
11611be35a1SLionel Sambuc
~app(void)11711be35a1SLionel Sambuc impl::app::~app(void)
11811be35a1SLionel Sambuc {
11911be35a1SLionel Sambuc }
12011be35a1SLionel Sambuc
12111be35a1SLionel Sambuc bool
inited(void)12211be35a1SLionel Sambuc impl::app::inited(void)
12311be35a1SLionel Sambuc {
12411be35a1SLionel Sambuc return m_argc != -1;
12511be35a1SLionel Sambuc }
12611be35a1SLionel Sambuc
12711be35a1SLionel Sambuc impl::app::options_set
options(void)12811be35a1SLionel Sambuc impl::app::options(void)
12911be35a1SLionel Sambuc {
130*0a6a1f1dSLionel Sambuc return specific_options();
13111be35a1SLionel Sambuc }
13211be35a1SLionel Sambuc
13311be35a1SLionel Sambuc std::string
specific_args(void) const13411be35a1SLionel Sambuc impl::app::specific_args(void)
13511be35a1SLionel Sambuc const
13611be35a1SLionel Sambuc {
13711be35a1SLionel Sambuc return "";
13811be35a1SLionel Sambuc }
13911be35a1SLionel Sambuc
14011be35a1SLionel Sambuc impl::app::options_set
specific_options(void) const14111be35a1SLionel Sambuc impl::app::specific_options(void)
14211be35a1SLionel Sambuc const
14311be35a1SLionel Sambuc {
14411be35a1SLionel Sambuc return options_set();
14511be35a1SLionel Sambuc }
14611be35a1SLionel Sambuc
14711be35a1SLionel Sambuc void
process_option(int ch ATF_DEFS_ATTRIBUTE_UNUSED,const char * arg ATF_DEFS_ATTRIBUTE_UNUSED)14811be35a1SLionel Sambuc impl::app::process_option(int ch ATF_DEFS_ATTRIBUTE_UNUSED,
14911be35a1SLionel Sambuc const char* arg ATF_DEFS_ATTRIBUTE_UNUSED)
15011be35a1SLionel Sambuc {
15111be35a1SLionel Sambuc }
15211be35a1SLionel Sambuc
15311be35a1SLionel Sambuc void
process_options(void)15411be35a1SLionel Sambuc impl::app::process_options(void)
15511be35a1SLionel Sambuc {
15611be35a1SLionel Sambuc PRE(inited());
15711be35a1SLionel Sambuc
15811be35a1SLionel Sambuc std::string optstr;
15911be35a1SLionel Sambuc #if defined(HAVE_GNU_GETOPT)
16011be35a1SLionel Sambuc optstr += '+'; // Turn on POSIX behavior.
16111be35a1SLionel Sambuc #endif
16211be35a1SLionel Sambuc optstr += ':';
16311be35a1SLionel Sambuc {
16411be35a1SLionel Sambuc options_set opts = options();
16511be35a1SLionel Sambuc for (options_set::const_iterator iter = opts.begin();
16611be35a1SLionel Sambuc iter != opts.end(); iter++) {
16711be35a1SLionel Sambuc const option& opt = (*iter);
16811be35a1SLionel Sambuc
16911be35a1SLionel Sambuc optstr += opt.m_character;
17011be35a1SLionel Sambuc if (!opt.m_argument.empty())
17111be35a1SLionel Sambuc optstr += ':';
17211be35a1SLionel Sambuc }
17311be35a1SLionel Sambuc }
17411be35a1SLionel Sambuc
17511be35a1SLionel Sambuc int ch;
17611be35a1SLionel Sambuc const int old_opterr = ::opterr;
17711be35a1SLionel Sambuc ::opterr = 0;
17811be35a1SLionel Sambuc while ((ch = ::getopt(m_argc, m_argv, optstr.c_str())) != -1) {
17911be35a1SLionel Sambuc switch (ch) {
18011be35a1SLionel Sambuc case ':':
18111be35a1SLionel Sambuc throw usage_error("Option -%c requires an argument.",
18211be35a1SLionel Sambuc ::optopt);
18311be35a1SLionel Sambuc
18411be35a1SLionel Sambuc case '?':
18511be35a1SLionel Sambuc throw usage_error("Unknown option -%c.", ::optopt);
18611be35a1SLionel Sambuc
18711be35a1SLionel Sambuc default:
18811be35a1SLionel Sambuc process_option(ch, ::optarg);
18911be35a1SLionel Sambuc }
19011be35a1SLionel Sambuc }
19111be35a1SLionel Sambuc m_argc -= ::optind;
19211be35a1SLionel Sambuc m_argv += ::optind;
19311be35a1SLionel Sambuc
19411be35a1SLionel Sambuc // Clear getopt state just in case the test wants to use it.
19511be35a1SLionel Sambuc opterr = old_opterr;
19611be35a1SLionel Sambuc optind = 1;
19711be35a1SLionel Sambuc #if defined(HAVE_OPTRESET)
19811be35a1SLionel Sambuc optreset = 1;
19911be35a1SLionel Sambuc #endif
20011be35a1SLionel Sambuc }
20111be35a1SLionel Sambuc
20211be35a1SLionel Sambuc int
run(int argc,char * const * argv)20311be35a1SLionel Sambuc impl::app::run(int argc, char* const* argv)
20411be35a1SLionel Sambuc {
20511be35a1SLionel Sambuc PRE(argc > 0);
20611be35a1SLionel Sambuc PRE(argv != NULL);
20711be35a1SLionel Sambuc
20811be35a1SLionel Sambuc m_argc = argc;
20911be35a1SLionel Sambuc m_argv = argv;
21011be35a1SLionel Sambuc
21111be35a1SLionel Sambuc m_argv0 = m_argv[0];
21211be35a1SLionel Sambuc
21311be35a1SLionel Sambuc m_prog_name = std::strrchr(m_argv[0], '/');
21411be35a1SLionel Sambuc if (m_prog_name == NULL)
21511be35a1SLionel Sambuc m_prog_name = m_argv[0];
21611be35a1SLionel Sambuc else
21711be35a1SLionel Sambuc m_prog_name++;
21811be35a1SLionel Sambuc
21911be35a1SLionel Sambuc // Libtool workaround: if running from within the source tree (binaries
22011be35a1SLionel Sambuc // that are not installed yet), skip the "lt-" prefix added to files in
22111be35a1SLionel Sambuc // the ".libs" directory to show the real (not temporary) name.
22211be35a1SLionel Sambuc if (std::strncmp(m_prog_name, "lt-", 3) == 0)
22311be35a1SLionel Sambuc m_prog_name += 3;
22411be35a1SLionel Sambuc
22511be35a1SLionel Sambuc const std::string bug =
22611be35a1SLionel Sambuc std::string("This is probably a bug in ") + m_prog_name +
22711be35a1SLionel Sambuc " or one of the libraries it uses. Please report this problem to "
22811be35a1SLionel Sambuc PACKAGE_BUGREPORT " and provide as many details as possible "
22911be35a1SLionel Sambuc "describing how you got to this condition.";
23011be35a1SLionel Sambuc
23111be35a1SLionel Sambuc int errcode;
23211be35a1SLionel Sambuc try {
23311be35a1SLionel Sambuc process_options();
23411be35a1SLionel Sambuc errcode = main();
23511be35a1SLionel Sambuc } catch (const usage_error& e) {
23611be35a1SLionel Sambuc std::cerr << m_prog_name << ": ERROR: " << e.what() << "\n";
23711be35a1SLionel Sambuc std::cerr << m_prog_name << ": See " << m_manpage << " for usage "
23811be35a1SLionel Sambuc "details.\n";
23911be35a1SLionel Sambuc errcode = EXIT_FAILURE;
24011be35a1SLionel Sambuc } catch (const std::runtime_error& e) {
24111be35a1SLionel Sambuc std::cerr << m_prog_name << ": ERROR: " << e.what() << "\n";
24211be35a1SLionel Sambuc errcode = EXIT_FAILURE;
24311be35a1SLionel Sambuc } catch (const std::exception& e) {
24411be35a1SLionel Sambuc std::cerr << m_prog_name << ": ERROR: Caught unexpected error: "
24511be35a1SLionel Sambuc << e.what() << "\n";
24611be35a1SLionel Sambuc errcode = EXIT_FAILURE;
24711be35a1SLionel Sambuc } catch (...) {
24811be35a1SLionel Sambuc std::cerr << m_prog_name << ": ERROR: Caught unknown error\n";
24911be35a1SLionel Sambuc errcode = EXIT_FAILURE;
25011be35a1SLionel Sambuc }
25111be35a1SLionel Sambuc return errcode;
25211be35a1SLionel Sambuc }
253