2 * ion/mod_query/history.h
4 * Copyright (c) Tuomo Valkonen 1999-2009.
6 * See the included file LICENSE for details.
11 #include <ioncore/common.h>
12 #include <libextl/extl.h>
17 #define HISTORY_SIZE 1024
20 static int hist_head=HISTORY_SIZE;
21 static int hist_count=0;
22 static char *hist[HISTORY_SIZE];
27 if(i<0 || i>=hist_count)
29 return (hist_head+i)%HISTORY_SIZE;
34 * Push an entry into line editor history.
37 bool mod_query_history_push(const char *str)
44 mod_query_history_push_(s);
50 void mod_query_history_push_(char *str)
52 int ndx=mod_query_history_search(str, 0, FALSE, TRUE);
56 return; /* First entry already */
61 while(++ndx<hist_count){
71 hist_head=HISTORY_SIZE-1;
73 if(hist_count==HISTORY_SIZE)
74 free(hist[hist_head]);
83 * Get entry at index \var{n} in line editor history, 0 being the latest.
87 const char *mod_query_history_get(int n)
90 return (i<0 ? NULL : hist[i]);
95 * Clear line editor history.
98 void mod_query_history_clear()
100 while(hist_count!=0){
101 free(hist[hist_head]);
103 if(++hist_head==HISTORY_SIZE)
106 hist_head=HISTORY_SIZE;
111 static bool match(const char *h, const char *b, bool exact)
118 /* Special case: search in any context. */
119 if(*b=='*' && *(b+1)==':'){
128 : strncmp(h, b, strlen(b))==0);
132 static const char *skip_colon(const char *s)
134 const char *p=strchr(s, ':');
135 return (p!=NULL ? p+1 : s);
140 * Try to find matching history entry. Returns -1 if none was
141 * found. The parameter \var{from} specifies where to start
142 * searching from, and \var{bwd} causes backward search from
143 * that point. If \var{exact} is not set, \var{s} only required
144 * to be a prefix of the match.
148 int mod_query_history_search(const char *s, int from, bool bwd, bool exact)
151 int i=get_index(from);
154 if(match(hist[i], s, exact))
164 uint mod_query_history_complete(const char *s, char ***h_ret)
166 char **h=ALLOC_N(char *, hist_count);
172 for(i=0; i<hist_count; i++){
176 if(match(hist[j], s, FALSE)){
177 h[n]=scopy(skip_colon(hist[j]));
193 * Return table of history entries.
197 ExtlTab mod_query_history_table()
199 ExtlTab tab=extl_create_table();
202 for(i=0; i<hist_count; i++){
204 extl_table_seti_s(tab, i+1, hist[j]);