1 /* @(#)ttymodes.c	1.25 11/08/11 Copyright 1984-2011 J. Schilling */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static	UConst char sccsid[] =
5 	"@(#)ttymodes.c	1.25 11/08/11 Copyright 1984-2011 J. Schilling";
6 #endif
7 /*
8  *	Terminal driver tty mode handling
9  *
10  *	Copyright (c) 1984-2011 J. Schilling
11  */
12 /*
13  * The contents of this file are subject to the terms of the
14  * Common Development and Distribution License, Version 1.0 only
15  * (the "License").  You may not use this file except in compliance
16  * with the License.
17  *
18  * See the file CDDL.Schily.txt in this distribution for details.
19  * A copy of the CDDL is also available via the Internet at
20  * http://www.opensource.org/licenses/cddl1.txt
21  *
22  * When distributing Covered Code, include this CDDL HEADER in each
23  * file and include the License file CDDL.Schily.txt from this distribution.
24  */
25 
26 #include "ved.h"
27 #include <schily/termios.h>
28 
29 	int	intrchar = -1;
30 LOCAL	BOOL	got_modes = FALSE;
31 
32 #ifndef tos
33 #ifdef	JOS
34 LOCAL	TTYMODE old = DEFAULT_TTY_MODES;
35 LOCAL	TTYMODE tty;
36 #else
37 #ifdef	USE_V7_TTY
38 LOCAL	struct sgttyb	old;
39 LOCAL	struct sgttyb	tty;
40 LOCAL	struct tchars	oldt;
41 LOCAL	struct tchars	ttyt;
42 LOCAL	struct ltchars	oldl;
43 LOCAL	struct ltchars	ttyl;
44 LOCAL	int		lbit;
45 
46 #else	/* USE_V7_TTY */
47 
48 #ifdef	USE_TERMIOS
49 LOCAL	struct termios	old;
50 LOCAL	struct termios	tty;
51 #endif
52 
53 #endif	/* USE_V7_TTY */
54 #endif
55 #endif /* tos */
56 
57 /*
58  * From the tty modes...
59  */
60 extern	char	GT;		/* Gtty told us that we may use tabs	*/
61 extern	char	NONL;		/* Terminal don't needs linefeed on CR	*/
62 extern	char	UPPERCASE;	/* Terminal is upper case only		*/
63 extern	short	ospeed;
64 
65 EXPORT	void	get_modes	__PR((ewin_t *wp));
66 EXPORT	void	set_modes	__PR((void));
67 LOCAL	void	set_ttymodes	__PR((void));
68 EXPORT	void	set_oldmodes	__PR((void));
69 
70 /*
71  * Get old (pre editing) tty modes from driver
72  */
73 EXPORT void
get_modes(wp)74 get_modes(wp)
75 	ewin_t	*wp;
76 {
77 #ifndef	tos
78 #ifdef	JOS
79 	spfun(1, GTTY, &old);
80 	if (old.t_i8bit == TRUE || old.t_o8bit == TRUE) wp->raw8 = TRUE;
81 #else
82 #	ifdef	USE_V7_TTY
83 		ioctl(STDIN_FILENO, TIOCGETP, &old);
84 		ospeed = old.sg_ospeed;
85 		if ((old.sg_flags & RAW)) wp->raw8 = TRUE;
86 
87 		GT = (old.sg_flags & XTABS) != XTABS;
88 		NONL = (old.sg_flags & CRMOD) == 0;
89 		UPPERCASE = (old.sg_flags & LCASE) != 0;
90 #ifdef	LPASS8
91 		ioctl(STDIN_FILENO, TIOCLGET, &lbit);
92 		if (lbit & LPASS8)
93 			wp->raw8 = TRUE;
94 #endif
95 		ioctl(STDIN_FILENO, TIOCGETC, &oldt);
96 		intrchar = oldt.t_intrc;
97 
98 		ioctl(STDIN_FILENO, TIOCGLTC, &oldl);
99 
100 #	else	/* USE_V7_TTY */
101 
102 #	ifdef	USE_TERMIOS
103 #		ifdef	TCSANOW
104 		tcgetattr(STDIN_FILENO, &old);
105 		ospeed = (short)cfgetospeed(&old);
106 #		else
107 		ioctl(STDIN_FILENO, TCGETS, &old);
108 #ifndef	CBAUD					/* FreeBSD */
109 		ospeed = old.c_ospeed;
110 #else
111 		ospeed = old.c_cflag & CBAUD;
112 #endif
113 #		endif
114 		if (!(old.c_iflag & ISTRIP) && (old.c_cflag & CSIZE) == CS8)
115 			wp->raw8 = TRUE;
116 		intrchar = old.c_cc[VINTR];
117 
118 		GT = (old.c_oflag & XTABS) != XTABS;
119 #if	ONLCR == 0
120 		NONL = FALSE;
121 #else
122 		NONL = (old.c_oflag & ONLCR) == 0;
123 #endif
124 #ifndef	IUCLC
125 		UPPERCASE = FALSE;
126 #else
127 		UPPERCASE = (old.c_iflag & IUCLC) != 0;
128 #endif
129 #	endif	/* USE_TERMIOS */
130 #	endif	/* USE_V7_TTY */
131 #endif
132 #endif /* tos */
133 	got_modes = TRUE;
134 }
135 
136 /*
137  * Set-up editing tty modes and then set the tty driver to editing mode
138  */
139 EXPORT void
set_modes()140 set_modes()
141 {
142 #ifndef	tos
143 #ifdef	JOS
144 	movebytes((char *) &old, (char *) &tty, sizeof (TTYMODE));
145 	CLEAR_ECHO_MODES(tty);
146 	tty.t_wakealpha = TRUE;		tty.t_wakectl = TRUE;
147 	tty.t_escape = FALSE;		tty.t_irawedit = TRUE;
148 	tty.t_eof = FALSE;		tty.t_icrlf = FALSE;
149 	tty.t_prctl = FALSE;
150 	tty.t_octl = FALSE;		tty.t_ocrlf = FALSE;
151 	tty.t_oxtab = FALSE;
152 #else
153 #	ifdef	USE_V7_TTY
154 	movebytes((char *) &old, (char *) &tty, sizeof (struct sgttyb));
155 	movebytes((char *) &oldt, (char *) &ttyt, sizeof (struct tchars));
156 	tty.sg_flags &= ~(ECHO|CRMOD);
157 	tty.sg_flags |= CBREAK;
158 /*	ttyt.t_intrc = -1;*/
159 	ttyt.t_quitc = -1;
160 /*
161 	ttyt.t_startc = -1;
162 	ttyt.t_stopc = -1;
163 */
164 	ttyt.t_eofc = -1;
165 	ttyt.t_brkc = -1;
166 	ttyl.t_suspc = -1;
167 	ttyl.t_dsuspc = -1;
168 	ttyl.t_rprntc = -1;
169 	ttyl.t_flushc = -1;
170 	ttyl.t_werasc = -1;
171 	ttyl.t_lnextc = -1;
172 
173 #	else	/* USE_V7_TTY */
174 
175 #	ifdef	USE_TERMIOS
176 	movebytes((char *) &old, (char *) &tty, sizeof (struct termios));
177 	tty.c_iflag |= (IXON|IGNBRK);
178 	tty.c_iflag &= ~(BRKINT|INLCR|ICRNL);
179 /*	tty.c_oflag |= 0;*/
180 	tty.c_oflag &= ~(ONLCR|OCRNL|ONLRET|XTABS);
181 	tty.c_lflag |= (ISIG);
182 	tty.c_lflag &= ~(ICANON|ECHO);
183 	/*
184 	 * Keep VINTR to allow to interrupt commands.
185 	 */
186 	tty.c_cc[VQUIT] = _POSIX_VDISABLE;
187 	tty.c_cc[VERASE] = _POSIX_VDISABLE;
188 	tty.c_cc[VKILL] = _POSIX_VDISABLE;
189 	/*
190 	 * On HP-UX, VMIN/VTIME are not mapped to VEOF/VEOL.
191 	 */
192 	tty.c_cc[VEOF] = _POSIX_VDISABLE;
193 	tty.c_cc[VEOL] = _POSIX_VDISABLE;
194 #ifdef	VEOL2
195 	tty.c_cc[VEOL2] = _POSIX_VDISABLE;
196 #endif
197 #ifdef	VSWTCH
198 	tty.c_cc[VSWTCH] = _POSIX_VDISABLE;
199 #endif
200 	/*
201 	 * Keep VSTART/VSTOP.
202 	 */
203 #ifdef	VSUSP
204 	tty.c_cc[VSUSP] = _POSIX_VDISABLE;
205 #endif
206 #ifdef	VDSUSP
207 	tty.c_cc[VDSUSP] = _POSIX_VDISABLE;
208 #endif
209 #ifdef	VREPRINT
210 	tty.c_cc[VREPRINT] = _POSIX_VDISABLE;
211 #endif
212 #ifdef	VDISCARD
213 	tty.c_cc[VDISCARD] = _POSIX_VDISABLE;
214 #endif
215 #ifdef	VWERASE
216 	tty.c_cc[VWERASE] = _POSIX_VDISABLE;
217 #endif
218 #ifdef	VLNEXT
219 	tty.c_cc[VLNEXT] = _POSIX_VDISABLE;
220 #endif
221 	tty.c_cc[VMIN] = 1;
222 	tty.c_cc[VTIME] = 0;
223 #	endif	/* USE_TERMIOS */
224 #	endif	/* USE_V7_TTY */
225 #endif
226 #endif	/* tos */
227 	set_ttymodes();
228 }
229 
230 /*
231  * Set tty modes to editing tty modes
232  */
233 LOCAL void
set_ttymodes()234 set_ttymodes()
235 {
236 #ifndef	tos
237 #ifdef	JOS
238 	spfun(1, STTY, &tty);
239 #else
240 #	ifdef	USE_V7_TTY
241 		ioctl(STDIN_FILENO, TIOCSETN, &tty);
242 		ioctl(STDIN_FILENO, TIOCSETC, &ttyt);
243 		ioctl(STDIN_FILENO, TIOCSLTC, &ttyl);
244 #	else	/* USE_V7_TTY */
245 
246 #	ifdef	USE_TERMIOS
247 #		ifdef	TCSANOW
248 		tcsetattr(STDIN_FILENO, TCSADRAIN, &tty);
249 #		else
250 		ioctl(STDIN_FILENO, TCSETSW, &tty);
251 #		endif
252 #	endif	/* USE_TERMIOS */
253 #	endif	/* USE_V7_TTY */
254 #endif
255 #endif	/* tos */
256 }
257 
258 /*
259  * Re-set tty modes to saved old modes
260  */
261 EXPORT void
set_oldmodes()262 set_oldmodes()
263 {
264 	if (!got_modes)
265 		return;
266 #ifndef	tos
267 #ifdef	JOS
268 	spfun(1, STTY, &old);
269 #else
270 #	ifdef	USE_V7_TTY
271 		ioctl(STDIN_FILENO, TIOCSETN, &old);
272 		ioctl(STDIN_FILENO, TIOCSETC, &oldt);
273 		ioctl(STDIN_FILENO, TIOCSLTC, &oldl);
274 #	else	/* USE_V7_TTY */
275 
276 #	ifdef	USE_TERMIOS
277 #		ifdef	TCSANOW
278 		tcsetattr(STDIN_FILENO, TCSADRAIN, &old);
279 #		else
280 		ioctl(STDIN_FILENO, TCSETSW, &old);
281 #		endif
282 #	endif	/* USE_TERMIOS */
283 #	endif	/* USE_V7_TTY */
284 #endif
285 #endif	/* tos */
286 }
287