Subversion Repositories HelenOS

Rev

Rev 1708 | Rev 1787 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
510 jermar 1
/*
2
 * Copyright (C) 2005 Jakub Jermar
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
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
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
1702 cejka 29
 /** @addtogroup genericconsole
30
 * @{
31
 */
32
 
1264 jermar 33
/**
34
 * @file    kconsole.c
35
 * @brief   Kernel console.
36
 *
37
 * This file contains kernel thread managing the kernel console.
38
 */
39
 
518 jermar 40
#include <console/kconsole.h>
510 jermar 41
#include <console/console.h>
42
#include <console/chardev.h>
596 jermar 43
#include <console/cmd.h>
510 jermar 44
#include <print.h>
517 jermar 45
#include <panic.h>
510 jermar 46
#include <typedefs.h>
47
#include <arch/types.h>
788 jermar 48
#include <adt/list.h>
517 jermar 49
#include <arch.h>
50
#include <macros.h>
518 jermar 51
#include <debug.h>
596 jermar 52
#include <func.h>
582 palkovsky 53
#include <symtab.h>
609 palkovsky 54
#include <macros.h>
510 jermar 55
 
517 jermar 56
/** Simple kernel console.
57
 *
58
 * The console is realized by kernel thread kconsole.
518 jermar 59
 * It doesn't understand any useful command on its own,
60
 * but makes it possible for other kernel subsystems to
517 jermar 61
 * register their own commands.
62
 */
63
 
64
/** Locking.
65
 *
66
 * There is a list of cmd_info_t structures. This list
67
 * is protected by cmd_lock spinlock. Note that specially
68
 * the link elements of cmd_info_t are protected by
69
 * this lock.
70
 *
71
 * Each cmd_info_t also has its own lock, which protects
72
 * all elements thereof except the link element.
73
 *
74
 * cmd_lock must be acquired before any cmd_info lock.
75
 * When locking two cmd info structures, structure with
76
 * lower address must be locked first.
77
 */
78
 
623 jermar 79
SPINLOCK_INITIALIZE(cmd_lock);  /**< Lock protecting command list. */
624 jermar 80
LIST_INITIALIZE(cmd_head);  /**< Command list. */
517 jermar 81
 
82
static cmd_info_t *parse_cmdline(char *cmdline, size_t len);
518 jermar 83
static bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end);
601 palkovsky 84
static char history[KCONSOLE_HISTORY][MAX_CMDLINE] = {};
517 jermar 85
 
86
/** Initialize kconsole data structures. */
87
void kconsole_init(void)
88
{
601 palkovsky 89
    int i;
90
 
596 jermar 91
    cmd_init();
601 palkovsky 92
    for (i=0; i<KCONSOLE_HISTORY; i++)
93
        history[i][0] = '\0';
517 jermar 94
}
95
 
96
 
97
/** Register kconsole command.
98
 *
99
 * @param cmd Structure describing the command.
100
 *
101
 * @return 0 on failure, 1 on success.
102
 */
103
int cmd_register(cmd_info_t *cmd)
104
{
105
    link_t *cur;
106
 
107
    spinlock_lock(&cmd_lock);
108
 
109
    /*
110
     * Make sure the command is not already listed.
111
     */
112
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
113
        cmd_info_t *hlp;
114
 
115
        hlp = list_get_instance(cur, cmd_info_t, link);
116
 
117
        if (hlp == cmd) {
118
            /* The command is already there. */
119
            spinlock_unlock(&cmd_lock);
120
            return 0;
121
        }
122
 
123
        /* Avoid deadlock. */
124
        if (hlp < cmd) {
125
            spinlock_lock(&hlp->lock);
126
            spinlock_lock(&cmd->lock);
127
        } else {
128
            spinlock_lock(&cmd->lock);
129
            spinlock_lock(&hlp->lock);
130
        }
635 palkovsky 131
        if ((strncmp(hlp->name,
132
                 cmd->name, max(strlen(cmd->name),
133
                        strlen(hlp->name))) == 0)) {
517 jermar 134
            /* The command is already there. */
135
            spinlock_unlock(&hlp->lock);
136
            spinlock_unlock(&cmd->lock);
137
            spinlock_unlock(&cmd_lock);
138
            return 0;
139
        }
140
 
141
        spinlock_unlock(&hlp->lock);
142
        spinlock_unlock(&cmd->lock);
143
    }
144
 
145
    /*
146
     * Now the command can be added.
147
     */
