diff options
author | Brian Ashworth <bosrsf04@gmail.com> | 2019-06-19 18:21:18 -0400 |
---|---|---|
committer | Simon Ser <contact@emersion.fr> | 2019-06-20 10:13:58 +0300 |
commit | c346c020bf93d455dab917dd27d86afc78273dd2 (patch) | |
tree | d041867620d3f08299ca3707f05dcc084e39202f /sway/commands.c | |
parent | config/xwayland: retain xwayland status on reload (diff) | |
download | sway-c346c020bf93d455dab917dd27d86afc78273dd2.tar.gz sway-c346c020bf93d455dab917dd27d86afc78273dd2.tar.zst sway-c346c020bf93d455dab917dd27d86afc78273dd2.zip |
config: fix find_handler logic
Without this change, the handlers listed in the config_handlers or
command_handlers arrays (depending on reading or active) in commands.c
would be valid subcommands. To make matters worse, they would also take
precedence over the defined subcommand handlers.
This corrects find_handler to only work on the handler array given
instead of implicitly trying others.
Diffstat (limited to 'sway/commands.c')
-rw-r--r-- | sway/commands.c | 60 |
1 files changed, 25 insertions, 35 deletions
diff --git a/sway/commands.c b/sway/commands.c index a670f813..b841ef09 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -143,44 +143,34 @@ static int handler_compare(const void *_a, const void *_b) { | |||
143 | return strcasecmp(a->command, b->command); | 143 | return strcasecmp(a->command, b->command); |
144 | } | 144 | } |
145 | 145 | ||
146 | struct cmd_handler *find_handler(char *line, struct cmd_handler *cmd_handlers, | 146 | struct cmd_handler *find_handler(char *line, struct cmd_handler *handlers, |
147 | int handlers_size) { | 147 | size_t handlers_size) { |
148 | struct cmd_handler d = { .command=line }; | 148 | if (!handlers || !handlers_size) { |
149 | struct cmd_handler *res = NULL; | 149 | return NULL; |
150 | sway_log(SWAY_DEBUG, "find_handler(%s)", line); | ||
151 | |||
152 | bool config_loading = config->reading || !config->active; | ||
153 | |||
154 | if (!config_loading) { | ||
155 | res = bsearch(&d, command_handlers, | ||
156 | sizeof(command_handlers) / sizeof(struct cmd_handler), | ||
157 | sizeof(struct cmd_handler), handler_compare); | ||
158 | |||
159 | if (res) { | ||
160 | return res; | ||
161 | } | ||
162 | } | 150 | } |
151 | struct cmd_handler query = { .command = line }; | ||
152 | return bsearch(&query, handlers, | ||
153 | handlers_size / sizeof(struct cmd_handler), | ||
154 | sizeof(struct cmd_handler), handler_compare); | ||
155 | } | ||
163 | 156 | ||
157 | static struct cmd_handler *find_handler_ex(char *line, | ||
158 | struct cmd_handler *config_handlers, size_t config_handlers_size, | ||
159 | struct cmd_handler *command_handlers, size_t command_handlers_size, | ||
160 | struct cmd_handler *handlers, size_t handlers_size) { | ||
161 | struct cmd_handler *handler = NULL; | ||
164 | if (config->reading) { | 162 | if (config->reading) { |
165 | res = bsearch(&d, config_handlers, | 163 | handler = find_handler(line, config_handlers, config_handlers_size); |
166 | sizeof(config_handlers) / sizeof(struct cmd_handler), | 164 | } else if (config->active) { |
167 | sizeof(struct cmd_handler), handler_compare); | 165 | handler = find_handler(line, command_handlers, command_handlers_size); |
168 | |||
169 | if (res) { | ||
170 | return res; | ||
171 | } | ||
172 | } | 166 | } |
167 | return handler ? handler : find_handler(line, handlers, handlers_size); | ||
168 | } | ||
173 | 169 | ||
174 | if (!cmd_handlers) { | 170 | static struct cmd_handler *find_core_handler(char *line) { |
175 | cmd_handlers = handlers; | 171 | return find_handler_ex(line, config_handlers, sizeof(config_handlers), |
176 | handlers_size = sizeof(handlers); | 172 | command_handlers, sizeof(command_handlers), |
177 | } | 173 | handlers, sizeof(handlers)); |
178 | |||
179 | res = bsearch(&d, cmd_handlers, | ||
180 | handlers_size / sizeof(struct cmd_handler), | ||
181 | sizeof(struct cmd_handler), handler_compare); | ||
182 | |||
183 | return res; | ||
184 | } | 174 | } |
185 | 175 | ||
186 | static void set_config_node(struct sway_node *node) { | 176 | static void set_config_node(struct sway_node *node) { |
@@ -270,7 +260,7 @@ list_t *execute_command(char *_exec, struct sway_seat *seat, | |||
270 | } | 260 | } |
271 | } | 261 | } |
272 | } | 262 | } |
273 | struct cmd_handler *handler = find_handler(argv[0], NULL, 0); | 263 | struct cmd_handler *handler = find_core_handler(argv[0]); |
274 | if (!handler) { | 264 | if (!handler) { |
275 | list_add(res_list, cmd_results_new(CMD_INVALID, | 265 | list_add(res_list, cmd_results_new(CMD_INVALID, |
276 | "Unknown/invalid command '%s'", argv[0])); | 266 | "Unknown/invalid command '%s'", argv[0])); |
@@ -360,7 +350,7 @@ struct cmd_results *config_command(char *exec, char **new_block) { | |||
360 | 350 | ||
361 | // Determine the command handler | 351 | // Determine the command handler |
362 | sway_log(SWAY_INFO, "Config command: %s", exec); | 352 | sway_log(SWAY_INFO, "Config command: %s", exec); |
363 | struct cmd_handler *handler = find_handler(argv[0], NULL, 0); | 353 | struct cmd_handler *handler = find_core_handler(argv[0]); |
364 | if (!handler || !handler->handle) { | 354 | if (!handler || !handler->handle) { |
365 | const char *error = handler | 355 | const char *error = handler |
366 | ? "Command '%s' is shimmed, but unimplemented" | 356 | ? "Command '%s' is shimmed, but unimplemented" |