xref: /freebsd/contrib/libedit/tty.h (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*	$NetBSD: tty.h,v 1.24 2021/07/31 20:51:32 andvar Exp $	*/
2 
3 /*-
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Christos Zoulas of Cornell University.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)tty.h	8.1 (Berkeley) 6/4/93
35  */
36 
37 /*
38  * el.tty.h: Local terminal header
39  */
40 #ifndef _h_el_tty
41 #define	_h_el_tty
42 
43 #include <termios.h>
44 #include <unistd.h>
45 
46 /* Define our own since everyone gets it wrong! */
47 #define	CONTROL(A)	((A) & 037)
48 
49 /*
50  * Aix compatible names
51  */
52 # if defined(VWERSE) && !defined(VWERASE)
53 #  define VWERASE VWERSE
54 # endif /* VWERSE && !VWERASE */
55 
56 # if defined(VDISCRD) && !defined(VDISCARD)
57 #  define VDISCARD VDISCRD
58 # endif /* VDISCRD && !VDISCARD */
59 
60 # if defined(VFLUSHO) && !defined(VDISCARD)
61 #  define VDISCARD VFLUSHO
62 # endif  /* VFLUSHO && VDISCARD */
63 
64 # if defined(VSTRT) && !defined(VSTART)
65 #  define VSTART VSTRT
66 # endif /* VSTRT && ! VSTART */
67 
68 # if defined(VSTAT) && !defined(VSTATUS)
69 #  define VSTATUS VSTAT
70 # endif /* VSTAT && ! VSTATUS */
71 
72 # ifndef ONLRET
73 #  define ONLRET 0
74 # endif /* ONLRET */
75 
76 # ifndef TAB3
77 #  ifdef OXTABS
78 #   define TAB3 OXTABS
79 #  else
80 #   define TAB3 0
81 #  endif /* OXTABS */
82 # endif /* !TAB3 */
83 
84 # if defined(OXTABS) && !defined(XTABS)
85 #  define XTABS OXTABS
86 # endif /* OXTABS && !XTABS */
87 
88 # ifndef ONLCR
89 #  define ONLCR 0
90 # endif /* ONLCR */
91 
92 # ifndef IEXTEN
93 #  define IEXTEN 0
94 # endif /* IEXTEN */
95 
96 # ifndef ECHOCTL
97 #  define ECHOCTL 0
98 # endif /* ECHOCTL */
99 
100 # ifndef PARENB
101 #  define PARENB 0
102 # endif /* PARENB */
103 
104 # ifndef EXTPROC
105 #  define EXTPROC 0
106 # endif /* EXTPROC */
107 
108 # ifndef FLUSHO
109 #  define FLUSHO  0
110 # endif /* FLUSHO */
111 
112 
113 # if defined(VDISABLE) && !defined(_POSIX_VDISABLE)
114 #  define _POSIX_VDISABLE VDISABLE
115 # endif /* VDISABLE && ! _POSIX_VDISABLE */
116 
117 /*
118  * Work around ISC's definition of IEXTEN which is
119  * XCASE!
120  */
121 # ifdef ISC
122 #  if defined(IEXTEN) && defined(XCASE)
123 #   if IEXTEN == XCASE
124 #    undef IEXTEN
125 #    define IEXTEN 0
126 #   endif /* IEXTEN == XCASE */
127 #  endif /* IEXTEN && XCASE */
128 #  if defined(IEXTEN) && !defined(XCASE)
129 #   define XCASE IEXTEN
130 #   undef IEXTEN
131 #   define IEXTEN 0
132 #  endif /* IEXTEN && !XCASE */
133 # endif /* ISC */
134 
135 /*
136  * Work around convex weirdness where turning off IEXTEN makes us
137  * lose all postprocessing!
138  */
139 #if defined(convex) || defined(__convex__)
140 # if defined(IEXTEN) && IEXTEN != 0
141 #  undef IEXTEN
142 #  define IEXTEN 0
143 # endif /* IEXTEN != 0 */
144 #endif /* convex || __convex__ */
145 
146 /*
147  * So that we don't lose job control.
148  */
149 #ifdef __SVR4
150 # undef CSWTCH
151 #endif
152 
153 #ifndef _POSIX_VDISABLE
154 # define _POSIX_VDISABLE ((unsigned char) -1)
155 #endif /* _POSIX_VDISABLE */
156 
157 #if !defined(CREPRINT) && defined(CRPRNT)
158 # define CREPRINT CRPRNT
159 #endif /* !CREPRINT && CRPRNT */
160 #if !defined(CDISCARD) && defined(CFLUSH)
161 # define CDISCARD CFLUSH
162 #endif /* !CDISCARD && CFLUSH */
163 
164 #ifndef CINTR
165 # define CINTR		CONTROL('c')
166 #endif /* CINTR */
167 #ifndef CQUIT
168 # define CQUIT		034	/* ^\ */
169 #endif /* CQUIT */
170 #ifndef CERASE
171 # define CERASE		0177	/* ^? */
172 #endif /* CERASE */
173 #ifndef CKILL
174 # define CKILL		CONTROL('u')
175 #endif /* CKILL */
176 #ifndef CEOF
177 # define CEOF		CONTROL('d')
178 #endif /* CEOF */
179 #ifndef CEOL
180 # define CEOL		_POSIX_VDISABLE
181 #endif /* CEOL */
182 #ifndef CEOL2
183 # define CEOL2		_POSIX_VDISABLE
184 #endif /* CEOL2 */
185 #ifndef CSWTCH
186 # define CSWTCH		_POSIX_VDISABLE
187 #endif /* CSWTCH */
188 #ifndef CDSWTCH
189 # define CDSWTCH	_POSIX_VDISABLE
190 #endif /* CDSWTCH */
191 #ifndef CERASE2
192 # define CERASE2	_POSIX_VDISABLE
193 #endif /* CERASE2 */
194 #ifndef CSTART
195 # define CSTART		CONTROL('q')
196 #endif /* CSTART */
197 #ifndef CSTOP
198 # define CSTOP		CONTROL('s')
199 #endif /* CSTOP */
200 #ifndef CSUSP
201 # define CSUSP		CONTROL('z')
202 #endif /* CSUSP */
203 #ifndef CDSUSP
204 # define CDSUSP		CONTROL('y')
205 #endif /* CDSUSP */
206 
207 #ifdef hpux
208 
209 # ifndef CREPRINT
210 #  define CREPRINT	_POSIX_VDISABLE
211 # endif /* CREPRINT */
212 # ifndef CDISCARD
213 #  define CDISCARD	_POSIX_VDISABLE
214 # endif /* CDISCARD */
215 # ifndef CLNEXT
216 #  define CLNEXT	_POSIX_VDISABLE
217 # endif /* CLNEXT */
218 # ifndef CWERASE
219 #  define CWERASE	_POSIX_VDISABLE
220 # endif /* CWERASE */
221 
222 #else /* !hpux */
223 
224 # ifndef CREPRINT
225 #  define CREPRINT	CONTROL('r')
226 # endif /* CREPRINT */
227 # ifndef CDISCARD
228 #  define CDISCARD	CONTROL('o')
229 # endif /* CDISCARD */
230 # ifndef CLNEXT
231 #  define CLNEXT	CONTROL('v')
232 # endif /* CLNEXT */
233 # ifndef CWERASE
234 #  define CWERASE	CONTROL('w')
235 # endif /* CWERASE */
236 
237 #endif /* hpux */
238 
239 #ifndef CSTATUS
240 # define CSTATUS	CONTROL('t')
241 #endif /* CSTATUS */
242 #ifndef CPAGE
243 # define CPAGE		' '
244 #endif /* CPAGE */
245 #ifndef CPGOFF
246 # define CPGOFF		CONTROL('m')
247 #endif /* CPGOFF */
248 #ifndef CKILL2
249 # define CKILL2		_POSIX_VDISABLE
250 #endif /* CKILL2 */
251 #ifndef CBRK
252 # ifndef masscomp
253 #  define CBRK		0377
254 # else
255 #  define CBRK		'\0'
256 # endif /* masscomp */
257 #endif /* CBRK */
258 #ifndef CMIN
259 # define CMIN		CEOF
260 #endif /* CMIN */
261 #ifndef CTIME
262 # define CTIME		CEOL
263 #endif /* CTIME */
264 
265 /*
266  * Fix for sun inconsistency. On termio VSUSP and the rest of the
267  * ttychars > NCC are defined. So we undefine them.
268  */
269 #if defined(TERMIO) || defined(POSIX)
270 # if defined(POSIX) && defined(NCCS)
271 #  define NUMCC		NCCS
272 # else
273 #  ifdef NCC
274 #   define NUMCC	NCC
275 #  endif /* NCC */
276 # endif /* POSIX && NCCS */
277 # ifdef NUMCC
278 #  ifdef VINTR
279 #   if NUMCC <= VINTR
280 #    undef VINTR
281 #   endif /* NUMCC <= VINTR */
282 #  endif /* VINTR */
283 #  ifdef VQUIT
284 #   if NUMCC <= VQUIT
285 #    undef VQUIT
286 #   endif /* NUMCC <= VQUIT */
287 #  endif /* VQUIT */
288 #  ifdef VERASE
289 #   if NUMCC <= VERASE
290 #    undef VERASE
291 #   endif /* NUMCC <= VERASE */
292 #  endif /* VERASE */
293 #  ifdef VKILL
294 #   if NUMCC <= VKILL
295 #    undef VKILL
296 #   endif /* NUMCC <= VKILL */
297 #  endif /* VKILL */
298 #  ifdef VEOF
299 #   if NUMCC <= VEOF
300 #    undef VEOF
301 #   endif /* NUMCC <= VEOF */
302 #  endif /* VEOF */
303 #  ifdef VEOL
304 #   if NUMCC <= VEOL
305 #    undef VEOL
306 #   endif /* NUMCC <= VEOL */
307 #  endif /* VEOL */
308 #  ifdef VEOL2
309 #   if NUMCC <= VEOL2
310 #    undef VEOL2
311 #   endif /* NUMCC <= VEOL2 */
312 #  endif /* VEOL2 */
313 #  ifdef VSWTCH
314 #   if NUMCC <= VSWTCH
315 #    undef VSWTCH
316 #   endif /* NUMCC <= VSWTCH */
317 #  endif /* VSWTCH */
318 #  ifdef VDSWTCH
319 #   if NUMCC <= VDSWTCH
320 #    undef VDSWTCH
321 #   endif /* NUMCC <= VDSWTCH */
322 #  endif /* VDSWTCH */
323 #  ifdef VERASE2
324 #   if NUMCC <= VERASE2
325 #    undef VERASE2
326 #   endif /* NUMCC <= VERASE2 */
327 #  endif /* VERASE2 */
328 #  ifdef VSTART
329 #   if NUMCC <= VSTART
330 #    undef VSTART
331 #   endif /* NUMCC <= VSTART */
332 #  endif /* VSTART */
333 #  ifdef VSTOP
334 #   if NUMCC <= VSTOP
335 #    undef VSTOP
336 #   endif /* NUMCC <= VSTOP */
337 #  endif /* VSTOP */
338 #  ifdef VWERASE
339 #   if NUMCC <= VWERASE
340 #    undef VWERASE
341 #   endif /* NUMCC <= VWERASE */
342 #  endif /* VWERASE */
343 #  ifdef VSUSP
344 #   if NUMCC <= VSUSP
345 #    undef VSUSP
346 #   endif /* NUMCC <= VSUSP */
347 #  endif /* VSUSP */
348 #  ifdef VDSUSP
349 #   if NUMCC <= VDSUSP
350 #    undef VDSUSP
351 #   endif /* NUMCC <= VDSUSP */
352 #  endif /* VDSUSP */
353 #  ifdef VREPRINT
354 #   if NUMCC <= VREPRINT
355 #    undef VREPRINT
356 #   endif /* NUMCC <= VREPRINT */
357 #  endif /* VREPRINT */
358 #  ifdef VDISCARD
359 #   if NUMCC <= VDISCARD
360 #    undef VDISCARD
361 #   endif /* NUMCC <= VDISCARD */
362 #  endif /* VDISCARD */
363 #  ifdef VLNEXT
364 #   if NUMCC <= VLNEXT
365 #    undef VLNEXT
366 #   endif /* NUMCC <= VLNEXT */
367 #  endif /* VLNEXT */
368 #  ifdef VSTATUS
369 #   if NUMCC <= VSTATUS
370 #    undef VSTATUS
371 #   endif /* NUMCC <= VSTATUS */
372 #  endif /* VSTATUS */
373 #  ifdef VPAGE
374 #   if NUMCC <= VPAGE
375 #    undef VPAGE
376 #   endif /* NUMCC <= VPAGE */
377 #  endif /* VPAGE */
378 #  ifdef VPGOFF
379 #   if NUMCC <= VPGOFF
380 #    undef VPGOFF
381 #   endif /* NUMCC <= VPGOFF */
382 #  endif /* VPGOFF */
383 #  ifdef VKILL2
384 #   if NUMCC <= VKILL2
385 #    undef VKILL2
386 #   endif /* NUMCC <= VKILL2 */
387 #  endif /* VKILL2 */
388 #  ifdef VBRK
389 #   if NUMCC <= VBRK
390 #    undef VBRK
391 #   endif /* NUMCC <= VBRK */
392 #  endif /* VBRK */
393 #  ifdef VMIN
394 #   if NUMCC <= VMIN
395 #    undef VMIN
396 #   endif /* NUMCC <= VMIN */
397 #  endif /* VMIN */
398 #  ifdef VTIME
399 #   if NUMCC <= VTIME
400 #    undef VTIME
401 #   endif /* NUMCC <= VTIME */
402 #  endif /* VTIME */
403 # endif /* NUMCC */
404 #endif /* !POSIX */
405 
406 #define	C_INTR		 0
407 #define	C_QUIT		 1
408 #define	C_ERASE		 2
409 #define	C_KILL		 3
410 #define	C_EOF		 4
411 #define	C_EOL		 5
412 #define	C_EOL2		 6
413 #define	C_SWTCH		 7
414 #define	C_DSWTCH	 8
415 #define	C_ERASE2	 9
416 #define	C_START		10
417 #define	C_STOP		11
418 #define	C_WERASE	12
419 #define	C_SUSP		13
420 #define	C_DSUSP		14
421 #define	C_REPRINT	15
422 #define	C_DISCARD	16
423 #define	C_LNEXT		17
424 #define	C_STATUS	18
425 #define	C_PAGE		19
426 #define	C_PGOFF		20
427 #define	C_KILL2		21
428 #define	C_BRK		22
429 #define	C_MIN		23
430 #define	C_TIME		24
431 #define	C_NCC		25
432 #define	C_SH(A)		((unsigned int)(1 << (A)))
433 
434 /*
435  * Terminal dependent data structures
436  */
437 #define	EX_IO	0	/* while we are executing	*/
438 #define	ED_IO	1	/* while we are editing		*/
439 #define	TS_IO	2	/* new mode from terminal	*/
440 #define	QU_IO	2	/* used only for quoted chars	*/
441 #define	NN_IO	3	/* The number of entries	*/
442 
443 /* Don't re-order */
444 #define	MD_INP	0
445 #define	MD_OUT	1
446 #define	MD_CTL	2
447 #define	MD_LIN	3
448 #define	MD_CHAR	4
449 #define	MD_NN	5
450 
451 typedef struct {
452 	const char	*t_name;
453 	unsigned int	 t_setmask;
454 	unsigned int	 t_clrmask;
455 } ttyperm_t[NN_IO][MD_NN];
456 
457 typedef unsigned char ttychar_t[NN_IO][C_NCC];
458 
459 libedit_private int	tty_init(EditLine *);
460 libedit_private void	tty_end(EditLine *, int);
461 libedit_private int	tty_stty(EditLine *, int, const wchar_t **);
462 libedit_private int	tty_rawmode(EditLine *);
463 libedit_private int	tty_cookedmode(EditLine *);
464 libedit_private int	tty_quotemode(EditLine *);
465 libedit_private int	tty_noquotemode(EditLine *);
466 libedit_private void	tty_bind_char(EditLine *, int);
467 libedit_private int	tty_get_signal_character(EditLine *, int);
468 
469 typedef struct {
470     ttyperm_t t_t;
471     ttychar_t t_c;
472     struct termios t_or, t_ex, t_ed, t_ts;
473     int t_tabs;
474     int t_eight;
475     speed_t t_speed;
476     unsigned char t_mode;
477     unsigned char t_vdisable;
478     unsigned char t_initialized;
479 } el_tty_t;
480 
481 
482 #endif /* _h_el_tty */
483