shell.c 14.1 KB
Newer Older
1
/*
B
Bernard Xiong 已提交
2
 *  shell implementation for finsh shell.
3
 *
B
Bernard Xiong 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
 *
 *  This file is part of RT-Thread (http://www.rt-thread.org)
 *  Maintainer: bernard.xiong <bernard.xiong at gmail.com>
 *
 *  All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 25 26
 *
 * Change Logs:
 * Date           Author       Notes
B
Bernard Xiong 已提交
27
 * 2006-04-30     Bernard      the first version for FinSH
28 29 30
 * 2006-05-08     Bernard      change finsh thread stack to 2048
 * 2006-06-03     Bernard      add support for skyeye
 * 2006-09-24     Bernard      remove the code related with hardware
B
bernard.xiong 已提交
31
 * 2010-01-18     Bernard      fix down then up key bug.
32
 * 2010-03-19     Bernard      fix backspace issue and fix device read in shell.
33
 * 2010-04-01     Bernard      add prompt output when start and remove the empty history
34 35
 * 2011-02-23     Bernard      fix variable section end issue of finsh shell
 *                             initialization when use GNU GCC compiler.
36 37 38 39 40 41
 */

#include <rtthread.h>
#include <rthw.h>

#include "finsh.h"
42
#include "shell.h"
43

B
Bernard Xiong 已提交
44 45 46 47
#ifdef FINSH_USING_MSH
#include "msh.h"
#endif

48 49 50 51
#ifdef _WIN32
#include <stdio.h> /* for putchar */
#endif

52
/* finsh thread */
53
static struct rt_thread finsh_thread;
54
ALIGN(RT_ALIGN_SIZE)
55 56
static char finsh_thread_stack[FINSH_THREAD_STACK_SIZE];
struct finsh_shell* shell;
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

#if !defined (RT_USING_NEWLIB) && !defined (RT_USING_MINILIBC)
int strcmp (const char *s1, const char *s2)
{
	while (*s1 && *s1 == *s2) s1++, s2++;

	return (*s1 - *s2);
}

#ifdef RT_USING_HEAP
char *strdup(const char *s)
{
	size_t len = strlen(s) + 1;
	char *tmp = (char *)rt_malloc(len);

	if(!tmp) return NULL;

	rt_memcpy(tmp, s, len);
	return tmp;
}
#endif
B
bernard.xiong 已提交
78

79
#if !defined(__CC_ARM) && !defined(__IAR_SYSTEMS_ICC__) && !defined(__ADSPBLACKFIN__) && !defined(_MSC_VER)
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
int isalpha( int ch )
{
	return (unsigned int)((ch | 0x20) - 'a') < 26u;
}

int atoi(const char* s)
{
	long int v=0;
	int sign=1;
	while ( *s == ' '  ||  (unsigned int)(*s - 9) < 5u) s++;

	switch (*s)
	{
	case '-': sign=-1;
	case '+': ++s;
	}

	while ((unsigned int) (*s - '0') < 10u)
	{
		v=v*10+*s-'0'; ++s;
	}

	return sign==-1?-v:v;
}

int isprint(unsigned char ch)
{
    return (unsigned int)(ch - ' ') < 127u - ' ';
B
bernard.xiong 已提交
108
}
B
bernard.xiong 已提交
109
#endif
110 111
#endif

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
#if defined(RT_USING_DFS) && defined(DFS_USING_WORKDIR)
#include <dfs_posix.h>
const char* finsh_get_prompt()
{
	#define _PROMPT "finsh "
	static char finsh_prompt[RT_CONSOLEBUF_SIZE + 1] = {_PROMPT};
	
	/* get current working directory */
	getcwd(&finsh_prompt[6], RT_CONSOLEBUF_SIZE - 8);
	strcat(finsh_prompt, ">");

	return finsh_prompt;
}
#endif

127 128
static rt_err_t finsh_rx_ind(rt_device_t dev, rt_size_t size)
{
129 130
	RT_ASSERT(shell != RT_NULL);

131
	/* release semaphore to let finsh thread rx data */
132
	rt_sem_release(&shell->rx_sem);
133 134 135 136

	return RT_EOK;
}

