source: rtems/cpukit/libmisc/monitor/mon-command.c @ f97536d

5
Last change on this file since f97536d was f97536d, checked in by Sebastian Huber <sebastian.huber@…>, on 10/16/15 at 06:21:48

basdefs.h: Add and use RTEMS_UNUSED

  • Property mode set to 100644
File size: 5.2 KB
Line 
1/**
2 * @file
3 *
4 * @brief Command support routines for RTEMS monitor.
5 */
6
7/*
8 * 2001-01-30 KJO (vac4050@cae597.rsc.raytheon.com):
9 *  Fixed rtems_monitor_command_lookup() to accept partial
10 *  commands to uniqeness.  Added support for setting
11 *  the monitor prompt via an environment variable:
12 *  RTEMS_MONITOR_PROMPT
13 *
14 * CCJ: 26-3-2000, adding command history and command line
15 * editing. This code is donated from My Right Boot and not
16 * covered by GPL, only the RTEMS license.
17 */
18
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#include <string.h>
24#include <stdio.h>
25
26#include <rtems.h>
27#include <rtems/monitor.h>
28
29static void
30rtems_monitor_show_help (
31  const rtems_monitor_command_entry_t *help_cmd,
32  int                           max_cmd_len
33)
34{
35#define MAX_HELP_LINE_LENGTH (75 - max_cmd_len - 2)
36
37  if (help_cmd && help_cmd->command)
38  {
39    const char *help = help_cmd->usage;
40    int         help_len = strlen (help);
41    int         spaces = max_cmd_len - strlen (help_cmd->command);
42    int         show_this_line = 0;
43    int         line_one = 1;
44    int         c;
45
46    fprintf(stdout,"%s", help_cmd->command);
47
48    if (help_len == 0)
49    {
50      fprintf(stdout," - No help associated.\n");
51      return;
52    }
53
54    while (help_len)
55    {
56      fprintf(stdout,"%*c", spaces, ' ');
57
58      if (line_one)
59        fprintf(stdout," - ");
60
61      spaces   = max_cmd_len + 2;
62      line_one = 0;
63
64      /*
65       * See if greater then the line length if so, work back
66       * from the end for a space, tab or lf or cr.
67       */
68
69      if (help_len > MAX_HELP_LINE_LENGTH)
70      {
71        for (show_this_line = MAX_HELP_LINE_LENGTH - 1;
72             show_this_line;
73             show_this_line--)
74          if ((help[show_this_line] == ' ') ||
75              (help[show_this_line] == '\n') ||
76              (help[show_this_line] == '\r'))
77            break;
78
79        /*
80         * If show_this_line is 0, it is a very long word !!
81         */
82
83        if (show_this_line == 0)
84          show_this_line = MAX_HELP_LINE_LENGTH - 1;
85      }
86      else
87        show_this_line = help_len;
88
89      for (c = 0; c < show_this_line; c++)
90        if ((help[c] == '\r') || (help[c] == '\n'))
91          show_this_line = c;
92        else
93          putchar (help[c]);
94
95      fprintf(stdout,"\n");
96
97      help     += show_this_line;
98      help_len -= show_this_line;
99
100      /*
101       * Move past the line feeds or what ever else is being skipped.
102       */
103
104      while (help_len)
105      {
106        if ((*help != '\r') && (*help != '\n'))
107          break;
108
109        if (*help != ' ')
110        {
111          help++;
112          help_len--;
113          break;
114        }
115        help++;
116        help_len--;
117      }
118    }
119  }
120}
121
122void
123rtems_monitor_command_usage(
124  const rtems_monitor_command_entry_t *table,
125  const char                          *command_name
126)
127{
128  const rtems_monitor_command_entry_t *command = table;
129  int                           max_cmd_len = 0;
130
131  /* if first entry in table is a usage, then print it out */
132
133  if (command_name && (*command_name != '\0'))
134  {
135    command = rtems_monitor_command_lookup (command_name);
136
137    if (command)
138      rtems_monitor_show_help (command, strlen (command_name));
139    else
140      fprintf(stdout,"Unrecognised command; try just 'help'\n");
141    return;
142  }
143
144  /*
145   * Find the largest command size.
146   */
147
148  while (command)
149  {
150    int len = command->command ? strlen (command->command) : 0 ;
151
152    if (len > max_cmd_len)
153      max_cmd_len = len;
154
155    command = command->next;
156  }
157
158  max_cmd_len++;
159
160  command = table;
161
162  /*
163   * Now some nice formatting for the help.
164   */
165
166  while (command)
167  {
168    rtems_monitor_show_help (command, max_cmd_len);
169    command = command->next;
170  }
171}
172
173
174void rtems_monitor_help_cmd(
175  int                                argc,
176  char                             **argv,
177  const rtems_monitor_command_arg_t *command_arg,
178  bool                               verbose RTEMS_UNUSED
179)
180{
181  int arg;
182  const rtems_monitor_command_entry_t *command =
183    command_arg->monitor_command_entry;
184
185  if (argc == 1)
186    rtems_monitor_command_usage(command, 0);
187  else
188  {
189    for (arg = 1; argv[arg]; arg++)
190      rtems_monitor_command_usage(command, argv[arg]);
191  }
192}
193
194typedef struct {
195  const char *name;
196  size_t length;
197  const rtems_monitor_command_entry_t *match;
198} rtems_monitor_command_lookup_entry;
199
200static bool rtems_monitor_command_lookup_routine(
201  const rtems_monitor_command_entry_t *e,
202  void *arg
203)
204{
205  rtems_monitor_command_lookup_entry *le =
206    (rtems_monitor_command_lookup_entry *) arg;
207
208  /* Check name */
209  if (strncmp(e->command, le->name, le->length) == 0) {
210    /* Check for ambiguity */
211    if (le->match == NULL) {
212      le->match = e;
213    } else {
214      return false;
215    }
216  }
217
218  return true;
219}
220
221/**
222 * @brief Looks for a command with the name @a name in the list of registered
223 * commands.
224 *
225 * The parameter @a name must not be NULL.
226 *
227 * Returns the corresponding command entry or NULL if no command is found.
228 */
229const rtems_monitor_command_entry_t *rtems_monitor_command_lookup(
230  const char *name
231)
232{
233  rtems_monitor_command_lookup_entry e = {
234    .name = name,
235    .length = strlen( name),
236    .match = NULL
237  };
238
239  rtems_monitor_command_iterate(rtems_monitor_command_lookup_routine, &e);
240
241  return e.match;
242}
Note: See TracBrowser for help on using the repository browser.