recover the terminal's behaviour when runing exit in finsh

This commit is contained in:
prife 2013-01-17 12:06:23 +08:00
parent b8aaa6e730
commit 8999948650
2 changed files with 28 additions and 10 deletions

View File

@ -32,7 +32,7 @@ rt_uint8_t *rt_hw_sram_init(void)
#ifdef _WIN32 #ifdef _WIN32
_exit(1); _exit(1);
#else #else
exit(1); exit(1);
#endif #endif
} }
return heap; return heap;
@ -45,11 +45,11 @@ rt_uint8_t *rt_hw_sram_init(void)
void rt_hw_win32_low_cpu(void) void rt_hw_win32_low_cpu(void)
{ {
#ifdef _WIN32 #ifdef _WIN32
/* in windows */ /* in windows */
Sleep(1000); Sleep(1000);
#else #else
/* in linux */ /* in linux */
sleep(1); sleep(1);
#endif #endif
} }
@ -67,6 +67,18 @@ _CRTIMP void __cdecl abort(void);
void rt_hw_exit(void) void rt_hw_exit(void)
{ {
rt_kprintf("RT-Thread, bye\n"); 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); exit(0);
} }
FINSH_FUNCTION_EXPORT_ALIAS(rt_hw_exit, exit, exit rt - thread); FINSH_FUNCTION_EXPORT_ALIAS(rt_hw_exit, exit, exit rt - thread);

View File

@ -129,19 +129,25 @@ static int savekey(unsigned char key)
static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam) static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam)
#else #else
static struct termios oldt, newt;
/*simulate windows' getch(), it works!!*/ /*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); tcgetattr(STDIN_FILENO, &oldt);
newt = oldt; newt = oldt;
//set termios' local mode /* set termios' local mode */
newt.c_lflag &= ~(ECHO|ICANON); newt.c_lflag &= ~(ECHO|ICANON);
tcsetattr(STDIN_FILENO, TCSANOW, &newt); tcsetattr(STDIN_FILENO, TCSANOW, &newt);
} }
void restore_stty(void)
{
/* recover terminal's attribute */
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
}
#define getch getchar #define getch getchar
static void * ThreadforKeyGet(void * lpParam) static void * ThreadforKeyGet(void * lpParam)
@ -154,7 +160,7 @@ static void * ThreadforKeyGet(void * lpParam)
/* set the getchar without buffer */ /* set the getchar without buffer */
sigfillset(&sigmask); sigfillset(&sigmask);
pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask); pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
setgetchar(); set_stty();
#endif #endif
(void)lpParam; //prevent compiler warnings (void)lpParam; //prevent compiler warnings
for (;;) for (;;)