input.c revision 136644
121308Sache/* input.c -- character input functions for readline. */
221308Sache
321308Sache/* Copyright (C) 1994 Free Software Foundation, Inc.
421308Sache
521308Sache   This file is part of the GNU Readline Library, a library for
621308Sache   reading lines of text with interactive input and history editing.
721308Sache
821308Sache   The GNU Readline Library is free software; you can redistribute it
921308Sache   and/or modify it under the terms of the GNU General Public License
1021308Sache   as published by the Free Software Foundation; either version 2, or
1121308Sache   (at your option) any later version.
1221308Sache
1321308Sache   The GNU Readline Library is distributed in the hope that it will be
1421308Sache   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
1521308Sache   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1621308Sache   GNU General Public License for more details.
1721308Sache
1821308Sache   The GNU General Public License is often shipped with GNU software, and
1921308Sache   is generally kept in a file called COPYING or LICENSE.  If you do not
2021308Sache   have a copy of the license, write to the Free Software Foundation,
2158310Sache   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
2221308Sache#define READLINE_LIBRARY
2321308Sache
24136644Sache#if defined (__TANDEM)
25136644Sache#  include <floss.h>
26136644Sache#endif
27136644Sache
2821308Sache#if defined (HAVE_CONFIG_H)
2921308Sache#  include <config.h>
3021308Sache#endif
3121308Sache
3221308Sache#include <sys/types.h>
3321308Sache#include <fcntl.h>
3421308Sache#if defined (HAVE_SYS_FILE_H)
3521308Sache#  include <sys/file.h>
3621308Sache#endif /* HAVE_SYS_FILE_H */
3721308Sache
3821308Sache#if defined (HAVE_UNISTD_H)
3921308Sache#  include <unistd.h>
4021308Sache#endif /* HAVE_UNISTD_H */
4121308Sache
4221308Sache#if defined (HAVE_STDLIB_H)
4321308Sache#  include <stdlib.h>
4421308Sache#else
4521308Sache#  include "ansi_stdlib.h"
4621308Sache#endif /* HAVE_STDLIB_H */
4721308Sache
4821308Sache#if defined (HAVE_SELECT)
4921308Sache#  if !defined (HAVE_SYS_SELECT_H) || !defined (M_UNIX)
5021308Sache#    include <sys/time.h>
5121308Sache#  endif
5221308Sache#endif /* HAVE_SELECT */
5321308Sache#if defined (HAVE_SYS_SELECT_H)
5421308Sache#  include <sys/select.h>
5521308Sache#endif
5621308Sache
5721308Sache#if defined (FIONREAD_IN_SYS_IOCTL)
5821308Sache#  include <sys/ioctl.h>
5921308Sache#endif
6021308Sache
6121308Sache#include <stdio.h>
6221308Sache#include <errno.h>
6321308Sache
6421308Sache#if !defined (errno)
6521308Sacheextern int errno;
6621308Sache#endif /* !errno */
6721308Sache
6821308Sache/* System-specific feature definitions and include files. */
6921308Sache#include "rldefs.h"
70119610Sache#include "rlmbutil.h"
7121308Sache
7221308Sache/* Some standard library routines. */
7321308Sache#include "readline.h"
7421308Sache
7558310Sache#include "rlprivate.h"
7658310Sache#include "rlshell.h"
7758310Sache#include "xmalloc.h"
7858310Sache
7921308Sache/* What kind of non-blocking I/O do we have? */
8021308Sache#if !defined (O_NDELAY) && defined (O_NONBLOCK)
8121308Sache#  define O_NDELAY O_NONBLOCK	/* Posix style */
8221308Sache#endif
8321308Sache
8421308Sache/* Non-null means it is a pointer to a function to run while waiting for
8521308Sache   character input. */
8675406Sacherl_hook_func_t *rl_event_hook = (rl_hook_func_t *)NULL;
8721308Sache
8875406Sacherl_getc_func_t *rl_getc_function = rl_getc;
8921308Sache
9075406Sachestatic int _keyboard_input_timeout = 100000;		/* 0.1 seconds; it's in usec */
9175406Sache
92119610Sachestatic int ibuffer_space PARAMS((void));
93119610Sachestatic int rl_get_char PARAMS((int *));
94119610Sachestatic int rl_gather_tyi PARAMS((void));
95119610Sache
9621308Sache/* **************************************************************** */
9721308Sache/*								    */
9821308Sache/*			Character Input Buffering       	    */
9921308Sache/*								    */
10021308Sache/* **************************************************************** */
10121308Sache
10221308Sachestatic int pop_index, push_index;
10321308Sachestatic unsigned char ibuffer[512];
10421308Sachestatic int ibuffer_len = sizeof (ibuffer) - 1;
10521308Sache
10621308Sache#define any_typein (push_index != pop_index)
10721308Sache
10821308Sacheint
10921308Sache_rl_any_typein ()
11021308Sache{
11121308Sache  return any_typein;
11221308Sache}
11321308Sache
11447558Sache/* Return the amount of space available in the buffer for stuffing
11547558Sache   characters. */
11621308Sachestatic int
11721308Sacheibuffer_space ()
11821308Sache{
11921308Sache  if (pop_index > push_index)
12047558Sache    return (pop_index - push_index - 1);
12121308Sache  else
12221308Sache    return (ibuffer_len - (push_index - pop_index));
12321308Sache}
12421308Sache
12521308Sache/* Get a key from the buffer of characters to be read.
12621308Sache   Return the key in KEY.
12721308Sache   Result is KEY if there was a key, or 0 if there wasn't. */
12821308Sachestatic int
12921308Sacherl_get_char (key)
13021308Sache     int *key;
13121308Sache{
13221308Sache  if (push_index == pop_index)
13321308Sache    return (0);
13421308Sache
13521308Sache  *key = ibuffer[pop_index++];
13621308Sache
13721308Sache  if (pop_index >= ibuffer_len)
13821308Sache    pop_index = 0;
13921308Sache
14021308Sache  return (1);
14121308Sache}
14221308Sache
14321308Sache/* Stuff KEY into the *front* of the input buffer.
14421308Sache   Returns non-zero if successful, zero if there is
14521308Sache   no space left in the buffer. */
146119610Sacheint
147119610Sache_rl_unget_char (key)
14821308Sache     int key;
14921308Sache{
15021308Sache  if (ibuffer_space ())
15121308Sache    {
15221308Sache      pop_index--;
15321308Sache      if (pop_index < 0)
15421308Sache	pop_index = ibuffer_len - 1;
15521308Sache      ibuffer[pop_index] = key;
15621308Sache      return (1);
15721308Sache    }
15821308Sache  return (0);
15921308Sache}
16021308Sache
161136644Sacheint
162136644Sache_rl_pushed_input_available ()
163136644Sache{
164136644Sache  return (push_index != pop_index);
165136644Sache}
166136644Sache
167119610Sache/* If a character is available to be read, then read it and stuff it into
168119610Sache   IBUFFER.  Otherwise, just return.  Returns number of characters read
169119610Sache   (0 if none available) and -1 on error (EIO). */
170119610Sachestatic int
17121308Sacherl_gather_tyi ()
17221308Sache{
17321308Sache  int tty;
17421308Sache  register int tem, result;
175136644Sache  int chars_avail, k;
17621308Sache  char input;
17721308Sache#if defined(HAVE_SELECT)
17821308Sache  fd_set readfds, exceptfds;
17921308Sache  struct timeval timeout;
18021308Sache#endif
18121308Sache
18221308Sache  tty = fileno (rl_instream);
18321308Sache
18421308Sache#if defined (HAVE_SELECT)
18521308Sache  FD_ZERO (&readfds);
18621308Sache  FD_ZERO (&exceptfds);
18721308Sache  FD_SET (tty, &readfds);
18821308Sache  FD_SET (tty, &exceptfds);
18921308Sache  timeout.tv_sec = 0;
19075406Sache  timeout.tv_usec = _keyboard_input_timeout;
191119610Sache  result = select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout);
192119610Sache  if (result <= 0)
193119610Sache    return 0;	/* Nothing to read. */
19421308Sache#endif
19521308Sache
19621308Sache  result = -1;
19721308Sache#if defined (FIONREAD)
198119610Sache  errno = 0;
19921308Sache  result = ioctl (tty, FIONREAD, &chars_avail);
200119610Sache  if (result == -1 && errno == EIO)
201119610Sache    return -1;
20221308Sache#endif
20321308Sache
20421308Sache#if defined (O_NDELAY)
20521308Sache  if (result == -1)
20621308Sache    {
20721308Sache      tem = fcntl (tty, F_GETFL, 0);
20821308Sache
20921308Sache      fcntl (tty, F_SETFL, (tem | O_NDELAY));
21021308Sache      chars_avail = read (tty, &input, 1);
21121308Sache
21221308Sache      fcntl (tty, F_SETFL, tem);
21321308Sache      if (chars_avail == -1 && errno == EAGAIN)
214119610Sache	return 0;
215136644Sache      if (chars_avail == 0)	/* EOF */
216136644Sache	{
217136644Sache	  rl_stuff_char (EOF);
218136644Sache	  return (0);
219136644Sache	}
22021308Sache    }
22121308Sache#endif /* O_NDELAY */
22221308Sache
22321308Sache  /* If there's nothing available, don't waste time trying to read
22421308Sache     something. */
22521308Sache  if (chars_avail <= 0)
226119610Sache    return 0;
22721308Sache
22821308Sache  tem = ibuffer_space ();
22921308Sache
23021308Sache  if (chars_avail > tem)
23121308Sache    chars_avail = tem;
23221308Sache
23321308Sache  /* One cannot read all of the available input.  I can only read a single
23421308Sache     character at a time, or else programs which require input can be
23521308Sache     thwarted.  If the buffer is larger than one character, I lose.
23621308Sache     Damn! */
23721308Sache  if (tem < ibuffer_len)
23821308Sache    chars_avail = 0;
23921308Sache
24021308Sache  if (result != -1)
24121308Sache    {
24221308Sache      while (chars_avail--)
243136644Sache	{
244136644Sache	  k = (*rl_getc_function) (rl_instream);
245136644Sache	  rl_stuff_char (k);
246136644Sache	  if (k == NEWLINE || k == RETURN)
247136644Sache	    break;
248136644Sache	}
24921308Sache    }
25021308Sache  else
25121308Sache    {
25221308Sache      if (chars_avail)
25321308Sache	rl_stuff_char (input);
25421308Sache    }
255119610Sache
256119610Sache  return 1;
25721308Sache}
25821308Sache
25975406Sacheint
26075406Sacherl_set_keyboard_input_timeout (u)
26175406Sache     int u;
26275406Sache{
26375406Sache  int o;
26475406Sache
26575406Sache  o = _keyboard_input_timeout;
26675406Sache  if (u > 0)
26775406Sache    _keyboard_input_timeout = u;
26875406Sache  return (o);
26975406Sache}
27075406Sache
27121308Sache/* Is there input available to be read on the readline input file
272119610Sache   descriptor?  Only works if the system has select(2) or FIONREAD.
273119610Sache   Uses the value of _keyboard_input_timeout as the timeout; if another
274119610Sache   readline function wants to specify a timeout and not leave it up to
275119610Sache   the user, it should use _rl_input_queued(timeout_value_in_microseconds)
276119610Sache   instead. */
27721308Sacheint
27821308Sache_rl_input_available ()
27921308Sache{
28021308Sache#if defined(HAVE_SELECT)
28121308Sache  fd_set readfds, exceptfds;
28221308Sache  struct timeval timeout;
28321308Sache#endif
284119610Sache#if !defined (HAVE_SELECT) && defined(FIONREAD)
28521308Sache  int chars_avail;
28621308Sache#endif
28721308Sache  int tty;
28821308Sache
28921308Sache  tty = fileno (rl_instream);
29021308Sache
29121308Sache#if defined (HAVE_SELECT)
29221308Sache  FD_ZERO (&readfds);
29321308Sache  FD_ZERO (&exceptfds);
29421308Sache  FD_SET (tty, &readfds);
29521308Sache  FD_SET (tty, &exceptfds);
29621308Sache  timeout.tv_sec = 0;
29775406Sache  timeout.tv_usec = _keyboard_input_timeout;
29821308Sache  return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0);
299119610Sache#else
30021308Sache
30121308Sache#if defined (FIONREAD)
30221308Sache  if (ioctl (tty, FIONREAD, &chars_avail) == 0)
30321308Sache    return (chars_avail);
30421308Sache#endif
30521308Sache
306119610Sache#endif
307119610Sache
30821308Sache  return 0;
30921308Sache}
31021308Sache
311119610Sacheint
312119610Sache_rl_input_queued (t)
313119610Sache     int t;
314119610Sache{
315119610Sache  int old_timeout, r;
316119610Sache
317119610Sache  old_timeout = rl_set_keyboard_input_timeout (t);
318119610Sache  r = _rl_input_available ();
319119610Sache  rl_set_keyboard_input_timeout (old_timeout);
320119610Sache  return r;
321119610Sache}
322119610Sache
32321308Sachevoid
32421308Sache_rl_insert_typein (c)
32521308Sache     int c;
32621308Sache{
32721308Sache  int key, t, i;
32821308Sache  char *string;
32921308Sache
33021308Sache  i = key = 0;
331119610Sache  string = (char *)xmalloc (ibuffer_len + 1);
33221308Sache  string[i++] = (char) c;
33321308Sache
33421308Sache  while ((t = rl_get_char (&key)) &&
33521308Sache	 _rl_keymap[key].type == ISFUNC &&
33621308Sache	 _rl_keymap[key].function == rl_insert)
33721308Sache    string[i++] = key;
33821308Sache
33921308Sache  if (t)
340119610Sache    _rl_unget_char (key);
34121308Sache
34221308Sache  string[i] = '\0';
34321308Sache  rl_insert_text (string);
34421308Sache  free (string);
34521308Sache}
34621308Sache
34747558Sache/* Add KEY to the buffer of characters to be read.  Returns 1 if the
34847558Sache   character was stuffed correctly; 0 otherwise. */
34947558Sacheint
35047558Sacherl_stuff_char (key)
35147558Sache     int key;
35247558Sache{
35347558Sache  if (ibuffer_space () == 0)
35447558Sache    return 0;
35547558Sache
35647558Sache  if (key == EOF)
35747558Sache    {
35847558Sache      key = NEWLINE;
35947558Sache      rl_pending_input = EOF;
36075406Sache      RL_SETSTATE (RL_STATE_INPUTPENDING);
36147558Sache    }
36247558Sache  ibuffer[push_index++] = key;
36347558Sache  if (push_index >= ibuffer_len)
36447558Sache    push_index = 0;
36547558Sache
36647558Sache  return 1;
36747558Sache}
36847558Sache
36947558Sache/* Make C be the next command to be executed. */
37047558Sacheint
37147558Sacherl_execute_next (c)
37247558Sache     int c;
37347558Sache{
37447558Sache  rl_pending_input = c;
37575406Sache  RL_SETSTATE (RL_STATE_INPUTPENDING);
37647558Sache  return 0;
37747558Sache}
37847558Sache
37975406Sache/* Clear any pending input pushed with rl_execute_next() */
38075406Sacheint
38175406Sacherl_clear_pending_input ()
38275406Sache{
38375406Sache  rl_pending_input = 0;
38475406Sache  RL_UNSETSTATE (RL_STATE_INPUTPENDING);
38575406Sache  return 0;
38675406Sache}
38775406Sache
38821308Sache/* **************************************************************** */
38921308Sache/*								    */
39021308Sache/*			     Character Input			    */
39121308Sache/*								    */
39221308Sache/* **************************************************************** */
39321308Sache
39421308Sache/* Read a key, including pending input. */
39521308Sacheint
39621308Sacherl_read_key ()
39721308Sache{
39821308Sache  int c;
39921308Sache
40021308Sache  rl_key_sequence_length++;
40121308Sache
40221308Sache  if (rl_pending_input)
40321308Sache    {
40421308Sache      c = rl_pending_input;
40575406Sache      rl_clear_pending_input ();
40621308Sache    }
40721308Sache  else
40821308Sache    {
40921308Sache      /* If input is coming from a macro, then use that. */
41021308Sache      if (c = _rl_next_macro_key ())
41121308Sache	return (c);
41221308Sache
41321308Sache      /* If the user has an event function, then call it periodically. */
41421308Sache      if (rl_event_hook)
41521308Sache	{
41621308Sache	  while (rl_event_hook && rl_get_char (&c) == 0)
41721308Sache	    {
41821308Sache	      (*rl_event_hook) ();
41975406Sache	      if (rl_done)		/* XXX - experimental */
42075406Sache		return ('\n');
421119610Sache	      if (rl_gather_tyi () < 0)	/* XXX - EIO */
422119610Sache		{
423119610Sache		  rl_done = 1;
424119610Sache		  return ('\n');
425119610Sache		}
42621308Sache	    }
42721308Sache	}
42821308Sache      else
42921308Sache	{
43021308Sache	  if (rl_get_char (&c) == 0)
43121308Sache	    c = (*rl_getc_function) (rl_instream);
43221308Sache	}
43321308Sache    }
43421308Sache
43521308Sache  return (c);
43621308Sache}
43721308Sache
43821308Sacheint
43921308Sacherl_getc (stream)
44021308Sache     FILE *stream;
44121308Sache{
44258310Sache  int result;
44321308Sache  unsigned char c;
44421308Sache
44521308Sache  while (1)
44621308Sache    {
44721308Sache      result = read (fileno (stream), &c, sizeof (unsigned char));
44821308Sache
44921308Sache      if (result == sizeof (unsigned char))
45021308Sache	return (c);
45121308Sache
45221308Sache      /* If zero characters are returned, then the file that we are
45321308Sache	 reading from is empty!  Return EOF in that case. */
45421308Sache      if (result == 0)
45521308Sache	return (EOF);
45621308Sache
45747558Sache#if defined (__BEOS__)
45847558Sache      if (errno == EINTR)
45947558Sache	continue;
46047558Sache#endif
46147558Sache
46221308Sache#if defined (EWOULDBLOCK)
46358310Sache#  define X_EWOULDBLOCK EWOULDBLOCK
46458310Sache#else
46558310Sache#  define X_EWOULDBLOCK -99
46658310Sache#endif
46758310Sache
46858310Sache#if defined (EAGAIN)
46958310Sache#  define X_EAGAIN EAGAIN
47058310Sache#else
47158310Sache#  define X_EAGAIN -99
47258310Sache#endif
47358310Sache
47458310Sache      if (errno == X_EWOULDBLOCK || errno == X_EAGAIN)
47521308Sache	{
47675406Sache	  if (sh_unset_nodelay_mode (fileno (stream)) < 0)
47721308Sache	    return (EOF);
47821308Sache	  continue;
47921308Sache	}
48021308Sache
48158310Sache#undef X_EWOULDBLOCK
48258310Sache#undef X_EAGAIN
48321308Sache
48421308Sache      /* If the error that we received was SIGINT, then try again,
48521308Sache	 this is simply an interrupted system call to read ().
48621308Sache	 Otherwise, some error ocurred, also signifying EOF. */
48721308Sache      if (errno != EINTR)
48821308Sache	return (EOF);
48921308Sache    }
49021308Sache}
491119610Sache
492119610Sache#if defined (HANDLE_MULTIBYTE)
493119610Sache/* read multibyte char */
494119610Sacheint
495119610Sache_rl_read_mbchar (mbchar, size)
496119610Sache     char *mbchar;
497119610Sache     int size;
498119610Sache{
499119610Sache  int mb_len = 0;
500119610Sache  size_t mbchar_bytes_length;
501119610Sache  wchar_t wc;
502119610Sache  mbstate_t ps, ps_back;
503119610Sache
504119610Sache  memset(&ps, 0, sizeof (mbstate_t));
505119610Sache  memset(&ps_back, 0, sizeof (mbstate_t));
506119610Sache
507119610Sache  while (mb_len < size)
508119610Sache    {
509119610Sache      RL_SETSTATE(RL_STATE_MOREINPUT);
510119610Sache      mbchar[mb_len++] = rl_read_key ();
511119610Sache      RL_UNSETSTATE(RL_STATE_MOREINPUT);
512119610Sache
513119610Sache      mbchar_bytes_length = mbrtowc (&wc, mbchar, mb_len, &ps);
514119610Sache      if (mbchar_bytes_length == (size_t)(-1))
515119610Sache	break;		/* invalid byte sequence for the current locale */
516119610Sache      else if (mbchar_bytes_length == (size_t)(-2))
517119610Sache	{
518119610Sache	  /* shorted bytes */
519119610Sache	  ps = ps_back;
520119610Sache	  continue;
521119610Sache	}
522119610Sache      else if (mbchar_bytes_length > (size_t)(0))
523119610Sache	break;
524119610Sache    }
525119610Sache
526119610Sache  return mb_len;
527119610Sache}
528119610Sache
529119610Sache/* Read a multibyte-character string whose first character is FIRST into
530119610Sache   the buffer MB of length MBLEN.  Returns the last character read, which
531119610Sache   may be FIRST.  Used by the search functions, among others.  Very similar
532119610Sache   to _rl_read_mbchar. */
533119610Sacheint
534119610Sache_rl_read_mbstring (first, mb, mblen)
535119610Sache     int first;
536119610Sache     char *mb;
537119610Sache     int mblen;
538119610Sache{
539119610Sache  int i, c;
540119610Sache  mbstate_t ps;
541119610Sache
542119610Sache  c = first;
543119610Sache  memset (mb, 0, mblen);
544119610Sache  for (i = 0; i < mblen; i++)
545119610Sache    {
546119610Sache      mb[i] = (char)c;
547119610Sache      memset (&ps, 0, sizeof (mbstate_t));
548119610Sache      if (_rl_get_char_len (mb, &ps) == -2)
549119610Sache	{
550119610Sache	  /* Read more for multibyte character */
551119610Sache	  RL_SETSTATE (RL_STATE_MOREINPUT);
552119610Sache	  c = rl_read_key ();
553119610Sache	  RL_UNSETSTATE (RL_STATE_MOREINPUT);
554119610Sache	}
555119610Sache      else
556119610Sache	break;
557119610Sache    }
558119610Sache  return c;
559119610Sache}
560119610Sache#endif /* HANDLE_MULTIBYTE */
561