summaryrefslogtreecommitdiffstats
path: root/cpukit/libmisc/monitor/mon-command.c
blob: e5ee6aee53bf1b3a9452e2a9e3d96b0af2e7b34c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
 * @file
 *
 * @brief Command support routines for RTEMS monitor.
 */

/*
 * $Id$
 *
 * 2001-01-30 KJO (vac4050@cae597.rsc.raytheon.com):
 *  Fixed rtems_monitor_command_lookup() to accept partial
 *  commands to uniqeness.  Added support for setting
 *  the monitor prompt via an environment variable:
 *  RTEMS_MONITOR_PROMPT
 *
 * CCJ: 26-3-2000, adding command history and command line
 * editing. This code is donated from My Right Boot and not
 * covered by GPL, only the RTEMS license.
 */

#include <string.h>
#include <stdio.h>

#include <rtems.h>
#include <rtems/monitor.h>

/*
 * Look up a command in a command table
 *
 */

const rtems_monitor_command_entry_t *rtems_monitor_command_lookup(
  const rtems_monitor_command_entry_t *table,
  const char *command_name
)
{
  const rtems_monitor_command_entry_t *found_it = NULL;
  size_t command_length = 0;

  if (command_name == NULL) {
    return NULL;
  }

  command_length = strlen(command_name);

  while (table != NULL) {
    if (table->command != NULL) {
      /* Check for ambiguity */
      if (!strncmp(table->command, command_name, command_length)) {
        if (found_it == NULL) {
          found_it = table;
        } else {
          return NULL;
        }
      }
    }
    table = table->next;
  }

  /* No ambiguity (the possible partial command was unique after all) */

  /* Ignore empty commands */
  if (found_it == NULL || found_it->command_function == NULL) {
    return NULL;
  }

  return found_it;
}

static void
rtems_monitor_show_help (
  const rtems_monitor_command_entry_t *help_cmd,
  int                           max_cmd_len
)
{
#define MAX_HELP_LINE_LENGTH (75 - max_cmd_len - 2)

  if (help_cmd && help_cmd->command)
  {
    const char *help = help_cmd->usage;
    int         help_len = strlen (help);
    int         spaces = max_cmd_len - strlen (help_cmd->command);
    int         show_this_line = 0;
    int         line_one = 1;
    int         c;

    fprintf(stdout,"%s", help_cmd->command);

    if (help_len == 0)
    {
      fprintf(stdout," - No help associated.\n");
      return;
    }

    while (help_len)
    {
      fprintf(stdout,"%*c", spaces, ' ');

      if (line_one)
        fprintf(stdout," - ");

      spaces   = max_cmd_len + 2;
      line_one = 0;

      /*
       * See if greater then the line length if so, work back
       * from the end for a space, tab or lf or cr.
       */

      if (help_len > MAX_HELP_LINE_LENGTH)
      {
        for (show_this_line = MAX_HELP_LINE_LENGTH - 1;
             show_this_line;
             show_this_line--)
          if ((help[show_this_line] == ' ') ||
              (help[show_this_line] == '\n') ||
              (help[show_this_line] == '\r'))
            break;

        /*
         * If show_this_line is 0, it is a very long word !!
         */

        if (show_this_line == 0)
          show_this_line = MAX_HELP_LINE_LENGTH - 1;
      }
      else
        show_this_line = help_len;

      for (c = 0; c < show_this_line; c++)
        if ((help[c] == '\r') || (help[c] == '\n'))
          show_this_line = c;
        else
          putchar (help[c]);

      fprintf(stdout,"\n");

      help     += show_this_line;
      help_len -= show_this_line;

      /*
       * Move past the line feeds or what ever else is being skipped.
       */

      while (help_len)
      {
        if ((*help != '\r') && (*help != '\n'))
          break;

        if (*help != ' ')
        {
          help++;
          help_len--;
          break;
        }
        help++;
        help_len--;
      }
    }
  }
}

void
rtems_monitor_command_usage(
  const rtems_monitor_command_entry_t *table,
  const char                          *command_name
)
{
  const rtems_monitor_command_entry_t *command = table;
  int                           max_cmd_len = 0;

  /* if first entry in table is a usage, then print it out */

  if (command_name && (*command_name != '\0'))
  {
    command = rtems_monitor_command_lookup (table, command_name);

    if (command)
      rtems_monitor_show_help (command, strlen (command_name));
    else
      fprintf(stdout,"Unrecognised command; try just 'help'\n");
    return;
  }

  /*
   * Find the largest command size.
   */

  while (command)
  {
    int len = command->command ? strlen (command->command) : 0 ;

    if (len > max_cmd_len)
      max_cmd_len = len;

    command = command->next;
  }

  max_cmd_len++;

  command = table;

  /*
   * Now some nice formatting for the help.
   */

  while (command)
  {
    rtems_monitor_show_help (command, max_cmd_len);
    command = command->next;
  }
}


void rtems_monitor_help_cmd(
  int                                argc,
  char                             **argv,
  const rtems_monitor_command_arg_t *command_arg,
  bool                               verbose
)
{
  int arg;
  const rtems_monitor_command_entry_t *command =
    command_arg->monitor_command_entry;

  if (argc == 1)
    rtems_monitor_command_usage(command, 0);
  else
  {
    for (arg = 1; argv[arg]; arg++)
      rtems_monitor_command_usage(command, argv[arg]);
  }
}