1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

* make pseudoterm reset terminal on sigint, make it honour ctrl-c

This commit is contained in:
Kaspar Schleiser 2010-11-29 16:17:44 +01:00
parent 14e2a4718f
commit e556579282

View File

@ -36,6 +36,12 @@ int init() {
return result; return result;
} }
struct termios old_term_setting;
void close_tty() {
tcsetattr(tty_fd, TCSANOW, &old_term_setting);
}
void sig_handler(int signal) { void sig_handler(int signal) {
if (signal == SIGUSR1) { if (signal == SIGUSR1) {
if (stopped) { if (stopped) {
@ -43,6 +49,7 @@ void sig_handler(int signal) {
printf("\nSignal received, opening port.\r\n"); printf("\nSignal received, opening port.\r\n");
if (init() < 0) { if (init() < 0) {
printf("Cannot open port.\r\n"); printf("Cannot open port.\r\n");
close_tty();
exit(1); exit(1);
} }
} }
@ -53,6 +60,12 @@ void sig_handler(int signal) {
pthread_cancel(serial_reader); pthread_cancel(serial_reader);
close_serial_port(); close_serial_port();
} }
} else if (signal == SIGINT) {
printf("SIGINT received, exiting...\n");
pthread_cancel(serial_reader);
close_serial_port();
close_tty();
exit(0);
} }
} }
@ -65,10 +78,14 @@ int open_tty(void)
if (fd < 0) return -1; if (fd < 0) return -1;
r = tcgetattr(fd, &term_setting); r = tcgetattr(fd, &term_setting);
if (r != 0) return -2; if (r != 0) return -2;
old_term_setting = term_setting;
term_setting.c_oflag |= ( ONLRET ); term_setting.c_oflag |= ( ONLRET );
term_setting.c_iflag |= (IGNBRK | IGNPAR); term_setting.c_iflag |= (/*IGNBRK |*/ BRKINT | IGNPAR);
term_setting.c_iflag &= ~(ISTRIP | BRKINT); term_setting.c_iflag &= ~(ISTRIP);
term_setting.c_lflag &= ~(ICANON | ISIG | ECHO); term_setting.c_lflag &= ~(ICANON |/* ISIG |*/ ECHO);
term_setting.c_lflag |= ( ISIG );
term_setting.c_cflag |= CREAD; term_setting.c_cflag |= CREAD;
term_setting.c_cc[VMIN] = 1; term_setting.c_cc[VMIN] = 1;
term_setting.c_cc[VTIME] = 1; term_setting.c_cc[VTIME] = 1;
@ -80,10 +97,12 @@ int open_tty(void)
void install_sighandler() { void install_sighandler() {
struct sigaction action; struct sigaction action;
sigemptyset (&action.sa_mask); sigemptyset (&action.sa_mask);
sigaddset( &action.sa_mask, SIGINT );
sigaddset( &action.sa_mask, SIGUSR1 ); sigaddset( &action.sa_mask, SIGUSR1 );
sigaddset( &action.sa_mask, SIGUSR2 ); sigaddset( &action.sa_mask, SIGUSR2 );
action.sa_flags = 0; action.sa_flags = 0;
action.sa_handler = sig_handler; action.sa_handler = sig_handler;
sigaction(SIGINT, &action, NULL);
sigaction(SIGUSR1, &action, NULL); sigaction(SIGUSR1, &action, NULL);
sigaction(SIGUSR2, &action, NULL); sigaction(SIGUSR2, &action, NULL);
} }
@ -120,6 +139,7 @@ int main(int argc, char** argv) {
write_serial_port(ttybuf, i); write_serial_port(ttybuf, i);
} }
close_serial_port(); close_serial_port();
close_tty();
system("tset -c"); system("tset -c");
return 0; return 0;
} }
@ -128,6 +148,7 @@ int main(int argc, char** argv) {
write_serial_port(ttybuf,n); write_serial_port(ttybuf,n);
} }
close_tty();
close_serial_port(); close_serial_port();
return 0; return 0;
} }