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. */
94 #define WIN32_LEAN_AND_MEAN 1
97 int w32_isatty (int fd
)
101 HANDLE h
= (HANDLE
) _get_osfhandle (fd
);
104 if (h
== INVALID_HANDLE_VALUE
)
109 if (GetConsoleMode (h
, &ignored
) != 0)
116 #define isatty(x) w32_isatty(x)
119 /* **************************************************************** */
121 /* Character Input Buffering */
123 /* **************************************************************** */
125 static int pop_index
, push_index
;
126 static unsigned char ibuffer
[512];
127 static int ibuffer_len
= sizeof (ibuffer
) - 1;
129 #define any_typein (push_index != pop_index)
137 /* Return the amount of space available in the buffer for stuffing
142 if (pop_index
> push_index
)
143 return (pop_index
- push_index
- 1);
145 return (ibuffer_len
- (push_index
- pop_index
));
148 /* Get a key from the buffer of characters to be read.
149 Return the key in KEY.
150 Result is KEY if there was a key, or 0 if there wasn't. */
155 if (push_index
== pop_index
)
158 *key
= ibuffer
[pop_index
++];
160 if (pop_index
>= ibuffer_len
)
162 if (pop_index
> ibuffer_len
)
169 /* Stuff KEY into the *front* of the input buffer.
170 Returns non-zero if successful, zero if there is
171 no space left in the buffer. */
176 if (ibuffer_space ())
180 pop_index
= ibuffer_len
;
181 ibuffer
[pop_index
] = key
;
188 _rl_pushed_input_available ()
190 return (push_index
!= pop_index
);
193 /* If a character is available to be read, then read it and stuff it into
194 IBUFFER. Otherwise, just return. Returns number of characters read
195 (0 if none available) and -1 on error (EIO). */
200 register int tem
, result
;
203 #if defined(HAVE_SELECT)
204 fd_set readfds
, exceptfds
;
205 struct timeval timeout
;
209 tty
= fileno (rl_instream
);
211 #if defined (HAVE_SELECT)
213 FD_ZERO (&exceptfds
);
214 FD_SET (tty
, &readfds
);
215 FD_SET (tty
, &exceptfds
);
216 USEC_TO_TIMEVAL (_keyboard_input_timeout
, timeout
);
217 result
= select (tty
+ 1, &readfds
, (fd_set
*)NULL
, &exceptfds
, &timeout
);
219 return 0; /* Nothing to read. */
223 #if defined (FIONREAD)
225 result
= ioctl (tty
, FIONREAD
, &chars_avail
);
226 if (result
== -1 && errno
== EIO
)
230 #if defined (O_NDELAY)
233 tem
= fcntl (tty
, F_GETFL
, 0);
235 fcntl (tty
, F_SETFL
, (tem
| O_NDELAY
));
236 chars_avail
= read (tty
, &input
, 1);
238 fcntl (tty
, F_SETFL
, tem
);
239 if (chars_avail
== -1 && errno
== EAGAIN
)
241 if (chars_avail
== 0) /* EOF */
247 #endif /* O_NDELAY */
249 #if defined (__MINGW32__)
250 /* Use getch/_kbhit to check for available console input, in the same way
251 that we read it normally. */
252 chars_avail
= isatty (tty
) ? _kbhit () : 0;
256 /* If there's nothing available, don't waste time trying to read
258 if (chars_avail
<= 0)
261 tem
= ibuffer_space ();
263 if (chars_avail
> tem
)
266 /* One cannot read all of the available input. I can only read a single
267 character at a time, or else programs which require input can be
268 thwarted. If the buffer is larger than one character, I lose.
270 if (tem
< ibuffer_len
)
275 while (chars_avail
--)
278 k
= (*rl_getc_function
) (rl_instream
);
279 if (rl_stuff_char (k
) == 0)
280 break; /* some problem; no more room */
281 if (k
== NEWLINE
|| k
== RETURN
)
288 rl_stuff_char (input
);
295 rl_set_keyboard_input_timeout (u
)
300 o
= _keyboard_input_timeout
;
302 _keyboard_input_timeout
= u
;
306 /* Is there input available to be read on the readline input file
307 descriptor? Only works if the system has select(2) or FIONREAD.
308 Uses the value of _keyboard_input_timeout as the timeout; if another
309 readline function wants to specify a timeout and not leave it up to
310 the user, it should use _rl_input_queued(timeout_value_in_microseconds)
313 _rl_input_available ()
315 #if defined(HAVE_SELECT)
316 fd_set readfds
, exceptfds
;
317 struct timeval timeout
;
319 #if !defined (HAVE_SELECT) && defined(FIONREAD)
324 tty
= fileno (rl_instream
);
326 #if defined (HAVE_SELECT)
328 FD_ZERO (&exceptfds
);
329 FD_SET (tty
, &readfds
);
330 FD_SET (tty
, &exceptfds
);
332 timeout
.tv_usec
= _keyboard_input_timeout
;
333 return (select (tty
+ 1, &readfds
, (fd_set
*)NULL
, &exceptfds
, &timeout
) > 0);
336 #if defined (FIONREAD)
337 if (ioctl (tty
, FIONREAD
, &chars_avail
) == 0)
338 return (chars_avail
);
343 #if defined (__MINGW32__)
357 old_timeout
= rl_set_keyboard_input_timeout (t
);
358 r
= _rl_input_available ();
359 rl_set_keyboard_input_timeout (old_timeout
);
364 _rl_insert_typein (c
)
371 string
= (char *)xmalloc (ibuffer_len
+ 1);
372 string
[i
++] = (char) c
;
374 while ((t
= rl_get_char (&key
)) &&
375 _rl_keymap
[key
].type
== ISFUNC
&&
376 _rl_keymap
[key
].function
== rl_insert
)
380 _rl_unget_char (key
);
383 rl_insert_text (string
);
387 /* Add KEY to the buffer of characters to be read. Returns 1 if the
388 character was stuffed correctly; 0 otherwise. */
393 if (ibuffer_space () == 0)
399 rl_pending_input
= EOF
;
400 RL_SETSTATE (RL_STATE_INPUTPENDING
);
402 ibuffer
[push_index
++] = key
;
404 if (push_index
>= ibuffer_len
)
406 if (push_index
> ibuffer_len
)
413 /* Make C be the next command to be executed. */
418 rl_pending_input
= c
;
419 RL_SETSTATE (RL_STATE_INPUTPENDING
);
423 /* Clear any pending input pushed with rl_execute_next() */
425 rl_clear_pending_input ()
427 rl_pending_input
= 0;
428 RL_UNSETSTATE (RL_STATE_INPUTPENDING
);
432 /* **************************************************************** */
434 /* Character Input */
436 /* **************************************************************** */
438 /* Read a key, including pending input. */
444 rl_key_sequence_length
++;
446 if (rl_pending_input
)
448 c
= rl_pending_input
;
449 rl_clear_pending_input ();
453 /* If input is coming from a macro, then use that. */
454 if (c
= _rl_next_macro_key ())
457 /* If the user has an event function, then call it periodically. */
460 while (rl_event_hook
)
462 if (rl_gather_tyi () < 0) /* XXX - EIO */
468 if (rl_get_char (&c
) != 0)
470 if (rl_done
) /* XXX - experimental */
477 if (rl_get_char (&c
) == 0)
478 c
= (*rl_getc_function
) (rl_instream
);
497 #if defined (__MINGW32__)
498 /* Use _getch to make sure we call the function from MS runtime,
499 even if some curses library is linked in. */
500 if (isatty (fileno (stream
)))
503 result
= read (fileno (stream
), &c
, sizeof (unsigned char));
505 if (result
== sizeof (unsigned char))
508 /* If zero characters are returned, then the file that we are
509 reading from is empty! Return EOF in that case. */
513 #if defined (__BEOS__)
518 #if defined (EWOULDBLOCK)
519 # define X_EWOULDBLOCK EWOULDBLOCK
521 # define X_EWOULDBLOCK -99
525 # define X_EAGAIN EAGAIN
527 # define X_EAGAIN -99
530 if (errno
== X_EWOULDBLOCK
|| errno
== X_EAGAIN
)
532 if (sh_unset_nodelay_mode (fileno (stream
)) < 0)
540 /* If the error that we received was SIGINT, then try again,
541 this is simply an interrupted system call to read ().
542 Otherwise, some error ocurred, also signifying EOF. */
544 return (RL_ISSTATE (RL_STATE_READCMD
) ? READERR
: EOF
);
548 #if defined (HANDLE_MULTIBYTE)
549 /* read multibyte char */
551 _rl_read_mbchar (mbchar
, size
)
556 size_t mbchar_bytes_length
;
558 mbstate_t ps
, ps_back
;
560 memset(&ps
, 0, sizeof (mbstate_t));
561 memset(&ps_back
, 0, sizeof (mbstate_t));
564 while (mb_len
< size
)
566 RL_SETSTATE(RL_STATE_MOREINPUT
);
568 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
573 mbchar
[mb_len
++] = c
;
575 mbchar_bytes_length
= mbrtowc (&wc
, mbchar
, mb_len
, &ps
);
576 if (mbchar_bytes_length
== (size_t)(-1))
577 break; /* invalid byte sequence for the current locale */
578 else if (mbchar_bytes_length
== (size_t)(-2))
584 else if (mbchar_bytes_length
== 0)
586 mbchar
[0] = '\0'; /* null wide character */
590 else if (mbchar_bytes_length
> (size_t)(0))
597 /* Read a multibyte-character string whose first character is FIRST into
598 the buffer MB of length MLEN. Returns the last character read, which
599 may be FIRST. Used by the search functions, among others. Very similar
600 to _rl_read_mbchar. */
602 _rl_read_mbstring (first
, mb
, mlen
)
611 memset (mb
, 0, mlen
);
612 for (i
= 0; c
>= 0 && i
< mlen
; i
++)
615 memset (&ps
, 0, sizeof (mbstate_t));
616 if (_rl_get_char_len (mb
, &ps
) == -2)
618 /* Read more for multibyte character */
619 RL_SETSTATE (RL_STATE_MOREINPUT
);
621 RL_UNSETSTATE (RL_STATE_MOREINPUT
);
628 #endif /* HANDLE_MULTIBYTE */
This page took 0.089765 seconds and 4 git commands to generate.