OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [branches/] [oc/] [gdb-5.0/] [readline/] [input.c] - Diff between revs 106 and 1765

Only display areas with differences | Details | Blame | View Log

Rev 106 Rev 1765
/* input.c -- character input functions for readline. */
/* input.c -- character input functions for readline. */
 
 
/* Copyright (C) 1994 Free Software Foundation, Inc.
/* Copyright (C) 1994 Free Software Foundation, Inc.
 
 
   This file is part of the GNU Readline Library, a library for
   This file is part of the GNU Readline Library, a library for
   reading lines of text with interactive input and history editing.
   reading lines of text with interactive input and history editing.
 
 
   The GNU Readline Library is free software; you can redistribute it
   The GNU Readline Library is free software; you can redistribute it
   and/or modify it under the terms of the GNU General Public License
   and/or modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2, or
   as published by the Free Software Foundation; either version 2, or
   (at your option) any later version.
   (at your option) any later version.
 
 
   The GNU Readline Library is distributed in the hope that it will be
   The GNU Readline Library is distributed in the hope that it will be
   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   GNU General Public License for more details.
 
 
   The GNU General Public License is often shipped with GNU software, and
   The GNU General Public License is often shipped with GNU software, and
   is generally kept in a file called COPYING or LICENSE.  If you do not
   is generally kept in a file called COPYING or LICENSE.  If you do not
   have a copy of the license, write to the Free Software Foundation,
   have a copy of the license, write to the Free Software Foundation,
   675 Mass Ave, Cambridge, MA 02139, USA. */
   675 Mass Ave, Cambridge, MA 02139, USA. */
