]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/libreadline/input.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / contrib / libreadline / input.c
1 /* input.c -- character input functions for readline. */
2
3 /* Copyright (C) 1994-2005 Free Software Foundation, Inc.
4
5    This file is part of the GNU Readline Library, a library for
6    reading lines of text with interactive input and history editing.
7
8    The GNU Readline Library is free software; you can redistribute it
9    and/or modify it under the terms of the GNU General Public License
10    as published by the Free Software Foundation; either version 2, or
11    (at your option) any later version.
12
13    The GNU Readline Library is distributed in the hope that it will be
14    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    The GNU General Public License is often shipped with GNU software, and
19    is generally kept in a file called COPYING or LICENSE.  If you do not
20    have a copy of the license, write to the Free Software Foundation,
21    59 Temple Place, Suite 330, Boston, MA 02111 USA. */
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
137   if (pop_index >= ibuffer_len)
138     pop_index = 0;
139
140   return (1);
141 }
142
143 /* Stuff KEY into the *front* of the input buffer.
144    Returns non-zero if successful, zero if there is
145    no space left in the buffer. */
146 int
147 _rl_unget_char (key)
148      int key;
149 {
150   if (ibuffer_space ())
151     {
152       pop_index--;
153       if (pop_index < 0)
154         pop_index = ibuffer_len - 1;
155       ibuffer[pop_index] = key;
156       return (1);
157     }
158   return (0);
159 }
160
161 int
162 _rl_pushed_input_available ()
163 {
164   return (push_index != pop_index);
165 }
166
167 /* If a character is available to be read, then read it and stuff it into
168    IBUFFER.  Otherwise, just return.  Returns number of characters read
169    (0 if none available) and -1 on error (EIO). */
170 static int
171 rl_gather_tyi ()
172 {
173   int tty;
174   register int tem, result;
175   int chars_avail, k;
176   char input;
177 #if defined(HAVE_SELECT)
178   fd_set readfds, exceptfds;
179   struct timeval timeout;
180 #endif
181
182   chars_avail = 0;
183   tty = fileno (rl_instream);
184
185 #if defined (HAVE_SELECT)
186   FD_ZERO (&readfds);
187   FD_ZERO (&exceptfds);
188   FD_SET (tty, &readfds);
189   FD_SET (tty, &exceptfds);
190   timeout.tv_sec = 0;
191   timeout.tv_usec = _keyboard_input_timeout;
192   result = select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout);
193   if (result <= 0)
194     return 0;   /* Nothing to read. */
195 #endif
196
197   result = -1;
198 #if defined (FIONREAD)
199   errno = 0;
200   result = ioctl (tty, FIONREAD, &chars_avail);
201   if (result == -1 && errno == EIO)
202     return -1;
203 #endif
204
205 #if defined (O_NDELAY)
206   if (result == -1)
207     {
208       tem = fcntl (tty, F_GETFL, 0);
209
210       fcntl (tty, F_SETFL, (tem | O_NDELAY));
211       chars_avail = read (tty, &input, 1);
212
213       fcntl (tty, F_SETFL, tem);
214       if (chars_avail == -1 && errno == EAGAIN)
215         return 0;
216       if (chars_avail == 0)     /* EOF */
217         {
218           rl_stuff_char (EOF);
219           return (0);
220         }
221     }
222 #endif /* O_NDELAY */
223
224 #if defined (__MINGW32__)
225   /* Use getch/_kbhit to check for available console input, in the same way
226      that we read it normally. */
227    chars_avail = isatty (tty) ? _kbhit () : 0;
228    result = 0;
229 #endif
230
231   /* If there's nothing available, don't waste time trying to read
232      something. */
233   if (chars_avail <= 0)
234     return 0;
235
236   tem = ibuffer_space ();
237
238   if (chars_avail > tem)
239     chars_avail = tem;
240
241   /* One cannot read all of the available input.  I can only read a single
242      character at a time, or else programs which require input can be
243      thwarted.  If the buffer is larger than one character, I lose.
244      Damn! */
245   if (tem < ibuffer_len)
246     chars_avail = 0;
247
248   if (result != -1)
249     {
250       while (chars_avail--)
251         {
252           k = (*rl_getc_function) (rl_instream);
253           rl_stuff_char (k);
254           if (k == NEWLINE || k == RETURN)
255             break;
256         }
257     }
258   else
259     {
260       if (chars_avail)
261         rl_stuff_char (input);
262     }
263
264   return 1;
265 }
266
267 int
268 rl_set_keyboard_input_timeout (u)
269      int u;
270 {
271   int o;
272
273   o = _keyboard_input_timeout;
274   if (u >= 0)
275     _keyboard_input_timeout = u;
276   return (o);
277 }
278
279 /* Is there input available to be read on the readline input file
280    descriptor?  Only works if the system has select(2) or FIONREAD.
281    Uses the value of _keyboard_input_timeout as the timeout; if another
282    readline function wants to specify a timeout and not leave it up to
283    the user, it should use _rl_input_queued(timeout_value_in_microseconds)
284    instead. */
285 int
286 _rl_input_available ()
287 {
288 #if defined(HAVE_SELECT)
289   fd_set readfds, exceptfds;
290   struct timeval timeout;
291 #endif
292 #if !defined (HAVE_SELECT) && defined(FIONREAD)
293   int chars_avail;
294 #endif
295   int tty;
296
297   tty = fileno (rl_instream);
298
299 #if defined (HAVE_SELECT)
300   FD_ZERO (&readfds);
301   FD_ZERO (&exceptfds);
302   FD_SET (tty, &readfds);
303   FD_SET (tty, &exceptfds);
304   timeout.tv_sec = 0;
305   timeout.tv_usec = _keyboard_input_timeout;
306   return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0);
307 #else
308
309 #if defined (FIONREAD)
310   if (ioctl (tty, FIONREAD, &chars_avail) == 0)
311     return (chars_avail);
312 #endif
313
314 #endif
315
316 #if defined (__MINGW32__)
317   if (isatty (tty))
318     return (_kbhit ());
319 #endif
320
321   return 0;
322 }
323
324 int
325 _rl_input_queued (t)
326      int t;
327 {
328   int old_timeout, r;
329
330   old_timeout = rl_set_keyboard_input_timeout (t);
331   r = _rl_input_available ();
332   rl_set_keyboard_input_timeout (old_timeout);
333   return r;
334 }
335
336 void
337 _rl_insert_typein (c)
338      int c;     
339 {       
340   int key, t, i;
341   char *string;
342
343   i = key = 0;
344   string = (char *)xmalloc (ibuffer_len + 1);
345   string[i++] = (char) c;
346
347   while ((t = rl_get_char (&key)) &&
348          _rl_keymap[key].type == ISFUNC &&
349          _rl_keymap[key].function == rl_insert)
350     string[i++] = key;
351
352   if (t)
353     _rl_unget_char (key);
354
355   string[i] = '\0';
356   rl_insert_text (string);
357   free (string);
358 }
359
360 /* Add KEY to the buffer of characters to be read.  Returns 1 if the
361    character was stuffed correctly; 0 otherwise. */
362 int
363 rl_stuff_char (key)
364      int key;
365 {
366   if (ibuffer_space () == 0)
367     return 0;
368
369   if (key == EOF)
370     {
371       key = NEWLINE;
372       rl_pending_input = EOF;
373       RL_SETSTATE (RL_STATE_INPUTPENDING);
374     }
375   ibuffer[push_index++] = key;
376   if (push_index >= ibuffer_len)
377     push_index = 0;
378
379   return 1;
380 }
381
382 /* Make C be the next command to be executed. */
383 int
384 rl_execute_next (c)
385      int c;
386 {
387   rl_pending_input = c;
388   RL_SETSTATE (RL_STATE_INPUTPENDING);
389   return 0;
390 }
391
392 /* Clear any pending input pushed with rl_execute_next() */
393 int
394 rl_clear_pending_input ()
395 {
396   rl_pending_input = 0;
397   RL_UNSETSTATE (RL_STATE_INPUTPENDING);
398   return 0;
399 }
400
401 /* **************************************************************** */
402 /*                                                                  */
403 /*                           Character Input                        */
404 /*                                                                  */
405 /* **************************************************************** */
406
407 /* Read a key, including pending input. */
408 int
409 rl_read_key ()
410 {
411   int c;
412
413   rl_key_sequence_length++;
414
415   if (rl_pending_input)
416     {
417       c = rl_pending_input;
418       rl_clear_pending_input ();
419     }
420   else
421     {
422       /* If input is coming from a macro, then use that. */
423       if (c = _rl_next_macro_key ())
424         return (c);
425
426       /* If the user has an event function, then call it periodically. */
427       if (rl_event_hook)
428         {
429           while (rl_event_hook && rl_get_char (&c) == 0)
430             {
431               (*rl_event_hook) ();
432               if (rl_done)              /* XXX - experimental */
433                 return ('\n');
434               if (rl_gather_tyi () < 0) /* XXX - EIO */
435                 {
436                   rl_done = 1;
437                   return ('\n');
438                 }
439             }
440         }
441       else
442         {
443           if (rl_get_char (&c) == 0)
444             c = (*rl_getc_function) (rl_instream);
445         }
446     }
447
448   return (c);
449 }
450
451 int
452 rl_getc (stream)
453      FILE *stream;
454 {
455   int result;
456   unsigned char c;
457
458   while (1)
459     {
460 #if defined (__MINGW32__)
461       if (isatty (fileno (stream)))
462         return (getch ());
463 #endif
464       result = read (fileno (stream), &c, sizeof (unsigned char));
465
466       if (result == sizeof (unsigned char))
467         return (c);
468
469       /* If zero characters are returned, then the file that we are
470          reading from is empty!  Return EOF in that case. */
471       if (result == 0)
472         return (EOF);
473
474 #if defined (__BEOS__)
475       if (errno == EINTR)
476         continue;
477 #endif
478
479 #if defined (EWOULDBLOCK)
480 #  define X_EWOULDBLOCK EWOULDBLOCK
481 #else
482 #  define X_EWOULDBLOCK -99
483 #endif
484
485 #if defined (EAGAIN)
486 #  define X_EAGAIN EAGAIN
487 #else
488 #  define X_EAGAIN -99
489 #endif
490
491       if (errno == X_EWOULDBLOCK || errno == X_EAGAIN)
492         {
493           if (sh_unset_nodelay_mode (fileno (stream)) < 0)
494             return (EOF);
495           continue;
496         }
497
498 #undef X_EWOULDBLOCK
499 #undef X_EAGAIN
500
501       /* If the error that we received was SIGINT, then try again,
502          this is simply an interrupted system call to read ().
503          Otherwise, some error ocurred, also signifying EOF. */
504       if (errno != EINTR)
505         return (RL_ISSTATE (RL_STATE_READCMD) ? READERR : EOF);
506     }
507 }
508
509 #if defined (HANDLE_MULTIBYTE)
510 /* read multibyte char */
511 int
512 _rl_read_mbchar (mbchar, size)
513      char *mbchar;
514      int size;
515 {
516   int mb_len = 0;
517   size_t mbchar_bytes_length;
518   wchar_t wc;
519   mbstate_t ps, ps_back;
520
521   memset(&ps, 0, sizeof (mbstate_t));
522   memset(&ps_back, 0, sizeof (mbstate_t));
523   
524   while (mb_len < size)
525     {
526       RL_SETSTATE(RL_STATE_MOREINPUT);
527       mbchar[mb_len++] = rl_read_key ();
528       RL_UNSETSTATE(RL_STATE_MOREINPUT);
529
530       mbchar_bytes_length = mbrtowc (&wc, mbchar, mb_len, &ps);
531       if (mbchar_bytes_length == (size_t)(-1))
532         break;          /* invalid byte sequence for the current locale */
533       else if (mbchar_bytes_length == (size_t)(-2))
534         {
535           /* shorted bytes */
536           ps = ps_back;
537           continue;
538         } 
539       else if (mbchar_bytes_length == 0)
540         {
541           mbchar[0] = '\0';     /* null wide character */
542           mb_len = 1;
543           break;
544         }
545       else if (mbchar_bytes_length > (size_t)(0))
546         break;
547     }
548
549   return mb_len;
550 }
551
552 /* Read a multibyte-character string whose first character is FIRST into
553    the buffer MB of length MLEN.  Returns the last character read, which
554    may be FIRST.  Used by the search functions, among others.  Very similar
555    to _rl_read_mbchar. */
556 int
557 _rl_read_mbstring (first, mb, mlen)
558      int first;
559      char *mb;
560      int mlen;
561 {
562   int i, c;
563   mbstate_t ps;
564
565   c = first;
566   memset (mb, 0, mlen);
567   for (i = 0; i < mlen; i++)
568     {
569       mb[i] = (char)c;
570       memset (&ps, 0, sizeof (mbstate_t));
571       if (_rl_get_char_len (mb, &ps) == -2)
572         {
573           /* Read more for multibyte character */
574           RL_SETSTATE (RL_STATE_MOREINPUT);
575           c = rl_read_key ();
576           RL_UNSETSTATE (RL_STATE_MOREINPUT);
577         }
578       else
579         break;
580     }
581   return c;
582 }
583 #endif /* HANDLE_MULTIBYTE */