1 /****************************************************************************
2  * Copyright (c) 1998-2010,2012 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 
29 /*
30  *		def_prog_mode()
31  *		def_shell_mode()
32  *		reset_prog_mode()
33  *		reset_shell_mode()
34  *		savetty()
35  *		resetty()
36  */
37 
38 #include <curses.priv.h>
39 
40 #ifndef CUR
41 #define CUR SP_TERMTYPE
42 #endif
43 
44 MODULE_ID("$Id: lib_ttyflags.c,v 1.28 2012/01/21 19:21:29 KO.Myung-Hun Exp $")
45 
46 NCURSES_EXPORT(int)
47 NCURSES_SP_NAME(_nc_get_tty_mode) (NCURSES_SP_DCLx TTY * buf)
48 {
49     int result = OK;
50 
51     if (buf == 0 || SP_PARM == 0) {
52 	result = ERR;
53     } else {
54 	TERMINAL *termp = TerminalOf(SP_PARM);
55 
56 	if (0 == termp) {
57 	    result = ERR;
58 	} else {
59 #ifdef USE_TERM_DRIVER
60 	    result = CallDriver_2(SP_PARM, sgmode, FALSE, buf);
61 #else
62 	    for (;;) {
63 		if (GET_TTY(termp->Filedes, buf) != 0) {
64 		    if (errno == EINTR)
65 			continue;
66 		    result = ERR;
67 		}
68 		break;
69 	    }
70 #endif
71 	}
72 
73 	if (result == ERR)
74 	    memset(buf, 0, sizeof(*buf));
75 
76 	TR(TRACE_BITS, ("_nc_get_tty_mode(%d): %s",
77 			termp ? termp->Filedes : -1,
78 			_nc_trace_ttymode(buf)));
79     }
80     return (result);
81 }
82 
83 #if NCURSES_SP_FUNCS
84 NCURSES_EXPORT(int)
85 _nc_get_tty_mode(TTY * buf)
86 {
87     return NCURSES_SP_NAME(_nc_get_tty_mode) (CURRENT_SCREEN, buf);
88 }
89 #endif
90 
91 NCURSES_EXPORT(int)
92 NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_DCLx TTY * buf)
93 {
94     int result = OK;
95 
96     if (buf == 0 || SP_PARM == 0) {
97 	result = ERR;
98     } else {
99 	TERMINAL *termp = TerminalOf(SP_PARM);
100 
101 	if (0 == termp) {
102 	    result = ERR;
103 	} else {
104 #ifdef USE_TERM_DRIVER
105 	    result = CallDriver_2(SP_PARM, sgmode, TRUE, buf);
106 #else
107 	    for (;;) {
108 		if ((SET_TTY(termp->Filedes, buf) != 0)
109 #if USE_KLIBC_KBD
110 		    && !isatty(termp->Filedes)
111 #endif
112 		    ) {
113 		    if (errno == EINTR)
114 			continue;
115 		    if ((errno == ENOTTY) && (SP_PARM != 0))
116 			SP_PARM->_notty = TRUE;
117 		    result = ERR;
118 		}
119 		break;
120 	    }
121 #endif
122 	}
123 	TR(TRACE_BITS, ("_nc_set_tty_mode(%d): %s",
124 			termp ? termp->Filedes : -1,
125 			_nc_trace_ttymode(buf)));
126     }
127     return (result);
128 }
129 
130 #if NCURSES_SP_FUNCS
131 NCURSES_EXPORT(int)
132 _nc_set_tty_mode(TTY * buf)
133 {
134     return NCURSES_SP_NAME(_nc_set_tty_mode) (CURRENT_SCREEN, buf);
135 }
136 #endif
137 
138 NCURSES_EXPORT(int)
139 NCURSES_SP_NAME(def_shell_mode) (NCURSES_SP_DCL0)
140 {
141     int rc = ERR;
142     TERMINAL *termp = TerminalOf(SP_PARM);
143 
144     T((T_CALLED("def_shell_mode(%p)"), (void *) SP_PARM));
145 
146     if (termp != 0) {
147 #ifdef USE_TERM_DRIVER
148 	rc = CallDriver_2(SP_PARM, mode, FALSE, TRUE);
149 #else
150 	/*
151 	 * If XTABS was on, remove the tab and backtab capabilities.
152 	 */
153 	if (_nc_get_tty_mode(&termp->Ottyb) == OK) {
154 #ifdef TERMIOS
155 	    if (termp->Ottyb.c_oflag & OFLAGS_TABS)
156 		tab = back_tab = NULL;
157 #else
158 	    if (termp->Ottyb.sg_flags & XTABS)
159 		tab = back_tab = NULL;
160 #endif
161 	    rc = OK;
162 	}
163 #endif
164     }
165     returnCode(rc);
166 }
167 
168 #if NCURSES_SP_FUNCS
169 NCURSES_EXPORT(int)
170 def_shell_mode(void)
171 {
172     return NCURSES_SP_NAME(def_shell_mode) (CURRENT_SCREEN);
173 }
174 #endif
175 
176 NCURSES_EXPORT(int)
177 NCURSES_SP_NAME(def_prog_mode) (NCURSES_SP_DCL0)
178 {
179     int rc = ERR;
180     TERMINAL *termp = TerminalOf(SP_PARM);
181 
182     T((T_CALLED("def_prog_mode(%p)"), (void *) SP_PARM));
183 
184     if (termp != 0) {
185 #ifdef USE_TERM_DRIVER
186 	rc = CallDriver_2(SP_PARM, mode, TRUE, TRUE);
187 #else
188 	/*
189 	 * Turn off the XTABS bit in the tty structure if it was on.
190 	 */
191 	if (_nc_get_tty_mode(&termp->Nttyb) == OK) {
192 #ifdef TERMIOS
193 	    termp->Nttyb.c_oflag &= (unsigned) (~OFLAGS_TABS);
194 #else
195 	    termp->Nttyb.sg_flags &= (unsigned) (~XTABS);
196 #endif
197 	    rc = OK;
198 	}
199 #endif
200     }
201     returnCode(rc);
202 }
203 
204 #if NCURSES_SP_FUNCS
205 NCURSES_EXPORT(int)
206 def_prog_mode(void)
207 {
208     return NCURSES_SP_NAME(def_prog_mode) (CURRENT_SCREEN);
209 }
210 #endif
211 
212 NCURSES_EXPORT(int)
213 NCURSES_SP_NAME(reset_prog_mode) (NCURSES_SP_DCL0)
214 {
215     int rc = ERR;
216     TERMINAL *termp = TerminalOf(SP_PARM);
217 
218     T((T_CALLED("reset_prog_mode(%p)"), (void *) SP_PARM));
219 
220     if (termp != 0) {
221 #ifdef USE_TERM_DRIVER
222 	rc = CallDriver_2(SP_PARM, mode, TRUE, FALSE);
223 #else
224 	if (_nc_set_tty_mode(&termp->Nttyb) == OK) {
225 	    if (SP_PARM) {
226 		if (SP_PARM->_keypad_on)
227 		    _nc_keypad(SP_PARM, TRUE);
228 		NC_BUFFERED(SP_PARM, TRUE);
229 	    }
230 	    rc = OK;
231 	}
232 #endif
233     }
234     returnCode(rc);
235 }
236 
237 #if NCURSES_SP_FUNCS
238 NCURSES_EXPORT(int)
239 reset_prog_mode(void)
240 {
241     return NCURSES_SP_NAME(reset_prog_mode) (CURRENT_SCREEN);
242 }
243 #endif
244 
245 NCURSES_EXPORT(int)
246 NCURSES_SP_NAME(reset_shell_mode) (NCURSES_SP_DCL0)
247 {
248     int rc = ERR;
249     TERMINAL *termp = TerminalOf(SP_PARM);
250 
251     T((T_CALLED("reset_shell_mode(%p)"), (void *) SP_PARM));
252 
253     if (termp != 0) {
254 #ifdef USE_TERM_DRIVER
255 	rc = CallDriver_2(SP_PARM, mode, FALSE, FALSE);
256 #else
257 	if (SP_PARM) {
258 	    _nc_keypad(SP_PARM, FALSE);
259 	    _nc_flush();
260 	    NC_BUFFERED(SP_PARM, FALSE);
261 	}
262 	rc = _nc_set_tty_mode(&termp->Ottyb);
263 #endif
264     }
265     returnCode(rc);
266 }
267 
268 #if NCURSES_SP_FUNCS
269 NCURSES_EXPORT(int)
270 reset_shell_mode(void)
271 {
272     return NCURSES_SP_NAME(reset_shell_mode) (CURRENT_SCREEN);
273 }
274 #endif
275 
276 static TTY *
277 saved_tty(NCURSES_SP_DCL0)
278 {
279     TTY *result = 0;
280 
281     if (SP_PARM != 0) {
282 	result = (TTY *) & (SP_PARM->_saved_tty);
283     } else {
284 	if (_nc_prescreen.saved_tty == 0) {
285 	    _nc_prescreen.saved_tty = typeCalloc(TTY, 1);
286 	}
287 	result = _nc_prescreen.saved_tty;
288     }
289     return result;
290 }
291 
292 /*
293 **	savetty()  and  resetty()
294 **
295 */
296 
297 NCURSES_EXPORT(int)
298 NCURSES_SP_NAME(savetty) (NCURSES_SP_DCL0)
299 {
300     T((T_CALLED("savetty(%p)"), (void *) SP_PARM));
301     returnCode(NCURSES_SP_NAME(_nc_get_tty_mode) (NCURSES_SP_ARGx saved_tty(NCURSES_SP_ARG)));
302 }
303 
304 #if NCURSES_SP_FUNCS
305 NCURSES_EXPORT(int)
306 savetty(void)
307 {
308     return NCURSES_SP_NAME(savetty) (CURRENT_SCREEN);
309 }
310 #endif
311 
312 NCURSES_EXPORT(int)
313 NCURSES_SP_NAME(resetty) (NCURSES_SP_DCL0)
314 {
315     T((T_CALLED("resetty(%p)"), (void *) SP_PARM));
316     returnCode(NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx saved_tty(NCURSES_SP_ARG)));
317 }
318 
319 #if NCURSES_SP_FUNCS
320 NCURSES_EXPORT(int)
321 resetty(void)
322 {
323     return NCURSES_SP_NAME(resetty) (CURRENT_SCREEN);
324 }
325 #endif
326