Subversion Repositories HelenOS

Rev

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