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