From a7b3ff1ea8c264dbd31527018931c34346046c0a Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 23 Feb 2017 22:55:58 -0500 Subject: [PATCH] Handle "\n" -> newline expansion in the option_parser. It makes more sense to handle escapes in configuration strings when we parse them, rather than after-the-fact when we handle notifications that use the relevant config options. Do this in the proper backslash handler; rather than a naive string replace which doesn't allow the backslash to be escaped (`\\n` => 0x5C0x0A rather than 0x5C0x6E). it to the proper backslash handler --- src/notification.c | 2 +- src/option_parser.c | 3 +++ test/data/test-ini | 2 ++ test/option_parser.c | 4 ++++ 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/notification.c b/src/notification.c index 141fdfe..d20180c 100644 --- a/src/notification.c +++ b/src/notification.c @@ -368,7 +368,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, diff --git a/src/option_parser.c b/src/option_parser.c index 91de826..dbd361d 100644 --- a/src/option_parser.c +++ b/src/option_parser.c @@ -203,6 +203,9 @@ char *clean_value(char *value, int line_num) case '\\': case '"': break; + case 'n': + *unparsed = '\n'; + break; default: fprintf(stderr, "Warning: invalid config file at line %d\n", diff --git a/test/data/test-ini b/test/data/test-ini index 4326b0a..e381d51 100644 --- a/test/data/test-ini +++ b/test/data/test-ini @@ -22,9 +22,11 @@ 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_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 diff --git a/test/option_parser.c b/test/option_parser.c index 173f16e..0a018ff 100644 --- a/test/option_parser.c +++ b/test/option_parser.c @@ -52,6 +52,8 @@ TEST test_ini_get_string(void) 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); @@ -59,6 +61,8 @@ TEST test_ini_get_string(void) 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", "")));