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/cmd_about.hpp"
36 #include "cli/common.ipp"
37 #include "utils/cmdline/exceptions.hpp"
38 #include "utils/cmdline/parser.ipp"
39 #include "utils/cmdline/ui.hpp"
40 #include "utils/defs.hpp"
41 #include "utils/env.hpp"
42 #include "utils/format/macros.hpp"
43 #include "utils/fs/path.hpp"
44 #include "utils/sanity.hpp"
46 #if defined(HAVE_CONFIG_H)
50 namespace cmdline
= utils::cmdline
;
51 namespace config
= utils::config
;
52 namespace fs
= utils::fs
;
60 /// Print the contents of a document.
62 /// If the file cannot be opened for whatever reason, an error message is
63 /// printed to the output of the program instead of the contents of the file.
65 /// \param ui Object to interact with the I/O of the program.
66 /// \param file The file to print.
68 /// \return True if the file was printed, false otherwise.
70 cat_file(cmdline::ui
* ui
, const fs::path
& file
)
72 std::ifstream
input(file
.c_str());
74 ui
->err(F("Failed to open %s") % file
);
79 while (std::getline(input
, line
).good())
86 } // anonymous namespace
89 /// Default constructor for cmd_about.
90 cmd_about::cmd_about(void) : cli_command(
91 "about", "[authors|license|version]", 0, 1,
92 "Shows general program information")
97 /// Entry point for the "about" subcommand.
99 /// \param ui Object to interact with the I/O of the program.
100 /// \param cmdline Representation of the command line to the subcommand.
101 /// \param unused_user_config The runtime configuration of the program.
103 /// \return 0 if everything is OK, 1 if any of the necessary documents cannot be
106 cmd_about::run(cmdline::ui
* ui
, const cmdline::parsed_cmdline
& cmdline
,
107 const config::tree
& UTILS_UNUSED_PARAM(user_config
))
109 const fs::path
docdir(utils::getenv_with_default(
110 "KYUA_DOCDIR", KYUA_DOCDIR
));
114 if (cmdline
.arguments().empty()) {
115 ui
->out(PACKAGE
" (" PACKAGE_NAME
") " PACKAGE_VERSION
);
117 ui
->out("License terms:");
119 success
&= cat_file(ui
, docdir
/ "COPYING");
121 ui
->out("Brought to you by:");
123 success
&= cat_file(ui
, docdir
/ "AUTHORS");
125 ui
->out(F("Homepage: %s") % PACKAGE_URL
);
127 const std::string
& topic
= cmdline
.arguments()[0];
129 if (topic
== "authors") {
130 success
&= cat_file(ui
, docdir
/ "AUTHORS");
131 } else if (topic
== "license") {
132 success
&= cat_file(ui
, docdir
/ "COPYING");
133 } else if (topic
== "version") {
134 ui
->out(PACKAGE
" (" PACKAGE_NAME
") " PACKAGE_VERSION
);
136 throw cmdline::usage_error(F("Invalid about topic '%s'") % topic
);
140 return success
? EXIT_SUCCESS
: EXIT_FAILURE
;