Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
510 jermar 1
/*
2071 jermar 2
 * Copyright (c) 2005 Jakub Jermar
510 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
 
1888 jermar 29
/** @addtogroup genericconsole
1702 cejka 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 <arch/types.h>
788 jermar 47
#include <adt/list.h>
517 jermar 48
#include <arch.h>
49
#include <macros.h>
518 jermar 50
#include <debug.h>
596 jermar 51
#include <func.h>
4345 svoboda 52
#include <string.h>
609 palkovsky 53
#include <macros.h>
4338 svoboda 54
#include <sysinfo/sysinfo.h>
55
#include <ddi/device.h>
4346 svoboda 56
#include <symtab.h>
57
#include <errno.h>
510 jermar 58
 
517 jermar 59
/** Simple kernel console.
60
 *
61
 * The console is realized by kernel thread kconsole.
518 jermar 62
 * It doesn't understand any useful command on its own,
63
 * but makes it possible for other kernel subsystems to
517 jermar 64
 * register their own commands.
65
 */
66
 
67
/** Locking.
68
 *
69
 * There is a list of cmd_info_t structures. This list
70
 * is protected by cmd_lock spinlock. Note that specially
71
 * the link elements of cmd_info_t are protected by
72
 * this lock.
73
 *
74
 * Each cmd_info_t also has its own lock, which protects
75
 * all elements thereof except the link element.
76
 *
77
 * cmd_lock must be acquired before any cmd_info lock.
78
 * When locking two cmd info structures, structure with
79
 * lower address must be locked first.
80
 */
81
 
623 jermar 82
SPINLOCK_INITIALIZE(cmd_lock);  /**< Lock protecting command list. */
624 jermar 83
LIST_INITIALIZE(cmd_head);  /**< Command list. */
517 jermar 84
 
85
static cmd_info_t *parse_cmdline(char *cmdline, size_t len);
2108 jermar 86
static bool parse_argument(char *cmdline, size_t len, index_t *start,
87
    index_t *end);
601 palkovsky 88
static char history[KCONSOLE_HISTORY][MAX_CMDLINE] = {};
517 jermar 89
 
4338 svoboda 90
/*
91
 * For now, we use 0 as INR.
92
 * However, it is therefore desirable to have architecture specific
93
 * definition of KCONSOLE_VIRT_INR in the future.
94
 */
95
#define KCONSOLE_VIRT_INR  0
96
 
97
bool kconsole_notify = false;
98
irq_t kconsole_irq;
99
 
100
 
101
/** Allways refuse IRQ ownership.
102
 *
103
 * This is not a real IRQ, so we always decline.
104
 *
105
 * @return Always returns IRQ_DECLINE.
106
 *
107
 */
4344 svoboda 108
static irq_ownership_t kconsole_claim(irq_t *irq)
4338 svoboda 109
{
110
    return IRQ_DECLINE;
111
}
112
 
113
 
114
/** Initialize kconsole data structures
115
 *
116
 * This is the most basic initialization, almost no
117
 * other kernel subsystem is ready yet.
118
 *
119
 */
517 jermar 120
void kconsole_init(void)
121
{
4338 svoboda 122
    unsigned int i;
601 palkovsky 123
 
596 jermar 124
    cmd_init();
2108 jermar 125
    for (i = 0; i < KCONSOLE_HISTORY; i++)
601 palkovsky 126
        history[i][0] = '\0';
517 jermar 127
}
128
 
129
 
4338 svoboda 130
/** Initialize kconsole notification mechanism
131
 *
132
 * Initialize the virtual IRQ notification mechanism.
133
 *
134
 */
135
void kconsole_notify_init(void)
136
{
137
    devno_t devno = device_assign_devno();
138
 
139
    sysinfo_set_item_val("kconsole.present", NULL, true);
140
    sysinfo_set_item_val("kconsole.devno", NULL, devno);
141
    sysinfo_set_item_val("kconsole.inr", NULL, KCONSOLE_VIRT_INR);
142
 
143
    irq_initialize(&kconsole_irq);
144
    kconsole_irq.devno = devno;
145
    kconsole_irq.inr = KCONSOLE_VIRT_INR;
146
    kconsole_irq.claim = kconsole_claim;
147
    irq_register(&kconsole_irq);
148
 
149
    kconsole_notify = true;
150
}
151
 
152
 
517 jermar 153
/** Register kconsole command.
154
 *
155
 * @param cmd Structure describing the command.
156
 *
157
 * @return 0 on failure, 1 on success.
158
 */
