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 | */ |
---|
29 | static 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 | */ |
---|
82 | static 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 | rtems_shell_cmd_t * shell_cmd = rtems_shell_first_cmd; |
---|
91 | |
---|
92 | lines_env = getenv("SHELL_LINES"); |
---|
93 | if (lines_env) |
---|
94 | lines = strtol(lines_env, 0, 0); |
---|
95 | else |
---|
96 | lines = 16; |
---|
97 | |
---|
98 | if (argc<2) { |
---|
99 | printf("help: ('r' repeat last cmd - 'e' edit last cmd)\n" |
---|
100 | " TOPIC? The topics are\n"); |
---|
101 | topic = rtems_shell_first_topic; |
---|
102 | col = 0; |
---|
103 | while (topic) { |
---|
104 | if (!col){ |
---|
105 | col = printf(" %s",topic->topic); |
---|
106 | } else { |
---|
107 | if ((col+strlen(topic->topic)+2)>78){ |
---|
108 | printf("\n"); |
---|
109 | col = printf(" %s",topic->topic); |
---|
110 | } else { |
---|
111 | col+= printf(", %s",topic->topic); |
---|
112 | } |
---|
113 | } |
---|
114 | topic = topic->next; |
---|
115 | } |
---|
116 | printf("\n"); |
---|
117 | return 1; |
---|
118 | } |
---|
119 | line = 0; |
---|
120 | for (arg = 1;arg<argc;arg++) { |
---|
121 | if (lines && (line > lines)) { |
---|
122 | printf("Press any key to continue...");getchar(); |
---|
123 | printf("\n"); |
---|
124 | line = 0; |
---|
125 | } |
---|
126 | topic = rtems_shell_lookup_topic(argv[arg]); |
---|
127 | if (!topic){ |
---|
128 | if ((shell_cmd = rtems_shell_lookup_cmd(argv[arg])) == NULL) { |
---|
129 | printf("help: topic or cmd '%s' not found. Try <help> alone for a list\n", |
---|
130 | argv[arg]); |
---|
131 | line++; |
---|
132 | } else { |
---|
133 | line+= rtems_shell_help_cmd(shell_cmd); |
---|
134 | } |
---|
135 | continue; |
---|
136 | } |
---|
137 | printf("help: list for the topic '%s'\n",argv[arg]); |
---|
138 | line++; |
---|
139 | while (shell_cmd) { |
---|
140 | if (!strcmp(topic->topic,shell_cmd->topic)) |
---|
141 | line+= rtems_shell_help_cmd(shell_cmd); |
---|
142 | if (lines && (line > lines)) { |
---|
143 | printf("Press any key to continue..."); |
---|
144 | getchar(); |
---|
145 | printf("\n"); |
---|
146 | line = 0; |
---|
147 | } |
---|
148 | shell_cmd = shell_cmd->next; |
---|
149 | } |
---|
150 | } |
---|
151 | puts(""); |
---|
152 | return 0; |
---|
153 | } |
---|
154 | |
---|
155 | rtems_shell_cmd_t rtems_shell_HELP_Command = { |
---|
156 | .name = "help", |
---|
157 | .usage = "help [topic] # list of usage of commands", |
---|
158 | .topic = "help", |
---|
159 | .command = rtems_shell_help, |
---|
160 | .mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH |
---|
161 | }; |
---|