1 /* $Id: d1_espec.c,v 1.8 2005-02-08 00:36:08 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
29 #include <yaz/proto.h>
30 #include <idzebra/data1.h>
32 static Z_Variant *read_variant(int argc, char **argv, NMEM nmem,
33 const char *file, int lineno)
35 Z_Variant *r = (Z_Variant *)nmem_malloc(nmem, sizeof(*r));
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));
46 r->triples = (Z_Triple **)nmem_malloc(nmem, sizeof(Z_Triple*) * argc);
49 r->num_triples = argc;
50 for (i = 0; i < argc; i++)
56 if (sscanf(argv[i], "(%d,%d,%511[^)])", &zclass, &type, value) < 3)
58 yaz_log(YLOG_WARN, "%s:%d: Syntax error in variant component '%s'",
59 file, lineno, argv[i]);
62 t = r->triples[i] = (Z_Triple *)nmem_malloc(nmem, sizeof(Z_Triple));
64 t->zclass = nmem_intdup(nmem, zclass);
65 t->type = nmem_intdup(nmem, type);
67 * This is wrong.. we gotta look up the correct type for the
68 * variant, I guess... damn this stuff.
72 t->which = Z_Triple_null;
73 t->value.null = odr_nullval();
75 else if (d1_isdigit(*value))
77 t->which = Z_Triple_integer;
78 t->value.integer = nmem_intdup(nmem, atoi(value));
82 t->which = Z_Triple_internationalString;
83 t->value.internationalString = nmem_strdup(nmem, value);
89 static Z_Occurrences *read_occurrences(char *occ, NMEM nmem,
90 const char *file, int lineno)
92 Z_Occurrences *op = (Z_Occurrences *)nmem_malloc(nmem, sizeof(*op));
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;
103 else if (!strcmp(occ, "all"))
105 op->which = Z_Occurrences_all;
106 op->u.all = odr_nullval();
108 else if (!strcmp(occ, "last"))
110 op->which = Z_Occurrences_last;
111 op->u.all = odr_nullval();
115 Z_OccurValues *ov = (Z_OccurValues *)nmem_malloc(nmem, sizeof(*ov));
117 if (!d1_isdigit(*occ))
119 yaz_log(YLOG_WARN, "%s:%d: Bad occurrences-spec %s",
123 op->which = Z_Occurrences_values;
125 ov->start = nmem_intdup(nmem, atoi(occ));
126 if ((p = strchr(occ, '+')))
127 ov->howMany = nmem_intdup(nmem, atoi(p + 1));
135 static Z_ETagUnit *read_tagunit(char *buf, NMEM nmem,
136 const char *file, int lineno)
138 Z_ETagUnit *u = (Z_ETagUnit *)nmem_malloc(nmem, sizeof(*u));
141 char value[512], occ[512];
145 u->which = Z_ETagUnit_wildPath;
146 u->u.wildPath = odr_nullval();
148 else if (*buf == '?')
150 u->which = Z_ETagUnit_wildThing;
152 u->u.wildThing = read_occurrences(buf+2, nmem, file, lineno);
154 u->u.wildThing = read_occurrences(0, nmem, file, lineno);
156 else if ((terms = sscanf(buf, "(%d,%511[^)]):%511[a-zA-Z0-9+]",
157 &type, value, occ)) >= 2)
162 int force_string = 0;
168 if (*valp && valp[strlen(valp)-1] == '\'')
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))
179 t->tagValue->which = Z_StringOrNumeric_numeric;
180 t->tagValue->u.numeric = nmem_intdup(nmem, numval);
184 t->tagValue->which = Z_StringOrNumeric_string;
185 t->tagValue->u.string = nmem_strdup(nmem, valp);
187 if (terms > 2) /* an occurrences-spec exists */
188 t->occurrences = read_occurrences(occ, nmem, file, lineno);
192 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]);