159
int cmd_register(cmd_info_t *cmd)
160
{
161
    link_t *cur;
162
 
163
    spinlock_lock(&cmd_lock);
164
 
165
    /*
166
     * Make sure the command is not already listed.
167
     */
168
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
169
        cmd_info_t *hlp;
170
 
171
        hlp = list_get_instance(cur, cmd_info_t, link);
172
 
173
        if (hlp == cmd) {
174
            /* The command is already there. */
175
            spinlock_unlock(&cmd_lock);
176
            return 0;
177
        }
178
 
179
        /* Avoid deadlock. */
180
        if (hlp < cmd) {
181
            spinlock_lock(&hlp->lock);
182
            spinlock_lock(&cmd->lock);
183
        } else {
184
            spinlock_lock(&cmd->lock);
185
            spinlock_lock(&hlp->lock);
186
        }
2108 jermar 187
        if ((strncmp(hlp->name, cmd->name, max(strlen(cmd->name),
188
            strlen(hlp->name))) == 0)) {
517 jermar 189
            /* The command is already there. */
190
            spinlock_unlock(&hlp->lock);
191
            spinlock_unlock(&cmd->lock);
192
            spinlock_unlock(&cmd_lock);
193
            return 0;
194
        }
195
 
196
        spinlock_unlock(&hlp->lock);
197
        spinlock_unlock(&cmd->lock);
198
    }
199
 
200
    /*
201
     * Now the command can be added.
202
     */
203
    list_append(&cmd->link, &cmd_head);
204
 
205
    spinlock_unlock(&cmd_lock);
206
    return 1;
207
}
208
 
635 palkovsky 209
/** Print count times a character */
601 palkovsky 210
static void rdln_print_c(char ch, int count)
211
{
212
    int i;
2108 jermar 213
    for (i = 0; i < count; i++)
601 palkovsky 214
        putchar(ch);
215
}
216
 
635 palkovsky 217
/** Insert character to string */
601 palkovsky 218
static void insert_char(char *str, char ch, int pos)
219
{
220
    int i;
221
 
2108 jermar 222
    for (i = strlen(str); i > pos; i--)
223
        str[i] = str[i - 1];
601 palkovsky 224
    str[pos] = ch;
225
}
226
 
640 jermar 227
/** Try to find a command beginning with prefix */
3197 svoboda 228
static const char *cmdtab_search_one(const char *name,link_t **startpos)
601 palkovsky 229
{
2113 decky 230
    size_t namelen = strlen(name);
601 palkovsky 231
    const char *curname;
232
 
233
    spinlock_lock(&cmd_lock);
234
 
235
    if (!*startpos)
236
        *startpos = cmd_head.next;
237
 
2108 jermar 238
    for (; *startpos != &cmd_head; *startpos = (*startpos)->next) {
601 palkovsky 239
        cmd_info_t *hlp;
240
        hlp = list_get_instance(*startpos, cmd_info_t, link);
241
 
242
        curname = hlp->name;
243
        if (strlen(curname) < namelen)
244
            continue;
245
        if (strncmp(curname, name, namelen) == 0) {
246
            spinlock_unlock(&cmd_lock);
247
            return curname+namelen;
248
        }
249
    }
250
    spinlock_unlock(&cmd_lock);
251
    return NULL;
252
}
253
 
254
 
255
/** Command completion of the commands
256
 *
257
 * @param name - string to match, changed to hint on exit
258
 * @return number of found matches
259
 */
260
static int cmdtab_compl(char *name)
261
{
4346 svoboda 262
    static char output[/*MAX_SYMBOL_NAME*/128 + 1];
601 palkovsky 263
    link_t *startpos = NULL;
264
    const char *foundtxt;
265
    int found = 0;
266
    int i;
267
 
268
    output[0] = '\0';
269
    while ((foundtxt = cmdtab_search_one(name, &startpos))) {
270
        startpos = startpos->next;
271
        if (!found)
3197 svoboda 272
            strncpy(output, foundtxt, strlen(foundtxt) + 1);
601 palkovsky 273
        else {
2108 jermar 274
            for (i = 0; output[i] && foundtxt[i] &&
275
                output[i] == foundtxt[i]; i++)
601 palkovsky 276
                ;
277
            output[i] = '\0';
278
        }
279
        found++;
280
    }
281
    if (!found)
282
        return 0;
283
 
602 palkovsky 284
    if (found > 1 && !strlen(output)) {
601 palkovsky 285
        printf("\n");
286
        startpos = NULL;
287
        while ((foundtxt = cmdtab_search_one(name, &startpos))) {
288
            cmd_info_t *hlp;
289
            hlp = list_get_instance(startpos, cmd_info_t, link);
290
            printf("%s - %s\n", hlp->name, hlp->description);
291
            startpos = startpos->next;
292
        }
293
    }
4346 svoboda 294
    strncpy(name, output, 128/*MAX_SYMBOL_NAME*/);
601 palkovsky 295
    return found;
296
}
297
 
