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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-6.8/] [readline/] [history.c] - Diff between revs 827 and 840

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 827 Rev 840
/* history.c -- standalone history library */
/* history.c -- standalone history library */
 
 
/* Copyright (C) 1989-2005 Free Software Foundation, Inc.
/* Copyright (C) 1989-2005 Free Software Foundation, Inc.
 
 
   This file contains the GNU History Library (the Library), a set of
   This file contains the GNU History Library (the Library), a set of
   routines for managing the text of previously typed lines.
   routines for managing the text of previously typed lines.
 
 
   The Library is free software; you can redistribute it and/or modify
   The Library is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   the Free Software Foundation; either version 2, or (at your option)
   any later version.
   any later version.
 
 
   The Library is distributed in the hope that it will be useful, but
   The Library is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
   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,
   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
 
 
/* The goal is to make the implementation transparent, so that you
/* The goal is to make the implementation transparent, so that you
   don't have to know what data types are used, just what functions
   don't have to know what data types are used, just what functions
   you can call.  I think I have done that. */
   you can call.  I think I have done that. */
#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 <stdio.h>
#include <stdio.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_UNISTD_H)
#if defined (HAVE_UNISTD_H)
#  ifdef _MINIX
#  ifdef _MINIX
#    include <sys/types.h>
#    include <sys/types.h>
#  endif
#  endif
#  include <unistd.h>
#  include <unistd.h>
#endif
#endif
 
 
#include "history.h"
#include "history.h"
#include "histlib.h"
#include "histlib.h"
 
 
#include "xmalloc.h"
#include "xmalloc.h"
 
 
/* The number of slots to increase the_history by. */
/* The number of slots to increase the_history by. */
#define DEFAULT_HISTORY_GROW_SIZE 50
#define DEFAULT_HISTORY_GROW_SIZE 50
 
 
static char *hist_inittime PARAMS((void));
static char *hist_inittime PARAMS((void));
 
 
/* **************************************************************** */
/* **************************************************************** */
/*                                                                  */
/*                                                                  */
/*                      History Functions                           */
/*                      History Functions                           */
/*                                                                  */
/*                                                                  */
/* **************************************************************** */
/* **************************************************************** */
 
 
/* An array of HIST_ENTRY.  This is where we store the history. */
/* An array of HIST_ENTRY.  This is where we store the history. */
static HIST_ENTRY **the_history = (HIST_ENTRY **)NULL;
static HIST_ENTRY **the_history = (HIST_ENTRY **)NULL;
 
 
/* Non-zero means that we have enforced a limit on the amount of
/* Non-zero means that we have enforced a limit on the amount of
   history that we save. */
   history that we save. */
static int history_stifled;
static int history_stifled;
 
 
/* The current number of slots allocated to the input_history. */
/* The current number of slots allocated to the input_history. */
static int history_size;
static int history_size;
 
 
/* If HISTORY_STIFLED is non-zero, then this is the maximum number of
/* If HISTORY_STIFLED is non-zero, then this is the maximum number of
   entries to remember. */
   entries to remember. */
int history_max_entries;
int history_max_entries;
int max_input_history;  /* backwards compatibility */
int max_input_history;  /* backwards compatibility */
 
 
/* The current location of the interactive history pointer.  Just makes
/* The current location of the interactive history pointer.  Just makes
   life easier for outside callers. */
   life easier for outside callers. */
