1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
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. *
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. *
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 ******************************************************************************/
23 #include "config/i2-config.hpp"
24 #include "config/expression.hpp"
25 #include "config/configitembuilder.hpp"
26 #include "config/applyrule.hpp"
27 #include "config/objectrule.hpp"
28 #include "base/debuginfo.hpp"
29 #include "base/array.hpp"
30 #include "base/dictionary.hpp"
31 #include "base/scriptfunction.hpp"
32 #include "base/scriptsignal.hpp"
33 #include "base/scriptvariable.hpp"
34 #include "base/configerror.hpp"
35 #include "base/convert.hpp"
36 #include "base/objectlock.hpp"
37 #include <boost/foreach.hpp>
47 static inline Value Variable(VMFrame& frame, const String& name)
52 if (frame.Locals && frame.Locals->Contains(name))
53 return frame.Locals->Get(name);
54 else if (frame.Locals != frame.Self && HasField(frame.Self, name))
55 return GetField(frame.Self, name);
57 return ScriptVariable::Get(name);
60 static inline Value FunctionCall(VMFrame& frame, const Value& funcName, const std::vector<Value>& arguments)
62 ScriptFunction::Ptr func;
64 if (funcName.IsObjectType<ScriptFunction>())
67 func = ScriptFunction::GetByName(funcName);
70 BOOST_THROW_EXCEPTION(ConfigError("Function '" + funcName + "' does not exist."));
72 return func->Invoke(arguments);
75 static inline Value Indexer(VMFrame& frame, const Value& value, const String& index)
77 if (value.IsObjectType<Dictionary>()) {
78 Dictionary::Ptr dict = value;
79 return dict->Get(index);
80 } else if (value.IsObjectType<Array>()) {
81 Array::Ptr arr = value;
82 return arr->Get(Convert::ToLong(index));
83 } else if (value.IsObject()) {
84 Object::Ptr object = value;
85 Type::Ptr type = object->GetReflectionType();
88 BOOST_THROW_EXCEPTION(ConfigError("Dot operator applied to object which does not support reflection"));
90 int field = type->GetFieldId(index);
93 BOOST_THROW_EXCEPTION(ConfigError("Tried to access invalid property '" + index + "'"));
95 return object->GetField(field);
96 } else if (value.IsEmpty()) {
99 BOOST_THROW_EXCEPTION(ConfigError("Dot operator cannot be applied to type '" + value.GetTypeName() + "'"));
103 static inline Value NewFunction(VMFrame& frame, const String& name, const std::vector<String>& args,
104 std::map<String, Expression *> *closedVars, const boost::shared_ptr<Expression>& expression)
106 ScriptFunction::Ptr func = new ScriptFunction(boost::bind(&FunctionWrapper, _1, args,
107 EvaluateClosedVars(frame, closedVars), expression));
110 ScriptFunction::Register(name, func);
115 static inline Value NewSlot(VMFrame& frame, const String& signal, const Value& slot)
117 ScriptSignal::Ptr sig = ScriptSignal::GetByName(signal);
120 BOOST_THROW_EXCEPTION(ConfigError("Signal '" + signal + "' does not exist."));
122 sig->AddSlot(boost::bind(SlotWrapper, slot, _1));
127 static inline Value NewApply(VMFrame& frame, const String& type, const String& target, const String& name, const boost::shared_ptr<Expression>& filter,
128 const String& fkvar, const String& fvvar, const boost::shared_ptr<Expression>& fterm, std::map<String, Expression *> *closedVars,
129 const boost::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
131 ApplyRule::AddRule(type, target, name, expression, filter, fkvar,
132 fvvar, fterm, debugInfo, EvaluateClosedVars(frame, closedVars));
137 static inline Value NewObject(VMFrame& frame, bool abstract, const String& type, const String& name, const boost::shared_ptr<Expression>& filter,
138 const String& zone, std::map<String, Expression *> *closedVars, const boost::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
140 ConfigItemBuilder::Ptr item = new ConfigItemBuilder(debugInfo);
142 String checkName = name;
145 Type::Ptr ptype = Type::GetByName(type);
147 NameComposer *nc = dynamic_cast<NameComposer *>(ptype.get());
150 checkName = nc->MakeName(name, Dictionary::Ptr());
153 if (!checkName.IsEmpty()) {
154 ConfigItem::Ptr oldItem = ConfigItem::GetObject(type, checkName);
157 std::ostringstream msgbuf;
158 msgbuf << "Object '" << name << "' of type '" << type << "' re-defined: " << debugInfo << "; previous definition: " << oldItem->GetDebugInfo();
159 BOOST_THROW_EXCEPTION(ConfigError(msgbuf.str()) << errinfo_debuginfo(debugInfo));
165 if (name.FindFirstOf("!") != String::NPos) {
166 std::ostringstream msgbuf;
167 msgbuf << "Name for object '" << name << "' of type '" << type << "' is invalid: Object names may not contain '!'";
168 BOOST_THROW_EXCEPTION(ConfigError(msgbuf.str()) << errinfo_debuginfo(debugInfo));
173 item->AddExpression(new OwnedExpression(expression));
174 item->SetAbstract(abstract);
175 item->SetScope(EvaluateClosedVars(frame, closedVars));
177 item->SetFilter(filter);
178 item->Compile()->Register();
183 static inline Value For(VMFrame& frame, const String& fkvar, const String& fvvar, const Value& value, Expression *expression, const DebugInfo& debugInfo = DebugInfo())
185 if (value.IsObjectType<Array>()) {
186 if (!fvvar.IsEmpty())
187 BOOST_THROW_EXCEPTION(ConfigError("Cannot use dictionary iterator for array.") << errinfo_debuginfo(debugInfo));
189 Array::Ptr arr = value;
191 ObjectLock olock(arr);
192 BOOST_FOREACH(const Value& value, arr) {
193 frame.Locals->Set(fkvar, value);
194 expression->Evaluate(frame);
196 } else if (value.IsObjectType<Dictionary>()) {
198 BOOST_THROW_EXCEPTION(ConfigError("Cannot use array iterator for dictionary.") << errinfo_debuginfo(debugInfo));
200 Dictionary::Ptr dict = value;
202 ObjectLock olock(dict);
203 BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
204 frame.Locals->Set(fkvar, kv.first);
205 frame.Locals->Set(fvvar, kv.second);
206 expression->Evaluate(frame);
210 BOOST_THROW_EXCEPTION(ConfigError("Invalid type in __for expression: " + value.GetTypeName()) << errinfo_debuginfo(debugInfo));
215 static inline bool HasField(const Object::Ptr& context, const String& field)
217 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
220 return dict->Contains(field);
222 Type::Ptr type = context->GetReflectionType();
227 return type->GetFieldId(field) != -1;
231 static inline Value GetField(const Object::Ptr& context, const String& field)
233 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
236 return dict->Get(field);
238 Type::Ptr type = context->GetReflectionType();
243 int fid = type->GetFieldId(field);
248 return context->GetField(fid);
252 static inline void SetField(const Object::Ptr& context, const String& field, const Value& value)
254 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
257 dict->Set(field, value);
259 Type::Ptr type = context->GetReflectionType();
262 BOOST_THROW_EXCEPTION(ConfigError("Cannot set field on object."));
264 int fid = type->GetFieldId(field);
267 BOOST_THROW_EXCEPTION(ConfigError("Attribute '" + field + "' does not exist."));
270 context->SetField(fid, value);
271 } catch (const boost::bad_lexical_cast&) {
272 BOOST_THROW_EXCEPTION(ConfigError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "'"));
273 } catch (const std::bad_cast&) {
274 BOOST_THROW_EXCEPTION(ConfigError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "'"));
280 static inline Value FunctionWrapper(const std::vector<Value>& arguments,
281 const std::vector<String>& funcargs, const Dictionary::Ptr& closedVars, const boost::shared_ptr<Expression>& expr)
283 if (arguments.size() < funcargs.size())
284 BOOST_THROW_EXCEPTION(ConfigError("Too few arguments for function"));
289 closedVars->CopyTo(frame.Locals);
291 for (std::vector<Value>::size_type i = 0; i < std::min(arguments.size(), funcargs.size()); i++)
292 frame.Locals->Set(funcargs[i], arguments[i]);
294 expr->Evaluate(frame);
298 static inline void SlotWrapper(const Value& funcName, const std::vector<Value>& arguments)
300 ScriptFunction::Ptr func;
302 if (funcName.IsObjectType<ScriptFunction>())
305 func = ScriptFunction::GetByName(funcName);
308 BOOST_THROW_EXCEPTION(ConfigError("Function '" + funcName + "' does not exist."));
310 func->Invoke(arguments);
313 static inline Dictionary::Ptr EvaluateClosedVars(VMFrame& frame, std::map<String, Expression *> *closedVars)
315 Dictionary::Ptr locals;
318 locals = new Dictionary();
320 typedef std::pair<String, Expression *> ClosedVar;
321 BOOST_FOREACH(const ClosedVar& cvar, *closedVars) {
322 locals->Set(cvar.first, cvar.second->Evaluate(frame));