4346 svoboda 298
static char *clever_readline(const char *prompt, indev_t *input)
601 palkovsky 299
{
300
    static int histposition = 0;
301
 
3197 svoboda 302
    static char tmp[MAX_CMDLINE + 1];
601 palkovsky 303
    int curlen = 0, position = 0;
304
    char *current = history[histposition];
305
    int i;
606 palkovsky 306
    char mod; /* Command Modifier */
601 palkovsky 307
    char c;
308
 
309
    printf("%s> ", prompt);
310
    while (1) {
311
        c = _getc(input);
312
        if (c == '\n') {
313
            putchar(c);
314
            break;
3197 svoboda 315
        }
316
        if (c == '\b') { /* Backspace */
601 palkovsky 317
            if (position == 0)
318
                continue;
2108 jermar 319
            for (i = position; i < curlen; i++)
320
                current[i - 1] = current[i];
601 palkovsky 321
            curlen--;
322
            position--;
323
            putchar('\b');
2108 jermar 324
            for (i = position; i < curlen; i++)
601 palkovsky 325
                putchar(current[i]);
326
            putchar(' ');
2108 jermar 327
            rdln_print_c('\b', curlen - position + 1);
601 palkovsky 328
            continue;
329
        }
607 palkovsky 330
        if (c == '\t') { /* Tabulator */
601 palkovsky 331
            int found;
332
 
333
            /* Move to the end of the word */
2108 jermar 334
            for (; position < curlen && current[position] != ' ';
335
                position++)
601 palkovsky 336
                putchar(current[position]);
337
            /* Copy to tmp last word */
2108 jermar 338
            for (i = position - 1; i >= 0 && current[i] != ' '; i--)
601 palkovsky 339
                ;
340
            /* If word begins with * or &, skip it */
341
            if (tmp[0] == '*' || tmp[0] == '&')
2108 jermar 342
                for (i = 1; tmp[i]; i++)
343
                    tmp[i - 1] = tmp[i];
601 palkovsky 344
            i++; /* I is at the start of the word */
2108 jermar 345
            strncpy(tmp, current + i, position - i + 1);
601 palkovsky 346
 
2108 jermar 347
            if (i == 0) { /* Command completion */
601 palkovsky 348
                found = cmdtab_compl(tmp);
349
            } else { /* Symtab completion */
350
                found = symtab_compl(tmp);
351
            }
352
 
353
            if (found == 0)
354
                continue;
2108 jermar 355
            for (i = 0; tmp[i] && curlen < MAX_CMDLINE;
356
                i++, curlen++)
357
                insert_char(current, tmp[i], i + position);
602 palkovsky 358
 
2108 jermar 359
            if (strlen(tmp) || found == 1) { /* If we have a hint */
360
                for (i = position; i < curlen; i++)
601 palkovsky 361
                    putchar(current[i]);
362
                position += strlen(tmp);
363
                /* Add space to end */
2108 jermar 364
                if (found == 1 && position == curlen &&
602 palkovsky 365
                    curlen < MAX_CMDLINE) {
601 palkovsky 366
                    current[position] = ' ';
367
                    curlen++;
368
                    position++;
369
                    putchar(' ');
370
                }
602 palkovsky 371
            } else { /* No hint, table was printed */
601 palkovsky 372
                printf("%s> ", prompt);
2108 jermar 373
                for (i = 0; i < curlen; i++)
601 palkovsky 374
                    putchar(current[i]);
375
                position += strlen(tmp);
376
            }
2108 jermar 377
            rdln_print_c('\b', curlen - position);
601 palkovsky 378
            continue;
379
        }
607 palkovsky 380
        if (c == 0x1b) { /* Special command */
606 palkovsky 381
            mod = _getc(input);
601 palkovsky 382
            c = _getc(input);
606 palkovsky 383
 
384
            if (mod != 0x5b && mod != 0x4f)
601 palkovsky 385
                continue;
606 palkovsky 386
 
387
            if (c == 0x33 && _getc(input) == 0x7e) {
607 palkovsky 388
                /* Delete */
606 palkovsky 389
                if (position == curlen)
390
                    continue;
2108 jermar 391
                for (i = position + 1; i < curlen; i++) {
606 palkovsky 392
                    putchar(current[i]);
2108 jermar 393
                    current[i - 1] = current[i];
606 palkovsky 394
                }
395
                putchar(' ');
2108 jermar 396
                rdln_print_c('\b', curlen - position);
606 palkovsky 397
                curlen--;
2108 jermar 398
            } else if (c == 0x48) { /* Home */
399
                rdln_print_c('\b', position);
606 palkovsky 400
                position = 0;
2108 jermar 401
            } else if (c == 0x46) {  /* End */
402
                for (i = position; i < curlen; i++)
606 palkovsky 403
                    putchar(current[i]);
404
                position = curlen;
2108 jermar 405
            } else if (c == 0x44) { /* Left */
601 palkovsky 406
                if (position > 0) {
407
                    putchar('\b');
408
                    position--;
409
                }
410
                continue;
2108 jermar 411
            } else if (c == 0x43) { /* Right */
601 palkovsky 412
                if (position < curlen) {
413
                    putchar(current[position]);
414
                    position++;
415
                }
416
                continue;
2108 jermar 417
            } else if (c == 0x41 || c == 0x42) {
418
                                /* Up, down */
419
                rdln_print_c('\b', position);
420
                rdln_print_c(' ', curlen);
421
                rdln_print_c('\b', curlen);
607 palkovsky 422
                if (c == 0x41) /* Up */
601 palkovsky 423
                    histposition--;
424
                else
425
                    histposition++;
2108 jermar 426
                if (histposition < 0) {
427
                    histposition = KCONSOLE_HISTORY - 1;
428
                } else {
429
                    histposition =
430
                        histposition % KCONSOLE_HISTORY;
431
                }
601 palkovsky 432
                current = history[histposition];
433
                printf("%s", current);
434
                curlen = strlen(current);
435
                position = curlen;
436
                continue;
437
            }
438
            continue;
439
        }
440
        if (curlen >= MAX_CMDLINE)
441
            continue;
442
 
443
        insert_char(current, c, position);
444
 
445
        curlen++;
2108 jermar 446
        for (i = position; i < curlen; i++)
601 palkovsky 447
            putchar(current[i]);
448
        position++;
2108 jermar 449
        rdln_print_c('\b',curlen - position);
601 palkovsky 450
    }
603 palkovsky 451
    if (curlen) {
452
        histposition++;
453
        histposition = histposition % KCONSOLE_HISTORY;
454
    }
601 palkovsky 455
    current[curlen] = '\0';
456
    return current;
457
}
458
 