#define READLINE_LIBRARY
#define READLINE_LIBRARY
 
 
#if defined (HAVE_CONFIG_H)
#if defined (HAVE_CONFIG_H)
#  include <config.h>
#  include <config.h>
#endif
#endif
 
 
#include <sys/types.h>
#include <sys/types.h>
#include <fcntl.h>
#include <fcntl.h>
#if defined (HAVE_SYS_FILE_H)
#if defined (HAVE_SYS_FILE_H)
#  include <sys/file.h>
#  include <sys/file.h>
#endif /* HAVE_SYS_FILE_H */
#endif /* HAVE_SYS_FILE_H */
 
 
#if defined (HAVE_UNISTD_H)
#if defined (HAVE_UNISTD_H)
#  include <unistd.h>
#  include <unistd.h>
#endif /* HAVE_UNISTD_H */
#endif /* HAVE_UNISTD_H */
 
 
#if defined (HAVE_STDLIB_H)
#if defined (HAVE_STDLIB_H)
#  include <stdlib.h>
#  include <stdlib.h>
#else
#else
#  include "ansi_stdlib.h"
#  include "ansi_stdlib.h"
#endif /* HAVE_STDLIB_H */
#endif /* HAVE_STDLIB_H */
 
 
#if defined (HAVE_SELECT)
#if defined (HAVE_SELECT)
#  if !defined (HAVE_SYS_SELECT_H) || !defined (M_UNIX)
#  if !defined (HAVE_SYS_SELECT_H) || !defined (M_UNIX)
#    include <sys/time.h>
#    include <sys/time.h>
#  endif
#  endif
#endif /* HAVE_SELECT */
#endif /* HAVE_SELECT */
#if defined (HAVE_SYS_SELECT_H)
#if defined (HAVE_SYS_SELECT_H)
#  include <sys/select.h>
#  include <sys/select.h>
#endif
#endif
 
 
#if defined (FIONREAD_IN_SYS_IOCTL)
#if defined (FIONREAD_IN_SYS_IOCTL)
#  include <sys/ioctl.h>
#  include <sys/ioctl.h>
#endif
#endif
 
 
#include <stdio.h>
#include <stdio.h>
#include <errno.h>
#include <errno.h>
 
 
#if !defined (errno)
#if !defined (errno)
extern int errno;
extern int errno;
#endif /* !errno */
#endif /* !errno */
 
 
/* System-specific feature definitions and include files. */
/* System-specific feature definitions and include files. */
#include "rldefs.h"
#include "rldefs.h"
 
 
/* Some standard library routines. */
/* Some standard library routines. */
#include "readline.h"
#include "readline.h"
 
 
/* What kind of non-blocking I/O do we have? */
/* What kind of non-blocking I/O do we have? */
#if !defined (O_NDELAY) && defined (O_NONBLOCK)
#if !defined (O_NDELAY) && defined (O_NONBLOCK)
#  define O_NDELAY O_NONBLOCK   /* Posix style */
#  define O_NDELAY O_NONBLOCK   /* Posix style */
#endif
#endif
 
 
/* Functions imported from other files in the library. */
/* Functions imported from other files in the library. */
extern char *xmalloc (), *xrealloc ();
extern char *xmalloc (), *xrealloc ();
 
 
/* Variables and functions from macro.c. */
/* Variables and functions from macro.c. */
extern void _rl_add_macro_char ();
extern void _rl_add_macro_char ();
extern void _rl_with_macro_input ();
extern void _rl_with_macro_input ();
extern int _rl_next_macro_key ();
extern int _rl_next_macro_key ();
extern int _rl_defining_kbd_macro;
extern int _rl_defining_kbd_macro;
 
 
#if defined (VI_MODE)
#if defined (VI_MODE)
extern void _rl_vi_set_last ();
extern void _rl_vi_set_last ();
extern int _rl_vi_textmod_command ();
extern int _rl_vi_textmod_command ();
#endif /* VI_MODE */
#endif /* VI_MODE */
 
 
extern FILE *rl_instream, *rl_outstream;
extern FILE *rl_instream, *rl_outstream;
extern Function *rl_last_func;
extern Function *rl_last_func;
extern int rl_key_sequence_length;
extern int rl_key_sequence_length;
extern int rl_pending_input;
extern int rl_pending_input;
extern int rl_editing_mode;
extern int rl_editing_mode;
 
 
extern Keymap _rl_keymap;
extern Keymap _rl_keymap;
 
 
extern int _rl_convert_meta_chars_to_ascii;
extern int _rl_convert_meta_chars_to_ascii;
 
 
#if defined (__GO32__) && !defined (HAVE_SELECT)
#if defined (__GO32__) && !defined (HAVE_SELECT)
#  include <pc.h>
#  include <pc.h>
#endif /* __GO32__ */
#endif /* __GO32__ */
 
 
/* Non-null means it is a pointer to a function to run while waiting for
/* Non-null means it is a pointer to a function to run while waiting for
   character input. */
   character input. */
Function *rl_event_hook = (Function *)NULL;
Function *rl_event_hook = (Function *)NULL;
 
 
Function *rl_getc_function = rl_getc;
Function *rl_getc_function = rl_getc;
 
 
/* **************************************************************** */
/* **************************************************************** */
/*                                                                  */
/*                                                                  */
/*                      Character Input Buffering                   */
/*                      Character Input Buffering                   */
/*                                                                  */
/*                                                                  */
/* **************************************************************** */
/* **************************************************************** */
 
 
static int pop_index, push_index;
static int pop_index, push_index;
static unsigned char ibuffer[512];
static unsigned char ibuffer[512];
static int ibuffer_len = sizeof (ibuffer) - 1;
static int ibuffer_len = sizeof (ibuffer) - 1;
 
 
#define any_typein (push_index != pop_index)
#define any_typein (push_index != pop_index)
 
 
int
int
_rl_any_typein ()
_rl_any_typein ()
{
{
  return any_typein;
  return any_typein;
}
}
 
 
/* Return the amount of space available in the buffer for stuffing
/* Return the amount of space available in the buffer for stuffing
   characters. */
   characters. */
