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