diff options
author | Chris Johns <chrisj@rtems.org> | 2022-11-22 21:05:48 +1100 |
---|---|---|
committer | Chris Johns <chrisj@rtems.org> | 2022-11-23 07:00:08 +1100 |
commit | 8425e679c149096a5d0a97990f6ebdbdd55ca522 (patch) | |
tree | ddde07991b57375146e3a0f77d6e71b3f0ebf324 /cpukit/libmisc/shell/main_help.c | |
parent | aarch64/versal: Add UART interrupt support (diff) | |
download | rtems-8425e679c149096a5d0a97990f6ebdbdd55ca522.tar.bz2 |
libmisc/shell: Support terminal size as env variables
Closes #4763
Diffstat (limited to 'cpukit/libmisc/shell/main_help.c')
-rw-r--r-- | cpukit/libmisc/shell/main_help.c | 94 |
1 files changed, 58 insertions, 36 deletions
diff --git a/cpukit/libmisc/shell/main_help.c b/cpukit/libmisc/shell/main_help.c index 564bc30a9c..e6d939d08f 100644 --- a/cpukit/libmisc/shell/main_help.c +++ b/cpukit/libmisc/shell/main_help.c @@ -22,23 +22,34 @@ #include "internal.h" #include <string.h> +static int rtems_shell_help_pause(int line, int lines) { + if (lines && line >= lines - 1) { + printf("\rPress any key to continue..."); + (void) getchar(); + printf("\r%*c\r", 29, ' '); + line = 0; + } + return line; +} + /* * show the help for one command. */ static int rtems_shell_help_cmd( - const rtems_shell_cmd_t *shell_cmd + const rtems_shell_cmd_t *shell_cmd, int indent, int line, + int cols, int lines ) { const char * pc; - int col,line; + int col; if (!rtems_shell_can_see_cmd(shell_cmd)) { return 0; } - printf("%-12.12s - ",shell_cmd->name); - col = 14; - line = 1; + printf("%-*s - ", indent, shell_cmd->name); + indent += 3; + col = indent; if (shell_cmd->alias) { printf("is an <alias> for command '%s'",shell_cmd->alias->name); } else if (shell_cmd->usage) { @@ -48,8 +59,10 @@ static int rtems_shell_help_cmd( case '\r': break; case '\n': - putchar('\n'); - col = 0; + if (*(pc + 1) != '\0') { + putchar('\n'); + col = 0; + } break; default: putchar(*pc); @@ -57,19 +70,21 @@ static int rtems_shell_help_cmd( break; } pc++; - if (col>78) { /* What daring... 78?*/ + if (col > (cols - 3)) { if (*pc) { putchar('\n'); col = 0; } } - if (!col && *pc) { - printf(" "); - col = 12;line++; + if (col == 0 && *pc) { + line = rtems_shell_help_pause(line + 1, lines); + printf("%*c", indent, ' '); + col = indent; } } } puts(""); + line = rtems_shell_help_pause(line + 1, lines); return line; } @@ -83,15 +98,27 @@ static int rtems_shell_help( char * argv[] ) { - int col,line,lines,arg; - char* lines_env; + int col,line,cols,lines,arg,indent; + char *lines_env, *cols_env; rtems_shell_topic_t *topic; + rtems_shell_cmd_t *shell_cmd; + lines = 16; + cols = 80; lines_env = getenv("SHELL_LINES"); - if (lines_env) + if (lines_env) { lines = strtol(lines_env, 0, 0); - else - lines = 16; + } else { + lines_env = getenv("LINES"); + if (lines_env) { + lines = strtol(lines_env, 0, 0); + } + } + + cols_env = getenv("COLUMNS"); + if (cols_env) { + cols = strtol(cols_env, 0, 0); + } if (argc<2) { printf("help: The topics are\n"); @@ -101,7 +128,7 @@ static int rtems_shell_help( if (!col){ col = printf(" %s",topic->topic); } else { - if ((col+strlen(topic->topic)+2)>78){ + if ((col+strlen(topic->topic)+2)>(cols - 2)){ printf("\n"); col = printf(" %s",topic->topic); } else { @@ -113,18 +140,19 @@ static int rtems_shell_help( printf("\n"); return 1; } + indent = 0; + shell_cmd = rtems_shell_first_cmd; + while (shell_cmd) { + size_t len = strlen(shell_cmd->name); + if (len > indent) { + indent = len; + } + shell_cmd = shell_cmd->next; + } line = 0; for (arg = 1;arg<argc;arg++) { const char *cur = argv[arg]; - rtems_shell_cmd_t *shell_cmd; - - if (lines && (line > lines)) { - printf("Press any key to continue..."); - (void) getchar(); /* we only want to know a character was pressed */ - printf("\n"); - line = 0; - } - topic = rtems_shell_lookup_topic(cur); + topic = rtems_shell_lookup_topic(cur); if (topic == NULL) { if ((shell_cmd = rtems_shell_lookup_cmd(cur)) == NULL) { if (strcmp(cur, "all") != 0) { @@ -132,11 +160,11 @@ static int rtems_shell_help( "help: topic or cmd '%s' not found. Try <help> alone for a list\n", cur ); - line++; + line = rtems_shell_help_pause(line + 1, lines); continue; } } else { - line+= rtems_shell_help_cmd(shell_cmd); + line = rtems_shell_help_cmd(shell_cmd, indent, line, cols, lines); continue; } } @@ -144,18 +172,12 @@ static int rtems_shell_help( line++; shell_cmd = rtems_shell_first_cmd; while (shell_cmd) { - if (topic == NULL || !strcmp(topic->topic,shell_cmd->topic)) - line+= rtems_shell_help_cmd(shell_cmd); - if (lines && (line > lines)) { - printf("Press any key to continue..."); - (void) getchar(); - printf("\n"); - line = 0; + if (topic == NULL || !strcmp(topic->topic,shell_cmd->topic)) { + line = rtems_shell_help_cmd(shell_cmd, indent, line, cols, lines); } shell_cmd = shell_cmd->next; } } - puts(""); return 0; } |