tabcomplete only if readline is in use
[yaz-moved-to-github.git] / client / tabcomplete.c
1 /*
2  * Copyright (C) 1995-2005, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: tabcomplete.c,v 1.13 2006-05-07 19:37:23 adam Exp $
6  */
7
8 #include <string.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #include <yaz/oid.h>
13 #include "tabcomplete.h"
14
15 #if HAVE_READLINE_READLINE_H
16 extern char** curret_global_list;
17
18 /* ***************************************************************************
19  *
20  * generic completer 
21  * 
22  * ***************************************************************************/
23
24 char* complete_from_list(char* completions[], const char *text, int state)
25 {       
26     static int idx;
27     
28     if(!completions) return NULL;
29     if(state==0) {
30         idx = 0;
31     }
32     for(; completions[idx]; ++ idx) {
33         if(!
34 #ifdef WIN32
35            _strnicmp
36 #else
37            strncasecmp
38 #endif              
39            (completions[idx],text,strlen(text))) {
40             ++idx; /* skip this entry on the next run */ 
41             return (char*)strdup(completions[idx-1]);
42         };
43     };
44     return NULL;
45 }
46
47
48 /* ***************************************************************************
49  * 
50  * code for getting a list of valid strings from the oid subsystem
51  * 
52  * ***************************************************************************/
53    
54
55 typedef struct {
56     oid_class oclass;
57     char** values;
58     size_t index;
59     size_t max;
60 } oid_callback_t;
61
62 /*!
63   This is the call back function given to oid_trav... it updates the list
64   of pointers into the oid owned data 
65 */
66
67 void oid_loader(struct oident* oid, void* data_)
68 {
69     oid_callback_t* data=(oid_callback_t*) data_;
70     
71     
72     if((oid->oclass == CLASS_GENERAL) || (oid->oclass == data->oclass)) {
73         if(data->index==data->max) {
74                         data->values=(char**)realloc(data->values,((data->max+1)*2)*sizeof(char*));
75                         data->max=(data->max+1)*2 - 1;
76         };
77         data->values[data->index]=oid->desc;
78         ++data->index;          
79     }
80 }
81
82 char** build_list_for_oclass(oid_class oclass) {        
83     oid_callback_t data;        
84     data.values = (char **) calloc(10,sizeof(char*));
85     data.index = 0;
86     data.max = 9;
87     data.oclass = oclass;
88     
89     oid_trav(oid_loader, &data);
90     
91     data.values[data.index]=0;
92     return data.values;    
93 }
94
95 /* ***************************************************************************
96  * 
97  * the completer functions 
98  * 
99  * ***************************************************************************/
100
101 char* complete_querytype(const char *text, int state)
102 {
103     char* querytypes[] = {"ccl2rpn","prefix","cclrpn","ccl","cql", 0};
104     return complete_from_list(querytypes,text,state);  
105 }
106
107 char* complete_auto_reconnect(const char *text, int state)
108 {
109     char* querytypes[] = {"on","off",0};
110     return complete_from_list(querytypes,text,state);  
111 }
112
113
114 char* complete_format(const char* text, int state)
115 {
116     char** list=build_list_for_oclass(CLASS_RECSYN);
117     char* res=complete_from_list(list,text,state);  
118     
119     free(list); 
120     return res;
121 }
122
123 char* complete_schema(const char* text, int state)
124 {
125     char** list=build_list_for_oclass(CLASS_SCHEMA);
126     char* res=complete_from_list(list,text,state);  
127     
128     free(list); 
129     return res;
130 }
131
132
133 char* complete_attributeset(const char* text, int state)
134 {
135     char** list=build_list_for_oclass(CLASS_ATTSET);
136     char* res=complete_from_list(list,text,state);  
137     
138     free(list); 
139     return res;
140 }
141
142
143 char* default_completer(const char* text, int state)
144 {
145     return complete_from_list(curret_global_list,text,state);
146 }
147 #endif
148
149 /*
150  * Local variables:
151  * c-basic-offset: 4
152  * indent-tabs-mode: nil
153  * End:
154  * vim: shiftwidth=4 tabstop=8 expandtab
155  */
156