4346 svoboda 459
bool kconsole_check_poll(void)
460
{
461
    return check_poll(stdin);
462
}
463
 
4337 svoboda 464
/** Kernel console prompt.
510 jermar 465
 *
1708 jermar 466
 * @param prompt Kernel console prompt (e.g kconsole/panic).
4337 svoboda 467
 * @param msg    Message to display in the beginning.
468
 * @param kcon   Wait for keypress to show the prompt
469
 *               and never exit.
470
 *
510 jermar 471
 */
4337 svoboda 472
void kconsole(char *prompt, char *msg, bool kcon)
510 jermar 473
{
517 jermar 474
    cmd_info_t *cmd_info;
475
    count_t len;
601 palkovsky 476
    char *cmdline;
4346 svoboda 477
 
510 jermar 478
    if (!stdin) {
4337 svoboda 479
        LOG("No stdin for kernel console");
510 jermar 480
        return;
481
    }
482
 
4337 svoboda 483
    if (msg)
484
        printf("%s", msg);
485
 
486
    if (kcon)
487
        _getc(stdin);
4346 svoboda 488
    else
489
        printf("Type \"exit\" to leave the console.\n");
4337 svoboda 490
 
510 jermar 491
    while (true) {
2113 decky 492
        cmdline = clever_readline((char *) prompt, stdin);
601 palkovsky 493
        len = strlen(cmdline);
494
        if (!len)
518 jermar 495
            continue;
4337 svoboda 496
 
4346 svoboda 497
        if ((!kcon) && (len == 4) && (strncmp(cmdline, "exit", 4) == 0))
498
            break;
499
 
517 jermar 500
        cmd_info = parse_cmdline(cmdline, len);
518 jermar 501
        if (!cmd_info)
517 jermar 502
            continue;
4337 svoboda 503
 
517 jermar 504
        (void) cmd_info->func(cmd_info->argv);
510 jermar 505
    }
506
}
517 jermar 507
 
