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