]>
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); */
485 /* We know at this point that _rl_caught_signal == 0 */
487 #if defined (__MINGW32__)
488 if (isatty (fileno (stream
)))
491 result
= read (fileno (stream
), &c
, sizeof (unsigned char));
493 if (result
== sizeof (unsigned char))
496 /* If zero characters are returned, then the file that we are
497 reading from is empty! Return EOF in that case. */
501 #if defined (__BEOS__)
506 #if defined (EWOULDBLOCK)
507 # define X_EWOULDBLOCK EWOULDBLOCK
509 # define X_EWOULDBLOCK -99
513 # define X_EAGAIN EAGAIN
515 # define X_EAGAIN -99
518 if (errno
== X_EWOULDBLOCK
|| errno
== X_EAGAIN
)
520 if (sh_unset_nodelay_mode (fileno (stream
)) < 0)
528 fprintf(stderr
, "rl_getc: result = %d errno = %d\n", result
, errno
);
530 /* If the error that we received was EINTR, then try again,
531 this is simply an interrupted system call to read (). We allow
532 the read to be interrupted if we caught SIGHUP or SIGTERM (but
533 not SIGINT; let the signal handler deal with that), but if the
534 application sets an event hook, call it for other signals.
535 Otherwise (not EINTR), some error occurred, also signifying EOF. */
537 return (RL_ISSTATE (RL_STATE_READCMD
) ? READERR
: EOF
);
538 else if (_rl_caught_signal
== SIGHUP
|| _rl_caught_signal
== SIGTERM
)
539 return (RL_ISSTATE (RL_STATE_READCMD
) ? READERR
: EOF
);
540 /* keyboard-generated signals of interest */
541 else if (_rl_caught_signal
== SIGINT
|| _rl_caught_signal
== SIGQUIT
)
543 /* non-keyboard-generated signals of interest */
544 else if (_rl_caught_signal
== SIGALRM
545 #if defined (SIGVTALRM)
546 || _rl_caught_signal
== SIGVTALRM
551 if (rl_signal_event_hook
)
552 (*rl_signal_event_hook
) ();
556 #if defined (HANDLE_MULTIBYTE)
557 /* read multibyte char */
559 _rl_read_mbchar (mbchar
, size
)
564 size_t mbchar_bytes_length
;
566 mbstate_t ps
, ps_back
;
568 memset(&ps
, 0, sizeof (mbstate_t));
569 memset(&ps_back
, 0, sizeof (mbstate_t));
572 while (mb_len
< size
)
574 RL_SETSTATE(RL_STATE_MOREINPUT
);
576 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
581 mbchar
[mb_len
++] = c
;
583 mbchar_bytes_length
= mbrtowc (&wc
, mbchar
, mb_len
, &ps
);
584 if (mbchar_bytes_length
== (size_t)(-1))
585 break; /* invalid byte sequence for the current locale */
586 else if (mbchar_bytes_length
== (size_t)(-2))
592 else if (mbchar_bytes_length
== 0)
594 mbchar
[0] = '\0'; /* null wide character */
598 else if (mbchar_bytes_length
> (size_t)(0))
605 /* Read a multibyte-character string whose first character is FIRST into
606 the buffer MB of length MLEN. Returns the last character read, which
607 may be FIRST. Used by the search functions, among others. Very similar
608 to _rl_read_mbchar. */
610 _rl_read_mbstring (first
, mb
, mlen
)
619 memset (mb
, 0, mlen
);
620 for (i
= 0; c
>= 0 && i
< mlen
; i
++)
623 memset (&ps
, 0, sizeof (mbstate_t));
624 if (_rl_get_char_len (mb
, &ps
) == -2)
626 /* Read more for multibyte character */
627 RL_SETSTATE (RL_STATE_MOREINPUT
);
629 RL_UNSETSTATE (RL_STATE_MOREINPUT
);
636 #endif /* HANDLE_MULTIBYTE */