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