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