4337 svoboda 508
/** Kernel console managing thread.
509
 *
510
 */
511
void kconsole_thread(void *data)
512
{
513
    kconsole("kconsole", "Kernel console ready (press any key to activate)\n", true);
514
}
515
 
1780 jermar 516
static int parse_int_arg(char *text, size_t len, unative_t *result)
585 palkovsky 517
{
1780 jermar 518
    uintptr_t symaddr;
585 palkovsky 519
    bool isaddr = false;
589 palkovsky 520
    bool isptr = false;
4346 svoboda 521
    int rc;
522
 
523
    static char symname[MAX_SYMBOL_NAME];
585 palkovsky 524
 
525
    /* If we get a name, try to find it in symbol table */
931 palkovsky 526
    if (text[0] == '&') {
527
        isaddr = true;
2108 jermar 528
        text++;
529
        len--;
931 palkovsky 530
    } else if (text[0] == '*') {
531
        isptr = true;
2108 jermar 532
        text++;
533
        len--;
931 palkovsky 534
    }
614 palkovsky 535
    if (text[0] < '0' || text[0] > '9') {
2108 jermar 536
        strncpy(symname, text, min(len + 1, MAX_SYMBOL_NAME));
4346 svoboda 537
        rc = symtab_addr_lookup(symname, &symaddr);
538
        switch (rc) {
539
        case ENOENT:
2108 jermar 540
            printf("Symbol %s not found.\n", symname);
585 palkovsky 541
            return -1;
4346 svoboda 542
        case EOVERFLOW:
2108 jermar 543
            printf("Duplicate symbol %s.\n", symname);
585 palkovsky 544
            symtab_print_search(symname);
545
            return -1;
4346 svoboda 546
        default:
547
            printf("No symbol information available.\n");
548
            return -1;
585 palkovsky 549
        }
4346 svoboda 550
 
932 palkovsky 551
        if (isaddr)
1780 jermar 552
            *result = (unative_t)symaddr;
932 palkovsky 553
        else if (isptr)
1780 jermar 554
            *result = **((unative_t **)symaddr);
932 palkovsky 555
        else
1780 jermar 556
            *result = *((unative_t *)symaddr);
932 palkovsky 557
    } else { /* It's a number - convert it */
585 palkovsky 558
        *result = atoi(text);
932 palkovsky 559
        if (isptr)
1780 jermar 560
            *result = *((unative_t *)*result);
932 palkovsky 561
    }
931 palkovsky 562
 
585 palkovsky 563
    return 0;
564
}
565
 
517 jermar 566
/** Parse command line.
567
 *
568
 * @param cmdline Command line as read from input device.
569
 * @param len Command line length.
570
 *
571
 * @return Structure describing the command.
572
 */
573
cmd_info_t *parse_cmdline(char *cmdline, size_t len)
574
{
575
    index_t start = 0, end = 0;
576
    cmd_info_t *cmd = NULL;
577
    link_t *cur;
2113 decky 578
    count_t i;
668 bondari 579
    int error = 0;
517 jermar 580
 
518 jermar 581
    if (!parse_argument(cmdline, len, &start, &end)) {
517 jermar 582
        /* Command line did not contain alphanumeric word. */
583
        return NULL;
584
    }
585
 
586
    spinlock_lock(&cmd_lock);
587
 
588
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
589
        cmd_info_t *hlp;
590
 
591
        hlp = list_get_instance(cur, cmd_info_t, link);
592
        spinlock_lock(&hlp->lock);
593
 
635 palkovsky 594
        if (strncmp(hlp->name, &cmdline[start], max(strlen(hlp->name),
2108 jermar 595
            end - start + 1)) == 0) {
517 jermar 596
            cmd = hlp;
597
            break;
598
        }
599
 
600
        spinlock_unlock(&hlp->lock);
601
    }
602
 
603
    spinlock_unlock(&cmd_lock);
604
 
605
    if (!cmd) {
606
        /* Unknown command. */
518 jermar 607
        printf("Unknown command.\n");
517 jermar 608
        return NULL;
609
    }
610
 
611
    /* cmd == hlp is locked */
612
 
