From 336944e9519ee90a523398b497328d353f8cef4d Mon Sep 17 00:00:00 2001 From: Vadim Bendebury Date: Mon, 14 May 2012 15:02:49 -0700 Subject: [PATCH] Arrange help output to sort 'column first' Sort the command name index, and then print them in file columns (as before), but ordering columns first. This makes it much easier to examine the 'help' command output. BUG=none TEST=manual . program the new image . execute the 'help' command . observe output printed sorted 'columns first' Change-Id: I7b15cef09a61fe20eb4ba5ee274ba1d72063f1c0 Signed-off-by: Vadim Bendebury --- common/console.c | 56 +++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/common/console.c b/common/console.c index cdb5daf5e6..3a57716db3 100644 --- a/common/console.c +++ b/common/console.c @@ -226,36 +226,42 @@ void console_task(void) /* Command handler - prints help. */ static int command_help(int argc, char **argv) { - const struct console_command *cmd; - const int ncmds = ((uint32_t)__cmds_end - (uint32_t)__cmds) / - sizeof(struct console_command); - const char *prev = " "; - int i; + const int ncmds = __cmds_end - __cmds; + int i, j, cols, rows; + unsigned char indices[ncmds]; - ccputs("Known commands:"); + /* Initialize the index. */ + for (i = 0; i < ncmds; i++) + indices[i] = i; - /* Sort the commands by name */ - for (i = 0; i < ncmds; i++) { - const char *next = "zzzz"; - - if (!(i % 5)) - ccputs("\n "); - - /* Find the next command */ - for (cmd = __cmds; cmd < __cmds_end; cmd++) { - if (strcasecmp(prev, cmd->name) < 0 && - strcasecmp(cmd->name, next) < 0) - next = cmd->name; + /* Bubble sort commands by name. */ + for (i = 0; i < (ncmds - 1); i++) { + for (j = i + 1; j < ncmds; j++) { + if (strcasecmp(__cmds[indices[i]].name, + __cmds[indices[j]].name) > 0) { + int tmp = indices[j]; + indices[j] = indices[i]; + indices[i] = tmp; + } } - - ccprintf("%-15s", next); - /* Generates enough output to overflow the buffer */ - cflush(); - - prev = next; } - ccputs("\n"); + ccputs("Known commands:\n"); + + cols = 5; /* printing in five columns */ + rows = (ncmds + 1) / cols; + for (i = 0; i < rows; i++) { + ccputs(" "); + for (j = 0; j < cols; j++) { + int index = j * rows + i; + if (index >= ncmds) + break; + ccprintf("%-15s", __cmds[indices[index]].name); + } + ccputs("\n"); + cflush(); + } + return EC_SUCCESS; } DECLARE_CONSOLE_COMMAND(help, command_help);