From 1abf5e35c54a823d60d2388750990e0edf6a83b6 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 14 Jul 2014 10:52:20 +0200 Subject: [PATCH] command: make variables more local --- src/command.c | 45 ++++++++++++++++++--------------------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/src/command.c b/src/command.c index 5621b16..62d68a5 100644 --- a/src/command.c +++ b/src/command.c @@ -268,15 +268,12 @@ get_cmds_max_name_width(command_definition_t *c) if (max != 0) return max; - size_t len; - command_definition_t *p; - - for (p = c; p->name != NULL; p++) { + for (command_definition_t *p = c; p->name != NULL; p++) { /* * width and length are considered the same here, as command * names are not translated. */ - len = (size_t) strlen(p->name); + size_t len = strlen(p->name); if (len > max) max = len; } @@ -288,11 +285,9 @@ get_cmds_max_name_width(command_definition_t *c) const char * key2str(int key) { - static char buf[32]; - int i; - - buf[0] = 0; switch(key) { + static char buf[32]; + case 0: return _("Undefined"); case ' ': @@ -328,7 +323,7 @@ key2str(int key) case KEY_IC: return _("Insert"); default: - for (i = 0; i <= 63; i++) + for (int i = 0; i <= 63; i++) if (key == KEY_F(i)) { g_snprintf(buf, 32, _("F%d"), i ); return buf; @@ -341,15 +336,14 @@ key2str(int key) g_snprintf(buf, 32, "%c", key); else g_snprintf(buf, 32, "0x%03X", key); + return buf; } - - return buf; } void command_dump_keys(void) { - for (int i = 0; cmds[i].description; i++) + for (size_t i = 0; cmds[i].description; i++) if (cmds[i].command != CMD_NONE) printf(" %20s : %s\n", get_key_names(cmds[i].command, true), @@ -361,7 +355,7 @@ command_dump_keys(void) static void set_key_flags(command_definition_t *cp, command_t command, int flags) { - for (int i = 0; cp[i].name; i++) { + for (size_t i = 0; cp[i].name; i++) { if (cp[i].command == command) { cp[i].flags |= flags; break; @@ -382,7 +376,7 @@ get_key_names(command_t command, bool all) if (!all) return keystr; - for (int j = 1; j < MAX_COMMAND_KEYS && + for (unsigned j = 1; j < MAX_COMMAND_KEYS && cmds[i].keys[j] > 0; j++) { g_strlcat(keystr, " ", sizeof(keystr)); g_strlcat(keystr, key2str(cmds[i].keys[j]), sizeof(keystr)); @@ -396,7 +390,7 @@ get_key_names(command_t command, bool all) const char * get_key_description(command_t command) { - for (int i = 0; cmds[i].description; i++) + for (size_t i = 0; cmds[i].description; i++) if (cmds[i].command == command) return _(cmds[i].description); @@ -406,7 +400,7 @@ get_key_description(command_t command) const char * get_key_command_name(command_t command) { - for (int i = 0; cmds[i].name; i++) + for (size_t i = 0; cmds[i].name; i++) if (cmds[i].command == command) return cmds[i].name; @@ -416,7 +410,7 @@ get_key_command_name(command_t command) command_t get_key_command_from_name(const char *name) { - for (int i = 0; cmds[i].name; i++) + for (size_t i = 0; cmds[i].name; i++) if (strcmp(name, cmds[i].name) == 0) return cmds[i].command; @@ -429,7 +423,7 @@ find_key_command(int key, const command_definition_t *c) assert(key != 0); assert(c != NULL); - for (int i = 0; c[i].name; i++) { + for (size_t i = 0; c[i].name; i++) { for (int j = 0; j < MAX_COMMAND_KEYS; j++) if (c[i].keys[j] == key) return c[i].command; @@ -447,9 +441,7 @@ get_key_command(int key) command_t get_keyboard_command(void) { - int key; - - key = wgetch(stdscr); + int key = wgetch(stdscr); if (key == ERR || key == '\0') return CMD_NONE; @@ -464,7 +456,7 @@ get_keyboard_command(void) int assign_keys(command_t command, int keys[MAX_COMMAND_KEYS]) { - for (int i = 0; cmds[i].name; i++) { + for (size_t i = 0; cmds[i].name; i++) { if (cmds[i].command == command) { memcpy(cmds[i].keys, keys, sizeof(int)*MAX_COMMAND_KEYS); #ifndef NCMPC_MINI @@ -482,16 +474,15 @@ assign_keys(command_t command, int keys[MAX_COMMAND_KEYS]) int check_key_bindings(command_definition_t *cp, char *buf, size_t bufsize) { - int i; int retval = 0; if (cp == NULL) cp = cmds; - for (i = 0; cp[i].name; i++) + for (size_t i = 0; cp[i].name; i++) cp[i].flags &= ~COMMAND_KEY_CONFLICT; - for (i = 0; cp[i].name; i++) { + for (size_t i = 0; cp[i].name; i++) { int j; command_t cmd; @@ -528,7 +519,7 @@ write_key_bindings(FILE *f, int flags) if (flags & KEYDEF_WRITE_HEADER) fprintf(f, "## Key bindings for ncmpc (generated by ncmpc)\n\n"); - for (int i = 0; cmds[i].name && !ferror(f); i++) { + for (size_t i = 0; cmds[i].name && !ferror(f); i++) { if (cmds[i].flags & COMMAND_KEY_MODIFIED || flags & KEYDEF_WRITE_ALL) { fprintf(f, "## %s\n", cmds[i].description); -- 2.30.2