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 | |
parent | 5069b53d6c561d0f949cb0aec1d70397a2fb8a90 (diff) |
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.
-rw-r--r-- | include/sway/commands.h | 3 | ||||
-rw-r--r-- | sway/commands.c | 60 |
2 files changed, 27 insertions, 36 deletions
diff --git a/include/sway/commands.h b/include/sway/commands.h index e0cd94d1..641f2504 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -47,7 +47,8 @@ struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val); struct cmd_handler *find_handler(char *line, struct cmd_handler *cmd_handlers, - int handlers_size); + size_t handlers_size); + /** * Parse and executes a command. * 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) { return strcasecmp(a->command, b->command); } -struct cmd_handler *find_handler(char *line, struct cmd_handler *cmd_handlers, - int handlers_size) { - struct cmd_handler d = { .command=line }; - struct cmd_handler *res = NULL; - sway_log(SWAY_DEBUG, "find_handler(%s)", line); - - bool config_loading = config->reading || !config->active; - - if (!config_loading) { - res = bsearch(&d, command_handlers, - sizeof(command_handlers) / sizeof(struct cmd_handler), - sizeof(struct cmd_handler), handler_compare); - - if (res) { - return res; - } +struct cmd_handler *find_handler(char *line, struct cmd_handler *handlers, + size_t handlers_size) { + if (!handlers || !handlers_size) { + return NULL; } + struct cmd_handler query = { .command = line }; + return bsearch(&query, handlers, + handlers_size / sizeof(struct cmd_handler), + sizeof(struct cmd_handler), handler_compare); +} +static struct cmd_handler *find_handler_ex(char *line, + struct cmd_handler *config_handlers, size_t config_handlers_size, + struct cmd_handler *command_handlers, size_t command_handlers_size, + struct cmd_handler *handlers, size_t handlers_size) { + struct cmd_handler *handler = NULL; if (config->reading) { - res = bsearch(&d, config_handlers, - sizeof(config_handlers) / sizeof(struct cmd_handler), - sizeof(struct cmd_handler), handler_compare); - - if (res) { - return res; - } + handler = find_handler(line, config_handlers, config_handlers_size); + } else if (config->active) { + handler = find_handler(line, command_handlers, command_handlers_size); } + return handler ? handler : find_handler(line, handlers, handlers_size); +} - if (!cmd_handlers) { - cmd_handlers = handlers; - handlers_size = sizeof(handlers); - } - - res = bsearch(&d, cmd_handlers, - handlers_size / sizeof(struct cmd_handler), - sizeof(struct cmd_handler), handler_compare); - - return res; +static struct cmd_handler *find_core_handler(char *line) { + return find_handler_ex(line, config_handlers, sizeof(config_handlers), + command_handlers, sizeof(command_handlers), + handlers, sizeof(handlers)); } static void set_config_node(struct sway_node *node) { @@ -270,7 +260,7 @@ list_t *execute_command(char *_exec, struct sway_seat *seat, } } } - struct cmd_handler *handler = find_handler(argv[0], NULL, 0); + struct cmd_handler *handler = find_core_handler(argv[0]); if (!handler) { list_add(res_list, cmd_results_new(CMD_INVALID, "Unknown/invalid command '%s'", argv[0])); @@ -360,7 +350,7 @@ struct cmd_results *config_command(char *exec, char **new_block) { // Determine the command handler sway_log(SWAY_INFO, "Config command: %s", exec); - struct cmd_handler *handler = find_handler(argv[0], NULL, 0); + struct cmd_handler *handler = find_core_handler(argv[0]); if (!handler || !handler->handle) { const char *error = handler ? "Command '%s' is shimmed, but unimplemented" |