B
bernard.xiong@gmail.com 已提交
137 138 139 140 141 142 143
/**
 * @ingroup finsh
 *
 * This function sets the input device of finsh shell.
 *
 * @param device_name the name of new input device.
 */
144
void finsh_set_device(const char* device_name)
145 146 147
{
	rt_device_t dev = RT_NULL;

148
	RT_ASSERT(shell != RT_NULL);
149 150 151
	dev = rt_device_find(device_name);
	if (dev != RT_NULL && rt_device_open(dev, RT_DEVICE_OFLAG_RDWR) == RT_EOK)
	{
152
		if (shell->device != RT_NULL)
153 154
		{
			/* close old finsh device */
155
			rt_device_close(shell->device);
156 157
		}

158
		shell->device = dev;
159 160 161 162 163 164 165 166
		rt_device_set_rx_indicate(dev, finsh_rx_ind);
	}
	else
	{
		rt_kprintf("finsh: can not find device:%s\n", device_name);
	}
}

B
bernard.xiong@gmail.com 已提交
167 168 169 170 171 172 173
/**
 * @ingroup finsh
 *
 * This function returns current finsh shell input device.
 *
 * @return the finsh shell input device name is returned.
 */
174 175 176 177 178 179
const char* finsh_get_device()
{
	RT_ASSERT(shell != RT_NULL);
	return shell->device->parent.name;
}

B
bernard.xiong@gmail.com 已提交
180 181 182 183 184 185 186 187 188 189
/**
 * @ingroup finsh
 *
 * This function set the echo mode of finsh shell.
 *
 * FINSH_OPTION_ECHO=0x01 is echo mode, other values are none-echo mode.
 *
 * @param echo the echo mode
 */
void finsh_set_echo(rt_uint32_t echo)
190 191
{
	RT_ASSERT(shell != RT_NULL);
192
	shell->echo_mode = (rt_uint8_t)echo;
193 194
}

B
bernard.xiong@gmail.com 已提交
195 196 197 198 199 200 201
/**
 * @ingroup finsh
 *
 * This function gets the echo mode of finsh shell.
 *
 * @return the echo mode
 */
202 203 204 205 206 207 208
rt_uint32_t finsh_get_echo()
{
	RT_ASSERT(shell != RT_NULL);

	return shell->echo_mode;
}

209 210 211 212 213
void finsh_auto_complete(char* prefix)
{
	extern void list_prefix(char* prefix);

	rt_kprintf("\n");
B
Bernard Xiong 已提交
214 215 216 217 218 219 220 221 222 223 224
#ifdef FINSH_USING_MSH
	if (msh_is_used() == RT_TRUE)
	{
		msh_auto_complete(prefix);
	}
	else 
#endif
	{
		list_prefix(prefix);
	}

225
	rt_kprintf("%s%s", FINSH_PROMPT, prefix);
226
}
227

228
void finsh_run_line(struct finsh_parser* parser, const char *line)
229
{
B
bernard.xiong 已提交
230 231
	const char* err_str;

232 233 234 235 236 237 238 239 240 241
	rt_kprintf("\n");
	finsh_parser_run(parser, (unsigned char*)line);

	/* compile node root */
	if (finsh_errno() == 0)
	{
		finsh_compiler_run(parser->root);
	}
	else
	{
B
bernard.xiong 已提交
242 243
		err_str = finsh_error_string(finsh_errno());
		rt_kprintf("%s\n", err_str);
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
	}

	/* run virtual machine */
	if (finsh_errno() == 0)
	{
		char ch;
		finsh_vm_run();

		ch = (unsigned char)finsh_stack_bottom();
		if (ch > 0x20 && ch < 0x7e)
		{
			rt_kprintf("\t'%c', %d, 0x%08x\n",
				(unsigned char)finsh_stack_bottom(),
				(unsigned int)finsh_stack_bottom(),
				(unsigned int)finsh_stack_bottom());
		}
		else
		{
			rt_kprintf("\t%d, 0x%08x\n",
				(unsigned int)finsh_stack_bottom(),
				(unsigned int)finsh_stack_bottom());
		}
	}
267

268 269 270
    finsh_flush(parser);
}