static int
static int
ibuffer_space ()
ibuffer_space ()
{
{
  if (pop_index > push_index)
  if (pop_index > push_index)
    return (pop_index - push_index - 1);
    return (pop_index - push_index - 1);
  else
  else
    return (ibuffer_len - (push_index - pop_index));
    return (ibuffer_len - (push_index - pop_index));
}
}
 
 
/* Get a key from the buffer of characters to be read.
/* Get a key from the buffer of characters to be read.
   Return the key in KEY.
   Return the key in KEY.
   Result is KEY if there was a key, or 0 if there wasn't. */
   Result is KEY if there was a key, or 0 if there wasn't. */
static int
static int
rl_get_char (key)
rl_get_char (key)
     int *key;
     int *key;
{
{
  if (push_index == pop_index)
  if (push_index == pop_index)
    return (0);
    return (0);
 
 
  *key = ibuffer[pop_index++];
  *key = ibuffer[pop_index++];
 
 
  if (pop_index >= ibuffer_len)
  if (pop_index >= ibuffer_len)
    pop_index = 0;
    pop_index = 0;
 
 
  return (1);
  return (1);
}
}
 
 
/* Stuff KEY into the *front* of the input buffer.
/* Stuff KEY into the *front* of the input buffer.
   Returns non-zero if successful, zero if there is
   Returns non-zero if successful, zero if there is
   no space left in the buffer. */
   no space left in the buffer. */
static int
static int
rl_unget_char (key)
rl_unget_char (key)
     int key;
     int key;
{
{
  if (ibuffer_space ())
  if (ibuffer_space ())
    {
    {
      pop_index--;
      pop_index--;
      if (pop_index < 0)
      if (pop_index < 0)
        pop_index = ibuffer_len - 1;
        pop_index = ibuffer_len - 1;
      ibuffer[pop_index] = key;
      ibuffer[pop_index] = key;
      return (1);
      return (1);
    }
    }
  return (0);
  return (0);
}
}
 
 
/* If a character is available to be read, then read it
/* If a character is available to be read, then read it
   and stuff it into IBUFFER.  Otherwise, just return. */
   and stuff it into IBUFFER.  Otherwise, just return. */
static void
static void
rl_gather_tyi ()
rl_gather_tyi ()
{
{
#if defined (__GO32__) && !defined (HAVE_SELECT)
#if defined (__GO32__) && !defined (HAVE_SELECT)
  char input;
  char input;
 
 
  if (isatty (0) && kbhit () && ibuffer_space ())
  if (isatty (0) && kbhit () && ibuffer_space ())
    {
    {
      int i;
      int i;
      i = (*rl_getc_function) (rl_instream);
      i = (*rl_getc_function) (rl_instream);
      rl_stuff_char (i);
      rl_stuff_char (i);
    }
    }
#else /* !__GO32__ */
#else /* !__GO32__ */
 
 
  int tty;
  int tty;
  register int tem, result;
  register int tem, result;
  int chars_avail;
  int chars_avail;
  char input;
  char input;
#if defined(HAVE_SELECT)
#if defined(HAVE_SELECT)
  fd_set readfds, exceptfds;
  fd_set readfds, exceptfds;
  struct timeval timeout;
  struct timeval timeout;
#endif
#endif
 
 
  tty = fileno (rl_instream);
  tty = fileno (rl_instream);
 
 
#if defined (HAVE_SELECT)
#if defined (HAVE_SELECT)
  FD_ZERO (&readfds);
  FD_ZERO (&readfds);
  FD_ZERO (&exceptfds);
  FD_ZERO (&exceptfds);
  FD_SET (tty, &readfds);
  FD_SET (tty, &readfds);
  FD_SET (tty, &exceptfds);
  FD_SET (tty, &exceptfds);
  timeout.tv_sec = 0;
  timeout.tv_sec = 0;
  timeout.tv_usec = 100000;     /* 0.1 seconds */
  timeout.tv_usec = 100000;     /* 0.1 seconds */
  if (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) <= 0)
  if (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) <= 0)
    return;     /* Nothing to read. */
    return;     /* Nothing to read. */
#endif
#endif
 
 
  result = -1;
  result = -1;
#if defined (FIONREAD)
#if defined (FIONREAD)
  result = ioctl (tty, FIONREAD, &chars_avail);
  result = ioctl (tty, FIONREAD, &chars_avail);
