Merge pull request #298 from LukeShu/fix-option-parser
option_parser: Allow comments on lines with quoted strings
This commit is contained in:
commit
3330c988fc
@ -304,7 +304,7 @@ int notification_init(notification * n, int id)
|
||||
|
||||
n->urls = notification_extract_markup_urls(&(n->body));
|
||||
|
||||
n->msg = string_replace_all("\\n", "\n", g_strdup(n->format));
|
||||
n->msg = g_strdup(n->format);
|
||||
n->msg = notification_replace_format("%a", n->appname, n->msg,
|
||||
MARKUP_NO);
|
||||
n->msg = notification_replace_format("%s", n->summary, n->msg,
|
||||
|
@ -29,7 +29,7 @@ static section_t *new_section(char *name);
|
||||
static section_t *get_section(char *name);
|
||||
static void add_entry(char *section_name, char *key, char *value);
|
||||
static char *get_value(char *section, char *key);
|
||||
static char *clean_value(char *value);
|
||||
static char *clean_value(char *value, int line_num);
|
||||
|
||||
static int cmdline_argc;
|
||||
static char **cmdline_argv;
|
||||
@ -91,7 +91,7 @@ void add_entry(char *section_name, char *key, char *value)
|
||||
int len = s->entry_count;
|
||||
s->entries = g_realloc(s->entries, sizeof(entry_t) * len);
|
||||
s->entries[s->entry_count - 1].key = g_strdup(key);
|
||||
s->entries[s->entry_count - 1].value = clean_value(value);
|
||||
s->entries[s->entry_count - 1].value = g_strdup(value);
|
||||
}
|
||||
|
||||
char *get_value(char *section, char *key)
|
||||
@ -186,20 +186,55 @@ int ini_get_bool(char *section, char *key, int def)
|
||||
}
|
||||
}
|
||||
|
||||
char *clean_value(char *value)
|
||||
char *clean_value(char *value, int line_num)
|
||||
{
|
||||
char *s;
|
||||
char *unparsed = value;
|
||||
|
||||
if (value[0] == '"')
|
||||
s = g_strdup(value + 1);
|
||||
bool in_quote = false;
|
||||
while ((unparsed = strpbrk(unparsed, "\"\\#;")) != NULL) {
|
||||
switch (*unparsed) {
|
||||
case '"':
|
||||
memmove(unparsed, unparsed + 1, strlen(unparsed));
|
||||
in_quote = !in_quote;
|
||||
break;
|
||||
case '\\':
|
||||
memmove(unparsed, unparsed + 1, strlen(unparsed));
|
||||
switch (*unparsed) {
|
||||
case '\\':
|
||||
case '"':
|
||||
break;
|
||||
case 'n':
|
||||
*unparsed = '\n';
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr,
|
||||
"Warning: invalid config file at line %d\n",
|
||||
line_num);
|
||||
fprintf(stderr,
|
||||
"Unrecognized backslash sequence '\\%c'\n",
|
||||
*unparsed);
|
||||
return NULL;
|
||||
}
|
||||
unparsed++;
|
||||
break;
|
||||
case '#':
|
||||
case ';':
|
||||
if (in_quote)
|
||||
unparsed++;
|
||||
else
|
||||
s = g_strdup(value);
|
||||
|
||||
if (s[strlen(s) - 1] == '"')
|
||||
s[strlen(s) - 1] = '\0';
|
||||
|
||||
return s;
|
||||
*unparsed = '\0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (in_quote) {
|
||||
fprintf(stderr,
|
||||
"Warning: invalid config file at line %d\n",
|
||||
line_num);
|
||||
fprintf(stderr, "Missing '\"'\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return g_strstrip(value);
|
||||
}
|
||||
|
||||
int load_ini_file(FILE * fp)
|
||||
@ -250,24 +285,8 @@ int load_ini_file(FILE * fp)
|
||||
|
||||
*equal = '\0';
|
||||
char *key = g_strstrip(start);
|
||||
char *value = g_strstrip(equal + 1);
|
||||
|
||||
char *quote = strchr(value, '"');
|
||||
if (quote) {
|
||||
char *closing_quote = strchr(quote + 1, '"');
|
||||
if (!closing_quote) {
|
||||
fprintf(stderr,
|
||||
"Warning: invalid config file at line %d\n",
|
||||
line_num);
|
||||
fprintf(stderr, "Missing '\"'\n");
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
char *comment = strpbrk(value, "#;");
|
||||
if (comment)
|
||||
*comment = '\0';
|
||||
}
|
||||
value = g_strstrip(value);
|
||||
char *value = clean_value(equal + 1, line_num);
|
||||
if (!value) continue;
|
||||
|
||||
if (!current_section) {
|
||||
fprintf(stderr,
|
||||
|
@ -20,8 +20,18 @@
|
||||
|
||||
[string]
|
||||
simple = A simple string
|
||||
simple_with_hcomment = A simple string # a comment
|
||||
simple_with_scomment = A simple string ; a comment
|
||||
simple_with_nl = A simple string\nwith newline
|
||||
quoted = "A quoted string"
|
||||
quoted_with_quotes = "A string "with quotes""
|
||||
quoted_with_hcomment = "A quoted string" # a comment
|
||||
quoted_with_scomment = "A quoted string" ; a comment
|
||||
quoted_with_nl = "A quoted string\nwith newline"
|
||||
quoted_with_quotes = "A string \"with quotes\""
|
||||
quoted_with_escapes = "A string \\\"with escapes\\"
|
||||
quoted_with_cchar = "A string; with #comment characters" # a comment
|
||||
quoted_in_middle = A string"; with #comment" characters # a comment
|
||||
escaped_quotes = String \"with quotes\"
|
||||
|
||||
[int]
|
||||
simple = 5
|
||||
|
@ -45,13 +45,35 @@ TEST test_ini_get_string(void)
|
||||
{
|
||||
char *string_section = "string";
|
||||
char *ptr;
|
||||
|
||||
ASSERT_STR_EQ("A simple string", (ptr = ini_get_string(string_section, "simple", "")));
|
||||
free(ptr);
|
||||
ASSERT_STR_EQ("A simple string", (ptr = ini_get_string(string_section, "simple_with_hcomment", "")));
|
||||
free(ptr);
|
||||
ASSERT_STR_EQ("A simple string", (ptr = ini_get_string(string_section, "simple_with_scomment", "")));
|
||||
free(ptr);
|
||||
ASSERT_STR_EQ("A simple string\nwith newline", (ptr = ini_get_string(string_section, "simple_with_nl", "")));
|
||||
free(ptr);
|
||||
|
||||
ASSERT_STR_EQ("A quoted string", (ptr = ini_get_string(string_section, "quoted", "")));
|
||||
free(ptr);
|
||||
ASSERT_STR_EQ("A quoted string", (ptr = ini_get_string(string_section, "quoted_with_hcomment", "")));
|
||||
free(ptr);
|
||||
ASSERT_STR_EQ("A quoted string", (ptr = ini_get_string(string_section, "quoted_with_scomment", "")));
|
||||
free(ptr);
|
||||
ASSERT_STR_EQ("A quoted string\nwith newline", (ptr = ini_get_string(string_section, "quoted_with_nl", "")));
|
||||
free(ptr);
|
||||
ASSERT_STR_EQ("A string \"with quotes\"", (ptr = ini_get_string(string_section, "quoted_with_quotes", "")));
|
||||
free(ptr);
|
||||
ASSERT_STR_EQ("A string \\\"with escapes\\", (ptr = ini_get_string(string_section, "quoted_with_escapes", "")));
|
||||
free(ptr);
|
||||
ASSERT_STR_EQ("A string; with #comment characters", (ptr = ini_get_string(string_section, "quoted_with_cchar", "")));
|
||||
free(ptr);
|
||||
ASSERT_STR_EQ("A string; with #comment characters", (ptr = ini_get_string(string_section, "quoted_in_middle", "")));
|
||||
free(ptr);
|
||||
|
||||
ASSERT_STR_EQ("String \"with quotes\"", (ptr = ini_get_string(string_section, "escaped_quotes", "")));
|
||||
free(ptr);
|
||||
|
||||
ASSERT_STR_EQ("default value", (ptr = ini_get_string(string_section, "nonexistent", "default value")));
|
||||
free(ptr);
|
||||
|
Loading…
x
Reference in New Issue
Block a user