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