Subversion Repositories HelenOS

Rev

Rev 3941 | Rev 4089 | 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>
4011 svoboda 52
#include <string.h>
582 palkovsky 53
#include <symtab.h>
609 palkovsky 54
#include <macros.h>
3761 decky 55
#include <sysinfo/sysinfo.h>
56
#include <ddi/device.h>
510 jermar 57
 
517 jermar 58
/** Simple kernel console.
59
 *
60
 * The console is realized by kernel thread kconsole.
518 jermar 61
 * It doesn't understand any useful command on its own,
62
 * but makes it possible for other kernel subsystems to
517 jermar 63
 * register their own commands.
64
 */
65
 
66
/** Locking.
67
 *
68
 * There is a list of cmd_info_t structures. This list
69
 * is protected by cmd_lock spinlock. Note that specially
70
 * the link elements of cmd_info_t are protected by
71
 * this lock.
72
 *
73
 * Each cmd_info_t also has its own lock, which protects
74
 * all elements thereof except the link element.
75
 *
76
 * cmd_lock must be acquired before any cmd_info lock.
77
 * When locking two cmd info structures, structure with
78
 * lower address must be locked first.
79
 */
80
 
623 jermar 81
SPINLOCK_INITIALIZE(cmd_lock);	/**< Lock protecting command list. */
624 jermar 82
LIST_INITIALIZE(cmd_head);	/**< Command list. */
517 jermar 83
 
84
static cmd_info_t *parse_cmdline(char *cmdline, size_t len);
2108 jermar 85
static bool parse_argument(char *cmdline, size_t len, index_t *start,
86
    index_t *end);
601 palkovsky 87
static char history[KCONSOLE_HISTORY][MAX_CMDLINE] = {};
517 jermar 88
 
3761 decky 89
/*
90
 * For now, we use 0 as INR.
91
 * However, it is therefore desirable to have architecture specific
92
 * definition of KCONSOLE_VIRT_INR in the future.
93
 */
94
#define KCONSOLE_VIRT_INR  0
95
 
96
bool kconsole_notify = false;
97
irq_t kconsole_irq;
98
 
99
 
100
/** Allways refuse IRQ ownership.
101
 *
102
 * This is not a real IRQ, so we always decline.
103
 *
104
 * @return Always returns IRQ_DECLINE.
105
 *
106
 */
3941 jermar 107
static irq_ownership_t kconsole_claim(irq_t *irq)
3761 decky 108
{
109
	return IRQ_DECLINE;
110
}
111
 
112
 
113
/** Initialize kconsole data structures
114
 *
115
 * This is the most basic initialization, almost no
116
 * other kernel subsystem is ready yet.
117
 *
118
 */
517 jermar 119
void kconsole_init(void)
120
{
3761 decky 121
	unsigned int i;
601 palkovsky 122
 
596 jermar 123
	cmd_init();
2108 jermar 124
	for (i = 0; i < KCONSOLE_HISTORY; i++)
601 palkovsky 125
		history[i][0] = '\0';
517 jermar 126
}
127
 
128
 
3761 decky 129
/** Initialize kconsole notification mechanism
130
 *
131
 * Initialize the virtual IRQ notification mechanism.
132
 *
133
 */
134
void kconsole_notify_init(void)
135
{
136
	devno_t devno = device_assign_devno();
137
 
138
	sysinfo_set_item_val("kconsole.present", NULL, true);
139
	sysinfo_set_item_val("kconsole.devno", NULL, devno);
140
	sysinfo_set_item_val("kconsole.inr", NULL, KCONSOLE_VIRT_INR);
141
 
142
	irq_initialize(&kconsole_irq);
143
	kconsole_irq.devno = devno;
144
	kconsole_irq.inr = KCONSOLE_VIRT_INR;
145
	kconsole_irq.claim = kconsole_claim;
146
	irq_register(&kconsole_irq);
147
 
148
	kconsole_notify = true;
149
}
150
 
