1 /* input.c -- character input functions for readline. */
3 /* Copyright (C) 1994-2013 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 /* A function to call if a read(2) is interrupted by a signal. */
84 rl_hook_func_t *rl_signal_event_hook = (rl_hook_func_t *)NULL;
86 /* A function to replace _rl_input_available for applications using the
87 callback interface. */
88 rl_hook_func_t *rl_input_available_hook = (rl_hook_func_t *)NULL;
90 rl_getc_func_t *rl_getc_function = rl_getc;
92 static int _keyboard_input_timeout = 100000; /* 0.1 seconds; it's in usec */
94 static int ibuffer_space PARAMS((void));
95 static int rl_get_char PARAMS((int *));
96 static int rl_gather_tyi PARAMS((void));
98 /* Windows isatty returns true for every character device, including the null
99 device, so we need to perform additional checks. */
100 #if defined (_WIN32) && !defined (__CYGWIN__)
102 #define WIN32_LEAN_AND_MEAN 1
106 win32_isatty (int fd)
108 return (_isatty (fd) ? ((((long) (HANDLE) _get_osfhandle (fd)) & 3) == 3) : 0);
111 #define isatty(x) win32_isatty(x)
114 /* **************************************************************** */
116 /* Character Input Buffering */
118 /* **************************************************************** */
120 static int pop_index, push_index;
121 static unsigned char ibuffer[512];
122 static int ibuffer_len = sizeof (ibuffer) - 1;
124 #define any_typein (push_index != pop_index)
133 _rl_pushed_input_available ()
135 return (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 non-zero 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;
188 /* If a character is available to be read, then read it and stuff it into
189 IBUFFER. Otherwise, just return. Returns number of characters read
190 (0 if none available) and -1 on error (EIO). */
195 register int tem, result;
198 #if defined(HAVE_SELECT)
199 fd_set readfds, exceptfds;
200 struct timeval timeout;
205 tty = fileno (rl_instream);
207 #if defined (HAVE_SELECT)
209 FD_ZERO (&exceptfds);
210 FD_SET (tty, &readfds);
211 FD_SET (tty, &exceptfds);
212 USEC_TO_TIMEVAL (_keyboard_input_timeout, timeout);
213 result = select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout);
215 return 0; /* Nothing to read. */
219 #if defined (FIONREAD)
221 result = ioctl (tty, FIONREAD, &chars_avail);
222 if (result == -1 && errno == EIO)
228 #if defined (O_NDELAY)
231 tem = fcntl (tty, F_GETFL, 0);
233 fcntl (tty, F_SETFL, (tem | O_NDELAY));
234 chars_avail = read (tty, &input, 1);
236 fcntl (tty, F_SETFL, tem);
237 if (chars_avail == -1 && errno == EAGAIN)
239 if (chars_avail == 0) /* EOF */
245 #endif /* O_NDELAY */
247 #if defined (__MINGW32__)
248 /* Use getch/_kbhit to check for available console input, in the same way
249 that we read it normally. */
250 chars_avail = isatty (tty) ? _kbhit () : 0;
254 /* If there's nothing available, don't waste time trying to read
256 if (chars_avail <= 0)
259 tem = ibuffer_space ();
261 if (chars_avail > tem)
264 /* One cannot read all of the available input. I can only read a single
265 character at a time, or else programs which require input can be
266 thwarted. If the buffer is larger than one character, I lose.
268 if (tem < ibuffer_len)
273 while (chars_avail--)
276 k = (*rl_getc_function) (rl_instream);
277 if (rl_stuff_char (k) == 0)
278 break; /* some problem; no more room */
279 if (k == NEWLINE || k == RETURN)
286 rl_stuff_char (input);
293 rl_set_keyboard_input_timeout (u)
298 o = _keyboard_input_timeout;
300 _keyboard_input_timeout = u;
304 /* Is there input available to be read on the readline input file
305 descriptor? Only works if the system has select(2) or FIONREAD.
306 Uses the value of _keyboard_input_timeout as the timeout; if another
307 readline function wants to specify a timeout and not leave it up to
308 the user, it should use _rl_input_queued(timeout_value_in_microseconds)
311 _rl_input_available ()
313 #if defined(HAVE_SELECT)
314 fd_set readfds, exceptfds;
315 struct timeval timeout;
317 #if !defined (HAVE_SELECT) && defined(FIONREAD)
322 if (rl_input_available_hook)
323 return (*rl_input_available_hook) ();
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 if (rl_pending_input)
447 c = rl_pending_input;
448 rl_clear_pending_input ();
452 /* If input is coming from a macro, then use that. */
453 if (c = _rl_next_macro_key ())
456 /* If the user has an event function, then call it periodically. */
459 while (rl_event_hook)
461 if (rl_get_char (&c) != 0)
464 if ((r = rl_gather_tyi ()) < 0) /* XXX - EIO */
469 else if (r > 0) /* read something */
473 if (rl_done) /* XXX - experimental */
480 if (rl_get_char (&c) == 0)
481 c = (*rl_getc_function) (rl_instream);
482 /* fprintf(stderr, "rl_read_key: calling RL_CHECK_SIGNALS: _rl_caught_signal = %d", _rl_caught_signal); */
496 #if defined (HAVE_PSELECT)
505 /* We know at this point that _rl_caught_signal == 0 */
507 #if defined (__MINGW32__)
508 if (isatty (fileno (stream)))
509 return (_getch ()); /* "There is no error return." */
512 #if defined (HAVE_PSELECT)
513 sigemptyset (&empty_set);
515 FD_SET (fileno (stream), &readfds);
516 result = pselect (fileno (stream) + 1, &readfds, NULL, NULL, NULL, &empty_set);
519 result = read (fileno (stream), &c, sizeof (unsigned char));
521 if (result == sizeof (unsigned char))
524 /* If zero characters are returned, then the file that we are
525 reading from is empty! Return EOF in that case. */
529 #if defined (__BEOS__)
534 #if defined (EWOULDBLOCK)
535 # define X_EWOULDBLOCK EWOULDBLOCK
537 # define X_EWOULDBLOCK -99
541 # define X_EAGAIN EAGAIN
543 # define X_EAGAIN -99
546 if (errno == X_EWOULDBLOCK || errno == X_EAGAIN)
548 if (sh_unset_nodelay_mode (fileno (stream)) < 0)
556 /* fprintf(stderr, "rl_getc: result = %d errno = %d\n", result, errno); */
559 /* If the error that we received was EINTR, then try again,
560 this is simply an interrupted system call to read (). We allow
561 the read to be interrupted if we caught SIGHUP, SIGTERM, or any
562 of the other signals readline treats specially. If the
563 application sets an event hook, call it for other signals.
564 Otherwise (not EINTR), some error occurred, also signifying EOF. */
566 return (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF);
567 /* fatal signals of interest */
568 else if (_rl_caught_signal == SIGHUP || _rl_caught_signal == SIGTERM)
569 return (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF);
570 /* keyboard-generated signals of interest */
571 else if (_rl_caught_signal == SIGINT || _rl_caught_signal == SIGQUIT)
573 /* non-keyboard-generated signals of interest */
574 else if (_rl_caught_signal == SIGWINCH)
576 else if (_rl_caught_signal == SIGALRM
577 #if defined (SIGVTALRM)
578 || _rl_caught_signal == SIGVTALRM
583 if (rl_signal_event_hook)
584 (*rl_signal_event_hook) ();
588 #if defined (HANDLE_MULTIBYTE)
589 /* read multibyte char */
591 _rl_read_mbchar (mbchar, size)
596 size_t mbchar_bytes_length;
598 mbstate_t ps, ps_back;
600 memset(&ps, 0, sizeof (mbstate_t));
601 memset(&ps_back, 0, sizeof (mbstate_t));
604 while (mb_len < size)
606 RL_SETSTATE(RL_STATE_MOREINPUT);
608 RL_UNSETSTATE(RL_STATE_MOREINPUT);
613 mbchar[mb_len++] = c;
615 mbchar_bytes_length = mbrtowc (&wc, mbchar, mb_len, &ps);
616 if (mbchar_bytes_length == (size_t)(-1))
617 break; /* invalid byte sequence for the current locale */
618 else if (mbchar_bytes_length == (size_t)(-2))
624 else if (mbchar_bytes_length == 0)
626 mbchar[0] = '\0'; /* null wide character */
630 else if (mbchar_bytes_length > (size_t)(0))
637 /* Read a multibyte-character string whose first character is FIRST into
638 the buffer MB of length MLEN. Returns the last character read, which
639 may be FIRST. Used by the search functions, among others. Very similar
640 to _rl_read_mbchar. */
642 _rl_read_mbstring (first, mb, mlen)
651 memset (mb, 0, mlen);
652 for (i = 0; c >= 0 && i < mlen; i++)
655 memset (&ps, 0, sizeof (mbstate_t));
656 if (_rl_get_char_len (mb, &ps) == -2)
658 /* Read more for multibyte character */
659 RL_SETSTATE (RL_STATE_MOREINPUT);
661 RL_UNSETSTATE (RL_STATE_MOREINPUT);
668 #endif /* HANDLE_MULTIBYTE */