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.
master
Brian Ashworth 6 years ago committed by Simon Ser
parent 5069b53d6c
commit c346c020bf

@ -47,7 +47,8 @@ struct cmd_results *checkarg(int argc, const char *name,
enum expected_args type, int val); enum expected_args type, int val);
struct cmd_handler *find_handler(char *line, struct cmd_handler *cmd_handlers, struct cmd_handler *find_handler(char *line, struct cmd_handler *cmd_handlers,
int handlers_size); size_t handlers_size);
/** /**
* Parse and executes a command. * Parse and executes a command.
* *

@ -143,44 +143,34 @@ static int handler_compare(const void *_a, const void *_b) {
return strcasecmp(a->command, b->command); return strcasecmp(a->command, b->command);
} }
struct cmd_handler *find_handler(char *line, struct cmd_handler *cmd_handlers, struct cmd_handler *find_handler(char *line, struct cmd_handler *handlers,
int handlers_size) { size_t handlers_size) {
struct cmd_handler d = { .command=line }; if (!handlers || !handlers_size) {
struct cmd_handler *res = NULL; return 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 query = { .command = line };
if (config->reading) { return bsearch(&query, handlers,
res = bsearch(&d, config_handlers, handlers_size / sizeof(struct cmd_handler),
sizeof(config_handlers) / sizeof(struct cmd_handler),
sizeof(struct cmd_handler), handler_compare); sizeof(struct cmd_handler), handler_compare);
}
if (res) { static struct cmd_handler *find_handler_ex(char *line,
return res; 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 (!cmd_handlers) { if (config->reading) {
cmd_handlers = handlers; handler = find_handler(line, config_handlers, config_handlers_size);
handlers_size = sizeof(handlers); } else if (config->active) {
handler = find_handler(line, command_handlers, command_handlers_size);
} }
return handler ? handler : find_handler(line, handlers, handlers_size);
}
res = bsearch(&d, cmd_handlers, static struct cmd_handler *find_core_handler(char *line) {
handlers_size / sizeof(struct cmd_handler), return find_handler_ex(line, config_handlers, sizeof(config_handlers),
sizeof(struct cmd_handler), handler_compare); command_handlers, sizeof(command_handlers),
handlers, sizeof(handlers));
return res;
} }
static void set_config_node(struct sway_node *node) { 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) { if (!handler) {
list_add(res_list, cmd_results_new(CMD_INVALID, list_add(res_list, cmd_results_new(CMD_INVALID,
"Unknown/invalid command '%s'", argv[0])); "Unknown/invalid command '%s'", argv[0]));
@ -360,7 +350,7 @@ struct cmd_results *config_command(char *exec, char **new_block) {
// Determine the command handler // Determine the command handler
sway_log(SWAY_INFO, "Config command: %s", exec); 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) { if (!handler || !handler->handle) {
const char *error = handler const char *error = handler
? "Command '%s' is shimmed, but unimplemented" ? "Command '%s' is shimmed, but unimplemented"

Loading…
Cancel
Save