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
|
/*
** Copyright (C) 2008-2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 3, or (at your option) any
** later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation,
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/
#include <config.h>
#include <functional>
#include <glib.h>
#include <glib-object.h>
#include <locale.h>
#include "mu-cmd.hh"
#include "mu-options.hh"
#include "utils/mu-utils.hh"
#include "utils/mu-logger.hh"
#include "mu-cmd.hh"
using namespace Mu;
static void
output_error(const std::string& what, bool use_color)
{
using Color = MaybeAnsi::Color;
MaybeAnsi col{use_color};
mu_printerrln("{}error{}: {}{}{}",
col.fg(Color::Red), col.reset(),
col.fg(Color::BrightYellow), what, col.reset());
}
static int
handle_result(const Result<void>& res, const Mu::Options& opts)
{
if (res)
return 0;
using Color = MaybeAnsi::Color;
MaybeAnsi col{!opts.nocolor};
// show the error and some help, but not if it's only a softerror.
if (!res.error().is_soft_error())
output_error(res.error().what(), !opts.nocolor);
else
mu_printerrln("{}{}{}",
col.fg(Color::BrightBlue), res.error().what(), col.reset());
// perhaps give some useful hint on how to solve it.
if (!res.error().hint().empty())
mu_printerrln("{}hint{}: {}{}{}",
col.fg(Color::Blue), col.reset(),
col.fg(Color::Green), res.error().hint(), col.reset());
if (res.error().exit_code() != 0 && !res.error().is_soft_error()) {
mu_warning("mu finishing with error: {}",
format_as(res.error()));
if (const auto& hint = res.error().hint(); !hint.empty())
mu_info("hint: {}", hint);
}
return res.error().exit_code();
}
namespace {
Options opts; // it's big, don't want it on the stack
}
int
main(int argc, char* argv[]) try
{
/*
* We handle this through explicit options
*/
g_unsetenv("XAPIAN_CJK_NGRAM");
/*
* set up locale
*/
::setlocale(LC_ALL, "");
/*
* read command-line options
*/
auto optsres{Options::make(argc, argv)};
if (!optsres) {
output_error(optsres.error().what(), !Options::default_no_color());
return optsres.error().exit_code();
} else if (!optsres->sub_command) {
// nothing more to do.
return 0;
}
opts = std::move(*optsres);
// setup logging
Logger::Options lopts{Logger::Options::None};
if (opts.log_stderr)
lopts |= Logger::Options::StdOutErr;
if (opts.debug)
lopts |= Logger::Options::Debug;
else if (opts.quiet) // ie. only consider when not in debug mode
lopts |= Logger::Options::Quiet;
if (!!g_getenv("MU_TEST"))
lopts |= Logger::Options::File;
const auto logger{Logger::make(opts.runtime_path(RuntimePath::LogFile), lopts)};
if (!logger) {
output_error(logger.error().what(), !opts.nocolor);
return logger.error().exit_code();
}
/*
* handle sub command
*/
return handle_result(mu_cmd_execute(opts), opts);
// exceptions should have been handled earlier, but catch them here,
// just in case...
} catch (const std::logic_error& le) {
mu_printerrln("caught logic-error: {}", le.what());
return 97;
} catch (const std::runtime_error& re) {
mu_printerrln("caught runtime-error: {}", re.what());
return 98;
} catch (...) {
mu_printerrln("caught exception");
return 99;
}
|