151
 
517 jermar 152
/** Register kconsole command.
153
 *
154
 * @param cmd Structure describing the command.
155
 *
156
 * @return 0 on failure, 1 on success.
157
 */
158
int cmd_register(cmd_info_t *cmd)
159
{
160
	link_t *cur;
161
 
162
	spinlock_lock(&cmd_lock);
163
 
164
	/*
165
	 * Make sure the command is not already listed.
166
	 */
167
	for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
168
		cmd_info_t *hlp;
169
 
170
		hlp = list_get_instance(cur, cmd_info_t, link);
171
 
172
		if (hlp == cmd) {
173
			/* The command is already there. */
174
			spinlock_unlock(&cmd_lock);
175
			return 0;
176
		}
177
 
178
		/* Avoid deadlock. */
179
		if (hlp < cmd) {
180
			spinlock_lock(&hlp->lock);
181
			spinlock_lock(&cmd->lock);
182
		} else {
183
			spinlock_lock(&cmd->lock);
184
			spinlock_lock(&hlp->lock);
185
		}
2108 jermar 186
		if ((strncmp(hlp->name, cmd->name, max(strlen(cmd->name),
187
		    strlen(hlp->name))) == 0)) {
517 jermar 188
			/* The command is already there. */
189
			spinlock_unlock(&hlp->lock);
190
			spinlock_unlock(&cmd->lock);
191
			spinlock_unlock(&cmd_lock);
192
			return 0;
193
		}
194
 
195
		spinlock_unlock(&hlp->lock);
196
		spinlock_unlock(&cmd->lock);
197
	}
198
 
199
	/*
200
	 * Now the command can be added.
201
	 */
202
	list_append(&cmd->link, &cmd_head);
203
 
204
	spinlock_unlock(&cmd_lock);
205
	return 1;
206
}
207
 
635 palkovsky 208
/** Print count times a character */
601 palkovsky 209
static void rdln_print_c(char ch, int count)
210
{
211
	int i;
2108 jermar 212
	for (i = 0; i < count; i++)
601 palkovsky 213
		putchar(ch);
214
}
215
 
635 palkovsky 216
/** Insert character to string */
601 palkovsky 217
static void insert_char(char *str, char ch, int pos)
218
{
219
	int i;
220
 
2108 jermar 221
	for (i = strlen(str); i > pos; i--)
222
		str[i] = str[i - 1];
601 palkovsky 223
	str[pos] = ch;
224
}
225
 
640 jermar 226
/** Try to find a command beginning with prefix */
3193 jermar 227
static const char *cmdtab_search_one(const char *name,link_t **startpos)
601 palkovsky 228
{
2113 decky 229
	size_t namelen = strlen(name);
601 palkovsky 230
	const char *curname;
231
 
232
	spinlock_lock(&cmd_lock);
233
 
234
	if (!*startpos)
235
		*startpos = cmd_head.next;
236
 
2108 jermar 237
	for (; *startpos != &cmd_head; *startpos = (*startpos)->next) {
601 palkovsky 238
		cmd_info_t *hlp;
239
		hlp = list_get_instance(*startpos, cmd_info_t, link);
240
 
241
		curname = hlp->name;
242
		if (strlen(curname) < namelen)
243
			continue;
244
		if (strncmp(curname, name, namelen) == 0) {
245
			spinlock_unlock(&cmd_lock);	
246
			return curname+namelen;
247
		}
248
	}
249
	spinlock_unlock(&cmd_lock);	
250
	return NULL;
251
}
252
 
253
 
254
/** Command completion of the commands 
255
 *
256
 * @param name - string to match, changed to hint on exit
257
 * @return number of found matches
258
 */
