]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ConfigParser.h
Merged from trunk
[thirdparty/squid.git] / src / ConfigParser.h
1
2 /*
3 *
4 * SQUID Web Proxy Cache http://www.squid-cache.org/
5 * ----------------------------------------------------------
6 *
7 * Squid is the result of efforts by numerous individuals from
8 * the Internet community; see the CONTRIBUTORS file for full
9 * details. Many organizations have provided support for Squid's
10 * development; see the SPONSORS file for full details. Squid is
11 * Copyrighted (C) 2001 by the Regents of the University of
12 * California; see the COPYRIGHT file for full details. Squid
13 * incorporates software developed and/or copyrighted by other
14 * sources; see the CREDITS file for full details.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
29 *
30 *
31 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
32 */
33
34 #ifndef SQUID_CONFIGPARSER_H
35 #define SQUID_CONFIGPARSER_H
36
37 #include "SquidString.h"
38
39 #include <queue>
40 #include <stack>
41 #include <string>
42
43 class wordlist;
44 /**
45 * Limit to how long any given config line may be.
46 * This affects squid.conf and all included files.
47 *
48 * Behaviour when setting larger than 2KB is unknown.
49 * The config parser read mechanism can cope, but the other systems
50 * receiving the data from its buffers on such lines may not.
51 */
52 #define CONFIG_LINE_LIMIT 2048
53
54 /**
55 * A configuration file Parser. Instances of this class track
56 * parsing state and perform tokenisation. Syntax is currently
57 * taken care of outside this class.
58 *
59 * One reason for this class is to allow testing of configuration
60 * using modules without linking cache_cf.o in - because that drags
61 * in all of squid by reference. Instead the tokeniser only is
62 * brought in.
63 */
64 class ConfigParser
65 {
66
67 public:
68 /**
69 * Parsed tokens type: simple tokens, quoted tokens or function
70 * like parameters.
71 */
72 enum TokenType {SimpleToken, QuotedToken, FunctionParameters};
73
74 void destruct();
75 static void ParseUShort(unsigned short *var);
76 static void ParseBool(bool *var);
77 static const char *QuoteString(const String &var);
78 static void ParseWordList(wordlist **list);
79
80 /**
81 * Backward compatibility wrapper for the ConfigParser::NextToken method.
82 * If the configuration_includes_quoted_values configuration parameter is
83 * set to 'off' this interprets the quoted tokens as filenames.
84 */
85 static char * strtokFile();
86
87 /**
88 * Returns the body of the next element. The element is either a token or
89 * a quoted string with optional escape sequences and/or macros. The body
90 * of a quoted string element does not include quotes or escape sequences.
91 * Future code will want to see Elements and not just their bodies.
92 */
93 static char *NextToken();
94
95 /**
96 * Backward compatibility wrapper for ConfigParser::RegexPattern method.
97 * If the configuration_includes_quoted_values configuration parameter is
98 * set to 'off' this interprets the quoted tokens as filenames.
99 */
100 static char *RegexStrtokFile();
101
102 /**
103 * Parse the next token as a regex patern. The regex patterns are non quoted
104 * tokens.
105 */
106 static char *RegexPattern();
107
108 /**
109 * Parse the next token with support for quoted values enabled even if
110 * the configuration_includes_quoted_values is set to off
111 */
112 static char *NextQuotedToken();
113
114 /// \return true if the last parsed token was quoted
115 static bool LastTokenWasQuoted() {return (LastTokenType == ConfigParser::QuotedToken);}
116
117 /**
118 * \return the next quoted string or the raw string data until the end of line.
119 * This method allows %macros in unquoted strings to keep compatibility
120 * for the logformat option.
121 */
122 static char *NextQuotedOrToEol();
123
124 /**
125 * Preview the next token. The next NextToken() and strtokFile() call
126 * will return the same token.
127 * On parse error (eg invalid characters in token) will return an
128 * error message as token.
129 */
130 static char *PeekAtToken();
131
132 /**
133 * The next NextToken call will return the token as next element
134 * It can be used repeatedly to add more than one tokens in a FIFO list.
135 */
136 static void TokenPutBack(const char *token);
137
138 /// Set the configuration file line to parse.
139 static void SetCfgLine(char *line);
140
141 /// Allow %macros inside quoted strings
142 static void EnableMacros() {AllowMacros_ = true;}
143
144 /// Do not allow %macros inside quoted strings
145 static void DisableMacros() {AllowMacros_ = false;}
146
147 /// configuration_includes_quoted_values in squid.conf
148 static bool RecognizeQuotedValues;
149
150 /**
151 * Strict syntax mode. Does not allow not alphanumeric characters in unquoted tokens.
152 * Controled by the configuration_includes_quoted_values in squid.conf but remains
153 * false when the the legacy ConfigParser::NextQuotedToken() call forces
154 * RecognizeQuotedValues to be temporary true.
155 */
156 static bool StrictMode;
157
158 protected:
159 /**
160 * Class used to store required information for the current
161 * configuration file.
162 */
163 class CfgFile
164 {
165 public:
166 CfgFile(): wordFile(NULL), parsePos(NULL), lineNo(0) { parseBuffer[0] = '\0';}
167 ~CfgFile();
168 /// True if the configuration file is open
169 bool isOpen() {return wordFile != NULL;}
170
171 /**
172 * Open the file given by 'path' and initializes the CfgFile object
173 * to start parsing
174 */
175 bool startParse(char *path);
176
177 /**
178 * Do the next parsing step:
179 * reads the next line from file if required.
180 * \return the body of next element or a NULL pointer if there are no more token elements in the file.
181 * \param type will be filled with the ConfigParse::TokenType for any element found, or left unchanged if NULL is returned.
182 */
183 char *parse(TokenType &type);
184
185 private:
186 bool getFileLine(); ///< Read the next line from the file
187 /**
188 * Return the body of the next element. If the wasQuoted is given
189 * set to true if the element was quoted.
190 */
191 char *nextElement(TokenType &type);
192 FILE *wordFile; ///< Pointer to the file.
193 char parseBuffer[CONFIG_LINE_LIMIT]; ///< Temporary buffer to store data to parse
194 const char *parsePos; ///< The next element position in parseBuffer string
195 public:
196 std::string filePath; ///< The file path
197 std::string currentLine; ///< The current line to parse
198 int lineNo; ///< Current line number
199 };
200
201 /// Return the last TokenPutBack() queued element or NULL if none exist
202 static char *Undo();
203
204 /**
205 * Unquotes the token, which must be quoted.
206 * \param next if it is not NULL, it is set after the end of token.
207 */
208 static char *UnQuote(const char *token, const char **next = NULL);
209
210 /**
211 * Does the real tokens parsing job: Ignore comments, unquote an
212 * element if required.
213 * \return the next token, or NULL if there are no available tokens in the nextToken string.
214 * \param nextToken updated to point to the pos after parsed token.
215 * \param type The token type
216 */
217 static char *TokenParse(const char * &nextToken, TokenType &type);
218
219 /// Wrapper method for TokenParse.
220 static char *NextElement(TokenType &type);
221 static std::stack<CfgFile *> CfgFiles; ///< The stack of open cfg files
222 static TokenType LastTokenType; ///< The type of last parsed element
223 static const char *CfgLine; ///< The current line to parse
224 static const char *CfgPos; ///< Pointer to the next element in cfgLine string
225 static std::queue<char *> CfgLineTokens_; ///< Store the list of tokens for current configuration line
226 static std::queue<std::string> Undo_; ///< The list with TokenPutBack() queued elements
227 static bool AllowMacros_;
228 static bool ParseQuotedOrToEol_; ///< The next tokens will be handled as quoted or to_eol token
229 static bool PreviewMode_; ///< The next token will not poped from cfg files, will just previewd.
230 };
231
232 int parseConfigFile(const char *file_name);
233
234 #endif /* SQUID_CONFIGPARSER_H */