Subversion Repositories HelenOS

Rev

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