148
    list_append(&cmd->link, &cmd_head);
149
 
150
    spinlock_unlock(&cmd_lock);
151
    return 1;
152
}
153
 
635 palkovsky 154
/** Print count times a character */
601 palkovsky 155
static void rdln_print_c(char ch, int count)
156
{
157
    int i;
158
    for (i=0;i<count;i++)
159
        putchar(ch);
160
}
161
 
635 palkovsky 162
/** Insert character to string */
601 palkovsky 163
static void insert_char(char *str, char ch, int pos)
164
{
165
    int i;
166
 
167
    for (i=strlen(str);i > pos; i--)
168
        str[i] = str[i-1];
169
    str[pos] = ch;
170
}
171
 
640 jermar 172
/** Try to find a command beginning with prefix */
601 palkovsky 173
static const char * cmdtab_search_one(const char *name,link_t **startpos)
174
{
175
    int namelen = strlen(name);
176
    const char *curname;
177
 
178
    spinlock_lock(&cmd_lock);
179
 
180
    if (!*startpos)
181
        *startpos = cmd_head.next;
182
 
183
    for (;*startpos != &cmd_head;*startpos = (*startpos)->next) {
184
        cmd_info_t *hlp;
185
        hlp = list_get_instance(*startpos, cmd_info_t, link);
186
 
187
        curname = hlp->name;
188
        if (strlen(curname) < namelen)
189
            continue;
190
        if (strncmp(curname, name, namelen) == 0) {
191
            spinlock_unlock(&cmd_lock);
192
            return curname+namelen;
193
        }
194
    }
195
    spinlock_unlock(&cmd_lock);
196
    return NULL;
197
}
198
 
199
 
200
/** Command completion of the commands
201
 *
202
 * @param name - string to match, changed to hint on exit
203
 * @return number of found matches
204
 */
205
static int cmdtab_compl(char *name)
206
{
207
    char output[MAX_SYMBOL_NAME+1];
208
    link_t *startpos = NULL;
209
    const char *foundtxt;
210
    int found = 0;
211
    int i;
212
 
213
    output[0] = '\0';
214
    while ((foundtxt = cmdtab_search_one(name, &startpos))) {
215
        startpos = startpos->next;
216
        if (!found)
217
            strncpy(output, foundtxt, strlen(foundtxt)+1);
218
        else {
219
            for (i=0; output[i] && foundtxt[i] && output[i]==foundtxt[i]; i++)
220
                ;
221
            output[i] = '\0';
222
        }
223
        found++;
224
    }
225
    if (!found)
226
        return 0;
227
 
602 palkovsky 228
    if (found > 1 && !strlen(output)) {
601 palkovsky 229
        printf("\n");
230
        startpos = NULL;
231
        while ((foundtxt = cmdtab_search_one(name, &startpos))) {
232
            cmd_info_t *hlp;
233
            hlp = list_get_instance(startpos, cmd_info_t, link);
234
            printf("%s - %s\n", hlp->name, hlp->description);
235
            startpos = startpos->next;
236
        }
237
    }
238
    strncpy(name, output, MAX_SYMBOL_NAME);
239
    return found;
240
 
241
}
242
 
