1 /*
2 
3                           Firewall Builder
4 
5                  Copyright (C) 2009 NetCitadel, LLC
6 
7   Author:  Vadim Kurland     vadim@fwbuilder.org
8 
9   $Id$
10 
11   This program is free software which we release under the GNU General Public
12   License. You may redistribute and/or modify this program under the terms
13   of that license as published by the Free Software Foundation; either
14   version 2 of the License, or (at your option) any later version.
15 
16   This program is distributed in the hope that it will be useful,
17   but WITHOUT ANY WARRANTY; without even the implied warranty of
18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   GNU General Public License for more details.
20 
21   To get a copy of the GNU General Public License, write to the Free Software
22   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 
24 */
25 
26 #ifndef __BASE_COMPILER_HH__
27 #define __BASE_COMPILER_HH__
28 
29 #include "fwbuilder/libfwbuilder-config.h"
30 #include "fwbuilder/FWException.h"
31 #include "fwbuilder/Rule.h"
32 
33 #include "fwcompiler/exceptions.h"
34 
35 #include <sstream>
36 
37 namespace fwcompiler {
38 
39     class FWCompilerException : public libfwbuilder::FWException {
40 	libfwbuilder::Rule *rule;
41 	public:
42 	FWCompilerException(libfwbuilder::Rule *r,const std::string &err);
getRule()43 	libfwbuilder::Rule *getRule() const { return rule; }
44     };
45 
46     class BaseCompiler
47     {
48 
49         std::string level_macro;
50         // all errors generated by the compiler
51 	std::stringstream errors_buffer;
52         // a dictionary mapping rule label to the list of errors associated
53         // with it.
54         std::map<std::string, std::list<std::string> > rule_errors;
55 
56         // in test mode we trat fatal errors as errors and continue after
57         // printing error message
58         bool test_mode;
59         // this is single-rule-compile mode; compiler is embedded in the GUI
60         bool embedded_mode;
61 
62         /**
63          * assembles standard error message using format similar to
64          * the error message format of gcc. Useful to prepare errors
65          * and warnings before calling error() or warning() methods
66          */
67         std::string stdErrorMessage(libfwbuilder::FWObject *fw,
68                                     libfwbuilder::FWObject *ruleset,
69                                     libfwbuilder::FWObject *rule,
70                                     const std::string &errstr);
71 
72         std::string setLevel(const std::string &level, const std::string &errstr);
73 
74         void printError(const std::string &errstr);
75 
76         void message(const std::string &level,
77                      libfwbuilder::FWObject *fw,
78                      libfwbuilder::FWObject *ruleset,
79                      libfwbuilder::FWObject *rule,
80                      const std::string &errstr);
81 
82 public:
83         typedef enum {FWCOMPILER_SUCCESS, FWCOMPILER_WARNING, FWCOMPILER_ERROR} termination_status;
84 
85 protected:
86         termination_status status;
87 
88 public:
89 
setTestMode()90         virtual void setTestMode() { test_mode = true; }
inTestMode()91         bool inTestMode() { return test_mode; }
92 
setEmbeddedMode()93         virtual void setEmbeddedMode() { embedded_mode = true; }
inEmbeddedMode()94         bool inEmbeddedMode() { return embedded_mode; }
95 
getStatus()96         termination_status getStatus() { return status; }
97 
98         /**
99          * prints error message and aborts the program. If compiler is
100          * in testing mode (flag test_mode==true), then just prints
101          * the error message and returns.
102          */
103 	virtual void abort(const std::string &errstr) throw(libfwbuilder::FWException);
104 	virtual void abort(libfwbuilder::FWObject *fw,
105                            libfwbuilder::FWObject *ruleset,
106                            libfwbuilder::FWObject *rule,
107                            const std::string &errstr) throw(libfwbuilder::FWException);
108 
109         /**
110          * prints an error message and returns
111          */
112 	virtual void error(const std::string &warnstr);
113 	virtual void error(libfwbuilder::FWObject *fw,
114                            libfwbuilder::FWObject *ruleset,
115                            libfwbuilder::FWObject *rule,
116                            const std::string &warnstr);
117 
118         /**
119          * prints warning message
120          */
121 	virtual void warning(const std::string &warnstr);
122 	virtual void warning(libfwbuilder::FWObject *fw,
123                              libfwbuilder::FWObject *ruleset,
124                              libfwbuilder::FWObject *rule,
125                              const std::string &warnstr);
126 
127         /**
128          * prints info message. These are only printed to stdout if compiler
129          * is not in embedded mode. In embedded mode info messages are ignored.
130          */
131 	virtual void info(const std::string &warnstr);
132 
~BaseCompiler()133 	virtual ~BaseCompiler() {};
134 
BaseCompiler()135 	BaseCompiler()
136         {
137             test_mode = false;
138             embedded_mode = false;
139             level_macro = "%LEVEL%";
140             status = FWCOMPILER_SUCCESS;
141         };
142 
143 	std::string getErrors(const std::string &comment_sep);
144 	bool haveErrorsAndWarnings();
145         void clearErrors();
146 
147         std::string getErrorsForRule(libfwbuilder::Rule *rule,
148                                      const std::string &comment_sep);
149 
150         /**
151          * fills a list of strings with regular expressions that match
152          * error messages
153          */
154         static void errorRegExp(std::list<std::string> *err_regexp);
155 
156         /**
157          * fills a list of strings with regular expressions that match
158          * warning messages
159          */
160         static void warningRegExp(std::list<std::string> *warn_regexp);
161 
162     };
163 }
164 
165 #endif
166