259
static int cmdtab_compl(char *name)
260
{
3193 jermar 261
	static char output[MAX_SYMBOL_NAME + 1];
601 palkovsky 262
	link_t *startpos = NULL;
263
	const char *foundtxt;
264
	int found = 0;
265
	int i;
266
 
267
	output[0] = '\0';
268
	while ((foundtxt = cmdtab_search_one(name, &startpos))) {
269
		startpos = startpos->next;
270
		if (!found)
3193 jermar 271
			strncpy(output, foundtxt, strlen(foundtxt) + 1);
601 palkovsky 272
		else {
2108 jermar 273
			for (i = 0; output[i] && foundtxt[i] &&
274
			    output[i] == foundtxt[i]; i++)
601 palkovsky 275
				;
276
			output[i] = '\0';
277
		}
278
		found++;
279
	}
280
	if (!found)
281
		return 0;
282
 
602 palkovsky 283
	if (found > 1 && !strlen(output)) {
601 palkovsky 284
		printf("\n");
285
		startpos = NULL;
286
		while ((foundtxt = cmdtab_search_one(name, &startpos))) {
287
			cmd_info_t *hlp;
288
			hlp = list_get_instance(startpos, cmd_info_t, link);
289
			printf("%s - %s\n", hlp->name, hlp->description);
290
			startpos = startpos->next;
291
		}
292
	}
293
	strncpy(name, output, MAX_SYMBOL_NAME);
294
	return found;
295
 
296
}
297
 