243
static char * clever_readline(const char *prompt, chardev_t *input)
244
{
245
    static int histposition = 0;
246
 
247
    char tmp[MAX_CMDLINE+1];
248
    int curlen = 0, position = 0;
249
    char *current = history[histposition];
250
    int i;
606 palkovsky 251
    char mod; /* Command Modifier */
601 palkovsky 252
    char c;
253
 
254
    printf("%s> ", prompt);
255
    while (1) {
256
        c = _getc(input);
257
        if (c == '\n') {
258
            putchar(c);
259
            break;
607 palkovsky 260
        } if (c == '\b') { /* Backspace */
601 palkovsky 261
            if (position == 0)
262
                continue;
263
            for (i=position; i<curlen;i++)
264
                current[i-1] = current[i];
265
            curlen--;
266
            position--;
267
            putchar('\b');
268
            for (i=position;i<curlen;i++)
269
                putchar(current[i]);
270
            putchar(' ');
271
            rdln_print_c('\b',curlen-position+1);
272
            continue;
273
        }
607 palkovsky 274
        if (c == '\t') { /* Tabulator */
601 palkovsky 275
            int found;
276
 
277
            /* Move to the end of the word */
278
            for (;position<curlen && current[position]!=' ';position++)
279
                putchar(current[position]);
280
            /* Copy to tmp last word */
281
            for (i=position-1;i >= 0 && current[i]!=' ' ;i--)
282
                ;
283
            /* If word begins with * or &, skip it */
284
            if (tmp[0] == '*' || tmp[0] == '&')
285
                for (i=1;tmp[i];i++)
286
                    tmp[i-1] = tmp[i];
287
            i++; /* I is at the start of the word */
288
            strncpy(tmp, current+i, position-i+1);
289
 
290
            if (i==0) { /* Command completion */
291
                found = cmdtab_compl(tmp);
292
            } else { /* Symtab completion */
293
                found = symtab_compl(tmp);
294
            }
295
 
296
            if (found == 0)
297
                continue;
298
            for (i=0;tmp[i] && curlen < MAX_CMDLINE;i++,curlen++)
299
                insert_char(current, tmp[i], i+position);
602 palkovsky 300
 
301
            if (strlen(tmp) || found==1) { /* If we have a hint */
302
                for (i=position;i<curlen;i++)
601 palkovsky 303
                    putchar(current[i]);
304
                position += strlen(tmp);
305
                /* Add space to end */
602 palkovsky 306
                if (found == 1 && position == curlen && \
307
                    curlen < MAX_CMDLINE) {
601 palkovsky 308
                    current[position] = ' ';
309
                    curlen++;
310
                    position++;
311
                    putchar(' ');
312
                }
602 palkovsky 313
            } else { /* No hint, table was printed */
601 palkovsky 314
                printf("%s> ", prompt);
315
                for (i=0; i<curlen;i++)
316
                    putchar(current[i]);
317
                position += strlen(tmp);
318
            }
319
            rdln_print_c('\b', curlen-position);
320
            continue;
321
        }
607 palkovsky 322
        if (c == 0x1b) { /* Special command */
606 palkovsky 323
            mod = _getc(input);
601 palkovsky 324
            c = _getc(input);
606 palkovsky 325
 
326
            if (mod != 0x5b && mod != 0x4f)
601 palkovsky 327
                continue;
606 palkovsky 328
 
329
            if (c == 0x33 && _getc(input) == 0x7e) {
607 palkovsky 330
                /* Delete */
606 palkovsky 331
                if (position == curlen)
332
                    continue;
333
                for (i=position+1; i<curlen;i++) {
334
                    putchar(current[i]);
335
                    current[i-1] = current[i];
336
                }
337
                putchar(' ');
338
                rdln_print_c('\b',curlen-position);
339
                curlen--;
340
            }
341
            else if (c == 0x48) { /* Home */
342
                rdln_print_c('\b',position);
343
                position = 0;
344
            }
607 palkovsky 345
            else if (c == 0x46) {  /* End */
606 palkovsky 346
                for (i=position;i<curlen;i++)
347
                    putchar(current[i]);
348
                position = curlen;
349
            }
350
            else if (c == 0x44) { /* Left */
601 palkovsky 351
                if (position > 0) {
352
                    putchar('\b');
353
                    position--;
354
                }
355
                continue;
356
            }
606 palkovsky 357
            else if (c == 0x43) { /* Right */
601 palkovsky 358
                if (position < curlen) {
359
                    putchar(current[position]);
360
                    position++;
361
                }
362
                continue;
363
            }
606 palkovsky 364
            else if (c == 0x41 || c == 0x42) {
365
                                /* Up,down */
601 palkovsky 366
                rdln_print_c('\b',position);
367
                rdln_print_c(' ',curlen);
368
                rdln_print_c('\b',curlen);
607 palkovsky 369
                if (c == 0x41) /* Up */
601 palkovsky 370
                    histposition--;
371
                else
372
                    histposition++;
373
                if (histposition < 0)
374
                    histposition = KCONSOLE_HISTORY -1 ;
375
                else
376
                    histposition =  histposition % KCONSOLE_HISTORY;
377
                current = history[histposition];
378
                printf("%s", current);
379
                curlen = strlen(current);
380
                position = curlen;
381
                continue;
382
            }
383
            continue;
384
        }
385
        if (curlen >= MAX_CMDLINE)
386
            continue;
387
 
388
        insert_char(current, c, position);
389
 
390
        curlen++;
391
        for (i=position;i<curlen;i++)
392
            putchar(current[i]);
393
        position++;
394
        rdln_print_c('\b',curlen-position);
395
    }
603 palkovsky 396
    if (curlen) {
397
        histposition++;
398
        histposition = histposition % KCONSOLE_HISTORY;
399
    }
601 palkovsky 400
    current[curlen] = '\0';
401
    return current;
402
}
403
 