271
#ifdef FINSH_USING_HISTORY
272 273 274 275 276 277 278 279 280 281 282 283
rt_bool_t finsh_handle_history(struct finsh_shell* shell, char ch)
{
	/*
	 * handle up and down key
	 * up key  : 0x1b 0x5b 0x41
	 * down key: 0x1b 0x5b 0x42
	 */
	if (ch == 0x1b)
	{
		shell->stat = WAIT_SPEC_KEY;
		return RT_TRUE;
	}
284

285 286 287 288 289 290 291
	if ((shell->stat == WAIT_SPEC_KEY))
	{
		if (ch == 0x5b)
		{
			shell->stat = WAIT_FUNC_KEY;
			return RT_TRUE;
		}
292

293 294 295
		shell->stat = WAIT_NORMAL;
		return RT_FALSE;
	}
296

297
	if (shell->stat == WAIT_FUNC_KEY)
298
	{
299
		shell->stat = WAIT_NORMAL;
300

301
		if (ch == 0x41) /* up key */
302
		{
303 304 305
			/* prev history */
			if (shell->current_history > 0)shell->current_history --;
			else
306
			{
307 308
				shell->current_history = 0;
				return RT_TRUE;
309
			}
310

311 312 313 314 315 316 317 318 319 320 321 322
			/* copy the history command */
			memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
				FINSH_CMD_SIZE);
			shell->line_position = strlen(shell->line);
			shell->use_history = 1;
		}
		else if (ch == 0x42) /* down key */
		{
			/* next history */
			if (shell->current_history < shell->history_count - 1)
				shell->current_history ++;
			else
323
			{
324 325
				/* set to the end of history */
				if (shell->history_count != 0)
326
				{
327
					shell->current_history = shell->history_count - 1;
328
				}
329
				else return RT_TRUE;
330
			}
331

332 333 334 335 336
			memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
				FINSH_CMD_SIZE);
			shell->line_position = strlen(shell->line);
			shell->use_history = 1;
		}
B
bernard.xiong 已提交
337

338 339
		if (shell->use_history)
		{
340 341 342 343 344 345 346 347 348
#if defined(_WIN32)
			int i;
			rt_kprintf("\r");

			for(i=0; i<= 60; i++)
				putchar(' ');
			rt_kprintf("\r");

#else
349
			rt_kprintf("\033[2K\r");
350
#endif
351 352 353 354
			rt_kprintf("%s%s", FINSH_PROMPT, shell->line);
			return RT_TRUE;;
		}
	}
355

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
	return RT_FALSE;
}

void finsh_push_history(struct finsh_shell* shell)
{
	if ((shell->use_history == 0) && (shell->line_position != 0))
	{
		/* push history */
		if (shell->history_count >= FINSH_HISTORY_LINES)
		{
			/* move history */
			int index;
			for (index = 0; index < FINSH_HISTORY_LINES - 1; index ++)
			{
				memcpy(&shell->cmd_history[index][0],
					&shell->cmd_history[index + 1][0], FINSH_CMD_SIZE);
372
			}
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
			memset(&shell->cmd_history[index][0], 0, FINSH_CMD_SIZE);
			memcpy(&shell->cmd_history[index][0], shell->line, shell->line_position);

			/* it's the maximum history */
			shell->history_count = FINSH_HISTORY_LINES;
		}
		else
		{
			memset(&shell->cmd_history[shell->history_count][0], 0, FINSH_CMD_SIZE);
			memcpy(&shell->cmd_history[shell->history_count][0], shell->line, shell->line_position);

			/* increase count and set current history position */
			shell->history_count ++;
		}
	}
	shell->current_history = shell->history_count;
}
#endif

