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