int history_offset;
int history_offset;
 
 
/* The number of strings currently stored in the history list. */
/* The number of strings currently stored in the history list. */
int history_length;
int history_length;
 
 
/* The logical `base' of the history array.  It defaults to 1. */
/* The logical `base' of the history array.  It defaults to 1. */
int history_base = 1;
int history_base = 1;
 
 
/* Return the current HISTORY_STATE of the history. */
/* Return the current HISTORY_STATE of the history. */
HISTORY_STATE *
HISTORY_STATE *
history_get_history_state ()
history_get_history_state ()
{
{
  HISTORY_STATE *state;
  HISTORY_STATE *state;
 
 
  state = (HISTORY_STATE *)xmalloc (sizeof (HISTORY_STATE));
  state = (HISTORY_STATE *)xmalloc (sizeof (HISTORY_STATE));
  state->entries = the_history;
  state->entries = the_history;
  state->offset = history_offset;
  state->offset = history_offset;
  state->length = history_length;
  state->length = history_length;
  state->size = history_size;
  state->size = history_size;
  state->flags = 0;
  state->flags = 0;
  if (history_stifled)
  if (history_stifled)
    state->flags |= HS_STIFLED;
    state->flags |= HS_STIFLED;
 
 
  return (state);
  return (state);
}
}
 
 
/* Set the state of the current history array to STATE. */
/* Set the state of the current history array to STATE. */
void
void
history_set_history_state (state)
history_set_history_state (state)
     HISTORY_STATE *state;
     HISTORY_STATE *state;
{
{
  the_history = state->entries;
  the_history = state->entries;
  history_offset = state->offset;
  history_offset = state->offset;
  history_length = state->length;
  history_length = state->length;
  history_size = state->size;
  history_size = state->size;
  if (state->flags & HS_STIFLED)
  if (state->flags & HS_STIFLED)
    history_stifled = 1;
    history_stifled = 1;
}
}
 
 
/* Begin a session in which the history functions might be used.  This
/* Begin a session in which the history functions might be used.  This
   initializes interactive variables. */
   initializes interactive variables. */
void
void
using_history ()
using_history ()
{
{
  history_offset = history_length;
  history_offset = history_length;
}
}
 
 
/* Return the number of bytes that the primary history entries are using.
/* Return the number of bytes that the primary history entries are using.
   This just adds up the lengths of the_history->lines and the associated
   This just adds up the lengths of the_history->lines and the associated
   timestamps. */
   timestamps. */
int
int
history_total_bytes ()
history_total_bytes ()
{
{
  register int i, result;
  register int i, result;
 
 
  for (i = result = 0; the_history && the_history[i]; i++)
  for (i = result = 0; the_history && the_history[i]; i++)
    result += HISTENT_BYTES (the_history[i]);
    result += HISTENT_BYTES (the_history[i]);
 
 
  return (result);
  return (result);
}
}
 
 
/* Returns the magic number which says what history element we are
/* Returns the magic number which says what history element we are
   looking at now.  In this implementation, it returns history_offset. */
   looking at now.  In this implementation, it returns history_offset. */
int
int
where_history ()
where_history ()
{
{
  return (history_offset);
  return (history_offset);
}
}
 
 
/* Make the current history item be the one at POS, an absolute index.
/* Make the current history item be the one at POS, an absolute index.
   Returns zero if POS is out of range, else non-zero. */
   Returns zero if POS is out of range, else non-zero. */
int
int
history_set_pos (pos)
history_set_pos (pos)
     int pos;
     int pos;
{
{
  if (pos > history_length || pos < 0 || !the_history)
  if (pos > history_length || pos < 0 || !the_history)
    return (0);
    return (0);
  history_offset = pos;
  history_offset = pos;
  return (1);
  return (1);
}
}
 
 
/* Return the current history array.  The caller has to be carefull, since this
/* Return the current history array.  The caller has to be carefull, since this
   is the actual array of data, and could be bashed or made corrupt easily.
   is the actual array of data, and could be bashed or made corrupt easily.
   The array is terminated with a NULL pointer. */
   The array is terminated with a NULL pointer. */
HIST_ENTRY **
HIST_ENTRY **
history_list ()
history_list ()
{
{
  return (the_history);
  return (the_history);
}
}
 
 
/* Return the history entry at the current position, as determined by
/* Return the history entry at the current position, as determined by
   history_offset.  If there is no entry there, return a NULL pointer. */
   history_offset.  If there is no entry there, return a NULL pointer. */
HIST_ENTRY *
HIST_ENTRY *
current_history ()
current_history ()
{
{
  return ((history_offset == history_length) || the_history == 0)
  return ((history_offset == history_length) || the_history == 0)
                ? (HIST_ENTRY *)NULL
                ? (HIST_ENTRY *)NULL
                : the_history[history_offset];
                : the_history[history_offset];
}
}
 
 
/* Back up history_offset to the previous history entry, and return
/* Back up history_offset to the previous history entry, and return
   a pointer to that entry.  If there is no previous entry then return
   a pointer to that entry.  If there is no previous entry then return
   a NULL pointer. */
   a NULL pointer. */
