From dbdc5a60fc35481e77fdda842fc7a2919b0fa6d5 Mon Sep 17 00:00:00 2001 From: koekeishiya Date: Fri, 22 May 2020 14:13:29 +0200 Subject: [PATCH] #533 translate 0x0a and 0x0d when outputting window titles through the query system --- CHANGELOG.md | 1 + src/misc/helpers.h | 29 ++++++++++++++++++++++------- src/window.c | 2 +- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 651e651c..8fbca746 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] - Properly clear focus-follows-mouse cache upon space change [#528](https://github.com/koekeishiya/yabai/issues/528) - Revised process type restrictions and observation requirements to correctly track some applications that don't identify correctly [#529](https://github.com/koekeishiya/yabai/issues/529) +- Translate newline (0x0a) and carriage return (0x0d) when outputting window titles through the query system [#533](https://github.com/koekeishiya/yabai/issues/533) ## [3.0.1] - 2020-05-09 ### Changed diff --git a/src/misc/helpers.h b/src/misc/helpers.h index 58c3b293..5bde92ff 100644 --- a/src/misc/helpers.h +++ b/src/misc/helpers.h @@ -43,27 +43,42 @@ static inline bool string_equals(const char *a, const char *b) return a && b && strcmp(a, b) == 0; } -static inline char *string_escape_quote(char *s) +static inline char *string_escape(char *s) { if (!s) return NULL; char *cursor = s; - int num_quotes = 0; + int num_replacements = 0; while (*cursor) { - if (*cursor == '"') ++num_quotes; + if ((*cursor == '"') || + (*cursor == '\n') || + (*cursor == '\r')) { + ++num_replacements; + } + ++cursor; } - if (!num_quotes) return NULL; + if (!num_replacements) return NULL; - int size_in_bytes = (int)(cursor - s) + num_quotes; + int size_in_bytes = (int)(cursor - s) + num_replacements; char *result = malloc(sizeof(char) * (size_in_bytes+1)); result[size_in_bytes] = '\0'; for (char *dst = result, *cursor = s; *cursor; ++cursor) { - if (*cursor == '"') *dst++ = '\\'; - *dst++ = *cursor; + if (*cursor == '"') { + *dst++ = '\\'; + *dst++ = *cursor; + } else if (*cursor == '\n') { + *dst++ = '\\'; + *dst++ = 'n'; + } else if (*cursor == '\r') { + *dst++ = '\\'; + *dst++ = 'r'; + } else { + *dst++ = *cursor; + } } return result; diff --git a/src/window.c b/src/window.c index fafcd26d..e5ee3642 100644 --- a/src/window.c +++ b/src/window.c @@ -106,7 +106,7 @@ uint64_t *window_space_list(struct window *window, int *count) void window_serialize(FILE *rsp, struct window *window) { char *title = window_title(window); - char *escaped_title = string_escape_quote(title); + char *escaped_title = string_escape(title); CGRect frame = window_frame(window); char *role = NULL; char *subrole = NULL;