Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1 | /* input.c -- character input functions for readline. */ |
| 2 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 3 | /* Copyright (C) 1994-2013 Free Software Foundation, Inc. |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 5 | This file is part of the GNU Readline Library (Readline), a library |
| 6 | for reading lines of text with interactive input and history editing. |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 8 | Readline is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 11 | (at your option) any later version. |
| 12 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 13 | Readline is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 16 | GNU General Public License for more details. |
| 17 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | You should have received a copy of the GNU General Public License |
| 19 | along with Readline. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 22 | #define READLINE_LIBRARY |
| 23 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 24 | #if defined (__TANDEM) |
| 25 | # include <floss.h> |
| 26 | #endif |
| 27 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 28 | #if defined (HAVE_CONFIG_H) |
| 29 | # include <config.h> |
| 30 | #endif |
| 31 | |
| 32 | #include <sys/types.h> |
| 33 | #include <fcntl.h> |
| 34 | #if defined (HAVE_SYS_FILE_H) |
| 35 | # include <sys/file.h> |
| 36 | #endif /* HAVE_SYS_FILE_H */ |
| 37 | |
| 38 | #if defined (HAVE_UNISTD_H) |
| 39 | # include <unistd.h> |
| 40 | #endif /* HAVE_UNISTD_H */ |
| 41 | |
| 42 | #if defined (HAVE_STDLIB_H) |
| 43 | # include <stdlib.h> |
| 44 | #else |
| 45 | # include "ansi_stdlib.h" |
| 46 | #endif /* HAVE_STDLIB_H */ |
| 47 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 48 | #include <signal.h> |
| 49 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 50 | #include "posixselect.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 51 | |
| 52 | #if defined (FIONREAD_IN_SYS_IOCTL) |
| 53 | # include <sys/ioctl.h> |
| 54 | #endif |
| 55 | |
| 56 | #include <stdio.h> |
| 57 | #include <errno.h> |
| 58 | |
| 59 | #if !defined (errno) |
Ricardo Cerqueira | a02fbff | 2013-07-25 22:35:34 +0100 | [diff] [blame] | 60 | #include <errno.h> |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 61 | #endif /* !errno */ |
| 62 | |
| 63 | /* System-specific feature definitions and include files. */ |
| 64 | #include "rldefs.h" |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 65 | #include "rlmbutil.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 66 | |
| 67 | /* Some standard library routines. */ |
| 68 | #include "readline.h" |
| 69 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 70 | #include "rlprivate.h" |
| 71 | #include "rlshell.h" |
| 72 | #include "xmalloc.h" |
| 73 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 74 | /* What kind of non-blocking I/O do we have? */ |
| 75 | #if !defined (O_NDELAY) && defined (O_NONBLOCK) |
| 76 | # define O_NDELAY O_NONBLOCK /* Posix style */ |
| 77 | #endif |
| 78 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 79 | /* Non-null means it is a pointer to a function to run while waiting for |
| 80 | character input. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 81 | rl_hook_func_t *rl_event_hook = (rl_hook_func_t *)NULL; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 82 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 83 | /* A function to call if a read(2) is interrupted by a signal. */ |
| 84 | rl_hook_func_t *rl_signal_event_hook = (rl_hook_func_t *)NULL; |
| 85 | |
| 86 | /* A function to replace _rl_input_available for applications using the |
| 87 | callback interface. */ |
| 88 | rl_hook_func_t *rl_input_available_hook = (rl_hook_func_t *)NULL; |
| 89 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 90 | rl_getc_func_t *rl_getc_function = rl_getc; |
| 91 | |
| 92 | static int _keyboard_input_timeout = 100000; /* 0.1 seconds; it's in usec */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 93 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 94 | static int ibuffer_space PARAMS((void)); |
| 95 | static int rl_get_char PARAMS((int *)); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 96 | static int rl_gather_tyi PARAMS((void)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 97 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 98 | /* **************************************************************** */ |
| 99 | /* */ |
| 100 | /* Character Input Buffering */ |
| 101 | /* */ |
| 102 | /* **************************************************************** */ |
| 103 | |
| 104 | static int pop_index, push_index; |
| 105 | static unsigned char ibuffer[512]; |
| 106 | static int ibuffer_len = sizeof (ibuffer) - 1; |
| 107 | |
| 108 | #define any_typein (push_index != pop_index) |
| 109 | |
| 110 | int |
| 111 | _rl_any_typein () |
| 112 | { |
| 113 | return any_typein; |
| 114 | } |
| 115 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 116 | int |
| 117 | _rl_pushed_input_available () |
| 118 | { |
| 119 | return (push_index != pop_index); |
| 120 | } |
| 121 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 122 | /* Return the amount of space available in the buffer for stuffing |
| 123 | characters. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 124 | static int |
| 125 | ibuffer_space () |
| 126 | { |
| 127 | if (pop_index > push_index) |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 128 | return (pop_index - push_index - 1); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 129 | else |
| 130 | return (ibuffer_len - (push_index - pop_index)); |
| 131 | } |
| 132 | |
| 133 | /* Get a key from the buffer of characters to be read. |
| 134 | Return the key in KEY. |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 135 | Result is non-zero if there was a key, or 0 if there wasn't. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 136 | static int |
| 137 | rl_get_char (key) |
| 138 | int *key; |
| 139 | { |
| 140 | if (push_index == pop_index) |
| 141 | return (0); |
| 142 | |
| 143 | *key = ibuffer[pop_index++]; |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 144 | #if 0 |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 145 | if (pop_index >= ibuffer_len) |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 146 | #else |
| 147 | if (pop_index > ibuffer_len) |
| 148 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 149 | pop_index = 0; |
| 150 | |
| 151 | return (1); |
| 152 | } |
| 153 | |
| 154 | /* Stuff KEY into the *front* of the input buffer. |
| 155 | Returns non-zero if successful, zero if there is |
| 156 | no space left in the buffer. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 157 | int |
| 158 | _rl_unget_char (key) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 159 | int key; |
| 160 | { |
| 161 | if (ibuffer_space ()) |
| 162 | { |
| 163 | pop_index--; |
| 164 | if (pop_index < 0) |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 165 | pop_index = ibuffer_len; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 166 | ibuffer[pop_index] = key; |
| 167 | return (1); |
| 168 | } |
| 169 | return (0); |
| 170 | } |
| 171 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 172 | /* If a character is available to be read, then read it and stuff it into |
| 173 | IBUFFER. Otherwise, just return. Returns number of characters read |
| 174 | (0 if none available) and -1 on error (EIO). */ |
| 175 | static int |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 176 | rl_gather_tyi () |
| 177 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 178 | int tty; |
| 179 | register int tem, result; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 180 | int chars_avail, k; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 181 | char input; |
| 182 | #if defined(HAVE_SELECT) |
| 183 | fd_set readfds, exceptfds; |
| 184 | struct timeval timeout; |
| 185 | #endif |
| 186 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 187 | chars_avail = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 188 | tty = fileno (rl_instream); |
| 189 | |
| 190 | #if defined (HAVE_SELECT) |
| 191 | FD_ZERO (&readfds); |
| 192 | FD_ZERO (&exceptfds); |
| 193 | FD_SET (tty, &readfds); |
| 194 | FD_SET (tty, &exceptfds); |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 195 | USEC_TO_TIMEVAL (_keyboard_input_timeout, timeout); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 196 | result = select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout); |
| 197 | if (result <= 0) |
| 198 | return 0; /* Nothing to read. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 199 | #endif |
| 200 | |
| 201 | result = -1; |
| 202 | #if defined (FIONREAD) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 203 | errno = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 204 | result = ioctl (tty, FIONREAD, &chars_avail); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 205 | if (result == -1 && errno == EIO) |
| 206 | return -1; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 207 | #endif |
| 208 | |
| 209 | #if defined (O_NDELAY) |
| 210 | if (result == -1) |
| 211 | { |
| 212 | tem = fcntl (tty, F_GETFL, 0); |
| 213 | |
| 214 | fcntl (tty, F_SETFL, (tem | O_NDELAY)); |
| 215 | chars_avail = read (tty, &input, 1); |
| 216 | |
| 217 | fcntl (tty, F_SETFL, tem); |
| 218 | if (chars_avail == -1 && errno == EAGAIN) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 219 | return 0; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 220 | if (chars_avail == 0) /* EOF */ |
| 221 | { |
| 222 | rl_stuff_char (EOF); |
| 223 | return (0); |
| 224 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 225 | } |
| 226 | #endif /* O_NDELAY */ |
| 227 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 228 | #if defined (__MINGW32__) |
| 229 | /* Use getch/_kbhit to check for available console input, in the same way |
| 230 | that we read it normally. */ |
| 231 | chars_avail = isatty (tty) ? _kbhit () : 0; |
| 232 | result = 0; |
| 233 | #endif |
| 234 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 235 | /* If there's nothing available, don't waste time trying to read |
| 236 | something. */ |
| 237 | if (chars_avail <= 0) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 238 | return 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 239 | |
| 240 | tem = ibuffer_space (); |
| 241 | |
| 242 | if (chars_avail > tem) |
| 243 | chars_avail = tem; |
| 244 | |
| 245 | /* One cannot read all of the available input. I can only read a single |
| 246 | character at a time, or else programs which require input can be |
| 247 | thwarted. If the buffer is larger than one character, I lose. |
| 248 | Damn! */ |
| 249 | if (tem < ibuffer_len) |
| 250 | chars_avail = 0; |
| 251 | |
| 252 | if (result != -1) |
| 253 | { |
| 254 | while (chars_avail--) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 255 | { |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 256 | RL_CHECK_SIGNALS (); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 257 | k = (*rl_getc_function) (rl_instream); |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 258 | if (rl_stuff_char (k) == 0) |
| 259 | break; /* some problem; no more room */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 260 | if (k == NEWLINE || k == RETURN) |
| 261 | break; |
| 262 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 263 | } |
| 264 | else |
| 265 | { |
| 266 | if (chars_avail) |
| 267 | rl_stuff_char (input); |
| 268 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 269 | |
| 270 | return 1; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 273 | int |
| 274 | rl_set_keyboard_input_timeout (u) |
| 275 | int u; |
| 276 | { |
| 277 | int o; |
| 278 | |
| 279 | o = _keyboard_input_timeout; |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 280 | if (u >= 0) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 281 | _keyboard_input_timeout = u; |
| 282 | return (o); |
| 283 | } |
| 284 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 285 | /* Is there input available to be read on the readline input file |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 286 | descriptor? Only works if the system has select(2) or FIONREAD. |
| 287 | Uses the value of _keyboard_input_timeout as the timeout; if another |
| 288 | readline function wants to specify a timeout and not leave it up to |
| 289 | the user, it should use _rl_input_queued(timeout_value_in_microseconds) |
| 290 | instead. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 291 | int |
| 292 | _rl_input_available () |
| 293 | { |
| 294 | #if defined(HAVE_SELECT) |
| 295 | fd_set readfds, exceptfds; |
| 296 | struct timeval timeout; |
| 297 | #endif |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 298 | #if !defined (HAVE_SELECT) && defined(FIONREAD) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 299 | int chars_avail; |
| 300 | #endif |
| 301 | int tty; |
| 302 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 303 | if (rl_input_available_hook) |
| 304 | return (*rl_input_available_hook) (); |
| 305 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 306 | tty = fileno (rl_instream); |
| 307 | |
| 308 | #if defined (HAVE_SELECT) |
| 309 | FD_ZERO (&readfds); |
| 310 | FD_ZERO (&exceptfds); |
| 311 | FD_SET (tty, &readfds); |
| 312 | FD_SET (tty, &exceptfds); |
| 313 | timeout.tv_sec = 0; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 314 | timeout.tv_usec = _keyboard_input_timeout; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 315 | return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 316 | #else |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 317 | |
| 318 | #if defined (FIONREAD) |
| 319 | if (ioctl (tty, FIONREAD, &chars_avail) == 0) |
| 320 | return (chars_avail); |
| 321 | #endif |
| 322 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 323 | #endif |
| 324 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 325 | #if defined (__MINGW32__) |
| 326 | if (isatty (tty)) |
| 327 | return (_kbhit ()); |
| 328 | #endif |
| 329 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 330 | return 0; |
| 331 | } |
| 332 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 333 | int |
| 334 | _rl_input_queued (t) |
| 335 | int t; |
| 336 | { |
| 337 | int old_timeout, r; |
| 338 | |
| 339 | old_timeout = rl_set_keyboard_input_timeout (t); |
| 340 | r = _rl_input_available (); |
| 341 | rl_set_keyboard_input_timeout (old_timeout); |
| 342 | return r; |
| 343 | } |
| 344 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 345 | void |
| 346 | _rl_insert_typein (c) |
| 347 | int c; |
| 348 | { |
| 349 | int key, t, i; |
| 350 | char *string; |
| 351 | |
| 352 | i = key = 0; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 353 | string = (char *)xmalloc (ibuffer_len + 1); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 354 | string[i++] = (char) c; |
| 355 | |
| 356 | while ((t = rl_get_char (&key)) && |
| 357 | _rl_keymap[key].type == ISFUNC && |
| 358 | _rl_keymap[key].function == rl_insert) |
| 359 | string[i++] = key; |
| 360 | |
| 361 | if (t) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 362 | _rl_unget_char (key); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 363 | |
| 364 | string[i] = '\0'; |
| 365 | rl_insert_text (string); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 366 | xfree (string); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 369 | /* Add KEY to the buffer of characters to be read. Returns 1 if the |
| 370 | character was stuffed correctly; 0 otherwise. */ |
| 371 | int |
| 372 | rl_stuff_char (key) |
| 373 | int key; |
| 374 | { |
| 375 | if (ibuffer_space () == 0) |
| 376 | return 0; |
| 377 | |
| 378 | if (key == EOF) |
| 379 | { |
| 380 | key = NEWLINE; |
| 381 | rl_pending_input = EOF; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 382 | RL_SETSTATE (RL_STATE_INPUTPENDING); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 383 | } |
| 384 | ibuffer[push_index++] = key; |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 385 | #if 0 |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 386 | if (push_index >= ibuffer_len) |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 387 | #else |
| 388 | if (push_index > ibuffer_len) |
| 389 | #endif |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 390 | push_index = 0; |
| 391 | |
| 392 | return 1; |
| 393 | } |
| 394 | |
| 395 | /* Make C be the next command to be executed. */ |
| 396 | int |
| 397 | rl_execute_next (c) |
| 398 | int c; |
| 399 | { |
| 400 | rl_pending_input = c; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 401 | RL_SETSTATE (RL_STATE_INPUTPENDING); |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | /* Clear any pending input pushed with rl_execute_next() */ |
| 406 | int |
| 407 | rl_clear_pending_input () |
| 408 | { |
| 409 | rl_pending_input = 0; |
| 410 | RL_UNSETSTATE (RL_STATE_INPUTPENDING); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 411 | return 0; |
| 412 | } |
| 413 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 414 | /* **************************************************************** */ |
| 415 | /* */ |
| 416 | /* Character Input */ |
| 417 | /* */ |
| 418 | /* **************************************************************** */ |
| 419 | |
| 420 | /* Read a key, including pending input. */ |
| 421 | int |
| 422 | rl_read_key () |
| 423 | { |
Chet Ramey | 7a127e7 | 2012-07-10 09:43:57 -0400 | [diff] [blame] | 424 | int c, r; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 425 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 426 | if (rl_pending_input) |
| 427 | { |
| 428 | c = rl_pending_input; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 429 | rl_clear_pending_input (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 430 | } |
| 431 | else |
| 432 | { |
| 433 | /* If input is coming from a macro, then use that. */ |
| 434 | if (c = _rl_next_macro_key ()) |
| 435 | return (c); |
| 436 | |
| 437 | /* If the user has an event function, then call it periodically. */ |
| 438 | if (rl_event_hook) |
| 439 | { |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 440 | while (rl_event_hook) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 441 | { |
Chet Ramey | 7a127e7 | 2012-07-10 09:43:57 -0400 | [diff] [blame] | 442 | if (rl_get_char (&c) != 0) |
| 443 | break; |
| 444 | |
| 445 | if ((r = rl_gather_tyi ()) < 0) /* XXX - EIO */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 446 | { |
| 447 | rl_done = 1; |
| 448 | return ('\n'); |
| 449 | } |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 450 | else if (r > 0) /* read something */ |
Chet Ramey | 7a127e7 | 2012-07-10 09:43:57 -0400 | [diff] [blame] | 451 | continue; |
| 452 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 453 | RL_CHECK_SIGNALS (); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 454 | if (rl_done) /* XXX - experimental */ |
| 455 | return ('\n'); |
| 456 | (*rl_event_hook) (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | else |
| 460 | { |
| 461 | if (rl_get_char (&c) == 0) |
| 462 | c = (*rl_getc_function) (rl_instream); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 463 | /* fprintf(stderr, "rl_read_key: calling RL_CHECK_SIGNALS: _rl_caught_signal = %d", _rl_caught_signal); */ |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 464 | RL_CHECK_SIGNALS (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
| 468 | return (c); |
| 469 | } |
| 470 | |
| 471 | int |
| 472 | rl_getc (stream) |
| 473 | FILE *stream; |
| 474 | { |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 475 | int result; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 476 | unsigned char c; |
| 477 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 478 | while (1) |
| 479 | { |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 480 | RL_CHECK_SIGNALS (); |
| 481 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 482 | /* We know at this point that _rl_caught_signal == 0 */ |
| 483 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 484 | #if defined (__MINGW32__) |
| 485 | if (isatty (fileno (stream))) |
| 486 | return (getch ()); |
| 487 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 488 | result = read (fileno (stream), &c, sizeof (unsigned char)); |
| 489 | |
| 490 | if (result == sizeof (unsigned char)) |
| 491 | return (c); |
| 492 | |
| 493 | /* If zero characters are returned, then the file that we are |
| 494 | reading from is empty! Return EOF in that case. */ |
| 495 | if (result == 0) |
| 496 | return (EOF); |
| 497 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 498 | #if defined (__BEOS__) |
| 499 | if (errno == EINTR) |
| 500 | continue; |
| 501 | #endif |
| 502 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 503 | #if defined (EWOULDBLOCK) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 504 | # define X_EWOULDBLOCK EWOULDBLOCK |
| 505 | #else |
| 506 | # define X_EWOULDBLOCK -99 |
| 507 | #endif |
| 508 | |
| 509 | #if defined (EAGAIN) |
| 510 | # define X_EAGAIN EAGAIN |
| 511 | #else |
| 512 | # define X_EAGAIN -99 |
| 513 | #endif |
| 514 | |
| 515 | if (errno == X_EWOULDBLOCK || errno == X_EAGAIN) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 516 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 517 | if (sh_unset_nodelay_mode (fileno (stream)) < 0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 518 | return (EOF); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 519 | continue; |
| 520 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 521 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 522 | #undef X_EWOULDBLOCK |
| 523 | #undef X_EAGAIN |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 524 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 525 | /* fprintf(stderr, "rl_getc: result = %d errno = %d\n", result, errno); */ |
| 526 | |
| 527 | /* If the error that we received was EINTR, then try again, |
| 528 | this is simply an interrupted system call to read (). We allow |
| 529 | the read to be interrupted if we caught SIGHUP or SIGTERM (but |
| 530 | not SIGINT; let the signal handler deal with that), but if the |
| 531 | application sets an event hook, call it for other signals. |
| 532 | Otherwise (not EINTR), some error occurred, also signifying EOF. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 533 | if (errno != EINTR) |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 534 | return (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 535 | else if (_rl_caught_signal == SIGHUP || _rl_caught_signal == SIGTERM) |
| 536 | return (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF); |
Chet Ramey | 9b86eb2 | 2014-08-01 15:30:33 -0400 | [diff] [blame] | 537 | /* keyboard-generated signals of interest */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 538 | else if (_rl_caught_signal == SIGINT || _rl_caught_signal == SIGQUIT) |
| 539 | RL_CHECK_SIGNALS (); |
Chet Ramey | 9b86eb2 | 2014-08-01 15:30:33 -0400 | [diff] [blame] | 540 | /* non-keyboard-generated signals of interest */ |
| 541 | else if (_rl_caught_signal == SIGALRM |
| 542 | #if defined (SIGVTALRM) |
| 543 | || _rl_caught_signal == SIGVTALRM |
| 544 | #endif |
| 545 | ) |
| 546 | RL_CHECK_SIGNALS (); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 547 | |
| 548 | if (rl_signal_event_hook) |
| 549 | (*rl_signal_event_hook) (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 550 | } |
| 551 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 552 | |
| 553 | #if defined (HANDLE_MULTIBYTE) |
| 554 | /* read multibyte char */ |
| 555 | int |
| 556 | _rl_read_mbchar (mbchar, size) |
| 557 | char *mbchar; |
| 558 | int size; |
| 559 | { |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 560 | int mb_len, c; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 561 | size_t mbchar_bytes_length; |
| 562 | wchar_t wc; |
| 563 | mbstate_t ps, ps_back; |
| 564 | |
| 565 | memset(&ps, 0, sizeof (mbstate_t)); |
| 566 | memset(&ps_back, 0, sizeof (mbstate_t)); |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 567 | |
| 568 | mb_len = 0; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 569 | while (mb_len < size) |
| 570 | { |
| 571 | RL_SETSTATE(RL_STATE_MOREINPUT); |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 572 | c = rl_read_key (); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 573 | RL_UNSETSTATE(RL_STATE_MOREINPUT); |
| 574 | |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 575 | if (c < 0) |
| 576 | break; |
| 577 | |
| 578 | mbchar[mb_len++] = c; |
| 579 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 580 | mbchar_bytes_length = mbrtowc (&wc, mbchar, mb_len, &ps); |
| 581 | if (mbchar_bytes_length == (size_t)(-1)) |
| 582 | break; /* invalid byte sequence for the current locale */ |
| 583 | else if (mbchar_bytes_length == (size_t)(-2)) |
| 584 | { |
| 585 | /* shorted bytes */ |
| 586 | ps = ps_back; |
| 587 | continue; |
| 588 | } |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 589 | else if (mbchar_bytes_length == 0) |
| 590 | { |
| 591 | mbchar[0] = '\0'; /* null wide character */ |
| 592 | mb_len = 1; |
| 593 | break; |
| 594 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 595 | else if (mbchar_bytes_length > (size_t)(0)) |
| 596 | break; |
| 597 | } |
| 598 | |
| 599 | return mb_len; |
| 600 | } |
| 601 | |
| 602 | /* Read a multibyte-character string whose first character is FIRST into |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 603 | the buffer MB of length MLEN. Returns the last character read, which |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 604 | may be FIRST. Used by the search functions, among others. Very similar |
| 605 | to _rl_read_mbchar. */ |
| 606 | int |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 607 | _rl_read_mbstring (first, mb, mlen) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 608 | int first; |
| 609 | char *mb; |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 610 | int mlen; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 611 | { |
| 612 | int i, c; |
| 613 | mbstate_t ps; |
| 614 | |
| 615 | c = first; |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 616 | memset (mb, 0, mlen); |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 617 | for (i = 0; c >= 0 && i < mlen; i++) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 618 | { |
| 619 | mb[i] = (char)c; |
| 620 | memset (&ps, 0, sizeof (mbstate_t)); |
| 621 | if (_rl_get_char_len (mb, &ps) == -2) |
| 622 | { |
| 623 | /* Read more for multibyte character */ |
| 624 | RL_SETSTATE (RL_STATE_MOREINPUT); |
| 625 | c = rl_read_key (); |
| 626 | RL_UNSETSTATE (RL_STATE_MOREINPUT); |
| 627 | } |
| 628 | else |
| 629 | break; |
| 630 | } |
| 631 | return c; |
| 632 | } |
| 633 | #endif /* HANDLE_MULTIBYTE */ |