#endif
#endif
 
 
#if defined (O_NDELAY)
#if defined (O_NDELAY)
  if (result == -1)
  if (result == -1)
    {
    {
      tem = fcntl (tty, F_GETFL, 0);
      tem = fcntl (tty, F_GETFL, 0);
 
 
      fcntl (tty, F_SETFL, (tem | O_NDELAY));
      fcntl (tty, F_SETFL, (tem | O_NDELAY));
      chars_avail = read (tty, &input, 1);
      chars_avail = read (tty, &input, 1);
 
 
      fcntl (tty, F_SETFL, tem);
      fcntl (tty, F_SETFL, tem);
      if (chars_avail == -1 && errno == EAGAIN)
      if (chars_avail == -1 && errno == EAGAIN)
        return;
        return;
    }
    }
#endif /* O_NDELAY */
#endif /* O_NDELAY */
 
 
  /* If there's nothing available, don't waste time trying to read
  /* If there's nothing available, don't waste time trying to read
     something. */
     something. */
  if (chars_avail <= 0)
  if (chars_avail <= 0)
    return;
    return;
 
 
  tem = ibuffer_space ();
  tem = ibuffer_space ();
 
 
  if (chars_avail > tem)
  if (chars_avail > tem)
    chars_avail = tem;
    chars_avail = tem;
 
 
  /* One cannot read all of the available input.  I can only read a single
  /* One cannot read all of the available input.  I can only read a single
     character at a time, or else programs which require input can be
     character at a time, or else programs which require input can be
     thwarted.  If the buffer is larger than one character, I lose.
     thwarted.  If the buffer is larger than one character, I lose.
     Damn! */
     Damn! */
  if (tem < ibuffer_len)
  if (tem < ibuffer_len)
    chars_avail = 0;
    chars_avail = 0;
 
 
  if (result != -1)
  if (result != -1)
    {
    {
      while (chars_avail--)
      while (chars_avail--)
        rl_stuff_char ((*rl_getc_function) (rl_instream));
        rl_stuff_char ((*rl_getc_function) (rl_instream));
    }
    }
  else
  else
    {
    {
      if (chars_avail)
      if (chars_avail)
        rl_stuff_char (input);
        rl_stuff_char (input);
    }
    }
#endif /* !__GO32__ */
#endif /* !__GO32__ */
}
}
 
 
/* Is there input available to be read on the readline input file
/* Is there input available to be read on the readline input file
   descriptor?  Only works if the system has select(2) or FIONREAD. */
   descriptor?  Only works if the system has select(2) or FIONREAD. */
