Fixed bug #687: Missing log lines. Immediate logging (i.e. flush) is no
[yaz-moved-to-github.git] / client / tabcomplete.c
1 /*
2  * Copyright (C) 1995-2006, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: tabcomplete.c,v 1.16 2006-05-07 20:57:26 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 completer 
18  * 
19  * ***************************************************************************/
20
21 char *complete_from_list(char* completions[], const char *text, int state)
22 {       
23 #if HAVE_READLINE_READLINE_H
24     static int idx;
25     
26     if(!completions) return NULL;
27     if(state==0) {
28         idx = 0;
29     }
30     for(; completions[idx]; ++ idx) {
31         if(!
32 #ifdef WIN32
33            _strnicmp
34 #else
35            strncasecmp
36 #endif              
37            (completions[idx],text,strlen(text))) {
38             ++idx; /* skip this entry on the next run */ 
39             return (char*)strdup(completions[idx-1]);
40         };
41     };
42 #endif
43     return NULL;
44 }
45
46
47 /* ***************************************************************************
48  * 
49  * code for getting a list of valid strings from the oid subsystem
50  * 
51  * ***************************************************************************/
52    
53
54 typedef struct {
55     oid_class oclass;
56     char** values;
57     size_t index;
58     size_t max;
59 } oid_callback_t;
60
61 /*!
62   This is the call back function given to oid_trav... it updates the list
63   of pointers into the oid owned data 
64 */
65
66 void oid_loader(struct oident* oid, void* data_)
67 {
68     oid_callback_t* data=(oid_callback_t*) data_;
69     
70     
71     if((oid->oclass == CLASS_GENERAL) || (oid->oclass == data->oclass)) {
72         if(data->index==data->max) {
73                         data->values=(char**)realloc(data->values,((data->max+1)*2)*sizeof(char*));
74                         data->max=(data->max+1)*2 - 1;
75         };
76         data->values[data->index]=oid->desc;
77         ++data->index;          
78     }
79 }
80
81 char** build_list_for_oclass(oid_class oclass)
82
83     oid_callback_t data;        
84     data.values = (char **) calloc(10,sizeof(char*));
85     data.index = 0;
86     data.max = 9;
87     data.oclass = oclass;
88     
89     oid_trav(oid_loader, &data);
90     
91     data.values[data.index]=0;
92     return data.values;    
93 }
94
95 /* ***************************************************************************
96  * 
97  * the completer functions 
98  * 
99  * ***************************************************************************/
100
101 char* complete_querytype(const char *text, int state)
102 {
103     char* querytypes[] = {"ccl2rpn","prefix","cclrpn","ccl","cql", 0};
104     return complete_from_list(querytypes,text,state);  
105 }
106
107 char* complete_auto_reconnect(const char *text, int state)
108 {
109     char* querytypes[] = {"on","off",0};
110     return complete_from_list(querytypes,text,state);  
111 }
112
113
114 char* complete_format(const char* text, int state)
115 {
116     char** list=build_list_for_oclass(CLASS_RECSYN);
117     char* res=complete_from_list(list,text,state);  
118     
119     free(list); 
120     return res;
121 }
122
123 char* complete_schema(const char* text, int state)
124 {
125     char** list = build_list_for_oclass(CLASS_SCHEMA);
126     char* res = complete_from_list(list,text,state);  
127     
128     free(list); 
129     return res;
130 }
131
132
133 char* complete_attributeset(const char* text, int state)
134 {
135     char** list = build_list_for_oclass(CLASS_ATTSET);
136     char* res = complete_from_list(list,text,state);  
137     
138     free(list); 
139     return res;
140 }
141
142
143 /*
144  * Local variables:
145  * c-basic-offset: 4
146  * indent-tabs-mode: nil
147  * End:
148  * vim: shiftwidth=4 tabstop=8 expandtab
149  */
150