1 /* input.c -- character input functions for readline. */
3 /* Copyright (C) 1994-2017 Free Software Foundation, Inc.
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.
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.
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.
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/>.
22 #define READLINE_LIBRARY
24 #if defined (__TANDEM)
28 #if defined (HAVE_CONFIG_H)
32 #include <sys/types.h>
34 #if defined (HAVE_SYS_FILE_H)
35 # include <sys/file.h>
36 #endif /* HAVE_SYS_FILE_H */
38 #if defined (HAVE_UNISTD_H)
40 #endif /* HAVE_UNISTD_H */
42 #if defined (HAVE_STDLIB_H)
45 # include "ansi_stdlib.h"
46 #endif /* HAVE_STDLIB_H */
50 #include "posixselect.h"
52 #if defined (FIONREAD_IN_SYS_IOCTL)
53 # include <sys/ioctl.h>
63 /* System-specific feature definitions and include files. */
67 /* Some standard library routines. */
70 #include "rlprivate.h"
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 */
79 #if defined (HAVE_PSELECT)
80 extern sigset_t _rl_orig_sigset
;
83 /* Non-null means it is a pointer to a function to run while waiting for
85 rl_hook_func_t
*rl_event_hook
= (rl_hook_func_t
*)NULL
;
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
;
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
;
94 rl_getc_func_t
*rl_getc_function
= rl_getc
;
96 static int _keyboard_input_timeout
= 100000; /* 0.1 seconds; it's in usec */
98 static int ibuffer_space
PARAMS((void));
99 static int rl_get_char
PARAMS((int *));
100 static int rl_gather_tyi
PARAMS((void));
102 #if defined (_WIN32) && !defined (__CYGWIN__)
104 /* 'isatty' in the Windows runtime returns non-zero for every
105 character device, including the null device. Repair that. */
108 #define WIN32_LEAN_AND_MEAN 1
111 int w32_isatty (int fd
)
118 if ((h
= (HANDLE
) _get_osfhandle (fd
)) == INVALID_HANDLE_VALUE
)
123 if (GetConsoleMode (h
, &ignored
) != 0)
130 #define isatty(x) w32_isatty(x)
133 /* **************************************************************** */
135 /* Character Input Buffering */
137 /* **************************************************************** */
139 static int pop_index
, push_index
;
140 static unsigned char ibuffer
[512];
141 static int ibuffer_len
= sizeof (ibuffer
) - 1;
143 #define any_typein (push_index != pop_index)
146 _rl_any_typein (void)
152 _rl_pushed_input_available (void)
154 return (push_index
!= pop_index
);
157 /* Return the amount of space available in the buffer for stuffing
162 if (pop_index
> push_index
)
163 return (pop_index
- push_index
- 1);
165 return (ibuffer_len
- (push_index
- pop_index
));
168 /* Get a key from the buffer of characters to be read.
169 Return the key in KEY.
170 Result is non-zero if there was a key, or 0 if there wasn't. */
172 rl_get_char (int *key
)
174 if (push_index
== pop_index
)
177 *key
= ibuffer
[pop_index
++];
179 if (pop_index
>= ibuffer_len
)
181 if (pop_index
> ibuffer_len
)
188 /* Stuff KEY into the *front* of the input buffer.
189 Returns non-zero if successful, zero if there is
190 no space left in the buffer. */
192 _rl_unget_char (int key
)
194 if (ibuffer_space ())
198 pop_index
= ibuffer_len
;
199 ibuffer
[pop_index
] = key
;
205 /* If a character is available to be read, then read it and stuff it into
206 IBUFFER. Otherwise, just return. Returns number of characters read
207 (0 if none available) and -1 on error (EIO). */
212 register int tem
, result
;
215 #if defined(HAVE_SELECT)
216 fd_set readfds
, exceptfds
;
217 struct timeval timeout
;
222 tty
= fileno (rl_instream
);
224 #if defined (HAVE_SELECT)
226 FD_ZERO (&exceptfds
);
227 FD_SET (tty
, &readfds
);
228 FD_SET (tty
, &exceptfds
);
229 USEC_TO_TIMEVAL (_keyboard_input_timeout
, timeout
);
230 result
= select (tty
+ 1, &readfds
, (fd_set
*)NULL
, &exceptfds
, &timeout
);
232 return 0; /* Nothing to read. */
237 #if defined (FIONREAD)
238 result
= ioctl (tty
, FIONREAD
, &chars_avail
);
239 if (result
== -1 && errno
== EIO
)
245 #if defined (O_NDELAY)
248 tem
= fcntl (tty
, F_GETFL
, 0);
250 fcntl (tty
, F_SETFL
, (tem
| O_NDELAY
));
251 chars_avail
= read (tty
, &input
, 1);
253 fcntl (tty
, F_SETFL
, tem
);
254 if (chars_avail
== -1 && errno
== EAGAIN
)
256 if (chars_avail
== -1 && errno
== EIO
)
258 if (chars_avail
== 0) /* EOF */
264 #endif /* O_NDELAY */
266 #if defined (__MINGW32__)
267 /* Use getch/_kbhit to check for available console input, in the same way
268 that we read it normally. */
269 chars_avail
= isatty (tty
) ? _kbhit () : 0;
273 /* If there's nothing available, don't waste time trying to read
275 if (chars_avail
<= 0)
278 tem
= ibuffer_space ();
280 if (chars_avail
> tem
)
283 /* One cannot read all of the available input. I can only read a single
284 character at a time, or else programs which require input can be
285 thwarted. If the buffer is larger than one character, I lose.
287 if (tem
< ibuffer_len
)
292 while (chars_avail
--)
295 k
= (*rl_getc_function
) (rl_instream
);
296 if (rl_stuff_char (k
) == 0)
297 break; /* some problem; no more room */
298 if (k
== NEWLINE
|| k
== RETURN
)
305 rl_stuff_char (input
);
312 rl_set_keyboard_input_timeout (int u
)
316 o
= _keyboard_input_timeout
;
318 _keyboard_input_timeout
= u
;
322 /* Is there input available to be read on the readline input file
323 descriptor? Only works if the system has select(2) or FIONREAD.
324 Uses the value of _keyboard_input_timeout as the timeout; if another
325 readline function wants to specify a timeout and not leave it up to
326 the user, it should use _rl_input_queued(timeout_value_in_microseconds)
329 _rl_input_available (void)
331 #if defined(HAVE_SELECT)
332 fd_set readfds
, exceptfds
;
333 struct timeval timeout
;
335 #if !defined (HAVE_SELECT) && defined(FIONREAD)
340 if (rl_input_available_hook
)
341 return (*rl_input_available_hook
) ();
343 tty
= fileno (rl_instream
);
345 #if defined (HAVE_SELECT)
347 FD_ZERO (&exceptfds
);
348 FD_SET (tty
, &readfds
);
349 FD_SET (tty
, &exceptfds
);
351 timeout
.tv_usec
= _keyboard_input_timeout
;
352 return (select (tty
+ 1, &readfds
, (fd_set
*)NULL
, &exceptfds
, &timeout
) > 0);
355 #if defined (FIONREAD)
356 if (ioctl (tty
, FIONREAD
, &chars_avail
) == 0)
357 return (chars_avail
);
362 #if defined (__MINGW32__)
371 _rl_input_queued (int t
)
375 old_timeout
= rl_set_keyboard_input_timeout (t
);
376 r
= _rl_input_available ();
377 rl_set_keyboard_input_timeout (old_timeout
);
382 _rl_insert_typein (int c
)
388 string
= (char *)xmalloc (ibuffer_len
+ 1);
389 string
[i
++] = (char) c
;
391 while ((t
= rl_get_char (&key
)) &&
392 _rl_keymap
[key
].type
== ISFUNC
&&
393 _rl_keymap
[key
].function
== rl_insert
)
397 _rl_unget_char (key
);
400 rl_insert_text (string
);
404 /* Add KEY to the buffer of characters to be read. Returns 1 if the
405 character was stuffed correctly; 0 otherwise. */
407 rl_stuff_char (int key
)
409 if (ibuffer_space () == 0)
415 rl_pending_input
= EOF
;
416 RL_SETSTATE (RL_STATE_INPUTPENDING
);
418 ibuffer
[push_index
++] = key
;
420 if (push_index
>= ibuffer_len
)
422 if (push_index
> ibuffer_len
)
429 /* Make C be the next command to be executed. */
431 rl_execute_next (int c
)
433 rl_pending_input
= c
;
434 RL_SETSTATE (RL_STATE_INPUTPENDING
);
438 /* Clear any pending input pushed with rl_execute_next() */
440 rl_clear_pending_input (void)
442 rl_pending_input
= 0;
443 RL_UNSETSTATE (RL_STATE_INPUTPENDING
);
447 /* **************************************************************** */
449 /* Character Input */
451 /* **************************************************************** */
453 /* Read a key, including pending input. */
459 if (rl_pending_input
)
461 c
= rl_pending_input
; /* XXX - cast to unsigned char if > 0? */
462 rl_clear_pending_input ();
466 /* If input is coming from a macro, then use that. */
467 if (c
= _rl_next_macro_key ())
468 return ((unsigned char)c
);
470 /* If the user has an event function, then call it periodically. */
473 while (rl_event_hook
)
475 if (rl_get_char (&c
) != 0)
478 if ((r
= rl_gather_tyi ()) < 0) /* XXX - EIO */
481 return (errno
== EIO
? (RL_ISSTATE (RL_STATE_READCMD
) ? READERR
: EOF
) : '\n');
483 else if (r
> 0) /* read something */
487 if (rl_done
) /* XXX - experimental */
494 if (rl_get_char (&c
) == 0)
495 c
= (*rl_getc_function
) (rl_instream
);
496 /* fprintf(stderr, "rl_read_key: calling RL_CHECK_SIGNALS: _rl_caught_signal = %d", _rl_caught_signal); */
505 rl_getc (FILE *stream
)
509 #if defined (HAVE_PSELECT)
518 /* We know at this point that _rl_caught_signal == 0 */
520 #if defined (__MINGW32__)
521 if (isatty (fileno (stream
)))
522 return (_getch ()); /* "There is no error return." */
525 #if defined (HAVE_PSELECT)
527 FD_SET (fileno (stream
), &readfds
);
528 # if defined (HANDLE_SIGNALS)
529 result
= pselect (fileno (stream
) + 1, &readfds
, NULL
, NULL
, NULL
, &_rl_orig_sigset
);
531 sigemptyset (&empty_set
);
532 sigprocmask (SIG_BLOCK
, (sigset_t
*)NULL
, &empty_set
);
533 result
= pselect (fileno (stream
) + 1, &readfds
, NULL
, NULL
, NULL
, &empty_set
);
534 # endif /* HANDLE_SIGNALS */
537 result
= read (fileno (stream
), &c
, sizeof (unsigned char));
539 if (result
== sizeof (unsigned char))
542 /* If zero characters are returned, then the file that we are
543 reading from is empty! Return EOF in that case. */
547 #if defined (__BEOS__)
552 #if defined (EWOULDBLOCK)
553 # define X_EWOULDBLOCK EWOULDBLOCK
555 # define X_EWOULDBLOCK -99
559 # define X_EAGAIN EAGAIN
561 # define X_EAGAIN -99
564 if (errno
== X_EWOULDBLOCK
|| errno
== X_EAGAIN
)
566 if (sh_unset_nodelay_mode (fileno (stream
)) < 0)
574 /* fprintf(stderr, "rl_getc: result = %d errno = %d\n", result, errno); */
577 /* If the error that we received was EINTR, then try again,
578 this is simply an interrupted system call to read (). We allow
579 the read to be interrupted if we caught SIGHUP, SIGTERM, or any
580 of the other signals readline treats specially. If the
581 application sets an event hook, call it for other signals.
582 Otherwise (not EINTR), some error occurred, also signifying EOF. */
584 return (RL_ISSTATE (RL_STATE_READCMD
) ? READERR
: EOF
);
585 /* fatal signals of interest */
587 else if (_rl_caught_signal
== SIGHUP
|| _rl_caught_signal
== SIGTERM
)
589 else if (_rl_caught_signal
== SIGTERM
)
591 return (RL_ISSTATE (RL_STATE_READCMD
) ? READERR
: EOF
);
592 /* keyboard-generated signals of interest */
593 #if defined (SIGQUIT)
594 else if (_rl_caught_signal
== SIGINT
|| _rl_caught_signal
== SIGQUIT
)
596 else if (_rl_caught_signal
== SIGINT
)
599 /* non-keyboard-generated signals of interest */
600 #if defined (SIGWINCH)
601 else if (_rl_caught_signal
== SIGWINCH
)
603 #endif /* SIGWINCH */
604 #if defined (SIGALRM)
605 else if (_rl_caught_signal
== SIGALRM
606 # if defined (SIGVTALRM)
607 || _rl_caught_signal
== SIGVTALRM
613 if (rl_signal_event_hook
)
614 (*rl_signal_event_hook
) ();
618 #if defined (HANDLE_MULTIBYTE)
619 /* read multibyte char */
621 _rl_read_mbchar (char *mbchar
, int size
)
624 size_t mbchar_bytes_length
;
626 mbstate_t ps
, ps_back
;
628 memset(&ps
, 0, sizeof (mbstate_t));
629 memset(&ps_back
, 0, sizeof (mbstate_t));
632 while (mb_len
< size
)
634 RL_SETSTATE(RL_STATE_MOREINPUT
);
636 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
641 mbchar
[mb_len
++] = c
;
643 mbchar_bytes_length
= mbrtowc (&wc
, mbchar
, mb_len
, &ps
);
644 if (mbchar_bytes_length
== (size_t)(-1))
645 break; /* invalid byte sequence for the current locale */
646 else if (mbchar_bytes_length
== (size_t)(-2))
652 else if (mbchar_bytes_length
== 0)
654 mbchar
[0] = '\0'; /* null wide character */
658 else if (mbchar_bytes_length
> (size_t)(0))
665 /* Read a multibyte-character string whose first character is FIRST into
666 the buffer MB of length MLEN. Returns the last character read, which
667 may be FIRST. Used by the search functions, among others. Very similar
668 to _rl_read_mbchar. */
670 _rl_read_mbstring (int first
, char *mb
, int mlen
)
676 memset (mb
, 0, mlen
);
677 for (i
= 0; c
>= 0 && i
< mlen
; i
++)
680 memset (&ps
, 0, sizeof (mbstate_t));
681 n
= _rl_get_char_len (mb
, &ps
);
684 /* Read more for multibyte character */
685 RL_SETSTATE (RL_STATE_MOREINPUT
);
687 RL_UNSETSTATE (RL_STATE_MOREINPUT
);
694 #endif /* HANDLE_MULTIBYTE */
This page took 0.058152 seconds and 4 git commands to generate.