#ifndef RT_USING_HEAP
struct finsh_shell _shell;
#endif
void finsh_thread_entry(void* parameter)
{
    char ch;

	/* normal is echo mode */
	shell->echo_mode = 1;

    finsh_init(&shell->parser);
	rt_kprintf(FINSH_PROMPT);

	while (1)
	{
		/* wait receive */
		if (rt_sem_take(&shell->rx_sem, RT_WAITING_FOREVER) != RT_EOK) continue;

		/* read one character from device */
		while (rt_device_read(shell->device, 0, &ch, 1) == 1)
		{
			/* handle history key */
			#ifdef FINSH_USING_HISTORY
			if (finsh_handle_history(shell, ch) == RT_TRUE) continue;
416
			#endif
417

418 419 420 421 422 423 424 425 426
			/* handle CR key */
			if (ch == '\r')
			{
				char next;

				if (rt_device_read(shell->device, 0, &next, 1) == 1)
					ch = next;
				else ch = '\r';
			}
427
			/* handle tab key */
428
			else if (ch == '\t')
429
			{
430
				/* auto complete */
431
				finsh_auto_complete(&shell->line[0]);
432
				/* re-calculate position */
433
				shell->line_position = strlen(shell->line);
434 435 436 437 438
				continue;
			}
			/* handle backspace key */
			else if (ch == 0x7f || ch == 0x08)
			{
439
				if (shell->line_position != 0)
440
				{
441
					rt_kprintf("%c %c", ch, ch);
442
				}
443 444 445
				if (shell->line_position <= 0) shell->line_position = 0;
				else shell->line_position --;
				shell->line[shell->line_position] = 0;
446
				continue;
447
			}
448

449
			/* handle end of line, break */
450
			if (ch == '\r' || ch == '\n')
451
			{
B
Bernard Xiong 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465
#ifdef FINSH_USING_MSH
				if (msh_is_used() == RT_TRUE && shell->line_position != 0)
				{
					rt_kprintf("\n");
					msh_exec(shell->line, shell->line_position);
					#ifdef FINSH_USING_HISTORY
					finsh_push_history(shell);
					#endif
				}
				else
#endif
				{
					/* add ';' and run the command line */
					shell->line[shell->line_position] = ';';
466

B
Bernard Xiong 已提交
467 468 469
					#ifdef FINSH_USING_HISTORY
					finsh_push_history(shell);
					#endif
470

B
Bernard Xiong 已提交
471 472 473
					if (shell->line_position != 0) finsh_run_line(&shell->parser, shell->line);
					else rt_kprintf("\n");
				}
474

475 476 477
				rt_kprintf(FINSH_PROMPT);
				memset(shell->line, 0, sizeof(shell->line));
				shell->line_position = 0;
478

479
				break;
480 481
			}

482
			/* it's a large line, discard it */
483
			if (shell->line_position >= FINSH_CMD_SIZE) shell->line_position = 0;
484 485

			/* normal character */
486 487 488 489
			shell->line[shell->line_position] = ch; ch = 0;
			if (shell->echo_mode) rt_kprintf("%c", shell->line[shell->line_position]);
			shell->line_position ++;
			shell->use_history = 0; /* it's a new command */
490
		} /* end of device read */
491 492 493
	}
}

wuyangyong's avatar
wuyangyong 已提交
494
void finsh_system_function_init(const void* begin, const void* end)
495 496 497 498 499
{
	_syscall_table_begin = (struct finsh_syscall*) begin;
	_syscall_table_end = (struct finsh_syscall*) end;
}

wuyangyong's avatar
wuyangyong 已提交
500
void finsh_system_var_init(const void* begin, const void* end)
501 502 503 504 505
{
	_sysvar_table_begin = (struct finsh_sysvar*) begin;
	_sysvar_table_end = (struct finsh_sysvar*) end;
}

506 507 508 509 510
#if defined(__ICCARM__)               /* for IAR compiler */
  #ifdef FINSH_USING_SYMTAB
    #pragma section="FSymTab"
    #pragma section="VSymTab"
  #endif
