Added a proper config file tokenizer that supports escaping sequences

and comments.

This allows for:

'"foo bar"' can be represented as 'foo\ bar'.

And allows for comments using the hash symbol (#), but not inside
escapes or quotes. "#this is not a comment", \#this\ is\ not\ a comment.

All configuration file parsers should be rewritten using this functionality.
This commit is contained in:
Jan Vidar Krey 2010-07-09 13:04:20 +02:00
parent 849a791f79
commit 4b22ccb73c
4 changed files with 270 additions and 0 deletions

View File

@ -163,6 +163,7 @@ libadc_common_SOURCES := \
src/adc/sid.c
libutils_SOURCES := \
src/util/config_token.c \
src/util/ipcalc.c \
src/util/list.c \
src/util/log.c \
@ -192,6 +193,7 @@ autotest_SOURCES := \
autotest/test_sid.tcc \
autotest/test_tiger.tcc \
autotest/test_timer.tcc \
autotest/test_tokenizer.tcc \
autotest/test_usermanager.tcc
autotest_OBJECTS = autotest.o

View File

@ -0,0 +1,87 @@
#include <uhub.h>
#define SETUP(X, STR) struct linked_list* tokens = cfg_tokenize(STR)
#define CLEANUP(X) do { list_clear(X, hub_free); list_destroy(X); } while(0)
static int match_str(const char* str1, char* str2)
{
size_t i;
for (i = 0; i < strlen(str2); i++)
if (str2[i] == '_')
str2[i] = ' ';
else if (str2[i] == '|')
str2[i] = '\t';
int ret = strcmp(str1, str2);
if (ret) {
fprintf(stderr, "\n Mismatch: \"%s\" != \"%s\"\n", str1, str2);
}
return ret;
}
static int count(const char* STR, size_t EXPECT) {
SETUP(tokens, STR);
int pass = list_size(tokens) == EXPECT;
CLEANUP(tokens);
return pass;
}
static int compare(const char* str, const char* ref) {
size_t i, max;
struct linked_list* compare = list_create();
SETUP(tokens, str);
split_string(ref, " ", compare, 0);
int pass = list_size(tokens) == list_size(compare);
if (pass) {
max = list_size(tokens);
for (i = 0; i < max; i++) {
char* token = (char*) list_get_index(tokens, i);
char* refer = (char*) list_get_index(compare, i);
if (match_str(token, refer)) {
pass = 0;
break;
}
}
}
CLEANUP(tokens);
CLEANUP(compare);
return pass;
}
EXO_TEST(tokenizer_basic_0, { return count("", 0); });
EXO_TEST(tokenizer_basic_1, { return count("a", 1); });
EXO_TEST(tokenizer_basic_1a, { return count(" a", 1); })
EXO_TEST(tokenizer_basic_1b, { return count(" a", 1); })
EXO_TEST(tokenizer_basic_1c, { return count(" a", 1); })
EXO_TEST(tokenizer_basic_1d, { return count(" a ", 1); })
EXO_TEST(tokenizer_basic_1e, { return count(" a ", 1); })
EXO_TEST(tokenizer_basic_2, { return count("a b", 2); });
EXO_TEST(tokenizer_basic_2a, { return count(" a b ", 2); });
EXO_TEST(tokenizer_basic_3, { return count("a b c", 3); });
EXO_TEST(tokenizer_basic_3a, { return count("a b c", 3); });
EXO_TEST(tokenizer_basic_3b, { return count("a b c", 3); });
EXO_TEST(tokenizer_basic_3c, { return count("a b c ", 3); });
EXO_TEST(tokenizer_basic_3d, { return count("a b c ", 3); });
EXO_TEST(tokenizer_basic_compare_0, { return compare("value1 value2 value3", "value1 value2 value3"); });
EXO_TEST(tokenizer_basic_compare_1, { return compare("a b c", "a b c"); });
EXO_TEST(tokenizer_basic_compare_2, { return compare("a b c", "a b c"); });
EXO_TEST(tokenizer_basic_compare_3, { return compare(" a b c", "a b c"); });
EXO_TEST(tokenizer_basic_compare_4, { return compare(" a b c ", "a b c"); });
EXO_TEST(tokenizer_basic_compare_5, { return compare("a b c ", "a b c"); });
EXO_TEST(tokenizer_basic_compare_6, { return compare("a b c ", "a b c"); });
EXO_TEST(tokenizer_comment_1, { return compare("value1 value2 # value3", "value1 value2"); });
EXO_TEST(tokenizer_comment_2, { return compare("value1 value2\\# value3", "value1 value2# value3"); });
EXO_TEST(tokenizer_comment_3, { return compare("value1 \"value2#\" value3", "value1 value2# value3"); });
EXO_TEST(tokenizer_escape_1, { return compare("\"value1\" value2", "value1 value2"); });
EXO_TEST(tokenizer_escape_2, { return compare("\"value1\\\"\" value2", "value1\" value2"); });
EXO_TEST(tokenizer_escape_3, { return compare("\"value1\" \"value 2\"", "value1 value_2"); });
EXO_TEST(tokenizer_escape_4, { return compare("\"value1\" value\\ 2", "value1 value_2"); });
EXO_TEST(tokenizer_escape_5, { return compare("\"value1\" value\\\\2", "value1 value\\2"); });
EXO_TEST(tokenizer_escape_6, { return compare("\"value1\" value\\\t2", "value1 value|2"); });
EXO_TEST(tokenizer_escape_7, { return compare("\"value1\" \"value\t2\"", "value1 value|2"); });

148
src/util/config_token.c Normal file
View File

@ -0,0 +1,148 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2010, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "uhub.h"
#define ADD_CHAR(X) do { *out = X; out++; token_size++; } while(0)
#define RESET_TOKEN do { ADD_CHAR('\0'); out = buffer; if (add_token(tokens, out)) token_count++; token_size = 0; buffer[0] = '\0'; } while (0)
static int add_token(struct linked_list* list, const char* token)
{
if (*token)
{
list_append(list, hub_strdup(token));
return 1;
}
return 0;
}
struct linked_list* cfg_tokenize(const char* line)
{
struct linked_list* tokens = list_create();
char* buffer = hub_malloc_zero(strlen(line));
char* out = buffer;
const char* p = line;
int backslash = 0;
char quote = 0;
size_t token_count = 0;
size_t token_size = 0;
for (; *p; p++)
{
switch (*p)
{
case '\\':
if (backslash)
{
ADD_CHAR('\\');
backslash = 0;
}
else
{
backslash = 1;
}
break;
case '#':
if (backslash)
{
ADD_CHAR('#');
backslash = 0;
}
else if (quote)
{
ADD_CHAR('#');
}
else
{
RESET_TOKEN;
return tokens;
}
break;
case '\"':
if (backslash)
{
ADD_CHAR('\"');
backslash = 0;
}
else if (quote)
{
quote = 0;
}
else
{
quote = 1;
}
break;
case '\r':
/* Pretend it does not exist! */
break;
case ' ':
case '\t':
if (quote)
{
ADD_CHAR(*p);
}
else if (backslash)
{
ADD_CHAR(*p);
backslash = 0;
}
else
{
RESET_TOKEN;
}
break;
default:
ADD_CHAR(*p);
}
}
RESET_TOKEN;
return tokens;
}
void cfg_tokens_free(struct linked_list* list)
{
list_clear(list, hub_free);
list_destroy(list);
}
/*
size_t cfg_token_count(const char* line)
{
if (!line || !*line)
return 0;
}
char* cfg_token_get(const char* line, size_t token)
{
}
char* cfg_token_add(const char* line, char* new_token)
{
}
*/

33
src/util/config_token.h Normal file
View File

@ -0,0 +1,33 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2010, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef HAVE_UHUB_CONFIG_TOKEN_H
#define HAVE_UHUB_CONFIG_TOKEN_H
struct linked_list;
struct linked_list* cfg_tokenize(const char* line);
void cfg_tokens_free(struct linked_list*);
size_t cfg_token_count(const char* line);
char* cfg_token_get(const char* line, size_t token);
char* cfg_token_add(const char* line, char* new_token);
#endif /* HAVE_UHUB_CONFIG_TOKEN_H */