xref: /freebsd/contrib/kyua/cli/cmd_about.cpp (revision fe815331bb40604ba31312acf7e4619674631777)
1 // Copyright 2010 The Kyua Authors.
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 #include "utils/text/regex.hpp"
46 
47 #if defined(HAVE_CONFIG_H)
48 #   include "config.h"
49 #endif
50 
51 namespace cmdline = utils::cmdline;
52 namespace config = utils::config;
53 namespace fs = utils::fs;
54 namespace text = utils::text;
55 
56 using cli::cmd_about;
57 
58 
59 namespace {
60 
61 
62 /// Print the contents of a document.
63 ///
64 /// If the file cannot be opened for whatever reason, an error message is
65 /// printed to the output of the program instead of the contents of the file.
66 ///
67 /// \param ui Object to interact with the I/O of the program.
68 /// \param file The file to print.
69 /// \param filter_re Regular expression to match the lines to print.  If empty,
70 ///     no filtering is applied.
71 ///
72 /// \return True if the file was printed, false otherwise.
73 static bool
74 cat_file(cmdline::ui* ui, const fs::path& file,
75          const std::string& filter_re = "")
76 {
77     std::ifstream input(file.c_str());
78     if (!input) {
79         ui->err(F("Failed to open %s") % file);
80         return false;
81     }
82 
83     std::string line;
84     if (filter_re.empty()) {
85         while (std::getline(input, line).good()) {
86             ui->out(line);
87         }
88     } else {
89         const text::regex filter = text::regex::compile(filter_re, 0);
90         while (std::getline(input, line).good()) {
91             if (filter.match(line)) {
92                 ui->out(line);
93             }
94         }
95     }
96     input.close();
97     return true;
98 }
99 
100 
101 }  // anonymous namespace
102 
103 
104 /// Default constructor for cmd_about.
105 cmd_about::cmd_about(void) : cli_command(
106     "about", "[authors|license|version]", 0, 1,
107     "Shows detailed authors and contributors; license; and version information")
108 {
109 }
110 
111 
112 /// Entry point for the "about" subcommand.
113 ///
114 /// \param ui Object to interact with the I/O of the program.
115 /// \param cmdline Representation of the command line to the subcommand.
116 ///
117 /// \return 0 if everything is OK, 1 if any of the necessary documents cannot be
118 /// opened.
119 int
120 cmd_about::run(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline,
121                const config::tree& /* user_config */)
122 {
123     const fs::path docdir(utils::getenv_with_default(
124         "KYUA_DOCDIR", KYUA_DOCDIR));
125 
126     bool success = true;
127 
128     static const char* list_re = "^\\* ";
129 
130     if (cmdline.arguments().empty()) {
131         ui->out(PACKAGE " (" PACKAGE_NAME ") " PACKAGE_VERSION);
132         ui->out("");
133         ui->out("License terms:");
134         ui->out("");
135         success &= cat_file(ui, docdir / "LICENSE");
136         ui->out("");
137         ui->out("Brought to you by:");
138         ui->out("");
139         success &= cat_file(ui, docdir / "AUTHORS", list_re);
140         ui->out("");
141         success &= cat_file(ui, docdir / "CONTRIBUTORS", list_re);
142         ui->out("");
143         ui->out(F("Homepage: %s") % PACKAGE_URL);
144     } else {
145         const std::string& topic = cmdline.arguments()[0];
146 
147         if (topic == "authors") {
148             success &= cat_file(ui, docdir / "AUTHORS", list_re);
149             success &= cat_file(ui, docdir / "CONTRIBUTORS", list_re);
150         } else if (topic == "license") {
151             success &= cat_file(ui, docdir / "LICENSE");
152         } else if (topic == "version") {
153             write_version_header(ui);
154         } else {
155             throw cmdline::usage_error(F("Invalid about topic '%s'") % topic);
156         }
157     }
158 
159     return success ? EXIT_SUCCESS : EXIT_FAILURE;
160 }
161