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