546c23977666be828b6d514603b1ee40dae14e5e
[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.14 2006-05-07 19:38:58 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 extern char** curret_global_list;
16
17 /* ***************************************************************************
18  *
19  * generic completer 
20  * 
21  * ***************************************************************************/
22
23 char* complete_from_list(char* completions[], const char *text, int state)
24 {       
25 #if HAVE_READLINE_READLINE_H
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 #endif
45     return NULL;
46 }
47
48
49 /* ***************************************************************************
50  * 
51  * code for getting a list of valid strings from the oid subsystem
52  * 
53  * ***************************************************************************/
54    
55
56 typedef struct {
57     oid_class oclass;
58     char** values;
59     size_t index;
60     size_t max;
61 } oid_callback_t;
62
63 /*!
64   This is the call back function given to oid_trav... it updates the list
65   of pointers into the oid owned data 
66 */
67
68 void oid_loader(struct oident* oid, void* data_)
69 {
70     oid_callback_t* data=(oid_callback_t*) data_;
71     
72     
73     if((oid->oclass == CLASS_GENERAL) || (oid->oclass == data->oclass)) {
74         if(data->index==data->max) {
75                         data->values=(char**)realloc(data->values,((data->max+1)*2)*sizeof(char*));
76                         data->max=(data->max+1)*2 - 1;
77         };
78         data->values[data->index]=oid->desc;
79         ++data->index;          
80     }
81 }
82
83 char** build_list_for_oclass(oid_class oclass) {        
84     oid_callback_t data;        
85     data.values = (char **) calloc(10,sizeof(char*));
86     data.index = 0;
87     data.max = 9;
88     data.oclass = oclass;
89     
90     oid_trav(oid_loader, &data);
91     
92     data.values[data.index]=0;
93     return data.values;    
94 }
95
96 /* ***************************************************************************
97  * 
98  * the completer functions 
99  * 
100  * ***************************************************************************/
101
102 char* complete_querytype(const char *text, int state)
103 {
104     char* querytypes[] = {"ccl2rpn","prefix","cclrpn","ccl","cql", 0};
105     return complete_from_list(querytypes,text,state);  
106 }
107
108 char* complete_auto_reconnect(const char *text, int state)
109 {
110     char* querytypes[] = {"on","off",0};
111     return complete_from_list(querytypes,text,state);  
112 }
113
114
115 char* complete_format(const char* text, int state)
116 {
117     char** list=build_list_for_oclass(CLASS_RECSYN);
118     char* res=complete_from_list(list,text,state);  
119     
120     free(list); 
121     return res;
122 }
123
124 char* complete_schema(const char* text, int state)
125 {
126     char** list=build_list_for_oclass(CLASS_SCHEMA);
127     char* res=complete_from_list(list,text,state);  
128     
129     free(list); 
130     return res;
131 }
132
133
134 char* complete_attributeset(const char* text, int state)
135 {
136     char** list=build_list_for_oclass(CLASS_ATTSET);
137     char* res=complete_from_list(list,text,state);  
138     
139     free(list); 
140     return res;
141 }
142
143
144 char* default_completer(const char* text, int state)
145 {
146     return complete_from_list(curret_global_list,text,state);
147 }
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