3193 jermar 298
static char *clever_readline(const char *prompt, chardev_t *input)
601 palkovsky 299
{
300
	static int histposition = 0;
301
 
3193 jermar 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;
3193 jermar 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
 
3707 decky 459
/** Kernel console prompt.
510 jermar 460
 *
1708 jermar 461
 * @param prompt Kernel console prompt (e.g kconsole/panic).
3707 decky 462
 * @param msg    Message to display in the beginning.
463
 * @param kcon   Wait for keypress to show the prompt
464
 *               and never exit.
465
 *
510 jermar 466
 */
3707 decky 467
void kconsole(char *prompt, char *msg, bool kcon)
510 jermar 468
{
517 jermar 469
	cmd_info_t *cmd_info;
470
	count_t len;
601 palkovsky 471
	char *cmdline;
510 jermar 472
 
473
	if (!stdin) {
3707 decky 474
		LOG("No stdin for kernel console");
510 jermar 475
		return;
476
	}
477
 
3707 decky 478
	if (msg)
479
		printf("%s", msg);
480
 
481
	if (kcon)
482
		_getc(stdin);
483
 
510 jermar 484
	while (true) {
2113 decky 485
		cmdline = clever_readline((char *) prompt, stdin);
601 palkovsky 486
		len = strlen(cmdline);
487
		if (!len)
518 jermar 488
			continue;
3707 decky 489
 
517 jermar 490
		cmd_info = parse_cmdline(cmdline, len);
518 jermar 491
		if (!cmd_info)
517 jermar 492
			continue;
3707 decky 493
 
494
		if ((!kcon)
495
		    && (strncmp(cmd_info->name, "exit", min(strlen(cmd_info->name), 5)) == 0))
609 palkovsky 496
			break;
3707 decky 497
 
517 jermar 498
		(void) cmd_info->func(cmd_info->argv);
510 jermar 499
	}
500
}
517 jermar 501
 
3707 decky 502
/** Kernel console managing thread.
503
 *
504
 */
505
void kconsole_thread(void *data)
506
{
507
	kconsole("kconsole", "Kernel console ready (press any key to activate)\n", true);
508
}
509
 
1780 jermar 510
static int parse_int_arg(char *text, size_t len, unative_t *result)
585 palkovsky 511
{
1972 jermar 512
	static char symname[MAX_SYMBOL_NAME];
1780 jermar 513
	uintptr_t symaddr;
585 palkovsky 514
	bool isaddr = false;
589 palkovsky 515
	bool isptr = false;
585 palkovsky 516
 
517
	/* If we get a name, try to find it in symbol table */
931 palkovsky 518
	if (text[0] == '&') {
519
		isaddr = true;
2108 jermar 520
		text++;
521
		len--;
931 palkovsky 522
	} else if (text[0] == '*') {
523
		isptr = true;
2108 jermar 524
		text++;
525
		len--;
931 palkovsky 526
	}
614 palkovsky 527
	if (text[0] < '0' || text[0] > '9') {
2108 jermar 528
		strncpy(symname, text, min(len + 1, MAX_SYMBOL_NAME));
585 palkovsky 529
		symaddr = get_symbol_addr(symname);
530
		if (!symaddr) {
2108 jermar 531
			printf("Symbol %s not found.\n", symname);
585 palkovsky 532
			return -1;
533
		}
1780 jermar 534
		if (symaddr == (uintptr_t) -1) {
2108 jermar 535
			printf("Duplicate symbol %s.\n", symname);
585 palkovsky 536
			symtab_print_search(symname);
537
			return -1;
538
		}
932 palkovsky 539
		if (isaddr)
1780 jermar 540
			*result = (unative_t)symaddr;
932 palkovsky 541
		else if (isptr)
1780 jermar 542
			*result = **((unative_t **)symaddr);
932 palkovsky 543
		else
1780 jermar 544
			*result = *((unative_t *)symaddr);
932 palkovsky 545
	} else { /* It's a number - convert it */
585 palkovsky 546
		*result = atoi(text);
932 palkovsky 547
		if (isptr)
1780 jermar 548
			*result = *((unative_t *)*result);
932 palkovsky 549
	}
931 palkovsky 550
 
585 palkovsky 551
	return 0;
552
}
553
 
517 jermar 554
/** Parse command line.
555
 *
556
 * @param cmdline Command line as read from input device.
557
 * @param len Command line length.
558
 *
559
 * @return Structure describing the command.
560
 */
561
cmd_info_t *parse_cmdline(char *cmdline, size_t len)
562
{
563
	index_t start = 0, end = 0;
564
	cmd_info_t *cmd = NULL;
565
	link_t *cur;
2113 decky 566
	count_t i;
668 bondari 567
	int error = 0;
517 jermar 568
 
518 jermar 569
	if (!parse_argument(cmdline, len, &start, &end)) {
517 jermar 570
		/* Command line did not contain alphanumeric word. */
571
		return NULL;
572
	}
573
 
574
	spinlock_lock(&cmd_lock);
575
 
576
	for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
577
		cmd_info_t *hlp;
578
 
579
		hlp = list_get_instance(cur, cmd_info_t, link);
580
		spinlock_lock(&hlp->lock);
581
 
635 palkovsky 582
		if (strncmp(hlp->name, &cmdline[start], max(strlen(hlp->name),
2108 jermar 583
		    end - start + 1)) == 0) {
517 jermar 584
			cmd = hlp;
585
			break;
586
		}
587
 
588
		spinlock_unlock(&hlp->lock);
589
	}
590
 
591
	spinlock_unlock(&cmd_lock);	
592
 
593
	if (!cmd) {
594
		/* Unknown command. */
518 jermar 595
		printf("Unknown command.\n");
517 jermar 596
		return NULL;
597
	}
598
 
599
	/* cmd == hlp is locked */
600
 
601
	/*
602
	 * The command line must be further analyzed and
603
	 * the parameters therefrom must be matched and
604
	 * converted to those specified in the cmd info
605
	 * structure.
606
	 */
518 jermar 607
 
608
	for (i = 0; i < cmd->argc; i++) {
609
		char *buf;
610
		start = end + 1;
611
		if (!parse_argument(cmdline, len, &start, &end)) {
612
			printf("Too few arguments.\n");
613
			spinlock_unlock(&cmd->lock);
614
			return NULL;
615
		}
616
 
668 bondari 617
		error = 0;
518 jermar 618
		switch (cmd->argv[i].type) {
582 palkovsky 619
		case ARG_TYPE_STRING:
2113 decky 620
			buf = (char *) cmd->argv[i].buffer;
621
			strncpy(buf, (const char *) &cmdline[start],
2108 jermar 622
			    min((end - start) + 2, cmd->argv[i].len));
3193 jermar 623
			buf[min((end - start) + 1, cmd->argv[i].len - 1)] =
624
			    '\0';
518 jermar 625
			break;
585 palkovsky 626
		case ARG_TYPE_INT: 
2108 jermar 627
			if (parse_int_arg(cmdline + start, end - start + 1, 
628
			    &cmd->argv[i].intval))
668 bondari 629
				error = 1;
518 jermar 630
			break;
585 palkovsky 631
		case ARG_TYPE_VAR:
2108 jermar 632
			if (start != end && cmdline[start] == '"' &&
633
			    cmdline[end] == '"') {
2113 decky 634
				buf = (char *) cmd->argv[i].buffer;
2108 jermar 635
				strncpy(buf, (const char *) &cmdline[start + 1],
636
				    min((end-start), cmd->argv[i].len));
637
				buf[min((end - start), cmd->argv[i].len - 1)] =
638
				    '\0';
1780 jermar 639
				cmd->argv[i].intval = (unative_t) buf;
585 palkovsky 640
				cmd->argv[i].vartype = ARG_TYPE_STRING;
3193 jermar 641
			} else if (!parse_int_arg(cmdline + start,
642
			    end - start + 1, &cmd->argv[i].intval)) {
585 palkovsky 643
				cmd->argv[i].vartype = ARG_TYPE_INT;
2108 jermar 644
			} else {
585 palkovsky 645
				printf("Unrecognized variable argument.\n");
668 bondari 646
				error = 1;
582 palkovsky 647
			}
585 palkovsky 648
			break;
582 palkovsky 649
		case ARG_TYPE_INVALID:
650
		default:
651
			printf("invalid argument type\n");
668 bondari 652
			error = 1;
582 palkovsky 653
			break;
518 jermar 654
		}
655
	}
517 jermar 656
 
668 bondari 657
	if (error) {
658
		spinlock_unlock(&cmd->lock);
659
		return NULL;
660
	}
661
 
518 jermar 662
	start = end + 1;
663
	if (parse_argument(cmdline, len, &start, &end)) {
664
		printf("Too many arguments.\n");
665
		spinlock_unlock(&cmd->lock);
666
		return NULL;
667
	}
668
 
517 jermar 669
	spinlock_unlock(&cmd->lock);
670
	return cmd;
671
}
672
 
518 jermar 673
/** Parse argument.
674
 *
675
 * Find start and end positions of command line argument.
676
 *
677
 * @param cmdline Command line as read from the input device.
678
 * @param len Number of characters in cmdline.
679
 * @param start On entry, 'start' contains pointer to the index 
680
 *        of first unprocessed character of cmdline.
681
 *        On successful exit, it marks beginning of the next argument.
682
 * @param end Undefined on entry. On exit, 'end' points to the last character
683
 *        of the next argument.
684
 *
685
 * @return false on failure, true on success.
686
 */
687
bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end)
688
{
2113 decky 689
	index_t i;
518 jermar 690
	bool found_start = false;
691
 
692
	ASSERT(start != NULL);
693
	ASSERT(end != NULL);
694
 
695
	for (i = *start; i < len; i++) {
696
		if (!found_start) {
2572 jermar 697
			if (isspace(cmdline[i]))
518 jermar 698
				(*start)++;
699
			else
700
				found_start = true;
701
		} else {
2572 jermar 702
			if (isspace(cmdline[i]))
518 jermar 703
				break;
704
		}
705
	}
706
	*end = i - 1;
707
 
708
	return found_start;
709
}
1702 cejka 710
 
1888 jermar 711
/** @}
1702 cejka 712
 */