]> git.ipfire.org Git - thirdparty/bash.git/blame - lib/readline/input.c
Bash-4.3 distribution sources and documentation
[thirdparty/bash.git] / lib / readline / input.c
CommitLineData
ccc6cda3
JA
1/* input.c -- character input functions for readline. */
2
ac50fbac 3/* Copyright (C) 1994-2013 Free Software Foundation, Inc.
ccc6cda3 4
3185942a
JA
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.
ccc6cda3 7
3185942a
JA
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
ccc6cda3
JA
11 (at your option) any later version.
12
3185942a
JA
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
ccc6cda3
JA
16 GNU General Public License for more details.
17
3185942a
JA
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/>.
20*/
21
ccc6cda3
JA
22#define READLINE_LIBRARY
23
b80f6443
JA
24#if defined (__TANDEM)
25# include <floss.h>
26#endif
27
ccc6cda3
JA
28#if defined (HAVE_CONFIG_H)
29# include <config.h>
30#endif
31
32#include <sys/types.h>
33#include <fcntl.h>
34#if defined (HAVE_SYS_FILE_H)
35# include <sys/file.h>
36#endif /* HAVE_SYS_FILE_H */
37
38#if defined (HAVE_UNISTD_H)
39# include <unistd.h>
40#endif /* HAVE_UNISTD_H */
41
42#if defined (HAVE_STDLIB_H)
43# include <stdlib.h>
44#else
45# include "ansi_stdlib.h"
46#endif /* HAVE_STDLIB_H */
47
ac50fbac
CR
48#include <signal.h>
49
0001803f 50#include "posixselect.h"
ccc6cda3
JA
51
52#if defined (FIONREAD_IN_SYS_IOCTL)
53# include <sys/ioctl.h>
54#endif
55
56#include <stdio.h>
57#include <errno.h>
58
59#if !defined (errno)
60extern int errno;
61#endif /* !errno */
62
63/* System-specific feature definitions and include files. */
64#include "rldefs.h"
7117c2d2 65#include "rlmbutil.h"
ccc6cda3
JA
66
67/* Some standard library routines. */
68#include "readline.h"
69
bb70624e
JA
70#include "rlprivate.h"
71#include "rlshell.h"
72#include "xmalloc.h"
73
ccc6cda3
JA
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 */
77#endif
78
ccc6cda3
JA
79/* Non-null means it is a pointer to a function to run while waiting for
80 character input. */
28ef6c31 81rl_hook_func_t *rl_event_hook = (rl_hook_func_t *)NULL;
ccc6cda3 82
ac50fbac
CR
83/* A function to call if a read(2) is interrupted by a signal. */
84rl_hook_func_t *rl_signal_event_hook = (rl_hook_func_t *)NULL;
85
86/* A function to replace _rl_input_available for applications using the
87 callback interface. */
88rl_hook_func_t *rl_input_available_hook = (rl_hook_func_t *)NULL;
89
28ef6c31
JA
90rl_getc_func_t *rl_getc_function = rl_getc;
91
92static int _keyboard_input_timeout = 100000; /* 0.1 seconds; it's in usec */
ccc6cda3 93
f73dda09
JA
94static int ibuffer_space PARAMS((void));
95static int rl_get_char PARAMS((int *));
7117c2d2 96static int rl_gather_tyi PARAMS((void));
f73dda09 97
ccc6cda3
JA
98/* **************************************************************** */
99/* */
100/* Character Input Buffering */
101/* */
102/* **************************************************************** */
103
104static int pop_index, push_index;
105static unsigned char ibuffer[512];
106static int ibuffer_len = sizeof (ibuffer) - 1;
107
108#define any_typein (push_index != pop_index)
109
110int
111_rl_any_typein ()
112{
113 return any_typein;
114}
115
ac50fbac
CR
116int
117_rl_pushed_input_available ()
118{
119 return (push_index != pop_index);
120}
121
b72432fd
JA
122/* Return the amount of space available in the buffer for stuffing
123 characters. */
ccc6cda3
JA
124static int
125ibuffer_space ()
126{
127 if (pop_index > push_index)
b72432fd 128 return (pop_index - push_index - 1);
ccc6cda3
JA
129 else
130 return (ibuffer_len - (push_index - pop_index));
131}
132
133/* Get a key from the buffer of characters to be read.
134 Return the key in KEY.
ac50fbac 135 Result is non-zero if there was a key, or 0 if there wasn't. */
ccc6cda3
JA
136static int
137rl_get_char (key)
138 int *key;
139{
140 if (push_index == pop_index)
141 return (0);
142
143 *key = ibuffer[pop_index++];
f1be666c 144#if 0
ccc6cda3 145 if (pop_index >= ibuffer_len)
f1be666c
JA
146#else
147 if (pop_index > ibuffer_len)
148#endif
ccc6cda3
JA
149 pop_index = 0;
150
151 return (1);
152}
153
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. */
7117c2d2
JA
157int
158_rl_unget_char (key)
ccc6cda3
JA
159 int key;
160{
161 if (ibuffer_space ())
162 {
163 pop_index--;
164 if (pop_index < 0)
f1be666c 165 pop_index = ibuffer_len;
ccc6cda3
JA
166 ibuffer[pop_index] = key;
167 return (1);
168 }
169 return (0);
170}
171
7117c2d2
JA
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). */
175static int
ccc6cda3
JA
176rl_gather_tyi ()
177{
ccc6cda3
JA
178 int tty;
179 register int tem, result;
b80f6443 180 int chars_avail, k;
ccc6cda3
JA
181 char input;
182#if defined(HAVE_SELECT)
183 fd_set readfds, exceptfds;
184 struct timeval timeout;
185#endif
186
0628567a 187 chars_avail = 0;
ccc6cda3
JA
188 tty = fileno (rl_instream);
189
190#if defined (HAVE_SELECT)
191 FD_ZERO (&readfds);
192 FD_ZERO (&exceptfds);
193 FD_SET (tty, &readfds);
194 FD_SET (tty, &exceptfds);
0001803f 195 USEC_TO_TIMEVAL (_keyboard_input_timeout, timeout);
7117c2d2
JA
196 result = select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout);
197 if (result <= 0)
198 return 0; /* Nothing to read. */
ccc6cda3
JA
199#endif
200
201 result = -1;
202#if defined (FIONREAD)
7117c2d2 203 errno = 0;
ccc6cda3 204 result = ioctl (tty, FIONREAD, &chars_avail);
7117c2d2
JA
205 if (result == -1 && errno == EIO)
206 return -1;
ccc6cda3
JA
207#endif
208
209#if defined (O_NDELAY)
210 if (result == -1)
211 {
212 tem = fcntl (tty, F_GETFL, 0);
213
214 fcntl (tty, F_SETFL, (tem | O_NDELAY));
215 chars_avail = read (tty, &input, 1);
216
217 fcntl (tty, F_SETFL, tem);
218 if (chars_avail == -1 && errno == EAGAIN)
7117c2d2 219 return 0;
b80f6443
JA
220 if (chars_avail == 0) /* EOF */
221 {
222 rl_stuff_char (EOF);
223 return (0);
224 }
ccc6cda3
JA
225 }
226#endif /* O_NDELAY */
227
0628567a
JA
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;
232 result = 0;
233#endif
234
ccc6cda3
JA
235 /* If there's nothing available, don't waste time trying to read
236 something. */
237 if (chars_avail <= 0)
7117c2d2 238 return 0;
ccc6cda3
JA
239
240 tem = ibuffer_space ();
241
242 if (chars_avail > tem)
243 chars_avail = tem;
244
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.
248 Damn! */
249 if (tem < ibuffer_len)
250 chars_avail = 0;
251
252 if (result != -1)
253 {
254 while (chars_avail--)
b80f6443 255 {
17345e5a 256 RL_CHECK_SIGNALS ();
b80f6443 257 k = (*rl_getc_function) (rl_instream);
f1be666c
JA
258 if (rl_stuff_char (k) == 0)
259 break; /* some problem; no more room */
b80f6443
JA
260 if (k == NEWLINE || k == RETURN)
261 break;
262 }
ccc6cda3
JA
263 }
264 else
265 {
266 if (chars_avail)
267 rl_stuff_char (input);
268 }
7117c2d2
JA
269
270 return 1;
ccc6cda3
JA
271}
272
28ef6c31
JA
273int
274rl_set_keyboard_input_timeout (u)
275 int u;
276{
277 int o;
278
279 o = _keyboard_input_timeout;
0628567a 280 if (u >= 0)
28ef6c31
JA
281 _keyboard_input_timeout = u;
282 return (o);
283}
284
ccc6cda3 285/* Is there input available to be read on the readline input file
7117c2d2
JA
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)
290 instead. */
ccc6cda3
JA
291int
292_rl_input_available ()
293{
294#if defined(HAVE_SELECT)
295 fd_set readfds, exceptfds;
296 struct timeval timeout;
297#endif
f73dda09 298#if !defined (HAVE_SELECT) && defined(FIONREAD)
ccc6cda3
JA
299 int chars_avail;
300#endif
301 int tty;
302
ac50fbac
CR
303 if (rl_input_available_hook)
304 return (*rl_input_available_hook) ();
305
ccc6cda3
JA
306 tty = fileno (rl_instream);
307
308#if defined (HAVE_SELECT)
309 FD_ZERO (&readfds);
310 FD_ZERO (&exceptfds);
311 FD_SET (tty, &readfds);
312 FD_SET (tty, &exceptfds);
313 timeout.tv_sec = 0;
28ef6c31 314 timeout.tv_usec = _keyboard_input_timeout;
ccc6cda3 315 return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0);
f73dda09 316#else
ccc6cda3
JA
317
318#if defined (FIONREAD)
319 if (ioctl (tty, FIONREAD, &chars_avail) == 0)
320 return (chars_avail);
f73dda09
JA
321#endif
322
0628567a
JA
323#endif
324
325#if defined (__MINGW32__)
326 if (isatty (tty))
327 return (_kbhit ());
ccc6cda3
JA
328#endif
329
330 return 0;
331}
332
7117c2d2
JA
333int
334_rl_input_queued (t)
335 int t;
336{
337 int old_timeout, r;
338
339 old_timeout = rl_set_keyboard_input_timeout (t);
340 r = _rl_input_available ();
341 rl_set_keyboard_input_timeout (old_timeout);
342 return r;
343}
344
ccc6cda3
JA
345void
346_rl_insert_typein (c)
347 int c;
348{
349 int key, t, i;
350 char *string;
351
352 i = key = 0;
f73dda09 353 string = (char *)xmalloc (ibuffer_len + 1);
ccc6cda3
JA
354 string[i++] = (char) c;
355
356 while ((t = rl_get_char (&key)) &&
357 _rl_keymap[key].type == ISFUNC &&
358 _rl_keymap[key].function == rl_insert)
359 string[i++] = key;
360
361 if (t)
7117c2d2 362 _rl_unget_char (key);
ccc6cda3
JA
363
364 string[i] = '\0';
365 rl_insert_text (string);
3185942a 366 xfree (string);
ccc6cda3
JA
367}
368
b72432fd
JA
369/* Add KEY to the buffer of characters to be read. Returns 1 if the
370 character was stuffed correctly; 0 otherwise. */
371int
372rl_stuff_char (key)
373 int key;
374{
375 if (ibuffer_space () == 0)
376 return 0;
377
378 if (key == EOF)
379 {
380 key = NEWLINE;
381 rl_pending_input = EOF;
28ef6c31 382 RL_SETSTATE (RL_STATE_INPUTPENDING);
b72432fd
JA
383 }
384 ibuffer[push_index++] = key;
f1be666c 385#if 0
b72432fd 386 if (push_index >= ibuffer_len)
f1be666c
JA
387#else
388 if (push_index > ibuffer_len)
389#endif
b72432fd
JA
390 push_index = 0;
391
392 return 1;
393}
394
395/* Make C be the next command to be executed. */
396int
397rl_execute_next (c)
398 int c;
399{
400 rl_pending_input = c;
28ef6c31
JA
401 RL_SETSTATE (RL_STATE_INPUTPENDING);
402 return 0;
403}
404
405/* Clear any pending input pushed with rl_execute_next() */
406int
407rl_clear_pending_input ()
408{
409 rl_pending_input = 0;
410 RL_UNSETSTATE (RL_STATE_INPUTPENDING);
b72432fd
JA
411 return 0;
412}
413
ccc6cda3
JA
414/* **************************************************************** */
415/* */
416/* Character Input */
417/* */
418/* **************************************************************** */
419
420/* Read a key, including pending input. */
421int
422rl_read_key ()
423{
7a127e74 424 int c, r;
ccc6cda3 425
ccc6cda3
JA
426 if (rl_pending_input)
427 {
428 c = rl_pending_input;
28ef6c31 429 rl_clear_pending_input ();
ccc6cda3
JA
430 }
431 else
432 {
433 /* If input is coming from a macro, then use that. */
434 if (c = _rl_next_macro_key ())
435 return (c);
436
437 /* If the user has an event function, then call it periodically. */
438 if (rl_event_hook)
439 {
495aee44 440 while (rl_event_hook)
ccc6cda3 441 {
7a127e74
CR
442 if (rl_get_char (&c) != 0)
443 break;
444
445 if ((r = rl_gather_tyi ()) < 0) /* XXX - EIO */
7117c2d2
JA
446 {
447 rl_done = 1;
448 return ('\n');
449 }
ac50fbac 450 else if (r > 0) /* read something */
7a127e74
CR
451 continue;
452
495aee44 453 RL_CHECK_SIGNALS ();
495aee44
CR
454 if (rl_done) /* XXX - experimental */
455 return ('\n');
456 (*rl_event_hook) ();
ccc6cda3
JA
457 }
458 }
459 else
460 {
461 if (rl_get_char (&c) == 0)
462 c = (*rl_getc_function) (rl_instream);
ac50fbac 463/* fprintf(stderr, "rl_read_key: calling RL_CHECK_SIGNALS: _rl_caught_signal = %d", _rl_caught_signal); */
17345e5a 464 RL_CHECK_SIGNALS ();
ccc6cda3
JA
465 }
466 }
467
468 return (c);
469}
470
471int
472rl_getc (stream)
473 FILE *stream;
474{
bb70624e 475 int result;
ccc6cda3
JA
476 unsigned char c;
477
ccc6cda3
JA
478 while (1)
479 {
17345e5a
JA
480 RL_CHECK_SIGNALS ();
481
ac50fbac
CR
482 /* We know at this point that _rl_caught_signal == 0 */
483
95732b49
JA
484#if defined (__MINGW32__)
485 if (isatty (fileno (stream)))
486 return (getch ());
487#endif
ccc6cda3
JA
488 result = read (fileno (stream), &c, sizeof (unsigned char));
489
490 if (result == sizeof (unsigned char))
491 return (c);
492
493 /* If zero characters are returned, then the file that we are
494 reading from is empty! Return EOF in that case. */
495 if (result == 0)
496 return (EOF);
497
b72432fd
JA
498#if defined (__BEOS__)
499 if (errno == EINTR)
500 continue;
501#endif
502
ccc6cda3 503#if defined (EWOULDBLOCK)
bb70624e
JA
504# define X_EWOULDBLOCK EWOULDBLOCK
505#else
506# define X_EWOULDBLOCK -99
507#endif
508
509#if defined (EAGAIN)
510# define X_EAGAIN EAGAIN
511#else
512# define X_EAGAIN -99
513#endif
514
515 if (errno == X_EWOULDBLOCK || errno == X_EAGAIN)
ccc6cda3 516 {
28ef6c31 517 if (sh_unset_nodelay_mode (fileno (stream)) < 0)
ccc6cda3 518 return (EOF);
ccc6cda3
JA
519 continue;
520 }
ccc6cda3 521
bb70624e
JA
522#undef X_EWOULDBLOCK
523#undef X_EAGAIN
ccc6cda3 524
ac50fbac
CR
525/* fprintf(stderr, "rl_getc: result = %d errno = %d\n", result, errno); */
526
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. */
ccc6cda3 533 if (errno != EINTR)
0628567a 534 return (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF);
ac50fbac
CR
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)
538 RL_CHECK_SIGNALS ();
539
540 if (rl_signal_event_hook)
541 (*rl_signal_event_hook) ();
ccc6cda3
JA
542 }
543}
7117c2d2
JA
544
545#if defined (HANDLE_MULTIBYTE)
546/* read multibyte char */
547int
548_rl_read_mbchar (mbchar, size)
549 char *mbchar;
550 int size;
551{
f1be666c 552 int mb_len, c;
7117c2d2
JA
553 size_t mbchar_bytes_length;
554 wchar_t wc;
555 mbstate_t ps, ps_back;
556
557 memset(&ps, 0, sizeof (mbstate_t));
558 memset(&ps_back, 0, sizeof (mbstate_t));
f1be666c
JA
559
560 mb_len = 0;
7117c2d2
JA
561 while (mb_len < size)
562 {
563 RL_SETSTATE(RL_STATE_MOREINPUT);
f1be666c 564 c = rl_read_key ();
7117c2d2
JA
565 RL_UNSETSTATE(RL_STATE_MOREINPUT);
566
f1be666c
JA
567 if (c < 0)
568 break;
569
570 mbchar[mb_len++] = c;
571
7117c2d2
JA
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))
576 {
577 /* shorted bytes */
578 ps = ps_back;
579 continue;
580 }
95732b49
JA
581 else if (mbchar_bytes_length == 0)
582 {
583 mbchar[0] = '\0'; /* null wide character */
584 mb_len = 1;
585 break;
586 }
7117c2d2
JA
587 else if (mbchar_bytes_length > (size_t)(0))
588 break;
589 }
590
591 return mb_len;
592}
593
594/* Read a multibyte-character string whose first character is FIRST into
0628567a 595 the buffer MB of length MLEN. Returns the last character read, which
7117c2d2
JA
596 may be FIRST. Used by the search functions, among others. Very similar
597 to _rl_read_mbchar. */
598int
0628567a 599_rl_read_mbstring (first, mb, mlen)
7117c2d2
JA
600 int first;
601 char *mb;
0628567a 602 int mlen;
7117c2d2
JA
603{
604 int i, c;
605 mbstate_t ps;
606
607 c = first;
0628567a 608 memset (mb, 0, mlen);
f1be666c 609 for (i = 0; c >= 0 && i < mlen; i++)
7117c2d2
JA
610 {
611 mb[i] = (char)c;
612 memset (&ps, 0, sizeof (mbstate_t));
613 if (_rl_get_char_len (mb, &ps) == -2)
614 {
615 /* Read more for multibyte character */
616 RL_SETSTATE (RL_STATE_MOREINPUT);
617 c = rl_read_key ();
618 RL_UNSETSTATE (RL_STATE_MOREINPUT);
619 }
620 else
621 break;
622 }
623 return c;
624}
625#endif /* HANDLE_MULTIBYTE */