| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/bash/src/lib/readline/input.c |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /* input.c -- character input functions for readline. */ | - | ||||||||||||
| 2 | - | |||||||||||||
| 3 | /* Copyright (C) 1994-2017 Free Software Foundation, Inc. | - | ||||||||||||
| 4 | - | |||||||||||||
| 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. | - | ||||||||||||
| 7 | - | |||||||||||||
| 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 | - | ||||||||||||
| 11 | (at your option) any later version. | - | ||||||||||||
| 12 | - | |||||||||||||
| 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 | - | ||||||||||||
| 16 | GNU General Public License for more details. | - | ||||||||||||
| 17 | - | |||||||||||||
| 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 | - | |||||||||||||
| 22 | #define READLINE_LIBRARY | - | ||||||||||||
| 23 | - | |||||||||||||
| 24 | #if defined (__TANDEM) | - | ||||||||||||
| 25 | # include <floss.h> | - | ||||||||||||
| 26 | #endif | - | ||||||||||||
| 27 | - | |||||||||||||
| 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 | - | |||||||||||||
| 48 | #include <signal.h> | - | ||||||||||||
| 49 | - | |||||||||||||
| 50 | #include "posixselect.h" | - | ||||||||||||
| 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) | - | ||||||||||||
| 60 | extern int errno; | - | ||||||||||||
| 61 | #endif /* !errno */ | - | ||||||||||||
| 62 | - | |||||||||||||
| 63 | /* System-specific feature definitions and include files. */ | - | ||||||||||||
| 64 | #include "rldefs.h" | - | ||||||||||||
| 65 | #include "rlmbutil.h" | - | ||||||||||||
| 66 | - | |||||||||||||
| 67 | /* Some standard library routines. */ | - | ||||||||||||
| 68 | #include "readline.h" | - | ||||||||||||
| 69 | - | |||||||||||||
| 70 | #include "rlprivate.h" | - | ||||||||||||
| 71 | #include "rlshell.h" | - | ||||||||||||
| 72 | #include "xmalloc.h" | - | ||||||||||||
| 73 | - | |||||||||||||
| 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 | - | |||||||||||||
| 79 | #if defined (HAVE_PSELECT) | - | ||||||||||||
| 80 | extern sigset_t _rl_orig_sigset; | - | ||||||||||||
| 81 | #endif | - | ||||||||||||
| 82 | - | |||||||||||||
| 83 | /* Non-null means it is a pointer to a function to run while waiting for | - | ||||||||||||
| 84 | character input. */ | - | ||||||||||||
| 85 | rl_hook_func_t *rl_event_hook = (rl_hook_func_t *)NULL; | - | ||||||||||||
| 86 | - | |||||||||||||
| 87 | /* A function to call if a read(2) is interrupted by a signal. */ | - | ||||||||||||
| 88 | rl_hook_func_t *rl_signal_event_hook = (rl_hook_func_t *)NULL; | - | ||||||||||||
| 89 | - | |||||||||||||
| 90 | /* A function to replace _rl_input_available for applications using the | - | ||||||||||||
| 91 | callback interface. */ | - | ||||||||||||
| 92 | rl_hook_func_t *rl_input_available_hook = (rl_hook_func_t *)NULL; | - | ||||||||||||
| 93 | - | |||||||||||||
| 94 | rl_getc_func_t *rl_getc_function = rl_getc; | - | ||||||||||||
| 95 | - | |||||||||||||
| 96 | static int _keyboard_input_timeout = 100000; /* 0.1 seconds; it's in usec */ | - | ||||||||||||
| 97 | - | |||||||||||||
| 98 | static int ibuffer_space PARAMS((void)); | - | ||||||||||||
| 99 | static int rl_get_char PARAMS((int *)); | - | ||||||||||||
| 100 | static int rl_gather_tyi PARAMS((void)); | - | ||||||||||||
| 101 | - | |||||||||||||
| 102 | /* Windows isatty returns true for every character device, including the null | - | ||||||||||||
| 103 | device, so we need to perform additional checks. */ | - | ||||||||||||
| 104 | #if defined (_WIN32) && !defined (__CYGWIN__) | - | ||||||||||||
| 105 | #include <conio.h> | - | ||||||||||||
| 106 | #include <io.h> | - | ||||||||||||
| 107 | #define WIN32_LEAN_AND_MEAN 1 | - | ||||||||||||
| 108 | #include <windows.h> | - | ||||||||||||
| 109 | - | |||||||||||||
| 110 | int | - | ||||||||||||
| 111 | win32_isatty (int fd) | - | ||||||||||||
| 112 | { | - | ||||||||||||
| 113 | return (_isatty (fd) ? ((((long) (HANDLE) _get_osfhandle (fd)) & 3) == 3) : 0); | - | ||||||||||||
| 114 | } | - | ||||||||||||
| 115 | - | |||||||||||||
| 116 | #define isatty(x) win32_isatty(x) | - | ||||||||||||
| 117 | #endif | - | ||||||||||||
| 118 | - | |||||||||||||
| 119 | /* **************************************************************** */ | - | ||||||||||||
| 120 | /* */ | - | ||||||||||||
| 121 | /* Character Input Buffering */ | - | ||||||||||||
| 122 | /* */ | - | ||||||||||||
| 123 | /* **************************************************************** */ | - | ||||||||||||
| 124 | - | |||||||||||||
| 125 | static int pop_index, push_index; | - | ||||||||||||
| 126 | static unsigned char ibuffer[512]; | - | ||||||||||||
| 127 | static int ibuffer_len = sizeof (ibuffer) - 1; | - | ||||||||||||
| 128 | - | |||||||||||||
| 129 | #define any_typein (push_index != pop_index) | - | ||||||||||||
| 130 | - | |||||||||||||
| 131 | int | - | ||||||||||||
| 132 | _rl_any_typein (void) | - | ||||||||||||
| 133 | { | - | ||||||||||||
| 134 | return any_typein; never executed: return (push_index != pop_index); | 0 | ||||||||||||
| 135 | } | - | ||||||||||||
| 136 | - | |||||||||||||
| 137 | int | - | ||||||||||||
| 138 | _rl_pushed_input_available (void) | - | ||||||||||||
| 139 | { | - | ||||||||||||
| 140 | return (push_index != pop_index); never executed: return (push_index != pop_index); | 0 | ||||||||||||
| 141 | } | - | ||||||||||||
| 142 | - | |||||||||||||
| 143 | /* Return the amount of space available in the buffer for stuffing | - | ||||||||||||
| 144 | characters. */ | - | ||||||||||||
| 145 | static int | - | ||||||||||||
| 146 | ibuffer_space (void) | - | ||||||||||||
| 147 | { | - | ||||||||||||
| 148 | if (pop_index > push_index)
| 0 | ||||||||||||
| 149 | return (pop_index - push_index - 1); never executed: return (pop_index - push_index - 1); | 0 | ||||||||||||
| 150 | else | - | ||||||||||||
| 151 | return (ibuffer_len - (push_index - pop_index)); never executed: return (ibuffer_len - (push_index - pop_index)); | 0 | ||||||||||||
| 152 | } | - | ||||||||||||
| 153 | - | |||||||||||||
| 154 | /* Get a key from the buffer of characters to be read. | - | ||||||||||||
| 155 | Return the key in KEY. | - | ||||||||||||
| 156 | Result is non-zero if there was a key, or 0 if there wasn't. */ | - | ||||||||||||
| 157 | static int | - | ||||||||||||
| 158 | rl_get_char (int *key) | - | ||||||||||||
| 159 | { | - | ||||||||||||
| 160 | if (push_index == pop_index)
| 0 | ||||||||||||
| 161 | return (0); never executed: return (0); | 0 | ||||||||||||
| 162 | - | |||||||||||||
| 163 | *key = ibuffer[pop_index++]; | - | ||||||||||||
| 164 | #if 0 | - | ||||||||||||
| 165 | if (pop_index >= ibuffer_len) | - | ||||||||||||
| 166 | #else | - | ||||||||||||
| 167 | if (pop_index > ibuffer_len)
| 0 | ||||||||||||
| 168 | #endif | - | ||||||||||||
| 169 | pop_index = 0; never executed: pop_index = 0; | 0 | ||||||||||||
| 170 | - | |||||||||||||
| 171 | return (1); never executed: return (1); | 0 | ||||||||||||
| 172 | } | - | ||||||||||||
| 173 | - | |||||||||||||
| 174 | /* Stuff KEY into the *front* of the input buffer. | - | ||||||||||||
| 175 | Returns non-zero if successful, zero if there is | - | ||||||||||||
| 176 | no space left in the buffer. */ | - | ||||||||||||
| 177 | int | - | ||||||||||||
| 178 | _rl_unget_char (int key) | - | ||||||||||||
| 179 | { | - | ||||||||||||
| 180 | if (ibuffer_space ())
| 0 | ||||||||||||
| 181 | { | - | ||||||||||||
| 182 | pop_index--; | - | ||||||||||||
| 183 | if (pop_index < 0)
| 0 | ||||||||||||
| 184 | pop_index = ibuffer_len; never executed: pop_index = ibuffer_len; | 0 | ||||||||||||
| 185 | ibuffer[pop_index] = key; | - | ||||||||||||
| 186 | return (1); never executed: return (1); | 0 | ||||||||||||
| 187 | } | - | ||||||||||||
| 188 | return (0); never executed: return (0); | 0 | ||||||||||||
| 189 | } | - | ||||||||||||
| 190 | - | |||||||||||||
| 191 | /* If a character is available to be read, then read it and stuff it into | - | ||||||||||||
| 192 | IBUFFER. Otherwise, just return. Returns number of characters read | - | ||||||||||||
| 193 | (0 if none available) and -1 on error (EIO). */ | - | ||||||||||||
| 194 | static int | - | ||||||||||||
| 195 | rl_gather_tyi (void) | - | ||||||||||||
| 196 | { | - | ||||||||||||
| 197 | int tty; | - | ||||||||||||
| 198 | register int tem, result; | - | ||||||||||||
| 199 | int chars_avail, k; | - | ||||||||||||
| 200 | char input; | - | ||||||||||||
| 201 | #if defined(HAVE_SELECT) | - | ||||||||||||
| 202 | fd_set readfds, exceptfds; | - | ||||||||||||
| 203 | struct timeval timeout; | - | ||||||||||||
| 204 | #endif | - | ||||||||||||
| 205 | - | |||||||||||||
| 206 | chars_avail = 0; | - | ||||||||||||
| 207 | input = 0; | - | ||||||||||||
| 208 | tty = fileno (rl_instream); | - | ||||||||||||
| 209 | - | |||||||||||||
| 210 | #if defined (HAVE_SELECT) | - | ||||||||||||
| 211 | FD_ZERO (&readfds); | - | ||||||||||||
| 212 | FD_ZERO (&exceptfds); | - | ||||||||||||
| 213 | FD_SET (tty, &readfds); | - | ||||||||||||
| 214 | FD_SET (tty, &exceptfds); | - | ||||||||||||
| 215 | USEC_TO_TIMEVAL (_keyboard_input_timeout, timeout); | - | ||||||||||||
| 216 | result = select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout); | - | ||||||||||||
| 217 | if (result <= 0)
| 0 | ||||||||||||
| 218 | return 0; /* Nothing to read. */ never executed: return 0; | 0 | ||||||||||||
| 219 | #endif | - | ||||||||||||
| 220 | - | |||||||||||||
| 221 | result = -1; | - | ||||||||||||
| 222 | errno = 0; | - | ||||||||||||
| 223 | #if defined (FIONREAD) | - | ||||||||||||
| 224 | result = ioctl (tty, FIONREAD, &chars_avail); | - | ||||||||||||
| 225 | if (result == -1 && errno == EIO)
| 0 | ||||||||||||
| 226 | return -1; never executed: return -1; | 0 | ||||||||||||
| 227 | if (result == -1)
| 0 | ||||||||||||
| 228 | chars_avail = 0; never executed: chars_avail = 0; | 0 | ||||||||||||
| 229 | #endif | - | ||||||||||||
| 230 | - | |||||||||||||
| 231 | #if defined (O_NDELAY) | - | ||||||||||||
| 232 | if (result == -1)
| 0 | ||||||||||||
| 233 | { | - | ||||||||||||
| 234 | tem = fcntl (tty, F_GETFL, 0); | - | ||||||||||||
| 235 | - | |||||||||||||
| 236 | fcntl (tty, F_SETFL, (tem | O_NDELAY)); | - | ||||||||||||
| 237 | chars_avail = read (tty, &input, 1); | - | ||||||||||||
| 238 | - | |||||||||||||
| 239 | fcntl (tty, F_SETFL, tem); | - | ||||||||||||
| 240 | if (chars_avail == -1 && errno == EAGAIN)
| 0 | ||||||||||||
| 241 | return 0; never executed: return 0; | 0 | ||||||||||||
| 242 | if (chars_avail == -1 && errno == EIO)
| 0 | ||||||||||||
| 243 | return -1; never executed: return -1; | 0 | ||||||||||||
| 244 | if (chars_avail == 0) /* EOF */
| 0 | ||||||||||||
| 245 | { | - | ||||||||||||
| 246 | rl_stuff_char (EOF); | - | ||||||||||||
| 247 | return (0); never executed: return (0); | 0 | ||||||||||||
| 248 | } | - | ||||||||||||
| 249 | } never executed: end of block | 0 | ||||||||||||
| 250 | #endif /* O_NDELAY */ | - | ||||||||||||
| 251 | - | |||||||||||||
| 252 | #if defined (__MINGW32__) | - | ||||||||||||
| 253 | /* Use getch/_kbhit to check for available console input, in the same way | - | ||||||||||||
| 254 | that we read it normally. */ | - | ||||||||||||
| 255 | chars_avail = isatty (tty) ? _kbhit () : 0; | - | ||||||||||||
| 256 | result = 0; | - | ||||||||||||
| 257 | #endif | - | ||||||||||||
| 258 | - | |||||||||||||
| 259 | /* If there's nothing available, don't waste time trying to read | - | ||||||||||||
| 260 | something. */ | - | ||||||||||||
| 261 | if (chars_avail <= 0)
| 0 | ||||||||||||
| 262 | return 0; never executed: return 0; | 0 | ||||||||||||
| 263 | - | |||||||||||||
| 264 | tem = ibuffer_space (); | - | ||||||||||||
| 265 | - | |||||||||||||
| 266 | if (chars_avail > tem)
| 0 | ||||||||||||
| 267 | chars_avail = tem; never executed: chars_avail = tem; | 0 | ||||||||||||
| 268 | - | |||||||||||||
| 269 | /* One cannot read all of the available input. I can only read a single | - | ||||||||||||
| 270 | character at a time, or else programs which require input can be | - | ||||||||||||
| 271 | thwarted. If the buffer is larger than one character, I lose. | - | ||||||||||||
| 272 | Damn! */ | - | ||||||||||||
| 273 | if (tem < ibuffer_len)
| 0 | ||||||||||||
| 274 | chars_avail = 0; never executed: chars_avail = 0; | 0 | ||||||||||||
| 275 | - | |||||||||||||
| 276 | if (result != -1)
| 0 | ||||||||||||
| 277 | { | - | ||||||||||||
| 278 | while (chars_avail--)
| 0 | ||||||||||||
| 279 | { | - | ||||||||||||
| 280 | RL_CHECK_SIGNALS (); never executed: _rl_signal_handler (_rl_caught_signal);
| 0 | ||||||||||||
| 281 | k = (*rl_getc_function) (rl_instream); | - | ||||||||||||
| 282 | if (rl_stuff_char (k) == 0)
| 0 | ||||||||||||
| 283 | break; /* some problem; no more room */ never executed: break; | 0 | ||||||||||||
| 284 | if (k == NEWLINE || k == RETURN)
| 0 | ||||||||||||
| 285 | break; never executed: break; | 0 | ||||||||||||
| 286 | } never executed: end of block | 0 | ||||||||||||
| 287 | } never executed: end of block | 0 | ||||||||||||
| 288 | else | - | ||||||||||||
| 289 | { | - | ||||||||||||
| 290 | if (chars_avail)
| 0 | ||||||||||||
| 291 | rl_stuff_char (input); never executed: rl_stuff_char (input); | 0 | ||||||||||||
| 292 | } never executed: end of block | 0 | ||||||||||||
| 293 | - | |||||||||||||
| 294 | return 1; never executed: return 1; | 0 | ||||||||||||
| 295 | } | - | ||||||||||||
| 296 | - | |||||||||||||
| 297 | int | - | ||||||||||||
| 298 | rl_set_keyboard_input_timeout (int u) | - | ||||||||||||
| 299 | { | - | ||||||||||||
| 300 | int o; | - | ||||||||||||
| 301 | - | |||||||||||||
| 302 | o = _keyboard_input_timeout; | - | ||||||||||||
| 303 | if (u >= 0)
| 0 | ||||||||||||
| 304 | _keyboard_input_timeout = u; never executed: _keyboard_input_timeout = u; | 0 | ||||||||||||
| 305 | return (o); never executed: return (o); | 0 | ||||||||||||
| 306 | } | - | ||||||||||||
| 307 | - | |||||||||||||
| 308 | /* Is there input available to be read on the readline input file | - | ||||||||||||
| 309 | descriptor? Only works if the system has select(2) or FIONREAD. | - | ||||||||||||
| 310 | Uses the value of _keyboard_input_timeout as the timeout; if another | - | ||||||||||||
| 311 | readline function wants to specify a timeout and not leave it up to | - | ||||||||||||
| 312 | the user, it should use _rl_input_queued(timeout_value_in_microseconds) | - | ||||||||||||
| 313 | instead. */ | - | ||||||||||||
| 314 | int | - | ||||||||||||
| 315 | _rl_input_available (void) | - | ||||||||||||
| 316 | { | - | ||||||||||||
| 317 | #if defined(HAVE_SELECT) | - | ||||||||||||
| 318 | fd_set readfds, exceptfds; | - | ||||||||||||
| 319 | struct timeval timeout; | - | ||||||||||||
| 320 | #endif | - | ||||||||||||
| 321 | #if !defined (HAVE_SELECT) && defined(FIONREAD) | - | ||||||||||||
| 322 | int chars_avail; | - | ||||||||||||
| 323 | #endif | - | ||||||||||||
| 324 | int tty; | - | ||||||||||||
| 325 | - | |||||||||||||
| 326 | if (rl_input_available_hook)
| 0 | ||||||||||||
| 327 | return (*rl_input_available_hook) (); never executed: return (*rl_input_available_hook) (); | 0 | ||||||||||||
| 328 | - | |||||||||||||
| 329 | tty = fileno (rl_instream); | - | ||||||||||||
| 330 | - | |||||||||||||
| 331 | #if defined (HAVE_SELECT) | - | ||||||||||||
| 332 | FD_ZERO (&readfds); | - | ||||||||||||
| 333 | FD_ZERO (&exceptfds); | - | ||||||||||||
| 334 | FD_SET (tty, &readfds); | - | ||||||||||||
| 335 | FD_SET (tty, &exceptfds); | - | ||||||||||||
| 336 | timeout.tv_sec = 0; | - | ||||||||||||
| 337 | timeout.tv_usec = _keyboard_input_timeout; | - | ||||||||||||
| 338 | return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0); never executed: return (select (tty + 1, &readfds, (fd_set *) ((void *)0) , &exceptfds, &timeout) > 0); | 0 | ||||||||||||
| 339 | #else | - | ||||||||||||
| 340 | - | |||||||||||||
| 341 | #if defined (FIONREAD) | - | ||||||||||||
| 342 | if (ioctl (tty, FIONREAD, &chars_avail) == 0) | - | ||||||||||||
| 343 | return (chars_avail); | - | ||||||||||||
| 344 | #endif | - | ||||||||||||
| 345 | - | |||||||||||||
| 346 | #endif | - | ||||||||||||
| 347 | - | |||||||||||||
| 348 | #if defined (__MINGW32__) | - | ||||||||||||
| 349 | if (isatty (tty)) | - | ||||||||||||
| 350 | return (_kbhit ()); | - | ||||||||||||
| 351 | #endif | - | ||||||||||||
| 352 | - | |||||||||||||
| 353 | return 0; dead code: return 0; | - | ||||||||||||
| 354 | } | - | ||||||||||||
| 355 | - | |||||||||||||
| 356 | int | - | ||||||||||||
| 357 | _rl_input_queued (int t) | - | ||||||||||||
| 358 | { | - | ||||||||||||
| 359 | int old_timeout, r; | - | ||||||||||||
| 360 | - | |||||||||||||
| 361 | old_timeout = rl_set_keyboard_input_timeout (t); | - | ||||||||||||
| 362 | r = _rl_input_available (); | - | ||||||||||||
| 363 | rl_set_keyboard_input_timeout (old_timeout); | - | ||||||||||||
| 364 | return r; never executed: return r; | 0 | ||||||||||||
| 365 | } | - | ||||||||||||
| 366 | - | |||||||||||||
| 367 | void | - | ||||||||||||
| 368 | _rl_insert_typein (int c) | - | ||||||||||||
| 369 | { | - | ||||||||||||
| 370 | int key, t, i; | - | ||||||||||||
| 371 | char *string; | - | ||||||||||||
| 372 | - | |||||||||||||
| 373 | i = key = 0; | - | ||||||||||||
| 374 | string = (char *)xmalloc (ibuffer_len + 1); | - | ||||||||||||
| 375 | string[i++] = (char) c; | - | ||||||||||||
| 376 | - | |||||||||||||
| 377 | while ((t = rl_get_char (&key)) &&
| 0 | ||||||||||||
| 378 | _rl_keymap[key].type == ISFUNC &&
| 0 | ||||||||||||
| 379 | _rl_keymap[key].function == rl_insert)
| 0 | ||||||||||||
| 380 | string[i++] = key; never executed: string[i++] = key; | 0 | ||||||||||||
| 381 | - | |||||||||||||
| 382 | if (t)
| 0 | ||||||||||||
| 383 | _rl_unget_char (key); never executed: _rl_unget_char (key); | 0 | ||||||||||||
| 384 | - | |||||||||||||
| 385 | string[i] = '\0'; | - | ||||||||||||
| 386 | rl_insert_text (string); | - | ||||||||||||
| 387 | xfree (string); | - | ||||||||||||
| 388 | } never executed: end of block | 0 | ||||||||||||
| 389 | - | |||||||||||||
| 390 | /* Add KEY to the buffer of characters to be read. Returns 1 if the | - | ||||||||||||
| 391 | character was stuffed correctly; 0 otherwise. */ | - | ||||||||||||
| 392 | int | - | ||||||||||||
| 393 | rl_stuff_char (int key) | - | ||||||||||||
| 394 | { | - | ||||||||||||
| 395 | if (ibuffer_space () == 0)
| 0 | ||||||||||||
| 396 | return 0; never executed: return 0; | 0 | ||||||||||||
| 397 | - | |||||||||||||
| 398 | if (key == EOF)
| 0 | ||||||||||||
| 399 | { | - | ||||||||||||
| 400 | key = NEWLINE; | - | ||||||||||||
| 401 | rl_pending_input = EOF; | - | ||||||||||||
| 402 | RL_SETSTATE (RL_STATE_INPUTPENDING); | - | ||||||||||||
| 403 | } never executed: end of block | 0 | ||||||||||||
| 404 | ibuffer[push_index++] = key; | - | ||||||||||||
| 405 | #if 0 | - | ||||||||||||
| 406 | if (push_index >= ibuffer_len) | - | ||||||||||||
| 407 | #else | - | ||||||||||||
| 408 | if (push_index > ibuffer_len)
| 0 | ||||||||||||
| 409 | #endif | - | ||||||||||||
| 410 | push_index = 0; never executed: push_index = 0; | 0 | ||||||||||||
| 411 | - | |||||||||||||
| 412 | return 1; never executed: return 1; | 0 | ||||||||||||
| 413 | } | - | ||||||||||||
| 414 | - | |||||||||||||
| 415 | /* Make C be the next command to be executed. */ | - | ||||||||||||
| 416 | int | - | ||||||||||||
| 417 | rl_execute_next (int c) | - | ||||||||||||
| 418 | { | - | ||||||||||||
| 419 | rl_pending_input = c; | - | ||||||||||||
| 420 | RL_SETSTATE (RL_STATE_INPUTPENDING); | - | ||||||||||||
| 421 | return 0; never executed: return 0; | 0 | ||||||||||||
| 422 | } | - | ||||||||||||
| 423 | - | |||||||||||||
| 424 | /* Clear any pending input pushed with rl_execute_next() */ | - | ||||||||||||
| 425 | int | - | ||||||||||||
| 426 | rl_clear_pending_input (void) | - | ||||||||||||
| 427 | { | - | ||||||||||||
| 428 | rl_pending_input = 0; | - | ||||||||||||
| 429 | RL_UNSETSTATE (RL_STATE_INPUTPENDING); | - | ||||||||||||
| 430 | return 0; never executed: return 0; | 0 | ||||||||||||
| 431 | } | - | ||||||||||||
| 432 | - | |||||||||||||
| 433 | /* **************************************************************** */ | - | ||||||||||||
| 434 | /* */ | - | ||||||||||||
| 435 | /* Character Input */ | - | ||||||||||||
| 436 | /* */ | - | ||||||||||||
| 437 | /* **************************************************************** */ | - | ||||||||||||
| 438 | - | |||||||||||||
| 439 | /* Read a key, including pending input. */ | - | ||||||||||||
| 440 | int | - | ||||||||||||
| 441 | rl_read_key (void) | - | ||||||||||||
| 442 | { | - | ||||||||||||
| 443 | int c, r; | - | ||||||||||||
| 444 | - | |||||||||||||
| 445 | if (rl_pending_input)
| 0 | ||||||||||||
| 446 | { | - | ||||||||||||
| 447 | c = rl_pending_input; /* XXX - cast to unsigned char if > 0? */ | - | ||||||||||||
| 448 | rl_clear_pending_input (); | - | ||||||||||||
| 449 | } never executed: end of block | 0 | ||||||||||||
| 450 | else | - | ||||||||||||
| 451 | { | - | ||||||||||||
| 452 | /* If input is coming from a macro, then use that. */ | - | ||||||||||||
| 453 | if (c = _rl_next_macro_key ())
| 0 | ||||||||||||
| 454 | return ((unsigned char)c); never executed: return ((unsigned char)c); | 0 | ||||||||||||
| 455 | - | |||||||||||||
| 456 | /* If the user has an event function, then call it periodically. */ | - | ||||||||||||
| 457 | if (rl_event_hook)
| 0 | ||||||||||||
| 458 | { | - | ||||||||||||
| 459 | while (rl_event_hook)
| 0 | ||||||||||||
| 460 | { | - | ||||||||||||
| 461 | if (rl_get_char (&c) != 0)
| 0 | ||||||||||||
| 462 | break; never executed: break; | 0 | ||||||||||||
| 463 | - | |||||||||||||
| 464 | if ((r = rl_gather_tyi ()) < 0) /* XXX - EIO */
| 0 | ||||||||||||
| 465 | { | - | ||||||||||||
| 466 | rl_done = 1; | - | ||||||||||||
| 467 | return (errno == EIO ? (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF) : '\n'); never executed: return ( (*__errno_location ()) == 5 ? ((rl_readline_state & (0x0000008)) ? (-2) : (-1) ) : '\n'); | 0 | ||||||||||||
| 468 | } | - | ||||||||||||
| 469 | else if (r > 0) /* read something */
| 0 | ||||||||||||
| 470 | continue; never executed: continue; | 0 | ||||||||||||
| 471 | - | |||||||||||||
| 472 | RL_CHECK_SIGNALS (); never executed: _rl_signal_handler (_rl_caught_signal);
| 0 | ||||||||||||
| 473 | if (rl_done) /* XXX - experimental */
| 0 | ||||||||||||
| 474 | return ('\n'); never executed: return ('\n'); | 0 | ||||||||||||
| 475 | (*rl_event_hook) (); | - | ||||||||||||
| 476 | } never executed: end of block | 0 | ||||||||||||
| 477 | } never executed: end of block | 0 | ||||||||||||
| 478 | else | - | ||||||||||||
| 479 | { | - | ||||||||||||
| 480 | if (rl_get_char (&c) == 0)
| 0 | ||||||||||||
| 481 | c = (*rl_getc_function) (rl_instream); never executed: c = (*rl_getc_function) (rl_instream); | 0 | ||||||||||||
| 482 | /* fprintf(stderr, "rl_read_key: calling RL_CHECK_SIGNALS: _rl_caught_signal = %d", _rl_caught_signal); */ | - | ||||||||||||
| 483 | RL_CHECK_SIGNALS (); never executed: _rl_signal_handler (_rl_caught_signal);
| 0 | ||||||||||||
| 484 | } never executed: end of block | 0 | ||||||||||||
| 485 | } | - | ||||||||||||
| 486 | - | |||||||||||||
| 487 | return (c); never executed: return (c); | 0 | ||||||||||||
| 488 | } | - | ||||||||||||
| 489 | - | |||||||||||||
| 490 | int | - | ||||||||||||
| 491 | rl_getc (FILE *stream) | - | ||||||||||||
| 492 | { | - | ||||||||||||
| 493 | int result; | - | ||||||||||||
| 494 | unsigned char c; | - | ||||||||||||
| 495 | #if defined (HAVE_PSELECT) | - | ||||||||||||
| 496 | sigset_t empty_set; | - | ||||||||||||
| 497 | fd_set readfds; | - | ||||||||||||
| 498 | #endif | - | ||||||||||||
| 499 | - | |||||||||||||
| 500 | while (1) | - | ||||||||||||
| 501 | { | - | ||||||||||||
| 502 | RL_CHECK_SIGNALS (); never executed: _rl_signal_handler (_rl_caught_signal);
| 0 | ||||||||||||
| 503 | - | |||||||||||||
| 504 | /* We know at this point that _rl_caught_signal == 0 */ | - | ||||||||||||
| 505 | - | |||||||||||||
| 506 | #if defined (__MINGW32__) | - | ||||||||||||
| 507 | if (isatty (fileno (stream))) | - | ||||||||||||
| 508 | return (_getch ()); /* "There is no error return." */ | - | ||||||||||||
| 509 | #endif | - | ||||||||||||
| 510 | result = 0; | - | ||||||||||||
| 511 | #if defined (HAVE_PSELECT) | - | ||||||||||||
| 512 | FD_ZERO (&readfds); | - | ||||||||||||
| 513 | FD_SET (fileno (stream), &readfds); | - | ||||||||||||
| 514 | # if defined (HANDLE_SIGNALS) | - | ||||||||||||
| 515 | result = pselect (fileno (stream) + 1, &readfds, NULL, NULL, NULL, &_rl_orig_sigset); | - | ||||||||||||
| 516 | # else | - | ||||||||||||
| 517 | sigemptyset (&empty_set); | - | ||||||||||||
| 518 | sigprocmask (SIG_BLOCK, (sigset_t *)NULL, &empty_set); | - | ||||||||||||
| 519 | result = pselect (fileno (stream) + 1, &readfds, NULL, NULL, NULL, &empty_set); | - | ||||||||||||
| 520 | # endif /* HANDLE_SIGNALS */ | - | ||||||||||||
| 521 | #endif | - | ||||||||||||
| 522 | if (result >= 0)
| 0 | ||||||||||||
| 523 | result = read (fileno (stream), &c, sizeof (unsigned char)); never executed: result = read (fileno (stream), &c, sizeof (unsigned char)); | 0 | ||||||||||||
| 524 | - | |||||||||||||
| 525 | if (result == sizeof (unsigned char))
| 0 | ||||||||||||
| 526 | return (c); never executed: return (c); | 0 | ||||||||||||
| 527 | - | |||||||||||||
| 528 | /* If zero characters are returned, then the file that we are | - | ||||||||||||
| 529 | reading from is empty! Return EOF in that case. */ | - | ||||||||||||
| 530 | if (result == 0)
| 0 | ||||||||||||
| 531 | return (EOF); never executed: return ( (-1) ); | 0 | ||||||||||||
| 532 | - | |||||||||||||
| 533 | #if defined (__BEOS__) | - | ||||||||||||
| 534 | if (errno == EINTR) | - | ||||||||||||
| 535 | continue; | - | ||||||||||||
| 536 | #endif | - | ||||||||||||
| 537 | - | |||||||||||||
| 538 | #if defined (EWOULDBLOCK) | - | ||||||||||||
| 539 | # define X_EWOULDBLOCK EWOULDBLOCK | - | ||||||||||||
| 540 | #else | - | ||||||||||||
| 541 | # define X_EWOULDBLOCK -99 | - | ||||||||||||
| 542 | #endif | - | ||||||||||||
| 543 | - | |||||||||||||
| 544 | #if defined (EAGAIN) | - | ||||||||||||
| 545 | # define X_EAGAIN EAGAIN | - | ||||||||||||
| 546 | #else | - | ||||||||||||
| 547 | # define X_EAGAIN -99 | - | ||||||||||||
| 548 | #endif | - | ||||||||||||
| 549 | - | |||||||||||||
| 550 | if (errno == X_EWOULDBLOCK || errno == X_EAGAIN)
| 0 | ||||||||||||
| 551 | { | - | ||||||||||||
| 552 | if (sh_unset_nodelay_mode (fileno (stream)) < 0)
| 0 | ||||||||||||
| 553 | return (EOF); never executed: return ( (-1) ); | 0 | ||||||||||||
| 554 | continue; never executed: continue; | 0 | ||||||||||||
| 555 | } | - | ||||||||||||
| 556 | - | |||||||||||||
| 557 | #undef X_EWOULDBLOCK | - | ||||||||||||
| 558 | #undef X_EAGAIN | - | ||||||||||||
| 559 | - | |||||||||||||
| 560 | /* fprintf(stderr, "rl_getc: result = %d errno = %d\n", result, errno); */ | - | ||||||||||||
| 561 | - | |||||||||||||
| 562 | handle_error: code before this statement never executed: handle_error: | 0 | ||||||||||||
| 563 | /* If the error that we received was EINTR, then try again, | - | ||||||||||||
| 564 | this is simply an interrupted system call to read (). We allow | - | ||||||||||||
| 565 | the read to be interrupted if we caught SIGHUP, SIGTERM, or any | - | ||||||||||||
| 566 | of the other signals readline treats specially. If the | - | ||||||||||||
| 567 | application sets an event hook, call it for other signals. | - | ||||||||||||
| 568 | Otherwise (not EINTR), some error occurred, also signifying EOF. */ | - | ||||||||||||
| 569 | if (errno != EINTR)
| 0 | ||||||||||||
| 570 | return (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF); never executed: return ((rl_readline_state & (0x0000008)) ? (-2) : (-1) ); | 0 | ||||||||||||
| 571 | /* fatal signals of interest */ | - | ||||||||||||
| 572 | #if defined (SIGHUP) | - | ||||||||||||
| 573 | else if (_rl_caught_signal == SIGHUP || _rl_caught_signal == SIGTERM)
| 0 | ||||||||||||
| 574 | #else | - | ||||||||||||
| 575 | else if (_rl_caught_signal == SIGTERM) | - | ||||||||||||
| 576 | #endif | - | ||||||||||||
| 577 | return (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF); never executed: return ((rl_readline_state & (0x0000008)) ? (-2) : (-1) ); | 0 | ||||||||||||
| 578 | /* keyboard-generated signals of interest */ | - | ||||||||||||
| 579 | #if defined (SIGQUIT) | - | ||||||||||||
| 580 | else if (_rl_caught_signal == SIGINT || _rl_caught_signal == SIGQUIT)
| 0 | ||||||||||||
| 581 | #else | - | ||||||||||||
| 582 | else if (_rl_caught_signal == SIGINT) | - | ||||||||||||
| 583 | #endif | - | ||||||||||||
| 584 | RL_CHECK_SIGNALS (); never executed: _rl_signal_handler (_rl_caught_signal);never executed: end of block
| 0 | ||||||||||||
| 585 | /* non-keyboard-generated signals of interest */ | - | ||||||||||||
| 586 | #if defined (SIGWINCH) | - | ||||||||||||
| 587 | else if (_rl_caught_signal == SIGWINCH)
| 0 | ||||||||||||
| 588 | RL_CHECK_SIGNALS (); never executed: _rl_signal_handler (_rl_caught_signal);never executed: end of block
| 0 | ||||||||||||
| 589 | #endif /* SIGWINCH */ | - | ||||||||||||
| 590 | #if defined (SIGALRM) | - | ||||||||||||
| 591 | else if (_rl_caught_signal == SIGALRM
| 0 | ||||||||||||
| 592 | # if defined (SIGVTALRM) | - | ||||||||||||
| 593 | || _rl_caught_signal == SIGVTALRM
| 0 | ||||||||||||
| 594 | # endif | - | ||||||||||||
| 595 | ) | - | ||||||||||||
| 596 | RL_CHECK_SIGNALS (); never executed: _rl_signal_handler (_rl_caught_signal);never executed: end of block
| 0 | ||||||||||||
| 597 | #endif /* SIGALRM */ | - | ||||||||||||
| 598 | - | |||||||||||||
| 599 | if (rl_signal_event_hook)
| 0 | ||||||||||||
| 600 | (*rl_signal_event_hook) (); never executed: (*rl_signal_event_hook) (); | 0 | ||||||||||||
| 601 | } never executed: end of block | 0 | ||||||||||||
| 602 | } never executed: end of block | 0 | ||||||||||||
| 603 | - | |||||||||||||
| 604 | #if defined (HANDLE_MULTIBYTE) | - | ||||||||||||
| 605 | /* read multibyte char */ | - | ||||||||||||
| 606 | int | - | ||||||||||||
| 607 | _rl_read_mbchar (char *mbchar, int size) | - | ||||||||||||
| 608 | { | - | ||||||||||||
| 609 | int mb_len, c; | - | ||||||||||||
| 610 | size_t mbchar_bytes_length; | - | ||||||||||||
| 611 | wchar_t wc; | - | ||||||||||||
| 612 | mbstate_t ps, ps_back; | - | ||||||||||||
| 613 | - | |||||||||||||
| 614 | memset(&ps, 0, sizeof (mbstate_t)); | - | ||||||||||||
| 615 | memset(&ps_back, 0, sizeof (mbstate_t)); | - | ||||||||||||
| 616 | - | |||||||||||||
| 617 | mb_len = 0; | - | ||||||||||||
| 618 | while (mb_len < size)
| 0 | ||||||||||||
| 619 | { | - | ||||||||||||
| 620 | RL_SETSTATE(RL_STATE_MOREINPUT); | - | ||||||||||||
| 621 | c = rl_read_key (); | - | ||||||||||||
| 622 | RL_UNSETSTATE(RL_STATE_MOREINPUT); | - | ||||||||||||
| 623 | - | |||||||||||||
| 624 | if (c < 0)
| 0 | ||||||||||||
| 625 | break; never executed: break; | 0 | ||||||||||||
| 626 | - | |||||||||||||
| 627 | mbchar[mb_len++] = c; | - | ||||||||||||
| 628 | - | |||||||||||||
| 629 | mbchar_bytes_length = mbrtowc (&wc, mbchar, mb_len, &ps); | - | ||||||||||||
| 630 | if (mbchar_bytes_length == (size_t)(-1))
| 0 | ||||||||||||
| 631 | break; /* invalid byte sequence for the current locale */ never executed: break; | 0 | ||||||||||||
| 632 | else if (mbchar_bytes_length == (size_t)(-2))
| 0 | ||||||||||||
| 633 | { | - | ||||||||||||
| 634 | /* shorted bytes */ | - | ||||||||||||
| 635 | ps = ps_back; | - | ||||||||||||
| 636 | continue; never executed: continue; | 0 | ||||||||||||
| 637 | } | - | ||||||||||||
| 638 | else if (mbchar_bytes_length == 0)
| 0 | ||||||||||||
| 639 | { | - | ||||||||||||
| 640 | mbchar[0] = '\0'; /* null wide character */ | - | ||||||||||||
| 641 | mb_len = 1; | - | ||||||||||||
| 642 | break; never executed: break; | 0 | ||||||||||||
| 643 | } | - | ||||||||||||
| 644 | else if (mbchar_bytes_length > (size_t)(0))
| 0 | ||||||||||||
| 645 | break; never executed: break; | 0 | ||||||||||||
| 646 | } never executed: end of block | 0 | ||||||||||||
| 647 | - | |||||||||||||
| 648 | return mb_len; never executed: return mb_len; | 0 | ||||||||||||
| 649 | } | - | ||||||||||||
| 650 | - | |||||||||||||
| 651 | /* Read a multibyte-character string whose first character is FIRST into | - | ||||||||||||
| 652 | the buffer MB of length MLEN. Returns the last character read, which | - | ||||||||||||
| 653 | may be FIRST. Used by the search functions, among others. Very similar | - | ||||||||||||
| 654 | to _rl_read_mbchar. */ | - | ||||||||||||
| 655 | int | - | ||||||||||||
| 656 | _rl_read_mbstring (int first, char *mb, int mlen) | - | ||||||||||||
| 657 | { | - | ||||||||||||
| 658 | int i, c, n; | - | ||||||||||||
| 659 | mbstate_t ps; | - | ||||||||||||
| 660 | - | |||||||||||||
| 661 | c = first; | - | ||||||||||||
| 662 | memset (mb, 0, mlen); | - | ||||||||||||
| 663 | for (i = 0; c >= 0 && i < mlen; i++)
| 0 | ||||||||||||
| 664 | { | - | ||||||||||||
| 665 | mb[i] = (char)c; | - | ||||||||||||
| 666 | memset (&ps, 0, sizeof (mbstate_t)); | - | ||||||||||||
| 667 | n = _rl_get_char_len (mb, &ps); | - | ||||||||||||
| 668 | if (n == -2)
| 0 | ||||||||||||
| 669 | { | - | ||||||||||||
| 670 | /* Read more for multibyte character */ | - | ||||||||||||
| 671 | RL_SETSTATE (RL_STATE_MOREINPUT); | - | ||||||||||||
| 672 | c = rl_read_key (); | - | ||||||||||||
| 673 | RL_UNSETSTATE (RL_STATE_MOREINPUT); | - | ||||||||||||
| 674 | } never executed: end of block | 0 | ||||||||||||
| 675 | else | - | ||||||||||||
| 676 | break; never executed: break; | 0 | ||||||||||||
| 677 | } | - | ||||||||||||
| 678 | return c; never executed: return c; | 0 | ||||||||||||
| 679 | } | - | ||||||||||||
| 680 | #endif /* HANDLE_MULTIBYTE */ | - | ||||||||||||
| Source code | Switch to Preprocessed file |