1 /****************************************************************************
2  * Copyright (c) 1998-2012,2014 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.30 2014/04/26 18:47:20 juergen 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, td_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, td_sgmode, TRUE, buf);
106 #else
107 	    for (;;) {
108 		if ((SET_TTY(termp->Filedes, buf) != 0)
109 #if USE_KLIBC_KBD
110 		    && !NC_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, td_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, td_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, td_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 	    }
229 	    rc = OK;
230 	}
231 #endif
232     }
233     returnCode(rc);
234 }
235 
236 #if NCURSES_SP_FUNCS
237 NCURSES_EXPORT(int)
238 reset_prog_mode(void)
239 {
240     return NCURSES_SP_NAME(reset_prog_mode) (CURRENT_SCREEN);
241 }
242 #endif
243 
244 NCURSES_EXPORT(int)
245 NCURSES_SP_NAME(reset_shell_mode) (NCURSES_SP_DCL0)
246 {
247     int rc = ERR;
248     TERMINAL *termp = TerminalOf(SP_PARM);
249 
250     T((T_CALLED("reset_shell_mode(%p)"), (void *) SP_PARM));
251 
252     if (termp != 0) {
253 #ifdef USE_TERM_DRIVER
254 	rc = CallDriver_2(SP_PARM, td_mode, FALSE, FALSE);
255 #else
256 	if (SP_PARM) {
257 	    _nc_keypad(SP_PARM, FALSE);
258 	    _nc_flush();
259 	}
260 	rc = _nc_set_tty_mode(&termp->Ottyb);
261 #endif
262     }
263     returnCode(rc);
264 }
265 
266 #if NCURSES_SP_FUNCS
267 NCURSES_EXPORT(int)
268 reset_shell_mode(void)
269 {
270     return NCURSES_SP_NAME(reset_shell_mode) (CURRENT_SCREEN);
271 }
272 #endif
273 
274 static TTY *
275 saved_tty(NCURSES_SP_DCL0)
276 {
277     TTY *result = 0;
278 
279     if (SP_PARM != 0) {
280 	result = (TTY *) & (SP_PARM->_saved_tty);
281     } else {
282 	if (_nc_prescreen.saved_tty == 0) {
283 	    _nc_prescreen.saved_tty = typeCalloc(TTY, 1);
284 	}
285 	result = _nc_prescreen.saved_tty;
286     }
287     return result;
288 }
289 
290 /*
291 **	savetty()  and  resetty()
292 **
293 */
294 
295 NCURSES_EXPORT(int)
296 NCURSES_SP_NAME(savetty) (NCURSES_SP_DCL0)
297 {
298     T((T_CALLED("savetty(%p)"), (void *) SP_PARM));
299     returnCode(NCURSES_SP_NAME(_nc_get_tty_mode) (NCURSES_SP_ARGx saved_tty(NCURSES_SP_ARG)));
300 }
301 
302 #if NCURSES_SP_FUNCS
303 NCURSES_EXPORT(int)
304 savetty(void)
305 {
306     return NCURSES_SP_NAME(savetty) (CURRENT_SCREEN);
307 }
308 #endif
309 
310 NCURSES_EXPORT(int)
311 NCURSES_SP_NAME(resetty) (NCURSES_SP_DCL0)
312 {
313     T((T_CALLED("resetty(%p)"), (void *) SP_PARM));
314     returnCode(NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx saved_tty(NCURSES_SP_ARG)));
315 }
316 
317 #if NCURSES_SP_FUNCS
318 NCURSES_EXPORT(int)
319 resetty(void)
320 {
321     return NCURSES_SP_NAME(resetty) (CURRENT_SCREEN);
322 }
323 #endif
324