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