01bb0e0914225378d6a44fea35ee77280d80aafb
[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.11 2005-06-24 19:56:52 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     static int idx;
26     
27     if(!completions) return NULL;
28     if(state==0) {
29         idx = 0;
30     }
31     for(; completions[idx]; ++ idx) {
32         if(!
33 #ifdef WIN32
34            _strnicmp
35 #else
36            strncasecmp
37 #endif              
38            (completions[idx],text,strlen(text))) {
39             ++idx; /* skip this entry on the next run */ 
40             return (char*)strdup(completions[idx-1]);
41         };
42     };
43     return NULL;
44 }
45
46
47 /* ***************************************************************************
48  * 
49  * code for getting a list of valid strings from the oid subsystem
50  * 
51  * ***************************************************************************/
52    
53
54 typedef struct {
55     oid_class oclass;
56     char** values;
57     size_t index;
58     size_t max;
59 } oid_callback_t;
60
61 /*!
62   This is the call back function given to oid_trav... it updates the list
63   of pointers into the oid owned data 
64 */
65
66 void oid_loader(struct oident* oid, void* data_)
67 {
68     oid_callback_t* data=(oid_callback_t*) data_;
69     
70     
71     if((oid->oclass == CLASS_GENERAL) || (oid->oclass == data->oclass)) {
72         if(data->index==data->max) {
73                         data->values=(char**)realloc(data->values,((data->max+1)*2)*sizeof(char*));
74                         data->max=(data->max+1)*2 - 1;
75         };
76         data->values[data->index]=oid->desc;
77         ++data->index;          
78     }
79 }
80
81 char** build_list_for_oclass(oid_class oclass) {        
82     oid_callback_t data;        
83     data.values = (char **) calloc(10,sizeof(char*));
84     data.index = 0;
85     data.max = 9;
86     data.oclass = oclass;
87     
88     oid_trav(oid_loader, &data);
89     
90     data.values[data.index]=0;
91     return data.values;    
92 }
93
94 /* ***************************************************************************
95  * 
96  * the completer functions 
97  * 
98  * ***************************************************************************/
99
100 char* complete_querytype(const char *text, int state)
101 {
102     char* querytypes[] = {"ccl2rpn","prefix","cclrpn","ccl","cql", 0};
103     return complete_from_list(querytypes,text,state);  
104 }
105
106 char* complete_auto_reconnect(const char *text, int state)
107 {
108     char* querytypes[] = {"on","off",0};
109     return complete_from_list(querytypes,text,state);  
110 }
111
112
113 char* complete_format(const char* text, int state)
114 {
115     char** list=build_list_for_oclass(CLASS_RECSYN);
116     char* res=complete_from_list(list,text,state);  
117     
118     free(list); 
119     return res;
120 }
121
122 char* complete_schema(const char* text, int state)
123 {
124     char** list=build_list_for_oclass(CLASS_SCHEMA);
125     char* res=complete_from_list(list,text,state);  
126     
127     free(list); 
128     return res;
129 }
130
131
132 char* complete_attributeset(const char* text, int state)
133 {
134     char** list=build_list_for_oclass(CLASS_ATTSET);
135     char* res=complete_from_list(list,text,state);  
136     
137     free(list); 
138     return res;
139 }
140
141
142 char* default_completer(const char* text, int state)
143 {
144     return complete_from_list(curret_global_list,text,state);
145 }
146