Re: [PATCH] libmisc/shell: Support terminal size as env variables

2022-11-22 Thread Chris Johns
On 23/11/22 12:44 am, Joel Sherrill wrote:
> On Tue, Nov 22, 2022, 6:02 AM mailto:chr...@rtems.org>> 
> wrote:
> From: Chris Johns mailto:chr...@rtems.org>>
> 
> Closes #4763
> ---
>  cpukit/libmisc/shell/main_edit.c | 17 +-
>  cpukit/libmisc/shell/main_help.c | 94 
>  cpukit/libmisc/shell/shell.c     | 83 +++-
>  3 files changed, 154 insertions(+), 40 deletions(-)
> 
> diff --git a/cpukit/libmisc/shell/main_edit.c 
> b/cpukit/libmisc/shell/main_edit.c
> index 4cc742719a..16c44f2a11 100644
> --- a/cpukit/libmisc/shell/main_edit.c
> +++ b/cpukit/libmisc/shell/main_edit.c
> @@ -755,8 +755,21 @@ static void get_console_size(struct env *env) {
>    env->cols = ws.ws_col;
>    env->lines = ws.ws_row - 1;
>  #elif defined(__rtems__)
> -  env->cols = 80;
> -  env->lines = 25;
> +  char* e;
> +  e = getenv("LINES");
> +  if (e != NULL) {
> +    int lines = strtol(e, 0, 10);
> +    if (lines > 0) {
> +      env->lines = lines - 1;
> +    }
> +  }
> +  e = getenv("COLUMNS");
> +  if (e != NULL) {
> +    int cols = strtol(e, 0, 10);
> +    if (cols > 0) {
> +      env->cols = cols - 1;
> +    }
> +  }
> 
> Does this lose the default values if the environment variables are not set?
> 

Yes. I will add the defaults back and then push.

Thanks for the review.

Chris
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel

Re: [PATCH] libmisc/shell: Support terminal size as env variables

2022-11-22 Thread Joel Sherrill
On Tue, Nov 22, 2022, 6:02 AM  wrote:

> From: Chris Johns 
>
> Closes #4763
> ---
>  cpukit/libmisc/shell/main_edit.c | 17 +-
>  cpukit/libmisc/shell/main_help.c | 94 
>  cpukit/libmisc/shell/shell.c | 83 +++-
>  3 files changed, 154 insertions(+), 40 deletions(-)
>
> diff --git a/cpukit/libmisc/shell/main_edit.c
> b/cpukit/libmisc/shell/main_edit.c
> index 4cc742719a..16c44f2a11 100644
> --- a/cpukit/libmisc/shell/main_edit.c
> +++ b/cpukit/libmisc/shell/main_edit.c
> @@ -755,8 +755,21 @@ static void get_console_size(struct env *env) {
>env->cols = ws.ws_col;
>env->lines = ws.ws_row - 1;
>  #elif defined(__rtems__)
> -  env->cols = 80;
> -  env->lines = 25;
> +  char* e;
> +  e = getenv("LINES");
> +  if (e != NULL) {
> +int lines = strtol(e, 0, 10);
> +if (lines > 0) {
> +  env->lines = lines - 1;
> +}
> +  }
> +  e = getenv("COLUMNS");
> +  if (e != NULL) {
> +int cols = strtol(e, 0, 10);
> +if (cols > 0) {
> +  env->cols = cols - 1;
> +}
> +  }
>

Does this lose the default values if the environment variables are not set?

 #else
>struct term *term = gettib()->proc->term;
>env->cols = term->cols;
> 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 
>
> +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;
> -  intcol,line;
> +  intcol;
>
>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  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  const char *cur = argv[arg];
> -rtems_shell_cmd_t *shell_cmd;
> -
> -if (lines && (line > lines)) {
> -  printf("Press any key to c

[PATCH] libmisc/shell: Support terminal size as env variables

2022-11-22 Thread chrisj
From: Chris Johns 

Closes #4763
---
 cpukit/libmisc/shell/main_edit.c | 17 +-
 cpukit/libmisc/shell/main_help.c | 94 
 cpukit/libmisc/shell/shell.c | 83 +++-
 3 files changed, 154 insertions(+), 40 deletions(-)

diff --git a/cpukit/libmisc/shell/main_edit.c b/cpukit/libmisc/shell/main_edit.c
index 4cc742719a..16c44f2a11 100644
--- a/cpukit/libmisc/shell/main_edit.c
+++ b/cpukit/libmisc/shell/main_edit.c
@@ -755,8 +755,21 @@ static void get_console_size(struct env *env) {
   env->cols = ws.ws_col;
   env->lines = ws.ws_row - 1;
 #elif defined(__rtems__)
-  env->cols = 80;
-  env->lines = 25;
+  char* e;
+  e = getenv("LINES");
+  if (e != NULL) {
+int lines = strtol(e, 0, 10);
+if (lines > 0) {
+  env->lines = lines - 1;
+}
+  }
+  e = getenv("COLUMNS");
+  if (e != NULL) {
+int cols = strtol(e, 0, 10);
+if (cols > 0) {
+  env->cols = cols - 1;
+}
+  }
 #else
   struct term *term = gettib()->proc->term;
   env->cols = term->cols;
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 
 
+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;
-  intcol,line;
+  intcol;
 
   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  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 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  alone for a list\n",
 cur
   );
-  line++;
+  line = rtems_shell_help_