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