1cad41cc023bd72c0d26f3dc07777a986f45e792
[idzebra-moved-to-github.git] / data1 / d1_espec.c
1 /* $Id: d1_espec.c,v 1.4 2004-10-05 12:23:25 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <string.h>
26
27 #include <yaz/odr.h>
28 #include <yaz/proto.h>
29 #include <yaz/log.h>
30 #include <idzebra/data1.h>
31
32 static Z_Variant *read_variant(int argc, char **argv, NMEM nmem,
33                                const char *file, int lineno)
34 {
35     Z_Variant *r = (Z_Variant *)nmem_malloc(nmem, sizeof(*r));
36     oident var1;
37     int i;
38     int oid[OID_SIZE];
39
40     var1.proto = PROTO_Z3950;
41     var1.oclass = CLASS_VARSET;
42     var1.value = VAL_VAR1;
43     r->globalVariantSetId = odr_oiddup_nmem(nmem, oid_ent_to_oid(&var1, oid));
44
45     if (argc)
46         r->triples = (Z_Triple **)nmem_malloc(nmem, sizeof(Z_Triple*) * argc);
47     else
48         r->triples = 0;
49     r->num_triples = argc;
50     for (i = 0; i < argc; i++)
51     {
52         int zclass, type;
53         char value[512];
54         Z_Triple *t;
55
56         if (sscanf(argv[i], "(%d,%d,%511[^)])", &zclass, &type, value) < 3)
57         {
58             yaz_log(LOG_WARN, "%s:%d: Syntax error in variant component '%s'",
59                     file, lineno, argv[i]);
60             return 0;
61         }
62         t = r->triples[i] = (Z_Triple *)nmem_malloc(nmem, sizeof(Z_Triple));
63         t->variantSetId = 0;
64         t->zclass = (int *)nmem_malloc(nmem, sizeof(int));
65         *t->zclass = zclass;
66         t->type = (int *)nmem_malloc(nmem, sizeof(int));
67         *t->type = type;
68         /*
69          * This is wrong.. we gotta look up the correct type for the
70          * variant, I guess... damn this stuff.
71          */
72         if (*value == '@')
73         {
74             t->which = Z_Triple_null;
75             t->value.null = odr_nullval();
76         }
77         else if (d1_isdigit(*value))
78         {
79             t->which = Z_Triple_integer;
80             t->value.integer = (int *)
81                 nmem_malloc(nmem, sizeof(*t->value.integer));
82             *t->value.integer = atoi(value);
83         }
84         else
85         {
86             t->which = Z_Triple_internationalString;
87             t->value.internationalString = (char *)
88                 nmem_malloc(nmem, strlen(value)+1);
89             strcpy(t->value.internationalString, value);
90         }
91     }
92     return r;
93 }
94
95 static Z_Occurrences *read_occurrences(char *occ, NMEM nmem,
96                                        const char *file, int lineno)
97 {
98     Z_Occurrences *op = (Z_Occurrences *)nmem_malloc(nmem, sizeof(*op));
99     char *p;
100     
101     if (!occ)
102     {
103         op->which = Z_Occurrences_values;
104         op->u.values = (Z_OccurValues *)
105             nmem_malloc(nmem, sizeof(Z_OccurValues));
106         op->u.values->start = (int *)nmem_malloc(nmem, sizeof(int));
107         *op->u.values->start = 1;
108         op->u.values->howMany = 0;
109     }
110     else if (!strcmp(occ, "all"))
111     {
112         op->which = Z_Occurrences_all;
113         op->u.all = odr_nullval();
114     }
115     else if (!strcmp(occ, "last"))
116     {
117         op->which = Z_Occurrences_last;
118         op->u.all = odr_nullval();
119     }
120     else
121     {
122         Z_OccurValues *ov = (Z_OccurValues *)nmem_malloc(nmem, sizeof(*ov));
123     
124         if (!d1_isdigit(*occ))
125         {
126             yaz_log(LOG_WARN, "%s:%d: Bad occurrences-spec %s",
127                     file, lineno, occ);
128             return 0;
129         }
130         op->which = Z_Occurrences_values;
131         op->u.values = ov;
132         ov->start = (int *)nmem_malloc(nmem, sizeof(*ov->start));
133         *ov->start = atoi(occ);
134         if ((p = strchr(occ, '+')))
135         {
136             ov->howMany = (int *)nmem_malloc(nmem, sizeof(*ov->howMany));
137             *ov->howMany = atoi(p + 1);
138         }
139         else
140             ov->howMany = 0;
141     }
142     return op;
143 }
144
145
146 static Z_ETagUnit *read_tagunit(char *buf, NMEM nmem,
147                                 const char *file, int lineno)
148 {
149     Z_ETagUnit *u = (Z_ETagUnit *)nmem_malloc(nmem, sizeof(*u));
150     int terms;
151     int type;
152     char value[512], occ[512];
153     
154     if (*buf == '*')
155     {
156         u->which = Z_ETagUnit_wildPath;
157         u->u.wildPath = odr_nullval();
158     }
159     else if (*buf == '?')
160     {
161         u->which = Z_ETagUnit_wildThing;
162         if (buf[1] == ':')
163             u->u.wildThing = read_occurrences(buf+2, nmem, file, lineno);
164         else
165             u->u.wildThing = read_occurrences(0, nmem, file, lineno);
166     }
167     else if ((terms = sscanf(buf, "(%d,%511[^)]):%511[a-zA-Z0-9+]",
168                              &type, value, occ)) >= 2)
169     {
170         int numval;
171         Z_SpecificTag *t;
172         char *valp = value;
173         int force_string = 0;
174         
175         if (*valp == '\'')
176         {
177             valp++;
178             force_string = 1;
179         }
180         u->which = Z_ETagUnit_specificTag;
181         u->u.specificTag = t = (Z_SpecificTag *)nmem_malloc(nmem, sizeof(*t));
182         t->tagType = (int *)nmem_malloc(nmem, sizeof(*t->tagType));
183         *t->tagType = type;
184         t->tagValue = (Z_StringOrNumeric *)
185             nmem_malloc(nmem, sizeof(*t->tagValue));
186         if (!force_string && (numval = atoi(valp)))
187         {
188             t->tagValue->which = Z_StringOrNumeric_numeric;
189             t->tagValue->u.numeric = (int *)nmem_malloc(nmem, sizeof(int));
190             *t->tagValue->u.numeric = numval;
191         }
192         else
193         {
194             t->tagValue->which = Z_StringOrNumeric_string;
195             t->tagValue->u.string = (char *)nmem_malloc(nmem, strlen(valp)+1);
196             strcpy(t->tagValue->u.string, valp);
197         }
198         if (terms > 2) /* an occurrences-spec exists */
199             t->occurrences = read_occurrences(occ, nmem, file, lineno);
200         else
201             t->occurrences = 0;
202     }
203     else
204     {
205         return 0;
206     }
207     return u;
208 }
209
210 /*
211  * Read an element-set specification from a file.
212  * NOTE: If !o, memory is allocated directly from the heap by nmem_malloc().
213  */
214 Z_Espec1 *data1_read_espec1 (data1_handle dh, const char *file)
215 {
216     FILE *f;
217     NMEM nmem = data1_nmem_get (dh);
218     int lineno = 0;
219     int argc, size_esn = 0;
220     char *argv[50], line[512];
221     Z_Espec1 *res = (Z_Espec1 *)nmem_malloc(nmem, sizeof(*res));
222     
223     if (!(f = data1_path_fopen(dh, file, "r")))
224     {
225         yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
226         return 0;
227     }
228     
229     res->num_elementSetNames = 0;
230     res->elementSetNames = 0;
231     res->defaultVariantSetId = 0;
232     res->defaultVariantRequest = 0;
233     res->defaultTagType = 0;
234     res->num_elements = 0;
235     res->elements = 0;
236     
237     while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
238         if (!strcmp(argv[0], "elementsetnames"))
239         {
240             int nnames = argc-1, i;
241             
242             if (!nnames)
243             {
244                 yaz_log(LOG_WARN, "%s:%d: Empty elementsetnames directive",
245                         file, lineno);
246                 continue;
247             }
248             
249             res->elementSetNames =
250                 (char **)nmem_malloc(nmem, sizeof(char**)*nnames);
251             for (i = 0; i < nnames; i++)
252             {
253                 res->elementSetNames[i] = (char *)
254                     nmem_malloc(nmem, strlen(argv[i+1])+1);
255                 strcpy(res->elementSetNames[i], argv[i+1]);
256             }
257             res->num_elementSetNames = nnames;
258         }
259         else if (!strcmp(argv[0], "defaultvariantsetid"))
260         {
261             if (argc != 2)
262             {
263                 yaz_log(LOG_WARN, "%s:%d: Bad # of args for %s",
264                         file, lineno, argv[0]);
265                 continue;
266             }
267             if (!(res->defaultVariantSetId =
268                   odr_getoidbystr_nmem(nmem, argv[1])))
269             {
270                 yaz_log(LOG_WARN, "%s:%d: Bad defaultvariantsetid",
271                         file, lineno);
272                 continue;
273             }
274         }
275         else if (!strcmp(argv[0], "defaulttagtype"))
276         {
277             if (argc != 2)
278             {
279                 yaz_log(LOG_WARN, "%s:%d: Bad # of args for %s",
280                         file, lineno, argv[0]);
281                 continue;
282             }
283             res->defaultTagType = (int *)nmem_malloc(nmem, sizeof(int));
284             *res->defaultTagType = atoi(argv[1]);
285         }
286         else if (!strcmp(argv[0], "defaultvariantrequest"))
287         {
288             if (!(res->defaultVariantRequest =
289                   read_variant(argc-1, argv+1, nmem, file, lineno)))
290             {
291                 yaz_log(LOG_WARN, "%s:%d: Bad defaultvariantrequest",
292                         file, lineno);
293                 continue;
294             }
295         }
296         else if (!strcmp(argv[0], "simpleelement"))
297         {
298             Z_ElementRequest *er;
299             Z_SimpleElement *se;
300             Z_ETagPath *tp;
301             char *path = argv[1];
302             char *ep;
303             int num, i = 0;
304             
305             if (!res->elements)
306                 res->elements = (Z_ElementRequest **)
307                     nmem_malloc(nmem, size_esn = 24*sizeof(er));
308             else if (res->num_elements >= (int) (size_esn/sizeof(er)))
309             {
310                 Z_ElementRequest **oe = res->elements;
311                 size_esn *= 2;
312                 res->elements = (Z_ElementRequest **)
313                     nmem_malloc (nmem, size_esn*sizeof(er));
314                 memcpy (res->elements, oe, size_esn/2);
315             }
316             if (argc < 2)
317             {
318                 yaz_log(LOG_WARN, "%s:%d: Bad # of args for %s",
319                         file, lineno, argv[0]);
320                 continue;
321             }
322             
323             res->elements[res->num_elements++] = er =
324                 (Z_ElementRequest *)nmem_malloc(nmem, sizeof(*er));
325             er->which = Z_ERequest_simpleElement;
326             er->u.simpleElement = se = (Z_SimpleElement *)
327                 nmem_malloc(nmem, sizeof(*se));
328             se->variantRequest = 0;
329             se->path = tp = (Z_ETagPath *)nmem_malloc(nmem, sizeof(*tp));
330             tp->num_tags = 0;
331             /*
332              * Parse the element selector.
333              */
334             for (num = 1, ep = path; (ep = strchr(ep, '/')); num++, ep++)
335                 ;
336             tp->tags = (Z_ETagUnit **)
337                 nmem_malloc(nmem, sizeof(Z_ETagUnit*)*num);
338             
339             for ((ep = strchr(path, '/')) ; path ;
340                  (void)((path = ep) && (ep = strchr(path, '/'))))
341             {
342                 Z_ETagUnit *tagunit;
343                 if (ep)
344                     ep++;
345                 
346                 assert(i<num);
347                 tagunit = read_tagunit(path, nmem, file, lineno);
348                 if (!tagunit)
349                 {
350                     yaz_log (LOG_WARN, "%s%d: Bad tag unit at %s",
351                              file, lineno, path);
352                     break;
353                 }
354                 tp->tags[tp->num_tags++] = tagunit;
355             }
356             
357             if (argc > 2 && !strcmp(argv[2], "variant"))
358                 se->variantRequest=
359                     read_variant(argc-3, argv+3, nmem, file, lineno);
360         }
361         else
362             yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'",
363                     file, lineno, argv[0]);
364     fclose (f);
365     return res;
366 }