ConfigParser.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
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.
7  */
8 
9 #ifndef SQUID_SRC_CONFIGPARSER_H
10 #define SQUID_SRC_CONFIGPARSER_H
11 
12 #include "acl/forward.h"
13 #include "base/forward.h"
14 #include "sbuf/forward.h"
15 #include "SquidString.h"
16 
17 #include <memory>
18 #include <queue>
19 #include <stack>
20 #include <string>
21 
22 class CachePeer;
23 class wordlist;
24 
33 #define CONFIG_LINE_LIMIT 2048
34 
46 {
47 
48 public:
54 
55  void destruct();
56 
58  void closeDirective();
59 
62 
64  SBuf token(const char *expectedTokenDescription);
65 
69  bool optionalKvPair(char * &key, char * &value);
70 
72  bool skipOptional(const char *keyword);
73 
76 
78  std::unique_ptr<RegexPattern> regex(const char *expectedRegexDescription);
79 
81  CachePeer &cachePeer(const char *peerNameTokenDescription);
82 
83  static void ParseUShort(unsigned short *var);
84  static void ParseBool(bool *var);
85  static const char *QuoteString(const String &var);
86  static void ParseWordList(wordlist **list);
87 
93  static char * strtokFile();
94 
101  static char *NextToken();
102 
108  static char *RegexStrtokFile();
109 
114  static char *NextQuotedToken();
115 
118 
124  static char *NextQuotedOrToEol();
125 
130  static bool NextKvPair(char * &key, char * &value);
131 
138  static char *PeekAtToken();
139 
141  static void SetCfgLine(char *line);
142 
144  static void EnableMacros() {AllowMacros_ = true;}
145 
147  static void DisableMacros() {AllowMacros_ = false;}
148 
149  static SBuf CurrentLocation();
150 
153 
160  static bool StrictMode;
161 
162 protected:
167  class CfgFile
168  {
169  public:
170  CfgFile(): wordFile(nullptr), parsePos(nullptr), lineNo(0) { parseBuffer[0] = '\0';}
171  ~CfgFile();
173  bool isOpen() {return wordFile != nullptr;}
174 
179  bool startParse(char *path);
180 
187  char *parse(TokenType &type);
188 
189  private:
190  bool getFileLine();
191 
195  char *nextElement(TokenType &type);
196  FILE *wordFile;
198  const char *parsePos;
199  public:
200  std::string filePath;
201  std::string currentLine;
202  int lineNo;
203  };
204 
209  static char *UnQuote(const char *token, const char **next = nullptr);
210 
218  static char *TokenParse(const char * &nextToken, TokenType &type);
219 
221  static char *NextElement(TokenType &type);
222  static std::stack<CfgFile *> CfgFiles;
224  static const char *CfgLine;
225  static const char *CfgPos;
226  static std::queue<char *> CfgLineTokens_;
227  static bool AllowMacros_;
228  static bool ParseQuotedOrToEol_;
229  static bool RecognizeQuotedPair_;
230  static bool PreviewMode_;
231  static bool ParseKvPair_;
233 };
234 
235 namespace Configuration {
237 void Parse();
238 }
239 
240 #endif /* SQUID_SRC_CONFIGPARSER_H */
241 
ACLList * optionalAclList()
parses an [if [!]<acl>...] construct
static SBuf CurrentLocation()
const char * parsePos
The next element position in parseBuffer string.
Definition: ConfigParser.h:198
static char * NextQuotedToken()
static char * strtokFile()
Definition: ConfigParser.cc:65
static bool RecognizeQuotedPair_
The next tokens may contain quoted-pair (-escaped) characters.
Definition: ConfigParser.h:229
static void EnableMacros()
Allow macros inside quoted strings.
Definition: ConfigParser.h:144
static bool PreviewMode_
The next token will not popped from cfg files, will just previewd.
Definition: ConfigParser.h:230
static void ParseBool(bool *var)
Definition: cache_cf.cc:3106
static bool StrictMode
Definition: ConfigParser.h:160
FILE * wordFile
Pointer to the file.
Definition: ConfigParser.h:196
static bool LastTokenWasQuoted()
Definition: ConfigParser.h:117
Definition: SBuf.h:93
static bool RecognizeQuotedValues
configuration_includes_quoted_values in squid.conf
Definition: ConfigParser.h:152
static char * PeekAtToken()
static char * UnQuote(const char *token, const char **next=nullptr)
static bool AllowMacros_
Definition: ConfigParser.h:227
char * parse(TokenType &type)
std::unique_ptr< RegexPattern > regex(const char *expectedRegexDescription)
extracts and returns a regex (including any optional flags)
std::string filePath
The file path.
Definition: ConfigParser.h:200
bool getFileLine()
Read the next line from the file.
#define CONFIG_LINE_LIMIT
Definition: ConfigParser.h:33
static TokenType LastTokenType
The type of last parsed element.
Definition: ConfigParser.h:223
static void DisableMacros()
Do not allow macros inside quoted strings.
Definition: ConfigParser.h:147
static std::queue< char * > CfgLineTokens_
Store the list of tokens for current configuration line.
Definition: ConfigParser.h:226
static bool ParseQuotedOrToEol_
The next tokens will be handled as quoted or to_eol token.
Definition: ConfigParser.h:228
static bool ParseKvPair_
The next token will be handled as kv-pair token.
Definition: ConfigParser.h:231
SBuf token(const char *expectedTokenDescription)
extracts and returns a required token
void rejectDuplicateDirective()
rejects configuration due to a repeated directive
void destruct()
Definition: ConfigParser.cc:38
bool optionalKvPair(char *&key, char *&value)
char * nextElement(TokenType &type)
CachePeer & cachePeer(const char *peerNameTokenDescription)
extracts a cache_peer name token and returns the corresponding CachePeer
static char * RegexStrtokFile()
static bool NextKvPair(char *&key, char *&value)
void closeDirective()
stops parsing the current configuration directive
static char * NextToken()
static void ParseWordList(wordlist **list)
Definition: cache_cf.cc:3128
static const char * QuoteString(const String &var)
static char * NextElement(TokenType &type)
Wrapper method for TokenParse.
void Parse()
interprets (and partially applies) squid.conf or equivalent configuration
Definition: cache_cf.cc:610
static const char * CfgLine
The current line to parse.
Definition: ConfigParser.h:224
bool skipOptional(const char *keyword)
either extracts the given (optional) token or returns false
static char * NextQuotedOrToEol()
static void SetCfgLine(char *line)
Set the configuration file line to parse.
static enum ConfigParser::ParsingStates KvPairState_
Parsing state while parsing kv-pair tokens.
Definition: ConfigParser.cc:31
static const char * CfgPos
Pointer to the next element in cfgLine string.
Definition: ConfigParser.h:225
char parseBuffer[CONFIG_LINE_LIMIT]
Temporary buffer to store data to parse.
Definition: ConfigParser.h:197
std::string currentLine
The current line to parse.
Definition: ConfigParser.h:201
static std::stack< CfgFile * > CfgFiles
The stack of open cfg files.
Definition: ConfigParser.h:222
static void ParseUShort(unsigned short *var)
Definition: cache_cf.cc:3100
bool startParse(char *path)
int lineNo
Current line number.
Definition: ConfigParser.h:202
static char * TokenParse(const char *&nextToken, TokenType &type)
bool isOpen()
True if the configuration file is open.
Definition: ConfigParser.h:173

 

Introduction

Documentation

Support

Miscellaneous