Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:19

0001 /*
0002  *
0003  *  Shell Help Command
0004  *
0005  *  The license and distribution terms for this file may be
0006  *  found in the file LICENSE in this distribution or at
0007  *  http://www.rtems.org/license/LICENSE.
0008  */
0009 
0010 #ifdef HAVE_CONFIG_H
0011 #include "config.h"
0012 #endif
0013 
0014 #include <stdio.h>
0015 #include <stdlib.h>
0016 #include <time.h>
0017 
0018 #include <rtems.h>
0019 #include <rtems/error.h>
0020 #include <rtems/shell.h>
0021 
0022 #include "internal.h"
0023 #include <string.h>
0024 
0025 static int rtems_shell_help_pause(int line, int lines) {
0026   if (lines && line >= lines - 1) {
0027     printf("\rPress any key to continue...");
0028     (void) getchar();
0029     printf("\r%*c\r", 29, ' ');
0030     line = 0;
0031   }
0032   return line;
0033 }
0034 
0035 /*
0036  * show the help for one command.
0037  */
0038 static int rtems_shell_help_cmd(
0039   const rtems_shell_cmd_t *shell_cmd, int indent, int line,
0040   int cols, int lines
0041 )
0042 {
0043   const char * pc;
0044   int    col;
0045 
0046   if (!rtems_shell_can_see_cmd(shell_cmd)) {
0047     return 0;
0048   }
0049 
0050   printf("%-*s - ", indent, shell_cmd->name);
0051   indent += 3;
0052   col = indent;
0053   if (shell_cmd->alias) {
0054     printf("is an <alias> for command '%s'",shell_cmd->alias->name);
0055   } else if (shell_cmd->usage) {
0056     pc = shell_cmd->usage;
0057     while (*pc) {
0058       switch(*pc) {
0059         case '\r':
0060           break;
0061         case '\n':
0062           if (*(pc + 1) != '\0') {
0063             putchar('\n');
0064             col = 0;
0065           }
0066           break;
0067         default:
0068           putchar(*pc);
0069           col++;
0070           break;
0071       }
0072       pc++;
0073       if (col > (cols - 3)) {
0074         if (*pc) {
0075           putchar('\n');
0076           col = 0;
0077         }
0078       }
0079       if (col == 0 && *pc) {
0080         line = rtems_shell_help_pause(line + 1, lines);
0081         printf("%*c", indent, ' ');
0082         col = indent;
0083       }
0084     }
0085   }
0086   puts("");
0087   line = rtems_shell_help_pause(line + 1, lines);
0088   return line;
0089 }
0090 
0091 /*
0092  * show the help. The first command implemented.
0093  * Can you see the header of routine? Known?
0094  * The same with all the commands....
0095  */
0096 static int rtems_shell_help(
0097   int argc,
0098   char * argv[]
0099 )
0100 {
0101   int col,line,cols,lines,arg,indent;
0102   char *lines_env, *cols_env;
0103   rtems_shell_topic_t *topic;
0104   rtems_shell_cmd_t *shell_cmd;
0105 
0106   lines = 16;
0107   cols = 80;
0108   lines_env = getenv("SHELL_LINES");
0109   if (lines_env) {
0110     lines = strtol(lines_env, 0, 0);
0111   } else {
0112     lines_env = getenv("LINES");
0113     if (lines_env) {
0114       lines = strtol(lines_env, 0, 0);
0115     }
0116   }
0117 
0118   cols_env = getenv("COLUMNS");
0119   if (cols_env) {
0120     cols = strtol(cols_env, 0, 0);
0121   }
0122 
0123   if (argc<2) {
0124     printf("help: The topics are\n");
0125     topic = rtems_shell_first_topic;
0126     col = printf("  all");
0127     while (topic) {
0128       if (!col){
0129         col = printf("  %s",topic->topic);
0130       } else {
0131         if ((col+strlen(topic->topic)+2)>(cols - 2)){
0132           printf("\n");
0133           col = printf("  %s",topic->topic);
0134         } else {
0135           col+= printf(", %s",topic->topic);
0136         }
0137       }
0138       topic = topic->next;
0139     }
0140     printf("\n");
0141     return 1;
0142   }
0143   indent = 0;
0144   shell_cmd = rtems_shell_first_cmd;
0145   while (shell_cmd) {
0146     size_t len = strlen(shell_cmd->name);
0147     if (len > indent) {
0148       indent = len;
0149     }
0150     shell_cmd = shell_cmd->next;
0151   }
0152   line = 0;
0153   for (arg = 1;arg<argc;arg++) {
0154     const char *cur = argv[arg];
0155     topic = rtems_shell_lookup_topic(cur);
0156     if (topic == NULL) {
0157       if ((shell_cmd = rtems_shell_lookup_cmd(cur)) == NULL) {
0158         if (strcmp(cur, "all") != 0) {
0159           printf(
0160             "help: topic or cmd '%s' not found. Try <help> alone for a list\n",
0161             cur
0162           );
0163           line = rtems_shell_help_pause(line + 1, lines);
0164           continue;
0165         }
0166       } else {
0167         line = rtems_shell_help_cmd(shell_cmd, indent, line, cols, lines);
0168         continue;
0169       }
0170     }
0171     printf("help: list for the topic '%s'\n",cur);
0172     line++;
0173     shell_cmd = rtems_shell_first_cmd;
0174     while (shell_cmd) {
0175       if (topic == NULL || !strcmp(topic->topic,shell_cmd->topic)) {
0176         line = rtems_shell_help_cmd(shell_cmd, indent, line, cols, lines);
0177       }
0178       shell_cmd = shell_cmd->next;
0179     }
0180   }
0181   return 0;
0182 }
0183 
0184 rtems_shell_cmd_t rtems_shell_HELP_Command  =  {
0185   .name = "help",
0186   .usage = "help [topic] # list of usage of commands",
0187   .topic = "help",
0188   .command = rtems_shell_help,
0189   .mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH
0190 };