OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [c/] [src/] [libmisc/] [monitor/] [mon-command.c] - Blame information for rev 279

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 30 unneback
/*
2
 * Command parsing routines for RTEMS monitor
3
 *
4
 * TODO:
5
 *
6
 *  $Id: mon-command.c,v 1.2 2001-09-27 12:01:43 chris Exp $
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
 
22
int
23
rtems_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
 
48
int
49
rtems_monitor_command_read(char *command,
50
                           int  *argc,
51
                           char **argv)
52
{
53
    static char monitor_prompt[32];
54
 
55
    /*
56
     * put node number in the prompt if we are multiprocessing
57
     */
58
 
59
    if (!rtems_configuration_get_user_multiprocessing_table())
60
        sprintf(monitor_prompt, "%s", MONITOR_PROMPT);
61
    else if (rtems_monitor_default_node != rtems_monitor_node)
62
        sprintf(monitor_prompt, "%d-%s-%d", rtems_monitor_node, MONITOR_PROMPT, rtems_monitor_default_node);
63
    else
64
        sprintf(monitor_prompt, "%d-%s", rtems_monitor_node, MONITOR_PROMPT);
65
 
66
#ifdef RTEMS_UNIX
67
    /* RTEMS on unix gets so many interrupt system calls this is hosed */
68
    printf("%s> ", monitor_prompt);
69
    fflush(stdout);
70
    while (gets(command) == (char *) 0)
71
        ;
72
#else
73
    do
74
    {
75
        printf("%s> ", monitor_prompt);
76
        fflush(stdout);
77
    } while (gets(command) == (char *) 0);
78
#endif
79
 
80
    return rtems_monitor_make_argv(command, argc, argv);
81
}
82
 
83
/*
84
 * Look up a command in a command table
85
 *
86
 */
87
 
88
rtems_monitor_command_entry_t *
89
rtems_monitor_command_lookup(
90
    rtems_monitor_command_entry_t *table,
91
    int                            argc,
92
    char                          **argv
93
)
94
{
95
    rtems_monitor_command_entry_t *p;
96
    rtems_monitor_command_entry_t *abbreviated_match = 0;
97
    int abbreviated_matches = 0;
98
    char *command;
99
    int command_length;
100
 
101
    command = argv[0];
102
 
103
    if ((table == 0) || (command == 0))
104
        goto failed;
105
 
106
    command_length = strlen(command);
107
 
108
    for (p = table; p->command; p++)
109
        if (STREQ(command, p->command))    /* exact match */
110
            goto done;
111
        else if (STRNEQ(command, p->command, command_length))
112
        {
113
            abbreviated_matches++;
114
            abbreviated_match = p;
115
        }
116
 
117
    /* no perfect match; is there a non-ambigous abbreviated match? */
118
    if ( ! abbreviated_match)
119
    {
120
        printf("Unrecognized command '%s'; try 'help'\n", command);
121
        goto failed;
122
    }
123
 
124
    if (abbreviated_matches > 1)
125
    {
126
        printf("Command '%s' is ambiguous; try 'help'\n", command);
127
        goto failed;
128
    }
129
 
130
    p = abbreviated_match;
131
 
132
done:
133
    if (p->command_function == 0)
134
        goto failed;
135
    return p;
136
 
137
failed:
138
    return 0;
139
}
140
 
141
void
142
rtems_monitor_command_usage(rtems_monitor_command_entry_t *table,
143
                            char *command_string)
144
{
145
    rtems_monitor_command_entry_t *help = 0;
146
    char *help_command_argv[2];
147
 
148
    /* if first entry in table is a usage, then print it out */
149
    if (command_string == 0)
150
    {
151
        if (STREQ(table->command, "--usage--") && table->usage)
152
            help = table;
153
    }
154
    else
155
    {
156
        help_command_argv[0] = command_string;
157
        help_command_argv[1] = 0;
158
        help = rtems_monitor_command_lookup(table, 1, help_command_argv);
159
    }
160
 
161
    if (help)
162
        printf("%s\n", help->usage);
163
}
164
 
165
 
166
void
167
rtems_monitor_help_cmd(
168
    int          argc,
169
    char       **argv,
170
    unsigned32   command_arg,
171
    boolean verbose
172
)
173
{
174
    int arg;
175
    rtems_monitor_command_entry_t *command;
176
 
177
    command = (rtems_monitor_command_entry_t *) command_arg;
178
 
179
    if (argc == 1)
180
        rtems_monitor_command_usage(command, 0);
181
    else
182
    {
183
        for (arg=1; argv[arg]; arg++)
184
            rtems_monitor_command_usage(command, argv[arg]);
185
    }
186
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.