forked from abligh/pty
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathttymodes.c
More file actions
169 lines (149 loc) · 3.38 KB
/
Copy pathttymodes.c
File metadata and controls
169 lines (149 loc) · 3.38 KB
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
163
164
165
166
167
168
169
#include "apue.h"
#include <termios.h>
#include <errno.h>
static struct termios save_termios;
static int ttysavefd = -1;
static enum
{ RESET, RAW, CBREAK } ttystate = RESET;
int
tty_cbreak (int fd) /* put terminal into a cbreak mode */
{
int err;
struct termios buf;
if (ttystate != RESET)
{
errno = EINVAL;
return (-1);
}
if (tcgetattr (fd, &buf) < 0)
return (-1);
save_termios = buf; /* structure copy */
/*
* Echo off, canonical mode off.
*/
buf.c_lflag &= ~(ECHO | ICANON);
/*
* Case B: 1 byte at a time, no timer.
*/
buf.c_cc[VMIN] = 1;
buf.c_cc[VTIME] = 0;
if (tcsetattr (fd, TCSAFLUSH, &buf) < 0)
return (-1);
/*
* Verify that the changes stuck. tcsetattr can return 0 on
* partial success.
*/
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)
{
/*
* Only some of the changes were made. Restore the
* original settings.
*/
tcsetattr (fd, TCSAFLUSH, &save_termios);
errno = EINVAL;
return (-1);
}
ttystate = CBREAK;
ttysavefd = fd;
return (0);
}
int
tty_raw (int fd) /* put terminal into a raw mode */
{
int err;
struct termios buf;
if (ttystate != RESET)
{
errno = EINVAL;
return (-1);
}
if (tcgetattr (fd, &buf) < 0)
return (-1);
save_termios = buf; /* structure copy */
/*
* Echo off, canonical mode off, extended input
* processing off, signal chars off.
*/
buf.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
/*
* No SIGINT on BREAK, CR-to-NL off, input parity
* check off, don't strip 8th bit on input, output
* flow control off.
*/
buf.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
/*
* Clear size bits, parity checking off.
*/
buf.c_cflag &= ~(CSIZE | PARENB);
/*
* Set 8 bits/char.
*/
buf.c_cflag |= CS8;
/*
* Output processing off.
*/
buf.c_oflag &= ~(OPOST);
/*
* Case B: 1 byte at a time, no timer.
*/
buf.c_cc[VMIN] = 1;
buf.c_cc[VTIME] = 0;
if (tcsetattr (fd, TCSAFLUSH, &buf) < 0)
return (-1);
/*
* Verify that the changes stuck. tcsetattr can return 0 on
* partial success.
*/
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)
{
/*
* Only some of the changes were made. Restore the
* original settings.
*/
tcsetattr (fd, TCSAFLUSH, &save_termios);
errno = EINVAL;
return (-1);
}
ttystate = RAW;
ttysavefd = fd;
return (0);
}
int
tty_reset (int fd) /* restore terminal's mode */
{
if (ttystate == RESET)
return (0);
if (tcsetattr (fd, TCSAFLUSH, &save_termios) < 0)
return (-1);
ttystate = RESET;
return (0);
}
void
tty_atexit (void) /* can be set up by atexit(tty_atexit) */
{
if (ttysavefd >= 0)
tty_reset (ttysavefd);
}
struct termios *
tty_termios (void) /* let caller see original tty state */
{
return (&save_termios);
}