]> granicus.if.org Git - icinga2/blob - lib/icinga/macroprocessor.cpp
macros: fix param name for ResolveMacros
[icinga2] / lib / icinga / macroprocessor.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 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 <boost/tuple/tuple.hpp>
27 #include <boost/smart_ptr/make_shared.hpp>
28 #include <boost/foreach.hpp>
29
30 using namespace icinga;
31
32 Value MacroProcessor::ResolveMacros(const Value& str, const std::vector<MacroResolver::Ptr>& resolvers,
33     const Dictionary::Ptr& cr, const MacroProcessor::EscapeCallback& escapeFn)
34 {
35         Value result;
36
37         if (str.IsScalar()) {
38                 result = InternalResolveMacros(str, resolvers, cr, escapeFn);
39         } else if (str.IsObjectType<Array>()) {
40                 Array::Ptr resultArr = boost::make_shared<Array>();
41                 Array::Ptr arr = str;
42
43                 ObjectLock olock(arr);
44
45                 BOOST_FOREACH(const Value& arg, arr) {
46                         /* Note: don't escape macros here. */
47                         resultArr->Add(InternalResolveMacros(arg, resolvers, cr, EscapeCallback()));
48                 }
49
50                 result = resultArr;
51         } else {
52                 BOOST_THROW_EXCEPTION(std::invalid_argument("Command is not a string or array."));
53         }
54
55         return result;
56 }
57
58 bool MacroProcessor::ResolveMacro(const String& macro, const std::vector<MacroResolver::Ptr>& resolvers,
59     const Dictionary::Ptr& cr, String *result)
60 {
61         BOOST_FOREACH(const MacroResolver::Ptr& resolver, resolvers) {
62                 if (resolver->ResolveMacro(macro, cr, result))
63                         return true;
64         }
65
66         return false;
67 }
68
69
70 String MacroProcessor::InternalResolveMacros(const String& str, const std::vector<MacroResolver::Ptr>& resolvers,
71     const Dictionary::Ptr& cr, const MacroProcessor::EscapeCallback& escapeFn)
72 {
73         size_t offset, pos_first, pos_second;
74         offset = 0;
75
76         String result = str;
77         while ((pos_first = result.FindFirstOf("$", offset)) != String::NPos) {
78                 pos_second = result.FindFirstOf("$", pos_first + 1);
79
80                 if (pos_second == String::NPos)
81                         BOOST_THROW_EXCEPTION(std::runtime_error("Closing $ not found in macro format string."));
82
83                 String name = result.SubStr(pos_first + 1, pos_second - pos_first - 1);
84
85                 String resolved_macro;
86                 bool found = ResolveMacro(name, resolvers, cr, &resolved_macro);
87
88                 /* $$ is an escape sequence for $. */
89                 if (name.IsEmpty()) {
90                         resolved_macro = "$";
91                         found = true;
92                 }
93
94                 if (!found)
95                         Log(LogWarning, "icinga", "Macro '" + name + "' is not defined.");
96
97                 result.Replace(pos_first, pos_second - pos_first + 1, resolved_macro);
98                 offset = pos_first + resolved_macro.GetLength();
99         }
100
101         return result;
102 }