From: Gunnar Beutner Date: Mon, 27 Mar 2017 12:26:56 +0000 (+0200) Subject: Improve function metadata by adding arguments X-Git-Tag: v2.7.0~183 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5d45c74be30290493648a7dc3e105f9ce50a946c;p=icinga2 Improve function metadata by adding arguments fixes #5087 --- diff --git a/lib/base/array-script.cpp b/lib/base/array-script.cpp index a00d5c2dc..03ae2cae8 100644 --- a/lib/base/array-script.cpp +++ b/lib/base/array-script.cpp @@ -228,21 +228,21 @@ Object::Ptr Array::GetPrototype(void) if (!prototype) { prototype = new Dictionary(); - prototype->Set("len", new Function("Array#len", WrapFunction(ArrayLen), true)); - prototype->Set("set", new Function("Array#set", WrapFunction(ArraySet))); - prototype->Set("get", new Function("Array#get", WrapFunction(ArrayGet))); - prototype->Set("add", new Function("Array#add", WrapFunction(ArrayAdd))); - prototype->Set("remove", new Function("Array#remove", WrapFunction(ArrayRemove))); - prototype->Set("contains", new Function("Array#contains", WrapFunction(ArrayContains), true)); + prototype->Set("len", new Function("Array#len", WrapFunction(ArrayLen), {}, true)); + prototype->Set("set", new Function("Array#set", WrapFunction(ArraySet), { "index", "value" })); + prototype->Set("get", new Function("Array#get", WrapFunction(ArrayGet), { "index" })); + prototype->Set("add", new Function("Array#add", WrapFunction(ArrayAdd), { "value" })); + prototype->Set("remove", new Function("Array#remove", WrapFunction(ArrayRemove), { "index" })); + prototype->Set("contains", new Function("Array#contains", WrapFunction(ArrayContains), { "value" }, true)); prototype->Set("clear", new Function("Array#clear", WrapFunction(ArrayClear))); - prototype->Set("sort", new Function("Array#sort", WrapFunction(ArraySort), true)); - prototype->Set("shallow_clone", new Function("Array#shallow_clone", WrapFunction(ArrayShallowClone), true)); - prototype->Set("join", new Function("Array#join", WrapFunction(ArrayJoin), true)); - prototype->Set("reverse", new Function("Array#reverse", WrapFunction(ArrayReverse), true)); - prototype->Set("map", new Function("Array#map", WrapFunction(ArrayMap), true)); - prototype->Set("reduce", new Function("Array#reduce", WrapFunction(ArrayReduce), true)); - prototype->Set("filter", new Function("Array#filter", WrapFunction(ArrayFilter), true)); - prototype->Set("unique", new Function("Array#unique", WrapFunction(ArrayUnique), true)); + prototype->Set("sort", new Function("Array#sort", WrapFunction(ArraySort), { "less_cmp" }, true)); + prototype->Set("shallow_clone", new Function("Array#shallow_clone", WrapFunction(ArrayShallowClone), {}, true)); + prototype->Set("join", new Function("Array#join", WrapFunction(ArrayJoin), { "separator" }, true)); + prototype->Set("reverse", new Function("Array#reverse", WrapFunction(ArrayReverse), {}, true)); + prototype->Set("map", new Function("Array#map", WrapFunction(ArrayMap), { "func" }, true)); + prototype->Set("reduce", new Function("Array#reduce", WrapFunction(ArrayReduce), { "reduce" }, true)); + prototype->Set("filter", new Function("Array#filter", WrapFunction(ArrayFilter), { "func" }, true)); + prototype->Set("unique", new Function("Array#unique", WrapFunction(ArrayUnique), {}, true)); } return prototype; diff --git a/lib/base/boolean-script.cpp b/lib/base/boolean-script.cpp index 5b03ea73f..75c979ada 100644 --- a/lib/base/boolean-script.cpp +++ b/lib/base/boolean-script.cpp @@ -38,7 +38,7 @@ Object::Ptr Boolean::GetPrototype(void) if (!prototype) { prototype = new Dictionary(); - prototype->Set("to_string", new Function("Boolean#to_string", WrapFunction(BooleanToString), true)); + prototype->Set("to_string", new Function("Boolean#to_string", WrapFunction(BooleanToString), {}, true)); } return prototype; diff --git a/lib/base/configobject-script.cpp b/lib/base/configobject-script.cpp index e3450081e..dfe4455b6 100644 --- a/lib/base/configobject-script.cpp +++ b/lib/base/configobject-script.cpp @@ -45,8 +45,8 @@ Object::Ptr ConfigObject::GetPrototype(void) if (!prototype) { prototype = new Dictionary(); - prototype->Set("modify_attribute", new Function("ConfigObject#modify_attribute", WrapFunction(ConfigObjectModifyAttribute), false)); - prototype->Set("restore_attribute", new Function("ConfigObject#restore_attribute", WrapFunction(ConfigObjectRestoreAttribute), false)); + prototype->Set("modify_attribute", new Function("ConfigObject#modify_attribute", WrapFunction(ConfigObjectModifyAttribute), { "attr", "value" }, false)); + prototype->Set("restore_attribute", new Function("ConfigObject#restore_attribute", WrapFunction(ConfigObjectRestoreAttribute), { "attr", "value" }, false)); } return prototype; diff --git a/lib/base/datetime-script.cpp b/lib/base/datetime-script.cpp index 35beb06b2..7d9a14cc2 100644 --- a/lib/base/datetime-script.cpp +++ b/lib/base/datetime-script.cpp @@ -39,7 +39,7 @@ Object::Ptr DateTime::GetPrototype(void) if (!prototype) { prototype = new Dictionary(); - prototype->Set("format", new Function("DateTime#format", WrapFunction(DateTimeFormat))); + prototype->Set("format", new Function("DateTime#format", WrapFunction(DateTimeFormat), { "format" })); } return prototype; diff --git a/lib/base/dictionary-script.cpp b/lib/base/dictionary-script.cpp index a988ba59a..908b719b2 100644 --- a/lib/base/dictionary-script.cpp +++ b/lib/base/dictionary-script.cpp @@ -85,13 +85,13 @@ Object::Ptr Dictionary::GetPrototype(void) if (!prototype) { prototype = new Dictionary(); - prototype->Set("len", new Function("Dictionary#len", WrapFunction(DictionaryLen), true)); - prototype->Set("set", new Function("Dictionary#set", WrapFunction(DictionarySet))); - prototype->Set("get", new Function("Dictionary#get", WrapFunction(DictionaryGet))); - prototype->Set("remove", new Function("Dictionary#remove", WrapFunction(DictionaryRemove))); - prototype->Set("contains", new Function("Dictionary#contains", WrapFunction(DictionaryContains), true)); - prototype->Set("shallow_clone", new Function("Dictionary#shallow_clone", WrapFunction(DictionaryShallowClone), true)); - prototype->Set("keys", new Function("Dictionary#keys", WrapFunction(DictionaryKeys), true)); + prototype->Set("len", new Function("Dictionary#len", WrapFunction(DictionaryLen), {}, true)); + prototype->Set("set", new Function("Dictionary#set", WrapFunction(DictionarySet), { "key", "value" })); + prototype->Set("get", new Function("Dictionary#get", WrapFunction(DictionaryGet), { "key" })); + prototype->Set("remove", new Function("Dictionary#remove", WrapFunction(DictionaryRemove), { "key" })); + prototype->Set("contains", new Function("Dictionary#contains", WrapFunction(DictionaryContains), { "key" }, true)); + prototype->Set("shallow_clone", new Function("Dictionary#shallow_clone", WrapFunction(DictionaryShallowClone), {}, true)); + prototype->Set("keys", new Function("Dictionary#keys", WrapFunction(DictionaryKeys), {}, true)); } return prototype; diff --git a/lib/base/function.cpp b/lib/base/function.cpp index b9beff6f3..8cc233774 100644 --- a/lib/base/function.cpp +++ b/lib/base/function.cpp @@ -19,18 +19,21 @@ #include "base/function.hpp" #include "base/function.tcpp" +#include "base/array.hpp" #include "base/scriptframe.hpp" using namespace icinga; REGISTER_TYPE_WITH_PROTOTYPE(Function, Function::GetPrototype()); -Function::Function(const String& name, const Callback& function, bool side_effect_free, bool deprecated) +Function::Function(const String& name, const Callback& function, const std::vector& args, + bool side_effect_free, bool deprecated) : m_Callback(function) { SetName(name, true); SetSideEffectFree(side_effect_free, true); SetDeprecated(deprecated, true); + SetArguments(Array::FromVector(args), true); } Value Function::Invoke(const std::vector& arguments) diff --git a/lib/base/function.hpp b/lib/base/function.hpp index efff4116f..faf64ad92 100644 --- a/lib/base/function.hpp +++ b/lib/base/function.hpp @@ -43,7 +43,8 @@ public: typedef boost::function& arguments)> Callback; - Function(const String& name, const Callback& function, bool side_effect_free = false, bool deprecated = false); + Function(const String& name, const Callback& function, const std::vector& args = std::vector(), + bool side_effect_free = false, bool deprecated = false); Value Invoke(const std::vector& arguments = std::vector()); Value Invoke(const Value& otherThis, const std::vector& arguments = std::vector()); @@ -66,47 +67,47 @@ private: Callback m_Callback; }; -#define REGISTER_SCRIPTFUNCTION_NS(ns, name, callback) \ +#define REGISTER_SCRIPTFUNCTION_NS(ns, name, callback, args) \ INITIALIZE_ONCE_WITH_PRIORITY([]() { \ - Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), false); \ + Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), String(args).Split(":"), false); \ ScriptGlobal::Set(#ns "." #name, sf); \ }, 10) -#define REGISTER_SCRIPTFUNCTION_NS_PREFIX(ns, name, callback) \ +#define REGISTER_SCRIPTFUNCTION_NS_PREFIX(ns, name, callback, args) \ INITIALIZE_ONCE_WITH_PRIORITY([]() { \ - Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), false); \ + Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), String(args).Split(":"), false); \ ScriptGlobal::Set(#ns "." #name, sf); \ - Function::Ptr dsf = new icinga::Function("Deprecated#__" #name " (deprecated)", WrapFunction(callback), false, true); \ + Function::Ptr dsf = new icinga::Function("Deprecated#__" #name " (deprecated)", WrapFunction(callback), String(args).Split(":"), false, true); \ ScriptGlobal::Set("Deprecated.__" #name, dsf); \ }, 10) -#define REGISTER_SCRIPTFUNCTION_NS_DEPRECATED(ns, name, callback) \ +#define REGISTER_SCRIPTFUNCTION_NS_DEPRECATED(ns, name, callback, args) \ INITIALIZE_ONCE_WITH_PRIORITY([]() { \ - Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), false); \ + Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), String(args).Split(":"), false); \ ScriptGlobal::Set(#ns "." #name, sf); \ - Function::Ptr dsf = new icinga::Function("Deprecated#" #name " (deprecated)", WrapFunction(callback), false, true); \ + Function::Ptr dsf = new icinga::Function("Deprecated#" #name " (deprecated)", WrapFunction(callback), String(args).Split(":"), false, true); \ ScriptGlobal::Set("Deprecated." #name, dsf); \ }, 10) -#define REGISTER_SAFE_SCRIPTFUNCTION_NS(ns, name, callback) \ +#define REGISTER_SAFE_SCRIPTFUNCTION_NS(ns, name, callback, args) \ INITIALIZE_ONCE_WITH_PRIORITY([]() { \ - Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), true); \ + Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), String(args).Split(":"), true); \ ScriptGlobal::Set(#ns "." #name, sf); \ }, 10) -#define REGISTER_SAFE_SCRIPTFUNCTION_NS_PREFIX(ns, name, callback) \ +#define REGISTER_SAFE_SCRIPTFUNCTION_NS_PREFIX(ns, name, callback, args) \ INITIALIZE_ONCE_WITH_PRIORITY([]() { \ - Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), true); \ + Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), String(args).Split(":"), true); \ ScriptGlobal::Set(#ns "." #name, sf); \ - Function::Ptr dsf = new icinga::Function("Deprecated#__" #name " (deprecated)", WrapFunction(callback), true, true); \ + Function::Ptr dsf = new icinga::Function("Deprecated#__" #name " (deprecated)", WrapFunction(callback), String(args).Split(":"), true, true); \ ScriptGlobal::Set("Deprecated.__" #name, dsf); \ }, 10) -#define REGISTER_SAFE_SCRIPTFUNCTION_NS_DEPRECATED(ns, name, callback) \ +#define REGISTER_SAFE_SCRIPTFUNCTION_NS_DEPRECATED(ns, name, callback, args) \ INITIALIZE_ONCE_WITH_PRIORITY([]() { \ - Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), true); \ + Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), String(args).Split(":"), true); \ ScriptGlobal::Set(#ns "." #name, sf); \ - Function::Ptr dsf = new icinga::Function("Deprecated#" #name " (deprecated)", WrapFunction(callback), true, true); \ + Function::Ptr dsf = new icinga::Function("Deprecated#" #name " (deprecated)", WrapFunction(callback), String(args).Split(":"), true, true); \ ScriptGlobal::Set("Deprecated." #name, dsf); \ }, 10) diff --git a/lib/base/function.ti b/lib/base/function.ti index f51b189b4..b42e90ee8 100644 --- a/lib/base/function.ti +++ b/lib/base/function.ti @@ -29,6 +29,7 @@ abstract class Function [config] String "name"; [config] bool side_effect_free; [config] bool deprecated; + [config] Array::Ptr arguments; }; } diff --git a/lib/base/json-script.cpp b/lib/base/json-script.cpp index 70c9e3af0..e342f5ee2 100644 --- a/lib/base/json-script.cpp +++ b/lib/base/json-script.cpp @@ -35,8 +35,8 @@ INITIALIZE_ONCE([]() { Dictionary::Ptr jsonObj = new Dictionary(); /* Methods */ - jsonObj->Set("encode", new Function("Json#encode", WrapFunction(JsonEncodeShim), true)); - jsonObj->Set("decode", new Function("Json#decode", WrapFunction(JsonDecode), true)); + jsonObj->Set("encode", new Function("Json#encode", WrapFunction(JsonEncodeShim), { "value" }, true)); + jsonObj->Set("decode", new Function("Json#decode", WrapFunction(JsonDecode), { "value" }, true)); ScriptGlobal::Set("Json", jsonObj); }); diff --git a/lib/base/math-script.cpp b/lib/base/math-script.cpp index a25c0e262..eb1f4b449 100644 --- a/lib/base/math-script.cpp +++ b/lib/base/math-script.cpp @@ -171,27 +171,27 @@ INITIALIZE_ONCE([]() { mathObj->Set("SQRT2", 1.41421356237309504880); /* Methods */ - mathObj->Set("abs", new Function("Math#abs", WrapFunction(MathAbs), true)); - mathObj->Set("acos", new Function("Math#acos", WrapFunction(MathAcos), true)); - mathObj->Set("asin", new Function("Math#asin", WrapFunction(MathAsin), true)); - mathObj->Set("atan", new Function("Math#atan", WrapFunction(MathAtan), true)); - mathObj->Set("atan2", new Function("Math#atan2", WrapFunction(MathAtan2), true)); - mathObj->Set("ceil", new Function("Math#ceil", WrapFunction(MathCeil), true)); - mathObj->Set("cos", new Function("Math#cos", WrapFunction(MathCos), true)); - mathObj->Set("exp", new Function("Math#exp", WrapFunction(MathExp), true)); - mathObj->Set("floor", new Function("Math#floor", WrapFunction(MathFloor), true)); - mathObj->Set("log", new Function("Math#log", WrapFunction(MathLog), true)); - mathObj->Set("max", new Function("Math#max", WrapFunction(MathMax), true)); - mathObj->Set("min", new Function("Math#min", WrapFunction(MathMin), true)); - mathObj->Set("pow", new Function("Math#pow", WrapFunction(MathPow), true)); - mathObj->Set("random", new Function("Math#random", WrapFunction(MathRandom), true)); - mathObj->Set("round", new Function("Math#round", WrapFunction(MathRound), true)); - mathObj->Set("sin", new Function("Math#sin", WrapFunction(MathSin), true)); - mathObj->Set("sqrt", new Function("Math#sqrt", WrapFunction(MathSqrt), true)); - mathObj->Set("tan", new Function("Math#tan", WrapFunction(MathTan), true)); - mathObj->Set("isnan", new Function("Math#isnan", WrapFunction(MathIsnan), true)); - mathObj->Set("isinf", new Function("Math#isinf", WrapFunction(MathIsinf), true)); - mathObj->Set("sign", new Function("Math#sign", WrapFunction(MathSign), true)); + mathObj->Set("abs", new Function("Math#abs", WrapFunction(MathAbs), { "x" }, true)); + mathObj->Set("acos", new Function("Math#acos", WrapFunction(MathAcos), { "x" }, true)); + mathObj->Set("asin", new Function("Math#asin", WrapFunction(MathAsin), { "x" }, true)); + mathObj->Set("atan", new Function("Math#atan", WrapFunction(MathAtan), { "x" }, true)); + mathObj->Set("atan2", new Function("Math#atan2", WrapFunction(MathAtan2), { "x", "y" }, true)); + mathObj->Set("ceil", new Function("Math#ceil", WrapFunction(MathCeil), { "x" }, true)); + mathObj->Set("cos", new Function("Math#cos", WrapFunction(MathCos), { "x" }, true)); + mathObj->Set("exp", new Function("Math#exp", WrapFunction(MathExp), { "x" }, true)); + mathObj->Set("floor", new Function("Math#floor", WrapFunction(MathFloor), { "x" }, true)); + mathObj->Set("log", new Function("Math#log", WrapFunction(MathLog), { "x" }, true)); + mathObj->Set("max", new Function("Math#max", WrapFunction(MathMax), {}, true)); + mathObj->Set("min", new Function("Math#min", WrapFunction(MathMin), {}, true)); + mathObj->Set("pow", new Function("Math#pow", WrapFunction(MathPow), { "x", "y" }, true)); + mathObj->Set("random", new Function("Math#random", WrapFunction(MathRandom), {}, true)); + mathObj->Set("round", new Function("Math#round", WrapFunction(MathRound), { "x" }, true)); + mathObj->Set("sin", new Function("Math#sin", WrapFunction(MathSin), { "x" }, true)); + mathObj->Set("sqrt", new Function("Math#sqrt", WrapFunction(MathSqrt), { "x" }, true)); + mathObj->Set("tan", new Function("Math#tan", WrapFunction(MathTan), { "x" }, true)); + mathObj->Set("isnan", new Function("Math#isnan", WrapFunction(MathIsnan), { "x" }, true)); + mathObj->Set("isinf", new Function("Math#isinf", WrapFunction(MathIsinf), { "x" }, true)); + mathObj->Set("sign", new Function("Math#sign", WrapFunction(MathSign), { "x" }, true)); ScriptGlobal::Set("Math", mathObj); }); diff --git a/lib/base/number-script.cpp b/lib/base/number-script.cpp index f816796df..4366277c7 100644 --- a/lib/base/number-script.cpp +++ b/lib/base/number-script.cpp @@ -37,7 +37,7 @@ Object::Ptr Number::GetPrototype(void) if (!prototype) { prototype = new Dictionary(); - prototype->Set("to_string", new Function("Number#to_string", WrapFunction(NumberToString), true)); + prototype->Set("to_string", new Function("Number#to_string", WrapFunction(NumberToString), {}, true)); } return prototype; diff --git a/lib/base/object-script.cpp b/lib/base/object-script.cpp index f905f1421..c8112a402 100644 --- a/lib/base/object-script.cpp +++ b/lib/base/object-script.cpp @@ -52,9 +52,9 @@ Object::Ptr Object::GetPrototype(void) if (!prototype) { prototype = new Dictionary(); - prototype->Set("to_string", new Function("Object#to_string", WrapFunction(ObjectToString), true)); - prototype->Set("notify_attribute", new Function("Object#notify_attribute", WrapFunction(ObjectNotifyAttribute), false)); - prototype->Set("clone", new Function("Object#clone", WrapFunction(ObjectClone), true)); + prototype->Set("to_string", new Function("Object#to_string", WrapFunction(ObjectToString), {}, true)); + prototype->Set("notify_attribute", new Function("Object#notify_attribute", WrapFunction(ObjectNotifyAttribute), { "attribute" }, false)); + prototype->Set("clone", new Function("Object#clone", WrapFunction(ObjectClone), {}, true)); } return prototype; diff --git a/lib/base/scriptutils.cpp b/lib/base/scriptutils.cpp index 3ef5ff3a9..9cbdc1c5f 100644 --- a/lib/base/scriptutils.cpp +++ b/lib/base/scriptutils.cpp @@ -36,36 +36,36 @@ using namespace icinga; -REGISTER_SAFE_SCRIPTFUNCTION_NS(System, regex, &ScriptUtils::Regex); -REGISTER_SAFE_SCRIPTFUNCTION_NS(System, match, &Utility::Match); -REGISTER_SAFE_SCRIPTFUNCTION_NS(System, cidr_match, &Utility::CidrMatch); -REGISTER_SAFE_SCRIPTFUNCTION_NS(System, len, &ScriptUtils::Len); -REGISTER_SAFE_SCRIPTFUNCTION_NS(System, union, &ScriptUtils::Union); -REGISTER_SAFE_SCRIPTFUNCTION_NS(System, intersection, &ScriptUtils::Intersection); -REGISTER_SCRIPTFUNCTION_NS(System, log, &ScriptUtils::Log); -REGISTER_SCRIPTFUNCTION_NS(System, range, &ScriptUtils::Range); -REGISTER_SCRIPTFUNCTION_NS(System, exit, &Application::Exit); -REGISTER_SAFE_SCRIPTFUNCTION_NS(System, typeof, &ScriptUtils::TypeOf); -REGISTER_SAFE_SCRIPTFUNCTION_NS(System, keys, &ScriptUtils::Keys); -REGISTER_SAFE_SCRIPTFUNCTION_NS(System, random, &Utility::Random); -REGISTER_SAFE_SCRIPTFUNCTION_NS(System, get_object, &ScriptUtils::GetObject); -REGISTER_SAFE_SCRIPTFUNCTION_NS(System, get_objects, &ScriptUtils::GetObjects); -REGISTER_SCRIPTFUNCTION_NS(System, assert, &ScriptUtils::Assert); -REGISTER_SAFE_SCRIPTFUNCTION_NS(System, string, &ScriptUtils::CastString); -REGISTER_SAFE_SCRIPTFUNCTION_NS(System, number, &ScriptUtils::CastNumber); -REGISTER_SAFE_SCRIPTFUNCTION_NS(System, bool, &ScriptUtils::CastBool); -REGISTER_SAFE_SCRIPTFUNCTION_NS(System, get_time, &Utility::GetTime); -REGISTER_SAFE_SCRIPTFUNCTION_NS(System, basename, &Utility::BaseName); -REGISTER_SAFE_SCRIPTFUNCTION_NS(System, dirname, &Utility::DirName); -REGISTER_SAFE_SCRIPTFUNCTION_NS(System, msi_get_component_path, &ScriptUtils::MsiGetComponentPathShim); -REGISTER_SAFE_SCRIPTFUNCTION_NS(System, track_parents, &ScriptUtils::TrackParents); -REGISTER_SAFE_SCRIPTFUNCTION_NS(System, escape_shell_cmd, &Utility::EscapeShellCmd); -REGISTER_SAFE_SCRIPTFUNCTION_NS(System, escape_shell_arg, &Utility::EscapeShellArg); +REGISTER_SAFE_SCRIPTFUNCTION_NS(System, regex, &ScriptUtils::Regex, "pattern:text"); +REGISTER_SAFE_SCRIPTFUNCTION_NS(System, match, &Utility::Match, "pattern:text"); +REGISTER_SAFE_SCRIPTFUNCTION_NS(System, cidr_match, &Utility::CidrMatch, "pattern:ip"); +REGISTER_SAFE_SCRIPTFUNCTION_NS(System, len, &ScriptUtils::Len, "value"); +REGISTER_SAFE_SCRIPTFUNCTION_NS(System, union, &ScriptUtils::Union, ""); +REGISTER_SAFE_SCRIPTFUNCTION_NS(System, intersection, &ScriptUtils::Intersection, ""); +REGISTER_SCRIPTFUNCTION_NS(System, log, &ScriptUtils::Log, "severity:facility:value"); +REGISTER_SCRIPTFUNCTION_NS(System, range, &ScriptUtils::Range, "start:end:increment"); +REGISTER_SCRIPTFUNCTION_NS(System, exit, &Application::Exit, "status"); +REGISTER_SAFE_SCRIPTFUNCTION_NS(System, typeof, &ScriptUtils::TypeOf, "value"); +REGISTER_SAFE_SCRIPTFUNCTION_NS(System, keys, &ScriptUtils::Keys, "value"); +REGISTER_SAFE_SCRIPTFUNCTION_NS(System, random, &Utility::Random, ""); +REGISTER_SAFE_SCRIPTFUNCTION_NS(System, get_object, &ScriptUtils::GetObject, "type:name"); +REGISTER_SAFE_SCRIPTFUNCTION_NS(System, get_objects, &ScriptUtils::GetObjects, "type"); +REGISTER_SCRIPTFUNCTION_NS(System, assert, &ScriptUtils::Assert, "value"); +REGISTER_SAFE_SCRIPTFUNCTION_NS(System, string, &ScriptUtils::CastString, "value"); +REGISTER_SAFE_SCRIPTFUNCTION_NS(System, number, &ScriptUtils::CastNumber, "value"); +REGISTER_SAFE_SCRIPTFUNCTION_NS(System, bool, &ScriptUtils::CastBool, "value"); +REGISTER_SAFE_SCRIPTFUNCTION_NS(System, get_time, &Utility::GetTime, ""); +REGISTER_SAFE_SCRIPTFUNCTION_NS(System, basename, &Utility::BaseName, "path"); +REGISTER_SAFE_SCRIPTFUNCTION_NS(System, dirname, &Utility::DirName, "path"); +REGISTER_SAFE_SCRIPTFUNCTION_NS(System, msi_get_component_path, &ScriptUtils::MsiGetComponentPathShim, "component"); +REGISTER_SAFE_SCRIPTFUNCTION_NS(System, track_parents, &ScriptUtils::TrackParents, "child"); +REGISTER_SAFE_SCRIPTFUNCTION_NS(System, escape_shell_cmd, &Utility::EscapeShellCmd, "cmd"); +REGISTER_SAFE_SCRIPTFUNCTION_NS(System, escape_shell_arg, &Utility::EscapeShellArg, "arg"); #ifdef _WIN32 -REGISTER_SAFE_SCRIPTFUNCTION_NS(System, escape_create_process_arg, &Utility::EscapeCreateProcessArg); +REGISTER_SAFE_SCRIPTFUNCTION_NS(System, escape_create_process_arg, &Utility::EscapeCreateProcessArg, "arg"); #endif /* _WIN32 */ -REGISTER_SCRIPTFUNCTION_NS(System, ptr, &ScriptUtils::Ptr); -REGISTER_SCRIPTFUNCTION_NS(System, sleep, &Utility::Sleep); +REGISTER_SCRIPTFUNCTION_NS(System, ptr, &ScriptUtils::Ptr, "object"); +REGISTER_SCRIPTFUNCTION_NS(System, sleep, &Utility::Sleep, "interval"); String ScriptUtils::CastString(const Value& value) { diff --git a/lib/base/string-script.cpp b/lib/base/string-script.cpp index 1147de9c4..e72370e51 100644 --- a/lib/base/string-script.cpp +++ b/lib/base/string-script.cpp @@ -145,17 +145,17 @@ Object::Ptr String::GetPrototype(void) if (!prototype) { prototype = new Dictionary(); - prototype->Set("len", new Function("String#len", WrapFunction(StringLen), true)); - prototype->Set("to_string", new Function("String#to_string", WrapFunction(StringToString), true)); - prototype->Set("substr", new Function("String#substr", WrapFunction(StringSubstr), true)); - prototype->Set("upper", new Function("String#upper", WrapFunction(StringUpper), true)); - prototype->Set("lower", new Function("String#lower", WrapFunction(StringLower), true)); - prototype->Set("split", new Function("String#split", WrapFunction(StringSplit), true)); - prototype->Set("find", new Function("String#find", WrapFunction(StringFind), true)); - prototype->Set("contains", new Function("String#contains", WrapFunction(StringContains), true)); - prototype->Set("replace", new Function("String#replace", WrapFunction(StringReplace), true)); - prototype->Set("reverse", new Function("String#reverse", WrapFunction(StringReverse), true)); - prototype->Set("trim", new Function("String#trim", WrapFunction(StringTrim), true)); + prototype->Set("len", new Function("String#len", WrapFunction(StringLen), {}, true)); + prototype->Set("to_string", new Function("String#to_string", WrapFunction(StringToString), {}, true)); + prototype->Set("substr", new Function("String#substr", WrapFunction(StringSubstr), { "start", "len" }, true)); + prototype->Set("upper", new Function("String#upper", WrapFunction(StringUpper), {}, true)); + prototype->Set("lower", new Function("String#lower", WrapFunction(StringLower), {}, true)); + prototype->Set("split", new Function("String#split", WrapFunction(StringSplit), {}, true)); + prototype->Set("find", new Function("String#find", WrapFunction(StringFind), { "str", "start" }, true)); + prototype->Set("contains", new Function("String#contains", WrapFunction(StringContains), { "str" }, true)); + prototype->Set("replace", new Function("String#replace", WrapFunction(StringReplace), { "search", "replacement" }, true)); + prototype->Set("reverse", new Function("String#reverse", WrapFunction(StringReverse), {}, true)); + prototype->Set("trim", new Function("String#trim", WrapFunction(StringTrim), {}, true)); } return prototype; diff --git a/lib/base/string.hpp b/lib/base/string.hpp index 38e40a2b8..3016bde34 100644 --- a/lib/base/string.hpp +++ b/lib/base/string.hpp @@ -24,6 +24,7 @@ #include "base/object.hpp" #include #include +#include #include #include #include @@ -238,6 +239,13 @@ public: return m_Data.substr(first, len); } + inline std::vector Split(const char *separators) const + { + std::vector result; + boost::algorithm::split(result, m_Data, boost::is_any_of(separators)); + return result; + } + inline void Replace(SizeType first, SizeType second, const String& str) { m_Data.replace(first, second, str); diff --git a/lib/base/typetype-script.cpp b/lib/base/typetype-script.cpp index 60fbcb9d0..5189845b3 100644 --- a/lib/base/typetype-script.cpp +++ b/lib/base/typetype-script.cpp @@ -48,7 +48,7 @@ Object::Ptr TypeType::GetPrototype(void) if (!prototype) { prototype = new Dictionary(); - prototype->Set("register_attribute_handler", new Function("Type#register_attribute_handler", WrapFunction(TypeRegisterAttributeHandler), false)); + prototype->Set("register_attribute_handler", new Function("Type#register_attribute_handler", WrapFunction(TypeRegisterAttributeHandler), { "field", "callback" }, false)); } return prototype; diff --git a/lib/config/configitem.cpp b/lib/config/configitem.cpp index 86f3118e3..d023487f5 100644 --- a/lib/config/configitem.cpp +++ b/lib/config/configitem.cpp @@ -46,7 +46,7 @@ ConfigItem::TypeMap ConfigItem::m_DefaultTemplates; ConfigItem::ItemList ConfigItem::m_UnnamedItems; ConfigItem::IgnoredItemList ConfigItem::m_IgnoredItems; -REGISTER_SCRIPTFUNCTION_NS(Internal, run_with_activation_context, &ConfigItem::RunWithActivationContext); +REGISTER_SCRIPTFUNCTION_NS(Internal, run_with_activation_context, &ConfigItem::RunWithActivationContext, "func"); /** * Constructor for the ConfigItem class. diff --git a/lib/config/vmops.hpp b/lib/config/vmops.hpp index 7433b5ef4..0c1d45800 100644 --- a/lib/config/vmops.hpp +++ b/lib/config/vmops.hpp @@ -113,7 +113,7 @@ public: std::map *closedVars, const boost::shared_ptr& expression) { return new Function(name, boost::bind(&FunctionWrapper, _1, args, - EvaluateClosedVars(frame, closedVars), expression)); + EvaluateClosedVars(frame, closedVars), expression), args); } static inline Value NewApply(ScriptFrame& frame, const String& type, const String& target, const String& name, const boost::shared_ptr& filter, diff --git a/lib/db_ido/idochecktask.cpp b/lib/db_ido/idochecktask.cpp index f8379dbdd..c5766038b 100644 --- a/lib/db_ido/idochecktask.cpp +++ b/lib/db_ido/idochecktask.cpp @@ -32,7 +32,7 @@ using namespace icinga; -REGISTER_SCRIPTFUNCTION_NS(Internal, IdoCheck, &IdoCheckTask::ScriptFunc); +REGISTER_SCRIPTFUNCTION_NS(Internal, IdoCheck, &IdoCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros"); void IdoCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros) diff --git a/lib/icinga/checkable-script.cpp b/lib/icinga/checkable-script.cpp index c76a61d5d..40626fef9 100644 --- a/lib/icinga/checkable-script.cpp +++ b/lib/icinga/checkable-script.cpp @@ -39,7 +39,7 @@ Object::Ptr Checkable::GetPrototype(void) if (!prototype) { prototype = new Dictionary(); - prototype->Set("process_check_result", new Function("Checkable#process_check_result", WrapFunction(CheckableProcessCheckResult), false)); + prototype->Set("process_check_result", new Function("Checkable#process_check_result", WrapFunction(CheckableProcessCheckResult), { "cr" }, false)); } return prototype; diff --git a/lib/icinga/legacytimeperiod.cpp b/lib/icinga/legacytimeperiod.cpp index 4c93daa4a..6d89839ea 100644 --- a/lib/icinga/legacytimeperiod.cpp +++ b/lib/icinga/legacytimeperiod.cpp @@ -30,7 +30,7 @@ using namespace icinga; -REGISTER_SCRIPTFUNCTION_NS(Internal, LegacyTimePeriod, &LegacyTimePeriod::ScriptFunc); +REGISTER_SCRIPTFUNCTION_NS(Internal, LegacyTimePeriod, &LegacyTimePeriod::ScriptFunc, "tp:begin:end"); bool LegacyTimePeriod::IsInTimeRange(tm *begin, tm *end, int stride, tm *reference) { diff --git a/lib/icinga/objectutils.cpp b/lib/icinga/objectutils.cpp index eef5fdfdc..be58508ba 100644 --- a/lib/icinga/objectutils.cpp +++ b/lib/icinga/objectutils.cpp @@ -29,16 +29,16 @@ using namespace icinga; -REGISTER_SCRIPTFUNCTION_NS(System, get_host, &Host::GetByName); -REGISTER_SCRIPTFUNCTION_NS(System, get_service, &ObjectUtils::GetService); -REGISTER_SCRIPTFUNCTION_NS(System, get_user, &User::GetByName); -REGISTER_SCRIPTFUNCTION_NS(System, get_check_command, &CheckCommand::GetByName); -REGISTER_SCRIPTFUNCTION_NS(System, get_event_command, &EventCommand::GetByName); -REGISTER_SCRIPTFUNCTION_NS(System, get_notification_command, &NotificationCommand::GetByName); -REGISTER_SCRIPTFUNCTION_NS(System, get_host_group, &HostGroup::GetByName); -REGISTER_SCRIPTFUNCTION_NS(System, get_service_group, &ServiceGroup::GetByName); -REGISTER_SCRIPTFUNCTION_NS(System, get_user_group, &UserGroup::GetByName); -REGISTER_SCRIPTFUNCTION_NS(System, get_time_period, &TimePeriod::GetByName); +REGISTER_SCRIPTFUNCTION_NS(System, get_host, &Host::GetByName, "name"); +REGISTER_SCRIPTFUNCTION_NS(System, get_service, &ObjectUtils::GetService, "host:name"); +REGISTER_SCRIPTFUNCTION_NS(System, get_user, &User::GetByName, "name"); +REGISTER_SCRIPTFUNCTION_NS(System, get_check_command, &CheckCommand::GetByName, "name"); +REGISTER_SCRIPTFUNCTION_NS(System, get_event_command, &EventCommand::GetByName, "name"); +REGISTER_SCRIPTFUNCTION_NS(System, get_notification_command, &NotificationCommand::GetByName, "name"); +REGISTER_SCRIPTFUNCTION_NS(System, get_host_group, &HostGroup::GetByName, "name"); +REGISTER_SCRIPTFUNCTION_NS(System, get_service_group, &ServiceGroup::GetByName, "name"); +REGISTER_SCRIPTFUNCTION_NS(System, get_user_group, &UserGroup::GetByName, "name"); +REGISTER_SCRIPTFUNCTION_NS(System, get_time_period, &TimePeriod::GetByName, "name"); Service::Ptr ObjectUtils::GetService(const String& host, const String& name) { diff --git a/lib/icinga/perfdatavalue.cpp b/lib/icinga/perfdatavalue.cpp index b41743f27..d350260bf 100644 --- a/lib/icinga/perfdatavalue.cpp +++ b/lib/icinga/perfdatavalue.cpp @@ -30,7 +30,7 @@ using namespace icinga; REGISTER_TYPE(PerfdataValue); -REGISTER_SCRIPTFUNCTION_NS(System, parse_performance_data, PerfdataValue::Parse); +REGISTER_SCRIPTFUNCTION_NS(System, parse_performance_data, PerfdataValue::Parse, "perfdata"); PerfdataValue::PerfdataValue(void) { } diff --git a/lib/methods/clrchecktask.cpp b/lib/methods/clrchecktask.cpp index dac1b37cd..576538524 100644 --- a/lib/methods/clrchecktask.cpp +++ b/lib/methods/clrchecktask.cpp @@ -38,7 +38,7 @@ using namespace icinga; -REGISTER_SCRIPTFUNCTION_NS(Internal, ClrCheck, &ClrCheckTask::ScriptFunc); +REGISTER_SCRIPTFUNCTION_NS(Internal, ClrCheck, &ClrCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros"); static boost::once_flag l_OnceFlag = BOOST_ONCE_INIT; diff --git a/lib/methods/clusterchecktask.cpp b/lib/methods/clusterchecktask.cpp index 5b296bcbb..2ad7f6578 100644 --- a/lib/methods/clusterchecktask.cpp +++ b/lib/methods/clusterchecktask.cpp @@ -33,7 +33,7 @@ using namespace icinga; -REGISTER_SCRIPTFUNCTION_NS(Internal, ClusterCheck, &ClusterCheckTask::ScriptFunc); +REGISTER_SCRIPTFUNCTION_NS(Internal, ClusterCheck, &ClusterCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros"); void ClusterCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros) diff --git a/lib/methods/clusterzonechecktask.cpp b/lib/methods/clusterzonechecktask.cpp index a19464fd5..f91c5259c 100644 --- a/lib/methods/clusterzonechecktask.cpp +++ b/lib/methods/clusterzonechecktask.cpp @@ -29,7 +29,7 @@ using namespace icinga; -REGISTER_SCRIPTFUNCTION_NS(Internal, ClusterZoneCheck, &ClusterZoneCheckTask::ScriptFunc); +REGISTER_SCRIPTFUNCTION_NS(Internal, ClusterZoneCheck, &ClusterZoneCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros"); void ClusterZoneCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros) diff --git a/lib/methods/exceptionchecktask.cpp b/lib/methods/exceptionchecktask.cpp index 83801bf31..0410621ab 100644 --- a/lib/methods/exceptionchecktask.cpp +++ b/lib/methods/exceptionchecktask.cpp @@ -29,7 +29,7 @@ using namespace icinga; -REGISTER_SCRIPTFUNCTION_NS(Internal, ExceptionCheck, &ExceptionCheckTask::ScriptFunc); +REGISTER_SCRIPTFUNCTION_NS(Internal, ExceptionCheck, &ExceptionCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros"); void ExceptionCheckTask::ScriptFunc(const Checkable::Ptr& service, const CheckResult::Ptr& cr, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros) diff --git a/lib/methods/icingachecktask.cpp b/lib/methods/icingachecktask.cpp index c475ac88f..7b5c54ec3 100644 --- a/lib/methods/icingachecktask.cpp +++ b/lib/methods/icingachecktask.cpp @@ -30,7 +30,7 @@ using namespace icinga; -REGISTER_SCRIPTFUNCTION_NS(Internal, IcingaCheck, &IcingaCheckTask::ScriptFunc); +REGISTER_SCRIPTFUNCTION_NS(Internal, IcingaCheck, &IcingaCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros"); void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& service, const CheckResult::Ptr& cr, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros) diff --git a/lib/methods/nullchecktask.cpp b/lib/methods/nullchecktask.cpp index 48ab036b3..564f3daf8 100644 --- a/lib/methods/nullchecktask.cpp +++ b/lib/methods/nullchecktask.cpp @@ -30,7 +30,7 @@ using namespace icinga; -REGISTER_SCRIPTFUNCTION_NS(Internal, NullCheck, &NullCheckTask::ScriptFunc); +REGISTER_SCRIPTFUNCTION_NS(Internal, NullCheck, &NullCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros"); void NullCheckTask::ScriptFunc(const Checkable::Ptr& service, const CheckResult::Ptr& cr, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros) diff --git a/lib/methods/nulleventtask.cpp b/lib/methods/nulleventtask.cpp index 98ef729e9..2d56e8a34 100644 --- a/lib/methods/nulleventtask.cpp +++ b/lib/methods/nulleventtask.cpp @@ -23,7 +23,7 @@ using namespace icinga; -REGISTER_SCRIPTFUNCTION_NS(Internal, NullEvent, &NullEventTask::ScriptFunc); +REGISTER_SCRIPTFUNCTION_NS(Internal, NullEvent, &NullEventTask::ScriptFunc, "checkable:resolvedMacros:useResolvedMacros"); void NullEventTask::ScriptFunc(const Checkable::Ptr&, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros) { } diff --git a/lib/methods/pluginchecktask.cpp b/lib/methods/pluginchecktask.cpp index 96631f8ff..abf2b0cd5 100644 --- a/lib/methods/pluginchecktask.cpp +++ b/lib/methods/pluginchecktask.cpp @@ -33,7 +33,7 @@ using namespace icinga; -REGISTER_SCRIPTFUNCTION_NS(Internal, PluginCheck, &PluginCheckTask::ScriptFunc); +REGISTER_SCRIPTFUNCTION_NS(Internal, PluginCheck, &PluginCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros"); void PluginCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros) diff --git a/lib/methods/plugineventtask.cpp b/lib/methods/plugineventtask.cpp index 56698dfda..48a0b9ce0 100644 --- a/lib/methods/plugineventtask.cpp +++ b/lib/methods/plugineventtask.cpp @@ -31,7 +31,7 @@ using namespace icinga; -REGISTER_SCRIPTFUNCTION_NS(Internal, PluginEvent, &PluginEventTask::ScriptFunc); +REGISTER_SCRIPTFUNCTION_NS(Internal, PluginEvent, &PluginEventTask::ScriptFunc, "checkable:resolvedMacros:useResolvedMacros"); void PluginEventTask::ScriptFunc(const Checkable::Ptr& checkable, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros) diff --git a/lib/methods/pluginnotificationtask.cpp b/lib/methods/pluginnotificationtask.cpp index 64cbb2ca2..42403e7bb 100644 --- a/lib/methods/pluginnotificationtask.cpp +++ b/lib/methods/pluginnotificationtask.cpp @@ -32,7 +32,7 @@ using namespace icinga; -REGISTER_SCRIPTFUNCTION_NS(Internal, PluginNotification, &PluginNotificationTask::ScriptFunc); +REGISTER_SCRIPTFUNCTION_NS(Internal, PluginNotification, &PluginNotificationTask::ScriptFunc, "notification:user:cr:itype:author:comment:resolvedMacros:useResolvedMacros"); void PluginNotificationTask::ScriptFunc(const Notification::Ptr& notification, const User::Ptr& user, const CheckResult::Ptr& cr, int itype, diff --git a/lib/methods/randomchecktask.cpp b/lib/methods/randomchecktask.cpp index 9fd018795..a0b90ccfe 100644 --- a/lib/methods/randomchecktask.cpp +++ b/lib/methods/randomchecktask.cpp @@ -30,7 +30,7 @@ using namespace icinga; -REGISTER_SCRIPTFUNCTION_NS(Internal, RandomCheck, &RandomCheckTask::ScriptFunc); +REGISTER_SCRIPTFUNCTION_NS(Internal, RandomCheck, &RandomCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros"); void RandomCheckTask::ScriptFunc(const Checkable::Ptr& service, const CheckResult::Ptr& cr, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros) diff --git a/lib/methods/timeperiodtask.cpp b/lib/methods/timeperiodtask.cpp index 09c669370..c26b4cedd 100644 --- a/lib/methods/timeperiodtask.cpp +++ b/lib/methods/timeperiodtask.cpp @@ -22,8 +22,8 @@ using namespace icinga; -REGISTER_SCRIPTFUNCTION_NS(Internal, EmptyTimePeriod, &TimePeriodTask::EmptyTimePeriodUpdate); -REGISTER_SCRIPTFUNCTION_NS(Internal, EvenMinutesTimePeriod, &TimePeriodTask::EvenMinutesTimePeriodUpdate); +REGISTER_SCRIPTFUNCTION_NS(Internal, EmptyTimePeriod, &TimePeriodTask::EmptyTimePeriodUpdate, "tp:begin:end"); +REGISTER_SCRIPTFUNCTION_NS(Internal, EvenMinutesTimePeriod, &TimePeriodTask::EvenMinutesTimePeriodUpdate, "tp:begin:end"); Array::Ptr TimePeriodTask::EmptyTimePeriodUpdate(const TimePeriod::Ptr&, double, double) {