diff --git a/notification.c b/notification.c index b0b12a4..f96931f 100644 --- a/notification.c +++ b/notification.c @@ -185,51 +185,20 @@ void notification_free(notification * n) */ char *notification_strip_markup(char *str) { - char *replace_buf, *start, *end; - if (str == NULL) { return NULL; } + /* strip all tags */ + string_strip_delimited(str, '<', '>'); + + /* unquote the remainder */ str = string_replace_all(""", "\"", str); str = string_replace_all("'", "'", str); str = string_replace_all("&", "&", str); str = string_replace_all("<", "<", str); str = string_replace_all(">", ">", str); - /* remove tags */ - str = string_replace_all("", "", str); - str = string_replace_all("", "", str); - str = string_replace_all("
", " ", str); - str = string_replace_all("
", " ", str); - str = string_replace_all("
", " ", str); - str = string_replace_all("", "", str); - str = string_replace_all("", "", str); - str = string_replace_all("", "", str); - str = string_replace_all("", "", str); - str = string_replace_all("", "", str); - - while ((start = strstr(str, ""); - if (end != NULL) { - replace_buf = strndup(start, end - start + 1); - str = string_replace(replace_buf, "", str); - free(replace_buf); - } else { - break; - } - } - - while ((start = strstr(str, ""); - if (end != NULL) { - replace_buf = strndup(start, end - start + 2); - str = string_replace(replace_buf, "", str); - free(replace_buf); - } else { - break; - } - } return str; } @@ -270,19 +239,25 @@ char *notification_replace_format(const char *needle, const char *replacement, tmp = notification_quote_markup(tmp); ret = string_replace_all(needle, tmp, haystack); free(tmp); - } else if (!allow_markup) { + } else { tmp = strdup(replacement); - if (!settings.ignore_newline) { + if (settings.ignore_newline) { + tmp = string_replace_all("
", " ", tmp); + tmp = string_replace_all("
", " ", tmp); + tmp = string_replace_all("
", " ", tmp); + } else { tmp = string_replace_all("
", "\n", tmp); tmp = string_replace_all("
", "\n", tmp); tmp = string_replace_all("
", "\n", tmp); } - tmp = notification_strip_markup(tmp); - tmp = notification_quote_markup(tmp); + + if (!allow_markup) { + tmp = notification_strip_markup(tmp); + tmp = notification_quote_markup(tmp); + } + ret = string_replace_all(needle, tmp, haystack); free(tmp); - } else { - ret = string_replace_all(needle, replacement, haystack); } return ret; @@ -320,7 +295,7 @@ char *notification_extract_markup_urls(char **str_ptr) { } free(replace_buf); } else { - break; + break; } } *str_ptr = str; diff --git a/utils.c b/utils.c index 0ab4b3a..7d07ffe 100644 --- a/utils.c +++ b/utils.c @@ -11,10 +11,10 @@ #include "dunst.h" char *string_replace_char(char needle, char replacement, char *haystack) { - char *current = haystack; - while ((current = strchr (current, needle)) != NULL) - *current++ = replacement; - return haystack; + char *current = haystack; + while ((current = strchr (current, needle)) != NULL) + *current++ = replacement; + return haystack; } char *string_replace_at(char *buf, int pos, int len, const char *repl) @@ -25,13 +25,21 @@ char *string_replace_at(char *buf, int pos, int len, const char *repl) buf_len = strlen(buf); repl_len = strlen(repl); size = (buf_len - len) + repl_len + 1; - tmp = malloc(size); + + if (repl_len <= len) { + tmp = buf; + } else { + tmp = malloc(size); + } memcpy(tmp, buf, pos); memcpy(tmp + pos, repl, repl_len); - memcpy(tmp + pos + repl_len, buf + pos + len, buf_len - (pos + len) + 1); + memmove(tmp + pos + repl_len, buf + pos + len, buf_len - (pos + len) + 1); + + if(tmp != buf) { + free(buf); + } - free(buf); return tmp; } @@ -105,6 +113,21 @@ char **string_to_argv(const char *s) return argv; } +void string_strip_delimited(char *str, char a, char b) +{ + int iread=-1, iwrite=0, copen=0; + while (str[++iread] != 0) { + if (str[iread] == a) { + ++copen; + } else if (str[iread] == b && copen > 0) { + --copen; + } else if (copen == 0) { + str[iwrite++] = str[iread]; + } + } + str[iwrite] = 0; +} + int digit_count(int i) { i = ABS(i); diff --git a/utils.h b/utils.h index 9617301..1009f96 100644 --- a/utils.h +++ b/utils.h @@ -17,6 +17,9 @@ char *string_append(char *a, const char *b, const char *sep); char **string_to_argv(const char *s); +/* strip content between two delimiter characters (inplace) */ +void string_strip_delimited(char *str, char a, char b); + /* exit with an error message */ void die(char *msg, int exit_value);