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