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