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)
25 # define _XOPEN_SOURCE_EXTENDED 1
26 # define _TANDEM_SOURCE 1
30 #if defined (HAVE_CONFIG_H)
34 #include <sys/types.h>
36 #if defined (HAVE_SYS_FILE_H)
37 # include <sys/file.h>
38 #endif /* HAVE_SYS_FILE_H */
40 #if defined (HAVE_UNISTD_H)
42 #endif /* HAVE_UNISTD_H */
44 #if defined (HAVE_STDLIB_H)
47 # include "ansi_stdlib.h"
48 #endif /* HAVE_STDLIB_H */
52 #include "posixselect.h"
54 #if defined (FIONREAD_IN_SYS_IOCTL)
55 # include <sys/ioctl.h>
65 /* System-specific feature definitions and include files. */
69 /* Some standard library routines. */
72 #include "rlprivate.h"
76 /* What kind of non-blocking I/O do we have? */
77 #if !defined (O_NDELAY) && defined (O_NONBLOCK)
78 # define O_NDELAY O_NONBLOCK /* Posix style */
81 #if defined (HAVE_PSELECT)
82 extern sigset_t _rl_orig_sigset
;
85 /* Non-null means it is a pointer to a function to run while waiting for
87 rl_hook_func_t
*rl_event_hook
= (rl_hook_func_t
*)NULL
;
89 /* A function to call if a read(2) is interrupted by a signal. */
90 rl_hook_func_t
*rl_signal_event_hook
= (rl_hook_func_t
*)NULL
;
92 /* A function to replace _rl_input_available for applications using the
93 callback interface. */
94 rl_hook_func_t
*rl_input_available_hook
= (rl_hook_func_t
*)NULL
;
96 rl_getc_func_t
*rl_getc_function
= rl_getc
;
98 static int _keyboard_input_timeout
= 100000; /* 0.1 seconds; it's in usec */
100 static int ibuffer_space
PARAMS((void));
101 static int rl_get_char
PARAMS((int *));
102 static int rl_gather_tyi
PARAMS((void));
104 /* Windows isatty returns true for every character device, including the null
105 device, so we need to perform additional checks. */
106 #if defined (_WIN32) && !defined (__CYGWIN__)
109 #define WIN32_LEAN_AND_MEAN 1
113 win32_isatty (int fd
)
120 if ((h
= (HANDLE
) _get_osfhandle (fd
)) == INVALID_HANDLE_VALUE
)
125 if (GetConsoleMode (h
, &ignored
) != 0)
132 #define isatty(x) win32_isatty(x)
135 /* **************************************************************** */
137 /* Character Input Buffering */
139 /* **************************************************************** */
141 static int pop_index
, push_index
;
142 static unsigned char ibuffer
[512];
143 static int ibuffer_len
= sizeof (ibuffer
) - 1;
145 #define any_typein (push_index != pop_index)
148 _rl_any_typein (void)
154 _rl_pushed_input_available (void)
156 return (push_index
!= pop_index
);
159 /* Return the amount of space available in the buffer for stuffing
164 if (pop_index
> push_index
)
165 return (pop_index
- push_index
- 1);
167 return (ibuffer_len
- (push_index
- pop_index
));
170 /* Get a key from the buffer of characters to be read.
171 Return the key in KEY.
172 Result is non-zero if there was a key, or 0 if there wasn't. */
174 rl_get_char (int *key
)
176 if (push_index
== pop_index
)
179 *key
= ibuffer
[pop_index
++];
181 if (pop_index
>= ibuffer_len
)
183 if (pop_index
> ibuffer_len
)
190 /* Stuff KEY into the *front* of the input buffer.
191 Returns non-zero if successful, zero if there is
192 no space left in the buffer. */
194 _rl_unget_char (int key
)
196 if (ibuffer_space ())
200 pop_index
= ibuffer_len
;
201 ibuffer
[pop_index
] = key
;
207 /* If a character is available to be read, then read it and stuff it into
208 IBUFFER. Otherwise, just return. Returns number of characters read
209 (0 if none available) and -1 on error (EIO). */
214 register int tem
, result
;
217 #if defined(HAVE_SELECT)
218 fd_set readfds
, exceptfds
;
219 struct timeval timeout
;
224 tty
= fileno (rl_instream
);
226 #if defined (HAVE_SELECT)
228 FD_ZERO (&exceptfds
);
229 FD_SET (tty
, &readfds
);
230 FD_SET (tty
, &exceptfds
);
231 USEC_TO_TIMEVAL (_keyboard_input_timeout
, timeout
);
232 result
= select (tty
+ 1, &readfds
, (fd_set
*)NULL
, &exceptfds
, &timeout
);
234 return 0; /* Nothing to read. */
239 #if defined (FIONREAD)
240 result
= ioctl (tty
, FIONREAD
, &chars_avail
);
241 if (result
== -1 && errno
== EIO
)
247 #if defined (O_NDELAY)
250 tem
= fcntl (tty
, F_GETFL
, 0);
252 fcntl (tty
, F_SETFL
, (tem
| O_NDELAY
));
253 chars_avail
= read (tty
, &input
, 1);
255 fcntl (tty
, F_SETFL
, tem
);
256 if (chars_avail
== -1 && errno
== EAGAIN
)
258 if (chars_avail
== -1 && errno
== EIO
)
260 if (chars_avail
== 0) /* EOF */
266 #endif /* O_NDELAY */
268 #if defined (__MINGW32__)
269 /* Use getch/_kbhit to check for available console input, in the same way
270 that we read it normally. */
271 chars_avail
= isatty (tty
) ? _kbhit () : 0;
275 /* If there's nothing available, don't waste time trying to read
277 if (chars_avail
<= 0)
280 tem
= ibuffer_space ();
282 if (chars_avail
> tem
)
285 /* One cannot read all of the available input. I can only read a single
286 character at a time, or else programs which require input can be
287 thwarted. If the buffer is larger than one character, I lose.
289 if (tem
< ibuffer_len
)
294 while (chars_avail
--)
297 k
= (*rl_getc_function
) (rl_instream
);
298 if (rl_stuff_char (k
) == 0)
299 break; /* some problem; no more room */
300 if (k
== NEWLINE
|| k
== RETURN
)
307 rl_stuff_char (input
);
314 rl_set_keyboard_input_timeout (int u
)
318 o
= _keyboard_input_timeout
;
320 _keyboard_input_timeout
= u
;
324 /* Is there input available to be read on the readline input file
325 descriptor? Only works if the system has select(2) or FIONREAD.
326 Uses the value of _keyboard_input_timeout as the timeout; if another
327 readline function wants to specify a timeout and not leave it up to
328 the user, it should use _rl_input_queued(timeout_value_in_microseconds)
331 _rl_input_available (void)
333 #if defined(HAVE_SELECT)
334 fd_set readfds
, exceptfds
;
335 struct timeval timeout
;
337 #if !defined (HAVE_SELECT) && defined(FIONREAD)
342 if (rl_input_available_hook
)
343 return (*rl_input_available_hook
) ();
345 tty
= fileno (rl_instream
);
347 #if defined (HAVE_SELECT)
349 FD_ZERO (&exceptfds
);
350 FD_SET (tty
, &readfds
);
351 FD_SET (tty
, &exceptfds
);
352 USEC_TO_TIMEVAL (_keyboard_input_timeout
, timeout
);
353 return (select (tty
+ 1, &readfds
, (fd_set
*)NULL
, &exceptfds
, &timeout
) > 0);
356 #if defined (FIONREAD)
357 if (ioctl (tty
, FIONREAD
, &chars_avail
) == 0)
358 return (chars_avail
);
363 #if defined (__MINGW32__)
372 _rl_nchars_available ()
374 int chars_avail
, fd
, result
;
378 #if defined (FIONREAD)
379 fd
= fileno (rl_instream
);
381 result
= ioctl (fd
, FIONREAD
, &chars_avail
);
382 if (result
== -1 && errno
== EIO
)
390 _rl_input_queued (int t
)
394 old_timeout
= rl_set_keyboard_input_timeout (t
);
395 r
= _rl_input_available ();
396 rl_set_keyboard_input_timeout (old_timeout
);
401 _rl_insert_typein (int c
)
407 string
= (char *)xmalloc (ibuffer_len
+ 1);
408 string
[i
++] = (char) c
;
410 while ((t
= rl_get_char (&key
)) &&
411 _rl_keymap
[key
].type
== ISFUNC
&&
412 _rl_keymap
[key
].function
== rl_insert
)
416 _rl_unget_char (key
);
419 rl_insert_text (string
);
423 /* Add KEY to the buffer of characters to be read. Returns 1 if the
424 character was stuffed correctly; 0 otherwise. */
426 rl_stuff_char (int key
)
428 if (ibuffer_space () == 0)
434 rl_pending_input
= EOF
;
435 RL_SETSTATE (RL_STATE_INPUTPENDING
);
437 ibuffer
[push_index
++] = key
;
439 if (push_index
>= ibuffer_len
)
441 if (push_index
> ibuffer_len
)
448 /* Make C be the next command to be executed. */
450 rl_execute_next (int c
)
452 rl_pending_input
= c
;
453 RL_SETSTATE (RL_STATE_INPUTPENDING
);
457 /* Clear any pending input pushed with rl_execute_next() */
459 rl_clear_pending_input (void)
461 rl_pending_input
= 0;
462 RL_UNSETSTATE (RL_STATE_INPUTPENDING
);
466 /* **************************************************************** */
468 /* Character Input */
470 /* **************************************************************** */
472 /* Read a key, including pending input. */
478 if (rl_pending_input
)
480 c
= rl_pending_input
; /* XXX - cast to unsigned char if > 0? */
481 rl_clear_pending_input ();
485 /* If input is coming from a macro, then use that. */
486 if (c
= _rl_next_macro_key ())
487 return ((unsigned char)c
);
489 /* If the user has an event function, then call it periodically. */
492 while (rl_event_hook
)
494 if (rl_get_char (&c
) != 0)
497 if ((r
= rl_gather_tyi ()) < 0) /* XXX - EIO */
500 return (errno
== EIO
? (RL_ISSTATE (RL_STATE_READCMD
) ? READERR
: EOF
) : '\n');
502 else if (r
> 0) /* read something */
506 if (rl_done
) /* XXX - experimental */
513 if (rl_get_char (&c
) == 0)
514 c
= (*rl_getc_function
) (rl_instream
);
515 /* fprintf(stderr, "rl_read_key: calling RL_CHECK_SIGNALS: _rl_caught_signal = %d\r\n", _rl_caught_signal); */
524 rl_getc (FILE *stream
)
528 #if defined (HAVE_PSELECT)
537 /* We know at this point that _rl_caught_signal == 0 */
539 #if defined (__MINGW32__)
540 if (isatty (fileno (stream
)))
541 return (_getch ()); /* "There is no error return." */
544 #if defined (HAVE_PSELECT)
546 FD_SET (fileno (stream
), &readfds
);
547 # if defined (HANDLE_SIGNALS)
548 result
= pselect (fileno (stream
) + 1, &readfds
, NULL
, NULL
, NULL
, &_rl_orig_sigset
);
550 sigemptyset (&empty_set
);
551 sigprocmask (SIG_BLOCK
, (sigset_t
*)NULL
, &empty_set
);
552 result
= pselect (fileno (stream
) + 1, &readfds
, NULL
, NULL
, NULL
, &empty_set
);
553 # endif /* HANDLE_SIGNALS */
556 result
= read (fileno (stream
), &c
, sizeof (unsigned char));
558 if (result
== sizeof (unsigned char))
561 /* If zero characters are returned, then the file that we are
562 reading from is empty! Return EOF in that case. */
566 #if defined (__BEOS__)
571 #if defined (EWOULDBLOCK)
572 # define X_EWOULDBLOCK EWOULDBLOCK
574 # define X_EWOULDBLOCK -99
578 # define X_EAGAIN EAGAIN
580 # define X_EAGAIN -99
583 if (errno
== X_EWOULDBLOCK
|| errno
== X_EAGAIN
)
585 if (sh_unset_nodelay_mode (fileno (stream
)) < 0)
593 /* fprintf(stderr, "rl_getc: result = %d errno = %d\n", result, errno); */
596 /* If the error that we received was EINTR, then try again,
597 this is simply an interrupted system call to read (). We allow
598 the read to be interrupted if we caught SIGHUP, SIGTERM, or any
599 of the other signals readline treats specially. If the
600 application sets an event hook, call it for other signals.
601 Otherwise (not EINTR), some error occurred, also signifying EOF. */
603 return (RL_ISSTATE (RL_STATE_READCMD
) ? READERR
: EOF
);
604 /* fatal signals of interest */
606 else if (_rl_caught_signal
== SIGHUP
|| _rl_caught_signal
== SIGTERM
)
608 else if (_rl_caught_signal
== SIGTERM
)
610 return (RL_ISSTATE (RL_STATE_READCMD
) ? READERR
: EOF
);
611 /* keyboard-generated signals of interest */
612 #if defined (SIGQUIT)
613 else if (_rl_caught_signal
== SIGINT
|| _rl_caught_signal
== SIGQUIT
)
615 else if (_rl_caught_signal
== SIGINT
)
618 #if defined (SIGTSTP)
619 else if (_rl_caught_signal
== SIGTSTP
)
622 /* non-keyboard-generated signals of interest */
623 #if defined (SIGWINCH)
624 else if (_rl_caught_signal
== SIGWINCH
)
626 #endif /* SIGWINCH */
627 #if defined (SIGALRM)
628 else if (_rl_caught_signal
== SIGALRM
629 # if defined (SIGVTALRM)
630 || _rl_caught_signal
== SIGVTALRM
636 if (rl_signal_event_hook
)
637 (*rl_signal_event_hook
) ();
641 #if defined (HANDLE_MULTIBYTE)
642 /* read multibyte char */
644 _rl_read_mbchar (char *mbchar
, int size
)
647 size_t mbchar_bytes_length
;
649 mbstate_t ps
, ps_back
;
651 memset(&ps
, 0, sizeof (mbstate_t));
652 memset(&ps_back
, 0, sizeof (mbstate_t));
655 while (mb_len
< size
)
657 c
= (mb_len
== 0) ? _rl_bracketed_read_key () : rl_read_key ();
662 mbchar
[mb_len
++] = c
;
664 mbchar_bytes_length
= mbrtowc (&wc
, mbchar
, mb_len
, &ps
);
665 if (mbchar_bytes_length
== (size_t)(-1))
666 break; /* invalid byte sequence for the current locale */
667 else if (mbchar_bytes_length
== (size_t)(-2))
673 else if (mbchar_bytes_length
== 0)
675 mbchar
[0] = '\0'; /* null wide character */
679 else if (mbchar_bytes_length
> (size_t)(0))
686 /* Read a multibyte-character string whose first character is FIRST into
687 the buffer MB of length MLEN. Returns the last character read, which
688 may be FIRST. Used by the search functions, among others. Very similar
689 to _rl_read_mbchar. */
691 _rl_read_mbstring (int first
, char *mb
, int mlen
)
697 memset (mb
, 0, mlen
);
698 for (i
= 0; c
>= 0 && i
< mlen
; i
++)
701 memset (&ps
, 0, sizeof (mbstate_t));
702 n
= _rl_get_char_len (mb
, &ps
);
705 /* Read more for multibyte character */
706 RL_SETSTATE (RL_STATE_MOREINPUT
);
708 RL_UNSETSTATE (RL_STATE_MOREINPUT
);
715 #endif /* HANDLE_MULTIBYTE */