1 /* $Id: d1_espec.c,v 1.14 2007-04-16 08:44:31 adam Exp $
2 Copyright (C) 1995-2007
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <yaz/proto.h>
31 #include <idzebra/data1.h>
32 #include <yaz/oid_db.h>
34 static Z_Variant *read_variant(int argc, char **argv, NMEM nmem,
35 const char *file, int lineno)
37 Z_Variant *r = (Z_Variant *)nmem_malloc(nmem, sizeof(*r));
40 r->globalVariantSetId = odr_oiddup_nmem(nmem, yaz_oid_variant1());
42 r->triples = (Z_Triple **)nmem_malloc(nmem, sizeof(Z_Triple*) * argc);
45 r->num_triples = argc;
46 for (i = 0; i < argc; i++)
52 if (sscanf(argv[i], "(%d,%d,%511[^)])", &zclass, &type, value) < 3)
54 yaz_log(YLOG_WARN, "%s:%d: Syntax error in variant component '%s'",
55 file, lineno, argv[i]);
58 t = r->triples[i] = (Z_Triple *)nmem_malloc(nmem, sizeof(Z_Triple));
60 t->zclass = nmem_intdup(nmem, zclass);
61 t->type = nmem_intdup(nmem, type);
63 * This is wrong.. we gotta look up the correct type for the
64 * variant, I guess... damn this stuff.
68 t->which = Z_Triple_null;
69 t->value.null = odr_nullval();
71 else if (d1_isdigit(*value))
73 t->which = Z_Triple_integer;
74 t->value.integer = nmem_intdup(nmem, atoi(value));
78 t->which = Z_Triple_internationalString;
79 t->value.internationalString = nmem_strdup(nmem, value);
85 static Z_Occurrences *read_occurrences(char *occ, NMEM nmem,
86 const char *file, int lineno)
88 Z_Occurrences *op = (Z_Occurrences *)nmem_malloc(nmem, sizeof(*op));
93 op->which = Z_Occurrences_values;
94 op->u.values = (Z_OccurValues *)
95 nmem_malloc(nmem, sizeof(Z_OccurValues));
96 op->u.values->start = nmem_intdup(nmem, 1);
97 op->u.values->howMany = 0;
99 else if (!strcmp(occ, "all"))
101 op->which = Z_Occurrences_all;
102 op->u.all = odr_nullval();
104 else if (!strcmp(occ, "last"))
106 op->which = Z_Occurrences_last;
107 op->u.all = odr_nullval();
111 Z_OccurValues *ov = (Z_OccurValues *)nmem_malloc(nmem, sizeof(*ov));
113 if (!d1_isdigit(*occ))
115 yaz_log(YLOG_WARN, "%s:%d: Bad occurrences-spec %s",
119 op->which = Z_Occurrences_values;
121 ov->start = nmem_intdup(nmem, atoi(occ));
122 if ((p = strchr(occ, '+')))
123 ov->howMany = nmem_intdup(nmem, atoi(p + 1));
131 static Z_ETagUnit *read_tagunit(char *buf, NMEM nmem,
132 const char *file, int lineno)
134 Z_ETagUnit *u = (Z_ETagUnit *)nmem_malloc(nmem, sizeof(*u));
137 char value[512], occ[512];
141 u->which = Z_ETagUnit_wildPath;
142 u->u.wildPath = odr_nullval();
144 else if (*buf == '?')
146 u->which = Z_ETagUnit_wildThing;
148 u->u.wildThing = read_occurrences(buf+2, nmem, file, lineno);
150 u->u.wildThing = read_occurrences(0, nmem, file, lineno);
152 else if ((terms = sscanf(buf, "(%d,%511[^)]):%511[a-zA-Z0-9+]",
153 &type, value, occ)) >= 2)
158 int force_string = 0;
164 if (*valp && valp[strlen(valp)-1] == '\'')
167 u->which = Z_ETagUnit_specificTag;
168 u->u.specificTag = t = (Z_SpecificTag *)nmem_malloc(nmem, sizeof(*t));
169 t->tagType = nmem_intdup(nmem, type);
170 t->tagValue = (Z_StringOrNumeric *)
171 nmem_malloc(nmem, sizeof(*t->tagValue));
172 if (!force_string && isdigit(*(unsigned char *)valp))
175 t->tagValue->which = Z_StringOrNumeric_numeric;
176 t->tagValue->u.numeric = nmem_intdup(nmem, numval);
180 t->tagValue->which = Z_StringOrNumeric_string;
181 t->tagValue->u.string = nmem_strdup(nmem, valp);
183 if (terms > 2) /* an occurrences-spec exists */
184 t->occurrences = read_occurrences(occ, nmem, file, lineno);
188 else if ((terms = sscanf(buf, "%511[^)]", value)) >= 1)
193 u->which = Z_ETagUnit_specificTag;
194 u->u.specificTag = t = (Z_SpecificTag *)nmem_malloc(nmem, sizeof(*t));
195 t->tagType = nmem_intdup(nmem, 3);
196 t->tagValue = (Z_StringOrNumeric *)
197 nmem_malloc(nmem, sizeof(*t->tagValue));
198 t->tagValue->which = Z_StringOrNumeric_string;
199 t->tagValue->u.string = nmem_strdup(nmem, valp);
200 t->occurrences = read_occurrences("all", nmem, file, lineno);
210 * Read an element-set specification from a file.
211 * NOTE: If !o, memory is allocated directly from the heap by nmem_malloc().
213 Z_Espec1 *data1_read_espec1 (data1_handle dh, const char *file)
216 NMEM nmem = data1_nmem_get (dh);
218 int argc, size_esn = 0;
219 char *argv[50], line[512];
220 Z_Espec1 *res = (Z_Espec1 *)nmem_malloc(nmem, sizeof(*res));
222 if (!(f = data1_path_fopen(dh, file, "r")))
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;
233 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
234 if (!strcmp(argv[0], "elementsetnames"))
236 int nnames = argc-1, i;
240 yaz_log(YLOG_WARN, "%s:%d: Empty elementsetnames directive",
245 res->elementSetNames =
246 (char **)nmem_malloc(nmem, sizeof(char**)*nnames);
247 for (i = 0; i < nnames; i++)
249 res->elementSetNames[i] = nmem_strdup(nmem, argv[i+1]);
251 res->num_elementSetNames = nnames;
253 else if (!strcmp(argv[0], "defaultvariantsetid"))
257 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for %s",
258 file, lineno, argv[0]);
261 if (!(res->defaultVariantSetId =
262 odr_getoidbystr_nmem(nmem, argv[1])))
264 yaz_log(YLOG_WARN, "%s:%d: Bad defaultvariantsetid",
269 else if (!strcmp(argv[0], "defaulttagtype"))
273 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for %s",
274 file, lineno, argv[0]);
277 res->defaultTagType = nmem_intdup(nmem, atoi(argv[1]));
279 else if (!strcmp(argv[0], "defaultvariantrequest"))
281 if (!(res->defaultVariantRequest =
282 read_variant(argc-1, argv+1, nmem, file, lineno)))
284 yaz_log(YLOG_WARN, "%s:%d: Bad defaultvariantrequest",
289 else if (!strcmp(argv[0], "simpleelement"))
291 Z_ElementRequest *er;
294 char *path = argv[1];
299 res->elements = (Z_ElementRequest **)
300 nmem_malloc(nmem, size_esn = 24*sizeof(er));
301 else if (res->num_elements >= (int) (size_esn/sizeof(er)))
303 Z_ElementRequest **oe = res->elements;
305 res->elements = (Z_ElementRequest **)
306 nmem_malloc (nmem, size_esn*sizeof(er));
307 memcpy (res->elements, oe, size_esn/2);
311 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for %s",
312 file, lineno, argv[0]);
316 res->elements[res->num_elements++] = er =
317 (Z_ElementRequest *)nmem_malloc(nmem, sizeof(*er));
318 er->which = Z_ERequest_simpleElement;
319 er->u.simpleElement = se = (Z_SimpleElement *)
320 nmem_malloc(nmem, sizeof(*se));
321 se->variantRequest = 0;
322 se->path = tp = (Z_ETagPath *)nmem_malloc(nmem, sizeof(*tp));
325 * Parse the element selector.
327 for (num = 1, ep = path; (ep = strchr(ep, '/')); num++, ep++)
329 tp->tags = (Z_ETagUnit **)
330 nmem_malloc(nmem, sizeof(Z_ETagUnit*)*num);
332 for ((ep = strchr(path, '/')) ; path ;
333 (void)((path = ep) && (ep = strchr(path, '/'))))
340 tagunit = read_tagunit(path, nmem, file, lineno);
343 yaz_log (YLOG_WARN, "%s%d: Bad tag unit at %s",
347 tp->tags[tp->num_tags++] = tagunit;
350 if (argc > 2 && !strcmp(argv[2], "variant"))
352 read_variant(argc-3, argv+3, nmem, file, lineno);
355 yaz_log(YLOG_WARN, "%s:%d: Unknown directive '%s'",
356 file, lineno, argv[0]);
363 * indent-tabs-mode: nil
365 * vim: shiftwidth=4 tabstop=8 expandtab