]> granicus.if.org Git - icinga2/blob - lib/icinga/macroprocessor.cpp
Avoid unnecessary copies when iterating over dictionaries.
[icinga2] / lib / icinga / macroprocessor.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/)   *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "icinga/macroprocessor.h"
21 #include "icinga/macroresolver.h"
22 #include "base/utility.h"
23 #include "base/array.h"
24 #include "base/objectlock.h"
25 #include "base/logger_fwd.h"
26 #include "base/context.h"
27 #include <boost/foreach.hpp>
28
29 using namespace icinga;
30
31 Value MacroProcessor::ResolveMacros(const Value& str, const std::vector<MacroResolver::Ptr>& resolvers,
32         const CheckResult::Ptr& cr, const MacroProcessor::EscapeCallback& escapeFn, const Array::Ptr& escapeMacros)
33 {
34         Value result;
35
36         if (str.IsEmpty())
37                 return Empty;
38
39         if (str.IsScalar()) {
40                 result = InternalResolveMacros(str, resolvers, cr, escapeFn, escapeMacros);
41         } else if (str.IsObjectType<Array>()) {
42                 Array::Ptr resultArr = make_shared<Array>();
43                 Array::Ptr arr = str;
44
45                 ObjectLock olock(arr);
46
47                 BOOST_FOREACH(const Value& arg, arr) {
48                         /* Note: don't escape macros here. */
49                         resultArr->Add(InternalResolveMacros(arg, resolvers, cr, EscapeCallback(), Array::Ptr()));
50                 }
51
52                 result = resultArr;
53         } else {
54                 BOOST_THROW_EXCEPTION(std::invalid_argument("Command is not a string or array."));
55         }
56
57         return result;
58 }
59
60 bool MacroProcessor::ResolveMacro(const String& macro, const std::vector<MacroResolver::Ptr>& resolvers,
61     const CheckResult::Ptr& cr, String *result)
62 {
63         CONTEXT("Resolving macro '" + macro + "'");
64
65         BOOST_FOREACH(const MacroResolver::Ptr& resolver, resolvers) {
66                 if (resolver->ResolveMacro(macro, cr, result))
67                         return true;
68         }
69
70         return false;
71 }
72
73
74 String MacroProcessor::InternalResolveMacros(const String& str, const std::vector<MacroResolver::Ptr>& resolvers,
75         const CheckResult::Ptr& cr, const MacroProcessor::EscapeCallback& escapeFn, const Array::Ptr& escapeMacros)
76 {
77         CONTEXT("Resolving macros for string '" + str + "'");
78
79         size_t offset, pos_first, pos_second;
80         offset = 0;
81
82         String result = str;
83         while ((pos_first = result.FindFirstOf("$", offset)) != String::NPos) {
84                 pos_second = result.FindFirstOf("$", pos_first + 1);
85
86                 if (pos_second == String::NPos)
87                         BOOST_THROW_EXCEPTION(std::runtime_error("Closing $ not found in macro format string."));
88
89                 String name = result.SubStr(pos_first + 1, pos_second - pos_first - 1);
90
91                 String resolved_macro;
92                 bool found = ResolveMacro(name, resolvers, cr, &resolved_macro);
93
94                 /* $$ is an escape sequence for $. */
95                 if (name.IsEmpty()) {
96                         resolved_macro = "$";
97                         found = true;
98                 }
99
100                 if (!found)
101                         Log(LogWarning, "icinga", "Macro '" + name + "' is not defined.");
102
103                 if (escapeFn && escapeMacros) {
104                         bool escape = false;
105
106                         ObjectLock olock(escapeMacros);
107                         BOOST_FOREACH(const String& escapeMacro, escapeMacros) {
108                                 if (escapeMacro == name) {
109                                         escape = true;
110                                         break;
111                                 }
112                         }
113
114                         if (escape)
115                                 resolved_macro = escapeFn(resolved_macro);
116                 }
117
118                 result.Replace(pos_first, pos_second - pos_first + 1, resolved_macro);
119                 offset = pos_first + resolved_macro.GetLength();
120         }
121
122         return result;
123 }