diff --git a/.clang-tidy b/.clang-tidy index ff799fe..9fa2b22 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -5,7 +5,10 @@ Checks: > -clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling, bugprone-*, -bugprone-easily-swappable-parameters, + -bugprone-signal-handler, cert-*, + -cert-msc54-cpp, + -cert-sig30-c, modernize-*, -modernize-macro-to-enum, performance-*, @@ -27,6 +30,9 @@ CheckOptions: - key: readability-identifier-naming.ConstantParameterCase value: UPPER_CASE + - key: readability-identifier-naming.ConstantCase + value: "UPPER_CASE" + - key: readability-identifier-length.VariableThreshold value: '2' - key: readability-identifier-length.ParameterThreshold diff --git a/include/config.h b/include/config.h index 1950a59..818f850 100644 --- a/include/config.h +++ b/include/config.h @@ -14,7 +14,7 @@ enum introspection_type { KEY, STRING_ARRAY, }; -static const char* NNULLABLE const intros_tys_names[] = { +static const char* NNULLABLE const INTROS_TYS_NAMES[] = { [STRING] = "STRING", [BOOL] = "BOOL", [KEY] = "KEY", @@ -43,70 +43,70 @@ struct introspection_item { struct table_##table { \ TABLE(STRUCT_BUILDER, table) \ } -#define BUILD_DEFAULT(table, TABLE) \ - static const struct table_##table default_table_##table = { \ +#define BUILD_DEFAULT(utable, table, TABLE) \ + static const struct table_##table DEFAULT_TABLE_##utable = { \ TABLE(DEFAULT_BUILDER, table)} -#define BUILD_INTROS(table, TABLE) \ - static const struct introspection_item intros_table_##table[] = { \ +#define BUILD_INTROS(utable, table, TABLE) \ + static const struct introspection_item INTROS_TABLE_##utable[] = { \ TABLE(INTROS_BUILDER, table)} -#define BUILD(table, TABLE) \ - BUILD_TABLE(table, TABLE); \ - BUILD_DEFAULT(table, TABLE); \ - BUILD_INTROS(table, TABLE); +#define BUILD(table, utable, TABLE) \ + BUILD_TABLE(table, TABLE); \ + BUILD_DEFAULT(utable, table, TABLE); \ + BUILD_INTROS(utable, table, TABLE); // should be ansi escape codes under \x1b[...m // if not prepared accordingly, it might break -#define TABLE_COLORS(F, name) \ - F(char*, bg, STRING, "48;2;38;28;28", name) \ - F(char*, fg, STRING, "22;24;38;2;245;245;245", name) \ - F(char*, err, STRING, "1;31", name) \ - F(char*, s_wayland, STRING, "38;2;255;174;66", name) \ - F(char*, s_xorg, STRING, "38;2;37;175;255", name) \ - F(char*, s_shell, STRING, "38;2;34;140;34", name) \ - F(char*, e_hostname, STRING, "38;2;255;64;64", name) \ - F(char*, e_date, STRING, "38;2;144;144;144", name) \ - F(char*, e_box, STRING, "38;2;122;122;122", name) \ - F(char*, e_header, STRING, "4;38;2;0;255;0", name) \ - F(char*, e_user, STRING, "36", name) \ - F(char*, e_passwd, STRING, "4;38;2;245;245;205", name) \ - F(char*, e_badpasswd, STRING, "3;4;31", name) \ - F(char*, e_key, STRING, "38;2;255;174;66", name) +#define TABLE_COLORS(F, name) \ + F(char* NNULLABLE, bg, STRING, "48;2;38;28;28", name) \ + F(char* NNULLABLE, fg, STRING, "22;24;38;2;245;245;245", name) \ + F(char* NNULLABLE, err, STRING, "1;31", name) \ + F(char* NNULLABLE, s_wayland, STRING, "38;2;255;174;66", name) \ + F(char* NNULLABLE, s_xorg, STRING, "38;2;37;175;255", name) \ + F(char* NNULLABLE, s_shell, STRING, "38;2;34;140;34", name) \ + F(char* NNULLABLE, e_hostname, STRING, "38;2;255;64;64", name) \ + F(char* NNULLABLE, e_date, STRING, "38;2;144;144;144", name) \ + F(char* NNULLABLE, e_box, STRING, "38;2;122;122;122", name) \ + F(char* NNULLABLE, e_header, STRING, "4;38;2;0;255;0", name) \ + F(char* NNULLABLE, e_user, STRING, "36", name) \ + F(char* NNULLABLE, e_passwd, STRING, "4;38;2;245;245;205", name) \ + F(char* NNULLABLE, e_badpasswd, STRING, "3;4;31", name) \ + F(char* NNULLABLE, e_key, STRING, "38;2;255;174;66", name) -BUILD(colors, TABLE_COLORS); +BUILD(colors, COLORS, TABLE_COLORS); // even if they're multiple bytes long // they should only take up 1 char size on display -#define TABLE_CHARS(F, name) \ - F(char*, hb, STRING, "─", name) \ - F(char*, vb, STRING, "│", name) \ - F(char*, ctl, STRING, "┌", name) \ - F(char*, ctr, STRING, "┐", name) \ - F(char*, cbl, STRING, "└", name) \ - F(char*, cbr, STRING, "┘", name) +#define TABLE_CHARS(F, name) \ + F(char* NNULLABLE, hb, STRING, "─", name) \ + F(char* NNULLABLE, vb, STRING, "│", name) \ + F(char* NNULLABLE, ctl, STRING, "┌", name) \ + F(char* NNULLABLE, ctr, STRING, "┐", name) \ + F(char* NNULLABLE, cbl, STRING, "└", name) \ + F(char* NNULLABLE, cbr, STRING, "┘", name) -BUILD(chars, TABLE_CHARS); +BUILD(chars, CHARS, TABLE_CHARS); #define TABLE_FUNCTIONS(F, name) \ F(enum keys, poweroff, KEY, F1, name) \ F(enum keys, reboot, KEY, F2, name) \ F(enum keys, refresh, KEY, F5, name) -BUILD(functions, TABLE_FUNCTIONS); +BUILD(functions, FUNCTIONS, TABLE_FUNCTIONS); -#define TABLE_STRINGS(F, name) \ - F(char*, f_poweroff, STRING, "poweroff", name) \ - F(char*, f_reboot, STRING, "reboot", name) \ - F(char*, f_refresh, STRING, "refresh", name) \ - F(char*, e_user, STRING, "user", name) \ - F(char*, e_passwd, STRING, "password", name) \ - F(char*, s_wayland, STRING, "wayland", name) \ - F(char*, s_xorg, STRING, "xorg", name) \ - F(char*, s_shell, STRING, "shell", name) \ - F(char*, opts_pre, STRING, "< ", name) \ - F(char*, opts_post, STRING, " >", name) +#define TABLE_STRINGS(F, name) \ + F(char* NNULLABLE, f_poweroff, STRING, "poweroff", name) \ + F(char* NNULLABLE, f_reboot, STRING, "reboot", name) \ + F(char* NNULLABLE, f_refresh, STRING, "refresh", name) \ + F(char* NNULLABLE, e_user, STRING, "user", name) \ + F(char* NNULLABLE, e_passwd, STRING, "password", name) \ + F(char* NNULLABLE, s_wayland, STRING, "wayland", name) \ + F(char* NNULLABLE, s_xorg, STRING, "xorg", name) \ + F(char* NNULLABLE, s_shell, STRING, "shell", name) \ + F(char* NNULLABLE, opts_pre, STRING, "< ", name) \ + F(char* NNULLABLE, opts_post, STRING, " >", name) -BUILD(strings, TABLE_STRINGS); +BUILD(strings, STRINGS, TABLE_STRINGS); #define NULL_VEC \ (struct Vector) { \ @@ -117,7 +117,7 @@ BUILD(strings, TABLE_STRINGS); F(struct Vector, source, STRING_ARRAY, NULL_VEC, name) \ F(struct Vector, user_source, STRING_ARRAY, NULL_VEC, name) -BUILD(behavior, TABLE_BEHAVIOR); +BUILD(behavior, BEHAVIOR, TABLE_BEHAVIOR); //// CONFIG struct config { @@ -128,12 +128,12 @@ struct config { struct table_behavior behavior; }; -static const struct config default_config = { - .colors = default_table_colors, - .chars = default_table_chars, - .functions = default_table_functions, - .strings = default_table_strings, - .behavior = default_table_behavior, +static const struct config DEFAULT_CONFIG = { + .colors = DEFAULT_TABLE_COLORS, + .chars = DEFAULT_TABLE_CHARS, + .functions = DEFAULT_TABLE_FUNCTIONS, + .strings = DEFAULT_TABLE_STRINGS, + .behavior = DEFAULT_TABLE_BEHAVIOR, }; struct introspection_table { @@ -143,17 +143,17 @@ struct introspection_table { size_t len; }; -static const struct introspection_table config_instrospection[] = { - {"colors", offsetof(struct config, colors), intros_table_colors, - sizeof(intros_table_colors) / sizeof(intros_table_colors[0])}, - {"chars", offsetof(struct config, chars), intros_table_chars, - sizeof(intros_table_chars) / sizeof(intros_table_chars[0])}, - {"functions", offsetof(struct config, functions), intros_table_functions, - sizeof(intros_table_functions) / sizeof(intros_table_functions[0])}, - {"strings", offsetof(struct config, strings), intros_table_strings, - sizeof(intros_table_strings) / sizeof(intros_table_strings[0])}, - {"behavior", offsetof(struct config, behavior), intros_table_behavior, - sizeof(intros_table_behavior) / sizeof(intros_table_behavior[0])}, +static const struct introspection_table CONFIG_INSTROSPECTION[] = { + {"colors", offsetof(struct config, colors), INTROS_TABLE_COLORS, + sizeof(INTROS_TABLE_COLORS) / sizeof(INTROS_TABLE_COLORS[0])}, + {"chars", offsetof(struct config, chars), INTROS_TABLE_CHARS, + sizeof(INTROS_TABLE_CHARS) / sizeof(INTROS_TABLE_CHARS[0])}, + {"functions", offsetof(struct config, functions), INTROS_TABLE_FUNCTIONS, + sizeof(INTROS_TABLE_FUNCTIONS) / sizeof(INTROS_TABLE_FUNCTIONS[0])}, + {"strings", offsetof(struct config, strings), INTROS_TABLE_STRINGS, + sizeof(INTROS_TABLE_STRINGS) / sizeof(INTROS_TABLE_STRINGS[0])}, + {"behavior", offsetof(struct config, behavior), INTROS_TABLE_BEHAVIOR, + sizeof(INTROS_TABLE_BEHAVIOR) / sizeof(INTROS_TABLE_BEHAVIOR[0])}, }; //// FUNCTIONS diff --git a/include/keys.h b/include/keys.h index 57d6b0f..8b37764 100644 --- a/include/keys.h +++ b/include/keys.h @@ -34,7 +34,7 @@ enum keys { PAGE_DOWN, }; -static const char* const key_names[] = { +static const char* const KEY_NAMES[] = { [ESC] = "ESC", [F1] = "F1", [F2] = "F2", @@ -70,7 +70,7 @@ struct key_mapping { const char* sequences[3]; }; -static const struct key_mapping key_mappings[] = { +static const struct key_mapping KEY_MAPPINGS[] = { {ESC, {"\x1b", NULL}}, {F1, {"\x1bOP", "\x1b[[A", NULL}}, {F2, {"\x1bOQ", "\x1b[[B", NULL}}, diff --git a/include/ui.h b/include/ui.h index 87e103c..d37df95 100644 --- a/include/ui.h +++ b/include/ui.h @@ -40,12 +40,12 @@ #define VALUE_MAXLEN (BOX_WIDTH - VALUES_COL + 1 - BOX_HMARGIN - 2) enum input { SESSION, USER, PASSWD }; -extern const u_char inputs_n; +extern const u_char INPUTS_N; void setup(struct config* config); int load(struct Vector* users, struct Vector* sessions); -void print_err(const char*); -void print_errno(const char*); +void print_err(const char* /*msg*/); +void print_errno(const char* /*descr*/); void ui_update_field(enum input focused_input); void ui_update_ffield(); diff --git a/include/ui_state.h b/include/ui_state.h index aa920ef..52e6df4 100644 --- a/include/ui_state.h +++ b/include/ui_state.h @@ -3,6 +3,7 @@ #include +#include "macros.h" #include "ui.h" extern enum input focused_input; @@ -11,8 +12,8 @@ extern struct opts_field of_session; extern struct opts_field of_user; extern struct opts_field of_passwd; -extern struct Vector* gusers; -extern struct Vector* gsessions; +extern struct Vector* UNULLABLE gusers; +extern struct Vector* UNULLABLE gsessions; struct opts_field* NNULLABLE get_opts_field(enum input from); struct opts_field* NNULLABLE get_opts_ffield(); @@ -23,6 +24,6 @@ struct session st_session(bool include_defshell); void st_ch_focus(char direction); void st_ch_of_opts(char direction); void st_ch_ef_col(char direction); -void st_kbd_type(char* text, bool cfg_include_defshell); +void st_kbd_type(char* NNULLABLE text, bool cfg_include_defshell); #endif diff --git a/include/util.h b/include/util.h index 3c3e35a..126533c 100644 --- a/include/util.h +++ b/include/util.h @@ -10,8 +10,8 @@ #include "keys.h" int find_keyname(enum keys* at, const char* name); -enum keys find_ansi(const char*); -void read_press(u_char*, char*); +enum keys find_ansi(const char* seq); +void read_press(u_char* length, char* out); bool utf8_iscont(char byte); size_t utf8len(const char* str); @@ -27,14 +27,14 @@ struct Vector { }; extern const struct Vector VEC_NEW; -int vec_resize(struct Vector*, size_t size); -int vec_reserve(struct Vector*, size_t size); -int vec_reserve_exact(struct Vector*, size_t size); -int vec_push(struct Vector*, void* item); -void vec_free(struct Vector*); -void vec_clear(struct Vector*); -void vec_reset(struct Vector*); -void* vec_pop(struct Vector*); // won't free it, nor shrink vec list space -void* vec_get(struct Vector*, size_t index); +int vec_resize(struct Vector* self, size_t size); +int vec_reserve(struct Vector* self, size_t size); +int vec_reserve_exact(struct Vector* self, size_t size); +int vec_push(struct Vector* self, void* item); +void vec_free(struct Vector* self); +void vec_clear(struct Vector* self); +void vec_reset(struct Vector* self); +void* vec_pop(struct Vector* self); // won't free it, nor shrink vec list space +void* vec_get(struct Vector* self, size_t index); #endif diff --git a/src/config.c b/src/config.c index 31a1392..c62d7e3 100644 --- a/src/config.c +++ b/src/config.c @@ -126,7 +126,10 @@ struct parser_error parse_key(enum introspection_type typ, union typ_ptr at, aux_str = strdup(key); if (!aux_str) FAIL("allocation failure"); aux_err = parse_str_inplace(aux_str); - if (aux_err.msg != NULL) return aux_err; + if (aux_err.msg != NULL) { + free(aux_str); + return aux_err; + } // FIXME: it be broken, prob the ptr arithmetic or smth, we mem leak // instead 😎 If the ptr is not the default it means it was prev // allocated, we should free if (*(char**)((uintptr_t)(&default_config) + @@ -154,7 +157,10 @@ struct parser_error parse_key(enum introspection_type typ, union typ_ptr at, aux_str = strdup(key); if (!aux_str) FAIL("allocation failure"); aux_err = parse_str_inplace(aux_str); - if (aux_err.msg != NULL) return aux_err; + if (aux_err.msg != NULL) { + free(aux_str); + return aux_err; + } vec_push(at.vec, aux_str); break; } @@ -171,12 +177,12 @@ struct status config_line_handler(void* _config, char* table, char* k, struct status ret = {.finish = false}; const struct introspection_table* this_intros_table = NULL; - for (size_t i = 0; i < LEN(config_instrospection); i++) { + for (size_t i = 0; i < LEN(CONFIG_INSTROSPECTION); i++) { if (table == NULL) { - if (table != config_instrospection[i].tname) continue; - } else if (strcmp(config_instrospection[i].tname, table) != 0) + if (table != CONFIG_INSTROSPECTION[i].tname) continue; + } else if (strcmp(CONFIG_INSTROSPECTION[i].tname, table) != 0) continue; - this_intros_table = &config_instrospection[i]; + this_intros_table = &CONFIG_INSTROSPECTION[i]; break; } if (this_intros_table == NULL) { @@ -202,7 +208,7 @@ struct status config_line_handler(void* _config, char* table, char* k, struct parser_error err = parse_key(this_intros_key->typ, k_addr, v, offset); if (err.msg != NULL) { log_printf("[E] cfg parser, failed to parse [%s.%s] (%s): %s\n", table, k, - intros_tys_names[this_intros_key->typ], err.msg); + INTROS_TYS_NAMES[this_intros_key->typ], err.msg); log_printf("%s\n%*c^\n", v, err.col); return ret; } diff --git a/src/desktop.c b/src/desktop.c index 6c33739..04c96c0 100644 --- a/src/desktop.c +++ b/src/desktop.c @@ -10,7 +10,7 @@ char* trim_str(char* str) { while (*str == ' ' || *str == '\t') str++; - size_t i = strlen(str); + size_t i = strlen(str); // NOLINT(readability-identifier-length) while (i > 0) { if (str[i - 1] != ' ' && str[i - 1] != '\t' && str[i - 1] != '\n') break; i--; diff --git a/src/efield.c b/src/efield.c index 45d0e49..2327c72 100644 --- a/src/efield.c +++ b/src/efield.c @@ -36,7 +36,7 @@ void efield_update(struct editable_field* self, char* update) { if (*update == BACKSPACE_CODE) { if (self->pos == 0) return; char* curr = &self->content[self->pos]; - char* prev = utf8back(curr); + char* prev = (char*)utf8back(curr); memmove(prev, curr, strlen(self->content) - self->pos + 1); self->pos -= curr - prev; diff --git a/src/main.c b/src/main.c index d4efdfc..3ec6f92 100644 --- a/src/main.c +++ b/src/main.c @@ -30,7 +30,7 @@ int main(int argc, char* argv[]) { // Chvt if (argc == 2) chvt_str(argv[1]); - struct config config = default_config; + struct config config = DEFAULT_CONFIG; char* conf_override = getenv("LIDM_CONF"); char* conf_path = conf_override ? conf_override : "/etc/lidm.ini"; if (parse_config(&config, conf_path) != 0) { diff --git a/src/sessions.c b/src/sessions.c index da7eff3..f8f09b3 100644 --- a/src/sessions.c +++ b/src/sessions.c @@ -16,7 +16,7 @@ struct source_dir { enum session_type type; char* dir; }; -static const struct source_dir sources[] = { +static const struct source_dir SOURCES[] = { {XORG, "/usr/share/xsessions"}, {WAYLAND, "/usr/share/wayland-sessions"}, }; @@ -120,10 +120,10 @@ struct Vector get_avaliable_sessions() { vec_reserve(&sessions, LIKELY_BOUND_SESSIONS); cb_sessions = &sessions; - for (size_t i = 0; i < (sizeof(sources) / sizeof(sources[0])); i++) { - log_printf("[I] parsing into %s\n", sources[i].dir); - session_type = sources[i].type; - ftw(sources[i].dir, &fn, 1); + for (size_t i = 0; i < (sizeof(SOURCES) / sizeof(SOURCES[0])); i++) { + log_printf("[I] parsing into %s\n", SOURCES[i].dir); + session_type = SOURCES[i].type; + ftw(SOURCES[i].dir, &fn, 1); } cb_sessions = NULL; diff --git a/src/ui.c b/src/ui.c index 4e18b7c..3eb3a0b 100644 --- a/src/ui.c +++ b/src/ui.c @@ -27,7 +27,7 @@ #include "users.h" #include "util.h" -const u_char inputs_n = 3; +const u_char INPUTS_N = 3; static void print_box(); static void print_footer(); @@ -182,21 +182,21 @@ int load(struct Vector* users, struct Vector* sessions) { of_passwd = ofield_new(0); /// PRINTING - const struct uint_point boxstart = box_start(); + const struct uint_point BOXSTART = box_start(); // printf box print_box(); // put hostname - printf("\x1b[%d;%dH\x1b[%sm%s\x1b[%sm", boxstart.y + HEAD_ROW, - boxstart.x + VALUES_COL - VALUES_SEPR - (uint)utf8len(hostname), + printf("\x1b[%d;%dH\x1b[%sm%s\x1b[%sm", BOXSTART.y + HEAD_ROW, + BOXSTART.x + VALUES_COL - VALUES_SEPR - (uint)utf8len(hostname), g_config->colors.e_hostname, hostname, g_config->colors.fg); if (hostname != unknown_str) free(hostname); // put date char* fmtd_time = fmt_time(); - printf("\x1b[%d;%dH\x1b[%sm%s\x1b[%sm", boxstart.y + HEAD_ROW, - boxstart.x + BOX_WIDTH - 1 - BOX_HMARGIN - (uint)utf8len(fmtd_time), + printf("\x1b[%d;%dH\x1b[%sm%s\x1b[%sm", BOXSTART.y + HEAD_ROW, + BOXSTART.x + BOX_WIDTH - 1 - BOX_HMARGIN - (uint)utf8len(fmtd_time), g_config->colors.e_date, fmtd_time, g_config->colors.fg); free(fmtd_time); @@ -392,9 +392,9 @@ static void print_row(uint wid, uint n, char* edge1, char* edge2, } static void print_box() { - const struct uint_point bstart = box_start(); + const struct uint_point BSTART = box_start(); - printf("\x1b[%d;%dH\x1b[%sm", bstart.y, bstart.x, g_config->colors.e_box); + printf("\x1b[%d;%dH\x1b[%sm", BSTART.y, BSTART.x, g_config->colors.e_box); print_row(BOX_WIDTH - 2, 1, g_config->chars.ctl, g_config->chars.ctr, g_config->chars.hb); print_empty_row(BOX_WIDTH - 2, BOX_HEIGHT - 2, g_config->chars.vb, @@ -408,9 +408,9 @@ static void print_box() { static void print_footer() { size_t bsize = snprintf( NULL, 0, "%s %s %s %s %s %s", g_config->strings.f_poweroff, - key_names[g_config->functions.poweroff], g_config->strings.f_reboot, - key_names[g_config->functions.reboot], g_config->strings.f_refresh, - key_names[g_config->functions.refresh]); + KEY_NAMES[g_config->functions.poweroff], g_config->strings.f_reboot, + KEY_NAMES[g_config->functions.reboot], g_config->strings.f_refresh, + KEY_NAMES[g_config->functions.refresh]); uint row = window.ws_row - 1; uint col = window.ws_col - 2 - bsize; @@ -418,9 +418,9 @@ static void print_footer() { "\x1b[%3$d;%4$dH%8$s \x1b[%1$sm%5$s\x1b[%2$sm %9$s " "\x1b[%1$sm%6$s\x1b[%2$sm %10$s \x1b[%1$sm%7$s\x1b[%2$sm", g_config->colors.e_key, g_config->colors.fg, row, col, - key_names[g_config->functions.poweroff], - key_names[g_config->functions.reboot], - key_names[g_config->functions.refresh], g_config->strings.f_poweroff, + KEY_NAMES[g_config->functions.poweroff], + KEY_NAMES[g_config->functions.reboot], + KEY_NAMES[g_config->functions.refresh], g_config->strings.f_poweroff, g_config->strings.f_reboot, g_config->strings.f_refresh); (void)fflush(stdout); } diff --git a/src/ui_state.c b/src/ui_state.c index 208b9cf..18e1b09 100644 --- a/src/ui_state.c +++ b/src/ui_state.c @@ -54,7 +54,7 @@ struct session st_session(bool include_defshell) { } void st_ch_focus(char direction) { - focused_input = (focused_input + direction) % inputs_n; + focused_input = (focused_input + direction) % INPUTS_N; ui_update_cursor_focus(); } diff --git a/src/util.c b/src/util.c index bcc8ec6..7701f9f 100644 --- a/src/util.c +++ b/src/util.c @@ -12,8 +12,8 @@ static int selret_magic(); int find_keyname(enum keys* at, const char* name) { - for (size_t i = 0; i < sizeof(key_mappings) / sizeof(key_mappings[0]); i++) { - if (strcmp(key_names[i], name) == 0) { + for (size_t i = 0; i < LEN(KEY_MAPPINGS); i++) { + if (strcmp(KEY_NAMES[i], name) == 0) { *at = (enum keys)i; return 0; } @@ -23,8 +23,8 @@ int find_keyname(enum keys* at, const char* name) { } enum keys find_ansi(const char* seq) { - for (size_t i = 0; i < sizeof(key_mappings) / sizeof(key_mappings[0]); i++) { - struct key_mapping mapping = key_mappings[i]; + for (size_t i = 0; i < LEN(KEY_MAPPINGS); i++) { + struct key_mapping mapping = KEY_MAPPINGS[i]; for (size_t j = 0; mapping.sequences[j] != NULL; j++) { if (strcmp(mapping.sequences[j], seq) == 0) { return (enum keys)i; @@ -115,72 +115,72 @@ const struct Vector VEC_NEW = { .pages = NULL, }; -int vec_resize(struct Vector* vec, size_t size) { - void** new_location = realloc(vec->pages, size * sizeof(void*)); +int vec_resize(struct Vector* self, size_t size) { + void** new_location = + (void**)realloc((void*)self->pages, size * sizeof(void*)); if (new_location != NULL) { - if (vec->length > size) vec->length = size; - vec->capacity = size; - vec->pages = new_location; + if (self->length > size) self->length = size; + self->capacity = size; + self->pages = new_location; } else { return -1; } return 0; } -int vec_reserve(struct Vector* vec, size_t size) { - uint32_t new_capacity = vec->capacity; - while (vec->length + size > new_capacity) { +int vec_reserve(struct Vector* self, size_t size) { + uint32_t new_capacity = self->capacity; + while (self->length + size > new_capacity) { new_capacity = new_capacity + (new_capacity >> 1) + 1; // cap * 1.5 + 1; 0 1 2 4 7 11... } - return vec_resize(vec, new_capacity); + return vec_resize(self, new_capacity); } -int vec_reserve_exact(struct Vector* vec, size_t size) { - uint32_t needed_capacity = vec->length + size; - if (vec->capacity < needed_capacity) { - return vec_resize(vec, needed_capacity); - } else { - return 0; +int vec_reserve_exact(struct Vector* self, size_t size) { + uint32_t needed_capacity = self->length + size; + if (self->capacity < needed_capacity) { + return vec_resize(self, needed_capacity); } -} - -int vec_push(struct Vector* vec, void* item) { - int res_ret = vec_reserve(vec, 1); - if (res_ret != 0) return res_ret; - - vec->pages[vec->length++] = item; return 0; } -void vec_free(struct Vector* vec) { - while (vec->length > 0) - free(vec->pages[--vec->length]); +int vec_push(struct Vector* self, void* item) { + int res_ret = vec_reserve(self, 1); + if (res_ret != 0) return res_ret; - vec_clear(vec); + self->pages[self->length++] = item; + return 0; } -void vec_clear(struct Vector* vec) { - free(vec->pages); - vec_reset(vec); +void vec_free(struct Vector* self) { + while (self->length > 0) + free(self->pages[--self->length]); + + vec_clear(self); } -void vec_reset(struct Vector* vec) { - *vec = (struct Vector){ +void vec_clear(struct Vector* self) { + free((void*)self->pages); + vec_reset(self); +} + +void vec_reset(struct Vector* self) { + *self = (struct Vector){ .length = 0, .capacity = 0, .pages = NULL, }; } -void* vec_pop(struct Vector* vec) { - if (vec->length == 0) return NULL; +void* vec_pop(struct Vector* self) { + if (self->length == 0) return NULL; - return vec->pages[--vec->length]; + return self->pages[--self->length]; } -void* vec_get(struct Vector* vec, size_t index) { - if (index >= vec->length) return NULL; +void* vec_get(struct Vector* self, size_t index) { + if (index >= self->length) return NULL; - return vec->pages[index]; + return self->pages[index]; }