For OIDs use Odr_oid type everywhere, i.e. do not assume Odr_oid=int.
[yaz-moved-to-github.git] / client / tabcomplete.c
1 /*
2  * Copyright (C) 1995-2007, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: tabcomplete.c,v 1.19 2007-05-08 08:22:35 adam Exp $
6  */
7
8 #include <string.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #include "tabcomplete.h"
13 #include <yaz/oid_db.h>
14
15 /* ***************************************************************************
16  *
17  * generic completer 
18  * 
19  * ***************************************************************************/
20
21 char *complete_from_list(const char** completions,
22                          const char *text, int state)
23 {       
24 #if HAVE_READLINE_READLINE_H
25     static int idx;
26     
27     if (!completions) 
28         return NULL;
29     if (state==0) 
30         idx = 0;
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 #endif
44     return NULL;
45 }
46
47
48 /* ***************************************************************************
49  * 
50  * code for getting a list of valid strings from the oid subsystem
51  * 
52  * ***************************************************************************/
53    
54
55 typedef struct {
56     int oclass;
57     const char** values;
58     size_t index;
59     size_t max;
60 } oid_callback_t;
61
62 /*!
63   This is the call back function given to oid_trav... it updates the list
64   of pointers into the oid owned data 
65 */
66
67 void oid_loader(const Odr_oid *oid,
68                 oid_class oclass, const char *name, void* data_)
69 {
70     oid_callback_t* data=(oid_callback_t*) data_;
71     
72     if ((oclass == CLASS_GENERAL) || (oclass == data->oclass))
73     {
74         if (data->index==data->max) 
75         {
76             data->values=(const char**)
77                 realloc(data->values,((data->max+1)*2)*sizeof(char*));
78             data->max=(data->max+1)*2 - 1;
79         }
80         data->values[data->index] = name;
81         ++data->index;          
82     }
83 }
84
85 const char** build_list_for_oclass(oid_class oclass)
86
87     oid_callback_t data;        
88     data.values = (const char **) calloc(10,sizeof(char*));
89     data.index = 0;
90     data.max = 9;
91     data.oclass = oclass;
92
93     yaz_oid_trav(yaz_oid_std(), oid_loader, &data);
94
95     data.values[data.index]=0;
96     return data.values;    
97 }
98
99 /* ***************************************************************************
100  * 
101  * the completer functions 
102  * 
103  * ***************************************************************************/
104
105 char* complete_querytype(const char *text, int state)
106 {
107     static const char* querytypes[] = {"ccl2rpn","prefix","cclrpn","ccl","cql", 0};
108     return complete_from_list(querytypes,text,state);  
109 }
110
111 char* complete_auto_reconnect(const char *text, int state)
112 {
113     static const char* querytypes[] = {"on","off",0};
114     return complete_from_list(querytypes,text,state);  
115 }
116
117
118 char* complete_format(const char* text, int state)
119 {
120     const char** list = build_list_for_oclass(CLASS_RECSYN);
121     char* res=complete_from_list(list,text,state);  
122     
123     free(list); 
124     return res;
125 }
126
127 char* complete_schema(const char* text, int state)
128 {
129     const char** list = build_list_for_oclass(CLASS_SCHEMA);
130     char* res = complete_from_list(list,text,state);  
131     
132     free(list); 
133     return res;
134 }
135
136
137 char* complete_attributeset(const char* text, int state)
138 {
139     const char** list = build_list_for_oclass(CLASS_ATTSET);
140     char* res = complete_from_list(list,text,state);  
141     
142     free(list); 
143     return res;
144 }
145
146
147 /*
148  * Local variables:
149  * c-basic-offset: 4
150  * indent-tabs-mode: nil
151  * End:
152  * vim: shiftwidth=4 tabstop=8 expandtab
153  */
154