xref: /netbsd-src/external/bsd/kyua-cli/dist/cli/cmd_about.cpp (revision 6b3a42af15b5e090c339512c790dd68f3d11a9d8)
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/cmd_about.hpp"
30 
31 #include <cstdlib>
32 #include <fstream>
33 #include <utility>
34 #include <vector>
35 
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"
45 
46 #if defined(HAVE_CONFIG_H)
47 #   include "config.h"
48 #endif
49 
50 namespace cmdline = utils::cmdline;
51 namespace config = utils::config;
52 namespace fs = utils::fs;
53 
54 using cli::cmd_about;
55 
56 
57 namespace {
58 
59 
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
cat_file(cmdline::ui * ui,const fs::path & file)70 cat_file(cmdline::ui* ui, const fs::path& file)
71 {
72     std::ifstream input(file.c_str());
73     if (!input) {
74         ui->err(F("Failed to open %s") % file);
75         return false;
76     }
77 
78     std::string line;
79     while (std::getline(input, line).good())
80         ui->out(line);
81     input.close();
82     return true;
83 }
84 
85 
86 }  // anonymous namespace
87 
88 
89 /// Default constructor for cmd_about.
cmd_about(void)90 cmd_about::cmd_about(void) : cli_command(
91     "about", "[authors|license|version]", 0, 1,
92     "Shows general program information")
93 {
94 }
95 
96 
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.
102 ///
103 /// \return 0 if everything is OK, 1 if any of the necessary documents cannot be
104 /// opened.
105 int
run(cmdline::ui * ui,const cmdline::parsed_cmdline & cmdline,const config::tree & UTILS_UNUSED_PARAM (user_config))106 cmd_about::run(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline,
107                const config::tree& UTILS_UNUSED_PARAM(user_config))
108 {
109     const fs::path docdir(utils::getenv_with_default(
110         "KYUA_DOCDIR", KYUA_DOCDIR));
111 
112     bool success = true;
113 
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];
128 
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);
137         }
138     }
139 
140     return success ? EXIT_SUCCESS : EXIT_FAILURE;
141 }
142