diff --git a/lib/config/applyrule-targeted.cpp b/lib/config/applyrule-targeted.cpp index 210c67764..c5bfe2086 100644 --- a/lib/config/applyrule-targeted.cpp +++ b/lib/config/applyrule-targeted.cpp @@ -96,16 +96,16 @@ bool ApplyRule::AddTargetedRule(const ApplyRule::Ptr& rule, const String& target * * @returns Whether the given assign filter is like above. */ -bool ApplyRule::GetTargetHosts(Expression* assignFilter, std::vector& hosts) +bool ApplyRule::GetTargetHosts(Expression* assignFilter, std::vector& hosts, const Dictionary::Ptr& constants) { auto lor (dynamic_cast(assignFilter)); if (lor) { - return GetTargetHosts(lor->GetOperand1().get(), hosts) - && GetTargetHosts(lor->GetOperand2().get(), hosts); + return GetTargetHosts(lor->GetOperand1().get(), hosts, constants) + && GetTargetHosts(lor->GetOperand2().get(), hosts, constants); } - auto name (GetComparedName(assignFilter, "host")); + auto name (GetComparedName(assignFilter, "host", constants)); if (name) { hosts.emplace_back(name); @@ -124,16 +124,16 @@ bool ApplyRule::GetTargetHosts(Expression* assignFilter, std::vector>& services) +bool ApplyRule::GetTargetServices(Expression* assignFilter, std::vector>& services, const Dictionary::Ptr& constants) { auto lor (dynamic_cast(assignFilter)); if (lor) { - return GetTargetServices(lor->GetOperand1().get(), services) - && GetTargetServices(lor->GetOperand2().get(), services); + return GetTargetServices(lor->GetOperand1().get(), services, constants) + && GetTargetServices(lor->GetOperand2().get(), services, constants); } - auto service (GetTargetService(assignFilter)); + auto service (GetTargetService(assignFilter, constants)); if (service.first) { services.emplace_back(service); @@ -152,7 +152,7 @@ bool ApplyRule::GetTargetServices(Expression* assignFilter, std::vector ApplyRule::GetTargetService(Expression* assignFilter) +std::pair ApplyRule::GetTargetService(Expression* assignFilter, const Dictionary::Ptr& constants) { auto land (dynamic_cast(assignFilter)); @@ -162,15 +162,15 @@ std::pair ApplyRule::GetTargetService(Expression auto op1 (land->GetOperand1().get()); auto op2 (land->GetOperand2().get()); - auto host (GetComparedName(op1, "host")); + auto host (GetComparedName(op1, "host", constants)); if (!host) { std::swap(op1, op2); - host = GetComparedName(op1, "host"); + host = GetComparedName(op1, "host", constants); } if (host) { - auto service (GetComparedName(op2, "service")); + auto service (GetComparedName(op2, "service", constants)); if (service) { return {host, service}; @@ -189,7 +189,7 @@ std::pair ApplyRule::GetTargetService(Expression * * @returns The object name on success and nullptr on failure. */ -const String * ApplyRule::GetComparedName(Expression* assignFilter, const char * lcType) +const String * ApplyRule::GetComparedName(Expression* assignFilter, const char * lcType, const Dictionary::Ptr& constants) { auto eq (dynamic_cast(assignFilter)); @@ -200,12 +200,12 @@ const String * ApplyRule::GetComparedName(Expression* assignFilter, const char * auto op1 (eq->GetOperand1().get()); auto op2 (eq->GetOperand2().get()); - if (IsNameIndexer(op1, lcType)) { - return GetLiteralStringValue(op2); + if (IsNameIndexer(op1, lcType, constants)) { + return GetConstString(op2, constants); } - if (IsNameIndexer(op2, lcType)) { - return GetLiteralStringValue(op1); + if (IsNameIndexer(op2, lcType, constants)) { + return GetConstString(op1, constants); } return nullptr; @@ -214,7 +214,7 @@ const String * ApplyRule::GetComparedName(Expression* assignFilter, const char * /** * @returns Whether the given expression is like $lcType$.name. */ -bool ApplyRule::IsNameIndexer(Expression* exp, const char * lcType) +bool ApplyRule::IsNameIndexer(Expression* exp, const char * lcType, const Dictionary::Ptr& constants) { auto ixr (dynamic_cast(exp)); @@ -228,27 +228,39 @@ bool ApplyRule::IsNameIndexer(Expression* exp, const char * lcType) return false; } - auto val (GetLiteralStringValue(ixr->GetOperand2().get())); + auto val (GetConstString(ixr->GetOperand2().get(), constants)); return val && *val == "name"; } /** - * @returns If the given expression is a string literal, the string. nullptr on failure. + * @returns If the given expression is a constant string, its address. nullptr on failure. */ -const String * ApplyRule::GetLiteralStringValue(Expression* exp) +const String * ApplyRule::GetConstString(Expression* exp, const Dictionary::Ptr& constants) +{ + auto cnst (GetConst(exp, constants)); + + return cnst && cnst->IsString() ? &cnst->Get() : nullptr; +} + +/** + * @returns If the given expression is a constant, its address. nullptr on failure. + */ +const Value * ApplyRule::GetConst(Expression* exp, const Dictionary::Ptr& constants) { auto lit (dynamic_cast(exp)); - if (!lit) { - return nullptr; + if (lit) { + return &lit->GetValue(); } - auto& val (lit->GetValue()); + if (constants) { + auto var (dynamic_cast(exp)); - if (!val.IsString()) { - return nullptr; + if (var) { + return constants->GetRef(var->GetVariable()); + } } - return &val.Get(); + return nullptr; } diff --git a/lib/config/applyrule.hpp b/lib/config/applyrule.hpp index c5f1709dd..40251a4a8 100644 --- a/lib/config/applyrule.hpp +++ b/lib/config/applyrule.hpp @@ -108,12 +108,13 @@ private: static RuleMap m_Rules; static bool AddTargetedRule(const ApplyRule::Ptr& rule, const String& targetType, PerSourceType& rules); - static bool GetTargetHosts(Expression* assignFilter, std::vector& hosts); - static bool GetTargetServices(Expression* assignFilter, std::vector>& services); - static std::pair GetTargetService(Expression* assignFilter); - static const String * GetComparedName(Expression* assignFilter, const char * lcType); - static bool IsNameIndexer(Expression* exp, const char * lcType); - static const String * GetLiteralStringValue(Expression* exp); + static bool GetTargetHosts(Expression* assignFilter, std::vector& hosts, const Dictionary::Ptr& constants = nullptr); + static bool GetTargetServices(Expression* assignFilter, std::vector>& services, const Dictionary::Ptr& constants = nullptr); + static std::pair GetTargetService(Expression* assignFilter, const Dictionary::Ptr& constants); + static const String * GetComparedName(Expression* assignFilter, const char * lcType, const Dictionary::Ptr& constants); + static bool IsNameIndexer(Expression* exp, const char * lcType, const Dictionary::Ptr& constants); + static const String * GetConstString(Expression* exp, const Dictionary::Ptr& constants); + static const Value * GetConst(Expression* exp, const Dictionary::Ptr& constants); ApplyRule(String name, Expression::Ptr expression, Expression::Ptr filter, String package, String fkvar, String fvvar, Expression::Ptr fterm,