511 512 513 514 515 516 517
#elif defined(__ADSPBLACKFIN__) /* for VisaulDSP++ Compiler*/
  #ifdef FINSH_USING_SYMTAB
    extern "asm" int __fsymtab_start;
    extern "asm" int __fsymtab_end;
    extern "asm" int __vsymtab_start;
    extern "asm" int __vsymtab_end;
  #endif
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
#elif defined(_MSC_VER)
#pragma section("FSymTab$a", read)
const char __fsym_begin_name[] = "__start";
const char __fsym_begin_desc[] = "begin of finsh";
__declspec(allocate("FSymTab$a")) const struct finsh_syscall __fsym_begin = 
{
	__fsym_begin_name,
	__fsym_begin_desc,
	NULL
};

#pragma section("FSymTab$z", read)
const char __fsym_end_name[] = "__end";
const char __fsym_end_desc[] = "end of finsh";
__declspec(allocate("FSymTab$z")) const struct finsh_syscall __fsym_end = 
{
	__fsym_end_name,
	__fsym_end_desc,
	NULL
};
538
#endif
B
bernard.xiong@gmail.com 已提交
539 540 541 542 543 544

/*
 * @ingroup finsh
 *
 * This function will initialize finsh shell
 */
545
void finsh_system_init(void)
546
{
547
	rt_err_t result;
548 549 550

#ifdef FINSH_USING_SYMTAB
#ifdef __CC_ARM                 /* ARM C Compiler */
wuyangyong's avatar
wuyangyong 已提交
551 552 553 554
    extern const int FSymTab$$Base;
    extern const int FSymTab$$Limit;
    extern const int VSymTab$$Base;
    extern const int VSymTab$$Limit;
555 556 557 558 559 560 561 562
	finsh_system_function_init(&FSymTab$$Base, &FSymTab$$Limit);
	finsh_system_var_init(&VSymTab$$Base, &VSymTab$$Limit);
#elif defined (__ICCARM__)      /* for IAR Compiler */
    finsh_system_function_init(__section_begin("FSymTab"),
                               __section_end("FSymTab"));
    finsh_system_var_init(__section_begin("VSymTab"),
                          __section_end("VSymTab"));
#elif defined (__GNUC__)        /* GNU GCC Compiler */
wuyangyong's avatar
wuyangyong 已提交
563 564 565 566
	extern const int __fsymtab_start;
	extern const int __fsymtab_end;
	extern const int __vsymtab_start;
	extern const int __vsymtab_end;
567
	finsh_system_function_init(&__fsymtab_start, &__fsymtab_end);
568
	finsh_system_var_init(&__vsymtab_start, &__vsymtab_end);
569 570 571
#elif defined(__ADSPBLACKFIN__) /* for VisualDSP++ Compiler */
    finsh_system_function_init(&__fsymtab_start, &__fsymtab_end);
    finsh_system_var_init(&__vsymtab_start, &__vsymtab_end);
572 573 574 575 576 577 578 579 580 581
#elif defined(_MSC_VER)
	unsigned int *ptr_begin, *ptr_end;

	ptr_begin = (unsigned int*)&__fsym_begin; ptr_begin += (sizeof(struct finsh_syscall)/sizeof(unsigned int));
	while (*ptr_begin == 0) ptr_begin ++;

	ptr_end = (unsigned int*) &__fsym_end; ptr_end --;
	while (*ptr_end == 0) ptr_end --;

	finsh_system_function_init(ptr_begin, ptr_end);
582 583 584
#endif
#endif

585 586 587 588 589
	/* create or set shell structure */
#ifdef RT_USING_HEAP
	shell = (struct finsh_shell*)rt_malloc(sizeof(struct finsh_shell));
#else
	shell = &_shell;
590 591 592 593 594 595
#endif
	if (shell == RT_NULL)
	{
		rt_kprintf("no memory for shell\n");
		return;
	}
B
bernard.xiong 已提交
596
	
597 598 599
	memset(shell, 0, sizeof(struct finsh_shell));

	rt_sem_init(&(shell->rx_sem), "shrx", 0, 0);
600
	result = rt_thread_init(&finsh_thread,
601 602 603
		"tshell",
		finsh_thread_entry, RT_NULL,
		&finsh_thread_stack[0], sizeof(finsh_thread_stack),
604
		FINSH_THREAD_PRIORITY, 10);
605

606 607
	if (result == RT_EOK)
		rt_thread_startup(&finsh_thread);
608
}