Subversion Repositories HelenOS

Rev

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