HIST_ENTRY *
HIST_ENTRY *
previous_history ()
previous_history ()
{
{
  return history_offset ? the_history[--history_offset] : (HIST_ENTRY *)NULL;
  return history_offset ? the_history[--history_offset] : (HIST_ENTRY *)NULL;
}
}
 
 
/* Move history_offset forward to the next history entry, and return
/* Move history_offset forward to the next history entry, and return
   a pointer to that entry.  If there is no next entry then return a
   a pointer to that entry.  If there is no next entry then return a
   NULL pointer. */
   NULL pointer. */
HIST_ENTRY *
HIST_ENTRY *
next_history ()
next_history ()
{
{
  return (history_offset == history_length) ? (HIST_ENTRY *)NULL : the_history[++history_offset];
  return (history_offset == history_length) ? (HIST_ENTRY *)NULL : the_history[++history_offset];
}
}
 
 
/* Return the history entry which is logically at OFFSET in the history array.
/* Return the history entry which is logically at OFFSET in the history array.
   OFFSET is relative to history_base. */
   OFFSET is relative to history_base. */
HIST_ENTRY *
HIST_ENTRY *
history_get (offset)
history_get (offset)
     int offset;
     int offset;
{
{
  int local_index;
  int local_index;
 
 
  local_index = offset - history_base;
  local_index = offset - history_base;
  return (local_index >= history_length || local_index < 0 || the_history == 0)
  return (local_index >= history_length || local_index < 0 || the_history == 0)
                ? (HIST_ENTRY *)NULL
                ? (HIST_ENTRY *)NULL
                : the_history[local_index];
                : the_history[local_index];
}
}
 
 
time_t
time_t
history_get_time (hist)
history_get_time (hist)
     HIST_ENTRY *hist;
     HIST_ENTRY *hist;
{
{
  char *ts;
  char *ts;
  time_t t;
  time_t t;
 
 
  if (hist == 0 || hist->timestamp == 0)
  if (hist == 0 || hist->timestamp == 0)
    return 0;
    return 0;
  ts = hist->timestamp;
  ts = hist->timestamp;
  if (ts[0] != history_comment_char)
  if (ts[0] != history_comment_char)
    return 0;
    return 0;
  t = (time_t) atol (ts + 1);           /* XXX - should use strtol() here */
  t = (time_t) atol (ts + 1);           /* XXX - should use strtol() here */
  return t;
  return t;
}
}
 
 
static char *
static char *
hist_inittime ()
hist_inittime ()
{
{
  time_t t;
  time_t t;
  char ts[64], *ret;
  char ts[64], *ret;
 
 
  t = (time_t) time ((time_t *)0);
  t = (time_t) time ((time_t *)0);
#if defined (HAVE_VSNPRINTF)            /* assume snprintf if vsnprintf exists */
#if defined (HAVE_VSNPRINTF)            /* assume snprintf if vsnprintf exists */
  snprintf (ts, sizeof (ts) - 1, "X%lu", (unsigned long) t);
  snprintf (ts, sizeof (ts) - 1, "X%lu", (unsigned long) t);
#else
#else
  sprintf (ts, "X%lu", (unsigned long) t);
  sprintf (ts, "X%lu", (unsigned long) t);
#endif
#endif
  ret = savestring (ts);
  ret = savestring (ts);
  ret[0] = history_comment_char;
  ret[0] = history_comment_char;
 
 
  return ret;
  return ret;
}
}
 
 
/* Place STRING at the end of the history list.  The data field
/* Place STRING at the end of the history list.  The data field
   is  set to NULL. */
   is  set to NULL. */
