2 * ion/mod_query/history.h
4 * Copyright (c) Tuomo Valkonen 1999-2007.
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, TRUE);
59 return; /* First entry already */
64 while(++ndx<hist_count){
74 hist_head=HISTORY_SIZE-1;
76 if(hist_count==HISTORY_SIZE)
77 free(hist[hist_head]);
86 * Get entry at index \var{n} in line editor history, 0 being the latest.
90 const char *mod_query_history_get(int n)
93 return (i<0 ? NULL : hist[i]);
98 * Clear line editor history.
101 void mod_query_history_clear()
103 while(hist_count!=0){
104 free(hist[hist_head]);
106 if(++hist_head==HISTORY_SIZE)
109 hist_head=HISTORY_SIZE;
114 static bool match(const char *h, const char *b, bool exact)
121 /* Special case: search in any context. */
122 if(*b=='*' && *(b+1)==':'){
131 : strncmp(h, b, strlen(b))==0);
135 static const char *skip_colon(const char *s)
137 const char *p=strchr(s, ':');
138 return (p!=NULL ? p+1 : s);
143 * Try to find matching history entry. Returns -1 if none was
144 * found. The parameter \var{from} specifies where to start
145 * searching from, and \var{bwd} causes backward search from
146 * that point. If \var{exact} is not set, \var{s} only required
147 * to be a prefix of the match.
151 int mod_query_history_search(const char *s, int from, bool bwd, bool exact)
154 int i=get_index(from);
157 if(match(hist[i], s, exact))
167 uint mod_query_history_complete(const char *s, char ***h_ret)
169 char **h=ALLOC_N(char *, hist_count);
175 for(i=0; i<hist_count; i++){
179 if(match(hist[j], s, FALSE)){
180 h[n]=scopy(skip_colon(hist[j]));
196 * Return table of history entries.
200 ExtlTab mod_query_history_table()
202 ExtlTab tab=extl_create_table();
205 for(i=0; i<hist_count; i++){
207 extl_table_seti_s(tab, i+1, hist[j]);