Rev 517 | Rev 532 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 517 | Rev 518 | ||
|---|---|---|---|
| Line 24... | Line 24... | ||
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
27 | */ |
| 28 | 28 | ||
| 29 | #include <main/kconsole.h> |
29 | #include <console/kconsole.h> |
| 30 | #include <console/console.h> |
30 | #include <console/console.h> |
| 31 | #include <console/chardev.h> |
31 | #include <console/chardev.h> |
| 32 | #include <print.h> |
32 | #include <print.h> |
| 33 | #include <panic.h> |
33 | #include <panic.h> |
| 34 | #include <typedefs.h> |
34 | #include <typedefs.h> |
| 35 | #include <arch/types.h> |
35 | #include <arch/types.h> |
| 36 | #include <list.h> |
36 | #include <list.h> |
| 37 | #include <arch.h> |
37 | #include <arch.h> |
| 38 | #include <func.h> |
38 | #include <func.h> |
| 39 | #include <macros.h> |
39 | #include <macros.h> |
| - | 40 | #include <debug.h> |
|
| 40 | 41 | ||
| 41 | #define MAX_CMDLINE 256 |
42 | #define MAX_CMDLINE 256 |
| 42 | 43 | ||
| 43 | /** Simple kernel console. |
44 | /** Simple kernel console. |
| 44 | * |
45 | * |
| 45 | * The console is realized by kernel thread kconsole. |
46 | * The console is realized by kernel thread kconsole. |
| 46 | * It doesn't understand any commands on its own, but |
47 | * It doesn't understand any useful command on its own, |
| 47 | * makes it possible for other kernel subsystems to |
48 | * but makes it possible for other kernel subsystems to |
| 48 | * register their own commands. |
49 | * register their own commands. |
| 49 | */ |
50 | */ |
| 50 | 51 | ||
| 51 | /** Locking. |
52 | /** Locking. |
| 52 | * |
53 | * |
| Line 65... | Line 66... | ||
| 65 | 66 | ||
| 66 | spinlock_t cmd_lock; /**< Lock protecting command list. */ |
67 | spinlock_t cmd_lock; /**< Lock protecting command list. */ |
| 67 | link_t cmd_head; /**< Command list. */ |
68 | link_t cmd_head; /**< Command list. */ |
| 68 | 69 | ||
| 69 | static cmd_info_t *parse_cmdline(char *cmdline, size_t len); |
70 | static cmd_info_t *parse_cmdline(char *cmdline, size_t len); |
| 70 | static int cmd_help(cmd_arg_t *cmd); |
71 | static bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end); |
| 71 | 72 | ||
| - | 73 | /** Data and methods for 'help' command. */ |
|
| - | 74 | static int cmd_help(cmd_arg_t *argv); |
|
| 72 | static cmd_info_t help_info; |
75 | static cmd_info_t help_info; |
| 73 | 76 | ||
| - | 77 | /** Data and methods for 'description' command. */ |
|
| - | 78 | static int cmd_desc(cmd_arg_t *argv); |
|
| - | 79 | static void desc_help(void); |
|
| - | 80 | static cmd_info_t desc_info; |
|
| - | 81 | static char desc_buf[MAX_CMDLINE+1]; |
|
| - | 82 | static cmd_arg_t desc_argv = { |
|
| - | 83 | .type = ARG_TYPE_STRING, |
|
| - | 84 | .buffer = desc_buf, |
|
| - | 85 | .len = sizeof(desc_buf) |
|
| - | 86 | }; |
|
| - | 87 | ||
| 74 | /** Initialize kconsole data structures. */ |
88 | /** Initialize kconsole data structures. */ |
| 75 | void kconsole_init(void) |
89 | void kconsole_init(void) |
| 76 | { |
90 | { |
| 77 | spinlock_initialize(&cmd_lock); |
91 | spinlock_initialize(&cmd_lock); |
| 78 | list_initialize(&cmd_head); |
92 | list_initialize(&cmd_head); |
| 79 | 93 | ||
| 80 | help_info.name = "help"; |
94 | help_info.name = "help"; |
| 81 | help_info.description = "List supported commands."; |
95 | help_info.description = "List supported commands."; |
| 82 | help_info.func = cmd_help; |
96 | help_info.func = cmd_help; |
| - | 97 | help_info.help = NULL; |
|
| 83 | help_info.argc = 0; |
98 | help_info.argc = 0; |
| 84 | help_info.argv = NULL; |
99 | help_info.argv = NULL; |
| 85 | 100 | ||
| 86 | spinlock_initialize(&help_info.lock); |
101 | spinlock_initialize(&help_info.lock); |
| 87 | link_initialize(&help_info.link); |
102 | link_initialize(&help_info.link); |
| 88 | 103 | ||
| 89 | if (!cmd_register(&help_info)) |
104 | if (!cmd_register(&help_info)) |
| 90 | panic("could not register command\n"); |
105 | panic("could not register command %s\n", help_info.name); |
| - | 106 | ||
| - | 107 | ||
| - | 108 | desc_info.name = "describe"; |
|
| - | 109 | desc_info.description = "Describe specified command."; |
|
| - | 110 | desc_info.help = desc_help; |
|
| - | 111 | desc_info.func = cmd_desc; |
|
| - | 112 | desc_info.argc = 1; |
|
| - | 113 | desc_info.argv = &desc_argv; |
|
| - | 114 | ||
| - | 115 | spinlock_initialize(&desc_info.lock); |
|
| - | 116 | link_initialize(&desc_info.link); |
|
| - | 117 | ||
| - | 118 | if (!cmd_register(&desc_info)) |
|
| - | 119 | panic("could not register command %s\n", desc_info.name); |
|
| 91 | } |
120 | } |
| 92 | 121 | ||
| 93 | 122 | ||
| 94 | /** Register kconsole command. |
123 | /** Register kconsole command. |
| 95 | * |
124 | * |
| Line 127... | Line 156... | ||
| 127 | } else { |
156 | } else { |
| 128 | spinlock_lock(&cmd->lock); |
157 | spinlock_lock(&cmd->lock); |
| 129 | spinlock_lock(&hlp->lock); |
158 | spinlock_lock(&hlp->lock); |
| 130 | } |
159 | } |
| 131 | 160 | ||
| 132 | if ((strcmp(hlp->name, cmd->name, strlen(cmd->name)) == 0)) { |
161 | if ((strncmp(hlp->name, cmd->name, strlen(cmd->name)) == 0)) { |
| 133 | /* The command is already there. */ |
162 | /* The command is already there. */ |
| 134 | spinlock_unlock(&hlp->lock); |
163 | spinlock_unlock(&hlp->lock); |
| 135 | spinlock_unlock(&cmd->lock); |
164 | spinlock_unlock(&cmd->lock); |
| 136 | spinlock_unlock(&cmd_lock); |
165 | spinlock_unlock(&cmd_lock); |
| 137 | interrupts_restore(ipl); |
166 | interrupts_restore(ipl); |
| Line 167... | Line 196... | ||
| 167 | return; |
196 | return; |
| 168 | } |
197 | } |
| 169 | 198 | ||
| 170 | while (true) { |
199 | while (true) { |
| 171 | printf("%s> ", __FUNCTION__); |
200 | printf("%s> ", __FUNCTION__); |
| 172 | len = gets(stdin, cmdline, sizeof(cmdline)); |
201 | if (!(len = gets(stdin, cmdline, sizeof(cmdline)))) |
| - | 202 | continue; |
|
| 173 | cmdline[len] = '\0'; |
203 | cmdline[len] = '\0'; |
| 174 | cmd_info = parse_cmdline(cmdline, len); |
204 | cmd_info = parse_cmdline(cmdline, len); |
| 175 | if (!cmd_info) { |
205 | if (!cmd_info) |
| 176 | printf("?\n"); |
- | |
| 177 | continue; |
206 | continue; |
| 178 | } |
- | |
| 179 | (void) cmd_info->func(cmd_info->argv); |
207 | (void) cmd_info->func(cmd_info->argv); |
| 180 | } |
208 | } |
| 181 | } |
209 | } |
| 182 | 210 | ||
| 183 | /** Parse command line. |
211 | /** Parse command line. |
| Line 188... | Line 216... | ||
| 188 | * @return Structure describing the command. |
216 | * @return Structure describing the command. |
| 189 | */ |
217 | */ |
| 190 | cmd_info_t *parse_cmdline(char *cmdline, size_t len) |
218 | cmd_info_t *parse_cmdline(char *cmdline, size_t len) |
| 191 | { |
219 | { |
| 192 | index_t start = 0, end = 0; |
220 | index_t start = 0, end = 0; |
| 193 | bool found_start = false; |
- | |
| 194 | cmd_info_t *cmd = NULL; |
221 | cmd_info_t *cmd = NULL; |
| 195 | link_t *cur; |
222 | link_t *cur; |
| 196 | ipl_t ipl; |
223 | ipl_t ipl; |
| 197 | int i; |
224 | int i; |
| 198 | 225 | ||
| 199 | for (i = 0; i < len; i++) { |
- | |
| 200 | if (!found_start) { |
- | |
| 201 | if (is_white(cmdline[i])) |
226 | if (!parse_argument(cmdline, len, &start, &end)) { |
| 202 | start++; |
- | |
| 203 | else |
- | |
| 204 | found_start = true; |
- | |
| 205 | } else { |
- | |
| 206 | if (is_white(cmdline[i])) |
- | |
| 207 | break; |
- | |
| 208 | } |
- | |
| 209 | } |
- | |
| 210 | end = i - 1; |
- | |
| 211 | - | ||
| 212 | if (!found_start) { |
- | |
| 213 | /* Command line did not contain alphanumeric word. */ |
227 | /* Command line did not contain alphanumeric word. */ |
| 214 | return NULL; |
228 | return NULL; |
| 215 | } |
229 | } |
| 216 | 230 | ||
| 217 | ipl = interrupts_disable(); |
231 | ipl = interrupts_disable(); |
| Line 221... | Line 235... | ||
| 221 | cmd_info_t *hlp; |
235 | cmd_info_t *hlp; |
| 222 | 236 | ||
| 223 | hlp = list_get_instance(cur, cmd_info_t, link); |
237 | hlp = list_get_instance(cur, cmd_info_t, link); |
| 224 | spinlock_lock(&hlp->lock); |
238 | spinlock_lock(&hlp->lock); |
| 225 | 239 | ||
| 226 | if (strcmp(hlp->name, &cmdline[start], (end - start) + 1) == 0) { |
240 | if (strncmp(hlp->name, &cmdline[start], (end - start) + 1) == 0) { |
| 227 | cmd = hlp; |
241 | cmd = hlp; |
| 228 | break; |
242 | break; |
| 229 | } |
243 | } |
| 230 | 244 | ||
| 231 | spinlock_unlock(&hlp->lock); |
245 | spinlock_unlock(&hlp->lock); |
| Line 233... | Line 247... | ||
| 233 | 247 | ||
| 234 | spinlock_unlock(&cmd_lock); |
248 | spinlock_unlock(&cmd_lock); |
| 235 | 249 | ||
| 236 | if (!cmd) { |
250 | if (!cmd) { |
| 237 | /* Unknown command. */ |
251 | /* Unknown command. */ |
| - | 252 | printf("Unknown command.\n"); |
|
| 238 | interrupts_restore(ipl); |
253 | interrupts_restore(ipl); |
| 239 | return NULL; |
254 | return NULL; |
| 240 | } |
255 | } |
| 241 | 256 | ||
| 242 | /* cmd == hlp is locked */ |
257 | /* cmd == hlp is locked */ |
| 243 | 258 | ||
| 244 | /* |
259 | /* |
| 245 | * TODO: |
- | |
| 246 | * The command line must be further analyzed and |
260 | * The command line must be further analyzed and |
| 247 | * the parameters therefrom must be matched and |
261 | * the parameters therefrom must be matched and |
| 248 | * converted to those specified in the cmd info |
262 | * converted to those specified in the cmd info |
| 249 | * structure. |
263 | * structure. |
| 250 | */ |
264 | */ |
| - | 265 | ||
| - | 266 | for (i = 0; i < cmd->argc; i++) { |
|
| - | 267 | char *buf; |
|
| - | 268 | start = end + 1; |
|
| - | 269 | if (!parse_argument(cmdline, len, &start, &end)) { |
|
| - | 270 | printf("Too few arguments.\n"); |
|
| - | 271 | spinlock_unlock(&cmd->lock); |
|
| - | 272 | interrupts_restore(ipl); |
|
| - | 273 | return NULL; |
|
| - | 274 | } |
|
| - | 275 | ||
| - | 276 | switch (cmd->argv[i].type) { |
|
| - | 277 | case ARG_TYPE_STRING: |
|
| - | 278 | buf = cmd->argv[i].buffer; |
|
| - | 279 | strncpy(buf, (const char *) &cmdline[start], min((end - start) + 1, cmd->argv[i].len - 1)); |
|
| - | 280 | buf[min((end - start) + 1, cmd->argv[i].len - 1)] = '\0'; |
|
| - | 281 | break; |
|
| - | 282 | case ARG_TYPE_INT: |
|
| - | 283 | case ARG_TYPE_INVALID: |
|
| - | 284 | default: |
|
| - | 285 | panic("invalid argument type\n"); |
|
| - | 286 | break; |
|
| - | 287 | } |
|
| - | 288 | } |
|
| - | 289 | ||
| - | 290 | start = end + 1; |
|
| - | 291 | if (parse_argument(cmdline, len, &start, &end)) { |
|
| - | 292 | printf("Too many arguments.\n"); |
|
| - | 293 | spinlock_unlock(&cmd->lock); |
|
| - | 294 | interrupts_restore(ipl); |
|
| - | 295 | return NULL; |
|
| - | 296 | } |
|
| 251 | 297 | ||
| 252 | spinlock_unlock(&cmd->lock); |
298 | spinlock_unlock(&cmd->lock); |
| 253 | interrupts_restore(ipl); |
299 | interrupts_restore(ipl); |
| 254 | return cmd; |
300 | return cmd; |
| 255 | } |
301 | } |
| 256 | 302 | ||
| - | 303 | /** Parse argument. |
|
| - | 304 | * |
|
| - | 305 | * Find start and end positions of command line argument. |
|
| - | 306 | * |
|
| - | 307 | * @param cmdline Command line as read from the input device. |
|
| - | 308 | * @param len Number of characters in cmdline. |
|
| - | 309 | * @param start On entry, 'start' contains pointer to the index |
|
| - | 310 | * of first unprocessed character of cmdline. |
|
| - | 311 | * On successful exit, it marks beginning of the next argument. |
|
| - | 312 | * @param end Undefined on entry. On exit, 'end' points to the last character |
|
| - | 313 | * of the next argument. |
|
| - | 314 | * |
|
| - | 315 | * @return false on failure, true on success. |
|
| - | 316 | */ |
|
| - | 317 | bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end) |
|
| - | 318 | { |
|
| - | 319 | int i; |
|
| - | 320 | bool found_start = false; |
|
| - | 321 | ||
| - | 322 | ASSERT(start != NULL); |
|
| - | 323 | ASSERT(end != NULL); |
|
| - | 324 | ||
| - | 325 | for (i = *start; i < len; i++) { |
|
| - | 326 | if (!found_start) { |
|
| - | 327 | if (is_white(cmdline[i])) |
|
| - | 328 | (*start)++; |
|
| - | 329 | else |
|
| - | 330 | found_start = true; |
|
| - | 331 | } else { |
|
| - | 332 | if (is_white(cmdline[i])) |
|
| - | 333 | break; |
|
| - | 334 | } |
|
| - | 335 | } |
|
| - | 336 | *end = i - 1; |
|
| - | 337 | ||
| - | 338 | return found_start; |
|
| - | 339 | } |
|
| - | 340 | ||
| - | 341 | ||
| 257 | /** List supported commands. |
342 | /** List supported commands. |
| 258 | * |
343 | * |
| 259 | * @param cmd Argument vector. |
344 | * @param argv Argument vector. |
| 260 | * |
345 | * |
| 261 | * @return 0 on failure, 1 on success. |
346 | * @return 0 on failure, 1 on success. |
| 262 | */ |
347 | */ |
| 263 | int cmd_help(cmd_arg_t *cmd) |
348 | int cmd_help(cmd_arg_t *argv) |
| 264 | { |
349 | { |
| 265 | link_t *cur; |
350 | link_t *cur; |
| 266 | ipl_t ipl; |
351 | ipl_t ipl; |
| 267 | 352 | ||
| 268 | ipl = interrupts_disable(); |
353 | ipl = interrupts_disable(); |
| Line 272... | Line 357... | ||
| 272 | cmd_info_t *hlp; |
357 | cmd_info_t *hlp; |
| 273 | 358 | ||
| 274 | hlp = list_get_instance(cur, cmd_info_t, link); |
359 | hlp = list_get_instance(cur, cmd_info_t, link); |
| 275 | spinlock_lock(&hlp->lock); |
360 | spinlock_lock(&hlp->lock); |
| 276 | 361 | ||
| 277 | printf("%s\t%s\n", hlp->name, hlp->description); |
362 | printf("%s - %s\n", hlp->name, hlp->description); |
| 278 | 363 | ||
| 279 | spinlock_unlock(&hlp->lock); |
364 | spinlock_unlock(&hlp->lock); |
| 280 | } |
365 | } |
| 281 | 366 | ||
| 282 | spinlock_unlock(&cmd_lock); |
367 | spinlock_unlock(&cmd_lock); |
| 283 | interrupts_restore(ipl); |
368 | interrupts_restore(ipl); |
| 284 | 369 | ||
| 285 | return 1; |
370 | return 1; |
| 286 | } |
371 | } |
| - | 372 | ||
| - | 373 | /** Describe specified command. |
|
| - | 374 | * |
|
| - | 375 | * @param argv Argument vector. |
|
| - | 376 | * |
|
| - | 377 | * @return 0 on failure, 1 on success. |
|
| - | 378 | */ |
|
| - | 379 | int cmd_desc(cmd_arg_t *argv) |
|
| - | 380 | { |
|
| - | 381 | link_t *cur; |
|
| - | 382 | ipl_t ipl; |
|
| - | 383 | ||
| - | 384 | ipl = interrupts_disable(); |
|
| - | 385 | spinlock_lock(&cmd_lock); |
|
| - | 386 | ||
| - | 387 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
|
| - | 388 | cmd_info_t *hlp; |
|
| - | 389 | ||
| - | 390 | hlp = list_get_instance(cur, cmd_info_t, link); |
|
| - | 391 | spinlock_lock(&hlp->lock); |
|
| - | 392 | ||
| - | 393 | if (strncmp(hlp->name, (const char *) argv->buffer, strlen(hlp->name)) == 0) { |
|
| - | 394 | printf("%s - %s\n", hlp->name, hlp->description); |
|
| - | 395 | if (hlp->help) |
|
| - | 396 | hlp->help(); |
|
| - | 397 | spinlock_unlock(&hlp->lock); |
|
| - | 398 | break; |
|
| - | 399 | } |
|
| - | 400 | ||
| - | 401 | spinlock_unlock(&hlp->lock); |
|
| - | 402 | } |
|
| - | 403 | ||
| - | 404 | spinlock_unlock(&cmd_lock); |
|
| - | 405 | interrupts_restore(ipl); |
|
| - | 406 | ||
| - | 407 | return 1; |
|
| - | 408 | } |
|
| - | 409 | ||
| - | 410 | /** Print detailed description of 'describe' command. */ |
|
| - | 411 | void desc_help(void) |
|
| - | 412 | { |
|
| - | 413 | printf("Syntax: describe command_name\n"); |
|
| - | 414 | } |
|