2007.
[idzebra-moved-to-github.git] / data1 / d1_espec.c
1 /* $Id: d1_espec.c,v 1.13 2007-01-15 15:10:14 adam Exp $
2    Copyright (C) 1995-2007
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <string.h>
26 #include <ctype.h>
27
28 #include <yaz/log.h>
29 #include <yaz/odr.h>
30 #include <yaz/proto.h>
31 #include <idzebra/data1.h>
32
33 static Z_Variant *read_variant(int argc, char **argv, NMEM nmem,
34                                const char *file, int lineno)
35 {
36     Z_Variant *r = (Z_Variant *)nmem_malloc(nmem, sizeof(*r));
37     oident var1;
38     int i;
39     int oid[OID_SIZE];
40
41     var1.proto = PROTO_Z3950;
42     var1.oclass = CLASS_VARSET;
43     var1.value = VAL_VAR1;
44     r->globalVariantSetId = odr_oiddup_nmem(nmem, oid_ent_to_oid(&var1, oid));
45
46     if (argc)
47         r->triples = (Z_Triple **)nmem_malloc(nmem, sizeof(Z_Triple*) * argc);
48     else
49         r->triples = 0;
50     r->num_triples = argc;
51     for (i = 0; i < argc; i++)
52     {
53         int zclass, type;
54         char value[512];
55         Z_Triple *t;
56
57         if (sscanf(argv[i], "(%d,%d,%511[^)])", &zclass, &type, value) < 3)
58         {
59             yaz_log(YLOG_WARN, "%s:%d: Syntax error in variant component '%s'",
60                     file, lineno, argv[i]);
61             return 0;
62         }
63         t = r->triples[i] = (Z_Triple *)nmem_malloc(nmem, sizeof(Z_Triple));
64         t->variantSetId = 0;
65         t->zclass = nmem_intdup(nmem, zclass);
66         t->type = nmem_intdup(nmem, type);
67         /*
68          * This is wrong.. we gotta look up the correct type for the
69          * variant, I guess... damn this stuff.
70          */
71         if (*value == '@')
72         {
73             t->which = Z_Triple_null;
74             t->value.null = odr_nullval();
75         }
76         else if (d1_isdigit(*value))
77         {
78             t->which = Z_Triple_integer;
79             t->value.integer = nmem_intdup(nmem, atoi(value));
80         }
81         else
82         {
83             t->which = Z_Triple_internationalString;
84             t->value.internationalString = nmem_strdup(nmem, value);
85         }
86     }
87     return r;
88 }
89
90 static Z_Occurrences *read_occurrences(char *occ, NMEM nmem,
91                                        const char *file, int lineno)
92 {
93     Z_Occurrences *op = (Z_Occurrences *)nmem_malloc(nmem, sizeof(*op));
94     char *p;
95     
96     if (!occ)
97     {
98         op->which = Z_Occurrences_values;
99         op->u.values = (Z_OccurValues *)
100             nmem_malloc(nmem, sizeof(Z_OccurValues));
101         op->u.values->start = nmem_intdup(nmem, 1);
102         op->u.values->howMany = 0;
103     }
104     else if (!strcmp(occ, "all"))
105     {
106         op->which = Z_Occurrences_all;
107         op->u.all = odr_nullval();
108     }
109     else if (!strcmp(occ, "last"))
110     {
111         op->which = Z_Occurrences_last;
112         op->u.all = odr_nullval();
113     }
114     else
115     {
116         Z_OccurValues *ov = (Z_OccurValues *)nmem_malloc(nmem, sizeof(*ov));
117     
118         if (!d1_isdigit(*occ))
119         {
120             yaz_log(YLOG_WARN, "%s:%d: Bad occurrences-spec %s",
121                     file, lineno, occ);
122             return 0;
123         }
124         op->which = Z_Occurrences_values;
125         op->u.values = ov;
126         ov->start = nmem_intdup(nmem, atoi(occ));
127         if ((p = strchr(occ, '+')))
128             ov->howMany = nmem_intdup(nmem, atoi(p + 1));
129         else
130             ov->howMany = 0;
131     }
132     return op;
133 }
134
135
136 static Z_ETagUnit *read_tagunit(char *buf, NMEM nmem,
137                                 const char *file, int lineno)
138 {
139     Z_ETagUnit *u = (Z_ETagUnit *)nmem_malloc(nmem, sizeof(*u));
140     int terms;
141     int type;
142     char value[512], occ[512];
143     
144     if (*buf == '*')
145     {
146         u->which = Z_ETagUnit_wildPath;
147         u->u.wildPath = odr_nullval();
148     }
149     else if (*buf == '?')
150     {
151         u->which = Z_ETagUnit_wildThing;
152         if (buf[1] == ':')
153             u->u.wildThing = read_occurrences(buf+2, nmem, file, lineno);
154         else
155             u->u.wildThing = read_occurrences(0, nmem, file, lineno);
156     }
157     else if ((terms = sscanf(buf, "(%d,%511[^)]):%511[a-zA-Z0-9+]",
158                              &type, value, occ)) >= 2)
159     {
160         int numval;
161         Z_SpecificTag *t;
162         char *valp = value;
163         int force_string = 0;
164         
165         if (*valp == '\'')
166         {
167             valp++;
168             force_string = 1;
169             if (*valp && valp[strlen(valp)-1] == '\'')
170                 *valp = '\0';
171         }
172         u->which = Z_ETagUnit_specificTag;
173         u->u.specificTag = t = (Z_SpecificTag *)nmem_malloc(nmem, sizeof(*t));
174         t->tagType = nmem_intdup(nmem, type);
175         t->tagValue = (Z_StringOrNumeric *)
176             nmem_malloc(nmem, sizeof(*t->tagValue));
177         if (!force_string && isdigit(*(unsigned char *)valp))
178         {
179             numval = atoi(valp);
180             t->tagValue->which = Z_StringOrNumeric_numeric;
181             t->tagValue->u.numeric = nmem_intdup(nmem, numval);
182         }
183         else
184         {
185             t->tagValue->which = Z_StringOrNumeric_string;
186             t->tagValue->u.string = nmem_strdup(nmem, valp);
187         }
188         if (terms > 2) /* an occurrences-spec exists */
189             t->occurrences = read_occurrences(occ, nmem, file, lineno);
190         else
191             t->occurrences = 0;
192     }
193     else if ((terms = sscanf(buf, "%511[^)]", value)) >= 1)
194     {
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         return 0;
229     
230     res->num_elementSetNames = 0;
231     res->elementSetNames = 0;
232     res->defaultVariantSetId = 0;
233     res->defaultVariantRequest = 0;
234     res->defaultTagType = 0;
235     res->num_elements = 0;
236     res->elements = 0;
237     
238     while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
239         if (!strcmp(argv[0], "elementsetnames"))
240         {
241             int nnames = argc-1, i;
242             
243             if (!nnames)
244             {
245                 yaz_log(YLOG_WARN, "%s:%d: Empty elementsetnames directive",
246                         file, lineno);
247                 continue;
248             }
249             
250             res->elementSetNames =
251                 (char **)nmem_malloc(nmem, sizeof(char**)*nnames);
252             for (i = 0; i < nnames; i++)
253             {
254                 res->elementSetNames[i] = nmem_strdup(nmem, argv[i+1]);
255             }
256             res->num_elementSetNames = nnames;
257         }
258         else if (!strcmp(argv[0], "defaultvariantsetid"))
259         {
260             if (argc != 2)
261             {
262                 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for %s",
263                         file, lineno, argv[0]);
264                 continue;
265             }
266             if (!(res->defaultVariantSetId =
267                   odr_getoidbystr_nmem(nmem, argv[1])))
268             {
269                 yaz_log(YLOG_WARN, "%s:%d: Bad defaultvariantsetid",
270                         file, lineno);
271                 continue;
272             }
273         }
274         else if (!strcmp(argv[0], "defaulttagtype"))
275         {
276             if (argc != 2)
277             {
278                 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for %s",
279                         file, lineno, argv[0]);
280                 continue;
281             }
282             res->defaultTagType = nmem_intdup(nmem, atoi(argv[1]));
283         }
284         else if (!strcmp(argv[0], "defaultvariantrequest"))
285         {
286             if (!(res->defaultVariantRequest =
287                   read_variant(argc-1, argv+1, nmem, file, lineno)))
288             {
289                 yaz_log(YLOG_WARN, "%s:%d: Bad defaultvariantrequest",
290                         file, lineno);
291                 continue;
292             }
293         }
294         else if (!strcmp(argv[0], "simpleelement"))
295         {
296             Z_ElementRequest *er;
297             Z_SimpleElement *se;
298             Z_ETagPath *tp;
299             char *path = argv[1];
300             char *ep;
301             int num, i = 0;
302             
303             if (!res->elements)
304                 res->elements = (Z_ElementRequest **)
305                     nmem_malloc(nmem, size_esn = 24*sizeof(er));
306             else if (res->num_elements >= (int) (size_esn/sizeof(er)))
307             {
308                 Z_ElementRequest **oe = res->elements;
309                 size_esn *= 2;
310                 res->elements = (Z_ElementRequest **)
311                     nmem_malloc (nmem, size_esn*sizeof(er));
312                 memcpy (res->elements, oe, size_esn/2);
313             }
314             if (argc < 2)
315             {
316                 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for %s",
317                         file, lineno, argv[0]);
318                 continue;
319             }
320             
321             res->elements[res->num_elements++] = er =
322                 (Z_ElementRequest *)nmem_malloc(nmem, sizeof(*er));
323             er->which = Z_ERequest_simpleElement;
324             er->u.simpleElement = se = (Z_SimpleElement *)
325                 nmem_malloc(nmem, sizeof(*se));
326             se->variantRequest = 0;
327             se->path = tp = (Z_ETagPath *)nmem_malloc(nmem, sizeof(*tp));
328             tp->num_tags = 0;
329             /*
330              * Parse the element selector.
331              */
332             for (num = 1, ep = path; (ep = strchr(ep, '/')); num++, ep++)
333                 ;
334             tp->tags = (Z_ETagUnit **)
335                 nmem_malloc(nmem, sizeof(Z_ETagUnit*)*num);
336             
337             for ((ep = strchr(path, '/')) ; path ;
338                  (void)((path = ep) && (ep = strchr(path, '/'))))
339             {
340                 Z_ETagUnit *tagunit;
341                 if (ep)
342                     ep++;
343                 
344                 assert(i<num);
345                 tagunit = read_tagunit(path, nmem, file, lineno);
346                 if (!tagunit)
347                 {
348                     yaz_log (YLOG_WARN, "%s%d: Bad tag unit at %s",
349                              file, lineno, path);
350                     break;
351                 }
352                 tp->tags[tp->num_tags++] = tagunit;
353             }
354             
355             if (argc > 2 && !strcmp(argv[2], "variant"))
356                 se->variantRequest=
357                     read_variant(argc-3, argv+3, nmem, file, lineno);
358         }
359         else
360             yaz_log(YLOG_WARN, "%s:%d: Unknown directive '%s'",
361                     file, lineno, argv[0]);
362     fclose (f);
363     return res;
364 }
365 /*
366  * Local variables:
367  * c-basic-offset: 4
368  * indent-tabs-mode: nil
369  * End:
370  * vim: shiftwidth=4 tabstop=8 expandtab
371  */
372