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 /* **************************************************************** */
91 /* Character Input Buffering */
93 /* **************************************************************** */
95 static int pop_index, push_index;
96 static unsigned char ibuffer[512];
97 static int ibuffer_len = sizeof (ibuffer) - 1;
99 #define any_typein (push_index != pop_index)
107 /* Return the amount of space available in the buffer for stuffing
112 if (pop_index > push_index)
113 return (pop_index - push_index - 1);
115 return (ibuffer_len - (push_index - pop_index));
118 /* Get a key from the buffer of characters to be read.
119 Return the key in KEY.
120 Result is KEY if there was a key, or 0 if there wasn't. */
125 if (push_index == pop_index)
128 *key = ibuffer[pop_index++];
130 if (pop_index >= ibuffer_len)
132 if (pop_index > ibuffer_len)
139 /* Stuff KEY into the *front* of the input buffer.
140 Returns non-zero if successful, zero if there is
141 no space left in the buffer. */
146 if (ibuffer_space ())
150 pop_index = ibuffer_len;
151 ibuffer[pop_index] = key;
158 _rl_pushed_input_available ()
160 return (push_index != pop_index);
163 /* If a character is available to be read, then read it and stuff it into
164 IBUFFER. Otherwise, just return. Returns number of characters read
165 (0 if none available) and -1 on error (EIO). */
170 register int tem, result;
173 #if defined(HAVE_SELECT)
174 fd_set readfds, exceptfds;
175 struct timeval timeout;
179 tty = fileno (rl_instream);
181 #if defined (HAVE_SELECT)
183 FD_ZERO (&exceptfds);
184 FD_SET (tty, &readfds);
185 FD_SET (tty, &exceptfds);
186 USEC_TO_TIMEVAL (_keyboard_input_timeout, timeout);
187 result = select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout);
189 return 0; /* Nothing to read. */
193 #if defined (FIONREAD)
195 result = ioctl (tty, FIONREAD, &chars_avail);
196 if (result == -1 && errno == EIO)
200 #if defined (O_NDELAY)
203 tem = fcntl (tty, F_GETFL, 0);
205 fcntl (tty, F_SETFL, (tem | O_NDELAY));
206 chars_avail = read (tty, &input, 1);
208 fcntl (tty, F_SETFL, tem);
209 if (chars_avail == -1 && errno == EAGAIN)
211 if (chars_avail == 0) /* EOF */
217 #endif /* O_NDELAY */
219 #if defined (__MINGW32__)
220 /* Use getch/_kbhit to check for available console input, in the same way
221 that we read it normally. */
222 chars_avail = isatty (tty) ? _kbhit () : 0;
226 /* If there's nothing available, don't waste time trying to read
228 if (chars_avail <= 0)
231 tem = ibuffer_space ();
233 if (chars_avail > tem)
236 /* One cannot read all of the available input. I can only read a single
237 character at a time, or else programs which require input can be
238 thwarted. If the buffer is larger than one character, I lose.
240 if (tem < ibuffer_len)
245 while (chars_avail--)
248 k = (*rl_getc_function) (rl_instream);
249 if (rl_stuff_char (k) == 0)
250 break; /* some problem; no more room */
251 if (k == NEWLINE || k == RETURN)
258 rl_stuff_char (input);
265 rl_set_keyboard_input_timeout (u)
270 o = _keyboard_input_timeout;
272 _keyboard_input_timeout = u;
276 /* Is there input available to be read on the readline input file
277 descriptor? Only works if the system has select(2) or FIONREAD.
278 Uses the value of _keyboard_input_timeout as the timeout; if another
279 readline function wants to specify a timeout and not leave it up to
280 the user, it should use _rl_input_queued(timeout_value_in_microseconds)
283 _rl_input_available ()
285 #if defined(HAVE_SELECT)
286 fd_set readfds, exceptfds;
287 struct timeval timeout;
289 #if !defined (HAVE_SELECT) && defined(FIONREAD)
294 tty = fileno (rl_instream);
296 #if defined (HAVE_SELECT)
298 FD_ZERO (&exceptfds);
299 FD_SET (tty, &readfds);
300 FD_SET (tty, &exceptfds);
302 timeout.tv_usec = _keyboard_input_timeout;
303 return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0);
306 #if defined (FIONREAD)
307 if (ioctl (tty, FIONREAD, &chars_avail) == 0)
308 return (chars_avail);
313 #if defined (__MINGW32__)
327 old_timeout = rl_set_keyboard_input_timeout (t);
328 r = _rl_input_available ();
329 rl_set_keyboard_input_timeout (old_timeout);
334 _rl_insert_typein (c)
341 string = (char *)xmalloc (ibuffer_len + 1);
342 string[i++] = (char) c;
344 while ((t = rl_get_char (&key)) &&
345 _rl_keymap[key].type == ISFUNC &&
346 _rl_keymap[key].function == rl_insert)
350 _rl_unget_char (key);
353 rl_insert_text (string);
357 /* Add KEY to the buffer of characters to be read. Returns 1 if the
358 character was stuffed correctly; 0 otherwise. */
363 if (ibuffer_space () == 0)
369 rl_pending_input = EOF;
370 RL_SETSTATE (RL_STATE_INPUTPENDING);
372 ibuffer[push_index++] = key;
374 if (push_index >= ibuffer_len)
376 if (push_index > ibuffer_len)
383 /* Make C be the next command to be executed. */
388 rl_pending_input = c;
389 RL_SETSTATE (RL_STATE_INPUTPENDING);
393 /* Clear any pending input pushed with rl_execute_next() */
395 rl_clear_pending_input ()
397 rl_pending_input = 0;
398 RL_UNSETSTATE (RL_STATE_INPUTPENDING);
402 /* **************************************************************** */
404 /* Character Input */
406 /* **************************************************************** */
408 /* Read a key, including pending input. */
414 rl_key_sequence_length++;
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 == 1) /* read something */
444 if (rl_done) /* XXX - experimental */
451 if (rl_get_char (&c) == 0)
452 c = (*rl_getc_function) (rl_instream);
471 #if defined (__MINGW32__)
472 if (isatty (fileno (stream)))
475 result = read (fileno (stream), &c, sizeof (unsigned char));
477 if (result == sizeof (unsigned char))
480 /* If zero characters are returned, then the file that we are
481 reading from is empty! Return EOF in that case. */
485 #if defined (__BEOS__)
490 #if defined (EWOULDBLOCK)
491 # define X_EWOULDBLOCK EWOULDBLOCK
493 # define X_EWOULDBLOCK -99
497 # define X_EAGAIN EAGAIN
499 # define X_EAGAIN -99
502 if (errno == X_EWOULDBLOCK || errno == X_EAGAIN)
504 if (sh_unset_nodelay_mode (fileno (stream)) < 0)
512 /* If the error that we received was SIGINT, then try again,
513 this is simply an interrupted system call to read ().
514 Otherwise, some error ocurred, also signifying EOF. */
516 return (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF);
520 #if defined (HANDLE_MULTIBYTE)
521 /* read multibyte char */
523 _rl_read_mbchar (mbchar, size)
528 size_t mbchar_bytes_length;
530 mbstate_t ps, ps_back;
532 memset(&ps, 0, sizeof (mbstate_t));
533 memset(&ps_back, 0, sizeof (mbstate_t));
536 while (mb_len < size)
538 RL_SETSTATE(RL_STATE_MOREINPUT);
540 RL_UNSETSTATE(RL_STATE_MOREINPUT);
545 mbchar[mb_len++] = c;
547 mbchar_bytes_length = mbrtowc (&wc, mbchar, mb_len, &ps);
548 if (mbchar_bytes_length == (size_t)(-1))
549 break; /* invalid byte sequence for the current locale */
550 else if (mbchar_bytes_length == (size_t)(-2))
556 else if (mbchar_bytes_length == 0)
558 mbchar[0] = '\0'; /* null wide character */
562 else if (mbchar_bytes_length > (size_t)(0))
569 /* Read a multibyte-character string whose first character is FIRST into
570 the buffer MB of length MLEN. Returns the last character read, which
571 may be FIRST. Used by the search functions, among others. Very similar
572 to _rl_read_mbchar. */
574 _rl_read_mbstring (first, mb, mlen)
583 memset (mb, 0, mlen);
584 for (i = 0; c >= 0 && i < mlen; i++)
587 memset (&ps, 0, sizeof (mbstate_t));
588 if (_rl_get_char_len (mb, &ps) == -2)
590 /* Read more for multibyte character */
591 RL_SETSTATE (RL_STATE_MOREINPUT);
593 RL_UNSETSTATE (RL_STATE_MOREINPUT);
600 #endif /* HANDLE_MULTIBYTE */