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