Handle shell special characters in bg file path

This changes it back so the path given to swaybg is enclosed in quotes.

Additionally, the only character that is escaped in the path stored is
double quotes now. This makes it so we don't need to keep an exhaustive
list of characters that need to be escaped.

The end user will still need to escape these characters in their config
or when passed to swaybg.
This commit is contained in:
Brian Ashworth 2018-09-15 22:21:12 -04:00
parent dc01e884f7
commit af45ee2d8b
2 changed files with 8 additions and 14 deletions

View file

@ -123,19 +123,13 @@ struct cmd_results *output_cmd_background(int argc, char **argv) {
}
free(src);
} else {
// Escape spaces and quotes in the final path for swaybg
// Escape double 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;
if (src[i] == '"') {
src = realloc(src, strlen(src) + 2);
memmove(src + i + 1, src + i, strlen(src + i) + 1);
*(src + i) = '\\';
i++;
}
}

View file

@ -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 : "");