]>
git.ipfire.org Git - thirdparty/bash.git/blob - lib/readline/input.c
1 /* input.c -- character input functions for readline. */
3 /* Copyright (C) 1994-2005 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2, or
11 (at your option) any later version.
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
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 */
48 #if defined (HAVE_SELECT)
49 # if !defined (HAVE_SYS_SELECT_H) || !defined (M_UNIX)
50 # include <sys/time.h>
52 #endif /* HAVE_SELECT */
53 #if defined (HAVE_SYS_SELECT_H)
54 # include <sys/select.h>
57 #if defined (FIONREAD_IN_SYS_IOCTL)
58 # include <sys/ioctl.h>
68 /* System-specific feature definitions and include files. */
72 /* Some standard library routines. */
75 #include "rlprivate.h"
79 /* What kind of non-blocking I/O do we have? */
80 #if !defined (O_NDELAY) && defined (O_NONBLOCK)
81 # define O_NDELAY O_NONBLOCK /* Posix style */
84 /* Non-null means it is a pointer to a function to run while waiting for
86 rl_hook_func_t
*rl_event_hook
= (rl_hook_func_t
*)NULL
;
88 rl_getc_func_t
*rl_getc_function
= rl_getc
;
90 static int _keyboard_input_timeout
= 100000; /* 0.1 seconds; it's in usec */
92 static int ibuffer_space
PARAMS((void));
93 static int rl_get_char
PARAMS((int *));
94 static int rl_gather_tyi
PARAMS((void));
96 /* **************************************************************** */
98 /* Character Input Buffering */
100 /* **************************************************************** */
102 static int pop_index
, push_index
;
103 static unsigned char ibuffer
[512];
104 static int ibuffer_len
= sizeof (ibuffer
) - 1;
106 #define any_typein (push_index != pop_index)
114 /* Return the amount of space available in the buffer for stuffing
119 if (pop_index
> push_index
)
120 return (pop_index
- push_index
- 1);
122 return (ibuffer_len
- (push_index
- pop_index
));
125 /* Get a key from the buffer of characters to be read.
126 Return the key in KEY.
127 Result is KEY if there was a key, or 0 if there wasn't. */
132 if (push_index
== pop_index
)
135 *key
= ibuffer
[pop_index
++];
137 if (pop_index
>= ibuffer_len
)
139 if (pop_index
> ibuffer_len
)
146 /* Stuff KEY into the *front* of the input buffer.
147 Returns non-zero if successful, zero if there is
148 no space left in the buffer. */
153 if (ibuffer_space ())
157 pop_index
= ibuffer_len
;
158 ibuffer
[pop_index
] = key
;
165 _rl_pushed_input_available ()
167 return (push_index
!= pop_index
);
170 /* If a character is available to be read, then read it and stuff it into
171 IBUFFER. Otherwise, just return. Returns number of characters read
172 (0 if none available) and -1 on error (EIO). */
177 register int tem
, result
;
180 #if defined(HAVE_SELECT)
181 fd_set readfds
, exceptfds
;
182 struct timeval timeout
;
186 tty
= fileno (rl_instream
);
188 #if defined (HAVE_SELECT)
190 FD_ZERO (&exceptfds
);
191 FD_SET (tty
, &readfds
);
192 FD_SET (tty
, &exceptfds
);
194 timeout
.tv_usec
= _keyboard_input_timeout
;
195 result
= select (tty
+ 1, &readfds
, (fd_set
*)NULL
, &exceptfds
, &timeout
);
197 return 0; /* Nothing to read. */
201 #if defined (FIONREAD)
203 result
= ioctl (tty
, FIONREAD
, &chars_avail
);
204 if (result
== -1 && errno
== EIO
)
208 #if defined (O_NDELAY)
211 tem
= fcntl (tty
, F_GETFL
, 0);
213 fcntl (tty
, F_SETFL
, (tem
| O_NDELAY
));
214 chars_avail
= read (tty
, &input
, 1);
216 fcntl (tty
, F_SETFL
, tem
);
217 if (chars_avail
== -1 && errno
== EAGAIN
)
219 if (chars_avail
== 0) /* EOF */
225 #endif /* O_NDELAY */
227 #if defined (__MINGW32__)
228 /* Use getch/_kbhit to check for available console input, in the same way
229 that we read it normally. */
230 chars_avail
= isatty (tty
) ? _kbhit () : 0;
234 /* If there's nothing available, don't waste time trying to read
236 if (chars_avail
<= 0)
239 tem
= ibuffer_space ();
241 if (chars_avail
> tem
)
244 /* One cannot read all of the available input. I can only read a single
245 character at a time, or else programs which require input can be
246 thwarted. If the buffer is larger than one character, I lose.
248 if (tem
< ibuffer_len
)
253 while (chars_avail
--)
255 k
= (*rl_getc_function
) (rl_instream
);
256 if (rl_stuff_char (k
) == 0)
257 break; /* some problem; no more room */
258 if (k
== NEWLINE
|| k
== RETURN
)
265 rl_stuff_char (input
);
272 rl_set_keyboard_input_timeout (u
)
277 o
= _keyboard_input_timeout
;
279 _keyboard_input_timeout
= u
;
283 /* Is there input available to be read on the readline input file
284 descriptor? Only works if the system has select(2) or FIONREAD.
285 Uses the value of _keyboard_input_timeout as the timeout; if another
286 readline function wants to specify a timeout and not leave it up to
287 the user, it should use _rl_input_queued(timeout_value_in_microseconds)
290 _rl_input_available ()
292 #if defined(HAVE_SELECT)
293 fd_set readfds
, exceptfds
;
294 struct timeval timeout
;
296 #if !defined (HAVE_SELECT) && defined(FIONREAD)
301 tty
= fileno (rl_instream
);
303 #if defined (HAVE_SELECT)
305 FD_ZERO (&exceptfds
);
306 FD_SET (tty
, &readfds
);
307 FD_SET (tty
, &exceptfds
);
309 timeout
.tv_usec
= _keyboard_input_timeout
;
310 return (select (tty
+ 1, &readfds
, (fd_set
*)NULL
, &exceptfds
, &timeout
) > 0);
313 #if defined (FIONREAD)
314 if (ioctl (tty
, FIONREAD
, &chars_avail
) == 0)
315 return (chars_avail
);
320 #if defined (__MINGW32__)
334 old_timeout
= rl_set_keyboard_input_timeout (t
);
335 r
= _rl_input_available ();
336 rl_set_keyboard_input_timeout (old_timeout
);
341 _rl_insert_typein (c
)
348 string
= (char *)xmalloc (ibuffer_len
+ 1);
349 string
[i
++] = (char) c
;
351 while ((t
= rl_get_char (&key
)) &&
352 _rl_keymap
[key
].type
== ISFUNC
&&
353 _rl_keymap
[key
].function
== rl_insert
)
357 _rl_unget_char (key
);
360 rl_insert_text (string
);
364 /* Add KEY to the buffer of characters to be read. Returns 1 if the
365 character was stuffed correctly; 0 otherwise. */
370 if (ibuffer_space () == 0)
376 rl_pending_input
= EOF
;
377 RL_SETSTATE (RL_STATE_INPUTPENDING
);
379 ibuffer
[push_index
++] = key
;
381 if (push_index
>= ibuffer_len
)
383 if (push_index
> ibuffer_len
)
390 /* Make C be the next command to be executed. */
395 rl_pending_input
= c
;
396 RL_SETSTATE (RL_STATE_INPUTPENDING
);
400 /* Clear any pending input pushed with rl_execute_next() */
402 rl_clear_pending_input ()
404 rl_pending_input
= 0;
405 RL_UNSETSTATE (RL_STATE_INPUTPENDING
);
409 /* **************************************************************** */
411 /* Character Input */
413 /* **************************************************************** */
415 /* Read a key, including pending input. */
421 rl_key_sequence_length
++;
423 if (rl_pending_input
)
425 c
= rl_pending_input
;
426 rl_clear_pending_input ();
430 /* If input is coming from a macro, then use that. */
431 if (c
= _rl_next_macro_key ())
434 /* If the user has an event function, then call it periodically. */
437 while (rl_event_hook
&& rl_get_char (&c
) == 0)
440 if (rl_done
) /* XXX - experimental */
442 if (rl_gather_tyi () < 0) /* XXX - EIO */
451 if (rl_get_char (&c
) == 0)
452 c
= (*rl_getc_function
) (rl_instream
);
468 #if defined (__MINGW32__)
469 if (isatty (fileno (stream
)))
472 result
= read (fileno (stream
), &c
, sizeof (unsigned char));
474 if (result
== sizeof (unsigned char))
477 /* If zero characters are returned, then the file that we are
478 reading from is empty! Return EOF in that case. */
482 #if defined (__BEOS__)
487 #if defined (EWOULDBLOCK)
488 # define X_EWOULDBLOCK EWOULDBLOCK
490 # define X_EWOULDBLOCK -99
494 # define X_EAGAIN EAGAIN
496 # define X_EAGAIN -99
499 if (errno
== X_EWOULDBLOCK
|| errno
== X_EAGAIN
)
501 if (sh_unset_nodelay_mode (fileno (stream
)) < 0)
509 /* If the error that we received was SIGINT, then try again,
510 this is simply an interrupted system call to read ().
511 Otherwise, some error ocurred, also signifying EOF. */
513 return (RL_ISSTATE (RL_STATE_READCMD
) ? READERR
: EOF
);
517 #if defined (HANDLE_MULTIBYTE)
518 /* read multibyte char */
520 _rl_read_mbchar (mbchar
, size
)
525 size_t mbchar_bytes_length
;
527 mbstate_t ps
, ps_back
;
529 memset(&ps
, 0, sizeof (mbstate_t));
530 memset(&ps_back
, 0, sizeof (mbstate_t));
533 while (mb_len
< size
)
535 RL_SETSTATE(RL_STATE_MOREINPUT
);
537 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
542 mbchar
[mb_len
++] = c
;
544 mbchar_bytes_length
= mbrtowc (&wc
, mbchar
, mb_len
, &ps
);
545 if (mbchar_bytes_length
== (size_t)(-1))
546 break; /* invalid byte sequence for the current locale */
547 else if (mbchar_bytes_length
== (size_t)(-2))
553 else if (mbchar_bytes_length
== 0)
555 mbchar
[0] = '\0'; /* null wide character */
559 else if (mbchar_bytes_length
> (size_t)(0))
566 /* Read a multibyte-character string whose first character is FIRST into
567 the buffer MB of length MLEN. Returns the last character read, which
568 may be FIRST. Used by the search functions, among others. Very similar
569 to _rl_read_mbchar. */
571 _rl_read_mbstring (first
, mb
, mlen
)
580 memset (mb
, 0, mlen
);
581 for (i
= 0; c
>= 0 && i
< mlen
; i
++)
584 memset (&ps
, 0, sizeof (mbstate_t));
585 if (_rl_get_char_len (mb
, &ps
) == -2)
587 /* Read more for multibyte character */
588 RL_SETSTATE (RL_STATE_MOREINPUT
);
590 RL_UNSETSTATE (RL_STATE_MOREINPUT
);
597 #endif /* HANDLE_MULTIBYTE */