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