summaryrefslogtreecommitdiffstats
path: root/lib/icinga/macroprocessor.hpp
blob: 7e74821536f37ce160f6651147d83ee8f75988d7 (plain)
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
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#ifndef MACROPROCESSOR_H
#define MACROPROCESSOR_H

#include "icinga/i2-icinga.hpp"
#include "icinga/checkable.hpp"
#include "base/value.hpp"
#include <vector>
#include <utility>

namespace icinga
{

/**
 * Resolves macros.
 *
 * @ingroup icinga
 */
class MacroProcessor
{
public:
	struct ResolverSpec
	{
		String Name;
		Object::Ptr Obj;

		// Whether to resolve not only e.g. $host.address$, but also just $address$
		bool ResolveShortMacros;

		inline ResolverSpec(String name, Object::Ptr obj, bool resolveShortMacros = true)
			: Name(std::move(name)), Obj(std::move(obj)), ResolveShortMacros(resolveShortMacros)
		{
		}
	};

	typedef std::function<Value (const Value&)> EscapeCallback;
	typedef std::vector<ResolverSpec> ResolverList;

	static Value ResolveMacros(const Value& str, const ResolverList& resolvers,
		const CheckResult::Ptr& cr = nullptr, String *missingMacro = nullptr,
		const EscapeCallback& escapeFn = EscapeCallback(),
		const Dictionary::Ptr& resolvedMacros = nullptr,
		bool useResolvedMacros = false, int recursionLevel = 0);

	static Value ResolveArguments(const Value& command, const Dictionary::Ptr& arguments,
		const MacroProcessor::ResolverList& resolvers, const CheckResult::Ptr& cr,
		const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros, int recursionLevel = 0);

	static bool ValidateMacroString(const String& macro);
	static void ValidateCustomVars(const ConfigObject::Ptr& object, const Dictionary::Ptr& value);

private:
	MacroProcessor();

	static bool ResolveMacro(const String& macro, const ResolverList& resolvers,
		const CheckResult::Ptr& cr, Value *result, bool *recursive_macro);
	static Value InternalResolveMacros(const String& str,
		const ResolverList& resolvers, const CheckResult::Ptr& cr,
		String *missingMacro, const EscapeCallback& escapeFn,
		const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros,
		int recursionLevel = 0);
	static Value EvaluateFunction(const Function::Ptr& func, const ResolverList& resolvers,
		const CheckResult::Ptr& cr, const MacroProcessor::EscapeCallback& escapeFn,
		const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros, int recursionLevel);

	static void AddArgumentHelper(const Array::Ptr& args, const String& key, const String& value,
		bool add_key, bool add_value, const Value& separator);
	static Value EscapeMacroShellArg(const Value& value);

};

}

#endif /* MACROPROCESSOR_H */