]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_ttyflags.c
ncurses 5.7 - patch 20090510
[ncurses.git] / ncurses / tinfo / lib_ttyflags.c
1 /****************************************************************************
2  * Copyright (c) 1998-2008,2009 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 #include <term.h>               /* cur_term */
40
41 #ifndef CUR
42 #define CUR SP_TERMTYPE 
43 #endif
44
45 MODULE_ID("$Id: lib_ttyflags.c,v 1.24 2009/05/10 00:48:29 tom Exp $")
46
47 NCURSES_EXPORT(int)
48 NCURSES_SP_NAME(_nc_get_tty_mode) (NCURSES_SP_DCLx TTY * buf)
49 {
50     int result = OK;
51
52     if (buf == 0) {
53         result = ERR;
54     } else {
55         if (cur_term == 0) {
56             result = ERR;
57         } else {
58             for (;;) {
59                 if (GET_TTY(cur_term->Filedes, buf) != 0) {
60                     if (errno == EINTR)
61                         continue;
62                     result = ERR;
63                 }
64                 break;
65             }
66         }
67
68         if (result == ERR)
69             memset(buf, 0, sizeof(*buf));
70
71         TR(TRACE_BITS, ("_nc_get_tty_mode(%d): %s",
72                         cur_term ? cur_term->Filedes : -1,
73                         _nc_trace_ttymode(buf)));
74     }
75     return (result);
76 }
77
78 #if NCURSES_SP_FUNCS
79 NCURSES_EXPORT(int)
80 _nc_get_tty_mode(TTY * buf)
81 {
82     return NCURSES_SP_NAME(_nc_get_tty_mode) (CURRENT_SCREEN, buf);
83 }
84 #endif
85
86 NCURSES_EXPORT(int)
87 NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_DCLx TTY * buf)
88 {
89     int result = OK;
90
91     if (buf == 0) {
92         result = ERR;
93     } else {
94         if (cur_term == 0) {
95             result = ERR;
96         } else {
97             for (;;) {
98                 if (SET_TTY(cur_term->Filedes, buf) != 0) {
99                     if (errno == EINTR)
100                         continue;
101                     if ((errno == ENOTTY) && (SP_PARM != 0))
102                         SP_PARM->_notty = TRUE;
103                     result = ERR;
104                 }
105                 break;
106             }
107         }
108         TR(TRACE_BITS, ("_nc_set_tty_mode(%d): %s",
109                         cur_term ? cur_term->Filedes : -1,
110                         _nc_trace_ttymode(buf)));
111     }
112     return (result);
113 }
114
115 #if NCURSES_SP_FUNCS
116 NCURSES_EXPORT(int)
117 _nc_set_tty_mode(TTY * buf)
118 {
119     return NCURSES_SP_NAME(_nc_set_tty_mode) (CURRENT_SCREEN, buf);
120 }
121 #endif
122
123 NCURSES_EXPORT(int)
124 NCURSES_SP_NAME(def_shell_mode) (NCURSES_SP_DCL0)
125 {
126     int rc = ERR;
127
128     T((T_CALLED("def_shell_mode()")));
129
130     if (cur_term != 0) {
131         /*
132          * If XTABS was on, remove the tab and backtab capabilities.
133          */
134         if (_nc_get_tty_mode(&cur_term->Ottyb) == OK) {
135 #ifdef TERMIOS
136             if (cur_term->Ottyb.c_oflag & OFLAGS_TABS)
137                 tab = back_tab = NULL;
138 #else
139             if (cur_term->Ottyb.sg_flags & XTABS)
140                 tab = back_tab = NULL;
141 #endif
142             rc = OK;
143         }
144     }
145     returnCode(rc);
146 }
147
148 #if NCURSES_SP_FUNCS
149 NCURSES_EXPORT(int)
150 def_shell_mode(void)
151 {
152     return NCURSES_SP_NAME(def_shell_mode) (CURRENT_SCREEN);
153 }
154 #endif
155
156 NCURSES_EXPORT(int)
157 NCURSES_SP_NAME(def_prog_mode) (NCURSES_SP_DCL0)
158 {
159     int rc = ERR;
160
161     T((T_CALLED("def_prog_mode()")));
162
163     if (cur_term != 0) {
164         /*
165          * Turn off the XTABS bit in the tty structure if it was on.
166          */
167         if (_nc_get_tty_mode(&cur_term->Nttyb) == OK) {
168 #ifdef TERMIOS
169             cur_term->Nttyb.c_oflag &= ~OFLAGS_TABS;
170 #else
171             cur_term->Nttyb.sg_flags &= ~XTABS;
172 #endif
173             rc = OK;
174         }
175     }
176     returnCode(rc);
177 }
178
179 #if NCURSES_SP_FUNCS
180 NCURSES_EXPORT(int)
181 def_prog_mode(void)
182 {
183     return NCURSES_SP_NAME(def_prog_mode) (CURRENT_SCREEN);
184 }
185 #endif
186
187 NCURSES_EXPORT(int)
188 NCURSES_SP_NAME(reset_prog_mode) (NCURSES_SP_DCL0)
189 {
190     T((T_CALLED("reset_prog_mode()")));
191
192     if (cur_term != 0) {
193         if (_nc_set_tty_mode(&cur_term->Nttyb) == OK) {
194             if (SP_PARM) {
195                 if (SP_PARM->_keypad_on)
196                     _nc_keypad(SP_PARM, TRUE);
197                 NC_BUFFERED(SP_PARM, TRUE);
198             }
199             returnCode(OK);
200         }
201     }
202     returnCode(ERR);
203 }
204
205 #if NCURSES_SP_FUNCS
206 NCURSES_EXPORT(int)
207 reset_prog_mode(void)
208 {
209     return NCURSES_SP_NAME(reset_prog_mode) (CURRENT_SCREEN);
210 }
211 #endif
212
213 NCURSES_EXPORT(int)
214 NCURSES_SP_NAME(reset_shell_mode) (NCURSES_SP_DCL0)
215 {
216     T((T_CALLED("reset_shell_mode()")));
217
218     if (cur_term != 0) {
219         if (SP_PARM) {
220             _nc_keypad(SP_PARM, FALSE);
221             _nc_flush();
222             NC_BUFFERED(SP_PARM, FALSE);
223         }
224         returnCode(_nc_set_tty_mode(&cur_term->Ottyb));
225     }
226     returnCode(ERR);
227 }
228
229 #if NCURSES_SP_FUNCS
230 NCURSES_EXPORT(int)
231 reset_shell_mode(void)
232 {
233     return NCURSES_SP_NAME(reset_shell_mode) (CURRENT_SCREEN);
234 }
235 #endif
236
237 static TTY *
238 saved_tty(void)
239 {
240     TTY *result = 0;
241
242     if (SP != 0) {
243         result = &(SP->_saved_tty);
244     } else {
245         if (_nc_prescreen.saved_tty == 0) {
246             _nc_prescreen.saved_tty = typeCalloc(TTY, 1);
247         }
248         result = _nc_prescreen.saved_tty;
249     }
250     return result;
251 }
252
253 /*
254 **      savetty()  and  resetty()
255 **
256 */
257
258 NCURSES_EXPORT(int)
259 savetty(void)
260 {
261     T((T_CALLED("savetty()")));
262
263     returnCode(_nc_get_tty_mode(saved_tty()));
264 }
265
266 NCURSES_EXPORT(int)
267 resetty(void)
268 {
269     T((T_CALLED("resetty()")));
270
271     returnCode(_nc_set_tty_mode(saved_tty()));
272 }