ncurses 4.2
[ncurses.git] / ncurses / lib_kernel.c
1 /****************************************************************************
2  * Copyright (c) 1998 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  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  ****************************************************************************/
33
34
35 /*
36  *      lib_kernel.c
37  *
38  *      Misc. low-level routines:
39  *              reset_prog_mode()
40  *              reset_shell_mode()
41  *              erasechar()
42  *              killchar()
43  *              flushinp()
44  *              savetty()
45  *              resetty()
46  *
47  * The baudrate() and delay_output() functions could logically live here,
48  * but are in other modules to reduce the static-link size of programs
49  * that use only these facilities.
50  */
51
52 #include <curses.priv.h>
53 #include <term.h>       /* cur_term */
54
55 MODULE_ID("$Id: lib_kernel.c,v 1.17 1998/02/11 12:13:57 tom Exp $")
56
57 int reset_prog_mode(void)
58 {
59         T((T_CALLED("reset_prog_mode()")));
60
61         if (cur_term != 0) {
62                 _nc_set_curterm(&cur_term->Nttyb);
63                 if (SP && stdscr && stdscr->_use_keypad)
64                         _nc_keypad(TRUE);
65                 returnCode(OK);
66         }
67         returnCode(ERR);
68 }
69
70
71 int reset_shell_mode(void)
72 {
73         T((T_CALLED("reset_shell_mode()")));
74
75         if (cur_term != 0) {
76                 if (SP)
77                 {
78                         fflush(SP->_ofp);
79                         _nc_keypad(FALSE);
80                 }
81                 returnCode(_nc_set_curterm(&cur_term->Ottyb));
82         }
83         returnCode(ERR);
84 }
85
86 /*
87  *      erasechar()
88  *
89  *      Return erase character as given in cur_term->Ottyb.
90  *
91  */
92
93 char
94 erasechar(void)
95 {
96         T((T_CALLED("erasechar()")));
97
98         if (cur_term != 0) {
99 #ifdef TERMIOS
100                 returnCode(cur_term->Ottyb.c_cc[VERASE]);
101 #else
102                 returnCode(cur_term->Ottyb.sg_erase);
103 #endif
104         }
105         returnCode(ERR);
106 }
107
108
109
110 /*
111  *      killchar()
112  *
113  *      Return kill character as given in cur_term->Ottyb.
114  *
115  */
116
117 char
118 killchar(void)
119 {
120         T((T_CALLED("killchar()")));
121
122         if (cur_term != 0) {
123 #ifdef TERMIOS
124                 returnCode(cur_term->Ottyb.c_cc[VKILL]);
125 #else
126                 returnCode(cur_term->Ottyb.sg_kill);
127 #endif
128         }
129         returnCode(ERR);
130 }
131
132
133
134 /*
135  *      flushinp()
136  *
137  *      Flush any input on cur_term->Filedes
138  *
139  */
140
141 int flushinp(void)
142 {
143         T((T_CALLED("flushinp()")));
144
145         if (cur_term != 0) {
146 #ifdef TERMIOS
147                 tcflush(cur_term->Filedes, TCIFLUSH);
148 #else
149                 errno = 0;
150                 do {
151                     ioctl(cur_term->Filedes, TIOCFLUSH, 0);
152                 } while
153                     (errno == EINTR);
154 #endif
155                 if (SP) {
156                         SP->_fifohead = -1;
157                         SP->_fifotail = 0;
158                         SP->_fifopeek = 0;
159                 }
160                 returnCode(OK);
161         }
162         returnCode(ERR);
163 }
164
165 /*
166 **      savetty()  and  resetty()
167 **
168 */
169
170 static TTY   buf;
171
172 int savetty(void)
173 {
174         T((T_CALLED("savetty()")));
175
176         returnCode(_nc_get_curterm(&buf));
177 }
178
179 int resetty(void)
180 {
181         T((T_CALLED("resetty()")));
182
183         returnCode(_nc_set_curterm(&buf));
184 }