9513965ec4114e992c350a7381869e1b82567e69
[yaz-moved-to-github.git] / client / tabcomplete.c
1 #include "tabcomplete.h"
2 #include <string.h>
3 #include <yaz/oid.h>
4 #include <stdio.h>
5
6
7 /* *****************************************************************************
8  *
9  * generic compleater 
10  * 
11  * *****************************************************************************/
12
13 char* complete_from_list(char* completions[], char *text, int state) {
14         static idx;
15         if(state==0) {
16                 idx = 0;
17         }
18         for(; completions[idx]; ++ idx) {
19                 if(!strncmp(completions[idx],text,strlen(text))) {
20                         ++idx; /* skip this entry on the next run */ 
21                         return (char*)strdup(completions[idx-1]);
22                 };
23         };
24         return NULL;
25 };
26
27
28 /* *****************************************************************************
29  * 
30  * code for getting a list of valid strings from the oid subsystem
31  * 
32  * *****************************************************************************/
33    
34
35 typedef struct {
36         oid_class oclass;
37         char** values;
38         size_t index;
39         size_t max;
40 } oid_callback_t;
41
42 /*!
43   This is the call back function given to oid_trav... it updates the list of pointers into the oid
44   owned data 
45 */
46
47 void oid_loader(struct oident* oid, void* data_) {
48         oid_callback_t* data=(oid_callback_t*) data_;
49         
50         //fprintf(stderr,"ja7: called with %d: %s\n",oid->oclass,oid->desc);    
51         if((oid->oclass == CLASS_GENERAL) || (oid->oclass == data->oclass)) {
52                 if(data->index==data->max) {
53                         data->values=(char**)realloc(data->values,((data->max+1)*2)*sizeof(char*));
54                         data->max=(data->max+1)*2 - 1;
55                 };
56                 data->values[data->index]=oid->desc;
57                 ++data->index;          
58         }
59 };
60
61 char** build_list_for_oclass(oid_class oclass) {        
62         oid_callback_t data;    
63         data.values = calloc(10,sizeof(char*));
64         data.index = 0;
65         data.max = 9;
66         data.oclass = oclass;
67                 
68         oid_trav(oid_loader, &data);
69         
70         data.values[data.index]=0;
71         return data.values;        
72 };
73
74 /* *****************************************************************************
75  * 
76  * the compleater functions 
77  * 
78  * *****************************************************************************/
79
80 char* complete_querytype(char *text, int state) {
81   char* querytypes[] = {"ccl2rpn","prefix","cclrpn","ccl",0};
82   return complete_from_list(querytypes,text,state);  
83 };
84
85
86 char* complete_format(char* text, int state) {
87         char** list=build_list_for_oclass(CLASS_RECSYN);
88         char* res=complete_from_list(list,text,state);  
89         
90         free(list);     
91         return res;
92 };
93
94 char* complete_schema(char* text, int state) {
95         char** list=build_list_for_oclass(CLASS_SCHEMA);
96         char* res=complete_from_list(list,text,state);  
97         
98         free(list);     
99         return res;
100 };
101
102
103 char* complete_attributeset(char* text, int state) {
104         char** list=build_list_for_oclass(CLASS_ATTSET);
105         char* res=complete_from_list(list,text,state);  
106         
107         free(list);     
108         return res;
109 };
110
111 /*
112  * Local variables:
113  * tab-width: 4
114  * c-basic-offset: 4
115  * End:
116  */