From dd44ae18b49f6cb54a4c361e9fab70f4d0fafeec Mon Sep 17 00:00:00 2001 From: Zachary T Welch Date: Tue, 17 Nov 2009 13:52:43 -0800 Subject: [PATCH] refactor handle_flash_bank_command Move variables to point of first use, reducing their scope. Add driver_name temporary to help arguments be changed later. Eliminates the useless 'found' variable, changing the code to terminate the loop immediate and return its success. --- src/flash/flash.c | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/flash/flash.c b/src/flash/flash.c index b960c64e..e93aa636 100644 --- a/src/flash/flash.c +++ b/src/flash/flash.c @@ -239,25 +239,24 @@ COMMAND_HELPER(flash_command_get_bank, unsigned name_index, COMMAND_HANDLER(handle_flash_bank_command) { - int retval; - int i; - int found = 0; - struct target *target; - if (CMD_ARGC < 6) { + LOG_ERROR("usage: flash bank " + " "); return ERROR_COMMAND_SYNTAX_ERROR; } + struct target *target; if ((target = get_target(CMD_ARGV[5])) == NULL) { LOG_ERROR("target '%s' not defined", CMD_ARGV[5]); return ERROR_FAIL; } - for (i = 0; flash_drivers[i]; i++) + const char *driver_name = CMD_ARGV[0]; + for (unsigned i = 0; flash_drivers[i]; i++) { - if (strcmp(CMD_ARGV[0], flash_drivers[i]->name) != 0) + if (strcmp(driver_name, flash_drivers[i]->name) != 0) continue; struct flash_bank *p, *c; @@ -265,7 +264,7 @@ COMMAND_HANDLER(handle_flash_bank_command) /* register flash specific commands */ if (flash_drivers[i]->register_commands(CMD_CTX) != ERROR_OK) { - LOG_ERROR("couldn't register '%s' commands", CMD_ARGV[0]); + LOG_ERROR("couldn't register '%s' commands", driver_name); return ERROR_FAIL; } @@ -281,10 +280,12 @@ COMMAND_HANDLER(handle_flash_bank_command) c->sectors = NULL; c->next = NULL; + int retval; retval = CALL_COMMAND_HANDLER(flash_drivers[i]->flash_bank_command, c); if (ERROR_OK != retval) { - LOG_ERROR("'%s' driver rejected flash bank at 0x%8.8" PRIx32 , CMD_ARGV[0], c->base); + LOG_ERROR("'%s' driver rejected flash bank at 0x%8.8" PRIx32, + driver_name, c->base); free(c); return retval; } @@ -305,17 +306,12 @@ COMMAND_HANDLER(handle_flash_bank_command) c->bank_number = 0; } - found = 1; + return ERROR_OK; } /* no matching flash driver found */ - if (!found) - { - LOG_ERROR("flash driver '%s' not found", CMD_ARGV[0]); - return ERROR_FAIL; - } - - return ERROR_OK; + LOG_ERROR("flash driver '%s' not found", driver_name); + return ERROR_FAIL; } COMMAND_HANDLER(handle_flash_info_command) -- 2.39.5