diff --git a/icinga-app/icinga.cpp b/icinga-app/icinga.cpp index 38525fc65..64c9585ae 100644 --- a/icinga-app/icinga.cpp +++ b/icinga-app/icinga.cpp @@ -27,7 +27,7 @@ #include "base/utility.hpp" #include "base/exception.hpp" #include "base/convert.hpp" -#include "base/scriptvariable.hpp" +#include "base/scriptglobal.hpp" #include "base/context.hpp" #include "base/console.hpp" #include "config.h" @@ -227,7 +227,7 @@ int Main(void) key = define; value = "1"; } - ScriptVariable::Set(key, value); + ScriptGlobal::Set(key, value); } } diff --git a/lib/base/CMakeLists.txt b/lib/base/CMakeLists.txt index 75e1783a3..c1ca8dfae 100644 --- a/lib/base/CMakeLists.txt +++ b/lib/base/CMakeLists.txt @@ -27,8 +27,8 @@ set(base_SOURCES convert.cpp debuginfo.cpp dictionary.cpp dictionary-script.cpp dynamicobject.cpp dynamicobject.thpp dynamictype.cpp exception.cpp fifo.cpp filelogger.cpp filelogger.thpp initialize.cpp json.cpp logger.cpp logger.thpp netstring.cpp networkstream.cpp number.cpp number-script.cpp object.cpp object-script.cpp primitivetype.cpp process.cpp - ringbuffer.cpp scripterror.cpp scriptframe.cpp scriptfunction.cpp scriptfunctionwrapper.cpp scriptsignal.cpp - scriptutils.cpp scriptvariable.cpp serializer.cpp socket.cpp stacktrace.cpp + ringbuffer.cpp scripterror.cpp scriptframe.cpp scriptfunction.cpp scriptfunctionwrapper.cpp scriptglobal.cpp + scriptutils.cpp serializer.cpp socket.cpp stacktrace.cpp statsfunction.cpp stdiostream.cpp stream.cpp streamlogger.cpp streamlogger.thpp string.cpp string-script.cpp sysloglogger.cpp sysloglogger.thpp tcpsocket.cpp thinmutex.cpp threadpool.cpp timer.cpp tlsstream.cpp tlsutility.cpp type.cpp unixsocket.cpp utility.cpp value.cpp diff --git a/lib/base/application.cpp b/lib/base/application.cpp index 21dce4503..f8ddc33bd 100644 --- a/lib/base/application.cpp +++ b/lib/base/application.cpp @@ -27,7 +27,7 @@ #include "base/debug.hpp" #include "base/type.hpp" #include "base/convert.hpp" -#include "base/scriptvariable.hpp" +#include "base/scriptglobal.hpp" #include "base/process.hpp" #include #include @@ -846,7 +846,7 @@ pid_t Application::ReadPidFile(const String& filename) */ String Application::GetPrefixDir(void) { - return ScriptVariable::Get("PrefixDir"); + return ScriptGlobal::Get("PrefixDir"); } /** @@ -856,7 +856,7 @@ String Application::GetPrefixDir(void) */ void Application::DeclarePrefixDir(const String& path) { - ScriptVariable::Set("PrefixDir", path, false); + ScriptGlobal::Set("PrefixDir", path); } /** @@ -866,7 +866,7 @@ void Application::DeclarePrefixDir(const String& path) */ String Application::GetSysconfDir(void) { - return ScriptVariable::Get("SysconfDir"); + return ScriptGlobal::Get("SysconfDir"); } /** @@ -876,7 +876,7 @@ String Application::GetSysconfDir(void) */ void Application::DeclareSysconfDir(const String& path) { - ScriptVariable::Set("SysconfDir", path, false); + ScriptGlobal::Set("SysconfDir", path); } /** @@ -886,7 +886,7 @@ void Application::DeclareSysconfDir(const String& path) */ String Application::GetRunDir(void) { - return ScriptVariable::Get("RunDir"); + return ScriptGlobal::Get("RunDir"); } /** @@ -896,7 +896,7 @@ String Application::GetRunDir(void) */ void Application::DeclareRunDir(const String& path) { - ScriptVariable::Set("RunDir", path, false); + ScriptGlobal::Set("RunDir", path); } /** @@ -906,7 +906,7 @@ void Application::DeclareRunDir(const String& path) */ String Application::GetLocalStateDir(void) { - return ScriptVariable::Get("LocalStateDir"); + return ScriptGlobal::Get("LocalStateDir"); } /** @@ -916,7 +916,7 @@ String Application::GetLocalStateDir(void) */ void Application::DeclareLocalStateDir(const String& path) { - ScriptVariable::Set("LocalStateDir", path, false); + ScriptGlobal::Set("LocalStateDir", path); } /** @@ -926,7 +926,7 @@ void Application::DeclareLocalStateDir(const String& path) */ String Application::GetZonesDir(void) { - return ScriptVariable::Get("ZonesDir", &Empty); + return ScriptGlobal::Get("ZonesDir", &Empty); } /** @@ -936,7 +936,7 @@ String Application::GetZonesDir(void) */ void Application::DeclareZonesDir(const String& path) { - ScriptVariable::Set("ZonesDir", path, false); + ScriptGlobal::Set("ZonesDir", path); } /** @@ -947,7 +947,7 @@ void Application::DeclareZonesDir(const String& path) String Application::GetPkgDataDir(void) { String defaultValue = ""; - return ScriptVariable::Get("PkgDataDir", &Empty); + return ScriptGlobal::Get("PkgDataDir", &Empty); } /** @@ -957,7 +957,7 @@ String Application::GetPkgDataDir(void) */ void Application::DeclarePkgDataDir(const String& path) { - ScriptVariable::Set("PkgDataDir", path, false); + ScriptGlobal::Set("PkgDataDir", path); } /** @@ -967,7 +967,7 @@ void Application::DeclarePkgDataDir(const String& path) */ String Application::GetIncludeConfDir(void) { - return ScriptVariable::Get("IncludeConfDir", &Empty); + return ScriptGlobal::Get("IncludeConfDir", &Empty); } /** @@ -977,7 +977,7 @@ String Application::GetIncludeConfDir(void) */ void Application::DeclareIncludeConfDir(const String& path) { - ScriptVariable::Set("IncludeConfDir", path, false); + ScriptGlobal::Set("IncludeConfDir", path); } /** @@ -987,7 +987,7 @@ void Application::DeclareIncludeConfDir(const String& path) */ String Application::GetStatePath(void) { - return ScriptVariable::Get("StatePath", &Empty); + return ScriptGlobal::Get("StatePath", &Empty); } /** @@ -997,7 +997,7 @@ String Application::GetStatePath(void) */ void Application::DeclareStatePath(const String& path) { - ScriptVariable::Set("StatePath", path, false); + ScriptGlobal::Set("StatePath", path); } /** @@ -1007,7 +1007,7 @@ void Application::DeclareStatePath(const String& path) */ String Application::GetObjectsPath(void) { - return ScriptVariable::Get("ObjectsPath", &Empty); + return ScriptGlobal::Get("ObjectsPath", &Empty); } /** @@ -1017,7 +1017,7 @@ String Application::GetObjectsPath(void) */ void Application::DeclareObjectsPath(const String& path) { - ScriptVariable::Set("ObjectsPath", path, false); + ScriptGlobal::Set("ObjectsPath", path); } /** @@ -1027,7 +1027,7 @@ void Application::DeclareObjectsPath(const String& path) */ String Application::GetVarsPath(void) { - return ScriptVariable::Get("VarsPath", &Empty); + return ScriptGlobal::Get("VarsPath", &Empty); } /** @@ -1037,7 +1037,7 @@ String Application::GetVarsPath(void) */ void Application::DeclareVarsPath(const String& path) { - ScriptVariable::Set("VarsPath", path, false); + ScriptGlobal::Set("VarsPath", path); } /** @@ -1047,7 +1047,7 @@ void Application::DeclareVarsPath(const String& path) */ String Application::GetPidPath(void) { - return ScriptVariable::Get("PidPath", &Empty); + return ScriptGlobal::Get("PidPath", &Empty); } /** @@ -1057,7 +1057,7 @@ String Application::GetPidPath(void) */ void Application::DeclarePidPath(const String& path) { - ScriptVariable::Set("PidPath", path, false); + ScriptGlobal::Set("PidPath", path); } /** @@ -1067,7 +1067,7 @@ void Application::DeclarePidPath(const String& path) */ String Application::GetApplicationType(void) { - return ScriptVariable::Get("ApplicationType"); + return ScriptGlobal::Get("ApplicationType"); } /** @@ -1077,7 +1077,7 @@ String Application::GetApplicationType(void) */ void Application::DeclareApplicationType(const String& type) { - ScriptVariable::Set("ApplicationType", type, false); + ScriptGlobal::Set("ApplicationType", type); } /** @@ -1087,7 +1087,7 @@ void Application::DeclareApplicationType(const String& type) */ String Application::GetRunAsUser(void) { - return ScriptVariable::Get("RunAsUser"); + return ScriptGlobal::Get("RunAsUser"); } /** @@ -1097,7 +1097,7 @@ String Application::GetRunAsUser(void) */ void Application::DeclareRunAsUser(const String& user) { - ScriptVariable::Set("RunAsUser", user, false); + ScriptGlobal::Set("RunAsUser", user); } /** @@ -1107,7 +1107,7 @@ void Application::DeclareRunAsUser(const String& user) */ String Application::GetRunAsGroup(void) { - return ScriptVariable::Get("RunAsGroup"); + return ScriptGlobal::Get("RunAsGroup"); } /** @@ -1117,7 +1117,7 @@ String Application::GetRunAsGroup(void) */ void Application::DeclareConcurrency(int ncpus) { - ScriptVariable::Set("Concurrency", ncpus, false); + ScriptGlobal::Set("Concurrency", ncpus); } /** @@ -1128,7 +1128,7 @@ void Application::DeclareConcurrency(int ncpus) int Application::GetConcurrency(void) { Value defaultConcurrency = boost::thread::hardware_concurrency(); - return ScriptVariable::Get("Concurrency", &defaultConcurrency); + return ScriptGlobal::Get("Concurrency", &defaultConcurrency); } /** @@ -1138,22 +1138,7 @@ int Application::GetConcurrency(void) */ void Application::DeclareRunAsGroup(const String& group) { - ScriptVariable::Set("RunAsGroup", group, false); -} - -void Application::MakeVariablesConstant(void) -{ - ScriptVariable::GetByName("PrefixDir")->SetConstant(true); - ScriptVariable::GetByName("SysconfDir")->SetConstant(true); - ScriptVariable::GetByName("LocalStateDir")->SetConstant(true); - ScriptVariable::GetByName("RunDir")->SetConstant(true); - ScriptVariable::GetByName("PkgDataDir")->SetConstant(true); - ScriptVariable::GetByName("StatePath")->SetConstant(true); - ScriptVariable::GetByName("ObjectsPath")->SetConstant(true); - ScriptVariable::GetByName("PidPath")->SetConstant(true); - ScriptVariable::GetByName("ApplicationType")->SetConstant(true); - ScriptVariable::GetByName("RunAsUser")->SetConstant(true); - ScriptVariable::GetByName("RunAsGroup")->SetConstant(true); + ScriptGlobal::Set("RunAsGroup", group); } /** diff --git a/lib/base/application.hpp b/lib/base/application.hpp index 8ae7cdd28..2a484c9ea 100644 --- a/lib/base/application.hpp +++ b/lib/base/application.hpp @@ -126,8 +126,6 @@ public: static int GetConcurrency(void); static void DeclareConcurrency(int ncpus); - static void MakeVariablesConstant(void); - static ThreadPool& GetTP(void); static String GetVersion(void); diff --git a/lib/base/dynamicobject.cpp b/lib/base/dynamicobject.cpp index bc33d7ef2..38b7acb96 100644 --- a/lib/base/dynamicobject.cpp +++ b/lib/base/dynamicobject.cpp @@ -29,7 +29,6 @@ #include "base/exception.hpp" #include "base/scriptfunction.hpp" #include "base/initialize.hpp" -#include "base/scriptvariable.hpp" #include "base/workqueue.hpp" #include "base/context.hpp" #include diff --git a/lib/base/logger.cpp b/lib/base/logger.cpp index 4dade74f3..e4a1fcbd6 100644 --- a/lib/base/logger.cpp +++ b/lib/base/logger.cpp @@ -24,7 +24,7 @@ #include "base/utility.hpp" #include "base/objectlock.hpp" #include "base/context.hpp" -#include "base/scriptvariable.hpp" +#include "base/scriptglobal.hpp" #include #include @@ -41,11 +41,11 @@ LogSeverity Logger::m_ConsoleLogSeverity = LogInformation; void Logger::StaticInitialize(void) { - ScriptVariable::Set("LogDebug", LogDebug, true, true); - ScriptVariable::Set("LogNotice", LogNotice, true, true); - ScriptVariable::Set("LogInformation", LogInformation, true, true); - ScriptVariable::Set("LogWarning", LogWarning, true, true); - ScriptVariable::Set("LogCritical", LogCritical, true, true); + ScriptGlobal::Set("LogDebug", LogDebug); + ScriptGlobal::Set("LogNotice", LogNotice); + ScriptGlobal::Set("LogInformation", LogInformation); + ScriptGlobal::Set("LogWarning", LogWarning); + ScriptGlobal::Set("LogCritical", LogCritical); } /** diff --git a/lib/base/process.cpp b/lib/base/process.cpp index 5e766bf0e..40d9392e3 100644 --- a/lib/base/process.cpp +++ b/lib/base/process.cpp @@ -26,7 +26,7 @@ #include "base/initialize.hpp" #include "base/logger.hpp" #include "base/utility.hpp" -#include "base/scriptvariable.hpp" +#include "base/scriptglobal.hpp" #include #include #include @@ -510,7 +510,7 @@ void Process::Run(const boost::function& callback) m_ExtraEnvironment.reset(); #ifdef HAVE_VFORK - Value use_vfork = ScriptVariable::Get("UseVfork"); + Value use_vfork = ScriptGlobal::Get("UseVfork"); if (use_vfork.IsEmpty() || static_cast(use_vfork)) m_Process = vfork(); diff --git a/lib/base/scriptfunction.cpp b/lib/base/scriptfunction.cpp index ee400234f..7ccfa59eb 100644 --- a/lib/base/scriptfunction.cpp +++ b/lib/base/scriptfunction.cpp @@ -18,7 +18,6 @@ ******************************************************************************/ #include "base/scriptfunction.hpp" -#include "base/scriptvariable.hpp" #include "base/primitivetype.hpp" #include "base/dictionary.hpp" @@ -46,25 +45,3 @@ Value ScriptFunction::Invoke(const std::vector& arguments) return m_Callback(arguments); } -ScriptFunction::Ptr ScriptFunction::GetByName(const String& name) -{ - ScriptVariable::Ptr sv = ScriptVariable::GetByName(name); - - if (!sv) - return ScriptFunction::Ptr(); - - return sv->GetData(); -} - -void ScriptFunction::Register(const String& name, const ScriptFunction::Ptr& function) -{ - ScriptVariable::Ptr sv = ScriptVariable::Set(name, function); - sv->SetConstant(true); -} - -void ScriptFunction::Unregister(const String& name) -{ - ScriptVariable::Unregister(name); -} - - diff --git a/lib/base/scriptfunction.hpp b/lib/base/scriptfunction.hpp index 78ef40aa7..6bdcee145 100644 --- a/lib/base/scriptfunction.hpp +++ b/lib/base/scriptfunction.hpp @@ -23,6 +23,7 @@ #include "base/i2-base.hpp" #include "base/value.hpp" #include "base/scriptfunctionwrapper.hpp" +#include "base/scriptglobal.hpp" #include #include @@ -45,10 +46,6 @@ public: Value Invoke(const std::vector& arguments); - static ScriptFunction::Ptr GetByName(const String& name); - static void Register(const String& name, const ScriptFunction::Ptr& function); - static void Unregister(const String& name); - private: Callback m_Callback; }; @@ -57,7 +54,7 @@ private: namespace { namespace UNIQUE_NAME(sf) { namespace sf ## name { \ void RegisterFunction(void) { \ ScriptFunction::Ptr sf = new icinga::ScriptFunction(WrapScriptFunction(callback)); \ - ScriptFunction::Register(#name, sf); \ + ScriptGlobal::Set(#name, sf); \ } \ INITIALIZE_ONCE(RegisterFunction); \ } } } diff --git a/lib/base/scriptvariable.cpp b/lib/base/scriptglobal.cpp similarity index 62% rename from lib/base/scriptvariable.cpp rename to lib/base/scriptglobal.cpp index 3fe1bfc59..ddcf23fcd 100644 --- a/lib/base/scriptvariable.cpp +++ b/lib/base/scriptglobal.cpp @@ -17,89 +17,46 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * ******************************************************************************/ -#include "base/scriptvariable.hpp" +#include "base/scriptglobal.hpp" #include "base/singleton.hpp" #include "base/logger.hpp" #include "base/stdiostream.hpp" #include "base/netstring.hpp" #include "base/json.hpp" #include "base/convert.hpp" +#include "base/objectlock.hpp" #include #include using namespace icinga; -ScriptVariable::ScriptVariable(const Value& data) - : m_Data(data), m_Constant(false) -{ } +Dictionary::Ptr ScriptGlobal::m_Globals = new Dictionary(); -ScriptVariable::Ptr ScriptVariable::GetByName(const String& name) +Value ScriptGlobal::Get(const String& name, const Value *defaultValue) { - return ScriptVariableRegistry::GetInstance()->GetItem(name); -} - -void ScriptVariable::SetConstant(bool constant) -{ - m_Constant = constant; -} - -bool ScriptVariable::IsConstant(void) const -{ - return m_Constant; -} - -void ScriptVariable::SetData(const Value& data) -{ - m_Data = data; -} - -Value ScriptVariable::GetData(void) const -{ - return m_Data; -} - -Value ScriptVariable::Get(const String& name, const Value *defaultValue) -{ - ScriptVariable::Ptr sv = GetByName(name); - - if (!sv) { + if (!m_Globals->Contains(name)) { if (defaultValue) return *defaultValue; BOOST_THROW_EXCEPTION(std::invalid_argument("Tried to access undefined script variable '" + name + "'")); } - return sv->GetData(); + return m_Globals->Get(name); } -ScriptVariable::Ptr ScriptVariable::Set(const String& name, const Value& value, bool overwrite, bool make_const) +void ScriptGlobal::Set(const String& name, const Value& value) { - ScriptVariable::Ptr sv = GetByName(name); - - if (!sv) { - sv = new ScriptVariable(value); - ScriptVariableRegistry::GetInstance()->Register(name, sv); - } else if (overwrite) { - if (sv->IsConstant()) - BOOST_THROW_EXCEPTION(std::invalid_argument("Tried to modify read-only script variable '" + name + "'")); - - sv->SetData(value); - } - - if (make_const) - sv->SetConstant(true); - - return sv; + m_Globals->Set(name, value); } -void ScriptVariable::Unregister(const String& name) +Dictionary::Ptr ScriptGlobal::GetGlobals(void) { - ScriptVariableRegistry::GetInstance()->Unregister(name); + return m_Globals; } -void ScriptVariable::WriteVariablesFile(const String& filename) +void ScriptGlobal::WriteToFile(const String& filename) { - Log(LogInformation, "ScriptVariable") + Log(LogInformation, "ScriptGlobal") << "Dumping variables to file '" << filename << "'"; String tempFilename = filename + ".tmp"; @@ -112,13 +69,13 @@ void ScriptVariable::WriteVariablesFile(const String& filename) StdioStream::Ptr sfp = new StdioStream(&fp, false); - BOOST_FOREACH(const ScriptVariableRegistry::ItemMap::value_type& kv, ScriptVariableRegistry::GetInstance()->GetItems()) { + ObjectLock olock(m_Globals); + BOOST_FOREACH(const Dictionary::Pair& kv, m_Globals) { Dictionary::Ptr persistentVariable = new Dictionary(); persistentVariable->Set("name", kv.first); - ScriptVariable::Ptr sv = kv.second; - Value value = sv->GetData(); + Value value = kv.second; if (value.IsObject()) value = Convert::ToString(value); @@ -146,8 +103,3 @@ void ScriptVariable::WriteVariablesFile(const String& filename) } } -ScriptVariableRegistry *ScriptVariableRegistry::GetInstance(void) -{ - return Singleton::GetInstance(); -} - diff --git a/lib/base/scriptsignal.cpp b/lib/base/scriptglobal.hpp similarity index 66% rename from lib/base/scriptsignal.cpp rename to lib/base/scriptglobal.hpp index e78fade6c..2c2af1bfb 100644 --- a/lib/base/scriptsignal.cpp +++ b/lib/base/scriptglobal.hpp @@ -17,40 +17,34 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * ******************************************************************************/ -#include "base/scriptsignal.hpp" -#include "base/scriptvariable.hpp" +#ifndef SCRIPTGLOBAL_H +#define SCRIPTGLOBAL_H -using namespace icinga; +#include "base/i2-base.hpp" +#include "base/dictionary.hpp" -void ScriptSignal::AddSlot(const Callback& slot) +namespace icinga { - m_Slots.push_back(slot); -} - -void ScriptSignal::Invoke(const std::vector& arguments) -{ - BOOST_FOREACH(const Callback& slot, m_Slots) - slot(arguments); -} - -ScriptSignal::Ptr ScriptSignal::GetByName(const String& name) -{ - ScriptVariable::Ptr sv = ScriptVariable::GetByName(name); - - if (!sv) - return ScriptSignal::Ptr(); - - return sv->GetData(); -} - -void ScriptSignal::Register(const String& name, const ScriptSignal::Ptr& function) -{ - ScriptVariable::Ptr sv = ScriptVariable::Set(name, function); - sv->SetConstant(true); -} - -void ScriptSignal::Unregister(const String& name) -{ - ScriptVariable::Unregister(name); + +/** + * Global script variables. + * + * @ingroup base + */ +class I2_BASE_API ScriptGlobal +{ +public: + static Value Get(const String& name, const Value *defaultValue = NULL); + static void Set(const String& name, const Value& value); + + static void WriteToFile(const String& filename); + + static Dictionary::Ptr GetGlobals(void); + +private: + static Dictionary::Ptr m_Globals; +}; + } +#endif /* SCRIPTGLOBAL_H */ diff --git a/lib/base/scriptsignal.hpp b/lib/base/scriptsignal.hpp deleted file mode 100644 index 76cea5c75..000000000 --- a/lib/base/scriptsignal.hpp +++ /dev/null @@ -1,65 +0,0 @@ -/****************************************************************************** - * Icinga 2 * - * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) * - * * - * This program is free software; you can redistribute it and/or * - * modify it under the terms of the GNU General Public License * - * as published by the Free Software Foundation; either version 2 * - * of the License, or (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the Free Software Foundation * - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * - ******************************************************************************/ - -#ifndef SCRIPTSIGNAL_H -#define SCRIPTSIGNAL_H - -#include "base/i2-base.hpp" -#include "base/value.hpp" -#include -#include - -namespace icinga -{ - -/** - * A signal that can be subscribed to by scripts. - * - * @ingroup base - */ -class I2_BASE_API ScriptSignal : public Object -{ -public: - DECLARE_PTR_TYPEDEFS(ScriptSignal); - - typedef boost::function& arguments)> Callback; - - void AddSlot(const Callback& slot); - void Invoke(const std::vector& arguments = std::vector()); - - static ScriptSignal::Ptr GetByName(const String& name); - static void Register(const String& name, const ScriptSignal::Ptr& signal); - static void Unregister(const String& name); - -private: - std::vector m_Slots; -}; - -#define REGISTER_SCRIPTSIGNAL(name) \ - namespace { namespace UNIQUE_NAME(sig) { namespace sig ## name { \ - void RegisterSignal(void) { \ - ScriptSignal::Ptr sig = new icinga::ScriptSignal(); \ - ScriptSignal::Register(#name, sig); \ - } \ - INITIALIZE_ONCE(RegisterSignal); \ - } } } - -} - -#endif /* SCRIPTSIGNAL_H */ diff --git a/lib/base/scriptvariable.hpp b/lib/base/scriptvariable.hpp deleted file mode 100644 index 6fc8f1f4e..000000000 --- a/lib/base/scriptvariable.hpp +++ /dev/null @@ -1,71 +0,0 @@ -/****************************************************************************** - * Icinga 2 * - * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) * - * * - * This program is free software; you can redistribute it and/or * - * modify it under the terms of the GNU General Public License * - * as published by the Free Software Foundation; either version 2 * - * of the License, or (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the Free Software Foundation * - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * - ******************************************************************************/ - -#ifndef SCRIPTVARIABLE_H -#define SCRIPTVARIABLE_H - -#include "base/i2-base.hpp" -#include "base/registry.hpp" -#include "base/value.hpp" - -namespace icinga -{ - -class ScriptVariable; - -class I2_BASE_API ScriptVariableRegistry : public Registry > -{ -public: - static ScriptVariableRegistry *GetInstance(void); -}; - -/** - * A script variables. - * - * @ingroup base - */ -class I2_BASE_API ScriptVariable : public Object -{ -public: - DECLARE_PTR_TYPEDEFS(ScriptVariable); - - ScriptVariable(const Value& data); - - void SetConstant(bool constant); - bool IsConstant(void) const; - - void SetData(const Value& data); - Value GetData(void) const; - - static ScriptVariable::Ptr GetByName(const String& name); - static void Unregister(const String& name); - - static Value Get(const String& name, const Value *defaultValue = NULL); - static ScriptVariable::Ptr Set(const String& name, const Value& value, bool overwrite = true, bool make_const = false); - - static void WriteVariablesFile(const String& filename); - -private: - Value m_Data; - bool m_Constant; -}; - -} - -#endif /* SCRIPTVARIABLE_H */ diff --git a/lib/base/type.cpp b/lib/base/type.cpp index 50f6dfd11..ae8faa2c3 100644 --- a/lib/base/type.cpp +++ b/lib/base/type.cpp @@ -18,7 +18,7 @@ ******************************************************************************/ #include "base/type.hpp" -#include "base/scriptvariable.hpp" +#include "base/scriptglobal.hpp" using namespace icinga; @@ -31,17 +31,12 @@ void Type::Register(const Type::Ptr& type) { VERIFY(GetByName(type->GetName()) == NULL); - ScriptVariable::Set(type->GetName(), type, true, true); + ScriptGlobal::Set(type->GetName(), type); } Type::Ptr Type::GetByName(const String& name) { - ScriptVariable::Ptr svtype = ScriptVariable::GetByName(name); - - if (!svtype) - return Type::Ptr(); - - Value ptype = svtype->GetData(); + Value ptype = ScriptGlobal::Get(name, &Empty); if (!ptype.IsObjectType()) return Type::Ptr(); diff --git a/lib/cli/daemoncommand.cpp b/lib/cli/daemoncommand.cpp index ad7252b3d..1dcd0fad1 100644 --- a/lib/cli/daemoncommand.cpp +++ b/lib/cli/daemoncommand.cpp @@ -28,9 +28,8 @@ #include "base/utility.hpp" #include "base/exception.hpp" #include "base/convert.hpp" -#include "base/scriptvariable.hpp" +#include "base/scriptglobal.hpp" #include "base/context.hpp" -#include "base/scriptsignal.hpp" #include "config.h" #include #include @@ -43,7 +42,6 @@ namespace po = boost::program_options; static po::variables_map g_AppParams; REGISTER_CLICOMMAND("daemon", DaemonCommand); -REGISTER_SCRIPTSIGNAL(onload); static String LoadAppType(const String& typeSpec) { @@ -182,10 +180,7 @@ static bool LoadConfigFiles(const boost::program_options::variables_map& vm, con ConfigCompilerContext::GetInstance()->FinishObjectsFile(); - ScriptVariable::WriteVariablesFile(varsfile); - - ScriptSignal::Ptr loadSignal = ScriptSignal::GetByName("onload"); - loadSignal->Invoke(); + ScriptGlobal::WriteToFile(varsfile); return true; } @@ -355,9 +350,7 @@ int DaemonCommand::Run(const po::variables_map& vm, const std::vector #include #include @@ -133,8 +133,8 @@ int NodeSetupCommand::SetupMaster(const boost::program_options::variables_map& v return 1; } - String user = ScriptVariable::Get("RunAsUser"); - String group = ScriptVariable::Get("RunAsGroup"); + String user = ScriptGlobal::Get("RunAsUser"); + String group = ScriptGlobal::Get("RunAsGroup"); if (!Utility::SetFileOwnership(pki_path, user, group)) { Log(LogWarning, "cli") @@ -374,8 +374,8 @@ int NodeSetupCommand::SetupNode(const boost::program_options::variables_map& vm, return 1; } - String user = ScriptVariable::Get("RunAsUser"); - String group = ScriptVariable::Get("RunAsGroup"); + String user = ScriptGlobal::Get("RunAsUser"); + String group = ScriptGlobal::Get("RunAsGroup"); if (!Utility::SetFileOwnership(pki_path, user, group)) { Log(LogWarning, "cli") diff --git a/lib/cli/nodeupdateconfigcommand.cpp b/lib/cli/nodeupdateconfigcommand.cpp index 8a820e325..e86609a39 100644 --- a/lib/cli/nodeupdateconfigcommand.cpp +++ b/lib/cli/nodeupdateconfigcommand.cpp @@ -24,7 +24,6 @@ #include "base/console.hpp" #include "base/application.hpp" #include "base/tlsutility.hpp" -#include "base/scriptvariable.hpp" #include "base/json.hpp" #include "base/objectlock.hpp" #include diff --git a/lib/cli/nodeutility.cpp b/lib/cli/nodeutility.cpp index 2133bc9ec..da55f2471 100644 --- a/lib/cli/nodeutility.cpp +++ b/lib/cli/nodeutility.cpp @@ -24,7 +24,7 @@ #include "base/tlsutility.hpp" #include "base/convert.hpp" #include "base/utility.hpp" -#include "base/scriptvariable.hpp" +#include "base/scriptglobal.hpp" #include "base/json.hpp" #include "base/netstring.hpp" #include "base/stdiostream.hpp" @@ -61,8 +61,8 @@ void NodeUtility::CreateRepositoryPath(const String& path) if (!Utility::PathExists(path)) Utility::MkDirP(path, 0750); - String user = ScriptVariable::Get("RunAsUser"); - String group = ScriptVariable::Get("RunAsGroup"); + String user = ScriptGlobal::Get("RunAsUser"); + String group = ScriptGlobal::Get("RunAsGroup"); if (!Utility::SetFileOwnership(path, user, group)) { Log(LogWarning, "cli") @@ -374,8 +374,8 @@ bool NodeUtility::WriteNodeConfigObjects(const String& filename, const Array::Pt Utility::MkDirP(path, 0755); - String user = ScriptVariable::Get("RunAsUser"); - String group = ScriptVariable::Get("RunAsGroup"); + String user = ScriptGlobal::Get("RunAsUser"); + String group = ScriptGlobal::Get("RunAsGroup"); if (!Utility::SetFileOwnership(path, user, group)) { Log(LogWarning, "cli") diff --git a/lib/cli/nodewizardcommand.cpp b/lib/cli/nodewizardcommand.cpp index ab5ea64fa..0478bfb02 100644 --- a/lib/cli/nodewizardcommand.cpp +++ b/lib/cli/nodewizardcommand.cpp @@ -25,7 +25,7 @@ #include "base/console.hpp" #include "base/application.hpp" #include "base/tlsutility.hpp" -#include "base/scriptvariable.hpp" +#include "base/scriptglobal.hpp" #include #include #include @@ -233,8 +233,8 @@ wizard_master_host: return 1; } - String user = ScriptVariable::Get("RunAsUser"); - String group = ScriptVariable::Get("RunAsGroup"); + String user = ScriptGlobal::Get("RunAsUser"); + String group = ScriptGlobal::Get("RunAsGroup"); if (!Utility::SetFileOwnership(pki_path, user, group)) { Log(LogWarning, "cli") @@ -418,8 +418,8 @@ wizard_ticket: return 1; } - String user = ScriptVariable::Get("RunAsUser"); - String group = ScriptVariable::Get("RunAsGroup"); + String user = ScriptGlobal::Get("RunAsUser"); + String group = ScriptGlobal::Get("RunAsGroup"); if (!Utility::SetFileOwnership(pki_path, user, group)) { Log(LogWarning, "cli") diff --git a/lib/cli/repositoryutility.cpp b/lib/cli/repositoryutility.cpp index b3efec940..ad35bf409 100644 --- a/lib/cli/repositoryutility.cpp +++ b/lib/cli/repositoryutility.cpp @@ -25,7 +25,7 @@ #include "base/logger.hpp" #include "base/application.hpp" #include "base/convert.hpp" -#include "base/scriptvariable.hpp" +#include "base/scriptglobal.hpp" #include "base/json.hpp" #include "base/netstring.hpp" #include "base/tlsutility.hpp" @@ -131,8 +131,8 @@ void RepositoryUtility::CreateRepositoryPath(const String& path) if (!Utility::PathExists(path)) Utility::MkDirP(path, 0750); - String user = ScriptVariable::Get("RunAsUser"); - String group = ScriptVariable::Get("RunAsGroup"); + String user = ScriptGlobal::Get("RunAsUser"); + String group = ScriptGlobal::Get("RunAsGroup"); if (!Utility::SetFileOwnership(path, user, group)) { Log(LogWarning, "cli") diff --git a/lib/cli/variablegetcommand.cpp b/lib/cli/variablegetcommand.cpp index 98a31d136..78e0384cd 100644 --- a/lib/cli/variablegetcommand.cpp +++ b/lib/cli/variablegetcommand.cpp @@ -30,7 +30,7 @@ #include "base/debug.hpp" #include "base/objectlock.hpp" #include "base/console.hpp" -#include "base/scriptvariable.hpp" +#include "base/scriptglobal.hpp" #include #include #include @@ -72,7 +72,7 @@ int VariableGetCommand::GetMinArguments(void) const int VariableGetCommand::Run(const boost::program_options::variables_map& vm, const std::vector& ap) const { if (vm.count("current")) { - std::cout << ScriptVariable::Get(ap[0], &Empty) << "\n"; + std::cout << ScriptGlobal::Get(ap[0], &Empty) << "\n"; return 0; } diff --git a/lib/config/config_lexer.ll b/lib/config/config_lexer.ll index 6968f014b..8ef3b5de6 100644 --- a/lib/config/config_lexer.ll +++ b/lib/config/config_lexer.ll @@ -28,7 +28,7 @@ using namespace icinga; #include "config/config_parser.hh" #include -#define YYLTYPE icinga::DebugInfo +#define YYLTYPE icinga::CompilerDebugInfo #define YY_EXTRA_TYPE ConfigCompiler * #define YY_USER_ACTION \ @@ -177,8 +177,9 @@ library return T_LIBRARY; null return T_NULL; true { yylval->boolean = 1; return T_BOOLEAN; } false { yylval->boolean = 0; return T_BOOLEAN; } -const return T_CONST; +const return T_GLOBAL; local return T_LOCAL; +this return T_THIS; global return T_GLOBAL; use return T_USE; apply return T_APPLY; @@ -190,7 +191,6 @@ ignore return T_IGNORE; function return T_FUNCTION; return return T_RETURN; for return T_FOR; -signal return T_SIGNAL; if return T_IF; else return T_ELSE; =\> return T_FOLLOWS; diff --git a/lib/config/config_parser.yy b/lib/config/config_parser.yy index f11d51b6e..704e37835 100644 --- a/lib/config/config_parser.yy +++ b/lib/config/config_parser.yy @@ -31,7 +31,6 @@ #include "config/objectrule.hpp" #include "base/value.hpp" #include "base/utility.hpp" -#include "base/scriptvariable.hpp" #include "base/exception.hpp" #include "base/dynamictype.hpp" #include "base/scripterror.hpp" @@ -39,7 +38,7 @@ #include #include -#define YYLTYPE icinga::DebugInfo +#define YYLTYPE icinga::CompilerDebugInfo #define YYERROR_VERBOSE #define YYLLOC_DEFAULT(Current, Rhs, N) \ @@ -70,7 +69,7 @@ do { \ using namespace icinga; template -static void MakeRBinaryOp(Expression** result, Expression *left, Expression *right, DebugInfo& diLeft, DebugInfo& diRight) +static void MakeRBinaryOp(Expression** result, Expression *left, Expression *right, const DebugInfo& diLeft, const DebugInfo& diRight) { *result = new T(left, right, DebugInfoRange(diLeft, diRight)); } @@ -82,6 +81,7 @@ static void MakeRBinaryOp(Expression** result, Expression *left, Expression *rig %locations %defines %error-verbose +%glr-parser %parse-param { std::vector *elist } %parse-param { ConfigCompiler *context } @@ -170,7 +170,6 @@ static void MakeRBinaryOp(Expression** result, Expression *left, Expression *rig %token T_FUNCTION "function (T_FUNCTION)" %token T_RETURN "return (T_RETURN)" %token T_FOR "for (T_FOR)" -%token T_SIGNAL "signal (T_SIGNAL)" %token T_IF "if (T_IF)" %token T_ELSE "else (T_ELSE)" %token T_FOLLOWS "=> (T_FOLLOWS)" @@ -180,9 +179,6 @@ static void MakeRBinaryOp(Expression** result, Expression *left, Expression *rig %type rterm_items_inner %type identifier_items %type identifier_items_inner -%type indexer -%type indexer_items -%type indexer_item %type typerulelist %type combined_set_op %type type @@ -190,7 +186,6 @@ static void MakeRBinaryOp(Expression** result, Expression *left, Expression *rig %type lterm_items %type lterm_items_inner %type rterm -%type rterm_without_indexer %type rterm_array %type rterm_scope %type lterm @@ -206,10 +201,10 @@ static void MakeRBinaryOp(Expression** result, Expression *left, Expression *rig %right T_FOLLOWS %right T_INCLUDE T_INCLUDE_RECURSIVE T_OBJECT T_TEMPLATE T_APPLY T_IMPORT T_ASSIGN T_IGNORE T_WHERE -%right T_FUNCTION T_SIGNAL T_FOR +%right T_FUNCTION T_FOR %left T_LOGICAL_OR %left T_LOGICAL_AND -%left T_GLOBAL T_LOCAL T_RETURN +%left T_RETURN %left T_IDENTIFIER %left T_SET T_SET_ADD T_SET_SUBTRACT T_SET_MULTIPLY T_SET_DIVIDE T_SET_MODULO T_SET_XOR T_SET_BINARY_AND T_SET_BINARY_OR %nonassoc T_EQUAL T_NOT_EQUAL @@ -224,12 +219,15 @@ static void MakeRBinaryOp(Expression** result, Expression *left, Expression *rig %left UNARY_MINUS %right '!' '~' %left '.' '(' '[' +%left T_LOCAL T_GLOBAL T_THIS %right ';' ',' %right T_NEWLINE %{ int yylex(YYSTYPE *lvalp, YYLTYPE *llocp, void *scanner); +extern int yydebug; + void yyerror(YYLTYPE *locp, std::vector *, ConfigCompiler *, const char *err) { BOOST_THROW_EXCEPTION(ScriptError(err, *locp)); @@ -241,6 +239,8 @@ Expression *ConfigCompiler::Compile(void) { std::vector elist; + //yydebug = 1; + if (yyparse(&elist, this) != 0) return NULL; @@ -261,18 +261,10 @@ script: statements } ; -statements: newlines lterm_items newlines +statements: newlines lterm_items { $$ = $2; } - | newlines lterm_items - { - $$ = $2; - } - | lterm_items newlines - { - $$ = $1; - } | lterm_items { $$ = $1; @@ -287,7 +279,23 @@ lterm_items: /* empty */ { $$ = $1; } - | lterm_items_inner sep + | lterm_items_inner ',' + { + $$ = $1; + } + | lterm_items_inner ',' newlines + { + $$ = $1; + } + | lterm_items_inner ';' + { + $$ = $1; + } + | lterm_items_inner ';' newlines + { + $$ = $1; + } + | lterm_items_inner newlines { $$ = $1; } @@ -317,17 +325,6 @@ library: T_LIBRARY T_STRING } ; -constant: T_CONST identifier T_SET rterm - { - ScriptFrame frame; - ScriptVariable::Ptr sv = ScriptVariable::Set($2, $4->Evaluate(frame)); - free($2); - delete $4; - - sv->SetConstant(true); - } - ; - identifier: T_IDENTIFIER | T_STRING { @@ -491,7 +488,11 @@ object_declaration: T_OBJECT } ; -identifier_items: identifier_items_inner +identifier_items: /* empty */ + { + $$ = new std::vector(); + } + | identifier_items_inner { $$ = $1; } @@ -501,11 +502,7 @@ identifier_items: identifier_items_inner } ; -identifier_items_inner: /* empty */ - { - $$ = new std::vector(); - } - | identifier +identifier_items_inner: identifier { $$ = new std::vector(); $$->push_back($1); @@ -523,46 +520,6 @@ identifier_items_inner: /* empty */ } ; -indexer: rterm_without_indexer indexer_items - { - $$ = $2; - $$->insert($$->begin(), $1); - } - ; - -indexer_items: indexer_item - { - $$ = new std::vector(); - $$->push_back($1); - } - | indexer_items indexer_item - { - $$ = $1; - $$->push_back($2); - } - ; - -indexer_item: '.' identifier - { - $$ = MakeLiteral($2); - free($2); - } - | '[' rterm ']' - { - $$ = $2; - } - ; - -scope_specifier: T_LOCAL - { - $$ = ScopeLocal; - } - | T_GLOBAL - { - $$ = ScopeGlobal; - } - ; - combined_set_op: T_SET | T_SET_ADD | T_SET_SUBTRACT @@ -577,6 +534,20 @@ combined_set_op: T_SET } ; +scope_specifier: T_LOCAL + { + $$ = ScopeLocal; + } + | T_GLOBAL + { + $$ = ScopeGlobal; + } + | T_THIS + { + $$ = ScopeThis; + } + ; + lterm: type { $$ = MakeLiteral(); // ASTify this @@ -585,29 +556,11 @@ lterm: type { $$ = MakeLiteral(); // ASTify this } - | constant + | rterm combined_set_op rterm { - $$ = MakeLiteral(); // ASTify this - } - | indexer combined_set_op rterm - { - $$ = new SetExpression(ScopeCurrent, *$1, $2, $3, DebugInfoRange(@1, @3)); - delete $1; - } - | identifier combined_set_op rterm - { - $$ = new SetExpression(ScopeCurrent, MakeIndexer($1), $2, $3, DebugInfoRange(@1, @3)); - free($1); - } - | scope_specifier indexer combined_set_op rterm - { - $$ = new SetExpression($1, *$2, $3, $4, DebugInfoRange(@1, @4)); - delete $2; - } - | scope_specifier identifier combined_set_op rterm - { - $$ = new SetExpression($1, MakeIndexer($2), $3, $4, DebugInfoRange(@1, @4)); - free($2); + Expression *expr = $1; + BindToScope(expr, ScopeCurrent); + $$ = new SetExpression(expr, $2, $3, DebugInfoRange(@1, @3)); } | T_INCLUDE T_STRING { @@ -672,11 +625,6 @@ lterm: type { $$ = $1; } - | T_SIGNAL identifier T_SET_ADD rterm - { - $$ = new SlotExpression($2, $4, DebugInfoRange(@1, @4)); - free($2); - } | T_FOR '(' identifier T_FOLLOWS identifier T_IN rterm ')' rterm_scope { DictExpression *aexpr = dynamic_cast($9); @@ -719,7 +667,7 @@ lterm: type FunctionExpression *fexpr = new FunctionExpression(*$4, $6, aexpr, DebugInfoRange(@1, @7)); delete $4; - $$ = new SetExpression(ScopeCurrent, MakeIndexer($2), OpSetLiteral, fexpr, DebugInfoRange(@1, @7)); + $$ = new SetExpression(MakeIndexer(ScopeCurrent, $2), OpSetLiteral, fexpr, DebugInfoRange(@1, @7)); free($2); } | scope_specifier T_FUNCTION identifier '(' identifier_items ')' use_specifier rterm_scope @@ -730,7 +678,7 @@ lterm: type FunctionExpression *fexpr = new FunctionExpression(*$5, $7, aexpr, DebugInfoRange(@1, @8)); delete $5; - $$ = new SetExpression($1, MakeIndexer($3), OpSetLiteral, fexpr, DebugInfoRange(@1, @8)); + $$ = new SetExpression(MakeIndexer($1, $3), OpSetLiteral, fexpr, DebugInfoRange(@1, @8)); free($3); } | rterm @@ -747,7 +695,15 @@ rterm_items: /* empty */ { $$ = $1; } - | rterm_items_inner arraysep + | rterm_items_inner ',' + { + $$ = $1; + } + | rterm_items_inner ',' newlines + { + $$ = $1; + } + | rterm_items_inner newlines { $$ = $1; } @@ -765,21 +721,11 @@ rterm_items_inner: rterm } ; -rterm_array: '[' newlines rterm_items newlines ']' - { - $$ = new ArrayExpression(*$3, DebugInfoRange(@1, @5)); - delete $3; - } - | '[' newlines rterm_items ']' +rterm_array: '[' newlines rterm_items ']' { $$ = new ArrayExpression(*$3, DebugInfoRange(@1, @4)); delete $3; } - | '[' rterm_items newlines ']' - { - $$ = new ArrayExpression(*$2, DebugInfoRange(@1, @4)); - delete $2; - } | '[' rterm_items ']' { $$ = new ArrayExpression(*$2, DebugInfoRange(@1, @3)); @@ -794,18 +740,7 @@ rterm_scope: '{' statements '}' } ; -rterm: rterm_without_indexer - { - $$ = $1; - } - | indexer - { - $$ = new IndexerExpression(*$1, @1); - delete $1; - } - ; - -rterm_without_indexer: T_STRING +rterm: T_STRING { $$ = MakeLiteral($1); free($1); @@ -822,17 +757,20 @@ rterm_without_indexer: T_STRING { $$ = MakeLiteral(); } - | indexer '(' rterm_items ')' - { - $$ = new FunctionCallExpression(*$1, NULL, *$3, DebugInfoRange(@1, @4)); - delete $1; - delete $3; - } | rterm '(' rterm_items ')' { - $$ = new FunctionCallExpression(MakeIndexer("this"), $1, *$3, DebugInfoRange(@1, @4)); + $$ = new FunctionCallExpression($1, *$3, DebugInfoRange(@1, @4)); delete $3; } + | rterm '.' T_IDENTIFIER %dprec 2 + { + $$ = new IndexerExpression($1, MakeLiteral($3), DebugInfoRange(@1, @3)); + free($3); + } + | rterm '[' rterm ']' + { + $$ = new IndexerExpression($1, $3, DebugInfoRange(@1, @3)); + } | T_IDENTIFIER { $$ = new VariableExpression($1, @1); @@ -850,6 +788,16 @@ rterm_without_indexer: T_STRING { $$ = new SubtractExpression(MakeLiteral(0), $2, DebugInfoRange(@1, @2)); } + | scope_specifier + { + $$ = new GetScopeExpression($1); + } + | scope_specifier T_IDENTIFIER + { + Expression *scope = new GetScopeExpression($1); + $$ = new IndexerExpression(scope, MakeLiteral($2), DebugInfoRange(@1, @2)); + free($2); + } | rterm_array { $$ = $1; @@ -907,7 +855,7 @@ rterm_without_indexer: T_STRING $$ = new FunctionExpression(args, new std::map(), $3, DebugInfoRange(@1, @3)); } - | T_BINARY_OR identifier_items T_BINARY_OR T_FOLLOWS rterm + | '(' identifier_items ')' T_FOLLOWS rterm { DictExpression *aexpr = dynamic_cast($5); if (aexpr) diff --git a/lib/config/configcompiler.cpp b/lib/config/configcompiler.cpp index b9b6369f3..60e40e42a 100644 --- a/lib/config/configcompiler.cpp +++ b/lib/config/configcompiler.cpp @@ -83,9 +83,9 @@ void *ConfigCompiler::GetScanner(void) const * * @returns The path. */ -String ConfigCompiler::GetPath(void) const +const char *ConfigCompiler::GetPath(void) const { - return m_Path; + return m_Path.CStr(); } void ConfigCompiler::SetZone(const String& zone) diff --git a/lib/config/configcompiler.hpp b/lib/config/configcompiler.hpp index d41cdbfb6..6e2eb5dd6 100644 --- a/lib/config/configcompiler.hpp +++ b/lib/config/configcompiler.hpp @@ -36,14 +36,28 @@ typedef void *yyscan_t; namespace icinga { - class ConfigCompiler; -} -int yylex(YYSTYPE *context, icinga::DebugInfo *di, yyscan_t scanner); -int yyparse(std::vector *elist, icinga::ConfigCompiler *context); - -namespace icinga +struct CompilerDebugInfo { + const char *Path; + + int FirstLine; + int FirstColumn; + + int LastLine; + int LastColumn; + + operator DebugInfo(void) const + { + DebugInfo di; + di.Path = Path; + di.FirstLine = FirstLine; + di.FirstColumn = FirstColumn; + di.LastLine = LastLine; + di.LastColumn = LastColumn; + return di; + } +}; /** * The configuration compiler can be used to compile a configuration file @@ -65,7 +79,7 @@ public: static void AddIncludeSearchDir(const String& dir); - String GetPath(void) const; + const char *GetPath(void) const; void SetZone(const String& zone); String GetZone(void) const; @@ -88,6 +102,15 @@ private: String m_Zone; void *m_Scanner; + + static std::vector m_IncludeSearchDirs; + + void InitializeScanner(void); + void DestroyScanner(void); + + void CompileHelper(void); + +public: bool m_Eof; int m_IgnoreNewlines; @@ -105,15 +128,7 @@ private: std::stack m_FVVar; std::stack m_FTerm; - static std::vector m_IncludeSearchDirs; - void InitializeScanner(void); - void DestroyScanner(void); - - void CompileHelper(void); - - friend int ::yylex(YYSTYPE *context, icinga::DebugInfo *di, yyscan_t scanner); - friend int ::yyparse(std::vector *elist, ConfigCompiler *context); }; class I2_CONFIG_API ConfigFragmentRegistry : public Registry diff --git a/lib/config/configitembuilder.cpp b/lib/config/configitembuilder.cpp index 02f8d65bc..d46bfa1a0 100644 --- a/lib/config/configitembuilder.cpp +++ b/lib/config/configitembuilder.cpp @@ -100,7 +100,7 @@ ConfigItem::Ptr ConfigItemBuilder::Compile(void) Array::Ptr templateArray = new Array(); templateArray->Add(m_Name); - exprs.push_back(new SetExpression(ScopeCurrent, MakeIndexer("templates"), OpSetAdd, + exprs.push_back(new SetExpression(MakeIndexer(ScopeCurrent, "templates"), OpSetAdd, new LiteralExpression(templateArray), m_DebugInfo)); DictExpression *dexpr = new DictExpression(m_Expressions, m_DebugInfo); diff --git a/lib/config/configtype.cpp b/lib/config/configtype.cpp index 423ad7a1f..1c20b49ba 100644 --- a/lib/config/configtype.cpp +++ b/lib/config/configtype.cpp @@ -175,7 +175,7 @@ void ConfigType::ValidateObject(const Object::Ptr& object, String validator = ruleList->GetValidator(); if (!validator.IsEmpty()) { - ScriptFunction::Ptr func = ScriptFunction::GetByName(validator); + ScriptFunction::Ptr func = ScriptGlobal::Get(validator, &Empty); if (!func) BOOST_THROW_EXCEPTION(std::invalid_argument("Validator function '" + validator + "' does not exist.")); @@ -234,7 +234,7 @@ void ConfigType::ValidateArray(const Array::Ptr& array, String validator = ruleList->GetValidator(); if (!validator.IsEmpty()) { - ScriptFunction::Ptr func = ScriptFunction::GetByName(validator); + ScriptFunction::Ptr func = ScriptGlobal::Get(validator, &Empty); if (!func) BOOST_THROW_EXCEPTION(std::invalid_argument("Validator function '" + validator + "' does not exist.")); diff --git a/lib/config/expression.cpp b/lib/config/expression.cpp index 634fe745f..0ad2c5a28 100644 --- a/lib/config/expression.cpp +++ b/lib/config/expression.cpp @@ -25,6 +25,7 @@ #include "base/object.hpp" #include "base/logger.hpp" #include "base/scripterror.hpp" +#include "base/scriptglobal.hpp" #include #include #include @@ -55,17 +56,21 @@ Value Expression::Evaluate(ScriptFrame& frame, DebugHint *dhint) const } } +bool Expression::GetReference(ScriptFrame& frame, bool init_dict, Value *parent, String *index, DebugHint **dhint) const +{ + return false; +} + const DebugInfo& Expression::GetDebugInfo(void) const { static DebugInfo debugInfo; return debugInfo; } -std::vector icinga::MakeIndexer(const String& index1) +Expression *icinga::MakeIndexer(ScopeSpecifier scopeSpec, const String& index) { - std::vector result; - result.push_back(new VariableExpression(index1)); - return result; + Expression *scope = new GetScopeExpression(scopeSpec); + return new IndexerExpression(scope, MakeLiteral(index)); } void DictExpression::MakeInline(void) @@ -92,6 +97,11 @@ Value VariableExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const return VMOps::Variable(frame, m_Variable, m_DebugInfo); } +bool VariableExpression::GetReference(ScriptFrame& frame, bool init_dict, Value *parent, String *index, DebugHint **dhint) const +{ + return false; +} + Value NegateExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const { return ~(long)m_Operand->Evaluate(frame); @@ -225,28 +235,11 @@ Value LogicalOrExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) cons Value FunctionCallExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const { Value self, vfunc; + String index; - if (!m_IName.empty()) { - Value result = m_IName[0]->Evaluate(frame); - - if (m_IName.size() == 2) - self = result; - - for (int i = 1; i < m_IName.size(); i++) { - if (result.IsEmpty()) - return Empty; - - Value index = m_IName[i]->Evaluate(frame); - result = VMOps::GetField(result, index, m_DebugInfo); - - if (i == m_IName.size() - 2) - self = result; - } - - vfunc= result; - } - - if (m_FName) + if (m_FName->GetReference(frame, false, &self, &index)) + vfunc = VMOps::GetField(self, index); + else vfunc = m_FName->Evaluate(frame); if (!vfunc.IsObjectType()) @@ -298,81 +291,35 @@ Value DictExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const return dframe->Self; } +Value GetScopeExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const +{ + if (m_ScopeSpec == ScopeLocal) + return frame.Locals; + else if (m_ScopeSpec == ScopeCurrent) + return frame.Self; + else if (m_ScopeSpec == ScopeThis) + return frame.Self; + else if (m_ScopeSpec == ScopeGlobal) + return ScriptGlobal::GetGlobals(); + else + ASSERT(!"Invalid scope."); +} + Value SetExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const { DebugHint *psdhint = dhint; - DebugHint sdhint; - Value parent, object; + Value parent; String index; - for (Array::SizeType i = 0; i < m_Indexer.size(); i++) { - Expression *indexExpr = m_Indexer[i]; - - String tempindex; - - if (i == 0) { - VariableExpression *vexpr = dynamic_cast(indexExpr); - - if (!vexpr) { - object = indexExpr->Evaluate(frame, dhint); - - if (!object) - BOOST_THROW_EXCEPTION(ScriptError("Left-hand side argument must not be null.", m_DebugInfo)); - - continue; - } - - tempindex = vexpr->GetVariable(); - } else - tempindex = indexExpr->Evaluate(frame, dhint); - - if (psdhint) { - sdhint = psdhint->GetChild(tempindex); - psdhint = &sdhint; - } - - if (i == 0) { - if (m_ScopeSpec == ScopeLocal) - parent = frame.Locals; - else if (m_ScopeSpec == ScopeCurrent) - parent = frame.Self; - else if (m_ScopeSpec == ScopeGlobal) { - ScriptVariable::Ptr sv = ScriptVariable::GetByName(tempindex); - - Dictionary::Ptr fglobals = new Dictionary(); - - if (sv) - fglobals->Set(tempindex, sv->GetData()); - - parent = fglobals; - } - } else - parent = object; - - if (i == m_Indexer.size() - 1) { - index = tempindex; - - /* No need to look up the last indexer's value if this is a direct set */ - if (m_Op == OpSetLiteral) - break; - } - - object = VMOps::GetField(parent, tempindex, m_DebugInfo); - - if (i != m_Indexer.size() - 1 && object.IsEmpty()) { - object = new Dictionary(); - - if (i == 0 && m_ScopeSpec == ScopeGlobal) - ScriptVariable::Set(tempindex, object); - else - VMOps::SetField(parent, tempindex, object, m_DebugInfo); - } - } + if (!m_Operand1->GetReference(frame, true, &parent, &index, &psdhint)) + BOOST_THROW_EXCEPTION(ScriptError("Expression cannot be assigned to.", m_DebugInfo)); Value right = m_Operand2->Evaluate(frame, dhint); if (m_Op != OpSetLiteral) { + Value object = VMOps::GetField(parent, index); + Expression *lhs = MakeLiteral(object); Expression *rhs = MakeLiteral(right); @@ -406,14 +353,15 @@ Value SetExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const } } - if (m_Indexer.size() == 1 && m_ScopeSpec == ScopeGlobal) - ScriptVariable::Set(index, right); - else - VMOps::SetField(parent, index, right, m_DebugInfo); + VMOps::SetField(parent, index, right, m_DebugInfo); - if (psdhint) + if (psdhint) { psdhint->AddMessage("=", m_DebugInfo); + if (psdhint != dhint) + delete psdhint; + } + return right; } @@ -434,7 +382,56 @@ Value ReturnExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const Value IndexerExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const { - return VMOps::Indexer(frame, m_Indexer, m_DebugInfo); + Value object = m_Operand1->Evaluate(frame, dhint); + String index = m_Operand2->Evaluate(frame, dhint); + return VMOps::GetField(object, index); +} + +bool IndexerExpression::GetReference(ScriptFrame& frame, bool init_dict, Value *parent, String *index, DebugHint **dhint) const +{ + Value vparent; + String vindex; + + if (m_Operand1->GetReference(frame, init_dict, &vparent, &vindex, dhint)) { + if (init_dict && VMOps::GetField(vparent, vindex).IsEmpty()) + VMOps::SetField(vparent, vindex, new Dictionary()); + + *parent = VMOps::GetField(vparent, vindex); + } else + *parent = m_Operand1->Evaluate(frame); + + *index = m_Operand2->Evaluate(frame); + + if (dhint && *dhint) + *dhint = new DebugHint((*dhint)->GetChild(*index)); + + return true; +} + +void icinga::BindToScope(Expression *& expr, ScopeSpecifier scopeSpec) +{ + IndexerExpression *iexpr = dynamic_cast(expr); + + if (iexpr) { + BindToScope(iexpr->m_Operand1, scopeSpec); + return; + } + + LiteralExpression *lexpr = dynamic_cast(expr); + + if (lexpr) { + Expression *scope = new GetScopeExpression(scopeSpec); + expr = new IndexerExpression(scope, lexpr, lexpr->GetDebugInfo()); + } + + VariableExpression *vexpr = dynamic_cast(expr); + + if (vexpr) { + Expression *scope = new GetScopeExpression(scopeSpec); + Expression *new_expr = new IndexerExpression(scope, MakeLiteral(vexpr->GetVariable()), vexpr->GetDebugInfo()); + delete expr; + expr = new_expr; + } } Value ImportExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const @@ -457,11 +454,6 @@ Value FunctionExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const return VMOps::NewFunction(frame, m_Args, m_ClosedVars, m_Expression); } -Value SlotExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const -{ - return VMOps::NewSlot(frame, m_Signal, m_Slot->Evaluate(frame)); -} - Value ApplyExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const { return VMOps::NewApply(frame, m_Type, m_Target, m_Name->Evaluate(frame), m_Filter, diff --git a/lib/config/expression.hpp b/lib/config/expression.hpp index 26b3979b6..a450829cc 100644 --- a/lib/config/expression.hpp +++ b/lib/config/expression.hpp @@ -110,6 +110,7 @@ enum ScopeSpecifier { ScopeLocal, ScopeCurrent, + ScopeThis, ScopeGlobal }; @@ -143,12 +144,13 @@ public: virtual ~Expression(void); Value Evaluate(ScriptFrame& frame, DebugHint *dhint = NULL) const; + virtual bool GetReference(ScriptFrame& frame, bool init_dict, Value *parent, String *index, DebugHint **dhint = NULL) const; + virtual const DebugInfo& GetDebugInfo(void) const; virtual Value DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const = 0; - virtual const DebugInfo& GetDebugInfo(void) const; }; -I2_CONFIG_API std::vector MakeIndexer(const String& index1); +I2_CONFIG_API Expression *MakeIndexer(ScopeSpecifier scopeSpec, const String& index); class I2_CONFIG_API OwnedExpression : public Expression { @@ -273,9 +275,12 @@ public: protected: virtual Value DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const; + virtual bool GetReference(ScriptFrame& frame, bool init_dict, Value *parent, String *index, DebugHint **dhint) const; private: String m_Variable; + + friend void BindToScope(Expression *& expr, ScopeSpecifier scopeSpec); }; class I2_CONFIG_API NegateExpression : public UnaryExpression @@ -523,15 +528,12 @@ protected: class I2_CONFIG_API FunctionCallExpression : public DebuggableExpression { public: - FunctionCallExpression(const std::vector iname, Expression *fname, const std::vector& args, const DebugInfo& debugInfo = DebugInfo()) - : DebuggableExpression(debugInfo), m_IName(iname), m_FName(fname), m_Args(args) + FunctionCallExpression(Expression *fname, const std::vector& args, const DebugInfo& debugInfo = DebugInfo()) + : DebuggableExpression(debugInfo), m_FName(fname), m_Args(args) { } ~FunctionCallExpression(void) { - BOOST_FOREACH(Expression *expr, m_IName) - delete expr; - delete m_FName; BOOST_FOREACH(Expression *expr, m_Args) @@ -542,7 +544,6 @@ protected: virtual Value DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const; public: - std::vector m_IName; Expression *m_FName; std::vector m_Args; }; @@ -590,30 +591,18 @@ private: bool m_Inline; }; -class I2_CONFIG_API SetExpression : public DebuggableExpression +class I2_CONFIG_API SetExpression : public BinaryExpression { public: - SetExpression(ScopeSpecifier scopeSpec, const std::vector& indexer, CombinedSetOp op, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : DebuggableExpression(debugInfo), m_ScopeSpec(scopeSpec), m_Op(op), m_Indexer(indexer), m_Operand2(operand2) + SetExpression(Expression *operand1, CombinedSetOp op, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(operand1, operand2, debugInfo), m_Op(op) { } - ~SetExpression(void) - { - BOOST_FOREACH(Expression *expr, m_Indexer) - delete expr; - - delete m_Operand2; - } - protected: virtual Value DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const; private: - ScopeSpecifier m_ScopeSpec; CombinedSetOp m_Op; - std::vector m_Indexer; - Expression *m_Operand2; - }; class I2_CONFIG_API ConditionalExpression : public DebuggableExpression @@ -650,26 +639,36 @@ protected: virtual Value DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const; }; -class I2_CONFIG_API IndexerExpression : public DebuggableExpression +class I2_CONFIG_API GetScopeExpression : public Expression { public: - IndexerExpression(const std::vector& indexer, const DebugInfo& debugInfo = DebugInfo()) - : DebuggableExpression(debugInfo), m_Indexer(indexer) + GetScopeExpression(ScopeSpecifier scopeSpec) + : m_ScopeSpec(scopeSpec) { } - ~IndexerExpression(void) - { - BOOST_FOREACH(Expression *expr, m_Indexer) - delete expr; - } - protected: virtual Value DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const; private: - std::vector m_Indexer; + ScopeSpecifier m_ScopeSpec; }; +class I2_CONFIG_API IndexerExpression : public BinaryExpression +{ +public: + IndexerExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(operand1, operand2, debugInfo) + { } + +protected: + virtual Value DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const; + virtual bool GetReference(ScriptFrame& frame, bool init_dict, Value *parent, String *index, DebugHint **dhint) const; + + friend void BindToScope(Expression *& expr, ScopeSpecifier scopeSpec); +}; + +I2_CONFIG_API void BindToScope(Expression *& expr, ScopeSpecifier scopeSpec); + class I2_CONFIG_API ImportExpression : public DebuggableExpression { public: @@ -706,21 +705,6 @@ private: boost::shared_ptr m_Expression; }; -class I2_CONFIG_API SlotExpression : public DebuggableExpression -{ -public: - SlotExpression(const String& signal, Expression *slot, const DebugInfo& debugInfo = DebugInfo()) - : DebuggableExpression(debugInfo), m_Signal(signal), m_Slot(slot) - { } - -protected: - virtual Value DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const; - -private: - String m_Signal; - Expression *m_Slot; -}; - class I2_CONFIG_API ApplyExpression : public DebuggableExpression { public: diff --git a/lib/config/vmops.hpp b/lib/config/vmops.hpp index 58608a86d..3ad9ece00 100644 --- a/lib/config/vmops.hpp +++ b/lib/config/vmops.hpp @@ -29,8 +29,7 @@ #include "base/array.hpp" #include "base/dictionary.hpp" #include "base/scriptfunction.hpp" -#include "base/scriptsignal.hpp" -#include "base/scriptvariable.hpp" +#include "base/scriptglobal.hpp" #include "base/scripterror.hpp" #include "base/convert.hpp" #include "base/objectlock.hpp" @@ -55,7 +54,7 @@ public: else if (frame.Self.IsObject() && frame.Locals != static_cast(frame.Self) && HasField(frame.Self, name)) return GetField(frame.Self, name, debugInfo); else - return ScriptVariable::Get(name); + return ScriptGlobal::Get(name); } static inline Value FunctionCall(ScriptFrame& frame, const Value& self, const ScriptFunction::Ptr& func, const std::vector& arguments) @@ -70,21 +69,6 @@ public: return func->Invoke(arguments); } - static inline Value Indexer(ScriptFrame& frame, const std::vector& indexer, const DebugInfo& debugInfo = DebugInfo()) - { - Value result = indexer[0]->Evaluate(frame); - - for (int i = 1; i < indexer.size(); i++) { - if (result.IsEmpty()) - return Empty; - - Value index = indexer[i]->Evaluate(frame); - result = GetField(result, index, debugInfo); - } - - return result; - } - static inline Value NewFunction(ScriptFrame& frame, const std::vector& args, std::map *closedVars, const boost::shared_ptr& expression) { @@ -92,18 +76,6 @@ public: EvaluateClosedVars(frame, closedVars), expression)); } - static inline Value NewSlot(ScriptFrame& frame, const String& signal, const Value& slot) - { - ScriptSignal::Ptr sig = ScriptSignal::GetByName(signal); - - if (!sig) - BOOST_THROW_EXCEPTION(ScriptError("Signal '" + signal + "' does not exist.")); - - sig->AddSlot(boost::bind(SlotWrapper, slot, _1)); - - return Empty; - } - static inline Value NewApply(ScriptFrame& frame, const String& type, const String& target, const String& name, const boost::shared_ptr& filter, const String& fkvar, const String& fvvar, const boost::shared_ptr& fterm, std::map *closedVars, const boost::shared_ptr& expression, const DebugInfo& debugInfo = DebugInfo()) @@ -340,21 +312,6 @@ private: return result; } - static inline void SlotWrapper(const Value& funcName, const std::vector& arguments) - { - ScriptFunction::Ptr func; - - if (funcName.IsObjectType()) - func = funcName; - else - func = ScriptFunction::GetByName(funcName); - - if (!func) - BOOST_THROW_EXCEPTION(ScriptError("Function '" + funcName + "' does not exist.")); - - func->Invoke(arguments); - } - static inline Dictionary::Ptr EvaluateClosedVars(ScriptFrame& frame, std::map *closedVars) { Dictionary::Ptr locals; diff --git a/lib/db_ido/dbquery.cpp b/lib/db_ido/dbquery.cpp index 2ecccbf5c..33bee8078 100644 --- a/lib/db_ido/dbquery.cpp +++ b/lib/db_ido/dbquery.cpp @@ -19,7 +19,7 @@ #include "db_ido/dbquery.hpp" #include "base/initialize.hpp" -#include "base/scriptvariable.hpp" +#include "base/scriptglobal.hpp" using namespace icinga; @@ -27,20 +27,20 @@ INITIALIZE_ONCE(&DbQuery::StaticInitialize); void DbQuery::StaticInitialize(void) { - ScriptVariable::Set("DbCatConfig", DbCatConfig, true, true); - ScriptVariable::Set("DbCatState", DbCatState, true, true); - ScriptVariable::Set("DbCatAcknowledgement", DbCatAcknowledgement, true, true); - ScriptVariable::Set("DbCatComment", DbCatComment, true, true); - ScriptVariable::Set("DbCatDowntime", DbCatDowntime, true, true); - ScriptVariable::Set("DbCatEventHandler", DbCatEventHandler, true, true); - ScriptVariable::Set("DbCatExternalCommand", DbCatExternalCommand, true, true); - ScriptVariable::Set("DbCatFlapping", DbCatFlapping, true, true); - ScriptVariable::Set("DbCatCheck", DbCatCheck, true, true); - ScriptVariable::Set("DbCatLog", DbCatLog, true, true); - ScriptVariable::Set("DbCatNotification", DbCatNotification, true, true); - ScriptVariable::Set("DbCatProgramStatus", DbCatProgramStatus, true, true); - ScriptVariable::Set("DbCatRetention", DbCatRetention, true, true); - ScriptVariable::Set("DbCatStateHistory", DbCatStateHistory, true, true); + ScriptGlobal::Set("DbCatConfig", DbCatConfig); + ScriptGlobal::Set("DbCatState", DbCatState); + ScriptGlobal::Set("DbCatAcknowledgement", DbCatAcknowledgement); + ScriptGlobal::Set("DbCatComment", DbCatComment); + ScriptGlobal::Set("DbCatDowntime", DbCatDowntime); + ScriptGlobal::Set("DbCatEventHandler", DbCatEventHandler); + ScriptGlobal::Set("DbCatExternalCommand", DbCatExternalCommand); + ScriptGlobal::Set("DbCatFlapping", DbCatFlapping); + ScriptGlobal::Set("DbCatCheck", DbCatCheck); + ScriptGlobal::Set("DbCatLog", DbCatLog); + ScriptGlobal::Set("DbCatNotification", DbCatNotification); + ScriptGlobal::Set("DbCatProgramStatus", DbCatProgramStatus); + ScriptGlobal::Set("DbCatRetention", DbCatRetention); + ScriptGlobal::Set("DbCatStateHistory", DbCatStateHistory); - ScriptVariable::Set("DbCatEverything", ~(unsigned int)0, true, true); + ScriptGlobal::Set("DbCatEverything", ~(unsigned int)0); } diff --git a/lib/icinga/dependency-apply.cpp b/lib/icinga/dependency-apply.cpp index 97c88d446..8dd0ace17 100644 --- a/lib/icinga/dependency-apply.cpp +++ b/lib/icinga/dependency-apply.cpp @@ -58,16 +58,16 @@ void Dependency::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, cons Service::Ptr service; tie(host, service) = GetHostService(checkable); - builder->AddExpression(new SetExpression(ScopeCurrent, MakeIndexer("parent_host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di)); - builder->AddExpression(new SetExpression(ScopeCurrent, MakeIndexer("child_host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di)); + builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "parent_host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di)); + builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "child_host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di)); if (service) - builder->AddExpression(new SetExpression(ScopeCurrent, MakeIndexer("child_service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), di)); + builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "child_service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), di)); String zone = checkable->GetZone(); if (!zone.IsEmpty()) - builder->AddExpression(new SetExpression(ScopeCurrent, MakeIndexer("zone"), OpSetLiteral, MakeLiteral(zone), di)); + builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "zone"), OpSetLiteral, MakeLiteral(zone), di)); builder->AddExpression(new OwnedExpression(rule.GetExpression())); diff --git a/lib/icinga/icingaapplication.cpp b/lib/icinga/icingaapplication.cpp index c957dff1e..bcd7a79b9 100644 --- a/lib/icinga/icingaapplication.cpp +++ b/lib/icinga/icingaapplication.cpp @@ -26,7 +26,7 @@ #include "base/debug.hpp" #include "base/utility.hpp" #include "base/timer.hpp" -#include "base/scriptvariable.hpp" +#include "base/scriptglobal.hpp" #include "base/initialize.hpp" #include "base/statsfunction.hpp" @@ -39,12 +39,12 @@ INITIALIZE_ONCE(&IcingaApplication::StaticInitialize); void IcingaApplication::StaticInitialize(void) { - ScriptVariable::Set("EnableNotifications", true); - ScriptVariable::Set("EnableEventHandlers", true); - ScriptVariable::Set("EnableFlapping", true); - ScriptVariable::Set("EnableHostChecks", true); - ScriptVariable::Set("EnableServiceChecks", true); - ScriptVariable::Set("EnablePerfdata", true); + ScriptGlobal::Set("EnableNotifications", true); + ScriptGlobal::Set("EnableEventHandlers", true); + ScriptGlobal::Set("EnableFlapping", true); + ScriptGlobal::Set("EnableHostChecks", true); + ScriptGlobal::Set("EnableServiceChecks", true); + ScriptGlobal::Set("EnablePerfdata", true); String node_name = Utility::GetFQDN(); @@ -58,7 +58,7 @@ void IcingaApplication::StaticInitialize(void) } } - ScriptVariable::Set("NodeName", node_name); + ScriptGlobal::Set("NodeName", node_name); } REGISTER_STATSFUNCTION(IcingaApplicationStats, &IcingaApplication::StatsFunc); @@ -134,17 +134,12 @@ IcingaApplication::Ptr IcingaApplication::GetInstance(void) Dictionary::Ptr IcingaApplication::GetVars(void) const { - ScriptVariable::Ptr sv = ScriptVariable::GetByName("Vars"); - - if (!sv) - return Dictionary::Ptr(); - - return sv->GetData(); + return ScriptGlobal::Get("Vars"); } String IcingaApplication::GetNodeName(void) const { - return ScriptVariable::Get("NodeName"); + return ScriptGlobal::Get("NodeName"); } bool IcingaApplication::ResolveMacro(const String& macro, const CheckResult::Ptr&, Value *result) const @@ -242,7 +237,7 @@ bool IcingaApplication::GetEnableNotifications(void) const if (!GetOverrideEnableNotifications().IsEmpty()) return GetOverrideEnableNotifications(); else - return ScriptVariable::Get("EnableNotifications"); + return ScriptGlobal::Get("EnableNotifications"); } void IcingaApplication::SetEnableNotifications(bool enabled) @@ -260,7 +255,7 @@ bool IcingaApplication::GetEnableEventHandlers(void) const if (!GetOverrideEnableEventHandlers().IsEmpty()) return GetOverrideEnableEventHandlers(); else - return ScriptVariable::Get("EnableEventHandlers"); + return ScriptGlobal::Get("EnableEventHandlers"); } void IcingaApplication::SetEnableEventHandlers(bool enabled) @@ -278,7 +273,7 @@ bool IcingaApplication::GetEnableFlapping(void) const if (!GetOverrideEnableFlapping().IsEmpty()) return GetOverrideEnableFlapping(); else - return ScriptVariable::Get("EnableFlapping"); + return ScriptGlobal::Get("EnableFlapping"); } void IcingaApplication::SetEnableFlapping(bool enabled) @@ -296,7 +291,7 @@ bool IcingaApplication::GetEnableHostChecks(void) const if (!GetOverrideEnableHostChecks().IsEmpty()) return GetOverrideEnableHostChecks(); else - return ScriptVariable::Get("EnableHostChecks"); + return ScriptGlobal::Get("EnableHostChecks"); } void IcingaApplication::SetEnableHostChecks(bool enabled) @@ -314,7 +309,7 @@ bool IcingaApplication::GetEnableServiceChecks(void) const if (!GetOverrideEnableServiceChecks().IsEmpty()) return GetOverrideEnableServiceChecks(); else - return ScriptVariable::Get("EnableServiceChecks"); + return ScriptGlobal::Get("EnableServiceChecks"); } void IcingaApplication::SetEnableServiceChecks(bool enabled) @@ -332,7 +327,7 @@ bool IcingaApplication::GetEnablePerfdata(void) const if (!GetOverrideEnablePerfdata().IsEmpty()) return GetOverrideEnablePerfdata(); else - return ScriptVariable::Get("EnablePerfdata"); + return ScriptGlobal::Get("EnablePerfdata"); } void IcingaApplication::SetEnablePerfdata(bool enabled) diff --git a/lib/icinga/notification-apply.cpp b/lib/icinga/notification-apply.cpp index 0dccc5f80..f7fd494cd 100644 --- a/lib/icinga/notification-apply.cpp +++ b/lib/icinga/notification-apply.cpp @@ -58,15 +58,15 @@ void Notification::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, co Service::Ptr service; tie(host, service) = GetHostService(checkable); - builder->AddExpression(new SetExpression(ScopeCurrent, MakeIndexer("host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di)); + builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di)); if (service) - builder->AddExpression(new SetExpression(ScopeCurrent, MakeIndexer("service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), di)); + builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), di)); String zone = checkable->GetZone(); if (!zone.IsEmpty()) - builder->AddExpression(new SetExpression(ScopeCurrent, MakeIndexer("zone"), OpSetLiteral, MakeLiteral(zone), di)); + builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "zone"), OpSetLiteral, MakeLiteral(zone), di)); builder->AddExpression(new OwnedExpression(rule.GetExpression())); diff --git a/lib/icinga/notification.cpp b/lib/icinga/notification.cpp index 1ebe3a5fd..8239fa39f 100644 --- a/lib/icinga/notification.cpp +++ b/lib/icinga/notification.cpp @@ -27,7 +27,7 @@ #include "base/convert.hpp" #include "base/exception.hpp" #include "base/initialize.hpp" -#include "base/scriptvariable.hpp" +#include "base/scriptglobal.hpp" #include "base/scriptfunction.hpp" #include @@ -58,22 +58,22 @@ String NotificationNameComposer::MakeName(const String& shortName, const Object: void Notification::StaticInitialize(void) { - ScriptVariable::Set("OK", StateFilterOK, true, true); - ScriptVariable::Set("Warning", StateFilterWarning, true, true); - ScriptVariable::Set("Critical", StateFilterCritical, true, true); - ScriptVariable::Set("Unknown", StateFilterUnknown, true, true); - ScriptVariable::Set("Up", StateFilterUp, true, true); - ScriptVariable::Set("Down", StateFilterDown, true, true); + ScriptGlobal::Set("OK", StateFilterOK); + ScriptGlobal::Set("Warning", StateFilterWarning); + ScriptGlobal::Set("Critical", StateFilterCritical); + ScriptGlobal::Set("Unknown", StateFilterUnknown); + ScriptGlobal::Set("Up", StateFilterUp); + ScriptGlobal::Set("Down", StateFilterDown); - ScriptVariable::Set("DowntimeStart", 1 << NotificationDowntimeStart, true, true); - ScriptVariable::Set("DowntimeEnd", 1 << NotificationDowntimeEnd, true, true); - ScriptVariable::Set("DowntimeRemoved", 1 << NotificationDowntimeRemoved, true, true); - ScriptVariable::Set("Custom", 1 << NotificationCustom, true, true); - ScriptVariable::Set("Acknowledgement", 1 << NotificationAcknowledgement, true, true); - ScriptVariable::Set("Problem", 1 << NotificationProblem, true, true); - ScriptVariable::Set("Recovery", 1 << NotificationRecovery, true, true); - ScriptVariable::Set("FlappingStart", 1 << NotificationFlappingStart, true, true); - ScriptVariable::Set("FlappingEnd", 1 << NotificationFlappingEnd, true, true); + ScriptGlobal::Set("DowntimeStart", 1 << NotificationDowntimeStart); + ScriptGlobal::Set("DowntimeEnd", 1 << NotificationDowntimeEnd); + ScriptGlobal::Set("DowntimeRemoved", 1 << NotificationDowntimeRemoved); + ScriptGlobal::Set("Custom", 1 << NotificationCustom); + ScriptGlobal::Set("Acknowledgement", 1 << NotificationAcknowledgement); + ScriptGlobal::Set("Problem", 1 << NotificationProblem); + ScriptGlobal::Set("Recovery", 1 << NotificationRecovery); + ScriptGlobal::Set("FlappingStart", 1 << NotificationFlappingStart); + ScriptGlobal::Set("FlappingEnd", 1 << NotificationFlappingEnd); } void Notification::OnConfigLoaded(void) diff --git a/lib/icinga/scheduleddowntime-apply.cpp b/lib/icinga/scheduleddowntime-apply.cpp index 52b86d3bf..71114c0e0 100644 --- a/lib/icinga/scheduleddowntime-apply.cpp +++ b/lib/icinga/scheduleddowntime-apply.cpp @@ -57,15 +57,15 @@ void ScheduledDowntime::EvaluateApplyRuleInstance(const Checkable::Ptr& checkabl Service::Ptr service; tie(host, service) = GetHostService(checkable); - builder->AddExpression(new SetExpression(ScopeCurrent, MakeIndexer("host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di)); + builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di)); if (service) - builder->AddExpression(new SetExpression(ScopeCurrent, MakeIndexer("service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), di)); + builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), di)); String zone = checkable->GetZone(); if (!zone.IsEmpty()) - builder->AddExpression(new SetExpression(ScopeCurrent, MakeIndexer("zone"), OpSetLiteral, MakeLiteral(zone), di)); + builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "zone"), OpSetLiteral, MakeLiteral(zone), di)); builder->AddExpression(new OwnedExpression(rule.GetExpression())); diff --git a/lib/icinga/service-apply.cpp b/lib/icinga/service-apply.cpp index 39a6b5dbd..d7be39235 100644 --- a/lib/icinga/service-apply.cpp +++ b/lib/icinga/service-apply.cpp @@ -52,14 +52,14 @@ void Service::EvaluateApplyRuleInstance(const Host::Ptr& host, const String& nam builder->SetName(name); builder->SetScope(frame.Locals); - builder->AddExpression(new SetExpression(ScopeCurrent, MakeIndexer("host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di)); + builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di)); - builder->AddExpression(new SetExpression(ScopeCurrent, MakeIndexer("name"), OpSetLiteral, MakeLiteral(name), di)); + builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "name"), OpSetLiteral, MakeLiteral(name), di)); String zone = host->GetZone(); if (!zone.IsEmpty()) - builder->AddExpression(new SetExpression(ScopeCurrent, MakeIndexer("zone"), OpSetLiteral, MakeLiteral(zone), di)); + builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "zone"), OpSetLiteral, MakeLiteral(zone), di)); builder->AddExpression(new OwnedExpression(rule.GetExpression()));