source: rtems/c/src/lib/libmisc/monitor/mon-command.c @ 1587af6

4.104.114.84.95
Last change on this file since 1587af6 was 5beb562, checked in by Joel Sherrill <joel.sherrill@…>, on 09/21/97 at 16:58:57

Cleaned up as part of adding the Monitor test.

  • Property mode set to 100644
File size: 4.1 KB
Line 
1/*
2 * Command parsing routines for RTEMS monitor
3 *
4 * TODO:
5 *
6 *  $Id$
7 */
8
9#include <rtems.h>
10
11#include <rtems/monitor.h>
12
13#include <stdio.h>
14#include <string.h>
15
16/*
17 * make_argv(cp): token-count
18 *      Break up the command line in 'cp' into global argv[] and argc (return
19 *      value).
20 */
21
22int
23rtems_monitor_make_argv(
24    char *cp,
25    int  *argc_p,
26    char **argv)
27{
28    int argc = 0;
29
30    while ((cp = strtok(cp, " \t\n\r")))
31    {
32        argv[argc++] = cp;
33        cp = (char *) NULL;
34    }
35    argv[argc] = (char *) NULL;                 /* end of argv */
36
37    return *argc_p = argc;
38}
39
40
41/*
42 * Read and break up a monitor command
43 *
44 * We have to loop on the gets call, since it will return NULL under UNIX
45 *  RTEMS when we get a signal (eg: SIGALRM).
46 */
47
48int
49rtems_monitor_command_read(char *command,
50                           int  *argc,
51                           char **argv)
52{
53    extern rtems_configuration_table  BSP_Configuration;
54    static char monitor_prompt[32];
55   
56    /*
57     * put node number in the prompt if we are multiprocessing
58     */
59
60    if (BSP_Configuration.User_multiprocessing_table == 0)
61        sprintf(monitor_prompt, "%s", MONITOR_PROMPT);
62    else if (rtems_monitor_default_node != rtems_monitor_node)
63        sprintf(monitor_prompt, "%d-%s-%d", rtems_monitor_node, MONITOR_PROMPT, rtems_monitor_default_node);
64    else
65        sprintf(monitor_prompt, "%d-%s", rtems_monitor_node, MONITOR_PROMPT);
66
67#ifdef RTEMS_UNIX
68    /* RTEMS on unix gets so many interrupt system calls this is hosed */
69    printf("%s> ", monitor_prompt);
70    fflush(stdout);
71    while (gets(command) == (char *) 0)
72        ;
73#else
74    do
75    {
76        printf("%s> ", monitor_prompt);
77        fflush(stdout);
78    } while (gets(command) == (char *) 0);
79#endif
80
81    return rtems_monitor_make_argv(command, argc, argv);
82}
83
84/*
85 * Look up a command in a command table
86 *
87 */
88
89rtems_monitor_command_entry_t *
90rtems_monitor_command_lookup(
91    rtems_monitor_command_entry_t *table,
92    int                            argc,
93    char                          **argv
94)
95{
96    rtems_monitor_command_entry_t *p;
97    rtems_monitor_command_entry_t *abbreviated_match = 0;
98    int abbreviated_matches = 0;
99    char *command;
100    int command_length;
101
102    command = argv[0];
103
104    if ((table == 0) || (command == 0))
105        goto failed;
106   
107    command_length = strlen(command);
108
109    for (p = table; p->command; p++)
110        if (STREQ(command, p->command))    /* exact match */
111            goto done;
112        else if (STRNEQ(command, p->command, command_length))
113        {
114            abbreviated_matches++;
115            abbreviated_match = p;
116        }
117
118    /* no perfect match; is there a non-ambigous abbreviated match? */
119    if ( ! abbreviated_match)
120    {
121        printf("Unrecognized command '%s'; try 'help'\n", command);
122        goto failed;
123    }   
124
125    if (abbreviated_matches > 1)
126    {
127        printf("Command '%s' is ambiguous; try 'help'\n", command);
128        goto failed;
129    }
130   
131    p = abbreviated_match;
132
133done:
134    if (p->command_function == 0)
135        goto failed;
136    return p;
137
138failed:
139    return 0;
140}
141
142void
143rtems_monitor_command_usage(rtems_monitor_command_entry_t *table,
144                            char *command_string)
145{
146    rtems_monitor_command_entry_t *help = 0;
147    char *help_command_argv[2];
148   
149    /* if first entry in table is a usage, then print it out */
150    if (command_string == 0)
151    {       
152        if (STREQ(table->command, "--usage--") && table->usage)
153            help = table;
154    }
155    else
156    {
157        help_command_argv[0] = command_string;
158        help_command_argv[1] = 0;
159        help = rtems_monitor_command_lookup(table, 1, help_command_argv);
160    }
161
162    if (help)
163        printf("%s\n", help->usage);
164}
165
166
167void
168rtems_monitor_help_cmd(
169    int          argc,
170    char       **argv,
171    unsigned32   command_arg,
172    boolean verbose
173)
174{
175    int arg;
176    rtems_monitor_command_entry_t *command;
177
178    command = (rtems_monitor_command_entry_t *) command_arg;
179   
180    if (argc == 1)
181        rtems_monitor_command_usage(command, 0);
182    else
183    {
184        for (arg=1; argv[arg]; arg++)
185            rtems_monitor_command_usage(command, argv[arg]);
186    }
187}
Note: See TracBrowser for help on using the repository browser.