]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ConfigParser.h
2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 #ifndef SQUID_CONFIGPARSER_H
10 #define SQUID_CONFIGPARSER_H
12 #include "SquidString.h"
20 * Limit to how long any given config line may be.
21 * This affects squid.conf and all included files.
23 * Behaviour when setting larger than 2KB is unknown.
24 * The config parser read mechanism can cope, but the other systems
25 * receiving the data from its buffers on such lines may not.
27 #define CONFIG_LINE_LIMIT 2048
30 * A configuration file Parser. Instances of this class track
31 * parsing state and perform tokenisation. Syntax is currently
32 * taken care of outside this class.
34 * One reason for this class is to allow testing of configuration
35 * using modules without linking cache_cf.o in - because that drags
36 * in all of squid by reference. Instead the tokeniser only is
44 * Parsed tokens type: simple tokens, quoted tokens or function
47 enum TokenType
{SimpleToken
, QuotedToken
, FunctionParameters
};
50 static void ParseUShort(unsigned short *var
);
51 static void ParseBool(bool *var
);
52 static const char *QuoteString(const String
&var
);
53 static void ParseWordList(wordlist
**list
);
56 * Backward compatibility wrapper for the ConfigParser::NextToken method.
57 * If the configuration_includes_quoted_values configuration parameter is
58 * set to 'off' this interprets the quoted tokens as filenames.
60 static char * strtokFile();
63 * Returns the body of the next element. The element is either a token or
64 * a quoted string with optional escape sequences and/or macros. The body
65 * of a quoted string element does not include quotes or escape sequences.
66 * Future code will want to see Elements and not just their bodies.
68 static char *NextToken();
71 * Backward compatibility wrapper for ConfigParser::RegexPattern method.
72 * If the configuration_includes_quoted_values configuration parameter is
73 * set to 'off' this interprets the quoted tokens as filenames.
75 static char *RegexStrtokFile();
78 * Parse the next token as a regex patern. The regex patterns are non quoted
81 static char *RegexPattern();
84 * Parse the next token with support for quoted values enabled even if
85 * the configuration_includes_quoted_values is set to off
87 static char *NextQuotedToken();
89 /// \return true if the last parsed token was quoted
90 static bool LastTokenWasQuoted() {return (LastTokenType
== ConfigParser::QuotedToken
);}
93 * \return the next quoted string or the raw string data until the end of line.
94 * This method allows %macros in unquoted strings to keep compatibility
95 * for the logformat option.
97 static char *NextQuotedOrToEol();
100 * the next key value pair which must be separated by "="
101 * \return true on success, false otherwise
103 static bool NextKvPair(char * &key
, char * &value
);
106 * Preview the next token. The next NextToken() and strtokFile() call
107 * will return the same token.
108 * On parse error (eg invalid characters in token) will return an
109 * error message as token.
111 static char *PeekAtToken();
114 * The next NextToken call will return the token as next element
115 * It can be used repeatedly to add more than one tokens in a FIFO list.
117 static void TokenPutBack(const char *token
);
119 /// Set the configuration file line to parse.
120 static void SetCfgLine(char *line
);
122 /// Allow %macros inside quoted strings
123 static void EnableMacros() {AllowMacros_
= true;}
125 /// Do not allow %macros inside quoted strings
126 static void DisableMacros() {AllowMacros_
= false;}
128 /// configuration_includes_quoted_values in squid.conf
129 static bool RecognizeQuotedValues
;
132 * Strict syntax mode. Does not allow not alphanumeric characters in unquoted tokens.
133 * Controled by the configuration_includes_quoted_values in squid.conf but remains
134 * false when the the legacy ConfigParser::NextQuotedToken() call forces
135 * RecognizeQuotedValues to be temporary true.
137 static bool StrictMode
;
141 * Class used to store required information for the current
142 * configuration file.
147 CfgFile(): wordFile(NULL
), parsePos(NULL
), lineNo(0) { parseBuffer
[0] = '\0';}
149 /// True if the configuration file is open
150 bool isOpen() {return wordFile
!= NULL
;}
153 * Open the file given by 'path' and initializes the CfgFile object
156 bool startParse(char *path
);
159 * Do the next parsing step:
160 * reads the next line from file if required.
161 * \return the body of next element or a NULL pointer if there are no more token elements in the file.
162 * \param type will be filled with the ConfigParse::TokenType for any element found, or left unchanged if NULL is returned.
164 char *parse(TokenType
&type
);
167 bool getFileLine(); ///< Read the next line from the file
169 * Return the body of the next element. If the wasQuoted is given
170 * set to true if the element was quoted.
172 char *nextElement(TokenType
&type
);
173 FILE *wordFile
; ///< Pointer to the file.
174 char parseBuffer
[CONFIG_LINE_LIMIT
]; ///< Temporary buffer to store data to parse
175 const char *parsePos
; ///< The next element position in parseBuffer string
177 std::string filePath
; ///< The file path
178 std::string currentLine
; ///< The current line to parse
179 int lineNo
; ///< Current line number
182 /// Return the last TokenPutBack() queued element or NULL if none exist
186 * Unquotes the token, which must be quoted.
187 * \param next if it is not NULL, it is set after the end of token.
189 static char *UnQuote(const char *token
, const char **next
= NULL
);
192 * Does the real tokens parsing job: Ignore comments, unquote an
193 * element if required.
194 * \return the next token, or NULL if there are no available tokens in the nextToken string.
195 * \param nextToken updated to point to the pos after parsed token.
196 * \param type The token type
198 static char *TokenParse(const char * &nextToken
, TokenType
&type
);
200 /// Wrapper method for TokenParse.
201 static char *NextElement(TokenType
&type
);
202 static std::stack
<CfgFile
*> CfgFiles
; ///< The stack of open cfg files
203 static TokenType LastTokenType
; ///< The type of last parsed element
204 static const char *CfgLine
; ///< The current line to parse
205 static const char *CfgPos
; ///< Pointer to the next element in cfgLine string
206 static std::queue
<char *> CfgLineTokens_
; ///< Store the list of tokens for current configuration line
207 static std::queue
<std::string
> Undo_
; ///< The list with TokenPutBack() queued elements
208 static bool AllowMacros_
;
209 static bool ParseQuotedOrToEol_
; ///< The next tokens will be handled as quoted or to_eol token
210 static bool RecognizeQuotedPair_
; ///< The next tokens may contain quoted-pair (\-escaped) characters
211 static bool PreviewMode_
; ///< The next token will not poped from cfg files, will just previewd.
212 static bool ParseKvPair_
; ///<The next token will be handled as kv-pair token
213 static enum ParsingStates
{atParseKey
, atParseValue
} KvPairState_
; ///< Parsing state while parsing kv-pair tokens
216 int parseConfigFile(const char *file_name
);
218 #endif /* SQUID_CONFIGPARSER_H */