1 /* input.c -- character input functions for readline. */
3 /* Copyright (C) 1994-2010 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 */
48 #include "posixselect.h"
50 #if defined (FIONREAD_IN_SYS_IOCTL)
51 # include <sys/ioctl.h>
61 /* System-specific feature definitions and include files. */
65 /* Some standard library routines. */
68 #include "rlprivate.h"
72 /* What kind of non-blocking I/O do we have? */
73 #if !defined (O_NDELAY) && defined (O_NONBLOCK)
74 # define O_NDELAY O_NONBLOCK /* Posix style */
77 /* Non-null means it is a pointer to a function to run while waiting for
79 rl_hook_func_t
*rl_event_hook
= (rl_hook_func_t
*)NULL
;
81 rl_getc_func_t
*rl_getc_function
= rl_getc
;
83 static int _keyboard_input_timeout
= 100000; /* 0.1 seconds; it's in usec */
85 static int ibuffer_space
PARAMS((void));
86 static int rl_get_char
PARAMS((int *));
87 static int rl_gather_tyi
PARAMS((void));
89 #if defined (_WIN32) && !defined (__CYGWIN__)
91 /* 'isatty' in the Windows runtime returns non-zero for every
92 character device, including the null device. Repair that. */
95 #define WIN32_LEAN_AND_MEAN 1
98 int w32_isatty (int fd
)
102 HANDLE h
= (HANDLE
) _get_osfhandle (fd
);
105 if (h
== INVALID_HANDLE_VALUE
)
110 if (GetConsoleMode (h
, &ignored
) != 0)
117 #define isatty(x) w32_isatty(x)
120 /* **************************************************************** */
122 /* Character Input Buffering */
124 /* **************************************************************** */
126 static int pop_index
, push_index
;
127 static unsigned char ibuffer
[512];
128 static int ibuffer_len
= sizeof (ibuffer
) - 1;
130 #define any_typein (push_index != pop_index)
138 /* Return the amount of space available in the buffer for stuffing
143 if (pop_index
> push_index
)
144 return (pop_index
- push_index
- 1);
146 return (ibuffer_len
- (push_index
- pop_index
));
149 /* Get a key from the buffer of characters to be read.
150 Return the key in KEY.
151 Result is KEY if there was a key, or 0 if there wasn't. */
156 if (push_index
== pop_index
)
159 *key
= ibuffer
[pop_index
++];
161 if (pop_index
>= ibuffer_len
)
163 if (pop_index
> ibuffer_len
)
170 /* Stuff KEY into the *front* of the input buffer.
171 Returns non-zero if successful, zero if there is
172 no space left in the buffer. */
177 if (ibuffer_space ())
181 pop_index
= ibuffer_len
;
182 ibuffer
[pop_index
] = key
;
189 _rl_pushed_input_available ()
191 return (push_index
!= pop_index
);
194 /* If a character is available to be read, then read it and stuff it into
195 IBUFFER. Otherwise, just return. Returns number of characters read
196 (0 if none available) and -1 on error (EIO). */
201 register int tem
, result
;
204 #if defined(HAVE_SELECT)
205 fd_set readfds
, exceptfds
;
206 struct timeval timeout
;
210 tty
= fileno (rl_instream
);
212 #if defined (HAVE_SELECT)
214 FD_ZERO (&exceptfds
);
215 FD_SET (tty
, &readfds
);
216 FD_SET (tty
, &exceptfds
);
217 USEC_TO_TIMEVAL (_keyboard_input_timeout
, timeout
);
218 result
= select (tty
+ 1, &readfds
, (fd_set
*)NULL
, &exceptfds
, &timeout
);
220 return 0; /* Nothing to read. */
224 #if defined (FIONREAD)
226 result
= ioctl (tty
, FIONREAD
, &chars_avail
);
227 if (result
== -1 && errno
== EIO
)
231 #if defined (O_NDELAY)
234 tem
= fcntl (tty
, F_GETFL
, 0);
236 fcntl (tty
, F_SETFL
, (tem
| O_NDELAY
));
237 chars_avail
= read (tty
, &input
, 1);
239 fcntl (tty
, F_SETFL
, tem
);
240 if (chars_avail
== -1 && errno
== EAGAIN
)
242 if (chars_avail
== 0) /* EOF */
248 #endif /* O_NDELAY */
250 #if defined (__MINGW32__)
251 /* Use getch/_kbhit to check for available console input, in the same way
252 that we read it normally. */
253 chars_avail
= isatty (tty
) ? _kbhit () : 0;
257 /* If there's nothing available, don't waste time trying to read
259 if (chars_avail
<= 0)
262 tem
= ibuffer_space ();
264 if (chars_avail
> tem
)
267 /* One cannot read all of the available input. I can only read a single
268 character at a time, or else programs which require input can be
269 thwarted. If the buffer is larger than one character, I lose.
271 if (tem
< ibuffer_len
)
276 while (chars_avail
--)
279 k
= (*rl_getc_function
) (rl_instream
);
280 if (rl_stuff_char (k
) == 0)
281 break; /* some problem; no more room */
282 if (k
== NEWLINE
|| k
== RETURN
)
289 rl_stuff_char (input
);
296 rl_set_keyboard_input_timeout (u
)
301 o
= _keyboard_input_timeout
;
303 _keyboard_input_timeout
= u
;
307 /* Is there input available to be read on the readline input file
308 descriptor? Only works if the system has select(2) or FIONREAD.
309 Uses the value of _keyboard_input_timeout as the timeout; if another
310 readline function wants to specify a timeout and not leave it up to
311 the user, it should use _rl_input_queued(timeout_value_in_microseconds)
314 _rl_input_available ()
316 #if defined(HAVE_SELECT)
317 fd_set readfds
, exceptfds
;
318 struct timeval timeout
;
320 #if !defined (HAVE_SELECT) && defined(FIONREAD)
325 tty
= fileno (rl_instream
);
327 #if defined (HAVE_SELECT)
329 FD_ZERO (&exceptfds
);
330 FD_SET (tty
, &readfds
);
331 FD_SET (tty
, &exceptfds
);
333 timeout
.tv_usec
= _keyboard_input_timeout
;
334 return (select (tty
+ 1, &readfds
, (fd_set
*)NULL
, &exceptfds
, &timeout
) > 0);
337 #if defined (FIONREAD)
338 if (ioctl (tty
, FIONREAD
, &chars_avail
) == 0)
339 return (chars_avail
);
344 #if defined (__MINGW32__)
358 old_timeout
= rl_set_keyboard_input_timeout (t
);
359 r
= _rl_input_available ();
360 rl_set_keyboard_input_timeout (old_timeout
);
365 _rl_insert_typein (c
)
372 string
= (char *)xmalloc (ibuffer_len
+ 1);
373 string
[i
++] = (char) c
;
375 while ((t
= rl_get_char (&key
)) &&
376 _rl_keymap
[key
].type
== ISFUNC
&&
377 _rl_keymap
[key
].function
== rl_insert
)
381 _rl_unget_char (key
);
384 rl_insert_text (string
);
388 /* Add KEY to the buffer of characters to be read. Returns 1 if the
389 character was stuffed correctly; 0 otherwise. */
394 if (ibuffer_space () == 0)
400 rl_pending_input
= EOF
;
401 RL_SETSTATE (RL_STATE_INPUTPENDING
);
403 ibuffer
[push_index
++] = key
;
405 if (push_index
>= ibuffer_len
)
407 if (push_index
> ibuffer_len
)
414 /* Make C be the next command to be executed. */
419 rl_pending_input
= c
;
420 RL_SETSTATE (RL_STATE_INPUTPENDING
);
424 /* Clear any pending input pushed with rl_execute_next() */
426 rl_clear_pending_input ()
428 rl_pending_input
= 0;
429 RL_UNSETSTATE (RL_STATE_INPUTPENDING
);
433 /* **************************************************************** */
435 /* Character Input */
437 /* **************************************************************** */
439 /* Read a key, including pending input. */
445 rl_key_sequence_length
++;
447 if (rl_pending_input
)
449 c
= rl_pending_input
;
450 rl_clear_pending_input ();
454 /* If input is coming from a macro, then use that. */
455 if (c
= _rl_next_macro_key ())
458 /* If the user has an event function, then call it periodically. */
461 while (rl_event_hook
)
463 if (rl_gather_tyi () < 0) /* XXX - EIO */
469 if (rl_get_char (&c
) != 0)
471 if (rl_done
) /* XXX - experimental */
478 if (rl_get_char (&c
) == 0)
479 c
= (*rl_getc_function
) (rl_instream
);
498 #if defined (__MINGW32__)
499 /* Use _getch to make sure we call the function from MS runtime,
500 even if some curses library is linked in. */
501 if (isatty (fileno (stream
)))
504 result
= read (fileno (stream
), &c
, sizeof (unsigned char));
506 if (result
== sizeof (unsigned char))
509 /* If zero characters are returned, then the file that we are
510 reading from is empty! Return EOF in that case. */
514 #if defined (__BEOS__)
519 #if defined (EWOULDBLOCK)
520 # define X_EWOULDBLOCK EWOULDBLOCK
522 # define X_EWOULDBLOCK -99
526 # define X_EAGAIN EAGAIN
528 # define X_EAGAIN -99
531 if (errno
== X_EWOULDBLOCK
|| errno
== X_EAGAIN
)
533 if (sh_unset_nodelay_mode (fileno (stream
)) < 0)
541 /* If the error that we received was SIGINT, then try again,
542 this is simply an interrupted system call to read ().
543 Otherwise, some error ocurred, also signifying EOF. */
545 return (RL_ISSTATE (RL_STATE_READCMD
) ? READERR
: EOF
);
549 #if defined (HANDLE_MULTIBYTE)
550 /* read multibyte char */
552 _rl_read_mbchar (mbchar
, size
)
557 size_t mbchar_bytes_length
;
559 mbstate_t ps
, ps_back
;
561 memset(&ps
, 0, sizeof (mbstate_t));
562 memset(&ps_back
, 0, sizeof (mbstate_t));
565 while (mb_len
< size
)
567 RL_SETSTATE(RL_STATE_MOREINPUT
);
569 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
574 mbchar
[mb_len
++] = c
;
576 mbchar_bytes_length
= mbrtowc (&wc
, mbchar
, mb_len
, &ps
);
577 if (mbchar_bytes_length
== (size_t)(-1))
578 break; /* invalid byte sequence for the current locale */
579 else if (mbchar_bytes_length
== (size_t)(-2))
585 else if (mbchar_bytes_length
== 0)
587 mbchar
[0] = '\0'; /* null wide character */
591 else if (mbchar_bytes_length
> (size_t)(0))
598 /* Read a multibyte-character string whose first character is FIRST into
599 the buffer MB of length MLEN. Returns the last character read, which
600 may be FIRST. Used by the search functions, among others. Very similar
601 to _rl_read_mbchar. */
603 _rl_read_mbstring (first
, mb
, mlen
)
612 memset (mb
, 0, mlen
);
613 for (i
= 0; c
>= 0 && i
< mlen
; i
++)
616 memset (&ps
, 0, sizeof (mbstate_t));
617 if (_rl_get_char_len (mb
, &ps
) == -2)
619 /* Read more for multibyte character */
620 RL_SETSTATE (RL_STATE_MOREINPUT
);
622 RL_UNSETSTATE (RL_STATE_MOREINPUT
);
629 #endif /* HANDLE_MULTIBYTE */