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