510 jermar 404
/** Kernel console managing thread.
405
 *
1708 jermar 406
 * @param prompt Kernel console prompt (e.g kconsole/panic).
510 jermar 407
 */
609 palkovsky 408
void kconsole(void *prompt)
510 jermar 409
{
517 jermar 410
    cmd_info_t *cmd_info;
411
    count_t len;
601 palkovsky 412
    char *cmdline;
510 jermar 413
 
414
    if (!stdin) {
415
        printf("%s: no stdin\n", __FUNCTION__);
416
        return;
417
    }
418
 
419
    while (true) {
609 palkovsky 420
        cmdline = clever_readline(prompt, stdin);
601 palkovsky 421
        len = strlen(cmdline);
422
        if (!len)
518 jermar 423
            continue;
517 jermar 424
        cmd_info = parse_cmdline(cmdline, len);
518 jermar 425
        if (!cmd_info)
517 jermar 426
            continue;
609 palkovsky 427
        if (strncmp(cmd_info->name,"exit", \
428
                min(strlen(cmd_info->name),5)) == 0)
429
            break;
517 jermar 430
        (void) cmd_info->func(cmd_info->argv);
510 jermar 431
    }
432
}
517 jermar 433
 
1780 jermar 434
static int parse_int_arg(char *text, size_t len, unative_t *result)
585 palkovsky 435
{
436
    char symname[MAX_SYMBOL_NAME];
1780 jermar 437
    uintptr_t symaddr;
585 palkovsky 438
    bool isaddr = false;
589 palkovsky 439
    bool isptr = false;
585 palkovsky 440
 
441
    /* If we get a name, try to find it in symbol table */
931 palkovsky 442
    if (text[0] == '&') {
443
        isaddr = true;
444
        text++;len--;
445
    } else if (text[0] == '*') {
446
        isptr = true;
447
        text++;len--;
448
    }
614 palkovsky 449
    if (text[0] < '0' || text[0] > '9') {
585 palkovsky 450
        strncpy(symname, text, min(len+1, MAX_SYMBOL_NAME));
451
        symaddr = get_symbol_addr(symname);
452
        if (!symaddr) {
453
            printf("Symbol %s not found.\n",symname);
454
            return -1;
455
        }
1780 jermar 456
        if (symaddr == (uintptr_t) -1) {
585 palkovsky 457
            printf("Duplicate symbol %s.\n",symname);
458
            symtab_print_search(symname);
459
            return -1;
460
        }
932 palkovsky 461
        if (isaddr)
1780 jermar 462
            *result = (unative_t)symaddr;
932 palkovsky 463
        else if (isptr)
1780 jermar 464
            *result = **((unative_t **)symaddr);
932 palkovsky 465
        else
1780 jermar 466
            *result = *((unative_t *)symaddr);
932 palkovsky 467
    } else { /* It's a number - convert it */
585 palkovsky 468
        *result = atoi(text);
932 palkovsky 469
        if (isptr)
1780 jermar 470
            *result = *((unative_t *)*result);
932 palkovsky 471
    }
931 palkovsky 472
 
585 palkovsky 473
    return 0;
474
}
475
 
517 jermar 476
/** Parse command line.
477
 *
478
 * @param cmdline Command line as read from input device.
479
 * @param len Command line length.
480
 *
481
 * @return Structure describing the command.
482
 */
483
cmd_info_t *parse_cmdline(char *cmdline, size_t len)
484
{
485
    index_t start = 0, end = 0;
486
    cmd_info_t *cmd = NULL;
487
    link_t *cur;
488
    int i;
668 bondari 489
    int error = 0;
517 jermar 490
 
518 jermar 491
    if (!parse_argument(cmdline, len, &start, &end)) {
517 jermar 492
        /* Command line did not contain alphanumeric word. */
493
        return NULL;
494
    }
495
 
496
    spinlock_lock(&cmd_lock);
497
 
498
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
499
        cmd_info_t *hlp;
500
 
501
        hlp = list_get_instance(cur, cmd_info_t, link);
502
        spinlock_lock(&hlp->lock);
503
 
635 palkovsky 504
        if (strncmp(hlp->name, &cmdline[start], max(strlen(hlp->name),
505
                                end-start+1)) == 0) {
517 jermar 506
            cmd = hlp;
507
            break;
508
        }
509
 
510
        spinlock_unlock(&hlp->lock);
511
    }
