1 /****************************************************************************
2  * Copyright 2020 Thomas E. Dickey                                          *
3  * Copyright 1998-2009,2010 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 
30 /****************************************************************************
31  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
32  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
33  *     and: Thomas E. Dickey                        2002                    *
34  *     and: Juergen Pfeifer                         2009                    *
35  ****************************************************************************/
36 
37 /*
38  *	lib_kernel.c
39  *
40  *	Misc. low-level routines:
41  *		erasechar()
42  *		killchar()
43  *		flushinp()
44  *
45  * The baudrate() and delay_output() functions could logically live here,
46  * but are in other modules to reduce the static-link size of programs
47  * that use only these facilities.
48  */
49 
50 #include <curses.priv.h>
51 
52 MODULE_ID("$Id: lib_kernel.c,v 1.32 2020/02/02 23:34:34 tom Exp $")
53 
54 static int
55 _nc_vdisable(void)
56 {
57     int value = -1;
58 #if defined(_POSIX_VDISABLE) && HAVE_UNISTD_H
59     value = _POSIX_VDISABLE;
60 #endif
61 #if defined(_PC_VDISABLE)
62     if (value == -1) {
63 	value = (int) fpathconf(0, _PC_VDISABLE);
64 	if (value == -1) {
65 	    value = 0377;
66 	}
67     }
68 #elif defined(VDISABLE)
69     if (value == -1)
70 	value = VDISABLE;
71 #endif
72     return value;
73 }
74 
75 /*
76  *	erasechar()
77  *
78  *	Return erase character as given in cur_term->Ottyb.
79  *
80  */
81 
82 NCURSES_EXPORT(char)
83 NCURSES_SP_NAME(erasechar) (NCURSES_SP_DCL0)
84 {
85     int result = ERR;
86     TERMINAL *termp = TerminalOf(SP_PARM);
87 
88     T((T_CALLED("erasechar(%p)"), (void *) SP_PARM));
89 
90     if (termp != 0) {
91 #ifdef TERMIOS
92 	result = termp->Ottyb.c_cc[VERASE];
93 	if (result == _nc_vdisable())
94 	    result = ERR;
95 #else
96 	result = termp->Ottyb.sg_erase;
97 #endif
98     }
99     returnChar((char) result);
100 }
101 
102 #if NCURSES_SP_FUNCS
103 NCURSES_EXPORT(char)
104 erasechar(void)
105 {
106     return NCURSES_SP_NAME(erasechar) (CURRENT_SCREEN);
107 }
108 #endif
109 
110 /*
111  *	killchar()
112  *
113  *	Return kill character as given in cur_term->Ottyb.
114  *
115  */
116 
117 NCURSES_EXPORT(char)
118 NCURSES_SP_NAME(killchar) (NCURSES_SP_DCL0)
119 {
120     int result = ERR;
121     TERMINAL *termp = TerminalOf(SP_PARM);
122 
123     T((T_CALLED("killchar(%p)"), (void *) SP_PARM));
124 
125     if (termp != 0) {
126 #ifdef TERMIOS
127 	result = termp->Ottyb.c_cc[VKILL];
128 	if (result == _nc_vdisable())
129 	    result = ERR;
130 #else
131 	result = termp->Ottyb.sg_kill;
132 #endif
133     }
134     returnChar((char) result);
135 }
136 
137 #if NCURSES_SP_FUNCS
138 NCURSES_EXPORT(char)
139 killchar(void)
140 {
141     return NCURSES_SP_NAME(killchar) (CURRENT_SCREEN);
142 }
143 #endif
144 
145 /*
146  *	flushinp()
147  *
148  *	Flush any input on cur_term->Filedes
149  *
150  */
151 
152 NCURSES_EXPORT(int)
153 NCURSES_SP_NAME(flushinp) (NCURSES_SP_DCL0)
154 {
155     TERMINAL *termp = TerminalOf(SP_PARM);
156 
157     T((T_CALLED("flushinp(%p)"), (void *) SP_PARM));
158 
159     if (termp != 0) {
160 #ifdef TERMIOS
161 	tcflush(termp->Filedes, TCIFLUSH);
162 #else
163 	errno = 0;
164 	do {
165 	    ioctl(termp->Filedes, TIOCFLUSH, 0);
166 	} while
167 	    (errno == EINTR);
168 #endif
169 	if (SP_PARM) {
170 	    SP_PARM->_fifohead = -1;
171 	    SP_PARM->_fifotail = 0;
172 	    SP_PARM->_fifopeek = 0;
173 	}
174 	returnCode(OK);
175     }
176     returnCode(ERR);
177 }
178 
179 #if NCURSES_SP_FUNCS
180 NCURSES_EXPORT(int)
181 flushinp(void)
182 {
183     return NCURSES_SP_NAME(flushinp) (CURRENT_SCREEN);
184 }
185 #endif
186