2 * ion/mod_query/history.h
4 * Copyright (c) Tuomo Valkonen 1999-2006.
6 * Ion is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
14 #include <ioncore/common.h>
15 #include <libextl/extl.h>
20 #define HISTORY_SIZE 1024
23 static int hist_head=HISTORY_SIZE;
24 static int hist_count=0;
25 static char *hist[HISTORY_SIZE];
30 if(i<0 || i>=hist_count)
32 return (hist_head+i)%HISTORY_SIZE;
37 * Push an entry into line editor history.
40 bool mod_query_history_push(const char *str)
47 mod_query_history_push_(s);
53 void mod_query_history_push_(char *str)
55 int ndx=mod_query_history_search(str, 0, FALSE);
58 return; /* First entry already */
63 while(++ndx<hist_count){
73 hist_head=HISTORY_SIZE-1;
75 if(hist_count==HISTORY_SIZE)
76 free(hist[hist_head]);
85 * Get entry at index \var{n} in line editor history, 0 being the latest.
89 const char *mod_query_history_get(int n)
92 return (i<0 ? NULL : hist[i]);
97 * Clear line editor history.
100 void mod_query_history_clear()
102 while(hist_count!=0){
103 free(hist[hist_head]);
105 if(++hist_head==HISTORY_SIZE)
108 hist_head=HISTORY_SIZE;
113 static bool match(const char *h, const char *b)
120 /* Special case: search in any context. */
121 if(*b=='*' && *(b+1)==':'){
128 return (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
147 int mod_query_history_search(const char *s, int from, bool bwd)
150 int i=get_index(from);
153 if(match(hist[i], s))
163 uint mod_query_history_complete(const char *s, char ***h_ret)
165 char **h=ALLOC_N(char *, hist_count);
171 for(i=0; i<hist_count; i++){
175 if(match(hist[j], s)){
176 h[n]=scopy(skip_colon(hist[j]));
192 * Return table of history entries.
196 ExtlTab mod_query_history_table()
198 ExtlTab tab=extl_create_table();
201 for(i=0; i<hist_count; i++){
203 extl_table_seti_s(tab, i+1, hist[j]);