1 /* $Id: d1_espec.c,v 1.9 2005-03-05 11:35:18 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
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
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
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
30 #include <yaz/proto.h>
31 #include <idzebra/data1.h>
33 static Z_Variant *read_variant(int argc, char **argv, NMEM nmem,
34 const char *file, int lineno)
36 Z_Variant *r = (Z_Variant *)nmem_malloc(nmem, sizeof(*r));
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));
47 r->triples = (Z_Triple **)nmem_malloc(nmem, sizeof(Z_Triple*) * argc);
50 r->num_triples = argc;
51 for (i = 0; i < argc; i++)
57 if (sscanf(argv[i], "(%d,%d,%511[^)])", &zclass, &type, value) < 3)
59 yaz_log(YLOG_WARN, "%s:%d: Syntax error in variant component '%s'",
60 file, lineno, argv[i]);
63 t = r->triples[i] = (Z_Triple *)nmem_malloc(nmem, sizeof(Z_Triple));
65 t->zclass = nmem_intdup(nmem, zclass);
66 t->type = nmem_intdup(nmem, type);
68 * This is wrong.. we gotta look up the correct type for the
69 * variant, I guess... damn this stuff.
73 t->which = Z_Triple_null;
74 t->value.null = odr_nullval();
76 else if (d1_isdigit(*value))
78 t->which = Z_Triple_integer;
79 t->value.integer = nmem_intdup(nmem, atoi(value));
83 t->which = Z_Triple_internationalString;
84 t->value.internationalString = nmem_strdup(nmem, value);
90 static Z_Occurrences *read_occurrences(char *occ, NMEM nmem,
91 const char *file, int lineno)
93 Z_Occurrences *op = (Z_Occurrences *)nmem_malloc(nmem, sizeof(*op));
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;
104 else if (!strcmp(occ, "all"))
106 op->which = Z_Occurrences_all;
107 op->u.all = odr_nullval();
109 else if (!strcmp(occ, "last"))
111 op->which = Z_Occurrences_last;
112 op->u.all = odr_nullval();
116 Z_OccurValues *ov = (Z_OccurValues *)nmem_malloc(nmem, sizeof(*ov));
118 if (!d1_isdigit(*occ))
120 yaz_log(YLOG_WARN, "%s:%d: Bad occurrences-spec %s",
124 op->which = Z_Occurrences_values;
126 ov->start = nmem_intdup(nmem, atoi(occ));
127 if ((p = strchr(occ, '+')))
128 ov->howMany = nmem_intdup(nmem, atoi(p + 1));
136 static Z_ETagUnit *read_tagunit(char *buf, NMEM nmem,
137 const char *file, int lineno)
139 Z_ETagUnit *u = (Z_ETagUnit *)nmem_malloc(nmem, sizeof(*u));
142 char value[512], occ[512];
146 u->which = Z_ETagUnit_wildPath;
147 u->u.wildPath = odr_nullval();
149 else if (*buf == '?')
151 u->which = Z_ETagUnit_wildThing;
153 u->u.wildThing = read_occurrences(buf+2, nmem, file, lineno);
155 u->u.wildThing = read_occurrences(0, nmem, file, lineno);
157 else if ((terms = sscanf(buf, "(%d,%511[^)]):%511[a-zA-Z0-9+]",
158 &type, value, occ)) >= 2)
163 int force_string = 0;
169 if (*valp && valp[strlen(valp)-1] == '\'')
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))
180 t->tagValue->which = Z_StringOrNumeric_numeric;
181 t->tagValue->u.numeric = nmem_intdup(nmem, numval);
185 t->tagValue->which = Z_StringOrNumeric_string;
186 t->tagValue->u.string = nmem_strdup(nmem, valp);
188 if (terms > 2) /* an occurrences-spec exists */
189 t->occurrences = read_occurrences(occ, nmem, file, lineno);
193 else if ((terms = sscanf(buf, "%511[^)]", value)) >= 1)
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);
215 * Read an element-set specification from a file.
216 * NOTE: If !o, memory is allocated directly from the heap by nmem_malloc().
218 Z_Espec1 *data1_read_espec1 (data1_handle dh, const char *file)
221 NMEM nmem = data1_nmem_get (dh);
223 int argc, size_esn = 0;
224 char *argv[50], line[512];
225 Z_Espec1 *res = (Z_Espec1 *)nmem_malloc(nmem, sizeof(*res));
227 if (!(f = data1_path_fopen(dh, file, "r")))
229 yaz_log(YLOG_WARN|YLOG_ERRNO, "%s", file);
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;
241 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
242 if (!strcmp(argv[0], "elementsetnames"))
244 int nnames = argc-1, i;
248 yaz_log(YLOG_WARN, "%s:%d: Empty elementsetnames directive",
253 res->elementSetNames =
254 (char **)nmem_malloc(nmem, sizeof(char**)*nnames);
255 for (i = 0; i < nnames; i++)
257 res->elementSetNames[i] = nmem_strdup(nmem, argv[i+1]);
259 res->num_elementSetNames = nnames;
261 else if (!strcmp(argv[0], "defaultvariantsetid"))
265 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for %s",
266 file, lineno, argv[0]);
269 if (!(res->defaultVariantSetId =
270 odr_getoidbystr_nmem(nmem, argv[1])))
272 yaz_log(YLOG_WARN, "%s:%d: Bad defaultvariantsetid",
277 else if (!strcmp(argv[0], "defaulttagtype"))
281 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for %s",
282 file, lineno, argv[0]);
285 res->defaultTagType = nmem_intdup(nmem, atoi(argv[1]));
287 else if (!strcmp(argv[0], "defaultvariantrequest"))
289 if (!(res->defaultVariantRequest =
290 read_variant(argc-1, argv+1, nmem, file, lineno)))
292 yaz_log(YLOG_WARN, "%s:%d: Bad defaultvariantrequest",
297 else if (!strcmp(argv[0], "simpleelement"))
299 Z_ElementRequest *er;
302 char *path = argv[1];
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)))
311 Z_ElementRequest **oe = res->elements;
313 res->elements = (Z_ElementRequest **)
314 nmem_malloc (nmem, size_esn*sizeof(er));
315 memcpy (res->elements, oe, size_esn/2);
319 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for %s",
320 file, lineno, argv[0]);
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));
333 * Parse the element selector.
335 for (num = 1, ep = path; (ep = strchr(ep, '/')); num++, ep++)
337 tp->tags = (Z_ETagUnit **)
338 nmem_malloc(nmem, sizeof(Z_ETagUnit*)*num);
340 for ((ep = strchr(path, '/')) ; path ;
341 (void)((path = ep) && (ep = strchr(path, '/'))))
348 tagunit = read_tagunit(path, nmem, file, lineno);
351 yaz_log (YLOG_WARN, "%s%d: Bad tag unit at %s",
355 tp->tags[tp->num_tags++] = tagunit;
358 if (argc > 2 && !strcmp(argv[2], "variant"))
360 read_variant(argc-3, argv+3, nmem, file, lineno);
363 yaz_log(YLOG_WARN, "%s:%d: Unknown directive '%s'",
364 file, lineno, argv[0]);