Improve the process for GNU tools
[minix3.git] / external / bsd / kyua-cli / dist / cli / cmd_about.cpp
blob6302c85ad50afc6859ca3b43ac0e18df3a7ff538
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.
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"
31 #include <cstdlib>
32 #include <fstream>
33 #include <utility>
34 #include <vector>
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)
47 # include "config.h"
48 #endif
50 namespace cmdline = utils::cmdline;
51 namespace config = utils::config;
52 namespace fs = utils::fs;
54 using cli::cmd_about;
57 namespace {
60 /// Print the contents of a document.
61 ///
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.
64 ///
65 /// \param ui Object to interact with the I/O of the program.
66 /// \param file The file to print.
67 ///
68 /// \return True if the file was printed, false otherwise.
69 static bool
70 cat_file(cmdline::ui* ui, const fs::path& file)
72 std::ifstream input(file.c_str());
73 if (!input) {
74 ui->err(F("Failed to open %s") % file);
75 return false;
78 std::string line;
79 while (std::getline(input, line).good())
80 ui->out(line);
81 input.close();
82 return true;
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.
98 ///
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
104 /// opened.
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));
112 bool success = true;
114 if (cmdline.arguments().empty()) {
115 ui->out(PACKAGE " (" PACKAGE_NAME ") " PACKAGE_VERSION);
116 ui->out("");
117 ui->out("License terms:");
118 ui->out("");
119 success &= cat_file(ui, docdir / "COPYING");
120 ui->out("");
121 ui->out("Brought to you by:");
122 ui->out("");
123 success &= cat_file(ui, docdir / "AUTHORS");
124 ui->out("");
125 ui->out(F("Homepage: %s") % PACKAGE_URL);
126 } else {
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);
135 } else {
136 throw cmdline::usage_error(F("Invalid about topic '%s'") % topic);
140 return success ? EXIT_SUCCESS : EXIT_FAILURE;