source: rtems/cpukit/libmisc/shell/main_help.c @ 26e04e2f

4.115
Last change on this file since 26e04e2f was 26e04e2f, checked in by Sebastian Huber <sebastian.huber@…>, on 12/12/14 at 08:42:07

shell: Add 'all' topic for help for all commands

  • 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  const rtems_shell_cmd_t *shell_cmd
31)
32{
33  const char * pc;
34  int    col,line;
35
36  if (!rtems_shell_can_see_cmd(shell_cmd)) {
37    return 0;
38  }
39
40  printf("%-12.12s - ",shell_cmd->name);
41  col = 14;
42  line = 1;
43  if (shell_cmd->alias) {
44    printf("is an <alias> for command '%s'",shell_cmd->alias->name);
45  } else if (shell_cmd->usage) {
46    pc = shell_cmd->usage;
47    while (*pc) {
48      switch(*pc) {
49        case '\r':
50          break;
51        case '\n':
52          putchar('\n');
53          col = 0;
54          break;
55        default:
56          putchar(*pc);
57          col++;
58          break;
59      }
60      pc++;
61      if (col>78) { /* What daring... 78?*/
62        if (*pc) {
63          putchar('\n');
64          col = 0;
65        }
66      }
67      if (!col && *pc) {
68        printf("            ");
69        col = 12;line++;
70      }
71    }
72  }
73  puts("");
74  return line;
75}
76
77/*
78 * show the help. The first command implemented.
79 * Can you see the header of routine? Known?
80 * The same with all the commands....
81 */
82static int rtems_shell_help(
83  int argc,
84  char * argv[]
85)
86{
87  int col,line,lines,arg;
88  char* lines_env;
89  rtems_shell_topic_t *topic;
90
91  lines_env = getenv("SHELL_LINES");
92  if (lines_env)
93    lines = strtol(lines_env, 0, 0);
94  else
95    lines = 16;
96
97  if (argc<2) {
98    printf("help: The topics are\n");
99    topic = rtems_shell_first_topic;
100    col = printf("  all");
101    while (topic) {
102      if (!col){
103        col = printf("  %s",topic->topic);
104      } else {
105        if ((col+strlen(topic->topic)+2)>78){
106          printf("\n");
107          col = printf("  %s",topic->topic);
108        } else {
109          col+= printf(", %s",topic->topic);
110        }
111      }
112      topic = topic->next;
113    }
114    printf("\n");
115    return 1;
116  }
117  line = 0;
118  for (arg = 1;arg<argc;arg++) {
119    const char *cur = argv[arg];
120    rtems_shell_cmd_t *shell_cmd;
121
122    if (lines && (line > lines)) {
123      printf("Press any key to continue...");getchar();
124      printf("\n");
125      line = 0;
126    }
127    topic  =  rtems_shell_lookup_topic(cur);
128    if (topic == NULL) {
129      if ((shell_cmd = rtems_shell_lookup_cmd(cur)) == NULL) {
130        if (strcmp(cur, "all") != 0) {
131          printf(
132            "help: topic or cmd '%s' not found. Try <help> alone for a list\n",
133            cur
134          );
135          line++;
136          continue;
137        }
138      } else {
139        line+= rtems_shell_help_cmd(shell_cmd);
140        continue;
141      }
142    }
143    printf("help: list for the topic '%s'\n",cur);
144    line++;
145    shell_cmd = rtems_shell_first_cmd;
146    while (shell_cmd) {
147      if (topic == NULL || !strcmp(topic->topic,shell_cmd->topic))
148        line+= rtems_shell_help_cmd(shell_cmd);
149      if (lines && (line > lines)) {
150        printf("Press any key to continue...");
151        getchar();
152        printf("\n");
153        line = 0;
154      }
155      shell_cmd = shell_cmd->next;
156    }
157  }
158  puts("");
159  return 0;
160}
161
162rtems_shell_cmd_t rtems_shell_HELP_Command  =  {
163  .name = "help",
164  .usage = "help [topic] # list of usage of commands",
165  .topic = "help",
166  .command = rtems_shell_help,
167  .mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH
168};
Note: See TracBrowser for help on using the repository browser.