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