613
    /*
614
     * The command line must be further analyzed and
615
     * the parameters therefrom must be matched and
616
     * converted to those specified in the cmd info
617
     * structure.
618
     */
518 jermar 619
 
620
    for (i = 0; i < cmd->argc; i++) {
621
        char *buf;
622
        start = end + 1;
623
        if (!parse_argument(cmdline, len, &start, &end)) {
624
            printf("Too few arguments.\n");
625
            spinlock_unlock(&cmd->lock);
626
            return NULL;
627
        }
628
 
668 bondari 629
        error = 0;
518 jermar 630
        switch (cmd->argv[i].type) {
582 palkovsky 631
        case ARG_TYPE_STRING:
2113 decky 632
            buf = (char *) cmd->argv[i].buffer;
633
            strncpy(buf, (const char *) &cmdline[start],
2108 jermar 634
                min((end - start) + 2, cmd->argv[i].len));
3197 svoboda 635
            buf[min((end - start) + 1, cmd->argv[i].len - 1)] =
636
                '\0';
518 jermar 637
            break;
585 palkovsky 638
        case ARG_TYPE_INT:
2108 jermar 639
            if (parse_int_arg(cmdline + start, end - start + 1,
640
                &cmd->argv[i].intval))
668 bondari 641
                error = 1;
518 jermar 642
            break;
585 palkovsky 643
        case ARG_TYPE_VAR:
2108 jermar 644
            if (start != end && cmdline[start] == '"' &&
645
                cmdline[end] == '"') {
2113 decky 646
                buf = (char *) cmd->argv[i].buffer;
2108 jermar 647
                strncpy(buf, (const char *) &cmdline[start + 1],
648
                    min((end-start), cmd->argv[i].len));
649
                buf[min((end - start), cmd->argv[i].len - 1)] =
650
                    '\0';
1780 jermar 651
                cmd->argv[i].intval = (unative_t) buf;
585 palkovsky 652
                cmd->argv[i].vartype = ARG_TYPE_STRING;
3197 svoboda 653
            } else if (!parse_int_arg(cmdline + start,
654
                end - start + 1, &cmd->argv[i].intval)) {
585 palkovsky 655
                cmd->argv[i].vartype = ARG_TYPE_INT;
2108 jermar 656
            } else {
585 palkovsky 657
                printf("Unrecognized variable argument.\n");
668 bondari 658
                error = 1;
582 palkovsky 659
            }
585 palkovsky 660
            break;
582 palkovsky 661
        case ARG_TYPE_INVALID:
662
        default:
663
            printf("invalid argument type\n");
668 bondari 664
            error = 1;
582 palkovsky 665
            break;
518 jermar 666
        }
667
    }
517 jermar 668
 
668 bondari 669
    if (error) {
670
        spinlock_unlock(&cmd->lock);
671
        return NULL;
672
    }
673
 
518 jermar 674
    start = end + 1;
675
    if (parse_argument(cmdline, len, &start, &end)) {
676
        printf("Too many arguments.\n");
677
        spinlock_unlock(&cmd->lock);
678
        return NULL;
679
    }
680
 
517 jermar 681
    spinlock_unlock(&cmd->lock);
682
    return cmd;
683
}
684
 
518 jermar 685
/** Parse argument.
686
 *
687
 * Find start and end positions of command line argument.
688
 *
689
 * @param cmdline Command line as read from the input device.
690
 * @param len Number of characters in cmdline.
691
 * @param start On entry, 'start' contains pointer to the index
692
 *        of first unprocessed character of cmdline.
693
 *        On successful exit, it marks beginning of the next argument.
694
 * @param end Undefined on entry. On exit, 'end' points to the last character
695
 *        of the next argument.
696
 *
697
 * @return false on failure, true on success.
698
 */
699
bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end)
700
{
2113 decky 701
    index_t i;
518 jermar 702
    bool found_start = false;
703
 
704
    ASSERT(start != NULL);
705
    ASSERT(end != NULL);
706
 
707
    for (i = *start; i < len; i++) {
708
        if (!found_start) {
2572 jermar 709
            if (isspace(cmdline[i]))
518 jermar 710
                (*start)++;
711
            else
712
                found_start = true;
713
        } else {
2572 jermar 714
            if (isspace(cmdline[i]))
518 jermar 715
                break;
716
        }
717
    }
718
    *end = i - 1;
719
 
720
    return found_start;
721
}
1702 cejka 722
 
1888 jermar 723
/** @}
1702 cejka 724
 */