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