+static int enable_echo(int enable)
+{
+ const char *portname = "/dev/tty";
+ struct termios tty;
+ int retval = -1;
+ int ret;
+ int fd;
+
+ memset(&tty, 0, sizeof(tty));
+
+ fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
+ if (fd < 0) {
+ fprintf(stderr, "open %s: ", portname);
+ perror("");
+ return 1;
+ }
+
+ ret = tcgetattr(fd, &tty);
+ if (ret != 0) {
+ perror("tcgetattr");
+ goto out;
+ }
+
+ // printf("lflag: 0%o\n", tty.c_lflag);
+ if (enable)
+ tty.c_lflag |= ECHO;
+ else
+ tty.c_lflag &= ~ECHO;
+
+ ret = tcsetattr(fd, TCSANOW, &tty);
+ if (ret != 0) {
+ perror("tcsetattr");
+ goto out;
+ }
+
+ retval = 0;
+out:
+ close(fd);
+
+ return retval;
+}
+
+static void signal_handler(int sig)
+{
+ enable_echo(1);
+ signal(sig, SIG_DFL);
+ raise(sig);
+}
+