]> granicus.if.org Git - icinga2/blob - lib/config/configcompiler.hpp
Move the parser's global variables to the ConfigCompiler class
[icinga2] / lib / config / configcompiler.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #ifndef CONFIGCOMPILER_H
21 #define CONFIGCOMPILER_H
22
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>
31 #include <iostream>
32 #include <stack>
33
34 typedef union YYSTYPE YYSTYPE;
35 typedef void *yyscan_t;
36
37 namespace icinga
38 {
39         class ConfigCompiler;
40 }
41
42 int yylex(YYSTYPE *context, icinga::DebugInfo *di, yyscan_t scanner);
43 int yyparse(std::vector<icinga::Expression *> *elist, icinga::ConfigCompiler *context);
44
45 namespace icinga
46 {
47
48 /**
49  * The configuration compiler can be used to compile a configuration file
50  * into a number of configuration items.
51  *
52  * @ingroup config
53  */
54 class I2_CONFIG_API ConfigCompiler
55 {
56 public:
57         explicit ConfigCompiler(const String& path, std::istream *input, const String& zone = String());
58         virtual ~ConfigCompiler(void);
59
60         Expression *Compile(void);
61
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());
65
66         static void AddIncludeSearchDir(const String& dir);
67
68         String GetPath(void) const;
69
70         void SetZone(const String& zone);
71         String GetZone(void) const;
72
73         static void CollectIncludes(std::vector<Expression *>& expressions, const String& file, const String& zone);
74
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);
79
80         size_t ReadInput(char *buffer, size_t max_bytes);
81         void *GetScanner(void) const;
82
83 private:
84         String m_Path;
85         std::istream *m_Input;
86         String m_Zone;
87
88         void *m_Scanner;
89         bool m_Eof;
90
91         std::stack<TypeRuleList::Ptr> m_RuleLists;
92         ConfigType::Ptr m_Type;
93
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;
102
103         static std::vector<String> m_IncludeSearchDirs;
104
105         void InitializeScanner(void);
106         void DestroyScanner(void);
107
108         friend int ::yylex(YYSTYPE *context, icinga::DebugInfo *di, yyscan_t scanner);
109         friend int ::yyparse(std::vector<Expression *> *elist, ConfigCompiler *context);
110 };
111
112 class I2_CONFIG_API ConfigFragmentRegistry : public Registry<ConfigFragmentRegistry, String>
113 {
114 public:
115         static ConfigFragmentRegistry *GetInstance(void);
116 };
117
118 #define REGISTER_CONFIG_FRAGMENT(id, name, fragment) \
119         namespace { \
120                 void RegisterConfigFragment(void) \
121                 { \
122                         icinga::ConfigFragmentRegistry::GetInstance()->Register(name, fragment); \
123                 } \
124                 \
125                 INITIALIZE_ONCE(RegisterConfigFragment); \
126         }
127
128 }
129
130 #endif /* CONFIGCOMPILER_H */