Fixed bug in inetd code. The server listened on tcp:@:9999 even
[yaz-moved-to-github.git] / retrieval / d1_espec.c
1 /*
2  * Copyright (c) 1995-1997, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_espec.c,v $
7  * Revision 1.13  1997-11-24 11:33:56  adam
8  * Using function odr_nullval() instead of global ODR_NULLVAL when
9  * appropriate.
10  *
11  * Revision 1.12  1997/10/31 12:20:09  adam
12  * Improved memory debugging for xmalloc/nmem.c. References to NMEM
13  * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
14  * Bug fix: missing fclose in data1_read_espec1.
15  *
16  * Revision 1.11  1997/09/29 13:18:59  adam
17  * Added function, oid_ent_to_oid, to replace the function
18  * oid_getoidbyent, which is not thread safe.
19  *
20  * Revision 1.10  1997/09/29 07:21:10  adam
21  * Added typecast to avoid warnings on MSVC.
22  *
23  * Revision 1.9  1997/09/17 12:10:35  adam
24  * YAZ version 1.4.
25  *
26  * Revision 1.8  1997/09/05 09:50:56  adam
27  * Removed global data1_tabpath - uses data1_get_tabpath() instead.
28  *
29  * Revision 1.7  1997/05/14 06:54:02  adam
30  * C++ support.
31  *
32  * Revision 1.6  1996/07/06 19:58:34  quinn
33  * System headerfiles gathered in yconfig
34  *
35  * Revision 1.5  1996/01/02  08:57:44  quinn
36  * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
37  *
38  * Revision 1.4  1995/12/05  11:16:10  quinn
39  * Fixed malloc of 0.
40  *
41  * Revision 1.3  1995/11/13  09:27:34  quinn
42  * Fiddling with the variant stuff.
43  *
44  * Revision 1.2  1995/11/01  16:34:56  quinn
45  * Making data1 look for tables in data1_tabpath
46  *
47  * Revision 1.1  1995/11/01  11:56:07  quinn
48  * Added Retrieval (data management) functions en masse.
49  *
50  *
51  */
52
53
54 #include <stdlib.h>
55 #include <assert.h>
56 #include <string.h>
57 #include <ctype.h>
58 #include <odr.h>
59 #include <proto.h>
60 #include <log.h>
61 #include <readconf.h>
62 #include <tpath.h>
63 #include <data1.h>
64
65 static Z_Variant *read_variant(int argc, char **argv, NMEM nmem)
66 {
67     Z_Variant *r = nmem_malloc(nmem, sizeof(*r));
68     oident var1;
69     int i;
70     int oid[OID_SIZE];
71
72     var1.proto = PROTO_Z3950;
73     var1.oclass = CLASS_VARSET;
74     var1.value = VAL_VAR1;
75     r->globalVariantSetId = odr_oiddup_nmem(nmem, oid_ent_to_oid(&var1, oid));
76
77     if (argc)
78         r->triples = nmem_malloc(nmem, sizeof(Z_Triple*) * argc);
79     else
80         r->triples = 0;
81     r->num_triples = argc;
82     for (i = 0; i < argc; i++)
83     {
84         int zclass, type;
85         char value[512];
86         Z_Triple *t;
87
88         if (sscanf(argv[i], "(%d,%d,%[^)])", &zclass, &type, value) < 3)
89         {
90             logf(LOG_WARN, "Syntax error in variant component '%s'",
91                 argv[i]);
92             return 0;
93         }
94         t = r->triples[i] = nmem_malloc(nmem, sizeof(Z_Triple));
95         t->variantSetId = 0;
96         t->zclass = nmem_malloc(nmem, sizeof(int));
97         *t->zclass = zclass;
98         t->type = nmem_malloc(nmem, sizeof(int));
99         *t->type = type;
100         /*
101          * This is wrong.. we gotta look up the correct type for the
102          * variant, I guess... damn this stuff.
103          */
104         if (*value == '@')
105         {
106             t->which = Z_Triple_null;
107             t->value.null = odr_nullval();
108         }
109         else if (isdigit(*value))
110         {
111             t->which = Z_Triple_integer;
112             t->value.integer = nmem_malloc(nmem, sizeof(*t->value.integer));
113             *t->value.integer = atoi(value);
114         }
115         else
116         {
117             t->which = Z_Triple_internationalString;
118             t->value.internationalString = nmem_malloc(nmem, strlen(value)+1);
119             strcpy(t->value.internationalString, value);
120         }
121     }
122     return r;
123 }
124
125 static Z_Occurrences *read_occurrences(char *occ, NMEM nmem)
126 {
127     Z_Occurrences *op = nmem_malloc(nmem, sizeof(*op));
128     char *p;
129
130     if (!occ)
131     {
132         op->which = Z_Occurrences_values;
133         op->u.values = nmem_malloc(nmem, sizeof(Z_OccurValues));
134         op->u.values->start = nmem_malloc(nmem, sizeof(int));
135         *op->u.values->start = 1;
136         op->u.values->howMany = 0;
137     }
138     else if (!strcmp(occ, "all"))
139     {
140         op->which = Z_Occurrences_all;
141         op->u.all = odr_nullval();
142     }
143     else if (!strcmp(occ, "last"))
144     {
145         op->which = Z_Occurrences_last;
146         op->u.all = odr_nullval();
147     }
148     else
149     {
150         Z_OccurValues *ov = nmem_malloc(nmem, sizeof(*ov));
151
152         if (!isdigit(*occ))
153         {
154             logf(LOG_WARN, "Bad occurrences-spec in %s", occ);
155             return 0;
156         }
157         op->which = Z_Occurrences_values;
158         op->u.values = ov;
159         ov->start = nmem_malloc(nmem, sizeof(*ov->start));
160         *ov->start = atoi(occ);
161         if ((p = strchr(occ, '+')))
162         {
163             ov->howMany = nmem_malloc(nmem, sizeof(*ov->howMany));
164             *ov->howMany = atoi(p + 1);
165         }
166         else
167             ov->howMany = 0;
168     }
169     return op;
170 }
171
172
173 static Z_ETagUnit *read_tagunit(char *buf, NMEM nmem)
174 {
175     Z_ETagUnit *u = nmem_malloc(nmem, sizeof(*u));
176     int terms;
177     int type;
178     char value[512], occ[512];
179
180     if (*buf == '*')
181     {
182         u->which = Z_ETagUnit_wildPath;
183         u->u.wildPath = odr_nullval();
184     }
185     else if (*buf == '?')
186     {
187         u->which = Z_ETagUnit_wildThing;
188         if (buf[1] == ':')
189             u->u.wildThing = read_occurrences(buf+2, nmem);
190         else
191             u->u.wildThing = read_occurrences(0, nmem);
192     }
193     else if ((terms = sscanf(buf, "(%d,%[^)]):%[a-z0-9+]", &type, value,
194         occ)) >= 2)
195     {
196         int numval;
197         Z_SpecificTag *t;
198         char *valp = value;
199         int force_string = 0;
200
201         if (*valp == '\'')
202         {
203             valp++;
204             force_string = 1;
205         }
206         u->which = Z_ETagUnit_specificTag;
207         u->u.specificTag = t = nmem_malloc(nmem, sizeof(*t));
208         t->tagType = nmem_malloc(nmem, sizeof(*t->tagType));
209         *t->tagType = type;
210         t->tagValue = nmem_malloc(nmem, sizeof(*t->tagValue));
211         if (!force_string && (numval = atoi(valp)))
212         {
213             t->tagValue->which = Z_StringOrNumeric_numeric;
214             t->tagValue->u.numeric = nmem_malloc(nmem, sizeof(int));
215             *t->tagValue->u.numeric = numval;
216         }
217         else
218         {
219             t->tagValue->which = Z_StringOrNumeric_string;
220             t->tagValue->u.string = nmem_malloc(nmem, strlen(valp)+1);
221             strcpy(t->tagValue->u.string, valp);
222         }
223         if (terms > 2) /* an occurrences-spec exists */
224             t->occurrences = read_occurrences(occ, nmem);
225         else
226             t->occurrences = 0;
227     }
228     return u;
229 }
230
231 /*
232  * Read an element-set specification from a file.
233  * NOTE: If !o, memory is allocated directly from the heap by nmem_malloc().
234  */
235 Z_Espec1 *data1_read_espec1 (data1_handle dh, const char *file)
236 {
237     FILE *f;
238     NMEM nmem = data1_nmem_get (dh);
239     int argc, size_esn = 0;
240     char *argv[50], line[512];
241     Z_Espec1 *res = nmem_malloc(nmem, sizeof(*res));
242
243     if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
244     {
245         logf(LOG_WARN|LOG_ERRNO, "%s", file);
246         return 0;
247     }
248
249     res->num_elementSetNames = 0;
250     res->elementSetNames = 0;
251     res->defaultVariantSetId = 0;
252     res->defaultVariantRequest = 0;
253     res->defaultTagType = 0;
254     res->num_elements = 0;
255     res->elements = 0;
256
257     while ((argc = readconf_line(f, line, 512, argv, 50)))
258         if (!strcmp(argv[0], "elementsetnames"))
259         {
260             int nnames = argc-1, i;
261
262             if (!nnames)
263             {
264                 logf(LOG_WARN, "%s: Empty elementsetnames directive",
265                     file);
266                 continue;
267             }
268
269             res->elementSetNames = nmem_malloc(nmem, sizeof(char**)*nnames);
270             for (i = 0; i < nnames; i++)
271             {
272                 res->elementSetNames[i] = nmem_malloc(nmem,
273                                                       strlen(argv[i+1])+1);
274                 strcpy(res->elementSetNames[i], argv[i+1]);
275             }
276             res->num_elementSetNames = nnames;
277         }
278         else if (!strcmp(argv[0], "defaultvariantsetid"))
279         {
280             if (argc != 2 || !(res->defaultVariantSetId =
281                 odr_getoidbystr_nmem(nmem, argv[1])))
282             {
283                 logf(LOG_WARN, "%s: Bad defaultvariantsetid directive", file);
284                 continue;
285             }
286         }
287         else if (!strcmp(argv[0], "defaulttagtype"))
288         {
289             if (argc != 2)
290             {
291                 logf(LOG_WARN, "%s: Bad defaulttagtype directive", file);
292                 continue;
293             }
294             res->defaultTagType = nmem_malloc(nmem, sizeof(int));
295             *res->defaultTagType = atoi(argv[1]);
296         }
297         else if (!strcmp(argv[0], "defaultvariantrequest"))
298         {
299             if (!(res->defaultVariantRequest = read_variant(argc-1,
300                                                             argv+1, nmem)))
301             {
302                 logf(LOG_WARN, "%s: Bad defaultvariantrequest", file);
303                 continue;
304             }
305         }
306         else if (!strcmp(argv[0], "simpleelement"))
307         {
308             Z_ElementRequest *er;
309             Z_SimpleElement *se;
310             Z_ETagPath *tp;
311             char *path = argv[1];
312             char *ep;
313             int num, i = 0;
314             
315             if (!res->elements)
316                 res->elements = nmem_malloc(nmem, size_esn = 24*sizeof(er));
317             else if (res->num_elements >= (int) (size_esn/sizeof(er)))
318             {
319                 Z_ElementRequest **oe = res->elements;
320                 size_esn *= 2;
321                 res->elements = nmem_malloc (nmem, size_esn*sizeof(er));
322                 memcpy (res->elements, oe, size_esn/2);
323             }
324             if (argc < 2)
325             {
326                 logf(LOG_WARN, "%s: Empty simpleelement directive", file);
327                 continue;
328             }
329             
330             res->elements[res->num_elements++] = er =
331                 nmem_malloc(nmem, sizeof(*er));
332             er->which = Z_ERequest_simpleElement;
333             er->u.simpleElement = se = nmem_malloc(nmem, sizeof(*se));
334             se->variantRequest = 0;
335             se->path = tp = nmem_malloc(nmem, sizeof(*tp));
336             tp->num_tags = 0;
337             /*
338              * Parse the element selector.
339              */
340             for (num = 1, ep = path; (ep = strchr(ep, '/')); num++, ep++)
341                 ;
342             tp->tags = nmem_malloc(nmem, sizeof(Z_ETagUnit*)*num);
343             
344             for ((ep = strchr(path, '/')) ; path ;
345                  (void)((path = ep) && (ep = strchr(path, '/'))))
346             {
347                 if (ep)
348                     ep++;
349                 
350                 assert(i<num);
351                 tp->tags[tp->num_tags++] = read_tagunit(path, nmem);
352             }
353             
354             if (argc > 2 && !strcmp(argv[2], "variant"))
355                 se->variantRequest= read_variant(argc-3, argv+3, nmem);
356         }
357         else
358         {
359             logf(LOG_WARN, "%s: Unknown directive %s", file, argv[0]);
360             fclose(f);
361             return 0;
362         }
363     fclose (f);
364     return res;
365 }