Added support for string components in simpleelement specs. Supported
[idzebra-moved-to-github.git] / data1 / d1_espec.c
1 /* $Id: d1_espec.c,v 1.8 2005-02-08 00:36:08 adam Exp $
2    Copyright (C) 1995-2005
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/log.h>
28 #include <yaz/odr.h>
29 #include <yaz/proto.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(YLOG_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 = nmem_intdup(nmem, zclass);
65         t->type = nmem_intdup(nmem, type);
66         /*
67          * This is wrong.. we gotta look up the correct type for the
68          * variant, I guess... damn this stuff.
69          */
70         if (*value == '@')
71         {
72             t->which = Z_Triple_null;
73             t->value.null = odr_nullval();
74         }
75         else if (d1_isdigit(*value))
76         {
77             t->which = Z_Triple_integer;
78             t->value.integer = nmem_intdup(nmem, atoi(value));
79         }
80         else
81         {
82             t->which = Z_Triple_internationalString;
83             t->value.internationalString = nmem_strdup(nmem, value);
84         }
85     }
86     return r;
87 }
88
89 static Z_Occurrences *read_occurrences(char *occ, NMEM nmem,
90                                        const char *file, int lineno)
91 {
92     Z_Occurrences *op = (Z_Occurrences *)nmem_malloc(nmem, sizeof(*op));
93     char *p;
94     
95     if (!occ)
96     {
97         op->which = Z_Occurrences_values;
98         op->u.values = (Z_OccurValues *)
99             nmem_malloc(nmem, sizeof(Z_OccurValues));
100         op->u.values->start = nmem_intdup(nmem, 1);
101         op->u.values->howMany = 0;
102     }
103     else if (!strcmp(occ, "all"))
104     {
105         op->which = Z_Occurrences_all;
106         op->u.all = odr_nullval();
107     }
108     else if (!strcmp(occ, "last"))
109     {
110         op->which = Z_Occurrences_last;
111         op->u.all = odr_nullval();
112     }
113     else
114     {
115         Z_OccurValues *ov = (Z_OccurValues *)nmem_malloc(nmem, sizeof(*ov));
116     
117         if (!d1_isdigit(*occ))
118         {
119             yaz_log(YLOG_WARN, "%s:%d: Bad occurrences-spec %s",
120                     file, lineno, occ);
121             return 0;
122         }
123         op->which = Z_Occurrences_values;
124         op->u.values = ov;
125         ov->start = nmem_intdup(nmem, atoi(occ));
126         if ((p = strchr(occ, '+')))
127             ov->howMany = nmem_intdup(nmem, atoi(p + 1));
128         else
129             ov->howMany = 0;
130     }
131     return op;
132 }
133
134
135 static Z_ETagUnit *read_tagunit(char *buf, NMEM nmem,
136                                 const char *file, int lineno)
137 {
138     Z_ETagUnit *u = (Z_ETagUnit *)nmem_malloc(nmem, sizeof(*u));
139     int terms;
140     int type;
141     char value[512], occ[512];
142     
143     if (*buf == '*')
144     {
145         u->which = Z_ETagUnit_wildPath;
146         u->u.wildPath = odr_nullval();
147     }
148     else if (*buf == '?')
149     {
150         u->which = Z_ETagUnit_wildThing;
151         if (buf[1] == ':')
152             u->u.wildThing = read_occurrences(buf+2, nmem, file, lineno);
153         else
154             u->u.wildThing = read_occurrences(0, nmem, file, lineno);
155     }
156     else if ((terms = sscanf(buf, "(%d,%511[^)]):%511[a-zA-Z0-9+]",
157                              &type, value, occ)) >= 2)
158     {
159         int numval;
160         Z_SpecificTag *t;
161         char *valp = value;
162         int force_string = 0;
163         
164         if (*valp == '\'')
165         {
166             valp++;
167             force_string = 1;
168             if (*valp && valp[strlen(valp)-1] == '\'')
169                 *valp = '\0';
170         }
171         u->which = Z_ETagUnit_specificTag;
172         u->u.specificTag = t = (Z_SpecificTag *)nmem_malloc(nmem, sizeof(*t));
173         t->tagType = nmem_intdup(nmem, type);
174         t->tagValue = (Z_StringOrNumeric *)
175             nmem_malloc(nmem, sizeof(*t->tagValue));
176         if (!force_string && isdigit(*(unsigned char *)valp))
177         {
178             numval = atoi(valp);
179             t->tagValue->which = Z_StringOrNumeric_numeric;
180             t->tagValue->u.numeric = nmem_intdup(nmem, numval);
181         }
182         else
183         {
184             t->tagValue->which = Z_StringOrNumeric_string;
185             t->tagValue->u.string = nmem_strdup(nmem, valp);
186         }
187         if (terms > 2) /* an occurrences-spec exists */
188             t->occurrences = read_occurrences(occ, nmem, file, lineno);
189         else
190             t->occurrences = 0;
191     }
192     else if ((terms = sscanf(buf, "%511[^)]", value)) >= 1)
193     {
194         int numval;
195         Z_SpecificTag *t;
196         char *valp = value;
197
198         u->which = Z_ETagUnit_specificTag;
199         u->u.specificTag = t = (Z_SpecificTag *)nmem_malloc(nmem, sizeof(*t));
200         t->tagType = nmem_intdup(nmem, 3);
201         t->tagValue = (Z_StringOrNumeric *)
202             nmem_malloc(nmem, sizeof(*t->tagValue));
203         t->tagValue->which = Z_StringOrNumeric_string;
204         t->tagValue->u.string = nmem_strdup(nmem, valp);
205         t->occurrences = read_occurrences("all", nmem, file, lineno);
206     }
207     else
208     {
209         return 0;
210     }
211     return u;
212 }
213
214 /*
215  * Read an element-set specification from a file.
216  * NOTE: If !o, memory is allocated directly from the heap by nmem_malloc().
217  */
218 Z_Espec1 *data1_read_espec1 (data1_handle dh, const char *file)
219 {
220     FILE *f;
221     NMEM nmem = data1_nmem_get (dh);
222     int lineno = 0;
223     int argc, size_esn = 0;
224     char *argv[50], line[512];
225     Z_Espec1 *res = (Z_Espec1 *)nmem_malloc(nmem, sizeof(*res));
226     
227     if (!(f = data1_path_fopen(dh, file, "r")))
228     {
229         yaz_log(YLOG_WARN|YLOG_ERRNO, "%s", file);
230         return 0;
231     }
232     
233     res->num_elementSetNames = 0;
234     res->elementSetNames = 0;
235     res->defaultVariantSetId = 0;
236     res->defaultVariantRequest = 0;
237     res->defaultTagType = 0;
238     res->num_elements = 0;
239     res->elements = 0;
240     
241     while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
242         if (!strcmp(argv[0], "elementsetnames"))
243         {
244             int nnames = argc-1, i;
245             
246             if (!nnames)
247             {
248                 yaz_log(YLOG_WARN, "%s:%d: Empty elementsetnames directive",
249                         file, lineno);
250                 continue;
251             }
252             
253             res->elementSetNames =
254                 (char **)nmem_malloc(nmem, sizeof(char**)*nnames);
255             for (i = 0; i < nnames; i++)
256             {
257                 res->elementSetNames[i] = nmem_strdup(nmem, argv[i+1]);
258             }
259             res->num_elementSetNames = nnames;
260         }
261         else if (!strcmp(argv[0], "defaultvariantsetid"))
262         {
263             if (argc != 2)
264             {
265                 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for %s",
266                         file, lineno, argv[0]);
267                 continue;
268             }
269             if (!(res->defaultVariantSetId =
270                   odr_getoidbystr_nmem(nmem, argv[1])))
271             {
272                 yaz_log(YLOG_WARN, "%s:%d: Bad defaultvariantsetid",
273                         file, lineno);
274                 continue;
275             }
276         }
277         else if (!strcmp(argv[0], "defaulttagtype"))
278         {
279             if (argc != 2)
280             {
281                 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for %s",
282                         file, lineno, argv[0]);
283                 continue;
284             }
285             res->defaultTagType = nmem_intdup(nmem, atoi(argv[1]));
286         }
287         else if (!strcmp(argv[0], "defaultvariantrequest"))
288         {
289             if (!(res->defaultVariantRequest =
290                   read_variant(argc-1, argv+1, nmem, file, lineno)))
291             {
292                 yaz_log(YLOG_WARN, "%s:%d: Bad defaultvariantrequest",
293                         file, lineno);
294                 continue;
295             }
296         }
297         else if (!strcmp(argv[0], "simpleelement"))
298         {
299             Z_ElementRequest *er;
300             Z_SimpleElement *se;
301             Z_ETagPath *tp;
302             char *path = argv[1];
303             char *ep;
304             int num, i = 0;
305             
306             if (!res->elements)
307                 res->elements = (Z_ElementRequest **)
308                     nmem_malloc(nmem, size_esn = 24*sizeof(er));
309             else if (res->num_elements >= (int) (size_esn/sizeof(er)))
310             {
311                 Z_ElementRequest **oe = res->elements;
312                 size_esn *= 2;
313                 res->elements = (Z_ElementRequest **)
314                     nmem_malloc (nmem, size_esn*sizeof(er));
315                 memcpy (res->elements, oe, size_esn/2);
316             }
317             if (argc < 2)
318             {
319                 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for %s",
320                         file, lineno, argv[0]);
321                 continue;
322             }
323             
324             res->elements[res->num_elements++] = er =
325                 (Z_ElementRequest *)nmem_malloc(nmem, sizeof(*er));
326             er->which = Z_ERequest_simpleElement;
327             er->u.simpleElement = se = (Z_SimpleElement *)
328                 nmem_malloc(nmem, sizeof(*se));
329             se->variantRequest = 0;
330             se->path = tp = (Z_ETagPath *)nmem_malloc(nmem, sizeof(*tp));
331             tp->num_tags = 0;
332             /*
333              * Parse the element selector.
334              */
335             for (num = 1, ep = path; (ep = strchr(ep, '/')); num++, ep++)
336                 ;
337             tp->tags = (Z_ETagUnit **)
338                 nmem_malloc(nmem, sizeof(Z_ETagUnit*)*num);
339             
340             for ((ep = strchr(path, '/')) ; path ;
341                  (void)((path = ep) && (ep = strchr(path, '/'))))
342             {
343                 Z_ETagUnit *tagunit;
344                 if (ep)
345                     ep++;
346                 
347                 assert(i<num);
348                 tagunit = read_tagunit(path, nmem, file, lineno);
349                 if (!tagunit)
350                 {
351                     yaz_log (YLOG_WARN, "%s%d: Bad tag unit at %s",
352                              file, lineno, path);
353                     break;
354                 }
355                 tp->tags[tp->num_tags++] = tagunit;
356             }
357             
358             if (argc > 2 && !strcmp(argv[2], "variant"))
359                 se->variantRequest=
360                     read_variant(argc-3, argv+3, nmem, file, lineno);
361         }
362         else
363             yaz_log(YLOG_WARN, "%s:%d: Unknown directive '%s'",
364                     file, lineno, argv[0]);
365     fclose (f);
366     return res;
367 }