#define READLINE_LIBRARY
#if defined (HAVE_CONFIG_H)
# include <config.h>
#endif
#include <stdio.h>
#if defined (HAVE_STDLIB_H)
# include <stdlib.h>
#else
# include "ansi_stdlib.h"
#endif
#if defined (HAVE_UNISTD_H)
# ifdef _MINIX
# include <sys/types.h>
# endif
# include <unistd.h>
#endif
#include "history.h"
#include "histlib.h"
#include "xmalloc.h"
#define DEFAULT_HISTORY_GROW_SIZE 50
static HIST_ENTRY **the_history = (HIST_ENTRY **)NULL;
static int history_stifled;
static int history_size;
int history_max_entries;
int max_input_history;
int history_offset;
int history_length;
int history_base = 1;
HISTORY_STATE *
history_get_history_state ()
{
HISTORY_STATE *state;
state = (HISTORY_STATE *)xmalloc (sizeof (HISTORY_STATE));
state->entries = the_history;
state->offset = history_offset;
state->length = history_length;
state->size = history_size;
state->flags = 0;
if (history_stifled)
state->flags |= HS_STIFLED;
return (state);
}
void
history_set_history_state (state)
HISTORY_STATE *state;
{
the_history = state->entries;
history_offset = state->offset;
history_length = state->length;
history_size = state->size;
if (state->flags & HS_STIFLED)
history_stifled = 1;
}
void
using_history ()
{
history_offset = history_length;
}
int
history_total_bytes ()
{
register int i, result;
for (i = result = 0; the_history && the_history[i]; i++)
result += strlen (the_history[i]->line);
return (result);
}
int
where_history ()
{
return (history_offset);
}
int
history_set_pos (pos)
int pos;
{
if (pos > history_length || pos < 0 || !the_history)
return (0);
history_offset = pos;
return (1);
}
HIST_ENTRY **
history_list ()
{
return (the_history);
}
HIST_ENTRY *
current_history ()
{
return ((history_offset == history_length) || the_history == 0)
? (HIST_ENTRY *)NULL
: the_history[history_offset];
}
HIST_ENTRY *
previous_history ()
{
return history_offset ? the_history[--history_offset] : (HIST_ENTRY *)NULL;
}
HIST_ENTRY *
next_history ()
{
return (history_offset == history_length) ? (HIST_ENTRY *)NULL : the_history[++history_offset];
}
HIST_ENTRY *
history_get (offset)
int offset;
{
int local_index;
local_index = offset - history_base;
return (local_index >= history_length || local_index < 0 || !the_history)
? (HIST_ENTRY *)NULL
: the_history[local_index];
}
void
add_history (string)
const char *string;
{
HIST_ENTRY *temp;
if (history_stifled && (history_length == history_max_entries))
{
register int i;
if (history_length == 0)
return;
if (the_history[0])
{
free (the_history[0]->line);
free (the_history[0]);
}
for (i = 0; i < history_length; i++)
the_history[i] = the_history[i + 1];
history_base++;
}
else
{
if (history_size == 0)
{
history_size = DEFAULT_HISTORY_GROW_SIZE;
the_history = (HIST_ENTRY **)xmalloc (history_size * sizeof (HIST_ENTRY *));
history_length = 1;
}
else
{
if (history_length == (history_size - 1))
{
history_size += DEFAULT_HISTORY_GROW_SIZE;
the_history = (HIST_ENTRY **)
xrealloc (the_history, history_size * sizeof (HIST_ENTRY *));
}
history_length++;
}
}
temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
temp->line = savestring (string);
temp->data = (char *)NULL;
the_history[history_length] = (HIST_ENTRY *)NULL;
the_history[history_length - 1] = temp;
}
HIST_ENTRY *
replace_history_entry (which, line, data)
int which;
const char *line;
histdata_t data;
{
HIST_ENTRY *temp, *old_value;
if (which >= history_length)
return ((HIST_ENTRY *)NULL);
temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
old_value = the_history[which];
temp->line = savestring (line);
temp->data = data;
the_history[which] = temp;
return (old_value);
}
HIST_ENTRY *
remove_history (which)
int which;
{
HIST_ENTRY *return_value;
register int i;
if (which >= history_length || !history_length)
return_value = (HIST_ENTRY *)NULL;
else
{
return_value = the_history[which];
for (i = which; i < history_length; i++)
the_history[i] = the_history[i + 1];
history_length--;
}
return (return_value);
}
void
stifle_history (max)
int max;
{
register int i, j;
if (max < 0)
max = 0;
if (history_length > max)
{
for (i = 0, j = history_length - max; i < j; i++)
{
free (the_history[i]->line);
free (the_history[i]);
}
history_base = i;
for (j = 0, i = history_length - max; j < max; i++, j++)
the_history[j] = the_history[i];
the_history[j] = (HIST_ENTRY *)NULL;
history_length = j;
}
history_stifled = 1;
max_input_history = history_max_entries = max;
}
int
unstifle_history ()
{
if (history_stifled)
{
history_stifled = 0;
return (history_max_entries);
}
else
return (-history_max_entries);
}
int
history_is_stifled ()
{
return (history_stifled);
}
void
clear_history ()
{
register int i;
for (i = 0; i < history_length; i++)
{
free (the_history[i]->line);
free (the_history[i]);
the_history[i] = (HIST_ENTRY *)NULL;
}
history_offset = history_length = 0;
}