source: rtems/cpukit/libmisc/shell/main_help.c @ c499856

4.115
Last change on this file since c499856 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 3.6 KB
Line 
1/*
2 *
3 *  Shell Help Command
4 *
5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
7 *  http://www.rtems.org/license/LICENSE.
8 */
9
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
14#include <stdio.h>
15#include <stdlib.h>
16#include <time.h>
17
18#include <rtems.h>
19#include <rtems/error.h>
20#include <rtems/system.h>
21#include <rtems/shell.h>
22
23#include "internal.h"
24#include <string.h>
25
26/*
27 * show the help for one command.
28 */
29static int rtems_shell_help_cmd(
30  rtems_shell_cmd_t *shell_cmd
31)
32{
33  const char * pc;
34  int    col,line;
35
36  printf("%-12.12s - ",shell_cmd->name);
37  col = 14;
38  line = 1;
39  if (shell_cmd->alias) {
40    printf("is an <alias> for command '%s'",shell_cmd->alias->name);
41  } else if (shell_cmd->usage) {
42    pc = shell_cmd->usage;
43    while (*pc) {
44      switch(*pc) {
45        case '\r':
46          break;
47        case '\n':
48          putchar('\n');
49          col = 0;
50          break;
51        default:
52          putchar(*pc);
53          col++;
54          break;
55      }
56      pc++;
57      if (col>78) { /* What daring... 78?*/
58        if (*pc) {
59          putchar('\n');
60          col = 0;
61        }
62      }
63      if (!col && *pc) {
64        printf("            ");
65        col = 12;line++;
66      }
67    }
68  }
69  puts("");
70  return line;
71}
72
73/*
74 * show the help. The first command implemented.
75 * Can you see the header of routine? Known?
76 * The same with all the commands....
77 */
78static int rtems_shell_help(
79  int argc,
80  char * argv[]
81)
82{
83  int col,line,lines,arg;
84  char* lines_env;
85  rtems_shell_topic_t *topic;
86  rtems_shell_cmd_t * shell_cmd = rtems_shell_first_cmd;
87
88  lines_env = getenv("SHELL_LINES");
89  if (lines_env)
90    lines = strtol(lines_env, 0, 0);
91  else
92    lines = 16;
93
94  if (argc<2) {
95    printf("help: ('r' repeat last cmd - 'e' edit last cmd)\n"
96           "  TOPIC? The topics are\n");
97    topic = rtems_shell_first_topic;
98    col = 0;
99    while (topic) {
100      if (!col){
101        col = printf("   %s",topic->topic);
102      } else {
103        if ((col+strlen(topic->topic)+2)>78){
104          printf("\n");
105          col = printf("   %s",topic->topic);
106        } else {
107          col+= printf(", %s",topic->topic);
108        }
109      }
110      topic = topic->next;
111    }
112    printf("\n");
113    return 1;
114  }
115  line = 0;
116  for (arg = 1;arg<argc;arg++) {
117    if (lines && (line > lines)) {
118      printf("Press any key to continue...");getchar();
119      printf("\n");
120      line = 0;
121    }
122    topic  =  rtems_shell_lookup_topic(argv[arg]);
123    if (!topic){
124      if ((shell_cmd = rtems_shell_lookup_cmd(argv[arg])) == NULL) {
125        printf("help: topic or cmd '%s' not found. Try <help> alone for a list\n",
126            argv[arg]);
127        line++;
128      } else {
129        line+= rtems_shell_help_cmd(shell_cmd);
130      }
131      continue;
132    }
133    printf("help: list for the topic '%s'\n",argv[arg]);
134    line++;
135    while (shell_cmd) {
136      if (!strcmp(topic->topic,shell_cmd->topic))
137        line+= rtems_shell_help_cmd(shell_cmd);
138      if (lines && (line > lines)) {
139        printf("Press any key to continue...");
140        getchar();
141        printf("\n");
142        line = 0;
143      }
144      shell_cmd = shell_cmd->next;
145    }
146  }
147  puts("");
148  return 0;
149}
150
151rtems_shell_cmd_t rtems_shell_HELP_Command  =  {
152  "help",                                       /* name  */
153   "help [topic] # list of usage of commands",  /* usage */
154  "help",                                       /* topic */
155  rtems_shell_help,                             /* command */
156  NULL,                                         /* alias */
157  NULL                                          /* next */
158};
Note: See TracBrowser for help on using the repository browser.