summaryrefslogtreecommitdiffhomepage
path: root/src/blog/termios/enable-xoff-xon.diff
blob: 64841d2d966b606beca91b046728859ef860779c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 #include <curses.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <termios.h>
 #include <unistd.h>
 #include <vis.h>

+static void restore_tcattrs(void);
+
+struct termios old_attrs;
+
 int
 main(void)
 {
 	struct termios tp;
 	tcgetattr(STDIN_FILENO, &tp);
+	old_attrs = tp;
 	tp.c_iflag &= ~(IXANY | IXON | IXOFF);
 	tcsetattr(STDIN_FILENO, TCSANOW, &tp);
+	atexit(restore_tcattrs);

 	initscr();                  /* Init screen */
 	noecho();                   /* Don’t echo keypresses */
@@ -25,19 +18,10 @@
 	for (;;) {
 		/* Read char and encode it as a string  */
 		char buf[5];
+		int ch = getch();
+		if (ch == '\x11')       /* ^Q */
+			break;
+		vis(buf, ch, VIS_WHITE, 0);
-		vis(buf, getch(), VIS_WHITE, 0);

 		clear();                /* Clear screen */
 		printw("%s\n", buf);    /* Write char to screen */
 		refresh();              /* Refresh display */
 	}
-	/* unreachable */
+	endwin();                   /* Destroy screen */
+	return EXIT_SUCCESS;
 }
+
+void
+restore_tcattrs(void)
+{
+	tcsetattr(STDIN_FILENO, TCSANOW, &old_attrs);
+}