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