]> git.ipfire.org Git - thirdparty/bash.git/blob - lib/readline/input.c
commit bash-20150626 snapshot
[thirdparty/bash.git] / lib / readline / input.c
1 /* input.c -- character input functions for readline. */
2
3 /* Copyright (C) 1994-2013 Free Software Foundation, Inc.
4
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.
7
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.
12
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.
17
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
22 #define READLINE_LIBRARY
23
24 #if defined (__TANDEM)
25 # include <floss.h>
26 #endif
27
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
48 #include <signal.h>
49
50 #include "posixselect.h"
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)
60 extern int errno;
61 #endif /* !errno */
62
63 /* System-specific feature definitions and include files. */
64 #include "rldefs.h"
65 #include "rlmbutil.h"
66
67 /* Some standard library routines. */
68 #include "readline.h"
69
70 #include "rlprivate.h"
71 #include "rlshell.h"
72 #include "xmalloc.h"
73
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
79 /* Non-null means it is a pointer to a function to run while waiting for
80 character input. */
81 rl_hook_func_t *rl_event_hook = (rl_hook_func_t *)NULL;
82
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;
85
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;
89
90 rl_getc_func_t *rl_getc_function = rl_getc;
91
92 static int _keyboard_input_timeout = 100000; /* 0.1 seconds; it's in usec */
93
94 static int ibuffer_space PARAMS((void));
95 static int rl_get_char PARAMS((int *));
96 static int rl_gather_tyi PARAMS((void));
97
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__)
101 #include <io.h>
102 #define WIN32_LEAN_AND_MEAN 1
103 #include <windows.h>
104
105 int
106 win32_isatty (int fd)
107 {
108 return (_isatty (fd) ? ((((long) (HANDLE) _get_osfhandle (fd)) & 3) == 3) : 0);
109 }
110
111 #define isatty(x) win32_isatty(x)
112 #endif
113
114 /* **************************************************************** */
115 /* */
116 /* Character Input Buffering */
117 /* */
118 /* **************************************************************** */
119
120 static int pop_index, push_index;
121 static unsigned char ibuffer[512];
122 static int ibuffer_len = sizeof (ibuffer) - 1;
123
124 #define any_typein (push_index != pop_index)
125
126 int
127 _rl_any_typein ()
128 {
129 return any_typein;
130 }
131
132 int
133 _rl_pushed_input_available ()
134 {
135 return (push_index != pop_index);
136 }
137
138 /* Return the amount of space available in the buffer for stuffing
139 characters. */
140 static int
141 ibuffer_space ()
142 {
143 if (pop_index > push_index)
144 return (pop_index - push_index - 1);
145 else
146 return (ibuffer_len - (push_index - pop_index));
147 }
148
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. */
152 static int
153 rl_get_char (key)
154 int *key;
155 {
156 if (push_index == pop_index)
157 return (0);
158
159 *key = ibuffer[pop_index++];
160 #if 0
161 if (pop_index >= ibuffer_len)
162 #else
163 if (pop_index > ibuffer_len)
164 #endif
165 pop_index = 0;
166
167 return (1);
168 }
169
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. */
173 int
174 _rl_unget_char (key)
175 int key;
176 {
177 if (ibuffer_space ())
178 {
179 pop_index--;
180 if (pop_index < 0)
181 pop_index = ibuffer_len;
182 ibuffer[pop_index] = key;
183 return (1);
184 }
185 return (0);
186 }
187
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). */
191 static int
192 rl_gather_tyi ()
193 {
194 int tty;
195 register int tem, result;
196 int chars_avail, k;
197 char input;
198 #if defined(HAVE_SELECT)
199 fd_set readfds, exceptfds;
200 struct timeval timeout;
201 #endif
202
203 chars_avail = 0;
204 input = 0;
205 tty = fileno (rl_instream);
206
207 #if defined (HAVE_SELECT)
208 FD_ZERO (&readfds);
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);
214 if (result <= 0)
215 return 0; /* Nothing to read. */
216 #endif
217
218 result = -1;
219 #if defined (FIONREAD)
220 errno = 0;
221 result = ioctl (tty, FIONREAD, &chars_avail);
222 if (result == -1 && errno == EIO)
223 return -1;
224 if (result == -1)
225 chars_avail = 0;
226 #endif
227
228 #if defined (O_NDELAY)
229 if (result == -1)
230 {
231 tem = fcntl (tty, F_GETFL, 0);
232
233 fcntl (tty, F_SETFL, (tem | O_NDELAY));
234 chars_avail = read (tty, &input, 1);
235
236 fcntl (tty, F_SETFL, tem);
237 if (chars_avail == -1 && errno == EAGAIN)
238 return 0;
239 if (chars_avail == 0) /* EOF */
240 {
241 rl_stuff_char (EOF);
242 return (0);
243 }
244 }
245 #endif /* O_NDELAY */
246
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;
251 result = 0;
252 #endif
253
254 /* If there's nothing available, don't waste time trying to read
255 something. */
256 if (chars_avail <= 0)
257 return 0;
258
259 tem = ibuffer_space ();
260
261 if (chars_avail > tem)
262 chars_avail = tem;
263
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.
267 Damn! */
268 if (tem < ibuffer_len)
269 chars_avail = 0;
270
271 if (result != -1)
272 {
273 while (chars_avail--)
274 {
275 RL_CHECK_SIGNALS ();
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)
280 break;
281 }
282 }
283 else
284 {
285 if (chars_avail)
286 rl_stuff_char (input);
287 }
288
289 return 1;
290 }
291
292 int
293 rl_set_keyboard_input_timeout (u)
294 int u;
295 {
296 int o;
297
298 o = _keyboard_input_timeout;
299 if (u >= 0)
300 _keyboard_input_timeout = u;
301 return (o);
302 }
303
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)
309 instead. */
310 int
311 _rl_input_available ()
312 {
313 #if defined(HAVE_SELECT)
314 fd_set readfds, exceptfds;
315 struct timeval timeout;
316 #endif
317 #if !defined (HAVE_SELECT) && defined(FIONREAD)
318 int chars_avail;
319 #endif
320 int tty;
321
322 if (rl_input_available_hook)
323 return (*rl_input_available_hook) ();
324
325 tty = fileno (rl_instream);
326
327 #if defined (HAVE_SELECT)
328 FD_ZERO (&readfds);
329 FD_ZERO (&exceptfds);
330 FD_SET (tty, &readfds);
331 FD_SET (tty, &exceptfds);
332 timeout.tv_sec = 0;
333 timeout.tv_usec = _keyboard_input_timeout;
334 return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0);
335 #else
336
337 #if defined (FIONREAD)
338 if (ioctl (tty, FIONREAD, &chars_avail) == 0)
339 return (chars_avail);
340 #endif
341
342 #endif
343
344 #if defined (__MINGW32__)
345 if (isatty (tty))
346 return (_kbhit ());
347 #endif
348
349 return 0;
350 }
351
352 int
353 _rl_input_queued (t)
354 int t;
355 {
356 int old_timeout, r;
357
358 old_timeout = rl_set_keyboard_input_timeout (t);
359 r = _rl_input_available ();
360 rl_set_keyboard_input_timeout (old_timeout);
361 return r;
362 }
363
364 void
365 _rl_insert_typein (c)
366 int c;
367 {
368 int key, t, i;
369 char *string;
370
371 i = key = 0;
372 string = (char *)xmalloc (ibuffer_len + 1);
373 string[i++] = (char) c;
374
375 while ((t = rl_get_char (&key)) &&
376 _rl_keymap[key].type == ISFUNC &&
377 _rl_keymap[key].function == rl_insert)
378 string[i++] = key;
379
380 if (t)
381 _rl_unget_char (key);
382
383 string[i] = '\0';
384 rl_insert_text (string);
385 xfree (string);
386 }
387
388 /* Add KEY to the buffer of characters to be read. Returns 1 if the
389 character was stuffed correctly; 0 otherwise. */
390 int
391 rl_stuff_char (key)
392 int key;
393 {
394 if (ibuffer_space () == 0)
395 return 0;
396
397 if (key == EOF)
398 {
399 key = NEWLINE;
400 rl_pending_input = EOF;
401 RL_SETSTATE (RL_STATE_INPUTPENDING);
402 }
403 ibuffer[push_index++] = key;
404 #if 0
405 if (push_index >= ibuffer_len)
406 #else
407 if (push_index > ibuffer_len)
408 #endif
409 push_index = 0;
410
411 return 1;
412 }
413
414 /* Make C be the next command to be executed. */
415 int
416 rl_execute_next (c)
417 int c;
418 {
419 rl_pending_input = c;
420 RL_SETSTATE (RL_STATE_INPUTPENDING);
421 return 0;
422 }
423
424 /* Clear any pending input pushed with rl_execute_next() */
425 int
426 rl_clear_pending_input ()
427 {
428 rl_pending_input = 0;
429 RL_UNSETSTATE (RL_STATE_INPUTPENDING);
430 return 0;
431 }
432
433 /* **************************************************************** */
434 /* */
435 /* Character Input */
436 /* */
437 /* **************************************************************** */
438
439 /* Read a key, including pending input. */
440 int
441 rl_read_key ()
442 {
443 int c, r;
444
445 if (rl_pending_input)
446 {
447 c = rl_pending_input;
448 rl_clear_pending_input ();
449 }
450 else
451 {
452 /* If input is coming from a macro, then use that. */
453 if (c = _rl_next_macro_key ())
454 return (c);
455
456 /* If the user has an event function, then call it periodically. */
457 if (rl_event_hook)
458 {
459 while (rl_event_hook)
460 {
461 if (rl_get_char (&c) != 0)
462 break;
463
464 if ((r = rl_gather_tyi ()) < 0) /* XXX - EIO */
465 {
466 rl_done = 1;
467 return ('\n');
468 }
469 else if (r > 0) /* read something */
470 continue;
471
472 RL_CHECK_SIGNALS ();
473 if (rl_done) /* XXX - experimental */
474 return ('\n');
475 (*rl_event_hook) ();
476 }
477 }
478 else
479 {
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); */
483 RL_CHECK_SIGNALS ();
484 }
485 }
486
487 return (c);
488 }
489
490 int
491 rl_getc (stream)
492 FILE *stream;
493 {
494 int result;
495 unsigned char c;
496 #if defined (HAVE_PSELECT)
497 sigset_t empty_set;
498 fd_set readfds;
499 #endif
500
501 while (1)
502 {
503 RL_CHECK_SIGNALS ();
504
505 /* We know at this point that _rl_caught_signal == 0 */
506
507 #if defined (__MINGW32__)
508 if (isatty (fileno (stream)))
509 return (_getch ()); /* "There is no error return." */
510 #endif
511 result = 0;
512 #if defined (HAVE_PSELECT)
513 sigemptyset (&empty_set);
514 FD_ZERO (&readfds);
515 FD_SET (fileno (stream), &readfds);
516 result = pselect (fileno (stream) + 1, &readfds, NULL, NULL, NULL, &empty_set);
517 #endif
518 if (result >= 0)
519 result = read (fileno (stream), &c, sizeof (unsigned char));
520
521 if (result == sizeof (unsigned char))
522 return (c);
523
524 /* If zero characters are returned, then the file that we are
525 reading from is empty! Return EOF in that case. */
526 if (result == 0)
527 return (EOF);
528
529 #if defined (__BEOS__)
530 if (errno == EINTR)
531 continue;
532 #endif
533
534 #if defined (EWOULDBLOCK)
535 # define X_EWOULDBLOCK EWOULDBLOCK
536 #else
537 # define X_EWOULDBLOCK -99
538 #endif
539
540 #if defined (EAGAIN)
541 # define X_EAGAIN EAGAIN
542 #else
543 # define X_EAGAIN -99
544 #endif
545
546 if (errno == X_EWOULDBLOCK || errno == X_EAGAIN)
547 {
548 if (sh_unset_nodelay_mode (fileno (stream)) < 0)
549 return (EOF);
550 continue;
551 }
552
553 #undef X_EWOULDBLOCK
554 #undef X_EAGAIN
555
556 /* fprintf(stderr, "rl_getc: result = %d errno = %d\n", result, errno); */
557
558 handle_error:
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. */
565 if (errno != EINTR)
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)
572 RL_CHECK_SIGNALS ();
573 /* non-keyboard-generated signals of interest */
574 else if (_rl_caught_signal == SIGWINCH)
575 RL_CHECK_SIGNALS ();
576 else if (_rl_caught_signal == SIGALRM
577 #if defined (SIGVTALRM)
578 || _rl_caught_signal == SIGVTALRM
579 #endif
580 )
581 RL_CHECK_SIGNALS ();
582
583 if (rl_signal_event_hook)
584 (*rl_signal_event_hook) ();
585 }
586 }
587
588 #if defined (HANDLE_MULTIBYTE)
589 /* read multibyte char */
590 int
591 _rl_read_mbchar (mbchar, size)
592 char *mbchar;
593 int size;
594 {
595 int mb_len, c;
596 size_t mbchar_bytes_length;
597 wchar_t wc;
598 mbstate_t ps, ps_back;
599
600 memset(&ps, 0, sizeof (mbstate_t));
601 memset(&ps_back, 0, sizeof (mbstate_t));
602
603 mb_len = 0;
604 while (mb_len < size)
605 {
606 RL_SETSTATE(RL_STATE_MOREINPUT);
607 c = rl_read_key ();
608 RL_UNSETSTATE(RL_STATE_MOREINPUT);
609
610 if (c < 0)
611 break;
612
613 mbchar[mb_len++] = c;
614
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))
619 {
620 /* shorted bytes */
621 ps = ps_back;
622 continue;
623 }
624 else if (mbchar_bytes_length == 0)
625 {
626 mbchar[0] = '\0'; /* null wide character */
627 mb_len = 1;
628 break;
629 }
630 else if (mbchar_bytes_length > (size_t)(0))
631 break;
632 }
633
634 return mb_len;
635 }
636
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. */
641 int
642 _rl_read_mbstring (first, mb, mlen)
643 int first;
644 char *mb;
645 int mlen;
646 {
647 int i, c;
648 mbstate_t ps;
649
650 c = first;
651 memset (mb, 0, mlen);
652 for (i = 0; c >= 0 && i < mlen; i++)
653 {
654 mb[i] = (char)c;
655 memset (&ps, 0, sizeof (mbstate_t));
656 if (_rl_get_char_len (mb, &ps) == -2)
657 {
658 /* Read more for multibyte character */
659 RL_SETSTATE (RL_STATE_MOREINPUT);
660 c = rl_read_key ();
661 RL_UNSETSTATE (RL_STATE_MOREINPUT);
662 }
663 else
664 break;
665 }
666 return c;
667 }
668 #endif /* HANDLE_MULTIBYTE */