1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
/*
* ion/mod_query/history.h
*
* Copyright (c) Tuomo Valkonen 1999-2007.
*
* See the included file LICENSE for details.
*/
#include <string.h>
#include <ioncore/common.h>
#include <libextl/extl.h>
#include "history.h"
#define HISTORY_SIZE 1024
static int hist_head=HISTORY_SIZE;
static int hist_count=0;
static char *hist[HISTORY_SIZE];
int get_index(int i)
{
if(i<0 || i>=hist_count)
return -1;
return (hist_head+i)%HISTORY_SIZE;
}
/*EXTL_DOC
* Push an entry into line editor history.
*/
EXTL_EXPORT
bool mod_query_history_push(const char *str)
{
char *s=scopy(str);
if(s==NULL)
return FALSE;
mod_query_history_push_(s);
return TRUE;
}
void mod_query_history_push_(char *str)
{
int ndx=mod_query_history_search(str, 0, FALSE, TRUE);
if(ndx==0){
free(str);
return; /* First entry already */
}else if(ndx>0){
int i, j;
i=get_index(ndx);
free(hist[i]);
while(++ndx<hist_count){
j=get_index(ndx);
hist[i]=hist[j];
i=j;
}
hist_count--;
}
hist_head--;
if(hist_head<0)
hist_head=HISTORY_SIZE-1;
if(hist_count==HISTORY_SIZE)
free(hist[hist_head]);
else
hist_count++;
hist[hist_head]=str;
}
/*EXTL_DOC
* Get entry at index \var{n} in line editor history, 0 being the latest.
*/
EXTL_SAFE
EXTL_EXPORT
const char *mod_query_history_get(int n)
{
int i=get_index(n);
return (i<0 ? NULL : hist[i]);
}
/*EXTL_DOC
* Clear line editor history.
*/
EXTL_EXPORT
void mod_query_history_clear()
{
while(hist_count!=0){
free(hist[hist_head]);
hist_count--;
if(++hist_head==HISTORY_SIZE)
hist_head=0;
}
hist_head=HISTORY_SIZE;
}
static bool match(const char *h, const char *b, bool exact)
{
const char *h_;
if(b==NULL)
return TRUE;
/* Special case: search in any context. */
if(*b=='*' && *(b+1)==':'){
b=b+2;
h_=strchr(h, ':');
if(h_!=NULL)
h=h_+1;
}
return (exact
? strcmp(h, b)==0
: strncmp(h, b, strlen(b))==0);
}
static const char *skip_colon(const char *s)
{
const char *p=strchr(s, ':');
return (p!=NULL ? p+1 : s);
}
/*EXTL_DOC
* Try to find matching history entry. Returns -1 if none was
* found. The parameter \var{from} specifies where to start
* searching from, and \var{bwd} causes backward search from
* that point. If \var{exact} is not set, \var{s} only required
* to be a prefix of the match.
*/
EXTL_SAFE
EXTL_EXPORT
int mod_query_history_search(const char *s, int from, bool bwd, bool exact)
{
while(1){
int i=get_index(from);
if(i<0)
return -1;
if(match(hist[i], s, exact))
return from;
if(bwd)
from--;
else
from++;
}
}
uint mod_query_history_complete(const char *s, char ***h_ret)
{
char **h=ALLOC_N(char *, hist_count);
int i, n=0;
if(h==NULL)
return 0;
for(i=0; i<hist_count; i++){
int j=get_index(i);
if(j<0)
break;
if(match(hist[j], s, FALSE)){
h[n]=scopy(skip_colon(hist[j]));
if(h[n]!=NULL)
n++;
}
}
if(n==0)
free(h);
else
*h_ret=h;
return n;
}
/*EXTL_DOC
* Return table of history entries.
*/
EXTL_SAFE
EXTL_EXPORT
ExtlTab mod_query_history_table()
{
ExtlTab tab=extl_create_table();
int i;
for(i=0; i<hist_count; i++){
int j=get_index(i);
extl_table_seti_s(tab, i+1, hist[j]);
}
return tab;
}
|