int
int
_rl_input_available ()
_rl_input_available ()
{
{
#if defined(HAVE_SELECT)
#if defined(HAVE_SELECT)
  fd_set readfds, exceptfds;
  fd_set readfds, exceptfds;
  struct timeval timeout;
  struct timeval timeout;
#endif
#endif
#if defined(FIONREAD)
#if defined(FIONREAD)
  int chars_avail;
  int chars_avail;
#endif
#endif
  int tty;
  int tty;
 
 
  tty = fileno (rl_instream);
  tty = fileno (rl_instream);
 
 
#if defined (HAVE_SELECT)
#if defined (HAVE_SELECT)
  FD_ZERO (&readfds);
  FD_ZERO (&readfds);
  FD_ZERO (&exceptfds);
  FD_ZERO (&exceptfds);
  FD_SET (tty, &readfds);
  FD_SET (tty, &readfds);
  FD_SET (tty, &exceptfds);
  FD_SET (tty, &exceptfds);
  timeout.tv_sec = 0;
  timeout.tv_sec = 0;
  timeout.tv_usec = 100000;     /* 0.1 seconds */
  timeout.tv_usec = 100000;     /* 0.1 seconds */
  return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0);
  return (select (tty + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout) > 0);
#endif
#endif
 
 
#if defined (FIONREAD)
#if defined (FIONREAD)
  if (ioctl (tty, FIONREAD, &chars_avail) == 0)
  if (ioctl (tty, FIONREAD, &chars_avail) == 0)
    return (chars_avail);
    return (chars_avail);
#endif
#endif
 
 
  return 0;
  return 0;
}
}
 
 
void
void
_rl_insert_typein (c)
_rl_insert_typein (c)
     int c;
     int c;
{
{
  int key, t, i;
  int key, t, i;
  char *string;
  char *string;
 
 
  i = key = 0;
  i = key = 0;
  string = xmalloc (ibuffer_len + 1);
  string = xmalloc (ibuffer_len + 1);
  string[i++] = (char) c;
  string[i++] = (char) c;
 
 
  while ((t = rl_get_char (&key)) &&
  while ((t = rl_get_char (&key)) &&
         _rl_keymap[key].type == ISFUNC &&
         _rl_keymap[key].type == ISFUNC &&
         _rl_keymap[key].function == rl_insert)
         _rl_keymap[key].function == rl_insert)
    string[i++] = key;
    string[i++] = key;
 
 
  if (t)
  if (t)
    rl_unget_char (key);
    rl_unget_char (key);
 
 
  string[i] = '\0';
  string[i] = '\0';
  rl_insert_text (string);
  rl_insert_text (string);
  free (string);
  free (string);
}
}
 
 
/* Add KEY to the buffer of characters to be read.  Returns 1 if the
/* Add KEY to the buffer of characters to be read.  Returns 1 if the
   character was stuffed correctly; 0 otherwise. */
   character was stuffed correctly; 0 otherwise. */
