Subversion Repositories HelenOS-historic

Rev

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

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