]>
git.ipfire.org Git - thirdparty/bash.git/blob - lib/readline/input.c
1 /* input.c -- character input functions for readline. */
3 /* Copyright (C) 1994-2011 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 rl_getc_func_t
*rl_getc_function
= rl_getc
;
85 static int _keyboard_input_timeout
= 100000; /* 0.1 seconds; it's in usec */
87 static int ibuffer_space
PARAMS((void));
88 static int rl_get_char
PARAMS((int *));
89 static int rl_gather_tyi
PARAMS((void));
91 /* **************************************************************** */
93 /* Character Input Buffering */
95 /* **************************************************************** */
97 static int pop_index
, push_index
;
98 static unsigned char ibuffer
[512];
99 static int ibuffer_len
= sizeof (ibuffer
) - 1;
101 #define any_typein (push_index != pop_index)
109 /* Return the amount of space available in the buffer for stuffing
114 if (pop_index
> push_index
)
115 return (pop_index
- push_index
- 1);
117 return (ibuffer_len
- (push_index
- pop_index
));
120 /* Get a key from the buffer of characters to be read.
121 Return the key in KEY.
122 Result is non-zero if there was a key, or 0 if there wasn't. */
127 if (push_index
== pop_index
)
130 *key
= ibuffer
[pop_index
++];
132 if (pop_index
>= ibuffer_len
)
134 if (pop_index
> ibuffer_len
)
141 /* Stuff KEY into the *front* of the input buffer.
142 Returns non-zero if successful, zero if there is
143 no space left in the buffer. */
148 if (ibuffer_space ())
152 pop_index
= ibuffer_len
;
153 ibuffer
[pop_index
] = key
;
160 _rl_pushed_input_available ()
162 return (push_index
!= pop_index
);
165 /* If a character is available to be read, then read it and stuff it into
166 IBUFFER. Otherwise, just return. Returns number of characters read
167 (0 if none available) and -1 on error (EIO). */
172 register int tem
, result
;
175 #if defined(HAVE_SELECT)
176 fd_set readfds
, exceptfds
;
177 struct timeval timeout
;
181 tty
= fileno (rl_instream
);
183 #if defined (HAVE_SELECT)
185 FD_ZERO (&exceptfds
);
186 FD_SET (tty
, &readfds
);
187 FD_SET (tty
, &exceptfds
);
188 USEC_TO_TIMEVAL (_keyboard_input_timeout
, timeout
);
189 result
= select (tty
+ 1, &readfds
, (fd_set
*)NULL
, &exceptfds
, &timeout
);
191 return 0; /* Nothing to read. */
195 #if defined (FIONREAD)
197 result
= ioctl (tty
, FIONREAD
, &chars_avail
);
198 if (result
== -1 && errno
== EIO
)
202 #if defined (O_NDELAY)
205 tem
= fcntl (tty
, F_GETFL
, 0);
207 fcntl (tty
, F_SETFL
, (tem
| O_NDELAY
));
208 chars_avail
= read (tty
, &input
, 1);
210 fcntl (tty
, F_SETFL
, tem
);
211 if (chars_avail
== -1 && errno
== EAGAIN
)
213 if (chars_avail
== 0) /* EOF */
219 #endif /* O_NDELAY */
221 #if defined (__MINGW32__)
222 /* Use getch/_kbhit to check for available console input, in the same way
223 that we read it normally. */
224 chars_avail
= isatty (tty
) ? _kbhit () : 0;
228 /* If there's nothing available, don't waste time trying to read
230 if (chars_avail
<= 0)
233 tem
= ibuffer_space ();
235 if (chars_avail
> tem
)
238 /* One cannot read all of the available input. I can only read a single
239 character at a time, or else programs which require input can be
240 thwarted. If the buffer is larger than one character, I lose.
242 if (tem
< ibuffer_len
)
247 while (chars_avail
--)
250 k
= (*rl_getc_function
) (rl_instream
);
251 if (rl_stuff_char (k
) == 0)
252 break; /* some problem; no more room */
253 if (k
== NEWLINE
|| k
== RETURN
)
260 rl_stuff_char (input
);
267 rl_set_keyboard_input_timeout (u
)
272 o
= _keyboard_input_timeout
;
274 _keyboard_input_timeout
= u
;
278 /* Is there input available to be read on the readline input file
279 descriptor? Only works if the system has select(2) or FIONREAD.
280 Uses the value of _keyboard_input_timeout as the timeout; if another
281 readline function wants to specify a timeout and not leave it up to
282 the user, it should use _rl_input_queued(timeout_value_in_microseconds)
285 _rl_input_available ()
287 #if defined(HAVE_SELECT)
288 fd_set readfds
, exceptfds
;
289 struct timeval timeout
;
291 #if !defined (HAVE_SELECT) && defined(FIONREAD)
296 tty
= fileno (rl_instream
);
298 #if defined (HAVE_SELECT)
300 FD_ZERO (&exceptfds
);
301 FD_SET (tty
, &readfds
);
302 FD_SET (tty
, &exceptfds
);
304 timeout
.tv_usec
= _keyboard_input_timeout
;
305 return (select (tty
+ 1, &readfds
, (fd_set
*)NULL
, &exceptfds
, &timeout
) > 0);
308 #if defined (FIONREAD)
309 if (ioctl (tty
, FIONREAD
, &chars_avail
) == 0)
310 return (chars_avail
);
315 #if defined (__MINGW32__)
329 old_timeout
= rl_set_keyboard_input_timeout (t
);
330 r
= _rl_input_available ();
331 rl_set_keyboard_input_timeout (old_timeout
);
336 _rl_insert_typein (c
)
343 string
= (char *)xmalloc (ibuffer_len
+ 1);
344 string
[i
++] = (char) c
;
346 while ((t
= rl_get_char (&key
)) &&
347 _rl_keymap
[key
].type
== ISFUNC
&&
348 _rl_keymap
[key
].function
== rl_insert
)
352 _rl_unget_char (key
);
355 rl_insert_text (string
);
359 /* Add KEY to the buffer of characters to be read. Returns 1 if the
360 character was stuffed correctly; 0 otherwise. */
365 if (ibuffer_space () == 0)
371 rl_pending_input
= EOF
;
372 RL_SETSTATE (RL_STATE_INPUTPENDING
);
374 ibuffer
[push_index
++] = key
;
376 if (push_index
>= ibuffer_len
)
378 if (push_index
> ibuffer_len
)
385 /* Make C be the next command to be executed. */
390 rl_pending_input
= c
;
391 RL_SETSTATE (RL_STATE_INPUTPENDING
);
395 /* Clear any pending input pushed with rl_execute_next() */
397 rl_clear_pending_input ()
399 rl_pending_input
= 0;
400 RL_UNSETSTATE (RL_STATE_INPUTPENDING
);
404 /* **************************************************************** */
406 /* Character Input */
408 /* **************************************************************** */
410 /* Read a key, including pending input. */
416 if (rl_pending_input
)
418 c
= rl_pending_input
;
419 rl_clear_pending_input ();
423 /* If input is coming from a macro, then use that. */
424 if (c
= _rl_next_macro_key ())
427 /* If the user has an event function, then call it periodically. */
430 while (rl_event_hook
)
432 if (rl_get_char (&c
) != 0)
435 if ((r
= rl_gather_tyi ()) < 0) /* XXX - EIO */
440 else if (r
> 0) /* read something */
444 if (rl_done
) /* XXX - experimental */
451 if (rl_get_char (&c
) == 0)
452 c
= (*rl_getc_function
) (rl_instream
);
453 /* fprintf(stderr, "rl_read_key: calling RL_CHECK_SIGNALS: _rl_caught_signal = %d", _rl_caught_signal); */
472 /* We know at this point that _rl_caught_signal == 0 */
474 #if defined (__MINGW32__)
475 if (isatty (fileno (stream
)))
478 result
= read (fileno (stream
), &c
, sizeof (unsigned char));
480 if (result
== sizeof (unsigned char))
483 /* If zero characters are returned, then the file that we are
484 reading from is empty! Return EOF in that case. */
488 #if defined (__BEOS__)
493 #if defined (EWOULDBLOCK)
494 # define X_EWOULDBLOCK EWOULDBLOCK
496 # define X_EWOULDBLOCK -99
500 # define X_EAGAIN EAGAIN
502 # define X_EAGAIN -99
505 if (errno
== X_EWOULDBLOCK
|| errno
== X_EAGAIN
)
507 if (sh_unset_nodelay_mode (fileno (stream
)) < 0)
515 /* If the error that we received was EINTR, then try again,
516 this is simply an interrupted system call to read ().
517 Otherwise, some error ocurred, also signifying EOF. */
519 return (RL_ISSTATE (RL_STATE_READCMD
) ? READERR
: EOF
);
520 else if (_rl_caught_signal
== SIGHUP
|| _rl_caught_signal
== SIGTERM
)
521 return (RL_ISSTATE (RL_STATE_READCMD
) ? READERR
: EOF
);
522 else if (rl_event_hook
)
527 #if defined (HANDLE_MULTIBYTE)
528 /* read multibyte char */
530 _rl_read_mbchar (mbchar
, size
)
535 size_t mbchar_bytes_length
;
537 mbstate_t ps
, ps_back
;
539 memset(&ps
, 0, sizeof (mbstate_t));
540 memset(&ps_back
, 0, sizeof (mbstate_t));
543 while (mb_len
< size
)
545 RL_SETSTATE(RL_STATE_MOREINPUT
);
547 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
552 mbchar
[mb_len
++] = c
;
554 mbchar_bytes_length
= mbrtowc (&wc
, mbchar
, mb_len
, &ps
);
555 if (mbchar_bytes_length
== (size_t)(-1))
556 break; /* invalid byte sequence for the current locale */
557 else if (mbchar_bytes_length
== (size_t)(-2))
563 else if (mbchar_bytes_length
== 0)
565 mbchar
[0] = '\0'; /* null wide character */
569 else if (mbchar_bytes_length
> (size_t)(0))
576 /* Read a multibyte-character string whose first character is FIRST into
577 the buffer MB of length MLEN. Returns the last character read, which
578 may be FIRST. Used by the search functions, among others. Very similar
579 to _rl_read_mbchar. */
581 _rl_read_mbstring (first
, mb
, mlen
)
590 memset (mb
, 0, mlen
);
591 for (i
= 0; c
>= 0 && i
< mlen
; i
++)
594 memset (&ps
, 0, sizeof (mbstate_t));
595 if (_rl_get_char_len (mb
, &ps
) == -2)
597 /* Read more for multibyte character */
598 RL_SETSTATE (RL_STATE_MOREINPUT
);
600 RL_UNSETSTATE (RL_STATE_MOREINPUT
);
607 #endif /* HANDLE_MULTIBYTE */