From 8999948650a3c1ba1417a68fb31122404dde4b89 Mon Sep 17 00:00:00 2001 From: prife Date: Thu, 17 Jan 2013 12:06:23 +0800 Subject: [PATCH] recover the terminal's behaviour when runing exit in finsh --- bsp/simlinux/drivers/board.c | 20 ++++++++++++++++---- bsp/simlinux/drivers/usart_sim.c | 18 ++++++++++++------ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/bsp/simlinux/drivers/board.c b/bsp/simlinux/drivers/board.c index 1ece5c8831..c39053e5da 100755 --- a/bsp/simlinux/drivers/board.c +++ b/bsp/simlinux/drivers/board.c @@ -32,7 +32,7 @@ rt_uint8_t *rt_hw_sram_init(void) #ifdef _WIN32 _exit(1); #else - exit(1); + exit(1); #endif } return heap; @@ -45,11 +45,11 @@ rt_uint8_t *rt_hw_sram_init(void) void rt_hw_win32_low_cpu(void) { #ifdef _WIN32 - /* in windows */ + /* in windows */ Sleep(1000); #else - /* in linux */ - sleep(1); + /* in linux */ + sleep(1); #endif } @@ -67,6 +67,18 @@ _CRTIMP void __cdecl abort(void); void rt_hw_exit(void) { rt_kprintf("RT-Thread, bye\n"); +#if !defined(_WIN32) && defined(__GNUC__) + /* * + * getchar reads key from buffer, while finsh need an non-buffer getchar + * in windows, getch is such an function, in linux, we had to change + * the behaviour of terminal to get an non-buffer getchar. + * in usart_sim.c, set_stty is called to do this work + * */ + { + extern void restore_stty(void); + restore_stty(); + } +#endif exit(0); } FINSH_FUNCTION_EXPORT_ALIAS(rt_hw_exit, exit, exit rt - thread); diff --git a/bsp/simlinux/drivers/usart_sim.c b/bsp/simlinux/drivers/usart_sim.c index 5ce223fb2b..0e9c528efe 100755 --- a/bsp/simlinux/drivers/usart_sim.c +++ b/bsp/simlinux/drivers/usart_sim.c @@ -129,19 +129,25 @@ static int savekey(unsigned char key) static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam) #else +static struct termios oldt, newt; /*simulate windows' getch(), it works!!*/ -static void setgetchar(void) +void set_stty(void) { - struct termios oldt, newt; - - // get terminal input's attribute + /* get terminal input's attribute */ tcgetattr(STDIN_FILENO, &oldt); newt = oldt; - //set termios' local mode + /* set termios' local mode */ newt.c_lflag &= ~(ECHO|ICANON); tcsetattr(STDIN_FILENO, TCSANOW, &newt); } + +void restore_stty(void) +{ + /* recover terminal's attribute */ + tcsetattr(STDIN_FILENO, TCSANOW, &oldt); +} + #define getch getchar static void * ThreadforKeyGet(void * lpParam) @@ -154,7 +160,7 @@ static void * ThreadforKeyGet(void * lpParam) /* set the getchar without buffer */ sigfillset(&sigmask); pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask); - setgetchar(); + set_stty(); #endif (void)lpParam; //prevent compiler warnings for (;;) -- GitLab