xref: /freebsd/contrib/atf/atf-c++/detail/application.cpp (revision 8f0ea33f2bbf3a6aa80235f0a02fa5f2780c2b17)
1c243e490SMarcel Moolenaar // Copyright (c) 2007 The NetBSD Foundation, Inc.
2c243e490SMarcel Moolenaar // All rights reserved.
3c243e490SMarcel Moolenaar //
4c243e490SMarcel Moolenaar // Redistribution and use in source and binary forms, with or without
5c243e490SMarcel Moolenaar // modification, are permitted provided that the following conditions
6c243e490SMarcel Moolenaar // are met:
7c243e490SMarcel Moolenaar // 1. Redistributions of source code must retain the above copyright
8c243e490SMarcel Moolenaar //    notice, this list of conditions and the following disclaimer.
9c243e490SMarcel Moolenaar // 2. Redistributions in binary form must reproduce the above copyright
10c243e490SMarcel Moolenaar //    notice, this list of conditions and the following disclaimer in the
11c243e490SMarcel Moolenaar //    documentation and/or other materials provided with the distribution.
12c243e490SMarcel Moolenaar //
13c243e490SMarcel Moolenaar // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14c243e490SMarcel Moolenaar // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15c243e490SMarcel Moolenaar // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16c243e490SMarcel Moolenaar // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17c243e490SMarcel Moolenaar // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18c243e490SMarcel Moolenaar // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19c243e490SMarcel Moolenaar // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20c243e490SMarcel Moolenaar // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21c243e490SMarcel Moolenaar // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22c243e490SMarcel Moolenaar // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23c243e490SMarcel Moolenaar // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24c243e490SMarcel Moolenaar // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*0677dfd1SJulio Merino 
26*0677dfd1SJulio Merino #include "atf-c++/detail/application.hpp"
27c243e490SMarcel Moolenaar 
28c243e490SMarcel Moolenaar #if defined(HAVE_CONFIG_H)
29*0677dfd1SJulio Merino #include "config.h"
30c243e490SMarcel Moolenaar #endif
31c243e490SMarcel Moolenaar 
32c243e490SMarcel Moolenaar extern "C" {
33c243e490SMarcel Moolenaar #include <unistd.h>
34c243e490SMarcel Moolenaar }
35c243e490SMarcel Moolenaar 
36c243e490SMarcel Moolenaar #include <cstdarg>
37c243e490SMarcel Moolenaar #include <cstdio>
38c243e490SMarcel Moolenaar #include <cstdlib>
39c243e490SMarcel Moolenaar #include <cstring>
40c243e490SMarcel Moolenaar #include <iostream>
41c243e490SMarcel Moolenaar 
42c243e490SMarcel Moolenaar extern "C" {
43c243e490SMarcel Moolenaar #include "atf-c/defs.h"
44c243e490SMarcel Moolenaar }
45c243e490SMarcel Moolenaar 
46*0677dfd1SJulio Merino #include "atf-c++/detail/sanity.hpp"
47c243e490SMarcel Moolenaar 
48c243e490SMarcel Moolenaar #if !defined(HAVE_VSNPRINTF_IN_STD)
49c243e490SMarcel Moolenaar namespace std {
50c243e490SMarcel Moolenaar using ::vsnprintf;
51c243e490SMarcel Moolenaar }
52c243e490SMarcel Moolenaar #endif // !defined(HAVE_VSNPRINTF_IN_STD)
53c243e490SMarcel Moolenaar 
54c243e490SMarcel Moolenaar namespace impl = atf::application;
55c243e490SMarcel Moolenaar #define IMPL_NAME "atf::application"
56c243e490SMarcel Moolenaar 
57c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
58c243e490SMarcel Moolenaar // The "usage_error" class.
59c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
60c243e490SMarcel Moolenaar 
usage_error(const char * fmt,...)61c243e490SMarcel Moolenaar impl::usage_error::usage_error(const char *fmt, ...)
62c243e490SMarcel Moolenaar     throw() :
63c243e490SMarcel Moolenaar     std::runtime_error("usage_error; message unformatted")
64c243e490SMarcel Moolenaar {
65c243e490SMarcel Moolenaar     va_list ap;
66c243e490SMarcel Moolenaar 
67c243e490SMarcel Moolenaar     va_start(ap, fmt);
68c243e490SMarcel Moolenaar     std::vsnprintf(m_text, sizeof(m_text), fmt, ap);
69c243e490SMarcel Moolenaar     va_end(ap);
70c243e490SMarcel Moolenaar }
71c243e490SMarcel Moolenaar 
~usage_error(void)72c243e490SMarcel Moolenaar impl::usage_error::~usage_error(void)
73c243e490SMarcel Moolenaar     throw()
74c243e490SMarcel Moolenaar {
75c243e490SMarcel Moolenaar }
76c243e490SMarcel Moolenaar 
77c243e490SMarcel Moolenaar const char*
what(void) const78c243e490SMarcel Moolenaar impl::usage_error::what(void)
79c243e490SMarcel Moolenaar     const throw()
80c243e490SMarcel Moolenaar {
81c243e490SMarcel Moolenaar     return m_text;
82c243e490SMarcel Moolenaar }
83c243e490SMarcel Moolenaar 
84c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
85c243e490SMarcel Moolenaar // The "application" class.
86c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
87c243e490SMarcel Moolenaar 
option(char ch,const std::string & a,const std::string & desc)88c243e490SMarcel Moolenaar impl::option::option(char ch,
89c243e490SMarcel Moolenaar                      const std::string& a,
90c243e490SMarcel Moolenaar                      const std::string& desc) :
91c243e490SMarcel Moolenaar     m_character(ch),
92c243e490SMarcel Moolenaar     m_argument(a),
93c243e490SMarcel Moolenaar     m_description(desc)
94c243e490SMarcel Moolenaar {
95c243e490SMarcel Moolenaar }
96c243e490SMarcel Moolenaar 
97c243e490SMarcel Moolenaar bool
operator <(const impl::option & o) const98c243e490SMarcel Moolenaar impl::option::operator<(const impl::option& o)
99c243e490SMarcel Moolenaar     const
100c243e490SMarcel Moolenaar {
101c243e490SMarcel Moolenaar     return m_character < o.m_character;
102c243e490SMarcel Moolenaar }
103c243e490SMarcel Moolenaar 
app(const std::string & description,const std::string & manpage)104c243e490SMarcel Moolenaar impl::app::app(const std::string& description,
1051a61beb0SJulio Merino                const std::string& manpage) :
106c243e490SMarcel Moolenaar     m_argc(-1),
107c243e490SMarcel Moolenaar     m_argv(NULL),
108c243e490SMarcel Moolenaar     m_prog_name(NULL),
109c243e490SMarcel Moolenaar     m_description(description),
1101a61beb0SJulio Merino     m_manpage(manpage)
111c243e490SMarcel Moolenaar {
112c243e490SMarcel Moolenaar }
113c243e490SMarcel Moolenaar 
~app(void)114c243e490SMarcel Moolenaar impl::app::~app(void)
115c243e490SMarcel Moolenaar {
116c243e490SMarcel Moolenaar }
117c243e490SMarcel Moolenaar 
118c243e490SMarcel Moolenaar bool
inited(void)119c243e490SMarcel Moolenaar impl::app::inited(void)
120c243e490SMarcel Moolenaar {
121c243e490SMarcel Moolenaar     return m_argc != -1;
122c243e490SMarcel Moolenaar }
123c243e490SMarcel Moolenaar 
124c243e490SMarcel Moolenaar impl::app::options_set
options(void)125c243e490SMarcel Moolenaar impl::app::options(void)
126c243e490SMarcel Moolenaar {
1271a61beb0SJulio Merino     return specific_options();
128c243e490SMarcel Moolenaar }
129c243e490SMarcel Moolenaar 
130c243e490SMarcel Moolenaar std::string
specific_args(void) const131c243e490SMarcel Moolenaar impl::app::specific_args(void)
132c243e490SMarcel Moolenaar     const
133c243e490SMarcel Moolenaar {
134c243e490SMarcel Moolenaar     return "";
135c243e490SMarcel Moolenaar }
136c243e490SMarcel Moolenaar 
137c243e490SMarcel Moolenaar impl::app::options_set
specific_options(void) const138c243e490SMarcel Moolenaar impl::app::specific_options(void)
139c243e490SMarcel Moolenaar     const
140c243e490SMarcel Moolenaar {
141c243e490SMarcel Moolenaar     return options_set();
142c243e490SMarcel Moolenaar }
143c243e490SMarcel Moolenaar 
144c243e490SMarcel Moolenaar void
process_option(int ch ATF_DEFS_ATTRIBUTE_UNUSED,const char * arg ATF_DEFS_ATTRIBUTE_UNUSED)145c243e490SMarcel Moolenaar impl::app::process_option(int ch ATF_DEFS_ATTRIBUTE_UNUSED,
146c243e490SMarcel Moolenaar                           const char* arg ATF_DEFS_ATTRIBUTE_UNUSED)
147c243e490SMarcel Moolenaar {
148c243e490SMarcel Moolenaar }
149c243e490SMarcel Moolenaar 
150c243e490SMarcel Moolenaar void
process_options(void)151c243e490SMarcel Moolenaar impl::app::process_options(void)
152c243e490SMarcel Moolenaar {
153c243e490SMarcel Moolenaar     PRE(inited());
154c243e490SMarcel Moolenaar 
155c243e490SMarcel Moolenaar     std::string optstr;
156c243e490SMarcel Moolenaar #if defined(HAVE_GNU_GETOPT)
157c243e490SMarcel Moolenaar     optstr += '+'; // Turn on POSIX behavior.
158c243e490SMarcel Moolenaar #endif
159c243e490SMarcel Moolenaar     optstr += ':';
160c243e490SMarcel Moolenaar     {
161c243e490SMarcel Moolenaar         options_set opts = options();
162c243e490SMarcel Moolenaar         for (options_set::const_iterator iter = opts.begin();
163c243e490SMarcel Moolenaar              iter != opts.end(); iter++) {
164c243e490SMarcel Moolenaar             const option& opt = (*iter);
165c243e490SMarcel Moolenaar 
166c243e490SMarcel Moolenaar             optstr += opt.m_character;
167c243e490SMarcel Moolenaar             if (!opt.m_argument.empty())
168c243e490SMarcel Moolenaar                 optstr += ':';
169c243e490SMarcel Moolenaar         }
170c243e490SMarcel Moolenaar     }
171c243e490SMarcel Moolenaar 
172c243e490SMarcel Moolenaar     int ch;
173c243e490SMarcel Moolenaar     const int old_opterr = ::opterr;
174c243e490SMarcel Moolenaar     ::opterr = 0;
175c243e490SMarcel Moolenaar     while ((ch = ::getopt(m_argc, m_argv, optstr.c_str())) != -1) {
176c243e490SMarcel Moolenaar         switch (ch) {
177c243e490SMarcel Moolenaar             case ':':
178c243e490SMarcel Moolenaar                 throw usage_error("Option -%c requires an argument.",
179c243e490SMarcel Moolenaar                                   ::optopt);
180c243e490SMarcel Moolenaar 
181c243e490SMarcel Moolenaar             case '?':
182c243e490SMarcel Moolenaar                 throw usage_error("Unknown option -%c.", ::optopt);
183c243e490SMarcel Moolenaar 
184c243e490SMarcel Moolenaar             default:
185c243e490SMarcel Moolenaar                 process_option(ch, ::optarg);
186c243e490SMarcel Moolenaar         }
187c243e490SMarcel Moolenaar     }
188c243e490SMarcel Moolenaar     m_argc -= ::optind;
189c243e490SMarcel Moolenaar     m_argv += ::optind;
190c243e490SMarcel Moolenaar 
191c243e490SMarcel Moolenaar     // Clear getopt state just in case the test wants to use it.
192c243e490SMarcel Moolenaar     opterr = old_opterr;
193c243e490SMarcel Moolenaar     optind = 1;
194c243e490SMarcel Moolenaar #if defined(HAVE_OPTRESET)
195c243e490SMarcel Moolenaar     optreset = 1;
196c243e490SMarcel Moolenaar #endif
197c243e490SMarcel Moolenaar }
198c243e490SMarcel Moolenaar 
199c243e490SMarcel Moolenaar int
run(int argc,char * const * argv)200c243e490SMarcel Moolenaar impl::app::run(int argc, char* const* argv)
201c243e490SMarcel Moolenaar {
202c243e490SMarcel Moolenaar     PRE(argc > 0);
203c243e490SMarcel Moolenaar     PRE(argv != NULL);
204c243e490SMarcel Moolenaar 
205c243e490SMarcel Moolenaar     m_argc = argc;
206c243e490SMarcel Moolenaar     m_argv = argv;
207c243e490SMarcel Moolenaar 
208c243e490SMarcel Moolenaar     m_argv0 = m_argv[0];
209c243e490SMarcel Moolenaar 
210c243e490SMarcel Moolenaar     m_prog_name = std::strrchr(m_argv[0], '/');
211c243e490SMarcel Moolenaar     if (m_prog_name == NULL)
212c243e490SMarcel Moolenaar         m_prog_name = m_argv[0];
213c243e490SMarcel Moolenaar     else
214c243e490SMarcel Moolenaar         m_prog_name++;
215c243e490SMarcel Moolenaar 
216c243e490SMarcel Moolenaar     // Libtool workaround: if running from within the source tree (binaries
217c243e490SMarcel Moolenaar     // that are not installed yet), skip the "lt-" prefix added to files in
218c243e490SMarcel Moolenaar     // the ".libs" directory to show the real (not temporary) name.
219c243e490SMarcel Moolenaar     if (std::strncmp(m_prog_name, "lt-", 3) == 0)
220c243e490SMarcel Moolenaar         m_prog_name += 3;
221c243e490SMarcel Moolenaar 
222c243e490SMarcel Moolenaar     const std::string bug =
223c243e490SMarcel Moolenaar         std::string("This is probably a bug in ") + m_prog_name +
224c243e490SMarcel Moolenaar         " or one of the libraries it uses.  Please report this problem to "
225c243e490SMarcel Moolenaar         PACKAGE_BUGREPORT " and provide as many details as possible "
226c243e490SMarcel Moolenaar         "describing how you got to this condition.";
227c243e490SMarcel Moolenaar 
228c243e490SMarcel Moolenaar     int errcode;
229c243e490SMarcel Moolenaar     try {
230c243e490SMarcel Moolenaar         process_options();
231c243e490SMarcel Moolenaar         errcode = main();
232c243e490SMarcel Moolenaar     } catch (const usage_error& e) {
233c243e490SMarcel Moolenaar         std::cerr << m_prog_name << ": ERROR: " << e.what() << "\n";
234c243e490SMarcel Moolenaar         std::cerr << m_prog_name << ": See " << m_manpage << " for usage "
235c243e490SMarcel Moolenaar             "details.\n";
236c243e490SMarcel Moolenaar         errcode = EXIT_FAILURE;
237c243e490SMarcel Moolenaar     } catch (const std::runtime_error& e) {
238c243e490SMarcel Moolenaar         std::cerr << m_prog_name << ": ERROR: " << e.what() << "\n";
239c243e490SMarcel Moolenaar         errcode = EXIT_FAILURE;
240c243e490SMarcel Moolenaar     } catch (const std::exception& e) {
241c243e490SMarcel Moolenaar         std::cerr << m_prog_name << ": ERROR: Caught unexpected error: "
242c243e490SMarcel Moolenaar                   << e.what() << "\n";
243c243e490SMarcel Moolenaar         errcode = EXIT_FAILURE;
244c243e490SMarcel Moolenaar     } catch (...) {
245c243e490SMarcel Moolenaar         std::cerr << m_prog_name << ": ERROR: Caught unknown error\n";
246c243e490SMarcel Moolenaar         errcode = EXIT_FAILURE;
247c243e490SMarcel Moolenaar     }
248c243e490SMarcel Moolenaar     return errcode;
249c243e490SMarcel Moolenaar }
250