-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.c
48 lines (40 loc) · 1.24 KB
/
common.c
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
48
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
int toggle_nonblocking_input()
{
static struct termios *saved_termios = NULL;
int ret = 0;
if (saved_termios) {
/* restore old settings */
if ((ret = tcsetattr(STDIN_FILENO, TCSANOW, saved_termios)) < 0) {
fprintf(stderr, "Failed restoring termios settings: %s\n", strerror(errno));
}
free(saved_termios);
saved_termios = NULL;
/* restore cursor */
fprintf(stderr, "\033[?25h");
} else {
struct termios new_termios;
/* get and backup current settings */
saved_termios = malloc(sizeof(struct termios));
if (tcgetattr(STDIN_FILENO, saved_termios) < 0) {
fprintf(stderr, "Failed retrieving current termios setings: %s\n", strerror(errno));
free(saved_termios);
saved_termios = NULL;
return -1;
}
memcpy(&new_termios, saved_termios, sizeof(struct termios));
/* disable echo and canonical mode (line by line input; line editing) */
new_termios.c_lflag &= ~(ICANON | ECHO);
if ((ret = tcsetattr(STDIN_FILENO, TCSANOW, &new_termios)) < 0) {
fprintf(stderr, "Failed setting to changed termios: %s\n", strerror(errno));
}
/* disable cursor */
fprintf(stderr, "\033[?25l");
}
return ret;
}