1 // Copyright 2010 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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.
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.
29 #include "cli/main.hpp"
31 #if defined(HAVE_CONFIG_H)
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"
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
;
82 using utils::optional
;
88 /// Executes the given subcommand with proper usage_error reporting.
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.
96 /// \return The exit code of the command. Typically 0 on success, some other
97 /// integer otherwise.
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
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.
107 run_subcommand(cmdline::ui
* ui
, cli::cli_command
* command
,
108 const cmdline::args_vector
& args
,
109 const config::tree
& user_config
)
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
>(
121 /// Exception-safe version of main.
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.
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.
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.
138 /// \throw cmdline::usage_error If the user ran the program with invalid
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.
144 safe_main(cmdline::ui
* ui
, int argc
, const char* const argv
[],
145 cli::cli_command_ptr mock_command
)
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
);
158 cmdline::commands_map
< cli::cli_command
> commands
;
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
));
166 commands
.insert(new cli::cmd_debug(), "Workspace");
167 commands
.insert(new cli::cmd_list(), "Workspace");
168 commands
.insert(new cli::cmd_test(), "Workspace");
170 commands
.insert(new cli::cmd_report(), "Reporting");
171 commands
.insert(new cli::cmd_report_html(), "Reporting");
173 if (mock_command
.get() != NULL
)
174 commands
.insert(mock_command
);
176 const cmdline::parsed_cmdline cmdline
= cmdline::parse(argc
, argv
, options
);
178 const fs::path
logfile(cmdline
.get_option
< cmdline::path_option
>(
180 fs::mkdir_p(logfile
.branch_path(), 0755);
181 LD(F("Log file is %s") % logfile
);
182 utils::install_crash_handlers(logfile
.str());
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());
190 if (cmdline
.arguments().empty())
191 throw cmdline::usage_error("No command provided");
192 const std::string cmdname
= cmdline
.arguments()[0];
194 const config::tree user_config
= cli::load_config(cmdline
,
197 cli::cli_command
* command
= commands
.find(cmdname
);
199 throw cmdline::usage_error(F("Unknown command '%s'") % cmdname
);
200 return run_subcommand(ui
, command
, cmdline
.arguments(), user_config
);
204 } // anonymous namespace
207 /// Gets the name of the default log file.
209 /// \return The path to the log file.
211 cli::detail::default_log_name(void)
213 // Update doc/troubleshooting.texi if you change this algorithm.
214 const optional
< std::string
> home(utils::getenv("HOME"));
216 return logging::generate_log_name(fs::path(home
.get()) / ".kyua" /
217 "logs", cmdline::progname());
219 const optional
< std::string
> tmpdir(utils::getenv("TMPDIR"));
221 return logging::generate_log_name(fs::path(tmpdir
.get()),
222 cmdline::progname());
224 return logging::generate_log_name(fs::path("/tmp"),
225 cmdline::progname());
231 /// Testable entry point, with catch-all exception handlers.
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.
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.
242 /// \return 0 on success, some other integer on error.
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.
248 cli::main(cmdline::ui
* ui
, const int argc
, const char* const* const argv
,
249 cli_command_ptr mock_command
)
252 const int exit_code
= safe_main(ui
, argc
, argv
, mock_command
);
254 // Codes above 1 are reserved to report conditions captured as
256 INV(exit_code
== EXIT_SUCCESS
|| exit_code
== EXIT_FAILURE
);
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
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();
276 ui
->err(F("Type '%s help %s' for usage information.") %
277 cmdline::progname() % e
.first
);
279 } catch (const cmdline::usage_error
& e
) {
280 const std::string message
= F("Usage error: %s.") % e
.what();
283 ui
->err(F("Type '%s help' for usage information.") %
284 cmdline::progname());
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
);
292 } catch (const std::runtime_error
& e
) {
293 cmdline::print_error(ui
, e
.what());
299 /// Delegate for ::main().
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).
305 /// \pre This function can only be called once.
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.
311 cli::main(const int argc
, const char* const* const argv
)
313 logging::set_inmemory();
315 LI(F("%s %s") % PACKAGE
% VERSION
);
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
);
322 cmdline::init(argv
[0]);
325 const int exit_code
= main(&ui
, argc
, argv
);
326 LI(F("Clean exit with code %s") % exit_code
);