void
void
add_history (string)
add_history (string)
     const char *string;
     const char *string;
{
{
  HIST_ENTRY *temp;
  HIST_ENTRY *temp;
 
 
  if (history_stifled && (history_length == history_max_entries))
  if (history_stifled && (history_length == history_max_entries))
    {
    {
      register int i;
      register int i;
 
 
      /* If the history is stifled, and history_length is zero,
      /* If the history is stifled, and history_length is zero,
         and it equals history_max_entries, we don't save items. */
         and it equals history_max_entries, we don't save items. */
      if (history_length == 0)
      if (history_length == 0)
        return;
        return;
 
 
      /* If there is something in the slot, then remove it. */
      /* If there is something in the slot, then remove it. */
      if (the_history[0])
      if (the_history[0])
        (void) free_history_entry (the_history[0]);
        (void) free_history_entry (the_history[0]);
 
 
      /* Copy the rest of the entries, moving down one slot. */
      /* Copy the rest of the entries, moving down one slot. */
      for (i = 0; i < history_length; i++)
      for (i = 0; i < history_length; i++)
        the_history[i] = the_history[i + 1];
        the_history[i] = the_history[i + 1];
 
 
      history_base++;
      history_base++;
    }
    }
  else
  else
    {
    {
      if (history_size == 0)
      if (history_size == 0)
        {
        {
          history_size = DEFAULT_HISTORY_GROW_SIZE;
          history_size = DEFAULT_HISTORY_GROW_SIZE;
          the_history = (HIST_ENTRY **)xmalloc (history_size * sizeof (HIST_ENTRY *));
          the_history = (HIST_ENTRY **)xmalloc (history_size * sizeof (HIST_ENTRY *));
          history_length = 1;
          history_length = 1;
        }
        }
      else
      else
        {
        {
          if (history_length == (history_size - 1))
          if (history_length == (history_size - 1))
            {
            {
              history_size += DEFAULT_HISTORY_GROW_SIZE;
              history_size += DEFAULT_HISTORY_GROW_SIZE;
              the_history = (HIST_ENTRY **)
              the_history = (HIST_ENTRY **)
                xrealloc (the_history, history_size * sizeof (HIST_ENTRY *));
                xrealloc (the_history, history_size * sizeof (HIST_ENTRY *));
            }
            }
          history_length++;
          history_length++;
        }
        }
    }
    }
 
 
  temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  temp->line = savestring (string);
  temp->line = savestring (string);
  temp->data = (char *)NULL;
  temp->data = (char *)NULL;
 
 
  temp->timestamp = hist_inittime ();
  temp->timestamp = hist_inittime ();
 
 
  the_history[history_length] = (HIST_ENTRY *)NULL;
  the_history[history_length] = (HIST_ENTRY *)NULL;
  the_history[history_length - 1] = temp;
  the_history[history_length - 1] = temp;
}
}
 
 
/* Change the time stamp of the most recent history entry to STRING. */
/* Change the time stamp of the most recent history entry to STRING. */
void
void
add_history_time (string)
add_history_time (string)
     const char *string;
     const char *string;
{
{
  HIST_ENTRY *hs;
  HIST_ENTRY *hs;
 
 
  hs = the_history[history_length - 1];
  hs = the_history[history_length - 1];
  FREE (hs->timestamp);
  FREE (hs->timestamp);
  hs->timestamp = savestring (string);
  hs->timestamp = savestring (string);
}
}
 
 
/* Free HIST and return the data so the calling application can free it
/* Free HIST and return the data so the calling application can free it
   if necessary and desired. */
   if necessary and desired. */
histdata_t
histdata_t
free_history_entry (hist)
free_history_entry (hist)
     HIST_ENTRY *hist;
     HIST_ENTRY *hist;
{
{
  histdata_t x;
  histdata_t x;
 
 
  if (hist == 0)
  if (hist == 0)
    return ((histdata_t) 0);
    return ((histdata_t) 0);
  FREE (hist->line);
  FREE (hist->line);
  FREE (hist->timestamp);
  FREE (hist->timestamp);
  x = hist->data;
  x = hist->data;
  free (hist);
  free (hist);
  return (x);
  return (x);
}
}
 
 
/* Make the history entry at WHICH have LINE and DATA.  This returns
/* Make the history entry at WHICH have LINE and DATA.  This returns
   the old entry so you can dispose of the data.  In the case of an
   the old entry so you can dispose of the data.  In the case of an
   invalid WHICH, a NULL pointer is returned. */
   invalid WHICH, a NULL pointer is returned. */