512
 
513
    spinlock_unlock(&cmd_lock);
514
 
515
    if (!cmd) {
516
        /* Unknown command. */
518 jermar 517
        printf("Unknown command.\n");
517 jermar 518
        return NULL;
519
    }
520
 
521
    /* cmd == hlp is locked */
522
 
523
    /*
524
     * The command line must be further analyzed and
525
     * the parameters therefrom must be matched and
526
     * converted to those specified in the cmd info
527
     * structure.
528
     */
518 jermar 529
 
530
    for (i = 0; i < cmd->argc; i++) {
531
        char *buf;
532
        start = end + 1;
533
        if (!parse_argument(cmdline, len, &start, &end)) {
534
            printf("Too few arguments.\n");
535
            spinlock_unlock(&cmd->lock);
536
            return NULL;
537
        }
538
 
668 bondari 539
        error = 0;
518 jermar 540
        switch (cmd->argv[i].type) {
582 palkovsky 541
        case ARG_TYPE_STRING:
518 jermar 542
                buf = cmd->argv[i].buffer;
585 palkovsky 543
                strncpy(buf, (const char *) &cmdline[start], min((end - start) + 2, cmd->argv[i].len));
518 jermar 544
            buf[min((end - start) + 1, cmd->argv[i].len - 1)] = '\0';
545
            break;
585 palkovsky 546
        case ARG_TYPE_INT:
547
            if (parse_int_arg(cmdline+start, end-start+1,
548
                      &cmd->argv[i].intval))
668 bondari 549
                error = 1;
518 jermar 550
            break;
585 palkovsky 551
        case ARG_TYPE_VAR:
552
            if (start != end && cmdline[start] == '"' && cmdline[end] == '"') {
553
                buf = cmd->argv[i].buffer;
554
                strncpy(buf, (const char *) &cmdline[start+1],
555
                    min((end-start), cmd->argv[i].len));
556
                buf[min((end - start), cmd->argv[i].len - 1)] = '\0';
1780 jermar 557
                cmd->argv[i].intval = (unative_t) buf;
585 palkovsky 558
                cmd->argv[i].vartype = ARG_TYPE_STRING;
559
            } else if (!parse_int_arg(cmdline+start, end-start+1,
560
                         &cmd->argv[i].intval))
561
                cmd->argv[i].vartype = ARG_TYPE_INT;
562
            else {
563
                printf("Unrecognized variable argument.\n");
668 bondari 564
                error = 1;
582 palkovsky 565
            }
585 palkovsky 566
            break;
582 palkovsky 567
        case ARG_TYPE_INVALID:
568
        default:
569
            printf("invalid argument type\n");
668 bondari 570
            error = 1;
582 palkovsky 571
            break;
518 jermar 572
        }
573
    }
517 jermar 574
 
668 bondari 575
    if (error) {
576
        spinlock_unlock(&cmd->lock);
577
        return NULL;
578
    }
579
 
518 jermar 580
    start = end + 1;
581
    if (parse_argument(cmdline, len, &start, &end)) {
582
        printf("Too many arguments.\n");
583
        spinlock_unlock(&cmd->lock);
584
        return NULL;
585
    }
586
 
517 jermar 587
    spinlock_unlock(&cmd->lock);
588
    return cmd;
589
}
590
 
518 jermar 591
/** Parse argument.
592
 *
593
 * Find start and end positions of command line argument.
594
 *
595
 * @param cmdline Command line as read from the input device.
596
 * @param len Number of characters in cmdline.
597
 * @param start On entry, 'start' contains pointer to the index
598
 *        of first unprocessed character of cmdline.
599
 *        On successful exit, it marks beginning of the next argument.
600
 * @param end Undefined on entry. On exit, 'end' points to the last character
601
 *        of the next argument.
602
 *
603
 * @return false on failure, true on success.
604
 */
605
bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end)
606
{
607
    int i;
608
    bool found_start = false;
609
 
610
    ASSERT(start != NULL);
611
    ASSERT(end != NULL);
612
 
613
    for (i = *start; i < len; i++) {
614
        if (!found_start) {
615
            if (is_white(cmdline[i]))
616
                (*start)++;
617
            else
618
                found_start = true;
619
        } else {
620
            if (is_white(cmdline[i]))
621
                break;
622
        }
623
    }
624
    *end = i - 1;
625
 
626
    return found_start;
627
}
1702 cejka 628
 
629
 /** @}
630
 */
631