1 | /* |
---|
2 | * |
---|
3 | * Shell Command Set Management |
---|
4 | * |
---|
5 | * Author: |
---|
6 | * WORK: fernando.ruiz@ctv.es |
---|
7 | * HOME: correo@fernando-ruiz.com |
---|
8 | * |
---|
9 | * The license and distribution terms for this file may be |
---|
10 | * found in the file LICENSE in this distribution or at |
---|
11 | * http://www.rtems.com/license/LICENSE. |
---|
12 | * |
---|
13 | * $Id$ |
---|
14 | */ |
---|
15 | |
---|
16 | #ifdef HAVE_CONFIG_H |
---|
17 | #include "config.h" |
---|
18 | #endif |
---|
19 | |
---|
20 | #include <stdio.h> |
---|
21 | #include <time.h> |
---|
22 | #include <termios.h> |
---|
23 | #include <string.h> |
---|
24 | #include <stdlib.h> |
---|
25 | #include <ctype.h> |
---|
26 | #include <sys/stat.h> |
---|
27 | #include <unistd.h> |
---|
28 | #include <errno.h> |
---|
29 | |
---|
30 | |
---|
31 | #include <rtems.h> |
---|
32 | #include <rtems/shell.h> |
---|
33 | #include <rtems/shellconfig.h> |
---|
34 | #include "internal.h" |
---|
35 | |
---|
36 | /* |
---|
37 | * Common linked list of shell commands. |
---|
38 | * |
---|
39 | * Because the help report is very long, there is a topic for each command. |
---|
40 | * |
---|
41 | * Help list the topics |
---|
42 | * help [topic] list the commands for the topic |
---|
43 | * help [command] help for the command |
---|
44 | * |
---|
45 | */ |
---|
46 | |
---|
47 | rtems_shell_cmd_t * rtems_shell_first_cmd; |
---|
48 | rtems_shell_topic_t * rtems_shell_first_topic; |
---|
49 | |
---|
50 | /* |
---|
51 | * Find the topic from the set of topics registered. |
---|
52 | */ |
---|
53 | rtems_shell_topic_t * rtems_shell_lookup_topic(const char * topic) { |
---|
54 | rtems_shell_topic_t * shell_topic; |
---|
55 | shell_topic=rtems_shell_first_topic; |
---|
56 | |
---|
57 | while (shell_topic) { |
---|
58 | if (!strcmp(shell_topic->topic,topic)) |
---|
59 | return shell_topic; |
---|
60 | shell_topic=shell_topic->next; |
---|
61 | } |
---|
62 | return (rtems_shell_topic_t *) NULL; |
---|
63 | } |
---|
64 | |
---|
65 | /* |
---|
66 | * Add a new topic to the list of topics |
---|
67 | */ |
---|
68 | rtems_shell_topic_t * rtems_shell_add_topic(const char * topic) { |
---|
69 | rtems_shell_topic_t * current,*aux; |
---|
70 | |
---|
71 | if (!rtems_shell_first_topic) { |
---|
72 | aux = malloc(sizeof(rtems_shell_topic_t)); |
---|
73 | aux->topic = topic; |
---|
74 | aux->next = (rtems_shell_topic_t*)NULL; |
---|
75 | return rtems_shell_first_topic = aux; |
---|
76 | } |
---|
77 | current=rtems_shell_first_topic; |
---|
78 | if (!strcmp(topic,current->topic)) |
---|
79 | return current; |
---|
80 | |
---|
81 | while (current->next) { |
---|
82 | if (!strcmp(topic,current->next->topic)) |
---|
83 | return current->next; |
---|
84 | current=current->next; |
---|
85 | } |
---|
86 | aux = malloc(sizeof(rtems_shell_topic_t)); |
---|
87 | aux->topic = topic; |
---|
88 | aux->next = (rtems_shell_topic_t*)NULL; |
---|
89 | current->next = aux; |
---|
90 | return aux; |
---|
91 | } |
---|
92 | |
---|
93 | /* |
---|
94 | * Find the command in the set |
---|
95 | */ |
---|
96 | rtems_shell_cmd_t * rtems_shell_lookup_cmd(const char * cmd) { |
---|
97 | rtems_shell_cmd_t * shell_cmd; |
---|
98 | shell_cmd=rtems_shell_first_cmd; |
---|
99 | while (shell_cmd) { |
---|
100 | if (!strcmp(shell_cmd->name,cmd)) return shell_cmd; |
---|
101 | shell_cmd=shell_cmd->next; |
---|
102 | }; |
---|
103 | return (rtems_shell_cmd_t *) NULL; |
---|
104 | } |
---|
105 | |
---|
106 | /* |
---|
107 | * Add a command structure to the set of known commands |
---|
108 | */ |
---|
109 | rtems_shell_cmd_t *rtems_shell_add_cmd_struct( |
---|
110 | rtems_shell_cmd_t *shell_cmd |
---|
111 | ) |
---|
112 | { |
---|
113 | rtems_shell_cmd_t *shell_pvt; |
---|
114 | |
---|
115 | shell_pvt = rtems_shell_first_cmd; |
---|
116 | while (shell_pvt) { |
---|
117 | if (strcmp(shell_pvt->name, shell_cmd->name) == 0) |
---|
118 | return NULL; |
---|
119 | shell_pvt = shell_pvt->next; |
---|
120 | } |
---|
121 | |
---|
122 | if ( !rtems_shell_first_cmd ) { |
---|
123 | rtems_shell_first_cmd = shell_cmd; |
---|
124 | } else { |
---|
125 | shell_pvt = rtems_shell_first_cmd; |
---|
126 | while (shell_pvt->next) |
---|
127 | shell_pvt = shell_pvt->next; |
---|
128 | shell_pvt->next = shell_cmd; |
---|
129 | } |
---|
130 | rtems_shell_add_topic( shell_cmd->topic ); |
---|
131 | return shell_cmd; |
---|
132 | } |
---|
133 | |
---|
134 | /* |
---|
135 | * Add a command as a set of arguments to the set and |
---|
136 | * allocate the command structure on the fly. |
---|
137 | */ |
---|
138 | rtems_shell_cmd_t * rtems_shell_add_cmd( |
---|
139 | const char *name, |
---|
140 | const char *topic, |
---|
141 | const char *usage, |
---|
142 | rtems_shell_command_t command |
---|
143 | ) |
---|
144 | { |
---|
145 | rtems_shell_cmd_t *shell_cmd = NULL; |
---|
146 | char *my_name = NULL; |
---|
147 | char *my_topic = NULL; |
---|
148 | char *my_usage = NULL; |
---|
149 | |
---|
150 | /* Reject empty commands */ |
---|
151 | if (name == NULL || command == NULL) { |
---|
152 | return NULL; |
---|
153 | } |
---|
154 | |
---|
155 | /* Allocate command stucture */ |
---|
156 | shell_cmd = (rtems_shell_cmd_t *) malloc(sizeof(rtems_shell_cmd_t)); |
---|
157 | if (shell_cmd == NULL) { |
---|
158 | return NULL; |
---|
159 | } |
---|
160 | |
---|
161 | /* Allocate strings */ |
---|
162 | my_name = strdup(name); |
---|
163 | my_topic = strdup(topic); |
---|
164 | my_usage = strdup(usage); |
---|
165 | |
---|
166 | /* Assign values */ |
---|
167 | shell_cmd->name = my_name; |
---|
168 | shell_cmd->topic = my_topic; |
---|
169 | shell_cmd->usage = my_usage; |
---|
170 | shell_cmd->command = command; |
---|
171 | shell_cmd->alias = NULL; |
---|
172 | shell_cmd->next = NULL; |
---|
173 | |
---|
174 | if (rtems_shell_add_cmd_struct(shell_cmd) == NULL) { |
---|
175 | /* Something is wrong, free allocated resources */ |
---|
176 | free(my_usage); |
---|
177 | free(my_topic); |
---|
178 | free(my_name); |
---|
179 | free(shell_cmd); |
---|
180 | |
---|
181 | return NULL; |
---|
182 | } |
---|
183 | |
---|
184 | return shell_cmd; |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | void rtems_shell_initialize_command_set(void) |
---|
189 | { |
---|
190 | rtems_shell_cmd_t **c; |
---|
191 | rtems_shell_alias_t **a; |
---|
192 | |
---|
193 | for ( c = rtems_shell_Initial_commands ; *c ; c++ ) { |
---|
194 | rtems_shell_add_cmd_struct( *c ); |
---|
195 | } |
---|
196 | |
---|
197 | for ( a = rtems_shell_Initial_aliases ; *a ; a++ ) { |
---|
198 | rtems_shell_alias_cmd( (*a)->name, (*a)->alias ); |
---|
199 | } |
---|
200 | |
---|
201 | rtems_shell_register_monitor_commands(); |
---|
202 | } |
---|
203 | |
---|
204 | /* ----------------------------------------------- * |
---|
205 | * you can make an alias for every command. |
---|
206 | * ----------------------------------------------- */ |
---|
207 | rtems_shell_cmd_t *rtems_shell_alias_cmd( |
---|
208 | const char *cmd, |
---|
209 | const char *alias |
---|
210 | ) |
---|
211 | { |
---|
212 | rtems_shell_cmd_t *shell_cmd, *shell_aux; |
---|
213 | |
---|
214 | shell_aux = (rtems_shell_cmd_t *) NULL; |
---|
215 | |
---|
216 | if (alias) { |
---|
217 | shell_aux = rtems_shell_lookup_cmd(alias); |
---|
218 | if (shell_aux != NULL) { |
---|
219 | return NULL; |
---|
220 | } |
---|
221 | shell_cmd = rtems_shell_lookup_cmd(cmd); |
---|
222 | if (shell_cmd != NULL) { |
---|
223 | shell_aux = rtems_shell_add_cmd( |
---|
224 | alias, |
---|
225 | shell_cmd->topic, |
---|
226 | shell_cmd->usage, |
---|
227 | shell_cmd->command |
---|
228 | ); |
---|
229 | if (shell_aux) |
---|
230 | shell_aux->alias = shell_cmd; |
---|
231 | } |
---|
232 | } |
---|
233 | return shell_aux; |
---|
234 | } |
---|