xref: /netbsd-src/external/bsd/kyua-cli/dist/cli/cmd_about.cpp (revision 6b3a42af15b5e090c339512c790dd68f3d11a9d8)
1*6b3a42afSjmmv // Copyright 2010 Google Inc.
2*6b3a42afSjmmv // All rights reserved.
3*6b3a42afSjmmv //
4*6b3a42afSjmmv // Redistribution and use in source and binary forms, with or without
5*6b3a42afSjmmv // modification, are permitted provided that the following conditions are
6*6b3a42afSjmmv // met:
7*6b3a42afSjmmv //
8*6b3a42afSjmmv // * Redistributions of source code must retain the above copyright
9*6b3a42afSjmmv //   notice, this list of conditions and the following disclaimer.
10*6b3a42afSjmmv // * Redistributions in binary form must reproduce the above copyright
11*6b3a42afSjmmv //   notice, this list of conditions and the following disclaimer in the
12*6b3a42afSjmmv //   documentation and/or other materials provided with the distribution.
13*6b3a42afSjmmv // * Neither the name of Google Inc. nor the names of its contributors
14*6b3a42afSjmmv //   may be used to endorse or promote products derived from this software
15*6b3a42afSjmmv //   without specific prior written permission.
16*6b3a42afSjmmv //
17*6b3a42afSjmmv // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*6b3a42afSjmmv // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*6b3a42afSjmmv // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*6b3a42afSjmmv // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*6b3a42afSjmmv // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*6b3a42afSjmmv // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*6b3a42afSjmmv // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*6b3a42afSjmmv // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*6b3a42afSjmmv // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*6b3a42afSjmmv // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*6b3a42afSjmmv // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*6b3a42afSjmmv 
29*6b3a42afSjmmv #include "cli/cmd_about.hpp"
30*6b3a42afSjmmv 
31*6b3a42afSjmmv #include <cstdlib>
32*6b3a42afSjmmv #include <fstream>
33*6b3a42afSjmmv #include <utility>
34*6b3a42afSjmmv #include <vector>
35*6b3a42afSjmmv 
36*6b3a42afSjmmv #include "cli/common.ipp"
37*6b3a42afSjmmv #include "utils/cmdline/exceptions.hpp"
38*6b3a42afSjmmv #include "utils/cmdline/parser.ipp"
39*6b3a42afSjmmv #include "utils/cmdline/ui.hpp"
40*6b3a42afSjmmv #include "utils/defs.hpp"
41*6b3a42afSjmmv #include "utils/env.hpp"
42*6b3a42afSjmmv #include "utils/format/macros.hpp"
43*6b3a42afSjmmv #include "utils/fs/path.hpp"
44*6b3a42afSjmmv #include "utils/sanity.hpp"
45*6b3a42afSjmmv 
46*6b3a42afSjmmv #if defined(HAVE_CONFIG_H)
47*6b3a42afSjmmv #   include "config.h"
48*6b3a42afSjmmv #endif
49*6b3a42afSjmmv 
50*6b3a42afSjmmv namespace cmdline = utils::cmdline;
51*6b3a42afSjmmv namespace config = utils::config;
52*6b3a42afSjmmv namespace fs = utils::fs;
53*6b3a42afSjmmv 
54*6b3a42afSjmmv using cli::cmd_about;
55*6b3a42afSjmmv 
56*6b3a42afSjmmv 
57*6b3a42afSjmmv namespace {
58*6b3a42afSjmmv 
59*6b3a42afSjmmv 
60*6b3a42afSjmmv /// Print the contents of a document.
61*6b3a42afSjmmv ///
62*6b3a42afSjmmv /// If the file cannot be opened for whatever reason, an error message is
63*6b3a42afSjmmv /// printed to the output of the program instead of the contents of the file.
64*6b3a42afSjmmv ///
65*6b3a42afSjmmv /// \param ui Object to interact with the I/O of the program.
66*6b3a42afSjmmv /// \param file The file to print.
67*6b3a42afSjmmv ///
68*6b3a42afSjmmv /// \return True if the file was printed, false otherwise.
69*6b3a42afSjmmv static bool
cat_file(cmdline::ui * ui,const fs::path & file)70*6b3a42afSjmmv cat_file(cmdline::ui* ui, const fs::path& file)
71*6b3a42afSjmmv {
72*6b3a42afSjmmv     std::ifstream input(file.c_str());
73*6b3a42afSjmmv     if (!input) {
74*6b3a42afSjmmv         ui->err(F("Failed to open %s") % file);
75*6b3a42afSjmmv         return false;
76*6b3a42afSjmmv     }
77*6b3a42afSjmmv 
78*6b3a42afSjmmv     std::string line;
79*6b3a42afSjmmv     while (std::getline(input, line).good())
80*6b3a42afSjmmv         ui->out(line);
81*6b3a42afSjmmv     input.close();
82*6b3a42afSjmmv     return true;
83*6b3a42afSjmmv }
84*6b3a42afSjmmv 
85*6b3a42afSjmmv 
86*6b3a42afSjmmv }  // anonymous namespace
87*6b3a42afSjmmv 
88*6b3a42afSjmmv 
89*6b3a42afSjmmv /// Default constructor for cmd_about.
cmd_about(void)90*6b3a42afSjmmv cmd_about::cmd_about(void) : cli_command(
91*6b3a42afSjmmv     "about", "[authors|license|version]", 0, 1,
92*6b3a42afSjmmv     "Shows general program information")
93*6b3a42afSjmmv {
94*6b3a42afSjmmv }
95*6b3a42afSjmmv 
96*6b3a42afSjmmv 
97*6b3a42afSjmmv /// Entry point for the "about" subcommand.
98*6b3a42afSjmmv ///
99*6b3a42afSjmmv /// \param ui Object to interact with the I/O of the program.
100*6b3a42afSjmmv /// \param cmdline Representation of the command line to the subcommand.
101*6b3a42afSjmmv /// \param unused_user_config The runtime configuration of the program.
102*6b3a42afSjmmv ///
103*6b3a42afSjmmv /// \return 0 if everything is OK, 1 if any of the necessary documents cannot be
104*6b3a42afSjmmv /// opened.
105*6b3a42afSjmmv int
run(cmdline::ui * ui,const cmdline::parsed_cmdline & cmdline,const config::tree & UTILS_UNUSED_PARAM (user_config))106*6b3a42afSjmmv cmd_about::run(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline,
107*6b3a42afSjmmv                const config::tree& UTILS_UNUSED_PARAM(user_config))
108*6b3a42afSjmmv {
109*6b3a42afSjmmv     const fs::path docdir(utils::getenv_with_default(
110*6b3a42afSjmmv         "KYUA_DOCDIR", KYUA_DOCDIR));
111*6b3a42afSjmmv 
112*6b3a42afSjmmv     bool success = true;
113*6b3a42afSjmmv 
114*6b3a42afSjmmv     if (cmdline.arguments().empty()) {
115*6b3a42afSjmmv         ui->out(PACKAGE " (" PACKAGE_NAME ") " PACKAGE_VERSION);
116*6b3a42afSjmmv         ui->out("");
117*6b3a42afSjmmv         ui->out("License terms:");
118*6b3a42afSjmmv         ui->out("");
119*6b3a42afSjmmv         success &= cat_file(ui, docdir / "COPYING");
120*6b3a42afSjmmv         ui->out("");
121*6b3a42afSjmmv         ui->out("Brought to you by:");
122*6b3a42afSjmmv         ui->out("");
123*6b3a42afSjmmv         success &= cat_file(ui, docdir / "AUTHORS");
124*6b3a42afSjmmv         ui->out("");
125*6b3a42afSjmmv         ui->out(F("Homepage: %s") % PACKAGE_URL);
126*6b3a42afSjmmv     } else {
127*6b3a42afSjmmv         const std::string& topic = cmdline.arguments()[0];
128*6b3a42afSjmmv 
129*6b3a42afSjmmv         if (topic == "authors") {
130*6b3a42afSjmmv             success &= cat_file(ui, docdir / "AUTHORS");
131*6b3a42afSjmmv         } else if (topic == "license") {
132*6b3a42afSjmmv             success &= cat_file(ui, docdir / "COPYING");
133*6b3a42afSjmmv         } else if (topic == "version") {
134*6b3a42afSjmmv             ui->out(PACKAGE " (" PACKAGE_NAME ") " PACKAGE_VERSION);
135*6b3a42afSjmmv         } else {
136*6b3a42afSjmmv             throw cmdline::usage_error(F("Invalid about topic '%s'") % topic);
137*6b3a42afSjmmv         }
138*6b3a42afSjmmv     }
139*6b3a42afSjmmv 
140*6b3a42afSjmmv     return success ? EXIT_SUCCESS : EXIT_FAILURE;
141*6b3a42afSjmmv }
142