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