870188aa5e36f7fcbc1e705d17cf3df5b97ec88a
[yaz-moved-to-github.git] / client / tabcomplete.c
1 /*
2  * Copyright (C) 1995-2006, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: tabcomplete.c,v 1.15 2006-05-07 19:43:00 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** current_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
85     oid_callback_t data;        
86     data.values = (char **) calloc(10,sizeof(char*));
87     data.index = 0;
88     data.max = 9;
89     data.oclass = oclass;
90     
91     oid_trav(oid_loader, &data);
92     
93     data.values[data.index]=0;
94     return data.values;    
95 }
96
97 /* ***************************************************************************
98  * 
99  * the completer functions 
100  * 
101  * ***************************************************************************/
102
103 char* complete_querytype(const char *text, int state)
104 {
105     char* querytypes[] = {"ccl2rpn","prefix","cclrpn","ccl","cql", 0};
106     return complete_from_list(querytypes,text,state);  
107 }
108
109 char* complete_auto_reconnect(const char *text, int state)
110 {
111     char* querytypes[] = {"on","off",0};
112     return complete_from_list(querytypes,text,state);  
113 }
114
115
116 char* complete_format(const char* text, int state)
117 {
118     char** list=build_list_for_oclass(CLASS_RECSYN);
119     char* res=complete_from_list(list,text,state);  
120     
121     free(list); 
122     return res;
123 }
124
125 char* complete_schema(const char* text, int state)
126 {
127     char** list = build_list_for_oclass(CLASS_SCHEMA);
128     char* res = complete_from_list(list,text,state);  
129     
130     free(list); 
131     return res;
132 }
133
134
135 char* complete_attributeset(const char* text, int state)
136 {
137     char** list = build_list_for_oclass(CLASS_ATTSET);
138     char* res = complete_from_list(list,text,state);  
139     
140     free(list); 
141     return res;
142 }
143
144
145 char* default_completer(const char* text, int state)
146 {
147     return complete_from_list(current_global_list, text, state);
148 }
149
150 /*
151  * Local variables:
152  * c-basic-offset: 4
153  * indent-tabs-mode: nil
154  * End:
155  * vim: shiftwidth=4 tabstop=8 expandtab
156  */
157