ncurses 5.6 - patch 20070512
[ncurses.git] / ncurses / tinfo / lib_ttyflags.c
1 /****************************************************************************
2  * Copyright (c) 1998-2005,2006 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.14 2006/12/30 21:37:20 tom Exp $")
42
43 NCURSES_EXPORT(int)
44 _nc_get_tty_mode(TTY * buf)
45 {
46     int result = OK;
47
48     if (cur_term == 0) {
49         result = ERR;
50     } else {
51         for (;;) {
52             if (GET_TTY(cur_term->Filedes, buf) != 0) {
53                 if (errno == EINTR)
54                     continue;
55                 result = ERR;
56             }
57             break;
58         }
59     }
60
61     if (result == ERR)
62         memset(buf, 0, sizeof(*buf));
63
64     TR(TRACE_BITS, ("_nc_get_tty_mode(%d): %s",
65                     cur_term->Filedes, _nc_trace_ttymode(buf)));
66     return (result);
67 }
68
69 NCURSES_EXPORT(int)
70 _nc_set_tty_mode(TTY * buf)
71 {
72     int result = OK;
73
74     if (cur_term == 0) {
75         result = ERR;
76     } else {
77         for (;;) {
78             if (SET_TTY(cur_term->Filedes, buf) != 0) {
79                 if (errno == EINTR)
80                     continue;
81                 if ((errno == ENOTTY) && (SP != 0))
82                     SP->_notty = TRUE;
83                 result = ERR;
84             }
85             break;
86         }
87     }
88     TR(TRACE_BITS, ("_nc_set_tty_mode(%d): %s",
89                     cur_term->Filedes, _nc_trace_ttymode(buf)));
90     return (result);
91 }
92
93 NCURSES_EXPORT(int)
94 def_shell_mode(void)
95 {
96     T((T_CALLED("def_shell_mode()")));
97
98     /*
99      * If XTABS was on, remove the tab and backtab capabilities.
100      */
101
102     if (_nc_get_tty_mode(&cur_term->Ottyb) != OK)
103         returnCode(ERR);
104 #ifdef TERMIOS
105     if (cur_term->Ottyb.c_oflag & OFLAGS_TABS)
106         tab = back_tab = NULL;
107 #else
108     if (cur_term->Ottyb.sg_flags & XTABS)
109         tab = back_tab = NULL;
110 #endif
111     returnCode(OK);
112 }
113
114 NCURSES_EXPORT(int)
115 def_prog_mode(void)
116 {
117     T((T_CALLED("def_prog_mode()")));
118
119     /*
120      * Turn off the XTABS bit in the tty structure if it was on.
121      */
122
123     if (_nc_get_tty_mode(&cur_term->Nttyb) != OK)
124         returnCode(ERR);
125 #ifdef TERMIOS
126     cur_term->Nttyb.c_oflag &= ~OFLAGS_TABS;
127 #else
128     cur_term->Nttyb.sg_flags &= ~XTABS;
129 #endif
130     returnCode(OK);
131 }
132
133 NCURSES_EXPORT(int)
134 reset_prog_mode(void)
135 {
136     T((T_CALLED("reset_prog_mode()")));
137
138     if (cur_term != 0) {
139         if (_nc_set_tty_mode(&cur_term->Nttyb) == OK) {
140             if (SP) {
141                 if (SP->_keypad_on)
142                     _nc_keypad(TRUE);
143                 NC_BUFFERED(TRUE);
144             }
145             returnCode(OK);
146         }
147     }
148     returnCode(ERR);
149 }
150
151 NCURSES_EXPORT(int)
152 reset_shell_mode(void)
153 {
154     T((T_CALLED("reset_shell_mode()")));
155
156     if (cur_term != 0) {
157         if (SP) {
158             _nc_keypad(FALSE);
159             _nc_flush();
160             NC_BUFFERED(FALSE);
161         }
162         returnCode(_nc_set_tty_mode(&cur_term->Ottyb));
163     }
164     returnCode(ERR);
165 }
166
167 /*
168 **      savetty()  and  resetty()
169 **
170 */
171
172 NCURSES_EXPORT(int)
173 savetty(void)
174 {
175     T((T_CALLED("savetty()")));
176
177     returnCode(_nc_get_tty_mode(&(SP->_saved_tty)));
178 }
179
180 NCURSES_EXPORT(int)
181 resetty(void)
182 {
183     T((T_CALLED("resetty()")));
184
185     returnCode(_nc_set_tty_mode(&(SP->_saved_tty)));
186 }