Declare all struct cmd_handler arrays const

And make the functions handling these arrays use const types.
This commit is contained in:
Manuel Stoeckl 2021-02-02 19:54:35 -05:00 committed by Simon Ser
parent 89b4bc4bc7
commit cb3c727632
8 changed files with 29 additions and 29 deletions

View file

@ -46,8 +46,8 @@ enum expected_args {
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,
size_t handlers_size);
const struct cmd_handler *find_handler(char *line,
const struct cmd_handler *cmd_handlers, size_t handlers_size);
/**
* Parse and executes a command.
@ -68,7 +68,7 @@ struct cmd_results *config_command(char *command, char **new_block);
* Parse and handle a sub command
*/
struct cmd_results *config_subcommand(char **argv, int argc,
struct cmd_handler *handlers, size_t handlers_size);
const struct cmd_handler *handlers, size_t handlers_size);
/*
* Parses a command policy rule.
*/

View file

@ -42,7 +42,7 @@ struct cmd_results *checkarg(int argc, const char *name, enum expected_args type
}
/* Keep alphabetized */
static struct cmd_handler handlers[] = {
static const struct cmd_handler handlers[] = {
{ "assign", cmd_assign },
{ "bar", cmd_bar },
{ "bindcode", cmd_bindcode },
@ -98,7 +98,7 @@ static struct cmd_handler handlers[] = {
};
/* Config-time only commands. Keep alphabetized */
static struct cmd_handler config_handlers[] = {
static const struct cmd_handler config_handlers[] = {
{ "default_orientation", cmd_default_orientation },
{ "include", cmd_include },
{ "swaybg_command", cmd_swaybg_command },
@ -108,7 +108,7 @@ static struct cmd_handler config_handlers[] = {
};
/* Runtime-only commands. Keep alphabetized */
static struct cmd_handler command_handlers[] = {
static const struct cmd_handler command_handlers[] = {
{ "border", cmd_border },
{ "create_output", cmd_create_output },
{ "exit", cmd_exit },
@ -144,22 +144,22 @@ 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 *handlers,
size_t handlers_size) {
const struct cmd_handler *find_handler(char *line,
const struct cmd_handler *handlers, size_t handlers_size) {
if (!handlers || !handlers_size) {
return NULL;
}
struct cmd_handler query = { .command = line };
const 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;
static const struct cmd_handler *find_handler_ex(char *line,
const struct cmd_handler *config_handlers, size_t config_handlers_size,
const struct cmd_handler *command_handlers, size_t command_handlers_size,
const struct cmd_handler *handlers, size_t handlers_size) {
const struct cmd_handler *handler = NULL;
if (config->reading) {
handler = find_handler(line, config_handlers, config_handlers_size);
} else if (config->active) {
@ -168,7 +168,7 @@ static struct cmd_handler *find_handler_ex(char *line,
return handler ? handler : find_handler(line, handlers, handlers_size);
}
static struct cmd_handler *find_core_handler(char *line) {
static const 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));
@ -265,7 +265,7 @@ list_t *execute_command(char *_exec, struct sway_seat *seat,
}
}
}
struct cmd_handler *handler = find_core_handler(argv[0]);
const 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]));
@ -370,7 +370,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_core_handler(argv[0]);
const struct cmd_handler *handler = find_core_handler(argv[0]);
if (!handler || !handler->handle) {
const char *error = handler
? "Command '%s' is shimmed, but unimplemented"
@ -418,12 +418,12 @@ cleanup:
}
struct cmd_results *config_subcommand(char **argv, int argc,
struct cmd_handler *handlers, size_t handlers_size) {
const struct cmd_handler *handlers, size_t handlers_size) {
char *command = join_args(argv, argc);
sway_log(SWAY_DEBUG, "Subcommand: %s", command);
free(command);
struct cmd_handler *handler = find_handler(argv[0], handlers,
const struct cmd_handler *handler = find_handler(argv[0], handlers,
handlers_size);
if (!handler) {
return cmd_results_new(CMD_INVALID,
@ -453,7 +453,7 @@ struct cmd_results *config_commands_command(char *exec) {
goto cleanup;
}
struct cmd_handler *handler = find_handler(cmd, NULL, 0);
const struct cmd_handler *handler = find_handler(cmd, NULL, 0);
if (!handler && strcmp(cmd, "*") != 0) {
results = cmd_results_new(CMD_INVALID,
"Unknown/invalid command '%s'", cmd);

View file

@ -8,7 +8,7 @@
#include "log.h"
// Must be in alphabetical order for bsearch
static struct cmd_handler bar_handlers[] = {
static const struct cmd_handler bar_handlers[] = {
{ "bindcode", bar_cmd_bindcode },
{ "binding_mode_indicator", bar_cmd_binding_mode_indicator },
{ "bindsym", bar_cmd_bindsym },
@ -41,7 +41,7 @@ static struct cmd_handler bar_handlers[] = {
};
// Must be in alphabetical order for bsearch
static struct cmd_handler bar_config_handlers[] = {
static const struct cmd_handler bar_config_handlers[] = {
{ "id", bar_cmd_id },
{ "swaybar_command", bar_cmd_swaybar_command },
};

View file

@ -4,7 +4,7 @@
#include "util.h"
// Must be in alphabetical order for bsearch
static struct cmd_handler bar_colors_handlers[] = {
static const struct cmd_handler bar_colors_handlers[] = {
{ "active_workspace", bar_colors_cmd_active_workspace },
{ "background", bar_colors_cmd_background },
{ "binding_mode", bar_colors_cmd_binding_mode },

View file

@ -7,7 +7,7 @@
#include "stringop.h"
// must be in order for the bsearch
static struct cmd_handler input_handlers[] = {
static const struct cmd_handler input_handlers[] = {
{ "accel_profile", input_cmd_accel_profile },
{ "calibration_matrix", input_cmd_calibration_matrix },
{ "click_method", input_cmd_click_method },
@ -40,7 +40,7 @@ static struct cmd_handler input_handlers[] = {
};
// must be in order for the bsearch
static struct cmd_handler input_config_handlers[] = {
static const struct cmd_handler input_config_handlers[] = {
{ "xkb_capslock", input_cmd_xkb_capslock },
{ "xkb_numlock", input_cmd_xkb_numlock },
};

View file

@ -9,7 +9,7 @@
#include "stringop.h"
// Must be in order for the bsearch
static struct cmd_handler mode_handlers[] = {
static const struct cmd_handler mode_handlers[] = {
{ "bindcode", cmd_bindcode },
{ "bindswitch", cmd_bindswitch },
{ "bindsym", cmd_bindsym },

View file

@ -6,7 +6,7 @@
#include "log.h"
// must be in order for the bsearch
static struct cmd_handler output_handlers[] = {
static const struct cmd_handler output_handlers[] = {
{ "adaptive_sync", output_cmd_adaptive_sync },
{ "background", output_cmd_background },
{ "bg", output_cmd_background },

View file

@ -8,13 +8,13 @@
// must be in order for the bsearch
// these handlers perform actions on the seat
static struct cmd_handler seat_action_handlers[] = {
static const struct cmd_handler seat_action_handlers[] = {
{ "cursor", seat_cmd_cursor },
};
// must be in order for the bsearch
// these handlers alter the seat config
static struct cmd_handler seat_handlers[] = {
static const struct cmd_handler seat_handlers[] = {
{ "attach", seat_cmd_attach },
{ "fallback", seat_cmd_fallback },
{ "hide_cursor", seat_cmd_hide_cursor },