Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1 | /**************************************************************************** |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 2 | * Copyright 2020-2022,2023 Thomas E. Dickey * |
| 3 | * Copyright 1998-2009,2010 Free Software Foundation, Inc. * |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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> * |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 33 | * and: Thomas E. Dickey 2002 * |
| 34 | * and: Juergen Pfeifer 2009 * |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 51 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 52 | MODULE_ID("$Id: lib_kernel.c,v 1.36 2023/06/10 13:29:06 tom Exp $") |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 53 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 54 | #ifdef TERMIOS |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 55 | static int |
| 56 | _nc_vdisable(void) |
| 57 | { |
| 58 | int value = -1; |
| 59 | #if defined(_POSIX_VDISABLE) && HAVE_UNISTD_H |
| 60 | value = _POSIX_VDISABLE; |
| 61 | #endif |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 62 | #if defined(_PC_VDISABLE) && HAVE_FPATHCONF |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 63 | if (value == -1) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 64 | value = (int) fpathconf(0, _PC_VDISABLE); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 65 | if (value == -1) { |
| 66 | value = 0377; |
| 67 | } |
| 68 | } |
| 69 | #elif defined(VDISABLE) |
| 70 | if (value == -1) |
| 71 | value = VDISABLE; |
| 72 | #endif |
| 73 | return value; |
| 74 | } |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 75 | #endif /* TERMIOS */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 76 | |
| 77 | /* |
| 78 | * erasechar() |
| 79 | * |
| 80 | * Return erase character as given in cur_term->Ottyb. |
| 81 | * |
| 82 | */ |
| 83 | |
| 84 | NCURSES_EXPORT(char) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 85 | NCURSES_SP_NAME(erasechar) (NCURSES_SP_DCL0) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 86 | { |
| 87 | int result = ERR; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 88 | TERMINAL *termp = TerminalOf(SP_PARM); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 89 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 90 | T((T_CALLED("erasechar(%p)"), (void *) SP_PARM)); |
| 91 | |
| 92 | if (termp != 0) { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 93 | #ifdef TERMIOS |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 94 | result = termp->Ottyb.c_cc[VERASE]; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 95 | if (result == _nc_vdisable()) |
| 96 | result = ERR; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 97 | #elif defined(EXP_WIN32_DRIVER) |
| 98 | result = ERR; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 99 | #else |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 100 | result = termp->Ottyb.sg_erase; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 101 | #endif |
| 102 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 103 | returnChar((char) result); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 104 | } |
| 105 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 106 | #if NCURSES_SP_FUNCS |
| 107 | NCURSES_EXPORT(char) |
| 108 | erasechar(void) |
| 109 | { |
| 110 | return NCURSES_SP_NAME(erasechar) (CURRENT_SCREEN); |
| 111 | } |
| 112 | #endif |
| 113 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 114 | /* |
| 115 | * killchar() |
| 116 | * |
| 117 | * Return kill character as given in cur_term->Ottyb. |
| 118 | * |
| 119 | */ |
| 120 | |
| 121 | NCURSES_EXPORT(char) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 122 | NCURSES_SP_NAME(killchar) (NCURSES_SP_DCL0) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 123 | { |
| 124 | int result = ERR; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 125 | TERMINAL *termp = TerminalOf(SP_PARM); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 126 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 127 | T((T_CALLED("killchar(%p)"), (void *) SP_PARM)); |
| 128 | |
| 129 | if (termp != 0) { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 130 | #ifdef TERMIOS |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 131 | result = termp->Ottyb.c_cc[VKILL]; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 132 | if (result == _nc_vdisable()) |
| 133 | result = ERR; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 134 | #elif defined(EXP_WIN32_DRIVER) |
| 135 | result = ERR; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 136 | #else |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 137 | result = termp->Ottyb.sg_kill; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 138 | #endif |
| 139 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 140 | returnChar((char) result); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 141 | } |
| 142 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 143 | #if NCURSES_SP_FUNCS |
| 144 | NCURSES_EXPORT(char) |
| 145 | killchar(void) |
| 146 | { |
| 147 | return NCURSES_SP_NAME(killchar) (CURRENT_SCREEN); |
| 148 | } |
| 149 | #endif |
| 150 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 151 | static void |
| 152 | flush_input(int fd) |
| 153 | { |
| 154 | #ifdef TERMIOS |
| 155 | tcflush(fd, TCIFLUSH); |
| 156 | #else /* !TERMIOS */ |
| 157 | errno = 0; |
| 158 | do { |
| 159 | #if defined(EXP_WIN32_DRIVER) |
| 160 | _nc_console_flush(_nc_console_fd2handle(fd)); |
| 161 | #else |
| 162 | ioctl(fd, TIOCFLUSH, 0); |
| 163 | #endif |
| 164 | } while |
| 165 | (errno == EINTR); |
| 166 | #endif |
| 167 | } |
| 168 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 169 | /* |
| 170 | * flushinp() |
| 171 | * |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 172 | * Flush any input on tty |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 173 | */ |
| 174 | |
| 175 | NCURSES_EXPORT(int) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 176 | NCURSES_SP_NAME(flushinp) (NCURSES_SP_DCL0) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 177 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 178 | T((T_CALLED("flushinp(%p)"), (void *) SP_PARM)); |
| 179 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 180 | if (SP_PARM != 0) { |
| 181 | if (NC_ISATTY(SP_PARM->_ifd)) |
| 182 | flush_input(SP_PARM->_ifd); |
| 183 | else if (NC_ISATTY(SP_PARM->_ofd)) |
| 184 | flush_input(SP_PARM->_ofd); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 185 | if (SP_PARM) { |
| 186 | SP_PARM->_fifohead = -1; |
| 187 | SP_PARM->_fifotail = 0; |
| 188 | SP_PARM->_fifopeek = 0; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 189 | } |
| 190 | returnCode(OK); |
| 191 | } |
| 192 | returnCode(ERR); |
| 193 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 194 | |
| 195 | #if NCURSES_SP_FUNCS |
| 196 | NCURSES_EXPORT(int) |
| 197 | flushinp(void) |
| 198 | { |
| 199 | return NCURSES_SP_NAME(flushinp) (CURRENT_SCREEN); |
| 200 | } |
| 201 | #endif |