source: rtems/cpukit/libmisc/shell/shell_cmdset.c @ 5c141d6a

4.115
Last change on this file since 5c141d6a was 5c141d6a, checked in by Sebastian Huber <sebastian.huber@…>, on Nov 18, 2014 at 9:36:27 AM

shell: Add and use rtems_shell_execute_cmd()

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