HIST_ENTRY *
HIST_ENTRY *
replace_history_entry (which, line, data)
replace_history_entry (which, line, data)
     int which;
     int which;
     const char *line;
     const char *line;
     histdata_t data;
     histdata_t data;
{
{
  HIST_ENTRY *temp, *old_value;
  HIST_ENTRY *temp, *old_value;
 
 
  if (which < 0 || which >= history_length)
  if (which < 0 || which >= history_length)
    return ((HIST_ENTRY *)NULL);
    return ((HIST_ENTRY *)NULL);
 
 
  temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  old_value = the_history[which];
  old_value = the_history[which];
 
 
  temp->line = savestring (line);
  temp->line = savestring (line);
  temp->data = data;
  temp->data = data;
  temp->timestamp = savestring (old_value->timestamp);
  temp->timestamp = savestring (old_value->timestamp);
  the_history[which] = temp;
  the_history[which] = temp;
 
 
  return (old_value);
  return (old_value);
}
}
 
 
/* Remove history element WHICH from the history.  The removed
/* Remove history element WHICH from the history.  The removed
   element is returned to you so you can free the line, data,
   element is returned to you so you can free the line, data,
   and containing structure. */
   and containing structure. */
HIST_ENTRY *
HIST_ENTRY *
remove_history (which)
remove_history (which)
     int which;
     int which;
{
{
  HIST_ENTRY *return_value;
  HIST_ENTRY *return_value;
  register int i;
  register int i;
 
 
  if (which < 0 || which >= history_length || history_length ==  0 || the_history == 0)
  if (which < 0 || which >= history_length || history_length ==  0 || the_history == 0)
    return ((HIST_ENTRY *)NULL);
    return ((HIST_ENTRY *)NULL);
 
 
  return_value = the_history[which];
  return_value = the_history[which];
 
 
  for (i = which; i < history_length; i++)
  for (i = which; i < history_length; i++)
    the_history[i] = the_history[i + 1];
    the_history[i] = the_history[i + 1];
 
 
  history_length--;
  history_length--;
 
 
  return (return_value);
  return (return_value);
}
}
 
 
/* Stifle the history list, remembering only MAX number of lines. */
/* Stifle the history list, remembering only MAX number of lines. */
void
void
stifle_history (max)
stifle_history (max)
     int max;
     int max;
{
{
  register int i, j;
  register int i, j;
 
 
  if (max < 0)
  if (max < 0)
    max = 0;
    max = 0;
 
 
  if (history_length > max)
  if (history_length > max)
    {
    {
      /* This loses because we cannot free the data. */
      /* This loses because we cannot free the data. */
      for (i = 0, j = history_length - max; i < j; i++)
      for (i = 0, j = history_length - max; i < j; i++)
        free_history_entry (the_history[i]);
        free_history_entry (the_history[i]);
 
 
      history_base = i;
      history_base = i;
      for (j = 0, i = history_length - max; j < max; i++, j++)
      for (j = 0, i = history_length - max; j < max; i++, j++)
        the_history[j] = the_history[i];
        the_history[j] = the_history[i];
      the_history[j] = (HIST_ENTRY *)NULL;
      the_history[j] = (HIST_ENTRY *)NULL;
      history_length = j;
      history_length = j;
    }
    }
 
 
  history_stifled = 1;
  history_stifled = 1;
  max_input_history = history_max_entries = max;
  max_input_history = history_max_entries = max;
}
}
 
 
/* Stop stifling the history.  This returns the previous maximum
/* Stop stifling the history.  This returns the previous maximum
   number of history entries.  The value is positive if the history
   number of history entries.  The value is positive if the history
   was stifled,  negative if it wasn't. */
   was stifled,  negative if it wasn't. */
int
int
unstifle_history ()
unstifle_history ()
{
{
  if (history_stifled)
  if (history_stifled)
    {
    {
      history_stifled = 0;
      history_stifled = 0;
      return (history_max_entries);
      return (history_max_entries);
    }
    }
  else
  else
    return (-history_max_entries);
    return (-history_max_entries);
}
}
 
 
int
int
history_is_stifled ()
history_is_stifled ()
{
{
  return (history_stifled);
  return (history_stifled);
}
}
 
 
void
void
clear_history ()
clear_history ()
{
{
  register int i;
  register int i;
 
 
  /* This loses because we cannot free the data. */
  /* This loses because we cannot free the data. */
  for (i = 0; i < history_length; i++)
  for (i = 0; i < history_length; i++)
    {
    {
      free_history_entry (the_history[i]);
      free_history_entry (the_history[i]);
      the_history[i] = (HIST_ENTRY *)NULL;
      the_history[i] = (HIST_ENTRY *)NULL;
    }
    }
 
 
  history_offset = history_length = 0;
  history_offset = history_length = 0;
}
}
 
 

powered by: WebSVN 2.1.0

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