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