1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #ifndef CONFIGCOMPILER_H
21 #define CONFIGCOMPILER_H
23 #include "config/i2-config.hpp"
24 #include "config/expression.hpp"
25 #include "config/configtype.hpp"
26 #include "base/debuginfo.hpp"
27 #include "base/registry.hpp"
28 #include "base/initialize.hpp"
29 #include "base/singleton.hpp"
30 #include <boost/function.hpp>
34 typedef union YYSTYPE YYSTYPE;
35 typedef void *yyscan_t;
42 int yylex(YYSTYPE *context, icinga::DebugInfo *di, yyscan_t scanner);
43 int yyparse(std::vector<icinga::Expression *> *elist, icinga::ConfigCompiler *context);
49 * The configuration compiler can be used to compile a configuration file
50 * into a number of configuration items.
54 class I2_CONFIG_API ConfigCompiler
57 explicit ConfigCompiler(const String& path, std::istream *input, const String& zone = String());
58 virtual ~ConfigCompiler(void);
60 Expression *Compile(void);
62 static Expression *CompileStream(const String& path, std::istream *stream, const String& zone = String());
63 static Expression *CompileFile(const String& path, const String& zone = String());
64 static Expression *CompileText(const String& path, const String& text, const String& zone = String());
66 static void AddIncludeSearchDir(const String& dir);
68 String GetPath(void) const;
70 void SetZone(const String& zone);
71 String GetZone(void) const;
73 static void CollectIncludes(std::vector<Expression *>& expressions, const String& file, const String& zone);
75 /* internally used methods */
76 Expression *HandleInclude(const String& include, bool search, const DebugInfo& debuginfo = DebugInfo());
77 Expression *HandleIncludeRecursive(const String& path, const String& pattern, const DebugInfo& debuginfo = DebugInfo());
78 void HandleLibrary(const String& library);
80 size_t ReadInput(char *buffer, size_t max_bytes);
81 void *GetScanner(void) const;
85 std::istream *m_Input;
91 std::stack<TypeRuleList::Ptr> m_RuleLists;
92 ConfigType::Ptr m_Type;
94 std::stack<bool> m_Apply;
95 std::stack<bool> m_ObjectAssign;
96 std::stack<bool> m_SeenAssign;
97 std::stack<Expression *> m_Assign;
98 std::stack<Expression *> m_Ignore;
99 std::stack<String> m_FKVar;
100 std::stack<String> m_FVVar;
101 std::stack<Expression *> m_FTerm;
103 static std::vector<String> m_IncludeSearchDirs;
105 void InitializeScanner(void);
106 void DestroyScanner(void);
108 friend int ::yylex(YYSTYPE *context, icinga::DebugInfo *di, yyscan_t scanner);
109 friend int ::yyparse(std::vector<Expression *> *elist, ConfigCompiler *context);
112 class I2_CONFIG_API ConfigFragmentRegistry : public Registry<ConfigFragmentRegistry, String>
115 static ConfigFragmentRegistry *GetInstance(void);
118 #define REGISTER_CONFIG_FRAGMENT(id, name, fragment) \
120 void RegisterConfigFragment(void) \
122 icinga::ConfigFragmentRegistry::GetInstance()->Register(name, fragment); \
125 INITIALIZE_ONCE(RegisterConfigFragment); \
130 #endif /* CONFIGCOMPILER_H */