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