forked from newsboat/newsboat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
newsboat.cpp
283 lines (257 loc) · 7.37 KB
/
newsboat.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <cstring>
#include <errno.h>
#include <iostream>
#include <ncurses.h>
#include <sstream>
#include <sys/utsname.h>
#include "cache.h"
#include "cliargsparser.h"
#include "config.h"
#include "configpaths.h"
#include "controller.h"
#include "dbexception.h"
#include "exception.h"
#include "matcherexception.h"
#include "rss/parser.h"
#include "utils.h"
#include "view.h"
#include "xlicense.h"
extern "C" {
void rs_setup_human_panic(void);
}
using namespace newsboat;
void print_usage(const std::string& argv0, const std::string& config_path,
const std::string& urls_path, const std::string& cache_path)
{
auto msg = strprintf::fmt(
_("%s %s\nusage: %s [-i <file>|-e] [-u <urlfile>] "
"[-c <cachefile>] [-x <command> ...] [-h]\n"),
PROGRAM_NAME,
utils::program_version(),
argv0);
std::cout << msg;
struct arg {
const char name;
const std::string longname;
const std::string params;
const std::string desc;
};
static const std::vector<arg> args = {
{'e', "export-to-opml", "", _s("export OPML feed to stdout")},
{'r', "refresh-on-start", "", _s("refresh feeds on start")},
{'i', "import-from-opml", _s("<file>"), _s("import OPML file")},
{
'u',
"url-file",
_s("<urlfile>"),
_s("read RSS feed URLs from <urlfile>")
},
{
'c',
"cache-file",
_s("<cachefile>"),
_s("use <cachefile> as cache file")
},
{
'C',
"config-file",
_s("<configfile>"),
_s("read configuration from <configfile>")
},
{'X', "vacuum", "", _s("compact the cache")},
{
'x',
"execute",
_s("<command>..."),
_s("execute list of commands")
},
{'q', "quiet", "", _s("quiet startup")},
{'v', "version", "", _s("get version information")},
{
'l',
"log-level",
_s("<loglevel>"),
_s("write a log with a certain loglevel (valid values: "
"1 to "
"6)")
},
{
'd',
"log-file",
_s("<logfile>"),
_s("use <logfile> as output log file")
},
{
'E',
"export-to-file",
_s("<file>"),
_s("export list of read articles to <file>")
},
{
'I',
"import-from-file",
_s("<file>"),
_s("import list of read articles from <file>")
},
{'h', "help", "", _s("this help")},
{'-', "cleanup", "", _s("remove unreferenced items from cache")}
};
std::stringstream ss;
for (const auto& a : args) {
std::string longcolumn;
if (a.name != '-') {
longcolumn += "-";
longcolumn += a.name;
longcolumn += ", ";
} else {
longcolumn += " ";
}
longcolumn += "--" + a.longname;
longcolumn += a.params.size() > 0 ? "=" + a.params : "";
ss << "\t" << longcolumn;
for (unsigned int j = 0; j < utils::gentabs(longcolumn); j++) {
ss << "\t";
}
ss << a.desc << std::endl;
}
std::cout << ss.str();
std::cout << '\n';
std::cout << _("Files:") << '\n';
/// This is printed out by --help before the path to the config file
const std::string tr_config = _("configuration");
/// This is printed out by --help before the path to the urls file
const std::string tr_urls = _("feed URLs");
/// This is printed out by --help before the path to the cache file
const std::string tr_cache = _("cache");
const auto widest = std::max({tr_config.length(), tr_urls.length(), tr_cache.length()});
const auto print_filepath = [widest](const std::string& name,
const std::string& value) {
std::cout << "\t- " << name << ": " << std::string(
widest - name.length(), ' ') << value << '\n';
};
print_filepath(tr_config, config_path);
print_filepath(tr_urls, urls_path);
print_filepath(tr_cache, cache_path);
std::cout << std::endl
<< _("Support at #newsboat at https://libera.chat or on our mailing "
"list https://groups.google.com/g/newsboat")
<< std::endl
<< _("For more information, check out https://newsboat.org/")
<< std::endl;
}
void print_version(const std::string& argv0, unsigned int level)
{
if (level <= 1) {
std::stringstream ss;
ss << PROGRAM_NAME << " " << utils::program_version() << " - "
<< PROGRAM_URL << std::endl;
ss << "Copyright (C) 2006-2015 Andreas Krennmair"
<< std::endl;
ss << "Copyright (C) 2015-2021 Alexander Batischev"
<< std::endl;
ss << "Copyright (C) 2006-2017 Newsbeuter contributors"
<< std::endl;
ss << "Copyright (C) 2017-2021 Newsboat contributors"
<< std::endl;
ss << std::endl;
ss << strprintf::fmt(
_("Newsboat is free software licensed "
"under the MIT License. (Type `%s -vv' "
"to see the full text.)"),
argv0)
<< std::endl;
ss << _("It bundles:") << std::endl;
ss << _("- JSON for Modern C++ library, licensed under the MIT License: "
"https://github.com/nlohmann/json")
<< std::endl;
ss << _("- optional-lite library, licensed under the Boost Software "
"License: https://github.com/martinmoene/optional-lite")
<< std::endl;
ss << _("- expected-lite library, licensed under the Boost Software "
"License: https://github.com/martinmoene/expected-lite")
<< std::endl;
ss << std::endl;
struct utsname xuts;
uname(&xuts);
ss << PROGRAM_NAME << " " << utils::program_version()
<< std::endl;
ss << "System: " << xuts.sysname << " " << xuts.release
<< " (" << xuts.machine << ")" << std::endl;
#if defined(__GNUC__) && defined(__VERSION__)
ss << "Compiler: g++ " << __VERSION__ << std::endl;
#endif
ss << "ncurses: " << curses_version()
<< " (compiled with " << NCURSES_VERSION << ")"
<< std::endl;
ss << "libcurl: " << curl_version() << " (compiled with "
<< LIBCURL_VERSION << ")" << std::endl;
ss << "SQLite: " << sqlite3_libversion()
<< " (compiled with " << SQLITE_VERSION << ")"
<< std::endl;
ss << "libxml2: compiled with " << LIBXML_DOTTED_VERSION
<< std::endl
<< std::endl;
std::cout << ss.str();
} else {
std::cout << LICENSE_str << std::endl;
}
}
int main(int argc, char* argv[])
{
rs_setup_human_panic();
utils::initialize_ssl_implementation();
setlocale(LC_CTYPE, "");
setlocale(LC_MESSAGES, "");
textdomain(PACKAGE);
bindtextdomain(PACKAGE, LOCALEDIR);
// Internally, Newsboat stores all strings in UTF-8, so we require gettext
// to return messages in that encoding.
bind_textdomain_codeset(PACKAGE, "UTF-8");
rsspp::Parser::global_init();
ConfigPaths configpaths;
if (!configpaths.initialized()) {
std::cerr << configpaths.error_message() << std::endl;
return EXIT_FAILURE;
}
Controller c(configpaths);
newsboat::View v(&c);
c.set_view(&v);
CliArgsParser args(argc, argv);
configpaths.process_args(args);
if (args.should_print_usage()) {
print_usage(args.program_name(), configpaths.config_file(),
configpaths.url_file(), configpaths.cache_file());
if (args.return_code().has_value()) {
return args.return_code().value();
}
} else if (args.show_version()) {
print_version(args.program_name(), args.show_version());
return EXIT_SUCCESS;
}
int ret;
try {
ret = c.run(args);
} catch (const newsboat::DbException& e) {
Stfl::reset();
std::cerr << strprintf::fmt(
_("Caught newsboat::DbException with message: %s"),
e.what())
<< std::endl;
::exit(EXIT_FAILURE);
} catch (const newsboat::MatcherException& e) {
Stfl::reset();
std::cerr << strprintf::fmt(
_("Caught newsboat::MatcherException with message: %s"),
e.what())
<< std::endl;
::exit(EXIT_FAILURE);
} catch (const newsboat::Exception& e) {
Stfl::reset();
std::cerr << strprintf::fmt(_("Caught newsboat::Exception with message: %s"),
e.what())
<< std::endl;
::exit(EXIT_FAILURE);
}
rsspp::Parser::global_cleanup();
return ret;
}