diff options
-rw-r--r-- | common/stringop.c | 19 | ||||
-rw-r--r-- | include/stringop.h | 4 | ||||
-rw-r--r-- | sway/commands/input/xkb_file.c | 22 | ||||
-rw-r--r-- | sway/commands/output/background.c | 16 |
4 files changed, 47 insertions, 14 deletions
diff --git a/common/stringop.c b/common/stringop.c index ac7df296..0df2b33d 100644 --- a/common/stringop.c +++ b/common/stringop.c | |||
@@ -5,6 +5,7 @@ | |||
5 | #include <stdlib.h> | 5 | #include <stdlib.h> |
6 | #include <string.h> | 6 | #include <string.h> |
7 | #include <strings.h> | 7 | #include <strings.h> |
8 | #include <wordexp.h> | ||
8 | #include "list.h" | 9 | #include "list.h" |
9 | #include "log.h" | 10 | #include "log.h" |
10 | #include "stringop.h" | 11 | #include "stringop.h" |
@@ -309,3 +310,21 @@ char *argsep(char **stringp, const char *delim, char *matched) { | |||
309 | } | 310 | } |
310 | return start; | 311 | return start; |
311 | } | 312 | } |
313 | |||
314 | bool expand_path(char **path) { | ||
315 | wordexp_t p = {0}; | ||
316 | while (strstr(*path, " ")) { | ||
317 | *path = realloc(*path, strlen(*path) + 2); | ||
318 | char *ptr = strstr(*path, " ") + 1; | ||
319 | memmove(ptr + 1, ptr, strlen(ptr) + 1); | ||
320 | *ptr = '\\'; | ||
321 | } | ||
322 | if (wordexp(*path, &p, 0) != 0 || p.we_wordv[0] == NULL) { | ||
323 | wordfree(&p); | ||
324 | return false; | ||
325 | } | ||
326 | free(*path); | ||
327 | *path = join_args(p.we_wordv, p.we_wordc); | ||
328 | wordfree(&p); | ||
329 | return true; | ||
330 | } | ||
diff --git a/include/stringop.h b/include/stringop.h index 2aabcee7..e3f4f0f7 100644 --- a/include/stringop.h +++ b/include/stringop.h | |||
@@ -1,6 +1,7 @@ | |||
1 | #ifndef _SWAY_STRINGOP_H | 1 | #ifndef _SWAY_STRINGOP_H |
2 | #define _SWAY_STRINGOP_H | 2 | #define _SWAY_STRINGOP_H |
3 | 3 | ||
4 | #include <stdbool.h> | ||
4 | #include "list.h" | 5 | #include "list.h" |
5 | 6 | ||
6 | void strip_whitespace(char *str); | 7 | void strip_whitespace(char *str); |
@@ -26,4 +27,7 @@ char *join_args(char **argv, int argc); | |||
26 | // Split string into 2 by delim, handle quotes | 27 | // Split string into 2 by delim, handle quotes |
27 | char *argsep(char **stringp, const char *delim, char *matched_delim); | 28 | char *argsep(char **stringp, const char *delim, char *matched_delim); |
28 | 29 | ||
30 | // Expand a path using shell replacements such as $HOME and ~ | ||
31 | bool expand_path(char **path); | ||
32 | |||
29 | #endif | 33 | #endif |
diff --git a/sway/commands/input/xkb_file.c b/sway/commands/input/xkb_file.c index ef59bffc..493f94fb 100644 --- a/sway/commands/input/xkb_file.c +++ b/sway/commands/input/xkb_file.c | |||
@@ -1,7 +1,10 @@ | |||
1 | #define _POSIX_C_SOURCE 200809L | 1 | #define _POSIX_C_SOURCE 200809L |
2 | #include <unistd.h> | ||
3 | #include <errno.h> | ||
2 | #include "sway/config.h" | 4 | #include "sway/config.h" |
3 | #include "sway/commands.h" | 5 | #include "sway/commands.h" |
4 | #include "log.h" | 6 | #include "log.h" |
7 | #include "stringop.h" | ||
5 | 8 | ||
6 | struct cmd_results *input_cmd_xkb_file(int argc, char **argv) { | 9 | struct cmd_results *input_cmd_xkb_file(int argc, char **argv) { |
7 | struct cmd_results *error = NULL; | 10 | struct cmd_results *error = NULL; |
@@ -18,6 +21,25 @@ struct cmd_results *input_cmd_xkb_file(int argc, char **argv) { | |||
18 | ic->xkb_file = NULL; | 21 | ic->xkb_file = NULL; |
19 | } else { | 22 | } else { |
20 | ic->xkb_file = strdup(argv[0]); | 23 | ic->xkb_file = strdup(argv[0]); |
24 | if (!expand_path(&ic->xkb_file)) { | ||
25 | error = cmd_results_new(CMD_INVALID, "Invalid syntax (%s)", | ||
26 | ic->xkb_file); | ||
27 | free(ic->xkb_file); | ||
28 | ic->xkb_file = NULL; | ||
29 | return error; | ||
30 | } | ||
31 | if (!ic->xkb_file) { | ||
32 | sway_log(SWAY_ERROR, "Failed to allocate expanded path"); | ||
33 | return cmd_results_new(CMD_FAILURE, "Unable to allocate resource"); | ||
34 | } | ||
35 | |||
36 | bool can_access = access(ic->xkb_file, F_OK) != -1; | ||
37 | if (!can_access) { | ||
38 | sway_log_errno(SWAY_ERROR, "Unable to access xkb file '%s'", | ||
39 | ic->xkb_file); | ||
40 | config_add_swaynag_warning("Unable to access xkb file '%s'", | ||
41 | ic->xkb_file); | ||
42 | } | ||
21 | } | 43 | } |
22 | ic->xkb_file_is_set = true; | 44 | ic->xkb_file_is_set = true; |
23 | 45 | ||
diff --git a/sway/commands/output/background.c b/sway/commands/output/background.c index 054fb707..68ee9fe1 100644 --- a/sway/commands/output/background.c +++ b/sway/commands/output/background.c | |||
@@ -4,7 +4,6 @@ | |||
4 | #include <string.h> | 4 | #include <string.h> |
5 | #include <strings.h> | 5 | #include <strings.h> |
6 | #include <unistd.h> | 6 | #include <unistd.h> |
7 | #include <wordexp.h> | ||
8 | #include <errno.h> | 7 | #include <errno.h> |
9 | #include "sway/commands.h" | 8 | #include "sway/commands.h" |
10 | #include "sway/config.h" | 9 | #include "sway/config.h" |
@@ -79,26 +78,15 @@ struct cmd_results *output_cmd_background(int argc, char **argv) { | |||
79 | return cmd_results_new(CMD_INVALID, "Missing background file"); | 78 | return cmd_results_new(CMD_INVALID, "Missing background file"); |
80 | } | 79 | } |
81 | 80 | ||
82 | wordexp_t p = {0}; | ||
83 | char *src = join_args(argv, j); | 81 | char *src = join_args(argv, j); |
84 | while (strstr(src, " ")) { | 82 | if (!expand_path(&src)) { |
85 | src = realloc(src, strlen(src) + 2); | ||
86 | char *ptr = strstr(src, " ") + 1; | ||
87 | memmove(ptr + 1, ptr, strlen(ptr) + 1); | ||
88 | *ptr = '\\'; | ||
89 | } | ||
90 | if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) { | ||
91 | struct cmd_results *cmd_res = cmd_results_new(CMD_INVALID, | 83 | struct cmd_results *cmd_res = cmd_results_new(CMD_INVALID, |
92 | "Invalid syntax (%s)", src); | 84 | "Invalid syntax (%s)", src); |
93 | free(src); | 85 | free(src); |
94 | wordfree(&p); | ||
95 | return cmd_res; | 86 | return cmd_res; |
96 | } | 87 | } |
97 | free(src); | ||
98 | src = join_args(p.we_wordv, p.we_wordc); | ||
99 | wordfree(&p); | ||
100 | if (!src) { | 88 | if (!src) { |
101 | sway_log(SWAY_ERROR, "Failed to duplicate string"); | 89 | sway_log(SWAY_ERROR, "Failed to allocate expanded path"); |
102 | return cmd_results_new(CMD_FAILURE, "Unable to allocate resource"); | 90 | return cmd_results_new(CMD_FAILURE, "Unable to allocate resource"); |
103 | } | 91 | } |
104 | 92 | ||