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