ncurses 5.6 - patch 20080621
[ncurses.git] / ncurses / tinfo / lib_ttyflags.c
1 /****************************************************************************
2  * Copyright (c) 1998-2007,2008 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.17 2008/06/21 20:53:06 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->Filedes, _nc_trace_ttymode(buf)));
69     }
70     return (result);
71 }
72
73 NCURSES_EXPORT(int)
74 _nc_set_tty_mode(TTY * buf)
75 {
76     int result = OK;
77
78     if (buf == 0) {
79         result = ERR;
80     } else {
81         if (cur_term == 0) {
82             result = ERR;
83         } else {
84             for (;;) {
85                 if (SET_TTY(cur_term->Filedes, buf) != 0) {
86                     if (errno == EINTR)
87                         continue;
88                     if ((errno == ENOTTY) && (SP != 0))
89                         SP->_notty = TRUE;
90                     result = ERR;
91                 }
92                 break;
93             }
94         }
95         TR(TRACE_BITS, ("_nc_set_tty_mode(%d): %s",
96                         cur_term->Filedes, _nc_trace_ttymode(buf)));
97     }
98     return (result);
99 }
100
101 NCURSES_EXPORT(int)
102 def_shell_mode(void)
103 {
104     int rc = ERR;
105
106     T((T_CALLED("def_shell_mode()")));
107
108     if (cur_term != 0) {
109         /*
110          * If XTABS was on, remove the tab and backtab capabilities.
111          */
112         if (_nc_get_tty_mode(&cur_term->Ottyb) == OK) {
113 #ifdef TERMIOS
114             if (cur_term->Ottyb.c_oflag & OFLAGS_TABS)
115                 tab = back_tab = NULL;
116 #else
117             if (cur_term->Ottyb.sg_flags & XTABS)
118                 tab = back_tab = NULL;
119 #endif
120             rc = OK;
121         }
122     }
123     returnCode(rc);
124 }
125
126 NCURSES_EXPORT(int)
127 def_prog_mode(void)
128 {
129     int rc = ERR;
130
131     T((T_CALLED("def_prog_mode()")));
132
133     if (cur_term != 0) {
134         /*
135          * Turn off the XTABS bit in the tty structure if it was on.
136          */
137         if (_nc_get_tty_mode(&cur_term->Nttyb) == OK) {
138 #ifdef TERMIOS
139             cur_term->Nttyb.c_oflag &= ~OFLAGS_TABS;
140 #else
141             cur_term->Nttyb.sg_flags &= ~XTABS;
142 #endif
143             rc = OK;
144         }
145     }
146     returnCode(rc);
147 }
148
149 NCURSES_EXPORT(int)
150 reset_prog_mode(void)
151 {
152     T((T_CALLED("reset_prog_mode()")));
153
154     if (cur_term != 0) {
155         if (_nc_set_tty_mode(&cur_term->Nttyb) == OK) {
156             if (SP) {
157                 if (SP->_keypad_on)
158                     _nc_keypad(SP, TRUE);
159                 NC_BUFFERED(TRUE);
160             }
161             returnCode(OK);
162         }
163     }
164     returnCode(ERR);
165 }
166
167 NCURSES_EXPORT(int)
168 reset_shell_mode(void)
169 {
170     T((T_CALLED("reset_shell_mode()")));
171
172     if (cur_term != 0) {
173         if (SP) {
174             _nc_keypad(SP, FALSE);
175             _nc_flush();
176             NC_BUFFERED(FALSE);
177         }
178         returnCode(_nc_set_tty_mode(&cur_term->Ottyb));
179     }
180     returnCode(ERR);
181 }
182
183 static TTY *
184 saved_tty(void)
185 {
186     TTY *result = 0;
187
188     if (SP != 0) {
189         result = &(SP->_saved_tty);
190     } else {
191         if (_nc_prescreen.saved_tty == 0) {
192             _nc_prescreen.saved_tty = typeCalloc(TTY, 1);
193         }
194         result = _nc_prescreen.saved_tty;
195     }
196     return result;
197 }
198
199 /*
200 **      savetty()  and  resetty()
201 **
202 */
203
204 NCURSES_EXPORT(int)
205 savetty(void)
206 {
207     T((T_CALLED("savetty()")));
208
209     returnCode(_nc_get_tty_mode(saved_tty()));
210 }
211
212 NCURSES_EXPORT(int)
213 resetty(void)
214 {
215     T((T_CALLED("resetty()")));
216
217     returnCode(_nc_set_tty_mode(saved_tty()));
218 }