source: rtems-schedsim/schedsim/shell/shared/shell_cmdset.c @ abb18dc

base initial
Last change on this file since abb18dc was abb18dc, checked in by Joel Sherrill <joel.sherrill@…>, on 04/25/11 at 15:53:10

Initial import.

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