xref: /freebsd/contrib/kyua/cli/main.cpp (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
1*b0d29bc4SBrooks Davis // Copyright 2010 The Kyua Authors.
2*b0d29bc4SBrooks Davis // All rights reserved.
3*b0d29bc4SBrooks Davis //
4*b0d29bc4SBrooks Davis // Redistribution and use in source and binary forms, with or without
5*b0d29bc4SBrooks Davis // modification, are permitted provided that the following conditions are
6*b0d29bc4SBrooks Davis // met:
7*b0d29bc4SBrooks Davis //
8*b0d29bc4SBrooks Davis // * Redistributions of source code must retain the above copyright
9*b0d29bc4SBrooks Davis //   notice, this list of conditions and the following disclaimer.
10*b0d29bc4SBrooks Davis // * Redistributions in binary form must reproduce the above copyright
11*b0d29bc4SBrooks Davis //   notice, this list of conditions and the following disclaimer in the
12*b0d29bc4SBrooks Davis //   documentation and/or other materials provided with the distribution.
13*b0d29bc4SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
14*b0d29bc4SBrooks Davis //   may be used to endorse or promote products derived from this software
15*b0d29bc4SBrooks Davis //   without specific prior written permission.
16*b0d29bc4SBrooks Davis //
17*b0d29bc4SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*b0d29bc4SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*b0d29bc4SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*b0d29bc4SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*b0d29bc4SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*b0d29bc4SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*b0d29bc4SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*b0d29bc4SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*b0d29bc4SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*b0d29bc4SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*b0d29bc4SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*b0d29bc4SBrooks Davis 
29*b0d29bc4SBrooks Davis #include "cli/main.hpp"
30*b0d29bc4SBrooks Davis 
31*b0d29bc4SBrooks Davis #if defined(HAVE_CONFIG_H)
32*b0d29bc4SBrooks Davis #   include "config.h"
33*b0d29bc4SBrooks Davis #endif
34*b0d29bc4SBrooks Davis 
35*b0d29bc4SBrooks Davis extern "C" {
36*b0d29bc4SBrooks Davis #include <signal.h>
37*b0d29bc4SBrooks Davis #include <unistd.h>
38*b0d29bc4SBrooks Davis }
39*b0d29bc4SBrooks Davis 
40*b0d29bc4SBrooks Davis #include <cstdlib>
41*b0d29bc4SBrooks Davis #include <iostream>
42*b0d29bc4SBrooks Davis #include <string>
43*b0d29bc4SBrooks Davis #include <utility>
44*b0d29bc4SBrooks Davis 
45*b0d29bc4SBrooks Davis #include "cli/cmd_about.hpp"
46*b0d29bc4SBrooks Davis #include "cli/cmd_config.hpp"
47*b0d29bc4SBrooks Davis #include "cli/cmd_db_exec.hpp"
48*b0d29bc4SBrooks Davis #include "cli/cmd_db_migrate.hpp"
49*b0d29bc4SBrooks Davis #include "cli/cmd_debug.hpp"
50*b0d29bc4SBrooks Davis #include "cli/cmd_help.hpp"
51*b0d29bc4SBrooks Davis #include "cli/cmd_list.hpp"
52*b0d29bc4SBrooks Davis #include "cli/cmd_report.hpp"
53*b0d29bc4SBrooks Davis #include "cli/cmd_report_html.hpp"
54*b0d29bc4SBrooks Davis #include "cli/cmd_report_junit.hpp"
55*b0d29bc4SBrooks Davis #include "cli/cmd_test.hpp"
56*b0d29bc4SBrooks Davis #include "cli/common.ipp"
57*b0d29bc4SBrooks Davis #include "cli/config.hpp"
58*b0d29bc4SBrooks Davis #include "engine/atf.hpp"
59*b0d29bc4SBrooks Davis #include "engine/plain.hpp"
60*b0d29bc4SBrooks Davis #include "engine/scheduler.hpp"
61*b0d29bc4SBrooks Davis #include "engine/tap.hpp"
62*b0d29bc4SBrooks Davis #include "store/exceptions.hpp"
63*b0d29bc4SBrooks Davis #include "utils/cmdline/commands_map.ipp"
64*b0d29bc4SBrooks Davis #include "utils/cmdline/exceptions.hpp"
65*b0d29bc4SBrooks Davis #include "utils/cmdline/globals.hpp"
66*b0d29bc4SBrooks Davis #include "utils/cmdline/options.hpp"
67*b0d29bc4SBrooks Davis #include "utils/cmdline/parser.ipp"
68*b0d29bc4SBrooks Davis #include "utils/cmdline/ui.hpp"
69*b0d29bc4SBrooks Davis #include "utils/config/tree.ipp"
70*b0d29bc4SBrooks Davis #include "utils/env.hpp"
71*b0d29bc4SBrooks Davis #include "utils/format/macros.hpp"
72*b0d29bc4SBrooks Davis #include "utils/fs/operations.hpp"
73*b0d29bc4SBrooks Davis #include "utils/fs/path.hpp"
74*b0d29bc4SBrooks Davis #include "utils/logging/macros.hpp"
75*b0d29bc4SBrooks Davis #include "utils/logging/operations.hpp"
76*b0d29bc4SBrooks Davis #include "utils/optional.ipp"
77*b0d29bc4SBrooks Davis #include "utils/sanity.hpp"
78*b0d29bc4SBrooks Davis #include "utils/signals/exceptions.hpp"
79*b0d29bc4SBrooks Davis 
80*b0d29bc4SBrooks Davis namespace cmdline = utils::cmdline;
81*b0d29bc4SBrooks Davis namespace config = utils::config;
82*b0d29bc4SBrooks Davis namespace fs = utils::fs;
83*b0d29bc4SBrooks Davis namespace logging = utils::logging;
84*b0d29bc4SBrooks Davis namespace signals = utils::signals;
85*b0d29bc4SBrooks Davis namespace scheduler = engine::scheduler;
86*b0d29bc4SBrooks Davis 
87*b0d29bc4SBrooks Davis using utils::none;
88*b0d29bc4SBrooks Davis using utils::optional;
89*b0d29bc4SBrooks Davis 
90*b0d29bc4SBrooks Davis 
91*b0d29bc4SBrooks Davis namespace {
92*b0d29bc4SBrooks Davis 
93*b0d29bc4SBrooks Davis 
94*b0d29bc4SBrooks Davis /// Registers all valid scheduler interfaces.
95*b0d29bc4SBrooks Davis ///
96*b0d29bc4SBrooks Davis /// This is part of Kyua's setup but it is a bit strange to find it here.  I am
97*b0d29bc4SBrooks Davis /// not sure what a better location would be though, so for now this is good
98*b0d29bc4SBrooks Davis /// enough.
99*b0d29bc4SBrooks Davis static void
register_scheduler_interfaces(void)100*b0d29bc4SBrooks Davis register_scheduler_interfaces(void)
101*b0d29bc4SBrooks Davis {
102*b0d29bc4SBrooks Davis     scheduler::register_interface(
103*b0d29bc4SBrooks Davis         "atf", std::shared_ptr< scheduler::interface >(
104*b0d29bc4SBrooks Davis             new engine::atf_interface()));
105*b0d29bc4SBrooks Davis     scheduler::register_interface(
106*b0d29bc4SBrooks Davis         "plain", std::shared_ptr< scheduler::interface >(
107*b0d29bc4SBrooks Davis             new engine::plain_interface()));
108*b0d29bc4SBrooks Davis     scheduler::register_interface(
109*b0d29bc4SBrooks Davis         "tap", std::shared_ptr< scheduler::interface >(
110*b0d29bc4SBrooks Davis             new engine::tap_interface()));
111*b0d29bc4SBrooks Davis }
112*b0d29bc4SBrooks Davis 
113*b0d29bc4SBrooks Davis 
114*b0d29bc4SBrooks Davis /// Executes the given subcommand with proper usage_error reporting.
115*b0d29bc4SBrooks Davis ///
116*b0d29bc4SBrooks Davis /// \param ui Object to interact with the I/O of the program.
117*b0d29bc4SBrooks Davis /// \param command The subcommand to execute.
118*b0d29bc4SBrooks Davis /// \param args The part of the command line passed to the subcommand.  The
119*b0d29bc4SBrooks Davis ///     first item of this collection must match the command name.
120*b0d29bc4SBrooks Davis /// \param user_config The runtime configuration to pass to the subcommand.
121*b0d29bc4SBrooks Davis ///
122*b0d29bc4SBrooks Davis /// \return The exit code of the command.  Typically 0 on success, some other
123*b0d29bc4SBrooks Davis /// integer otherwise.
124*b0d29bc4SBrooks Davis ///
125*b0d29bc4SBrooks Davis /// \throw cmdline::usage_error If the user input to the subcommand is invalid.
126*b0d29bc4SBrooks Davis ///     This error does not encode the command name within it, so this function
127*b0d29bc4SBrooks Davis ///     extends the message in the error to specify which subcommand was
128*b0d29bc4SBrooks Davis ///     affected.
129*b0d29bc4SBrooks Davis /// \throw std::exception This propagates any uncaught exception.  Such
130*b0d29bc4SBrooks Davis ///     exceptions are bugs, but we let them propagate so that the runtime will
131*b0d29bc4SBrooks Davis ///     abort and dump core.
132*b0d29bc4SBrooks Davis static int
run_subcommand(cmdline::ui * ui,cli::cli_command * command,const cmdline::args_vector & args,const config::tree & user_config)133*b0d29bc4SBrooks Davis run_subcommand(cmdline::ui* ui, cli::cli_command* command,
134*b0d29bc4SBrooks Davis                const cmdline::args_vector& args,
135*b0d29bc4SBrooks Davis                const config::tree& user_config)
136*b0d29bc4SBrooks Davis {
137*b0d29bc4SBrooks Davis     try {
138*b0d29bc4SBrooks Davis         PRE(command->name() == args[0]);
139*b0d29bc4SBrooks Davis         return command->main(ui, args, user_config);
140*b0d29bc4SBrooks Davis     } catch (const cmdline::usage_error& e) {
141*b0d29bc4SBrooks Davis         throw std::pair< std::string, cmdline::usage_error >(
142*b0d29bc4SBrooks Davis             command->name(), e);
143*b0d29bc4SBrooks Davis     }
144*b0d29bc4SBrooks Davis }
145*b0d29bc4SBrooks Davis 
146*b0d29bc4SBrooks Davis 
147*b0d29bc4SBrooks Davis /// Exception-safe version of main.
148*b0d29bc4SBrooks Davis ///
149*b0d29bc4SBrooks Davis /// This function provides the real meat of the entry point of the program.  It
150*b0d29bc4SBrooks Davis /// is allowed to throw some known exceptions which are parsed by the caller.
151*b0d29bc4SBrooks Davis /// Doing so keeps this function simpler and allow tests to actually validate
152*b0d29bc4SBrooks Davis /// that the errors reported are accurate.
153*b0d29bc4SBrooks Davis ///
154*b0d29bc4SBrooks Davis /// \return The exit code of the program.  Should be EXIT_SUCCESS on success and
155*b0d29bc4SBrooks Davis /// EXIT_FAILURE on failure.  The caller extends this to additional integers for
156*b0d29bc4SBrooks Davis /// errors reported through exceptions.
157*b0d29bc4SBrooks Davis ///
158*b0d29bc4SBrooks Davis /// \param ui Object to interact with the I/O of the program.
159*b0d29bc4SBrooks Davis /// \param argc The number of arguments passed on the command line.
160*b0d29bc4SBrooks Davis /// \param argv NULL-terminated array containing the command line arguments.
161*b0d29bc4SBrooks Davis /// \param mock_command An extra command provided for testing purposes; should
162*b0d29bc4SBrooks Davis ///     just be NULL other than for tests.
163*b0d29bc4SBrooks Davis ///
164*b0d29bc4SBrooks Davis /// \throw cmdline::usage_error If the user ran the program with invalid
165*b0d29bc4SBrooks Davis ///     arguments.
166*b0d29bc4SBrooks Davis /// \throw std::exception This propagates any uncaught exception.  Such
167*b0d29bc4SBrooks Davis ///     exceptions are bugs, but we let them propagate so that the runtime will
168*b0d29bc4SBrooks Davis ///     abort and dump core.
169*b0d29bc4SBrooks Davis static int
safe_main(cmdline::ui * ui,int argc,const char * const argv[],cli::cli_command_ptr mock_command)170*b0d29bc4SBrooks Davis safe_main(cmdline::ui* ui, int argc, const char* const argv[],
171*b0d29bc4SBrooks Davis           cli::cli_command_ptr mock_command)
172*b0d29bc4SBrooks Davis {
173*b0d29bc4SBrooks Davis     cmdline::options_vector options;
174*b0d29bc4SBrooks Davis     options.push_back(&cli::config_option);
175*b0d29bc4SBrooks Davis     options.push_back(&cli::variable_option);
176*b0d29bc4SBrooks Davis     const cmdline::string_option loglevel_option(
177*b0d29bc4SBrooks Davis         "loglevel", "Level of the messages to log", "level", "info");
178*b0d29bc4SBrooks Davis     options.push_back(&loglevel_option);
179*b0d29bc4SBrooks Davis     const cmdline::path_option logfile_option(
180*b0d29bc4SBrooks Davis         "logfile", "Path to the log file", "file",
181*b0d29bc4SBrooks Davis         cli::detail::default_log_name().c_str());
182*b0d29bc4SBrooks Davis     options.push_back(&logfile_option);
183*b0d29bc4SBrooks Davis 
184*b0d29bc4SBrooks Davis     cmdline::commands_map< cli::cli_command > commands;
185*b0d29bc4SBrooks Davis 
186*b0d29bc4SBrooks Davis     commands.insert(new cli::cmd_about());
187*b0d29bc4SBrooks Davis     commands.insert(new cli::cmd_config());
188*b0d29bc4SBrooks Davis     commands.insert(new cli::cmd_db_exec());
189*b0d29bc4SBrooks Davis     commands.insert(new cli::cmd_db_migrate());
190*b0d29bc4SBrooks Davis     commands.insert(new cli::cmd_help(&options, &commands));
191*b0d29bc4SBrooks Davis 
192*b0d29bc4SBrooks Davis     commands.insert(new cli::cmd_debug(), "Workspace");
193*b0d29bc4SBrooks Davis     commands.insert(new cli::cmd_list(), "Workspace");
194*b0d29bc4SBrooks Davis     commands.insert(new cli::cmd_test(), "Workspace");
195*b0d29bc4SBrooks Davis 
196*b0d29bc4SBrooks Davis     commands.insert(new cli::cmd_report(), "Reporting");
197*b0d29bc4SBrooks Davis     commands.insert(new cli::cmd_report_html(), "Reporting");
198*b0d29bc4SBrooks Davis     commands.insert(new cli::cmd_report_junit(), "Reporting");
199*b0d29bc4SBrooks Davis 
200*b0d29bc4SBrooks Davis     if (mock_command.get() != NULL)
201*b0d29bc4SBrooks Davis         commands.insert(mock_command);
202*b0d29bc4SBrooks Davis 
203*b0d29bc4SBrooks Davis     const cmdline::parsed_cmdline cmdline = cmdline::parse(argc, argv, options);
204*b0d29bc4SBrooks Davis 
205*b0d29bc4SBrooks Davis     const fs::path logfile(cmdline.get_option< cmdline::path_option >(
206*b0d29bc4SBrooks Davis         "logfile"));
207*b0d29bc4SBrooks Davis     fs::mkdir_p(logfile.branch_path(), 0755);
208*b0d29bc4SBrooks Davis     LD(F("Log file is %s") % logfile);
209*b0d29bc4SBrooks Davis     utils::install_crash_handlers(logfile.str());
210*b0d29bc4SBrooks Davis     try {
211*b0d29bc4SBrooks Davis         logging::set_persistency(cmdline.get_option< cmdline::string_option >(
212*b0d29bc4SBrooks Davis             "loglevel"), logfile);
213*b0d29bc4SBrooks Davis     } catch (const std::range_error& e) {
214*b0d29bc4SBrooks Davis         throw cmdline::usage_error(e.what());
215*b0d29bc4SBrooks Davis     }
216*b0d29bc4SBrooks Davis 
217*b0d29bc4SBrooks Davis     if (cmdline.arguments().empty())
218*b0d29bc4SBrooks Davis         throw cmdline::usage_error("No command provided");
219*b0d29bc4SBrooks Davis     const std::string cmdname = cmdline.arguments()[0];
220*b0d29bc4SBrooks Davis 
221*b0d29bc4SBrooks Davis     const config::tree user_config = cli::load_config(cmdline,
222*b0d29bc4SBrooks Davis                                                       cmdname != "help");
223*b0d29bc4SBrooks Davis 
224*b0d29bc4SBrooks Davis     cli::cli_command* command = commands.find(cmdname);
225*b0d29bc4SBrooks Davis     if (command == NULL)
226*b0d29bc4SBrooks Davis         throw cmdline::usage_error(F("Unknown command '%s'") % cmdname);
227*b0d29bc4SBrooks Davis     register_scheduler_interfaces();
228*b0d29bc4SBrooks Davis     return run_subcommand(ui, command, cmdline.arguments(), user_config);
229*b0d29bc4SBrooks Davis }
230*b0d29bc4SBrooks Davis 
231*b0d29bc4SBrooks Davis 
232*b0d29bc4SBrooks Davis }  // anonymous namespace
233*b0d29bc4SBrooks Davis 
234*b0d29bc4SBrooks Davis 
235*b0d29bc4SBrooks Davis /// Gets the name of the default log file.
236*b0d29bc4SBrooks Davis ///
237*b0d29bc4SBrooks Davis /// \return The path to the log file.
238*b0d29bc4SBrooks Davis fs::path
default_log_name(void)239*b0d29bc4SBrooks Davis cli::detail::default_log_name(void)
240*b0d29bc4SBrooks Davis {
241*b0d29bc4SBrooks Davis     // Update doc/troubleshooting.texi if you change this algorithm.
242*b0d29bc4SBrooks Davis     const optional< std::string > home(utils::getenv("HOME"));
243*b0d29bc4SBrooks Davis     if (home) {
244*b0d29bc4SBrooks Davis         return logging::generate_log_name(fs::path(home.get()) / ".kyua" /
245*b0d29bc4SBrooks Davis                                           "logs", cmdline::progname());
246*b0d29bc4SBrooks Davis     } else {
247*b0d29bc4SBrooks Davis         const optional< std::string > tmpdir(utils::getenv("TMPDIR"));
248*b0d29bc4SBrooks Davis         if (tmpdir) {
249*b0d29bc4SBrooks Davis             return logging::generate_log_name(fs::path(tmpdir.get()),
250*b0d29bc4SBrooks Davis                                               cmdline::progname());
251*b0d29bc4SBrooks Davis         } else {
252*b0d29bc4SBrooks Davis             return logging::generate_log_name(fs::path("/tmp"),
253*b0d29bc4SBrooks Davis                                               cmdline::progname());
254*b0d29bc4SBrooks Davis         }
255*b0d29bc4SBrooks Davis     }
256*b0d29bc4SBrooks Davis }
257*b0d29bc4SBrooks Davis 
258*b0d29bc4SBrooks Davis 
259*b0d29bc4SBrooks Davis /// Testable entry point, with catch-all exception handlers.
260*b0d29bc4SBrooks Davis ///
261*b0d29bc4SBrooks Davis /// This entry point does not perform any initialization of global state; it is
262*b0d29bc4SBrooks Davis /// provided to allow unit-testing of the utility's entry point.
263*b0d29bc4SBrooks Davis ///
264*b0d29bc4SBrooks Davis /// \param ui Object to interact with the I/O of the program.
265*b0d29bc4SBrooks Davis /// \param argc The number of arguments passed on the command line.
266*b0d29bc4SBrooks Davis /// \param argv NULL-terminated array containing the command line arguments.
267*b0d29bc4SBrooks Davis /// \param mock_command An extra command provided for testing purposes; should
268*b0d29bc4SBrooks Davis ///     just be NULL other than for tests.
269*b0d29bc4SBrooks Davis ///
270*b0d29bc4SBrooks Davis /// \return 0 on success, some other integer on error.
271*b0d29bc4SBrooks Davis ///
272*b0d29bc4SBrooks Davis /// \throw std::exception This propagates any uncaught exception.  Such
273*b0d29bc4SBrooks Davis ///     exceptions are bugs, but we let them propagate so that the runtime will
274*b0d29bc4SBrooks Davis ///     abort and dump core.
275*b0d29bc4SBrooks Davis int
main(cmdline::ui * ui,const int argc,const char * const * const argv,cli_command_ptr mock_command)276*b0d29bc4SBrooks Davis cli::main(cmdline::ui* ui, const int argc, const char* const* const argv,
277*b0d29bc4SBrooks Davis           cli_command_ptr mock_command)
278*b0d29bc4SBrooks Davis {
279*b0d29bc4SBrooks Davis     try {
280*b0d29bc4SBrooks Davis         const int exit_code = safe_main(ui, argc, argv, mock_command);
281*b0d29bc4SBrooks Davis 
282*b0d29bc4SBrooks Davis         // Codes above 1 are reserved to report conditions captured as
283*b0d29bc4SBrooks Davis         // exceptions below.
284*b0d29bc4SBrooks Davis         INV(exit_code == EXIT_SUCCESS || exit_code == EXIT_FAILURE);
285*b0d29bc4SBrooks Davis 
286*b0d29bc4SBrooks Davis         return exit_code;
287*b0d29bc4SBrooks Davis     } catch (const signals::interrupted_error& e) {
288*b0d29bc4SBrooks Davis         cmdline::print_error(ui, F("%s.") % e.what());
289*b0d29bc4SBrooks Davis         // Re-deliver the interruption signal to self so that we terminate with
290*b0d29bc4SBrooks Davis         // the right status.  At this point we should NOT have any custom signal
291*b0d29bc4SBrooks Davis         // handlers in place.
292*b0d29bc4SBrooks Davis         ::kill(getpid(), e.signo());
293*b0d29bc4SBrooks Davis         LD("Interrupt signal re-delivery did not terminate program");
294*b0d29bc4SBrooks Davis         // If we reach this, something went wrong because we did not exit as
295*b0d29bc4SBrooks Davis         // intended.  Return an internal error instead.  (Would be nicer to
296*b0d29bc4SBrooks Davis         // abort in principle, but it wouldn't be a nice experience if it ever
297*b0d29bc4SBrooks Davis         // happened.)
298*b0d29bc4SBrooks Davis         return 2;
299*b0d29bc4SBrooks Davis     } catch (const std::pair< std::string, cmdline::usage_error >& e) {
300*b0d29bc4SBrooks Davis         const std::string message = F("Usage error for command %s: %s.") %
301*b0d29bc4SBrooks Davis             e.first % e.second.what();
302*b0d29bc4SBrooks Davis         LE(message);
303*b0d29bc4SBrooks Davis         ui->err(message);
304*b0d29bc4SBrooks Davis         ui->err(F("Type '%s help %s' for usage information.") %
305*b0d29bc4SBrooks Davis                 cmdline::progname() % e.first);
306*b0d29bc4SBrooks Davis         return 3;
307*b0d29bc4SBrooks Davis     } catch (const cmdline::usage_error& e) {
308*b0d29bc4SBrooks Davis         const std::string message = F("Usage error: %s.") % e.what();
309*b0d29bc4SBrooks Davis         LE(message);
310*b0d29bc4SBrooks Davis         ui->err(message);
311*b0d29bc4SBrooks Davis         ui->err(F("Type '%s help' for usage information.") %
312*b0d29bc4SBrooks Davis                 cmdline::progname());
313*b0d29bc4SBrooks Davis         return 3;
314*b0d29bc4SBrooks Davis     } catch (const store::old_schema_error& e) {
315*b0d29bc4SBrooks Davis         const std::string message = F("The database has schema version %s, "
316*b0d29bc4SBrooks Davis                                       "which is too old; please use db-migrate "
317*b0d29bc4SBrooks Davis                                       "to upgrade it.") % e.old_version();
318*b0d29bc4SBrooks Davis         cmdline::print_error(ui, message);
319*b0d29bc4SBrooks Davis         return 2;
320*b0d29bc4SBrooks Davis     } catch (const std::runtime_error& e) {
321*b0d29bc4SBrooks Davis         cmdline::print_error(ui, F("%s.") % e.what());
322*b0d29bc4SBrooks Davis         return 2;
323*b0d29bc4SBrooks Davis     }
324*b0d29bc4SBrooks Davis }
325*b0d29bc4SBrooks Davis 
326*b0d29bc4SBrooks Davis 
327*b0d29bc4SBrooks Davis /// Delegate for ::main().
328*b0d29bc4SBrooks Davis ///
329*b0d29bc4SBrooks Davis /// This function is supposed to be called directly from the top-level ::main()
330*b0d29bc4SBrooks Davis /// function.  It takes care of initializing internal libraries and then calls
331*b0d29bc4SBrooks Davis /// main(ui, argc, argv).
332*b0d29bc4SBrooks Davis ///
333*b0d29bc4SBrooks Davis /// \pre This function can only be called once.
334*b0d29bc4SBrooks Davis ///
335*b0d29bc4SBrooks Davis /// \throw std::exception This propagates any uncaught exception.  Such
336*b0d29bc4SBrooks Davis ///     exceptions are bugs, but we let them propagate so that the runtime will
337*b0d29bc4SBrooks Davis ///     abort and dump core.
338*b0d29bc4SBrooks Davis int
main(const int argc,const char * const * const argv)339*b0d29bc4SBrooks Davis cli::main(const int argc, const char* const* const argv)
340*b0d29bc4SBrooks Davis {
341*b0d29bc4SBrooks Davis     logging::set_inmemory();
342*b0d29bc4SBrooks Davis 
343*b0d29bc4SBrooks Davis     LI(F("%s %s") % PACKAGE % VERSION);
344*b0d29bc4SBrooks Davis 
345*b0d29bc4SBrooks Davis     std::string plain_args;
346*b0d29bc4SBrooks Davis     for (const char* const* arg = argv; *arg != NULL; arg++)
347*b0d29bc4SBrooks Davis         plain_args += F(" %s") % *arg;
348*b0d29bc4SBrooks Davis     LI(F("Command line:%s") % plain_args);
349*b0d29bc4SBrooks Davis 
350*b0d29bc4SBrooks Davis     cmdline::init(argv[0]);
351*b0d29bc4SBrooks Davis     cmdline::ui ui;
352*b0d29bc4SBrooks Davis 
353*b0d29bc4SBrooks Davis     const int exit_code = main(&ui, argc, argv);
354*b0d29bc4SBrooks Davis     LI(F("Clean exit with code %s") % exit_code);
355*b0d29bc4SBrooks Davis     return exit_code;
356*b0d29bc4SBrooks Davis }
357