summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGerrit <chrome-bot@google.com>2012-05-14 18:23:22 -0700
committerGerrit Code Review <gerrit@gerrit.golo.chromium.org>2012-05-14 18:23:22 -0700
commit35b1b7bf907ea9ffedc1c0134d5be9488ff1dd70 (patch)
treec6b3a1466f8820a19f6cdbc124b59d9c5dfce215
parentdc703b5ae48442f094dd8f3122e4507a8b043970 (diff)
parent336944e9519ee90a523398b497328d353f8cef4d (diff)
downloadchrome-ec-35b1b7bf907ea9ffedc1c0134d5be9488ff1dd70.tar.gz
Merge "Arrange help output to sort 'column first'"
-rw-r--r--common/console.c56
1 files 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;
-
- ccputs("Known commands:");
-
- /* 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;
+ const int ncmds = __cmds_end - __cmds;
+ int i, j, cols, rows;
+ unsigned char indices[ncmds];
+
+ /* Initialize the index. */
+ for (i = 0; i < ncmds; i++)
+ indices[i] = i;
+
+ /* 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 */
+ 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();
-
- prev = next;
}
- ccputs("\n");
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(help, command_help);