-
Notifications
You must be signed in to change notification settings - Fork 2
/
tty.c
162 lines (136 loc) · 3.84 KB
/
tty.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/***********************************************************************
Theater Ticket Management System
Copyright(C) 2017 hepangda
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
3 any later version.
This program is distributed in the hope that it will be useful,
buf WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY of FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have recived a copy of the GNU Gereral Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Author: hepangda
E-mail: [email protected]
*************************************************************************/
#include"include/define.h"
#include"include/tty.h"
#include<stddef.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ioctl.h>
#include<sys/stat.h>
#include<string.h>
#include<errno.h>
int tty_disp(int option) {
int err, fd = STDIN_FILENO;
struct termios term;
if (tcgetattr(fd, &term) == -1) {
return 1;
}
if (option) {
term.c_lflag |= ECHO;
} else {
term.c_lflag &= ~ECHO;
}
err = tcsetattr(fd, TCSAFLUSH, &term);
if (err == -1 && err == EINTR) {
return 1;
}
return 0;
}
int tty_cbreak() {
int err, fd = STDIN_FILENO;
struct termios buf;
if (ttystate != RESET) {
errno = EINVAL;
return -1;
}
if (tcgetattr(fd, &buf) < 0)
return -1;
save_termios = buf;
buf.c_lflag &= ~(ECHO | ICANON);
buf.c_cc[VMIN] = 1;
buf.c_cc[VTIME] = 0;
if (tcsetattr(fd, TCSAFLUSH, &buf) < 0)
return -1;
if (tcgetattr(fd, &buf) < 0) {
err = errno;
tcsetattr(fd, TCSAFLUSH, &save_termios);
errno = err;
return -1;
}
if ((buf.c_lflag & (ECHO | ICANON)) || buf.c_cc[VMIN] != 1 ||
buf.c_cc[VTIME] != 0) {
tcsetattr(fd, TCSAFLUSH, &save_termios);
errno = EINVAL;
return -1;
}
ttystate = CBREAK;
ttysavefd = fd;
return 0;
}
int tty_raw() {
int err, fd = STDIN_FILENO;
struct termios buf;
if (ttystate != RESET) {
errno = EINVAL;
return -1;
}
if (tcgetattr(fd, &buf) < 0)
return -1;
save_termios = buf;
buf.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
buf.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
buf.c_cflag &= ~(CSIZE | PARENB);
buf.c_cflag |= CS8;
buf.c_oflag &= ~(OPOST);
buf.c_cc[VMIN] = 1;
buf.c_cc[VTIME] = 0;
if (tcsetattr(fd, TCSAFLUSH, &buf) < 0)
return -1;
if (tcgetattr(fd, &buf) < 0) {
err = errno;
tcsetattr(fd, TCSAFLUSH, &save_termios);
errno = err;
return -1;
}
if ((buf.c_lflag & (ECHO | ICANON | IEXTEN | ISIG)) ||
(buf.c_iflag & (BRKINT | ICRNL | INPCK | ISTRIP | IXON)) ||
(buf.c_cflag & (CSIZE | PARENB | CS8)) != CS8 ||
(buf.c_oflag & OPOST) || buf.c_cc[VMIN] != 1 ||
buf.c_cc[VTIME] != 0) {
tcsetattr(fd, TCSAFLUSH, &save_termios);
errno = EINVAL;
return -1;
}
ttystate = RAW;
ttysavefd = fd;
return 0;
}
int tty_reset() {
int fd = STDIN_FILENO;
if (ttystate == RESET)
return 0;
if (tcsetattr(fd, TCSAFLUSH, &save_termios) < 0)
return -1;
ttystate = RESET;
return 0;
}
void tty_atexit() {
if (ttysavefd >= 0)
tty_reset(ttysavefd);
}
struct termios *tty_termios() {
return &save_termios;
}
struct winsize tty_winsize() {
int fd = STDIN_FILENO;
struct winsize ret;
int succeed = ioctl(fd, TIOCGWINSZ, (char *)&ret);
if (succeed < 0) {
return ret;
}
return ret;
}