]>
git.ipfire.org Git - thirdparty/bash.git/blob - lib/readline/input.c
1 /* input.c -- character input functions for readline. */
3 /* Copyright (C) 1994-2013 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 /* Non-null means it is a pointer to a function to run while waiting for
81 rl_hook_func_t
*rl_event_hook
= (rl_hook_func_t
*)NULL
;
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
;
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
;
90 rl_getc_func_t
*rl_getc_function
= rl_getc
;
92 static int _keyboard_input_timeout
= 100000; /* 0.1 seconds; it's in usec */
94 static int ibuffer_space
PARAMS((void));
95 static int rl_get_char
PARAMS((int *));
96 static int rl_gather_tyi
PARAMS((void));
98 /* **************************************************************** */
100 /* Character Input Buffering */
102 /* **************************************************************** */
104 static int pop_index
, push_index
;
105 static unsigned char ibuffer
[512];
106 static int ibuffer_len
= sizeof (ibuffer
) - 1;
108 #define any_typein (push_index != pop_index)
117 _rl_pushed_input_available ()
119 return (push_index
!= pop_index
);
122 /* Return the amount of space available in the buffer for stuffing
127 if (pop_index
> push_index
)
128 return (pop_index
- push_index
- 1);
130 return (ibuffer_len
- (push_index
- pop_index
));
133 /* Get a key from the buffer of characters to be read.
134 Return the key in KEY.
135 Result is non-zero if there was a key, or 0 if there wasn't. */
140 if (push_index
== pop_index
)
143 *key
= ibuffer
[pop_index
++];
145 if (pop_index
>= ibuffer_len
)
147 if (pop_index
> ibuffer_len
)
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. */
161 if (ibuffer_space ())
165 pop_index
= ibuffer_len
;
166 ibuffer
[pop_index
] = key
;
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). */
179 register int tem
, result
;
182 #if defined(HAVE_SELECT)
183 fd_set readfds
, exceptfds
;
184 struct timeval timeout
;
189 tty
= fileno (rl_instream
);
191 #if defined (HAVE_SELECT)
193 FD_ZERO (&exceptfds
);
194 FD_SET (tty
, &readfds
);
195 FD_SET (tty
, &exceptfds
);
196 USEC_TO_TIMEVAL (_keyboard_input_timeout
, timeout
);
197 result
= select (tty
+ 1, &readfds
, (fd_set
*)NULL
, &exceptfds
, &timeout
);
199 return 0; /* Nothing to read. */
203 #if defined (FIONREAD)
205 result
= ioctl (tty
, FIONREAD
, &chars_avail
);
206 if (result
== -1 && errno
== EIO
)
212 #if defined (O_NDELAY)
215 tem
= fcntl (tty
, F_GETFL
, 0);
217 fcntl (tty
, F_SETFL
, (tem
| O_NDELAY
));
218 chars_avail
= read (tty
, &input
, 1);
220 fcntl (tty
, F_SETFL
, tem
);
221 if (chars_avail
== -1 && errno
== EAGAIN
)
223 if (chars_avail
== 0) /* EOF */
229 #endif /* O_NDELAY */
231 #if defined (__MINGW32__)
232 /* Use getch/_kbhit to check for available console input, in the same way
233 that we read it normally. */
234 chars_avail
= isatty (tty
) ? _kbhit () : 0;
238 /* If there's nothing available, don't waste time trying to read
240 if (chars_avail
<= 0)
243 tem
= ibuffer_space ();
245 if (chars_avail
> tem
)
248 /* One cannot read all of the available input. I can only read a single
249 character at a time, or else programs which require input can be
250 thwarted. If the buffer is larger than one character, I lose.
252 if (tem
< ibuffer_len
)
257 while (chars_avail
--)
260 k
= (*rl_getc_function
) (rl_instream
);
261 if (rl_stuff_char (k
) == 0)
262 break; /* some problem; no more room */
263 if (k
== NEWLINE
|| k
== RETURN
)
270 rl_stuff_char (input
);
277 rl_set_keyboard_input_timeout (u
)
282 o
= _keyboard_input_timeout
;
284 _keyboard_input_timeout
= u
;
288 /* Is there input available to be read on the readline input file
289 descriptor? Only works if the system has select(2) or FIONREAD.
290 Uses the value of _keyboard_input_timeout as the timeout; if another
291 readline function wants to specify a timeout and not leave it up to
292 the user, it should use _rl_input_queued(timeout_value_in_microseconds)
295 _rl_input_available ()
297 #if defined(HAVE_SELECT)
298 fd_set readfds
, exceptfds
;
299 struct timeval timeout
;
301 #if !defined (HAVE_SELECT) && defined(FIONREAD)
306 if (rl_input_available_hook
)
307 return (*rl_input_available_hook
) ();
309 tty
= fileno (rl_instream
);
311 #if defined (HAVE_SELECT)
313 FD_ZERO (&exceptfds
);
314 FD_SET (tty
, &readfds
);
315 FD_SET (tty
, &exceptfds
);
317 timeout
.tv_usec
= _keyboard_input_timeout
;
318 return (select (tty
+ 1, &readfds
, (fd_set
*)NULL
, &exceptfds
, &timeout
) > 0);
321 #if defined (FIONREAD)
322 if (ioctl (tty
, FIONREAD
, &chars_avail
) == 0)
323 return (chars_avail
);
328 #if defined (__MINGW32__)
342 old_timeout
= rl_set_keyboard_input_timeout (t
);
343 r
= _rl_input_available ();
344 rl_set_keyboard_input_timeout (old_timeout
);
349 _rl_insert_typein (c
)
356 string
= (char *)xmalloc (ibuffer_len
+ 1);
357 string
[i
++] = (char) c
;
359 while ((t
= rl_get_char (&key
)) &&
360 _rl_keymap
[key
].type
== ISFUNC
&&
361 _rl_keymap
[key
].function
== rl_insert
)
365 _rl_unget_char (key
);
368 rl_insert_text (string
);
372 /* Add KEY to the buffer of characters to be read. Returns 1 if the
373 character was stuffed correctly; 0 otherwise. */
378 if (ibuffer_space () == 0)
384 rl_pending_input
= EOF
;
385 RL_SETSTATE (RL_STATE_INPUTPENDING
);
387 ibuffer
[push_index
++] = key
;
389 if (push_index
>= ibuffer_len
)
391 if (push_index
> ibuffer_len
)
398 /* Make C be the next command to be executed. */
403 rl_pending_input
= c
;
404 RL_SETSTATE (RL_STATE_INPUTPENDING
);
408 /* Clear any pending input pushed with rl_execute_next() */
410 rl_clear_pending_input ()
412 rl_pending_input
= 0;
413 RL_UNSETSTATE (RL_STATE_INPUTPENDING
);
417 /* **************************************************************** */
419 /* Character Input */
421 /* **************************************************************** */
423 /* Read a key, including pending input. */
429 if (rl_pending_input
)
431 c
= rl_pending_input
;
432 rl_clear_pending_input ();
436 /* If input is coming from a macro, then use that. */
437 if (c
= _rl_next_macro_key ())
440 /* If the user has an event function, then call it periodically. */
443 while (rl_event_hook
)
445 if (rl_get_char (&c
) != 0)
448 if ((r
= rl_gather_tyi ()) < 0) /* XXX - EIO */
453 else if (r
> 0) /* read something */
457 if (rl_done
) /* XXX - experimental */
464 if (rl_get_char (&c
) == 0)
465 c
= (*rl_getc_function
) (rl_instream
);
466 /* fprintf(stderr, "rl_read_key: calling RL_CHECK_SIGNALS: _rl_caught_signal = %d", _rl_caught_signal); */
480 #if defined (HAVE_PSELECT)
489 /* We know at this point that _rl_caught_signal == 0 */
491 #if defined (__MINGW32__)
492 if (isatty (fileno (stream
)))
496 #if defined (HAVE_PSELECT)
497 sigemptyset (&empty_set
);
499 FD_SET (fileno (stream
), &readfds
);
500 result
= pselect (fileno (stream
) + 1, &readfds
, NULL
, NULL
, NULL
, &empty_set
);
502 if (result
< 0 && errno
== EINTR
)
507 result
= read (fileno (stream
), &c
, sizeof (unsigned char));
509 if (result
== sizeof (unsigned char))
512 /* If zero characters are returned, then the file that we are
513 reading from is empty! Return EOF in that case. */
517 #if defined (__BEOS__)
522 #if defined (EWOULDBLOCK)
523 # define X_EWOULDBLOCK EWOULDBLOCK
525 # define X_EWOULDBLOCK -99
529 # define X_EAGAIN EAGAIN
531 # define X_EAGAIN -99
534 if (errno
== X_EWOULDBLOCK
|| errno
== X_EAGAIN
)
536 if (sh_unset_nodelay_mode (fileno (stream
)) < 0)
544 /* fprintf(stderr, "rl_getc: result = %d errno = %d\n", result, errno); */
547 /* If the error that we received was EINTR, then try again,
548 this is simply an interrupted system call to read (). We allow
549 the read to be interrupted if we caught SIGHUP, SIGTERM, or any
550 of the other signals readline treats specially. If the
551 application sets an event hook, call it for other signals.
552 Otherwise (not EINTR), some error occurred, also signifying EOF. */
554 return (RL_ISSTATE (RL_STATE_READCMD
) ? READERR
: EOF
);
555 /* fatal signals of interest */
556 else if (_rl_caught_signal
== SIGHUP
|| _rl_caught_signal
== SIGTERM
)
557 return (RL_ISSTATE (RL_STATE_READCMD
) ? READERR
: EOF
);
558 /* keyboard-generated signals of interest */
559 else if (_rl_caught_signal
== SIGINT
|| _rl_caught_signal
== SIGQUIT
)
561 /* non-keyboard-generated signals of interest */
562 else if (_rl_caught_signal
== SIGWINCH
)
564 else if (_rl_caught_signal
== SIGALRM
565 #if defined (SIGVTALRM)
566 || _rl_caught_signal
== SIGVTALRM
571 if (rl_signal_event_hook
)
572 (*rl_signal_event_hook
) ();
576 #if defined (HANDLE_MULTIBYTE)
577 /* read multibyte char */
579 _rl_read_mbchar (mbchar
, size
)
584 size_t mbchar_bytes_length
;
586 mbstate_t ps
, ps_back
;
588 memset(&ps
, 0, sizeof (mbstate_t));
589 memset(&ps_back
, 0, sizeof (mbstate_t));
592 while (mb_len
< size
)
594 RL_SETSTATE(RL_STATE_MOREINPUT
);
596 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
601 mbchar
[mb_len
++] = c
;
603 mbchar_bytes_length
= mbrtowc (&wc
, mbchar
, mb_len
, &ps
);
604 if (mbchar_bytes_length
== (size_t)(-1))
605 break; /* invalid byte sequence for the current locale */
606 else if (mbchar_bytes_length
== (size_t)(-2))
612 else if (mbchar_bytes_length
== 0)
614 mbchar
[0] = '\0'; /* null wide character */
618 else if (mbchar_bytes_length
> (size_t)(0))
625 /* Read a multibyte-character string whose first character is FIRST into
626 the buffer MB of length MLEN. Returns the last character read, which
627 may be FIRST. Used by the search functions, among others. Very similar
628 to _rl_read_mbchar. */
630 _rl_read_mbstring (first
, mb
, mlen
)
639 memset (mb
, 0, mlen
);
640 for (i
= 0; c
>= 0 && i
< mlen
; i
++)
643 memset (&ps
, 0, sizeof (mbstate_t));
644 if (_rl_get_char_len (mb
, &ps
) == -2)
646 /* Read more for multibyte character */
647 RL_SETSTATE (RL_STATE_MOREINPUT
);
649 RL_UNSETSTATE (RL_STATE_MOREINPUT
);
656 #endif /* HANDLE_MULTIBYTE */