Allow range to be specified in termlist, e.g. title:w:range(data,2,4)
[idzebra-moved-to-github.git] / data1 / d1_espec.c
1 /* $Id: d1_espec.c,v 1.2 2002-10-22 13:19:50 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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 <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     return u;
204 }
205
206 /*
207  * Read an element-set specification from a file.
208  * NOTE: If !o, memory is allocated directly from the heap by nmem_malloc().
209  */
210 Z_Espec1 *data1_read_espec1 (data1_handle dh, const char *file)
211 {
212     FILE *f;
213     NMEM nmem = data1_nmem_get (dh);
214     int lineno = 0;
215     int argc, size_esn = 0;
216     char *argv[50], line[512];
217     Z_Espec1 *res = (Z_Espec1 *)nmem_malloc(nmem, sizeof(*res));
218     
219     if (!(f = data1_path_fopen(dh, file, "r")))
220     {
221         yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
222         return 0;
223     }
224     
225     res->num_elementSetNames = 0;
226     res->elementSetNames = 0;
227     res->defaultVariantSetId = 0;
228     res->defaultVariantRequest = 0;
229     res->defaultTagType = 0;
230     res->num_elements = 0;
231     res->elements = 0;
232     
233     while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
234         if (!strcmp(argv[0], "elementsetnames"))
235         {
236             int nnames = argc-1, i;
237             
238             if (!nnames)
239             {
240                 yaz_log(LOG_WARN, "%s:%d: Empty elementsetnames directive",
241                         file, lineno);
242                 continue;
243             }
244             
245             res->elementSetNames =
246                 (char **)nmem_malloc(nmem, sizeof(char**)*nnames);
247             for (i = 0; i < nnames; i++)
248             {
249                 res->elementSetNames[i] = (char *)
250                     nmem_malloc(nmem, strlen(argv[i+1])+1);
251                 strcpy(res->elementSetNames[i], argv[i+1]);
252             }
253             res->num_elementSetNames = nnames;
254         }
255         else if (!strcmp(argv[0], "defaultvariantsetid"))
256         {
257             if (argc != 2)
258             {
259                 yaz_log(LOG_WARN, "%s:%d: Bad # of args for %s",
260                         file, lineno, argv[0]);
261                 continue;
262             }
263             if (!(res->defaultVariantSetId =
264                   odr_getoidbystr_nmem(nmem, argv[1])))
265             {
266                 yaz_log(LOG_WARN, "%s:%d: Bad defaultvariantsetid",
267                         file, lineno);
268                 continue;
269             }
270         }
271         else if (!strcmp(argv[0], "defaulttagtype"))
272         {
273             if (argc != 2)
274             {
275                 yaz_log(LOG_WARN, "%s:%d: Bad # of args for %s",
276                         file, lineno, argv[0]);
277                 continue;
278             }
279             res->defaultTagType = (int *)nmem_malloc(nmem, sizeof(int));
280             *res->defaultTagType = atoi(argv[1]);
281         }
282         else if (!strcmp(argv[0], "defaultvariantrequest"))
283         {
284             if (!(res->defaultVariantRequest =
285                   read_variant(argc-1, argv+1, nmem, file, lineno)))
286             {
287                 yaz_log(LOG_WARN, "%s:%d: Bad defaultvariantrequest",
288                         file, lineno);
289                 continue;
290             }
291         }
292         else if (!strcmp(argv[0], "simpleelement"))
293         {
294             Z_ElementRequest *er;
295             Z_SimpleElement *se;
296             Z_ETagPath *tp;
297             char *path = argv[1];
298             char *ep;
299             int num, i = 0;
300             
301             if (!res->elements)
302                 res->elements = (Z_ElementRequest **)
303                     nmem_malloc(nmem, size_esn = 24*sizeof(er));
304             else if (res->num_elements >= (int) (size_esn/sizeof(er)))
305             {
306                 Z_ElementRequest **oe = res->elements;
307                 size_esn *= 2;
308                 res->elements = (Z_ElementRequest **)
309                     nmem_malloc (nmem, size_esn*sizeof(er));
310                 memcpy (res->elements, oe, size_esn/2);
311             }
312             if (argc < 2)
313             {
314                 yaz_log(LOG_WARN, "%s:%d: Bad # of args for %s",
315                         file, lineno, argv[0]);
316                 continue;
317             }
318             
319             res->elements[res->num_elements++] = er =
320                 (Z_ElementRequest *)nmem_malloc(nmem, sizeof(*er));
321             er->which = Z_ERequest_simpleElement;
322             er->u.simpleElement = se = (Z_SimpleElement *)
323                 nmem_malloc(nmem, sizeof(*se));
324             se->variantRequest = 0;
325             se->path = tp = (Z_ETagPath *)nmem_malloc(nmem, sizeof(*tp));
326             tp->num_tags = 0;
327             /*
328              * Parse the element selector.
329              */
330             for (num = 1, ep = path; (ep = strchr(ep, '/')); num++, ep++)
331                 ;
332             tp->tags = (Z_ETagUnit **)
333                 nmem_malloc(nmem, sizeof(Z_ETagUnit*)*num);
334             
335             for ((ep = strchr(path, '/')) ; path ;
336                  (void)((path = ep) && (ep = strchr(path, '/'))))
337             {
338                 Z_ETagUnit *tagunit;
339                 if (ep)
340                     ep++;
341                 
342                 assert(i<num);
343                 tagunit = read_tagunit(path, nmem, file, lineno);
344                 if (!tagunit)
345                 {
346                     yaz_log (LOG_WARN, "%s%d: Bad tag unit at %s",
347                              file, lineno, path);
348                     break;
349                 }
350                 tp->tags[tp->num_tags++] = tagunit;
351             }
352             
353             if (argc > 2 && !strcmp(argv[2], "variant"))
354                 se->variantRequest=
355                     read_variant(argc-3, argv+3, nmem, file, lineno);
356         }
357         else
358             yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'",
359                     file, lineno, argv[0]);
360     fclose (f);
361     return res;
362 }