From 0b64cce73385cb739ee89c983835b098f04ee68a Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Thu, 13 Sep 2018 16:36:09 -0400 Subject: [PATCH 1/3] Allow spaces in background file paths --- sway/commands/output/background.c | 2 +- sway/config/output.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sway/commands/output/background.c b/sway/commands/output/background.c index ddad679df..8ab52a2e8 100644 --- a/sway/commands/output/background.c +++ b/sway/commands/output/background.c @@ -71,7 +71,7 @@ struct cmd_results *output_cmd_background(int argc, char **argv) { return cmd_res; } free(src); - src = strdup(p.we_wordv[0]); + src = join_args(p.we_wordv, p.we_wordc); wordfree(&p); if (!src) { wlr_log(WLR_ERROR, "Failed to duplicate string"); diff --git a/sway/config/output.c b/sway/config/output.c index 74d79130a..6f337b66b 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -237,7 +237,7 @@ void apply_output_config(struct output_config *oc, struct sway_output *output) { wlr_log(WLR_DEBUG, "Setting background for output %d to %s", output_i, oc->background); - size_t len = snprintf(NULL, 0, "%s %d %s %s %s", + size_t len = snprintf(NULL, 0, "%s %d \"%s\" %s %s", config->swaybg_command ? config->swaybg_command : "swaybg", output_i, oc->background, oc->background_option, oc->background_fallback ? oc->background_fallback : ""); @@ -246,7 +246,7 @@ void apply_output_config(struct output_config *oc, struct sway_output *output) { wlr_log(WLR_DEBUG, "Unable to allocate swaybg command"); return; } - snprintf(command, len + 1, "%s %d %s %s %s", + snprintf(command, len + 1, "%s %d \"%s\" %s %s", config->swaybg_command ? config->swaybg_command : "swaybg", output_i, oc->background, oc->background_option, oc->background_fallback ? oc->background_fallback : ""); From 1cf737489db68312556d0a215c17e93a573b283f Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Thu, 13 Sep 2018 21:55:46 -0400 Subject: [PATCH 2/3] Escape spaces in background file path --- sway/commands/output/background.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sway/commands/output/background.c b/sway/commands/output/background.c index 8ab52a2e8..2ff5f0d7c 100644 --- a/sway/commands/output/background.c +++ b/sway/commands/output/background.c @@ -63,6 +63,12 @@ struct cmd_results *output_cmd_background(int argc, char **argv) { wordexp_t p; char *src = join_args(argv, j); + while (strstr(src, " ")) { + src = realloc(src, strlen(src) + 2); + char *ptr = strstr(src, " ") + 1; + memmove(ptr + 1, ptr, strlen(ptr) + 1); + *ptr = '\\'; + } if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) { struct cmd_results *cmd_res = cmd_results_new(CMD_INVALID, "output", "Invalid syntax (%s)", src); From 17fe8924f24a9445447dbb3d5a1f775c94203048 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Fri, 14 Sep 2018 08:51:01 -0400 Subject: [PATCH 3/3] Address ianyfan's comments wordexp p is now initialized to {0} to prevent a segfault on wordfree in the failure case. File paths with single quotes and double quotes are now supported. The quote can either be wrapped in the other quote or escaped with three backslashes. Additionally to make passing file paths with double quotes to swaybg easier, instead of enclosing the path given to swaybg in quotes, all spaces, single quotes, and double quotes in the resulting path are now escaped with a single backslash. --- sway/commands/output/background.c | 18 +++++++++++++++++- sway/config/output.c | 4 ++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/sway/commands/output/background.c b/sway/commands/output/background.c index 2ff5f0d7c..9e370d439 100644 --- a/sway/commands/output/background.c +++ b/sway/commands/output/background.c @@ -61,7 +61,7 @@ struct cmd_results *output_cmd_background(int argc, char **argv) { "Missing background scaling mode."); } - wordexp_t p; + wordexp_t p = {0}; char *src = join_args(argv, j); while (strstr(src, " ")) { src = realloc(src, strlen(src) + 2); @@ -123,6 +123,22 @@ struct cmd_results *output_cmd_background(int argc, char **argv) { } free(src); } else { + // Escape spaces and quotes in the final path for swaybg + for (size_t i = 0; i < strlen(src); i++) { + switch (src[i]) { + case ' ': + case '\'': + case '\"': + src = realloc(src, strlen(src) + 2); + memmove(src + i + 1, src + i, strlen(src + i) + 1); + *(src + i) = '\\'; + i++; + break; + default: + break; + } + } + output->background = src; output->background_option = strdup(mode); } diff --git a/sway/config/output.c b/sway/config/output.c index 6f337b66b..74d79130a 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -237,7 +237,7 @@ void apply_output_config(struct output_config *oc, struct sway_output *output) { wlr_log(WLR_DEBUG, "Setting background for output %d to %s", output_i, oc->background); - size_t len = snprintf(NULL, 0, "%s %d \"%s\" %s %s", + size_t len = snprintf(NULL, 0, "%s %d %s %s %s", config->swaybg_command ? config->swaybg_command : "swaybg", output_i, oc->background, oc->background_option, oc->background_fallback ? oc->background_fallback : ""); @@ -246,7 +246,7 @@ void apply_output_config(struct output_config *oc, struct sway_output *output) { wlr_log(WLR_DEBUG, "Unable to allocate swaybg command"); return; } - snprintf(command, len + 1, "%s %d \"%s\" %s %s", + snprintf(command, len + 1, "%s %d %s %s %s", config->swaybg_command ? config->swaybg_command : "swaybg", output_i, oc->background, oc->background_option, oc->background_fallback ? oc->background_fallback : "");