int
int
rl_stuff_char (key)
rl_stuff_char (key)
     int key;
     int key;
{
{
  if (ibuffer_space () == 0)
  if (ibuffer_space () == 0)
    return 0;
    return 0;
 
 
  if (key == EOF)
  if (key == EOF)
    {
    {
      key = NEWLINE;
      key = NEWLINE;
      rl_pending_input = EOF;
      rl_pending_input = EOF;
    }
    }
  ibuffer[push_index++] = key;
  ibuffer[push_index++] = key;
  if (push_index >= ibuffer_len)
  if (push_index >= ibuffer_len)
    push_index = 0;
    push_index = 0;
 
 
  return 1;
  return 1;
}
}
 
 
/* Make C be the next command to be executed. */
/* Make C be the next command to be executed. */
int
int
rl_execute_next (c)
rl_execute_next (c)
     int c;
     int c;
{
{
  rl_pending_input = c;
  rl_pending_input = c;
  return 0;
  return 0;
}
}
 
 
/* **************************************************************** */
/* **************************************************************** */
/*                                                                  */
/*                                                                  */
/*                           Character Input                        */
/*                           Character Input                        */
/*                                                                  */
/*                                                                  */
/* **************************************************************** */
/* **************************************************************** */
 
 
/* Read a key, including pending input. */
/* Read a key, including pending input. */
int
int
rl_read_key ()
rl_read_key ()
{
{
  int c;
  int c;
 
 
  rl_key_sequence_length++;
  rl_key_sequence_length++;
 
 
  if (rl_pending_input)
  if (rl_pending_input)
    {
    {
      c = rl_pending_input;
      c = rl_pending_input;
      rl_pending_input = 0;
      rl_pending_input = 0;
    }
    }
  else
  else
    {
    {
      /* If input is coming from a macro, then use that. */
      /* If input is coming from a macro, then use that. */
      if (c = _rl_next_macro_key ())
      if (c = _rl_next_macro_key ())
        return (c);
        return (c);
 
 
      /* If the user has an event function, then call it periodically. */
      /* If the user has an event function, then call it periodically. */
      if (rl_event_hook)
      if (rl_event_hook)
        {
        {
          while (rl_event_hook && rl_get_char (&c) == 0)
          while (rl_event_hook && rl_get_char (&c) == 0)
            {
            {
              (*rl_event_hook) ();
              (*rl_event_hook) ();
              rl_gather_tyi ();
              rl_gather_tyi ();
            }
            }
        }
        }
      else
      else
        {
        {
          if (rl_get_char (&c) == 0)
          if (rl_get_char (&c) == 0)
            c = (*rl_getc_function) (rl_instream);
            c = (*rl_getc_function) (rl_instream);
        }
        }
    }
    }
 
 
  return (c);
  return (c);
}
}
 
 
int
int
rl_getc (stream)
rl_getc (stream)
     FILE *stream;
     FILE *stream;
{
{
  int result, flags;
  int result, flags;
  unsigned char c;
  unsigned char c;
 
 
#if defined (__GO32__) && !defined (HAVE_TERMIOS_H)
#if defined (__GO32__) && !defined (HAVE_TERMIOS_H)
  if (isatty (0))
  if (isatty (0))
    return (getkey () & 0x7F);
    return (getkey () & 0x7F);
#endif /* __GO32__ */
#endif /* __GO32__ */
 
 
  while (1)
  while (1)
    {
    {
      result = read (fileno (stream), &c, sizeof (unsigned char));
      result = read (fileno (stream), &c, sizeof (unsigned char));
 
 
      if (result == sizeof (unsigned char))
      if (result == sizeof (unsigned char))
        return (c);
        return (c);
 
 
      /* If zero characters are returned, then the file that we are
      /* If zero characters are returned, then the file that we are
         reading from is empty!  Return EOF in that case. */
         reading from is empty!  Return EOF in that case. */
      if (result == 0)
      if (result == 0)
        return (EOF);
        return (EOF);
 
 
#if defined (__BEOS__)
#if defined (__BEOS__)
      if (errno == EINTR)
      if (errno == EINTR)
        continue;
        continue;
#endif
#endif
 
 
#if defined (EWOULDBLOCK)
#if defined (EWOULDBLOCK)
      if (errno == EWOULDBLOCK)
      if (errno == EWOULDBLOCK)
        {
        {
          if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
          if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
            return (EOF);
            return (EOF);
          if (flags & O_NDELAY)
          if (flags & O_NDELAY)
            {
            {
              flags &= ~O_NDELAY;
              flags &= ~O_NDELAY;
              fcntl (fileno (stream), F_SETFL, flags);
              fcntl (fileno (stream), F_SETFL, flags);
              continue;
              continue;
            }
            }
          continue;
          continue;
        }
        }
#endif /* EWOULDBLOCK */
#endif /* EWOULDBLOCK */
 
 
#if defined (_POSIX_VERSION) && defined (EAGAIN) && defined (O_NONBLOCK)
#if defined (_POSIX_VERSION) && defined (EAGAIN) && defined (O_NONBLOCK)
      if (errno == EAGAIN)
      if (errno == EAGAIN)
        {
        {
          if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
          if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
            return (EOF);
            return (EOF);
          if (flags & O_NONBLOCK)
          if (flags & O_NONBLOCK)
            {
            {
              flags &= ~O_NONBLOCK;
              flags &= ~O_NONBLOCK;
              fcntl (fileno (stream), F_SETFL, flags);
              fcntl (fileno (stream), F_SETFL, flags);
              continue;
              continue;
            }
            }
        }
        }
#endif /* _POSIX_VERSION && EAGAIN && O_NONBLOCK */
#endif /* _POSIX_VERSION && EAGAIN && O_NONBLOCK */
 
 
#if !defined (__GO32__) || defined (HAVE_TERMIOS_H)
#if !defined (__GO32__) || defined (HAVE_TERMIOS_H)
      /* If the error that we received was SIGINT, then try again,
      /* If the error that we received was SIGINT, then try again,
         this is simply an interrupted system call to read ().
         this is simply an interrupted system call to read ().
         Otherwise, some error ocurred, also signifying EOF. */
         Otherwise, some error ocurred, also signifying EOF. */
      if (errno != EINTR)
      if (errno != EINTR)
        return (EOF);
        return (EOF);
#endif /* !__GO32__ */
#endif /* !__GO32__ */
    }
    }
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.