Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade generated parse_line() to accept constant SBuf #1840

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/ConfigParser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
#include "fatal.h"
#include "globals.h"
#include "neighbors.h"
#include "parser/Tokenizer.h"
#include "sbuf/Stream.h"

bool ConfigParser::RecognizeQuotedValues = true;
bool ConfigParser::StrictMode = true;
std::stack<ConfigParser::CfgFile *> ConfigParser::CfgFiles;
ConfigParser::TokenType ConfigParser::LastTokenType = ConfigParser::SimpleToken;
const char *ConfigParser::CfgLine = nullptr;
SBuf ConfigParser::CfgLine;
const char *ConfigParser::CfgPos = nullptr;
std::queue<char *> ConfigParser::CfgLineTokens_;
bool ConfigParser::AllowMacros_ = false;
Expand Down Expand Up @@ -201,10 +202,10 @@ ConfigParser::UnQuote(const char *token, const char **next)
}

void
ConfigParser::SetCfgLine(char *line)
ConfigParser::SetCfgLine(const SBuf &line)
{
CfgLine = line;
CfgPos = line;
CfgPos = CfgLine.c_str();
while (!CfgLineTokens_.empty()) {
char *token = CfgLineTokens_.front();
CfgLineTokens_.pop();
Expand Down Expand Up @@ -561,6 +562,20 @@ ConfigParser::rejectDuplicateDirective()
throw TextException("duplicate configuration directive", Here());
}

SBuf
ConfigParser::openDirective(const SBuf &line)
{
Parser::Tokenizer tk(line);
SBuf directiveName; // TODO: Upgrade cfg_directive to a ConfigParser member (with SBuf type) and set it here.
static const auto spaceChars = CharacterSet("w_space", w_space);
static const auto directiveChars = spaceChars.complement("squid.conf directive name");
const auto found = tk.prefix(directiveName, directiveChars);
yadij marked this conversation as resolved.
Show resolved Hide resolved
Assure(found); // our callers are expected to fully handle non-directive lines
tk.skipAll(spaceChars);
SetCfgLine(tk.remaining()); // may be empty
return directiveName;
}

void
ConfigParser::closeDirective()
{
Expand Down
17 changes: 13 additions & 4 deletions src/ConfigParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class ConfigParser

void destruct();

/// starts parsing the current configuration directive
/// \returns directive name
/// \sa closeDirective()
SBuf openDirective(const SBuf &line);

/// stops parsing the current configuration directive
void closeDirective();

Expand Down Expand Up @@ -137,8 +142,8 @@ class ConfigParser
*/
static char *PeekAtToken();

/// Set the configuration file line to parse.
static void SetCfgLine(char *line);
/// Set current directive parameters (i.e. characters after the directive name).
static void SetCfgLine(const SBuf &);

/// Allow %macros inside quoted strings
static void EnableMacros() {AllowMacros_ = true;}
Expand Down Expand Up @@ -221,8 +226,12 @@ class ConfigParser
static char *NextElement(TokenType &type);
static std::stack<CfgFile *> CfgFiles; ///< The stack of open cfg files
static TokenType LastTokenType; ///< The type of last parsed element
static const char *CfgLine; ///< The current line to parse
static const char *CfgPos; ///< Pointer to the next element in cfgLine string

static SBuf CfgLine; ///< Directive parameters being parsed; \sa SetCfgLine()

// TODO: Replace with a Tokenizer or a similar iterative parsing class
static const char *CfgPos; ///< Pointer to the next element in CfgLine string

static std::queue<char *> CfgLineTokens_; ///< Store the list of tokens for current configuration line
static bool AllowMacros_;
static bool ParseQuotedOrToEol_; ///< The next tokens will be handled as quoted or to_eol token
Expand Down
3 changes: 2 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -2719,7 +2719,6 @@ tests_testConfigParser_SOURCES = \
tests/testConfigParser.cc
nodist_tests_testConfigParser_SOURCES = \
ConfigParser.cc \
tests/stub_SBuf.cc \
String.cc \
tests/stub_acl.cc \
tests/stub_cache_cf.cc \
Expand All @@ -2728,6 +2727,8 @@ nodist_tests_testConfigParser_SOURCES = \
tests/stub_libmem.cc \
tests/stub_neighbors.cc
tests_testConfigParser_LDADD = \
parser/libparser.la \
sbuf/libsbuf.la \
base/libbase.la \
$(LIBCPPUNIT_LIBS) \
$(COMPAT_LIB) \
Expand Down
19 changes: 18 additions & 1 deletion src/base/CharacterSet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#include "base/CharacterSet.h"

#include <algorithm>
#include <iostream>
#include <functional>
#include <iostream>
#include <limits>

CharacterSet &
CharacterSet::operator +=(const CharacterSet &src)
Expand Down Expand Up @@ -168,3 +169,19 @@ CharacterSet::RFC3986_UNRESERVED()
return *chars;
}

const CharacterSet &
CharacterSet::libcSpace()
{
const auto creator = []() -> auto {
const auto chars = new CharacterSet("libcSpace");
using charLimits = std::numeric_limits<CharacterSet::Storage::value_type>;
for (size_t idx = charLimits::min(); idx <= charLimits::max(); ++idx) {
if (xisspace(idx))
yadij marked this conversation as resolved.
Show resolved Hide resolved
chars->add(idx);
}
return chars;
};
static const auto chars = creator();
return *chars;
}

3 changes: 3 additions & 0 deletions src/base/CharacterSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ class CharacterSet
/// allowed URI characters that do not have a reserved purpose, RFC 3986
static const CharacterSet &RFC3986_UNRESERVED();

/// characters that libc isspace(3) matches, including any locale-specific ones
static const CharacterSet &libcSpace();

private:
/** index of characters in this set
*
Expand Down
Loading
Loading