lib_ttyflags.c revision 1.5
1/* $OpenBSD: lib_ttyflags.c,v 1.5 2010/01/12 23:22:06 nicm Exp $ */
2
3/****************************************************************************
4 * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
5 *                                                                          *
6 * Permission is hereby granted, free of charge, to any person obtaining a  *
7 * copy of this software and associated documentation files (the            *
8 * "Software"), to deal in the Software without restriction, including      *
9 * without limitation the rights to use, copy, modify, merge, publish,      *
10 * distribute, distribute with modifications, sublicense, and/or sell       *
11 * copies of the Software, and to permit persons to whom the Software is    *
12 * furnished to do so, subject to the following conditions:                 *
13 *                                                                          *
14 * The above copyright notice and this permission notice shall be included  *
15 * in all copies or substantial portions of the Software.                   *
16 *                                                                          *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24 *                                                                          *
25 * Except as contained in this notice, the name(s) of the above copyright   *
26 * holders shall not be used in advertising or otherwise to promote the     *
27 * sale, use or other dealings in this Software without prior written       *
28 * authorization.                                                           *
29 ****************************************************************************/
30
31/*
32 *		def_prog_mode()
33 *		def_shell_mode()
34 *		reset_prog_mode()
35 *		reset_shell_mode()
36 *		savetty()
37 *		resetty()
38 */
39
40#include <curses.priv.h>
41#include <term.h>		/* cur_term */
42
43MODULE_ID("$Id: lib_ttyflags.c,v 1.5 2010/01/12 23:22:06 nicm Exp $")
44
45NCURSES_EXPORT(int)
46_nc_get_tty_mode(TTY * buf)
47{
48    int result = OK;
49
50    if (buf == 0) {
51	result = ERR;
52    } else {
53	if (cur_term == 0) {
54	    result = ERR;
55	} else {
56	    for (;;) {
57		if (GET_TTY(cur_term->Filedes, buf) != 0) {
58		    if (errno == EINTR)
59			continue;
60		    result = ERR;
61		}
62		break;
63	    }
64	}
65
66	if (result == ERR)
67	    memset(buf, 0, sizeof(*buf));
68
69	TR(TRACE_BITS, ("_nc_get_tty_mode(%d): %s",
70			cur_term ? cur_term->Filedes : -1,
71			_nc_trace_ttymode(buf)));
72    }
73    return (result);
74}
75
76NCURSES_EXPORT(int)
77_nc_set_tty_mode(TTY * buf)
78{
79    int result = OK;
80
81    if (buf == 0) {
82	result = ERR;
83    } else {
84	if (cur_term == 0) {
85	    result = ERR;
86	} else {
87	    for (;;) {
88		if (SET_TTY(cur_term->Filedes, buf) != 0) {
89		    if (errno == EINTR)
90			continue;
91		    if ((errno == ENOTTY) && (SP != 0))
92			SP->_notty = TRUE;
93		    result = ERR;
94		}
95		break;
96	    }
97	}
98	TR(TRACE_BITS, ("_nc_set_tty_mode(%d): %s",
99			cur_term ? cur_term->Filedes : -1,
100			_nc_trace_ttymode(buf)));
101    }
102    return (result);
103}
104
105NCURSES_EXPORT(int)
106def_shell_mode(void)
107{
108    int rc = ERR;
109
110    T((T_CALLED("def_shell_mode()")));
111
112    if (cur_term != 0) {
113	/*
114	 * If XTABS was on, remove the tab and backtab capabilities.
115	 */
116	if (_nc_get_tty_mode(&cur_term->Ottyb) == OK) {
117#ifdef TERMIOS
118	    if (cur_term->Ottyb.c_oflag & OFLAGS_TABS)
119		tab = back_tab = NULL;
120#else
121	    if (cur_term->Ottyb.sg_flags & XTABS)
122		tab = back_tab = NULL;
123#endif
124	    rc = OK;
125	}
126    }
127    returnCode(rc);
128}
129
130NCURSES_EXPORT(int)
131def_prog_mode(void)
132{
133    int rc = ERR;
134
135    T((T_CALLED("def_prog_mode()")));
136
137    if (cur_term != 0) {
138	/*
139	 * Turn off the XTABS bit in the tty structure if it was on.
140	 */
141	if (_nc_get_tty_mode(&cur_term->Nttyb) == OK) {
142#ifdef TERMIOS
143	    cur_term->Nttyb.c_oflag &= ~OFLAGS_TABS;
144#else
145	    cur_term->Nttyb.sg_flags &= ~XTABS;
146#endif
147	    rc = OK;
148	}
149    }
150    returnCode(rc);
151}
152
153NCURSES_EXPORT(int)
154reset_prog_mode(void)
155{
156    T((T_CALLED("reset_prog_mode()")));
157
158    if (cur_term != 0) {
159	if (_nc_set_tty_mode(&cur_term->Nttyb) == OK) {
160	    if (SP) {
161		if (SP->_keypad_on)
162		    _nc_keypad(SP, TRUE);
163		NC_BUFFERED(TRUE);
164	    }
165	    returnCode(OK);
166	}
167    }
168    returnCode(ERR);
169}
170
171NCURSES_EXPORT(int)
172reset_shell_mode(void)
173{
174    T((T_CALLED("reset_shell_mode()")));
175
176    if (cur_term != 0) {
177	if (SP) {
178	    _nc_keypad(SP, FALSE);
179	    _nc_flush();
180	    NC_BUFFERED(FALSE);
181	}
182	returnCode(_nc_set_tty_mode(&cur_term->Ottyb));
183    }
184    returnCode(ERR);
185}
186
187static TTY *
188saved_tty(void)
189{
190    TTY *result = 0;
191
192    if (SP != 0) {
193	result = &(SP->_saved_tty);
194    } else {
195	if (_nc_prescreen.saved_tty == 0) {
196	    _nc_prescreen.saved_tty = typeCalloc(TTY, 1);
197	}
198	result = _nc_prescreen.saved_tty;
199    }
200    return result;
201}
202
203/*
204**	savetty()  and  resetty()
205**
206*/
207
208NCURSES_EXPORT(int)
209savetty(void)
210{
211    T((T_CALLED("savetty()")));
212
213    returnCode(_nc_get_tty_mode(saved_tty()));
214}
215
216NCURSES_EXPORT(int)
217resetty(void)
218{
219    T((T_CALLED("resetty()")));
220
221    returnCode(_nc_set_tty_mode(saved_tty()));
222}
223