2005-09-20 21:26:39 +08:00
|
|
|
/** \file history.c
|
|
|
|
History functions, part of the user interface.
|
|
|
|
*/
|
2006-08-11 09:18:35 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
2006-02-28 21:17:16 +08:00
|
|
|
#include "fallback.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "util.h"
|
2006-02-28 21:17:16 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "wutil.h"
|
|
|
|
#include "history.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "reader.h"
|
|
|
|
#include "env.h"
|
|
|
|
#include "sanity.h"
|
2006-02-11 08:13:17 +08:00
|
|
|
#include "signal.h"
|
2006-09-08 20:34:55 +08:00
|
|
|
#include "halloc.h"
|
|
|
|
#include "halloc_util.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
The history is implemented using a linked list. Searches are done
|
|
|
|
using linear search.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
A struct describing the state of the interactive history
|
|
|
|
list. Multiple states can be created. Use \c history_set_mode() to
|
|
|
|
change between history contexts.
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
Number of entries
|
|
|
|
*/
|
|
|
|
int count;
|
2006-06-17 21:07:08 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/**
|
|
|
|
Last history item
|
|
|
|
*/
|
|
|
|
ll_node_t *last;
|
2006-06-17 21:07:08 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/**
|
|
|
|
The last item loaded from file
|
|
|
|
*/
|
|
|
|
ll_node_t *last_loaded;
|
2006-02-06 23:08:10 +08:00
|
|
|
|
2006-06-17 21:07:08 +08:00
|
|
|
/**
|
|
|
|
Set to one if the file containing the saved history has been loaded
|
|
|
|
*/
|
2006-02-06 23:08:10 +08:00
|
|
|
int is_loaded;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2006-09-08 20:34:55 +08:00
|
|
|
history_data_t;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
static ll_node_t /** Last item in history */ *history_last=0, /** Current search position*/ *history_current =0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
We only try to save the part of the history that was written in
|
|
|
|
this session. This pointer keeps track of where the cutoff is.
|
|
|
|
|
|
|
|
The reason for wanting to do this is so that when two concurrent
|
|
|
|
fish sessions have exited, the latter one to exit won't push the
|
|
|
|
added history items of the prior session to the top of the history.
|
|
|
|
*/
|
|
|
|
static ll_node_t *last_loaded=0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
past_end is a kludge. It is set when the current history item is
|
|
|
|
unset, i.e. a new entry is being typed.
|
|
|
|
*/
|
|
|
|
static int past_end =1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Items in history
|
|
|
|
*/
|
|
|
|
static int history_count=0;
|
|
|
|
|
|
|
|
/**
|
2006-06-17 21:07:08 +08:00
|
|
|
The name of the current history list. The name is used to switch
|
|
|
|
between history lists for different commands as sell as for
|
|
|
|
deciding the name of the file to save the history in.
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
|
|
|
static wchar_t *mode_name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Hash table for storing all the history lists.
|
|
|
|
*/
|
|
|
|
static hash_table_t history_table;
|
|
|
|
|
2006-02-06 23:08:10 +08:00
|
|
|
/**
|
|
|
|
Flag, set to 1 once the history file has been loaded
|
|
|
|
*/
|
2006-06-16 00:03:27 +08:00
|
|
|
static int is_loaded=0;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2006-09-08 20:34:55 +08:00
|
|
|
static ll_node_t *unused = 0;
|
|
|
|
|
|
|
|
static ll_node_t *history_create_node()
|
|
|
|
{
|
|
|
|
ll_node_t *res;
|
|
|
|
|
|
|
|
if( !unused )
|
|
|
|
{
|
|
|
|
return halloc( global_context, sizeof( ll_node_t ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
res = unused;
|
|
|
|
unused = unused->next;
|
|
|
|
return res;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void history_free_node( ll_node_t *n )
|
|
|
|
{
|
|
|
|
n->next = unused;
|
|
|
|
unused = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/**
|
2006-02-06 23:08:10 +08:00
|
|
|
Load history from file
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
2006-06-22 08:12:28 +08:00
|
|
|
static int history_load()
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
|
|
|
wchar_t *fn;
|
|
|
|
wchar_t *buff=0;
|
|
|
|
int buff_len=0;
|
|
|
|
FILE *in_stream;
|
|
|
|
hash_table_t used;
|
2006-06-22 08:12:28 +08:00
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
if( !mode_name )
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2006-02-06 23:08:10 +08:00
|
|
|
|
|
|
|
is_loaded = 1;
|
|
|
|
|
2006-02-09 23:50:20 +08:00
|
|
|
signal_block();
|
2005-09-20 21:26:39 +08:00
|
|
|
hash_init2( &used,
|
|
|
|
&hash_wcs_func,
|
|
|
|
&hash_wcs_cmp,
|
|
|
|
4096 );
|
|
|
|
|
2006-02-06 23:08:10 +08:00
|
|
|
fn = wcsdupcat2( env_get(L"HOME"), L"/.", mode_name, L"_history", 0 );
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
in_stream = wfopen( fn, "r" );
|
|
|
|
|
|
|
|
if( in_stream != 0 )
|
|
|
|
{
|
|
|
|
while( !feof( in_stream ) )
|
|
|
|
{
|
|
|
|
int buff_read = fgetws2( &buff, &buff_len, in_stream );
|
|
|
|
if( buff_read == -1 )
|
|
|
|
{
|
2006-02-06 23:08:10 +08:00
|
|
|
debug( 1, L"The following non-fatal error occurred while reading command history from \'%ls\':", mode_name );
|
2005-09-20 21:26:39 +08:00
|
|
|
wperror( L"fgetws2" );
|
|
|
|
fclose( in_stream );
|
|
|
|
free( fn );
|
|
|
|
free( buff );
|
2006-02-09 23:50:20 +08:00
|
|
|
signal_unblock();
|
2006-06-22 08:12:28 +08:00
|
|
|
return -1;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
We do not call history_add here, since that would make
|
|
|
|
history_load() take quadratic time, and may be
|
|
|
|
unacceptably on slow systems with a long history file.
|
|
|
|
|
|
|
|
Use a hashtable to check for duplicates instead.
|
|
|
|
*/
|
2005-12-13 02:30:55 +08:00
|
|
|
if( wcslen(buff) && !hash_get( &used,
|
|
|
|
buff ) )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
|
|
|
history_count++;
|
|
|
|
|
2006-09-08 20:34:55 +08:00
|
|
|
history_current = history_create_node();
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2006-10-01 23:20:40 +08:00
|
|
|
history_current->data=halloc_wcsdup( global_context, buff );
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
hash_put( &used,
|
|
|
|
history_current->data,
|
|
|
|
L"" );
|
|
|
|
|
|
|
|
history_current->next=0;
|
|
|
|
history_current->prev = history_last;
|
|
|
|
if( history_last != 0 )
|
|
|
|
{
|
|
|
|
history_last->next = history_current;
|
|
|
|
}
|
|
|
|
history_last = history_current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose( in_stream );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( errno != ENOENT )
|
|
|
|
{
|
2006-02-06 23:08:10 +08:00
|
|
|
debug( 1, L"The following non-fatal error occurred while reading command history from \'%ls\':", mode_name );
|
2005-09-20 21:26:39 +08:00
|
|
|
wperror( L"fopen" );
|
2006-06-22 08:12:28 +08:00
|
|
|
res = -1;
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
hash_destroy( &used );
|
|
|
|
|
|
|
|
free( buff );
|
|
|
|
free( fn );
|
|
|
|
last_loaded = history_last;
|
2006-02-09 23:50:20 +08:00
|
|
|
signal_unblock();
|
2006-06-22 08:12:28 +08:00
|
|
|
return res;
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void history_init()
|
|
|
|
{
|
|
|
|
hash_init( &history_table,
|
|
|
|
&hash_wcs_func,
|
|
|
|
&hash_wcs_cmp );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Make sure the current history list is in the history_table.
|
|
|
|
*/
|
|
|
|
static void history_to_hash()
|
|
|
|
{
|
2006-09-08 20:34:55 +08:00
|
|
|
history_data_t *d;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2006-06-16 00:03:27 +08:00
|
|
|
if( !mode_name )
|
2006-04-20 07:42:11 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
2006-09-08 20:34:55 +08:00
|
|
|
d = (history_data_t *)hash_get( &history_table,
|
2005-09-20 21:26:39 +08:00
|
|
|
mode_name );
|
|
|
|
|
|
|
|
if( !d )
|
2006-09-08 20:34:55 +08:00
|
|
|
d = malloc( sizeof(history_data_t));
|
2005-09-20 21:26:39 +08:00
|
|
|
d->last=history_last;
|
|
|
|
d->last_loaded=last_loaded;
|
|
|
|
d->count=history_count;
|
2006-02-06 23:08:10 +08:00
|
|
|
d->is_loaded = is_loaded;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
hash_put( &history_table,
|
|
|
|
mode_name,
|
|
|
|
d );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void history_set_mode( wchar_t *name )
|
|
|
|
{
|
2006-09-08 20:34:55 +08:00
|
|
|
history_data_t *curr;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
if( mode_name )
|
|
|
|
{
|
2006-06-16 00:03:27 +08:00
|
|
|
/*
|
|
|
|
Move the current history to the hashtable
|
|
|
|
*/
|
2005-09-20 21:26:39 +08:00
|
|
|
history_to_hash();
|
|
|
|
}
|
2006-06-16 00:03:27 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
See if the new history already exists
|
|
|
|
*/
|
2006-09-08 20:34:55 +08:00
|
|
|
curr = (history_data_t *)hash_get( &history_table,
|
2005-09-20 21:26:39 +08:00
|
|
|
name );
|
|
|
|
if( curr )
|
|
|
|
{
|
2006-06-16 00:03:27 +08:00
|
|
|
/*
|
|
|
|
Yes. Restore it.
|
|
|
|
*/
|
2005-09-20 21:26:39 +08:00
|
|
|
mode_name = (wchar_t *)hash_get_key( &history_table,
|
|
|
|
name );
|
|
|
|
history_current = history_last = curr->last;
|
|
|
|
last_loaded = curr->last_loaded;
|
|
|
|
history_count = curr->count;
|
2006-02-06 23:08:10 +08:00
|
|
|
is_loaded = curr->is_loaded;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-06-16 00:03:27 +08:00
|
|
|
/*
|
|
|
|
Nope. Create a new history list.
|
|
|
|
*/
|
2005-09-20 21:26:39 +08:00
|
|
|
history_count=0;
|
|
|
|
history_last = history_current = last_loaded=0;
|
|
|
|
mode_name = wcsdup( name );
|
2006-02-06 23:08:10 +08:00
|
|
|
is_loaded = 0;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
past_end=1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Print history node
|
|
|
|
*/
|
|
|
|
static void history_save_node( ll_node_t *n, FILE *out )
|
|
|
|
{
|
|
|
|
if( n==0 )
|
|
|
|
return;
|
|
|
|
history_save_node( n->prev, out );
|
|
|
|
fwprintf(out, L"%ls\n", (wchar_t *)(n->data) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Merge the process history with the history on file, write to
|
|
|
|
disc. The merging operation is done so that two concurrently
|
|
|
|
running shells wont erase each others history.
|
|
|
|
*/
|
|
|
|
static void history_save()
|
|
|
|
{
|
|
|
|
wchar_t *fn;
|
|
|
|
FILE *out_stream;
|
|
|
|
/* First we save this sessions history in local variables */
|
|
|
|
ll_node_t *real_pos = history_last, *real_first = last_loaded;
|
|
|
|
|
2006-02-06 23:08:10 +08:00
|
|
|
if( !is_loaded )
|
|
|
|
return;
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !real_first )
|
|
|
|
{
|
|
|
|
real_first = history_current;
|
|
|
|
while( real_first->prev )
|
|
|
|
real_first = real_first->prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( real_pos == real_first )
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Then we load the history from file into the global pointers */
|
|
|
|
history_last=history_current=last_loaded=0;
|
|
|
|
history_count=0;
|
|
|
|
past_end=1;
|
|
|
|
|
2006-06-22 08:12:28 +08:00
|
|
|
if( !history_load() )
|
|
|
|
{
|
|
|
|
|
2006-09-08 20:34:55 +08:00
|
|
|
if( real_pos != 0 )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2006-09-08 20:34:55 +08:00
|
|
|
/*
|
|
|
|
Rewind the session history to the first item which was
|
|
|
|
added in this session
|
|
|
|
*/
|
|
|
|
while( (real_pos->prev != 0) && (real_pos->prev != real_first) )
|
|
|
|
{
|
|
|
|
real_pos = real_pos->prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free old history entries */
|
|
|
|
ll_node_t *kill_node_t = real_pos->prev;
|
|
|
|
while( kill_node_t != 0 )
|
|
|
|
{
|
|
|
|
ll_node_t *tmp = kill_node_t;
|
|
|
|
kill_node_t = kill_node_t->prev;
|
|
|
|
history_free_node( tmp );
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2006-09-08 20:34:55 +08:00
|
|
|
/*
|
|
|
|
Add all the history entries from this session to the global
|
|
|
|
history, free the old version
|
|
|
|
*/
|
|
|
|
while( real_pos != 0 )
|
|
|
|
{
|
|
|
|
ll_node_t *next = real_pos->next;
|
|
|
|
history_add( (wchar_t *)real_pos->data );
|
|
|
|
|
|
|
|
history_free_node( real_pos );
|
|
|
|
real_pos = next;
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2006-09-08 20:34:55 +08:00
|
|
|
/* Save the global history */
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2006-09-08 20:34:55 +08:00
|
|
|
fn = wcsdupcat2( env_get(L"HOME"), L"/.", mode_name, L"_history", 0 );
|
|
|
|
|
|
|
|
out_stream = wfopen( fn, "w" );
|
|
|
|
if( out_stream )
|
|
|
|
{
|
|
|
|
history_save_node( history_last, out_stream );
|
|
|
|
if( fclose( out_stream ) )
|
|
|
|
{
|
|
|
|
debug( 1, L"The following non-fatal error occurred while saving command history to \'%ls\':", fn );
|
|
|
|
wperror( L"fopen" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
|
|
|
debug( 1, L"The following non-fatal error occurred while saving command history to \'%ls\':", fn );
|
|
|
|
wperror( L"fopen" );
|
2006-09-08 20:34:55 +08:00
|
|
|
}
|
|
|
|
free( fn );
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2006-06-22 08:12:28 +08:00
|
|
|
}
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Save the specified mode to file
|
|
|
|
*/
|
|
|
|
|
2006-06-13 05:47:42 +08:00
|
|
|
static void history_destroy_mode( void *name, void *link )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
|
|
|
mode_name = (wchar_t *)name;
|
2006-09-08 20:34:55 +08:00
|
|
|
history_data_t *d = (history_data_t *)link;
|
2005-09-20 21:26:39 +08:00
|
|
|
history_last = history_current = d->last;
|
|
|
|
last_loaded = d->last_loaded;
|
|
|
|
history_count = d->count;
|
|
|
|
past_end=1;
|
|
|
|
|
|
|
|
// fwprintf( stderr, L"Destroy history mode \'%ls\'\n", mode_name );
|
2006-06-16 00:03:27 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( history_last )
|
|
|
|
{
|
|
|
|
history_save();
|
|
|
|
history_current = history_last;
|
|
|
|
while( history_current != 0 )
|
|
|
|
{
|
|
|
|
ll_node_t *tmp = history_current;
|
|
|
|
history_current = history_current->prev;
|
2006-09-08 20:34:55 +08:00
|
|
|
history_free_node( tmp );
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free( d );
|
|
|
|
free( mode_name );
|
|
|
|
}
|
|
|
|
|
|
|
|
void history_destroy()
|
|
|
|
{
|
2006-06-16 00:03:27 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/**
|
|
|
|
Make sure current mode is in table
|
|
|
|
*/
|
|
|
|
history_to_hash();
|
2006-06-16 00:03:27 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/**
|
|
|
|
Save all modes in table
|
|
|
|
*/
|
|
|
|
hash_foreach( &history_table,
|
|
|
|
&history_destroy_mode );
|
|
|
|
|
|
|
|
hash_destroy( &history_table );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Internal search function
|
|
|
|
*/
|
|
|
|
static ll_node_t *history_find( ll_node_t *n, const wchar_t *s )
|
|
|
|
{
|
2006-02-06 23:08:10 +08:00
|
|
|
if( !is_loaded )
|
2006-06-22 08:12:28 +08:00
|
|
|
{
|
|
|
|
if( history_load() )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-06 23:08:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( n == 0 )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if( wcscmp( s, (wchar_t *)n->data ) == 0 )
|
|
|
|
{
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return history_find( n->prev, s );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void history_add( const wchar_t *str )
|
|
|
|
{
|
|
|
|
ll_node_t *old_node;
|
|
|
|
|
2006-02-06 23:08:10 +08:00
|
|
|
if( !is_loaded )
|
2006-06-22 08:12:28 +08:00
|
|
|
{
|
|
|
|
if( history_load() )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-06 23:08:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( wcslen( str ) == 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
past_end=1;
|
|
|
|
|
|
|
|
old_node = history_find( history_last, str );
|
|
|
|
|
|
|
|
if( old_node == 0 )
|
|
|
|
{
|
|
|
|
history_count++;
|
2006-09-08 20:34:55 +08:00
|
|
|
history_current = history_create_node();
|
2006-10-01 23:20:40 +08:00
|
|
|
history_current->data=halloc_wcsdup( global_context, str );
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( old_node == last_loaded )
|
|
|
|
{
|
|
|
|
last_loaded = last_loaded->prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
history_current = old_node;
|
|
|
|
if( old_node == history_last )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( old_node->prev != 0 )
|
|
|
|
old_node->prev->next = old_node->next;
|
|
|
|
if( old_node->next != 0 )
|
|
|
|
old_node->next->prev = old_node->prev;
|
|
|
|
}
|
|
|
|
history_current->next=0;
|
|
|
|
history_current->prev = history_last;
|
|
|
|
if( history_last != 0 )
|
|
|
|
{
|
|
|
|
history_last->next = history_current;
|
|
|
|
}
|
|
|
|
history_last = history_current;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
This function tests if the search string is a match for the given string
|
|
|
|
*/
|
|
|
|
static int history_test( const wchar_t *needle, const wchar_t *haystack )
|
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
|
|
|
return wcsncmp( haystack, needle, wcslen(needle) )==0;
|
|
|
|
*/
|
2006-06-02 03:42:31 +08:00
|
|
|
return !!wcsstr( haystack, needle );
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const wchar_t *history_prev_match( const wchar_t *str )
|
|
|
|
{
|
2006-02-06 23:08:10 +08:00
|
|
|
if( !is_loaded )
|
2006-06-22 08:12:28 +08:00
|
|
|
{
|
|
|
|
if( history_load() )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-06 23:08:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( history_current == 0 )
|
|
|
|
return str;
|
|
|
|
|
|
|
|
if( history_current->prev == 0 )
|
|
|
|
{
|
2005-12-13 02:30:55 +08:00
|
|
|
return str;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
if( past_end )
|
|
|
|
past_end = 0;
|
|
|
|
else
|
|
|
|
history_current = history_current->prev;
|
|
|
|
|
|
|
|
if( history_test( str, history_current->data) )
|
|
|
|
return (wchar_t *)history_current->data;
|
|
|
|
else
|
|
|
|
return history_prev_match( str );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const wchar_t *history_next_match( const wchar_t *str)
|
|
|
|
{
|
2006-02-06 23:08:10 +08:00
|
|
|
if( !is_loaded )
|
2006-06-22 08:12:28 +08:00
|
|
|
{
|
|
|
|
if( history_load() )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-06 23:08:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( history_current == 0 )
|
|
|
|
return str;
|
|
|
|
|
|
|
|
if( history_current->next == 0 )
|
|
|
|
{
|
|
|
|
past_end = 1;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
history_current = history_current->next;
|
|
|
|
|
|
|
|
if( history_test( str, history_current->data ) )
|
|
|
|
return (wchar_t *)history_current->data;
|
|
|
|
else
|
|
|
|
return history_next_match( str );
|
|
|
|
}
|
|
|
|
|
|
|
|
void history_reset()
|
|
|
|
{
|
|
|
|
history_current = history_last;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Move to first history item
|
|
|
|
*/
|
|
|
|
void history_first()
|
|
|
|
{
|
2006-02-06 23:08:10 +08:00
|
|
|
if( is_loaded )
|
|
|
|
while( history_current->prev )
|
|
|
|
history_current = history_current->prev;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t *history_get( int idx )
|
|
|
|
{
|
2006-02-06 23:08:10 +08:00
|
|
|
ll_node_t *n;
|
2005-09-20 21:26:39 +08:00
|
|
|
int i;
|
2006-06-22 08:12:28 +08:00
|
|
|
|
2006-02-06 23:08:10 +08:00
|
|
|
if( !is_loaded )
|
2006-06-22 08:12:28 +08:00
|
|
|
{
|
|
|
|
if( history_load() )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-06 23:08:10 +08:00
|
|
|
n = history_last;
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( idx<0)
|
|
|
|
{
|
|
|
|
debug( 1, L"Tried to access negative history index %d", idx );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i=0; i<idx; i++ )
|
|
|
|
{
|
|
|
|
if( !n )
|
|
|
|
break;
|
|
|
|
n=n->prev;
|
|
|
|
}
|
|
|
|
return n?n->data:0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void history_sanity_check()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( history_current != 0 )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int history_ok = 1;
|
|
|
|
int found_current = 0;
|
|
|
|
|
|
|
|
validate_pointer( history_last, L"History root", 1);
|
|
|
|
|
|
|
|
ll_node_t *tmp = history_last;
|
|
|
|
for( i=0; i<history_count; i++ )
|
|
|
|
{
|
|
|
|
found_current |= tmp == history_current;
|
|
|
|
|
|
|
|
if( tmp == 0 )
|
|
|
|
{
|
|
|
|
history_ok = 0;
|
|
|
|
debug( 1, L"History items missing" );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( (tmp->prev != 0) && (tmp->prev->next != tmp ) )
|
|
|
|
{
|
|
|
|
history_ok = 0;
|
|
|
|
debug( 1, L"History items order is inconsistent" );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
validate_pointer( tmp->data, L"History data", 1);
|
|
|
|
if( tmp->data == 0 )
|
|
|
|
{
|
|
|
|
history_ok = 0;
|
|
|
|
debug( 1, L"Empty history item" );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
validate_pointer( tmp->prev, L"History node", 1);
|
|
|
|
tmp = tmp->prev;
|
|
|
|
}
|
|
|
|
if( tmp != 0 )
|
|
|
|
{
|
|
|
|
history_ok = 0;
|
|
|
|
debug( 1, L"History list too long" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( (i!= history_count )|| (!found_current))
|
|
|
|
{
|
|
|
|
debug( 1, L"No history item selected" );
|
|
|
|
history_ok=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !history_ok )
|
|
|
|
{
|
|
|
|
sanity_lose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|