Import readline 7.0 (patch 5)
[deliverable/binutils-gdb.git] / readline / input.c
CommitLineData
d60d9f65
SS
1/* input.c -- character input functions for readline. */
2
775e241e 3/* Copyright (C) 1994-2015 Free Software Foundation, Inc.
d60d9f65 4
cc88a640
JK
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.
d60d9f65 7
cc88a640
JK
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
d60d9f65
SS
11 (at your option) any later version.
12
cc88a640
JK
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
d60d9f65
SS
16 GNU General Public License for more details.
17
cc88a640
JK
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
d60d9f65
SS
22#define READLINE_LIBRARY
23
5bdf8622
DJ
24#if defined (__TANDEM)
25# include <floss.h>
26#endif
27
d60d9f65
SS
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
775e241e
TT
48#include <signal.h>
49
cc88a640 50#include "posixselect.h"
d60d9f65
SS
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"
9255ee31 65#include "rlmbutil.h"
d60d9f65
SS
66
67/* Some standard library routines. */
68#include "readline.h"
69
1b17e766
EZ
70#include "rlprivate.h"
71#include "rlshell.h"
72#include "xmalloc.h"
73
d60d9f65
SS
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
d60d9f65
SS
79/* Non-null means it is a pointer to a function to run while waiting for
80 character input. */
9255ee31 81rl_hook_func_t *rl_event_hook = (rl_hook_func_t *)NULL;
d60d9f65 82
775e241e
TT
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
9255ee31
EZ
90rl_getc_func_t *rl_getc_function = rl_getc;
91
92static int _keyboard_input_timeout = 100000; /* 0.1 seconds; it's in usec */
93
94static int ibuffer_space PARAMS((void));
95static int rl_get_char PARAMS((int *));
96static int rl_gather_tyi PARAMS((void));
d60d9f65 97
7f3c5ec8
EZ
98#if defined (_WIN32) && !defined (__CYGWIN__)
99
100/* 'isatty' in the Windows runtime returns non-zero for every
101 character device, including the null device. Repair that. */
102#include <io.h>
50e1d299 103#include <conio.h>
7f3c5ec8
EZ
104#define WIN32_LEAN_AND_MEAN 1
105#include <windows.h>
106
107int w32_isatty (int fd)
108{
109 if (_isatty(fd))
110 {
111 HANDLE h = (HANDLE) _get_osfhandle (fd);
112 DWORD ignored;
113
114 if (h == INVALID_HANDLE_VALUE)
115 {
116 errno = EBADF;
117 return 0;
118 }
119 if (GetConsoleMode (h, &ignored) != 0)
120 return 1;
121 }
122 errno = ENOTTY;
123 return 0;
124}
125
126#define isatty(x) w32_isatty(x)
127#endif
128
d60d9f65
SS
129/* **************************************************************** */
130/* */
131/* Character Input Buffering */
132/* */
133/* **************************************************************** */
134
135static int pop_index, push_index;
136static unsigned char ibuffer[512];
137static int ibuffer_len = sizeof (ibuffer) - 1;
138
139#define any_typein (push_index != pop_index)
140
141int
142_rl_any_typein ()
143{
144 return any_typein;
145}
146
775e241e
TT
147int
148_rl_pushed_input_available ()
149{
150 return (push_index != pop_index);
151}
152
c862e87b
JM
153/* Return the amount of space available in the buffer for stuffing
154 characters. */
d60d9f65
SS
155static int
156ibuffer_space ()
157{
158 if (pop_index > push_index)
c862e87b 159 return (pop_index - push_index - 1);
d60d9f65
SS
160 else
161 return (ibuffer_len - (push_index - pop_index));
162}
163
164/* Get a key from the buffer of characters to be read.
165 Return the key in KEY.
775e241e 166 Result is non-zero if there was a key, or 0 if there wasn't. */
d60d9f65
SS
167static int
168rl_get_char (key)
169 int *key;
170{
171 if (push_index == pop_index)
172 return (0);
173
174 *key = ibuffer[pop_index++];
cc88a640 175#if 0
d60d9f65 176 if (pop_index >= ibuffer_len)
cc88a640
JK
177#else
178 if (pop_index > ibuffer_len)
179#endif
d60d9f65
SS
180 pop_index = 0;
181
182 return (1);
183}
184
185/* Stuff KEY into the *front* of the input buffer.
186 Returns non-zero if successful, zero if there is
187 no space left in the buffer. */
9255ee31
EZ
188int
189_rl_unget_char (key)
d60d9f65
SS
190 int key;
191{
192 if (ibuffer_space ())
193 {
194 pop_index--;
195 if (pop_index < 0)
cc88a640 196 pop_index = ibuffer_len;
d60d9f65
SS
197 ibuffer[pop_index] = key;
198 return (1);
199 }
200 return (0);
201}
202
9255ee31
EZ
203/* If a character is available to be read, then read it and stuff it into
204 IBUFFER. Otherwise, just return. Returns number of characters read
205 (0 if none available) and -1 on error (EIO). */
206static int
d60d9f65
SS
207rl_gather_tyi ()
208{
d60d9f65
SS
209 int tty;
210 register int tem, result;
5bdf8622 211 int chars_avail, k;
d60d9f65
SS
212 char input;
213#if defined(HAVE_SELECT)
214 fd_set readfds, exceptfds;
215 struct timeval timeout;
216#endif
217
cc88a640 218 chars_avail = 0;
775e241e 219 input = 0;
d60d9f65
SS
220 tty = fileno (rl_instream);
221
222#if defined (HAVE_SELECT)
223 FD_ZERO (&readfds);
224 FD_ZERO (&exceptfds);
225 FD_SET (tty, &readfds);
226 FD_SET (tty, &exceptfds);
cc88a640 227 USEC_TO_TIMEVAL (_keyboard_input_timeout, timeout);
9255ee31
EZ
228 result = select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout);
229 if (result <= 0)
230 return 0; /* Nothing to read. */
d60d9f65
SS
231#endif
232
233 result = -1;
9255ee31 234 errno = 0;
775e241e 235#if defined (FIONREAD)
d60d9f65 236 result = ioctl (tty, FIONREAD, &chars_avail);
9255ee31
EZ
237 if (result == -1 && errno == EIO)
238 return -1;
775e241e
TT
239 if (result == -1)
240 chars_avail = 0;
d60d9f65
SS
241#endif
242
243#if defined (O_NDELAY)
244 if (result == -1)
245 {
246 tem = fcntl (tty, F_GETFL, 0);
247
248 fcntl (tty, F_SETFL, (tem | O_NDELAY));
249 chars_avail = read (tty, &input, 1);
250
251 fcntl (tty, F_SETFL, tem);
252 if (chars_avail == -1 && errno == EAGAIN)
9255ee31 253 return 0;
775e241e
TT
254 if (chars_avail == -1 && errno == EIO)
255 return -1;
5bdf8622
DJ
256 if (chars_avail == 0) /* EOF */
257 {
258 rl_stuff_char (EOF);
259 return (0);
260 }
d60d9f65
SS
261 }
262#endif /* O_NDELAY */
263
5bdf8622 264#if defined (__MINGW32__)
cc88a640
JK
265 /* Use getch/_kbhit to check for available console input, in the same way
266 that we read it normally. */
267 chars_avail = isatty (tty) ? _kbhit () : 0;
268 result = 0;
5bdf8622
DJ
269#endif
270
d60d9f65
SS
271 /* If there's nothing available, don't waste time trying to read
272 something. */
273 if (chars_avail <= 0)
9255ee31 274 return 0;
d60d9f65
SS
275
276 tem = ibuffer_space ();
277
278 if (chars_avail > tem)
279 chars_avail = tem;
280
281 /* One cannot read all of the available input. I can only read a single
282 character at a time, or else programs which require input can be
283 thwarted. If the buffer is larger than one character, I lose.
284 Damn! */
285 if (tem < ibuffer_len)
286 chars_avail = 0;
287
288 if (result != -1)
289 {
290 while (chars_avail--)
5bdf8622 291 {
cc88a640 292 RL_CHECK_SIGNALS ();
5bdf8622 293 k = (*rl_getc_function) (rl_instream);
cc88a640
JK
294 if (rl_stuff_char (k) == 0)
295 break; /* some problem; no more room */
5bdf8622
DJ
296 if (k == NEWLINE || k == RETURN)
297 break;
298 }
d60d9f65
SS
299 }
300 else
301 {
302 if (chars_avail)
303 rl_stuff_char (input);
304 }
9255ee31
EZ
305
306 return 1;
307}
308
309int
310rl_set_keyboard_input_timeout (u)
311 int u;
312{
313 int o;
314
315 o = _keyboard_input_timeout;
cc88a640 316 if (u >= 0)
9255ee31
EZ
317 _keyboard_input_timeout = u;
318 return (o);
d60d9f65
SS
319}
320
321/* Is there input available to be read on the readline input file
9255ee31
EZ
322 descriptor? Only works if the system has select(2) or FIONREAD.
323 Uses the value of _keyboard_input_timeout as the timeout; if another
324 readline function wants to specify a timeout and not leave it up to
325 the user, it should use _rl_input_queued(timeout_value_in_microseconds)
326 instead. */
d60d9f65
SS
327int
328_rl_input_available ()
329{
330#if defined(HAVE_SELECT)
331 fd_set readfds, exceptfds;
332 struct timeval timeout;
333#endif
9255ee31 334#if !defined (HAVE_SELECT) && defined(FIONREAD)
d60d9f65
SS
335 int chars_avail;
336#endif
337 int tty;
338
775e241e
TT
339 if (rl_input_available_hook)
340 return (*rl_input_available_hook) ();
341
d60d9f65
SS
342 tty = fileno (rl_instream);
343
344#if defined (HAVE_SELECT)
345 FD_ZERO (&readfds);
346 FD_ZERO (&exceptfds);
347 FD_SET (tty, &readfds);
348 FD_SET (tty, &exceptfds);
349 timeout.tv_sec = 0;
9255ee31 350 timeout.tv_usec = _keyboard_input_timeout;
d60d9f65 351 return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0);
9255ee31 352#else
d60d9f65
SS
353
354#if defined (FIONREAD)
355 if (ioctl (tty, FIONREAD, &chars_avail) == 0)
356 return (chars_avail);
9255ee31
EZ
357#endif
358
5bdf8622
DJ
359#endif
360
361#if defined (__MINGW32__)
cc88a640
JK
362 if (isatty (tty))
363 return (_kbhit ());
d60d9f65
SS
364#endif
365
366 return 0;
367}
368
9255ee31
EZ
369int
370_rl_input_queued (t)
371 int t;
372{
373 int old_timeout, r;
374
375 old_timeout = rl_set_keyboard_input_timeout (t);
376 r = _rl_input_available ();
377 rl_set_keyboard_input_timeout (old_timeout);
378 return r;
379}
380
d60d9f65
SS
381void
382_rl_insert_typein (c)
383 int c;
384{
385 int key, t, i;
386 char *string;
387
388 i = key = 0;
9255ee31 389 string = (char *)xmalloc (ibuffer_len + 1);
d60d9f65
SS
390 string[i++] = (char) c;
391
392 while ((t = rl_get_char (&key)) &&
393 _rl_keymap[key].type == ISFUNC &&
394 _rl_keymap[key].function == rl_insert)
395 string[i++] = key;
396
397 if (t)
9255ee31 398 _rl_unget_char (key);
d60d9f65
SS
399
400 string[i] = '\0';
401 rl_insert_text (string);
cc88a640 402 xfree (string);
d60d9f65
SS
403}
404
c862e87b
JM
405/* Add KEY to the buffer of characters to be read. Returns 1 if the
406 character was stuffed correctly; 0 otherwise. */
407int
408rl_stuff_char (key)
409 int key;
410{
411 if (ibuffer_space () == 0)
412 return 0;
413
414 if (key == EOF)
415 {
416 key = NEWLINE;
417 rl_pending_input = EOF;
9255ee31 418 RL_SETSTATE (RL_STATE_INPUTPENDING);
c862e87b
JM
419 }
420 ibuffer[push_index++] = key;
cc88a640 421#if 0
c862e87b 422 if (push_index >= ibuffer_len)
cc88a640
JK
423#else
424 if (push_index > ibuffer_len)
425#endif
c862e87b
JM
426 push_index = 0;
427
428 return 1;
429}
430
431/* Make C be the next command to be executed. */
432int
433rl_execute_next (c)
434 int c;
435{
436 rl_pending_input = c;
9255ee31
EZ
437 RL_SETSTATE (RL_STATE_INPUTPENDING);
438 return 0;
439}
440
441/* Clear any pending input pushed with rl_execute_next() */
442int
443rl_clear_pending_input ()
444{
445 rl_pending_input = 0;
446 RL_UNSETSTATE (RL_STATE_INPUTPENDING);
c862e87b
JM
447 return 0;
448}
449
d60d9f65
SS
450/* **************************************************************** */
451/* */
452/* Character Input */
453/* */
454/* **************************************************************** */
455
456/* Read a key, including pending input. */
457int
458rl_read_key ()
459{
775e241e 460 int c, r;
d60d9f65
SS
461
462 if (rl_pending_input)
463 {
464 c = rl_pending_input;
9255ee31 465 rl_clear_pending_input ();
d60d9f65
SS
466 }
467 else
468 {
469 /* If input is coming from a macro, then use that. */
470 if (c = _rl_next_macro_key ())
471 return (c);
472
473 /* If the user has an event function, then call it periodically. */
474 if (rl_event_hook)
475 {
cc88a640 476 while (rl_event_hook)
d60d9f65 477 {
775e241e
TT
478 if (rl_get_char (&c) != 0)
479 break;
480
481 if ((r = rl_gather_tyi ()) < 0) /* XXX - EIO */
9255ee31
EZ
482 {
483 rl_done = 1;
775e241e 484 return (errno == EIO ? (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF) : '\n');
9255ee31 485 }
775e241e
TT
486 else if (r > 0) /* read something */
487 continue;
488
cc88a640 489 RL_CHECK_SIGNALS ();
cc88a640
JK
490 if (rl_done) /* XXX - experimental */
491 return ('\n');
492 (*rl_event_hook) ();
d60d9f65
SS
493 }
494 }
495 else
496 {
497 if (rl_get_char (&c) == 0)
498 c = (*rl_getc_function) (rl_instream);
775e241e 499/* fprintf(stderr, "rl_read_key: calling RL_CHECK_SIGNALS: _rl_caught_signal = %d", _rl_caught_signal); */
cc88a640 500 RL_CHECK_SIGNALS ();
d60d9f65
SS
501 }
502 }
503
504 return (c);
505}
506
507int
508rl_getc (stream)
509 FILE *stream;
510{
1b17e766 511 int result;
d60d9f65 512 unsigned char c;
775e241e
TT
513#if defined (HAVE_PSELECT)
514 sigset_t empty_set;
515 fd_set readfds;
516#endif
d60d9f65 517
d60d9f65
SS
518 while (1)
519 {
cc88a640
JK
520 RL_CHECK_SIGNALS ();
521
775e241e
TT
522 /* We know at this point that _rl_caught_signal == 0 */
523
5bdf8622 524#if defined (__MINGW32__)
fd8be987 525 if (isatty (fileno (stream)))
775e241e
TT
526 return (_getch ()); /* "There is no error return." */
527#endif
528 result = 0;
529#if defined (HAVE_PSELECT)
530 sigemptyset (&empty_set);
531 sigprocmask (SIG_BLOCK, (sigset_t *)NULL, &empty_set);
532 FD_ZERO (&readfds);
533 FD_SET (fileno (stream), &readfds);
534 result = pselect (fileno (stream) + 1, &readfds, NULL, NULL, NULL, &empty_set);
fd8be987 535#endif
775e241e
TT
536 if (result >= 0)
537 result = read (fileno (stream), &c, sizeof (unsigned char));
d60d9f65
SS
538
539 if (result == sizeof (unsigned char))
540 return (c);
541
542 /* If zero characters are returned, then the file that we are
543 reading from is empty! Return EOF in that case. */
544 if (result == 0)
545 return (EOF);
546
c862e87b
JM
547#if defined (__BEOS__)
548 if (errno == EINTR)
549 continue;
550#endif
551
d60d9f65 552#if defined (EWOULDBLOCK)
1b17e766
EZ
553# define X_EWOULDBLOCK EWOULDBLOCK
554#else
555# define X_EWOULDBLOCK -99
556#endif
557
558#if defined (EAGAIN)
559# define X_EAGAIN EAGAIN
560#else
561# define X_EAGAIN -99
562#endif
563
564 if (errno == X_EWOULDBLOCK || errno == X_EAGAIN)
d60d9f65 565 {
9255ee31 566 if (sh_unset_nodelay_mode (fileno (stream)) < 0)
d60d9f65 567 return (EOF);
d60d9f65
SS
568 continue;
569 }
d60d9f65 570
1b17e766
EZ
571#undef X_EWOULDBLOCK
572#undef X_EAGAIN
d60d9f65 573
775e241e
TT
574/* fprintf(stderr, "rl_getc: result = %d errno = %d\n", result, errno); */
575
576handle_error:
577 /* If the error that we received was EINTR, then try again,
578 this is simply an interrupted system call to read (). We allow
579 the read to be interrupted if we caught SIGHUP, SIGTERM, or any
580 of the other signals readline treats specially. If the
581 application sets an event hook, call it for other signals.
582 Otherwise (not EINTR), some error occurred, also signifying EOF. */
d60d9f65 583 if (errno != EINTR)
cc88a640 584 return (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF);
775e241e
TT
585 /* fatal signals of interest */
586#if defined (SIGHUP)
587 else if (_rl_caught_signal == SIGHUP || _rl_caught_signal == SIGTERM)
588#else
589 else if (_rl_caught_signal == SIGTERM)
590#endif
591 return (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF);
592 /* keyboard-generated signals of interest */
593#if defined (SIGQUIT)
594 else if (_rl_caught_signal == SIGINT || _rl_caught_signal == SIGQUIT)
595#else
596 else if (_rl_caught_signal == SIGINT)
597#endif
598 RL_CHECK_SIGNALS ();
599 /* non-keyboard-generated signals of interest */
600#if defined (SIGWINCH)
601 else if (_rl_caught_signal == SIGWINCH)
602 RL_CHECK_SIGNALS ();
603#endif /* SIGWINCH */
604#if defined (SIGALRM)
605 else if (_rl_caught_signal == SIGALRM
606# if defined (SIGVTALRM)
607 || _rl_caught_signal == SIGVTALRM
608# endif
609 )
610 RL_CHECK_SIGNALS ();
611#endif /* SIGALRM */
612
613 if (rl_signal_event_hook)
614 (*rl_signal_event_hook) ();
d60d9f65
SS
615 }
616}
9255ee31
EZ
617
618#if defined (HANDLE_MULTIBYTE)
619/* read multibyte char */
620int
621_rl_read_mbchar (mbchar, size)
622 char *mbchar;
623 int size;
624{
cc88a640 625 int mb_len, c;
9255ee31
EZ
626 size_t mbchar_bytes_length;
627 wchar_t wc;
628 mbstate_t ps, ps_back;
629
630 memset(&ps, 0, sizeof (mbstate_t));
631 memset(&ps_back, 0, sizeof (mbstate_t));
cc88a640
JK
632
633 mb_len = 0;
9255ee31
EZ
634 while (mb_len < size)
635 {
636 RL_SETSTATE(RL_STATE_MOREINPUT);
cc88a640 637 c = rl_read_key ();
9255ee31
EZ
638 RL_UNSETSTATE(RL_STATE_MOREINPUT);
639
cc88a640
JK
640 if (c < 0)
641 break;
642
643 mbchar[mb_len++] = c;
644
9255ee31
EZ
645 mbchar_bytes_length = mbrtowc (&wc, mbchar, mb_len, &ps);
646 if (mbchar_bytes_length == (size_t)(-1))
647 break; /* invalid byte sequence for the current locale */
648 else if (mbchar_bytes_length == (size_t)(-2))
649 {
650 /* shorted bytes */
651 ps = ps_back;
652 continue;
653 }
5bdf8622
DJ
654 else if (mbchar_bytes_length == 0)
655 {
656 mbchar[0] = '\0'; /* null wide character */
657 mb_len = 1;
658 break;
659 }
9255ee31
EZ
660 else if (mbchar_bytes_length > (size_t)(0))
661 break;
662 }
663
664 return mb_len;
665}
666
667/* Read a multibyte-character string whose first character is FIRST into
cc88a640 668 the buffer MB of length MLEN. Returns the last character read, which
9255ee31
EZ
669 may be FIRST. Used by the search functions, among others. Very similar
670 to _rl_read_mbchar. */
671int
cc88a640 672_rl_read_mbstring (first, mb, mlen)
9255ee31
EZ
673 int first;
674 char *mb;
cc88a640 675 int mlen;
9255ee31
EZ
676{
677 int i, c;
678 mbstate_t ps;
679
680 c = first;
cc88a640
JK
681 memset (mb, 0, mlen);
682 for (i = 0; c >= 0 && i < mlen; i++)
9255ee31
EZ
683 {
684 mb[i] = (char)c;
685 memset (&ps, 0, sizeof (mbstate_t));
686 if (_rl_get_char_len (mb, &ps) == -2)
687 {
688 /* Read more for multibyte character */
689 RL_SETSTATE (RL_STATE_MOREINPUT);
690 c = rl_read_key ();
691 RL_UNSETSTATE (RL_STATE_MOREINPUT);
692 }
693 else
694 break;
695 }
696 return c;
697}
698#endif /* HANDLE_MULTIBYTE */
This page took 1.189283 seconds and 4 git commands to generate.