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