source: rtems/cpukit/libmisc/shell/shell_cmdset.c @ 55216fa

4.104.114.95
Last change on this file since 55216fa was 55216fa, checked in by Joel Sherrill <joel.sherrill@…>, on 12/17/07 at 22:39:29

2007-12-17 Joel Sherrill <joel.sherrill@…>

  • libmisc/shell/main_alias.c, libmisc/shell/main_blksync.c, libmisc/shell/main_cat.c, libmisc/shell/main_cd.c, libmisc/shell/main_chdir.c, libmisc/shell/main_chmod.c, libmisc/shell/main_chroot.c, libmisc/shell/main_cpuuse.c, libmisc/shell/main_date.c, libmisc/shell/main_dir.c, libmisc/shell/main_exit.c, libmisc/shell/main_help.c, libmisc/shell/main_id.c, libmisc/shell/main_logoff.c, libmisc/shell/main_ls.c, libmisc/shell/main_mallocinfo.c, libmisc/shell/main_mdump.c, libmisc/shell/main_medit.c, libmisc/shell/main_mfill.c, libmisc/shell/main_mkdir.c, libmisc/shell/main_mmove.c, libmisc/shell/main_mount.c, libmisc/shell/main_mount_ftp.c, libmisc/shell/main_mount_msdos.c, libmisc/shell/main_mount_nfs.c, libmisc/shell/main_mount_tftp.c, libmisc/shell/main_msdosfmt.c, libmisc/shell/main_mwdump.c, libmisc/shell/main_pwd.c, libmisc/shell/main_rm.c, libmisc/shell/main_rmdir.c, libmisc/shell/main_stackuse.c, libmisc/shell/main_tty.c, libmisc/shell/main_umask.c, libmisc/shell/main_unmount.c, libmisc/shell/main_whoami.c, libmisc/shell/shell_cmdset.c, libmisc/shell/shellconfig.h: Change rtems_Shell_ to rtems_shell_.
  • Property mode set to 100644
File size: 5.0 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.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
47rtems_shell_cmd_t   * rtems_shell_first_cmd;
48rtems_shell_topic_t * rtems_shell_first_topic;
49
50/*
51 *  Find the topic from the set of topics registered.
52 */
53rtems_shell_topic_t * rtems_shell_lookup_topic(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 */
68rtems_shell_topic_t * rtems_shell_add_topic(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 */
96rtems_shell_cmd_t * rtems_shell_lookup_cmd(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 */
109rtems_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 */
138rtems_shell_cmd_t * rtems_shell_add_cmd(
139  char                  *cmd,
140  char                  *topic,
141  char                  *usage,
142  rtems_shell_command_t  command
143)
144{
145  rtems_shell_cmd_t *shell_cmd;
146
147  if (!cmd)
148    return (rtems_shell_cmd_t *) NULL;
149  if (!command)
150    return (rtems_shell_cmd_t *) NULL;
151
152  shell_cmd          = (rtems_shell_cmd_t *) malloc(sizeof(rtems_shell_cmd_t));
153  shell_cmd->name    = strdup( cmd );
154  shell_cmd->topic   = strdup( topic );
155  shell_cmd->usage   = strdup( usage );
156  shell_cmd->command = command;
157  shell_cmd->alias   = (rtems_shell_cmd_t *) NULL;
158  shell_cmd->next    = (rtems_shell_cmd_t *) NULL;
159
160  if (rtems_shell_add_cmd_struct( shell_cmd ) == NULL) {
161    free( shell_cmd->usage );
162    free( shell_cmd->topic );
163    free( shell_cmd->name );
164    free( shell_cmd );
165    shell_cmd = NULL;
166  }
167
168  return shell_cmd;
169}
170
171
172void rtems_shell_initialize_command_set(void)
173{
174  rtems_shell_cmd_t **c;
175  rtems_shell_alias_t **a;
176
177  for ( c = rtems_shell_Initial_commands ; *c  ; c++ ) {
178    rtems_shell_add_cmd_struct( *c );
179  }
180
181  for ( a = rtems_shell_Initial_aliases ; *a  ; a++ ) {
182    rtems_shell_alias_cmd( (*a)->name, (*a)->alias );
183  }
184
185  rtems_shell_register_monitor_commands();
186}
187
188/* ----------------------------------------------- *
189 * you can make an alias for every command.
190 * ----------------------------------------------- */
191rtems_shell_cmd_t *rtems_shell_alias_cmd(
192  char *cmd,
193  char *alias
194)
195{
196  rtems_shell_cmd_t *shell_cmd, *shell_aux;
197
198  shell_aux = (rtems_shell_cmd_t *) NULL;
199
200  if (alias) {
201    shell_aux = rtems_shell_lookup_cmd(alias);
202    if (shell_aux != NULL) {
203      return NULL;
204    }
205    shell_cmd = rtems_shell_lookup_cmd(cmd);
206    if (shell_cmd != NULL) {
207      shell_aux = rtems_shell_add_cmd(
208         alias,
209         shell_cmd->topic,
210         shell_cmd->usage,
211         shell_cmd->command
212      );
213      if (shell_aux)
214        shell_aux->alias = shell_cmd;
215    }
216  }
217  return shell_aux;
218}
Note: See TracBrowser for help on using the repository browser.