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
/* **************************************************************** */
100
/* Character Input Buffering */
102
/* **************************************************************** */
104
static int pop_index, push_index;
105
static unsigned char ibuffer[512];
106
static int ibuffer_len = sizeof (ibuffer) - 1;
108
#define any_typein (push_index != pop_index)
117
_rl_pushed_input_available ()
119
return (push_index != pop_index);
122
/* Return the amount of space available in the buffer for stuffing
127
if (pop_index > push_index)
128
return (pop_index - push_index - 1);
130
return (ibuffer_len - (push_index - pop_index));
133
/* Get a key from the buffer of characters to be read.
134
Return the key in KEY.
135
Result is non-zero if there was a key, or 0 if there wasn't. */
140
if (push_index == pop_index)
143
*key = ibuffer[pop_index++];
145
if (pop_index >= ibuffer_len)
147
if (pop_index > ibuffer_len)
154
/* Stuff KEY into the *front* of the input buffer.
155
Returns non-zero if successful, zero if there is
156
no space left in the buffer. */
161
if (ibuffer_space ())
165
pop_index = ibuffer_len;
166
ibuffer[pop_index] = key;
172
/* If a character is available to be read, then read it and stuff it into
173
IBUFFER. Otherwise, just return. Returns number of characters read
174
(0 if none available) and -1 on error (EIO). */
179
register int tem, result;
182
#if defined(HAVE_SELECT)
183
fd_set readfds, exceptfds;
184
struct timeval timeout;
188
tty = fileno (rl_instream);
190
#if defined (HAVE_SELECT)
192
FD_ZERO (&exceptfds);
193
FD_SET (tty, &readfds);
194
FD_SET (tty, &exceptfds);
195
USEC_TO_TIMEVAL (_keyboard_input_timeout, timeout);
196
result = select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout);
198
return 0; /* Nothing to read. */
202
#if defined (FIONREAD)
204
result = ioctl (tty, FIONREAD, &chars_avail);
205
if (result == -1 && errno == EIO)
209
#if defined (O_NDELAY)
212
tem = fcntl (tty, F_GETFL, 0);
214
fcntl (tty, F_SETFL, (tem | O_NDELAY));
215
chars_avail = read (tty, &input, 1);
217
fcntl (tty, F_SETFL, tem);
218
if (chars_avail == -1 && errno == EAGAIN)
220
if (chars_avail == 0) /* EOF */
226
#endif /* O_NDELAY */
228
#if defined (__MINGW32__)
229
/* Use getch/_kbhit to check for available console input, in the same way
230
that we read it normally. */
231
chars_avail = isatty (tty) ? _kbhit () : 0;
235
/* If there's nothing available, don't waste time trying to read
237
if (chars_avail <= 0)
240
tem = ibuffer_space ();
242
if (chars_avail > tem)
245
/* One cannot read all of the available input. I can only read a single
246
character at a time, or else programs which require input can be
247
thwarted. If the buffer is larger than one character, I lose.
249
if (tem < ibuffer_len)
254
while (chars_avail--)
257
k = (*rl_getc_function) (rl_instream);
258
if (rl_stuff_char (k) == 0)
259
break; /* some problem; no more room */
260
if (k == NEWLINE || k == RETURN)
267
rl_stuff_char (input);
274
rl_set_keyboard_input_timeout (u)
279
o = _keyboard_input_timeout;
281
_keyboard_input_timeout = u;
285
/* Is there input available to be read on the readline input file
286
descriptor? Only works if the system has select(2) or FIONREAD.
287
Uses the value of _keyboard_input_timeout as the timeout; if another
288
readline function wants to specify a timeout and not leave it up to
289
the user, it should use _rl_input_queued(timeout_value_in_microseconds)
292
_rl_input_available ()
294
#if defined(HAVE_SELECT)
295
fd_set readfds, exceptfds;
296
struct timeval timeout;
298
#if !defined (HAVE_SELECT) && defined(FIONREAD)
303
if (rl_input_available_hook)
304
return (*rl_input_available_hook) ();
306
tty = fileno (rl_instream);
308
#if defined (HAVE_SELECT)
310
FD_ZERO (&exceptfds);
311
FD_SET (tty, &readfds);
312
FD_SET (tty, &exceptfds);
314
timeout.tv_usec = _keyboard_input_timeout;
315
return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0);
318
#if defined (FIONREAD)
319
if (ioctl (tty, FIONREAD, &chars_avail) == 0)
320
return (chars_avail);
325
#if defined (__MINGW32__)
339
old_timeout = rl_set_keyboard_input_timeout (t);
340
r = _rl_input_available ();
341
rl_set_keyboard_input_timeout (old_timeout);
346
_rl_insert_typein (c)
353
string = (char *)xmalloc (ibuffer_len + 1);
354
string[i++] = (char) c;
356
while ((t = rl_get_char (&key)) &&
357
_rl_keymap[key].type == ISFUNC &&
358
_rl_keymap[key].function == rl_insert)
362
_rl_unget_char (key);
365
rl_insert_text (string);
369
/* Add KEY to the buffer of characters to be read. Returns 1 if the
370
character was stuffed correctly; 0 otherwise. */
375
if (ibuffer_space () == 0)
381
rl_pending_input = EOF;
382
RL_SETSTATE (RL_STATE_INPUTPENDING);
384
ibuffer[push_index++] = key;
386
if (push_index >= ibuffer_len)
388
if (push_index > ibuffer_len)
395
/* Make C be the next command to be executed. */
400
rl_pending_input = c;
401
RL_SETSTATE (RL_STATE_INPUTPENDING);
405
/* Clear any pending input pushed with rl_execute_next() */
407
rl_clear_pending_input ()
409
rl_pending_input = 0;
410
RL_UNSETSTATE (RL_STATE_INPUTPENDING);
414
/* **************************************************************** */
416
/* Character Input */
418
/* **************************************************************** */
420
/* Read a key, including pending input. */
426
if (rl_pending_input)
428
c = rl_pending_input;
429
rl_clear_pending_input ();
433
/* If input is coming from a macro, then use that. */
434
if (c = _rl_next_macro_key ())
437
/* If the user has an event function, then call it periodically. */
440
while (rl_event_hook)
442
if (rl_get_char (&c) != 0)
445
if ((r = rl_gather_tyi ()) < 0) /* XXX - EIO */
450
else if (r > 0) /* read something */
454
if (rl_done) /* XXX - experimental */
461
if (rl_get_char (&c) == 0)
462
c = (*rl_getc_function) (rl_instream);
463
/* fprintf(stderr, "rl_read_key: calling RL_CHECK_SIGNALS: _rl_caught_signal = %d", _rl_caught_signal); */
482
/* We know at this point that _rl_caught_signal == 0 */
484
#if defined (__MINGW32__)
485
if (isatty (fileno (stream)))
488
result = read (fileno (stream), &c, sizeof (unsigned char));
490
if (result == sizeof (unsigned char))
493
/* If zero characters are returned, then the file that we are
494
reading from is empty! Return EOF in that case. */
498
#if defined (__BEOS__)
503
#if defined (EWOULDBLOCK)
504
# define X_EWOULDBLOCK EWOULDBLOCK
506
# define X_EWOULDBLOCK -99
510
# define X_EAGAIN EAGAIN
512
# define X_EAGAIN -99
515
if (errno == X_EWOULDBLOCK || errno == X_EAGAIN)
517
if (sh_unset_nodelay_mode (fileno (stream)) < 0)
525
/* fprintf(stderr, "rl_getc: result = %d errno = %d\n", result, errno); */
527
/* If the error that we received was EINTR, then try again,
528
this is simply an interrupted system call to read (). We allow
529
the read to be interrupted if we caught SIGHUP or SIGTERM (but
530
not SIGINT; let the signal handler deal with that), but if the
531
application sets an event hook, call it for other signals.
532
Otherwise (not EINTR), some error occurred, also signifying EOF. */
534
return (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF);
535
else if (_rl_caught_signal == SIGHUP || _rl_caught_signal == SIGTERM)
536
return (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF);
537
else if (_rl_caught_signal == SIGINT || _rl_caught_signal == SIGQUIT)
540
if (rl_signal_event_hook)
541
(*rl_signal_event_hook) ();
545
#if defined (HANDLE_MULTIBYTE)
546
/* read multibyte char */
548
_rl_read_mbchar (mbchar, size)
553
size_t mbchar_bytes_length;
555
mbstate_t ps, ps_back;
557
memset(&ps, 0, sizeof (mbstate_t));
558
memset(&ps_back, 0, sizeof (mbstate_t));
561
while (mb_len < size)
563
RL_SETSTATE(RL_STATE_MOREINPUT);
565
RL_UNSETSTATE(RL_STATE_MOREINPUT);
570
mbchar[mb_len++] = c;
572
mbchar_bytes_length = mbrtowc (&wc, mbchar, mb_len, &ps);
573
if (mbchar_bytes_length == (size_t)(-1))
574
break; /* invalid byte sequence for the current locale */
575
else if (mbchar_bytes_length == (size_t)(-2))
581
else if (mbchar_bytes_length == 0)
583
mbchar[0] = '\0'; /* null wide character */
587
else if (mbchar_bytes_length > (size_t)(0))
594
/* Read a multibyte-character string whose first character is FIRST into
595
the buffer MB of length MLEN. Returns the last character read, which
596
may be FIRST. Used by the search functions, among others. Very similar
597
to _rl_read_mbchar. */
599
_rl_read_mbstring (first, mb, mlen)
608
memset (mb, 0, mlen);
609
for (i = 0; c >= 0 && i < mlen; i++)
612
memset (&ps, 0, sizeof (mbstate_t));
613
if (_rl_get_char_len (mb, &ps) == -2)
615
/* Read more for multibyte character */
616
RL_SETSTATE (RL_STATE_MOREINPUT);
618
RL_UNSETSTATE (RL_STATE_MOREINPUT);
625
#endif /* HANDLE_MULTIBYTE */