Debian Codegraph @

ttymodes.c

raw
1 /* $Id: ttymodes.c,v 1.28 2024/10/20 21:54:50 tom Exp $ */
2
3 #include <vttest.h>
4 #include <ttymodes.h>
5 #include <esc.h> /* inflush() */
6
7 #undef tabs
8
9 #ifdef TAB3
10 # define tabs TAB3
11 #else
12 # ifdef XTABS
13 # define tabs XTABS
14 # else
15 # ifdef OXTABS
16 # define tabs OXTABS
17 # else
18 # define tabs 0
19 # endif
20 # endif
21 #endif
22
23 static TTY old_modes, new_modes;
24 /* *INDENT-OFF* */
25 static struct {
26 int name;
27 int code;
28 } speeds[] = {
29 {B0, 0},
30 {B50, 50},
31 {B75, 75},
32 {B110, 110},
33 {B134, 134},
34 {B150, 150},
35 {B200, 200},
36 {B300, 300},
37 {B600, 600},
38 {B1200, 1200},
39 {B1800, 1800},
40 {B2400, 2400},
41 {B4800, 4800},
42 {B9600, 9600},
43 #ifdef B19200
44 {B19200, 19200},
45 #else
46 #ifdef EXTA
47 {EXTA, 19200},
48 #endif
49 #endif
50 #ifdef B38400
51 {B38400, 38400},
52 #else
53 #ifdef EXTB
54 {EXTB, 38400},
55 #endif
56 #endif
57 #ifdef B57600
58 {B57600, 57600},
59 #endif
60 #ifdef B115200
61 {B115200, 115200},
62 #endif
63 };
64 /* *INDENT-ON* */
65
66 #if !USE_POSIX_TERMIOS && !USE_TERMIO && USE_SGTTY
67 static struct tchars old_tchars;
68 static struct ltchars old_ltchars;
69 #endif
70
71 #if USE_POSIX_TERMIOS || USE_TERMIO
72 static void
73 disable_control_chars(TTY * modes)
74 {
75 # if USE_POSIX_TERMIOS
76 int n;
77 int temp;
78 # ifdef HAVE_POSIX_VDISABLE
79 temp = _POSIX_VDISABLE;
80 # else
81 errno = 0;
82 temp = fpathconf(0, _PC_VDISABLE);
83 if (temp == -1) {
84 if (errno != 0) {
85 restore_ttymodes();
86 failed("Cannot disable special characters!\n");
87 }
88 temp = 0377;
89 }
90 # endif
91 for (n = 0; n < NCCS; n++)
92 modes->c_cc[n] = (unsigned char) temp;
93 # else /* USE_TERMIO */
94 # ifdef VSWTCH
95 modes->c_cc[VSWTCH] = VDISABLE;
96 # endif
97 modes->c_cc[VSUSP] = VDISABLE;
98 # if defined (VDSUSP) && defined(NCCS) && VDSUSP < NCCS
99 modes->c_cc[VDSUSP] = VDISABLE;
100 # endif
101 modes->c_cc[VSTART] = VDISABLE;
102 modes->c_cc[VSTOP] = VDISABLE;
103 # endif
104 modes->c_cc[VMIN] = 1;
105 modes->c_cc[VTIME] = 0;
106 }
107 #endif
108
109 static void
110 set_ttymodes(TTY * modes)
111 {
112 # if USE_POSIX_TERMIOS
113 fflush(stdout);
114 tcsetattr(0, TCSAFLUSH, modes);
115 # else
116 # if USE_TERMIO
117 tcsetattr(0, TCSETAF, modes);
118 # else /* USE_SGTTY */
119 stty(0, modes);
120 # endif
121 # endif
122 }
123
124 #ifndef log_ttymodes
125 void
126 log_ttymodes(char *file, int line)
127 {
128 if (LOG_ENABLED)
129 fprintf(log_fp, NOTE_STR "%s @%d\n", file, line);
130 }
131 #endif
132
133 #ifndef dump_ttymodes
134 void
135 dump_ttymodes(char *tag, int flag)
136 {
137 #ifdef UNIX
138 TTY tmp_modes;
139 if (LOG_ENABLED) {
140 fprintf(log_fp, NOTE_STR "%s (%d):\n", tag, flag);
141 # if USE_POSIX_TERMIOS || USE_TERMIO
142 tcgetattr(0, &tmp_modes);
143 fprintf(log_fp, NOTE_STR " iflag %08o\n", tmp_modes.c_iflag);
144 fprintf(log_fp, NOTE_STR " oflag %08o\n", tmp_modes.c_oflag);
145 fprintf(log_fp, NOTE_STR " lflag %08o\n", tmp_modes.c_lflag);
146 if (!(tmp_modes.c_lflag & ICANON)) {
147 fprintf(log_fp, NOTE_STR " %d:min =%d\n", VMIN, tmp_modes.c_cc[VMIN]);
148 fprintf(log_fp, NOTE_STR " %d:time =%d\n", VTIME, tmp_modes.c_cc[VTIME]);
149 }
150 # else
151 gtty(0, &tmp_modes);
152 fprintf(log_fp, NOTE_STR " flags %08o\n", tmp_modes.sg_flags);
153 # endif
154 }
155 #endif
156 }
157 #endif
158
159 void
160 close_tty(void)
161 {
162 restore_ttymodes();
163 }
164
165 void
166 init_ttymodes(int pn)
167 {
168 dump_ttymodes("init_ttymodes", pn);
169
170 #ifdef UNIX
171 if (pn == 0) {
172 int speed_code, n;
173
174 fflush(stdout);
175 # if USE_POSIX_TERMIOS || USE_TERMIO
176 tcgetattr(0, &old_modes);
177 speed_code = (int) cfgetospeed(&old_modes);
178 # else
179 # if USE_SGTTY
180 gtty(0, &old_modes);
181 ioctl(0, TIOCGETC, &old_tchars);
182 ioctl(0, TIOCGLTC, &old_ltchars);
183 speed_code = old_modes.sg_ospeed;
184 # endif
185 # endif
186 new_modes = old_modes;
187 for (n = 0; n < TABLESIZE(speeds); n++) {
188 if (speeds[n].name == speed_code) {
189 tty_speed = speeds[n].code;
190 break;
191 }
192 }
193 } else {
194 putchar(BEL);
195 fflush(stdout);
196 inflush();
197 new_modes = old_modes;
198 sleep(2);
199 }
200 # if USE_POSIX_TERMIOS || USE_TERMIO
201 new_modes.c_iflag = BRKINT | old_modes.c_iflag;
202 new_modes.c_oflag &= (unsigned) ~tabs;
203 # else /* USE_SGTTY */
204 new_modes.sg_flags = old_modes.sg_flags | CBREAK;
205 # endif
206 set_ttymodes(&new_modes);
207 # ifdef HAVE_FCNTL_H
208 # ifndef O_NDELAY
209 # define O_NDELAY O_NONBLOCK /* O_NONBLOCK is POSIX */
210 # endif
211 close(2);
212 if (open("/dev/tty", O_RDWR | O_NDELAY) != 2) {
213 restore_ttymodes();
214 failed("Cannot initialize tty modes!\n");
215 }
216 # endif
217 #endif /* UNIX */
218 dump_ttymodes("...init_ttymodes", pn);
219 }
220
221 void
222 restore_ttymodes(void)
223 {
224 dump_ttymodes("restore_ttymodes", -1);
225 #ifdef UNIX
226 set_ttymodes(&old_modes);
227 #endif
228 dump_ttymodes("...restore_ttymodes", -1);
229 }
230
231 void
232 set_tty_crmod(int enabled)
233 {
234 dump_ttymodes("set_tty_crmod", enabled);
235 #ifdef UNIX
236 # if USE_POSIX_TERMIOS || USE_TERMIO
237 # if USE_POSIX_TERMIOS
238 # define MASK_CRMOD ((unsigned) (ICRNL | IXON))
239 # else
240 # define MASK_CRMOD ((unsigned) (ICRNL))
241 # endif
242 if (enabled) {
243 new_modes.c_iflag |= MASK_CRMOD;
244 new_modes.c_lflag |= ICANON;
245 memcpy(new_modes.c_cc, old_modes.c_cc, sizeof(new_modes.c_cc));
246 } else {
247 new_modes.c_iflag &= ~MASK_CRMOD;
248 new_modes.c_lflag &= (unsigned) ~ICANON;
249 disable_control_chars(&new_modes);
250 }
251 # else
252 if (enabled)
253 new_modes.sg_flags |= CRMOD;
254 else
255 new_modes.sg_flags &= ~CRMOD;
256 # endif
257 set_ttymodes(&new_modes);
258 #endif
259 dump_ttymodes("...set_tty_crmod", enabled);
260 }
261
262 void
263 set_tty_echo(int enabled)
264 {
265 dump_ttymodes("set_tty_echo", enabled);
266 #ifdef UNIX
267 # if USE_POSIX_TERMIOS || USE_TERMIO
268 if (enabled)
269 new_modes.c_lflag |= ECHO;
270 else
271 new_modes.c_lflag &= (unsigned) ~ECHO;
272 # else /* USE_SGTTY */
273 if (enabled)
274 new_modes.sg_flags |= ECHO;
275 else
276 new_modes.sg_flags &= (unsigned) ~ECHO;
277 # endif
278 set_ttymodes(&new_modes);
279 #endif
280 dump_ttymodes("...set_tty_echo", enabled);
281 }
282
283 void
284 set_tty_raw(int enabled)
285 {
286 dump_ttymodes("set_tty_raw", enabled);
287 if (enabled) {
288 #ifdef UNIX
289 # if USE_POSIX_TERMIOS || USE_TERMIO
290 /* *INDENT-EQLS* */
291 new_modes.c_iflag = 0;
292 new_modes.c_lflag = 0;
293 new_modes.c_cc[VMIN] = 1;
294 new_modes.c_cc[VTIME] = 0;
295 set_ttymodes(&new_modes);
296 set_tty_crmod(FALSE);
297 # else /* USE_SGTTY */
298 # ifdef HAVE_FCNTL_H
299 new_modes.sg_flags &= ~CBREAK;
300 # endif
301 new_modes.sg_flags |= RAW;
302 set_ttymodes(&new_modes);
303 {
304 struct tchars tmp_tchars;
305 struct ltchars tmp_ltchars;
306 memset(&tmp_tchars, -1, sizeof(tmp_tchars));
307 memset(&tmp_ltchars, -1, sizeof(tmp_ltchars));
308 ioctl(0, TIOCSETC, &tmp_tchars);
309 ioctl(0, TIOCSLTC, &tmp_ltchars);
310 }
311 # endif
312 #endif
313 } else {
314 #ifdef UNIX
315 # if USE_POSIX_TERMIOS || USE_TERMIO
316 new_modes = old_modes;
317 # else /* USE_SGTTY */
318 new_modes.sg_flags &= ~RAW;
319 # ifdef HAVE_FCNTL_H
320 new_modes.sg_flags |= CBREAK;
321 # endif
322 ioctl(0, TIOCSETC, &old_tchars);
323 ioctl(0, TIOCSLTC, &old_ltchars);
324 # endif
325 set_ttymodes(&new_modes);
326 #endif
327 }
328 dump_ttymodes("...set_tty_raw", enabled);
329 }