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