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