提交 cb713bc9 编写于 作者: B bernard.xiong

fix backspace issue and fix device read issue in shell.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@516 bbd45198-f89e-11dd-88c7-29a3b14d5316
上级 b4bcc7b5
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
* 2006-06-03 Bernard add support for skyeye * 2006-06-03 Bernard add support for skyeye
* 2006-09-24 Bernard remove the code related with hardware * 2006-09-24 Bernard remove the code related with hardware
* 2010-01-18 Bernard fix down then up key bug. * 2010-01-18 Bernard fix down then up key bug.
* 2010-03-19 Bernard fix backspace issue and fix device read in shell.
*/ */
#include <rtthread.h> #include <rtthread.h>
...@@ -57,9 +58,7 @@ extern char rt_serial_getc(void); ...@@ -57,9 +58,7 @@ extern char rt_serial_getc(void);
struct rt_thread finsh_thread; struct rt_thread finsh_thread;
char finsh_thread_stack[2048]; char finsh_thread_stack[2048];
struct rt_semaphore uart_sem; struct rt_semaphore uart_sem;
#ifdef RT_USING_DEVICE
rt_device_t finsh_device; rt_device_t finsh_device;
#endif
#ifdef FINSH_USING_HISTORY #ifdef FINSH_USING_HISTORY
enum input_stat enum input_stat
{ {
...@@ -148,7 +147,6 @@ int isprint(unsigned char ch) ...@@ -148,7 +147,6 @@ int isprint(unsigned char ch)
#endif #endif
#endif #endif
#ifdef RT_USING_DEVICE
static rt_err_t finsh_rx_ind(rt_device_t dev, rt_size_t size) static rt_err_t finsh_rx_ind(rt_device_t dev, rt_size_t size)
{ {
/* release semaphore to let finsh thread rx data */ /* release semaphore to let finsh thread rx data */
...@@ -178,12 +176,6 @@ void finsh_set_device(char* device_name) ...@@ -178,12 +176,6 @@ void finsh_set_device(char* device_name)
rt_kprintf("finsh: can not find device:%s\n", device_name); rt_kprintf("finsh: can not find device:%s\n", device_name);
} }
} }
#else
void finsh_notify()
{
rt_sem_release(&uart_sem);
}
#endif
void finsh_auto_complete(char* prefix) void finsh_auto_complete(char* prefix)
{ {
...@@ -194,10 +186,50 @@ void finsh_auto_complete(char* prefix) ...@@ -194,10 +186,50 @@ void finsh_auto_complete(char* prefix)
rt_kprintf("finsh>>%s", prefix); rt_kprintf("finsh>>%s", prefix);
} }
void finsh_run_line(struct finsh_parser *parser, const char* line)
{
rt_kprintf("\n");
finsh_parser_run(parser, (unsigned char*)line);
/* compile node root */
if (finsh_errno() == 0)
{
finsh_compiler_run(parser->root);
}
else
{
rt_kprintf("%s\n", finsh_error_string(finsh_errno()));
}
/* 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());
}
}
finsh_flush(parser);
}
void finsh_thread_entry(void* parameter) void finsh_thread_entry(void* parameter)
{ {
struct finsh_parser parser; struct finsh_parser parser;
char line[256]; char line[256], ch;
int pos ; int pos ;
#ifdef FINSH_USING_HISTORY #ifdef FINSH_USING_HISTORY
enum input_stat stat; enum input_stat stat;
...@@ -207,32 +239,18 @@ void finsh_thread_entry(void* parameter) ...@@ -207,32 +239,18 @@ void finsh_thread_entry(void* parameter)
stat = WAIT_NORMAL; stat = WAIT_NORMAL;
current_history = 0; current_history = 0;
use_history = 0; use_history = 0;
memset(line, 0, sizeof(line));
finsh_init(&parser); finsh_init(&parser);
while (1) while (1)
{ {
rt_kprintf("finsh>>"); if (rt_sem_take(&uart_sem, RT_WAITING_FOREVER) != RT_EOK) continue;
memset(line, 0, sizeof(line));
pos = 0;
while (1)
{
if (rt_sem_take(&uart_sem, -1) == RT_EOK)
{
/* read one character from serial */
char ch;
#ifndef RT_USING_DEVICE /* read one character from device */
ch = rt_serial_getc(); while (rt_device_read(finsh_device, 0, &ch, 1) == 1)
if (ch != 0)
#else
rt_err_t rx_result;
rx_result = rt_device_read(finsh_device, 0, &ch, 1);
if (ch != 0 && rx_result == 1)
#endif
{ {
#ifdef FINSH_USING_HISTORY #ifdef FINSH_USING_HISTORY
/* /*
* handle up and down key * handle up and down key
* up key : 0x1b 0x5b 0x41 * up key : 0x1b 0x5b 0x41
...@@ -302,10 +320,9 @@ void finsh_thread_entry(void* parameter) ...@@ -302,10 +320,9 @@ void finsh_thread_entry(void* parameter)
continue; continue;
} }
} }
#endif #endif
/*
* handle tab key /* handle tab key */
*/
if (ch == '\t') if (ch == '\t')
{ {
/* auto complete */ /* auto complete */
...@@ -314,44 +331,25 @@ void finsh_thread_entry(void* parameter) ...@@ -314,44 +331,25 @@ void finsh_thread_entry(void* parameter)
pos = strlen(line); pos = strlen(line);
continue; continue;
} }
/* handle backspace key */
/* else if (ch == 0x7f || ch == 0x08)
* handle backspace key
*/
if (ch == 0x7f || ch == 0x08)
{ {
if (pos != 0)rt_kprintf("%c", ch); if (pos != 0)
line[pos--] = 0; {
if (pos < 0) pos = 0; rt_kprintf("%c %c", ch, ch);
}
if (pos <= 0) pos = 0;
else pos --;
line[pos] = 0;
continue; continue;
} }
/* handle end of line, break */
if (pos >= 256) /* it's a large line, discard it */ else if (ch == 0x0d || ch == 0x0a)
pos = 0;
line[pos] = ch;
rt_kprintf("%c", line[pos]);
/* if it's the end of line, break */
if (line[pos] == 0xd || line[pos] == 0xa)
{ {
/* change to ';' and break */ /* change to ';' and break */
line[pos] = ';'; line[pos] = ';';
break;
}
else use_history = 0; /* not "\n", it's a new command */
pos ++;
}
}
}
if (pos == 0) #ifdef FINSH_USING_HISTORY
{
rt_kprintf("\n");
continue;
}
#ifdef FINSH_USING_HISTORY
if (use_history == 0) if (use_history == 0)
{ {
/* push history */ /* push history */
...@@ -380,42 +378,26 @@ void finsh_thread_entry(void* parameter) ...@@ -380,42 +378,26 @@ void finsh_thread_entry(void* parameter)
} }
} }
current_history = finsh_history_count; current_history = finsh_history_count;
#endif #endif
rt_kprintf("\n"); if (pos != 0) finsh_run_line(&parser, line);
finsh_parser_run(&parser, (unsigned char*)&line[0]); else rt_kprintf("\n");
/* compile node root */ rt_kprintf("finsh>>");
if (finsh_errno() == 0) memset(line, 0, sizeof(line));
{ pos = 0;
finsh_compiler_run(parser.root);
}
else
{
rt_kprintf("%s\n", finsh_error_string(finsh_errno()));
}
/* run virtual machine */
if (finsh_errno() == 0)
{
finsh_vm_run();
if (isprint((unsigned char)finsh_stack_bottom())) break;
{
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%04x\n",
(unsigned int)finsh_stack_bottom(),
(unsigned int)finsh_stack_bottom());
}
} }
finsh_flush(&parser); /* it's a large line, discard it */
if (pos >= 256) pos = 0;
/* normal character */
line[pos] = ch; ch = 0;
rt_kprintf("%c", line[pos++]);
use_history = 0; /* it's a new command */
} /* end of device read */
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册