summaryrefslogtreecommitdiff
path: root/common/console.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/console.c')
-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);