1 // +------------------------------------------------------------------+
2 // | ____ _ _ __ __ _ __ |
3 // | / ___| |__ ___ ___| | __ | \/ | |/ / |
4 // | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
5 // | | |___| | | | __/ (__| < | | | | . \ |
6 // | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
8 // | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
9 // +------------------------------------------------------------------+
11 // This file is part of Check_MK.
12 // The official homepage is at http://mathias-kettner.de/check_mk.
14 // check_mk is free software; you can redistribute it and/or modify it
15 // under the terms of the GNU General Public License as published by
16 // the Free Software Foundation in version 2. check_mk is distributed
17 // in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
18 // out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
19 // PARTICULAR PURPOSE. See the GNU General Public License for more de-
20 // tails. You should have received a copy of the GNU General Public
21 // License along with GNU Make; see the file COPYING. If not, write
22 // to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 // Boston, MA 02110-1301 USA.
25 #include "OffsetStringMacroColumn.h"
27 #include <type_traits>
30 #include "MonitoringCore.h"
33 #include "StringUtils.h"
35 std::optional
<std::string
> MacroExpander::from_ptr(const char *str
) {
36 return str
== nullptr ? std::nullopt
: std::make_optional(str
);
39 CompoundMacroExpander::CompoundMacroExpander(
40 std::unique_ptr
<MacroExpander
> first
, std::unique_ptr
<MacroExpander
> second
)
41 : _first(std::move(first
)), _second(std::move(second
)) {}
43 std::optional
<std::string
> CompoundMacroExpander::expand(
44 const std::string
&str
) {
45 if (auto e
= _first
->expand(str
)) {
48 return _second
->expand(str
);
51 std::optional
<std::string
> UserMacroExpander::expand(const std::string
&str
) {
52 if (mk::starts_with(str
, "USER")) {
53 int n
= atoi(str
.substr(4).c_str());
54 if (1 <= n
&& n
<= MAX_USER_MACROS
) {
55 extern char *macro_user
[MAX_USER_MACROS
];
56 return from_ptr(macro_user
[n
- 1]);
62 CustomVariableExpander::CustomVariableExpander(std::string prefix
,
63 const customvariablesmember
*cvm
,
64 const MonitoringCore
*mc
)
65 : _prefix(std::move(prefix
)), _mc(mc
), _cvm(cvm
) {}
67 std::optional
<std::string
> CustomVariableExpander::expand(
68 const std::string
&str
) {
69 if (!mk::starts_with(str
, _prefix
)) {
73 RegExp
regExp(str
.substr(_prefix
.size()), RegExp::Case::ignore
,
74 RegExp::Syntax::literal
);
75 for (const auto &[name
, value
] :
76 _mc
->customAttributes(&_cvm
, AttributeKind::custom_variables
)) {
77 if (regExp
.match(name
)) {
85 std::string
expandMacros(const std::string
&raw
,
86 std::unique_ptr
<MacroExpander
> expander
) {
89 while (pos
< raw
.size()) {
90 auto start
= raw
.find('$', pos
);
91 if (start
== std::string::npos
) {
92 result
+= raw
.substr(pos
);
95 auto end
= raw
.find('$', start
+ 1);
96 if (end
== std::string::npos
) {
97 result
+= raw
.substr(pos
);
100 auto macroname
= raw
.substr(start
+ 1, end
- (start
+ 1));
101 if (auto replacement
= expander
->expand(macroname
)) {
102 result
+= raw
.substr(pos
, start
- pos
) + *replacement
;
104 result
+= raw
.substr(pos
, end
+ 1 - pos
);
112 std::string
OffsetStringMacroColumn::getValue(Row row
) const {
113 // TODO(sp): Use _mc!
115 if (auto p
= columnData
<void>(row
)) {
116 auto s
= offset_cast
<const char *>(p
, _string_offset
);
117 return *s
== nullptr ? "" : expandMacros(*s
, getMacroExpander(row
));