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