1 /* $Id: d1_espec.c,v 1.13 2007-01-15 15:10:14 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>
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")))
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;
238 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
239 if (!strcmp(argv[0], "elementsetnames"))
241 int nnames = argc-1, i;
245 yaz_log(YLOG_WARN, "%s:%d: Empty elementsetnames directive",
250 res->elementSetNames =
251 (char **)nmem_malloc(nmem, sizeof(char**)*nnames);
252 for (i = 0; i < nnames; i++)
254 res->elementSetNames[i] = nmem_strdup(nmem, argv[i+1]);
256 res->num_elementSetNames = nnames;
258 else if (!strcmp(argv[0], "defaultvariantsetid"))
262 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for %s",
263 file, lineno, argv[0]);
266 if (!(res->defaultVariantSetId =
267 odr_getoidbystr_nmem(nmem, argv[1])))
269 yaz_log(YLOG_WARN, "%s:%d: Bad defaultvariantsetid",
274 else if (!strcmp(argv[0], "defaulttagtype"))
278 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for %s",
279 file, lineno, argv[0]);
282 res->defaultTagType = nmem_intdup(nmem, atoi(argv[1]));
284 else if (!strcmp(argv[0], "defaultvariantrequest"))
286 if (!(res->defaultVariantRequest =
287 read_variant(argc-1, argv+1, nmem, file, lineno)))
289 yaz_log(YLOG_WARN, "%s:%d: Bad defaultvariantrequest",
294 else if (!strcmp(argv[0], "simpleelement"))
296 Z_ElementRequest *er;
299 char *path = argv[1];
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)))
308 Z_ElementRequest **oe = res->elements;
310 res->elements = (Z_ElementRequest **)
311 nmem_malloc (nmem, size_esn*sizeof(er));
312 memcpy (res->elements, oe, size_esn/2);
316 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for %s",
317 file, lineno, argv[0]);
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));
330 * Parse the element selector.
332 for (num = 1, ep = path; (ep = strchr(ep, '/')); num++, ep++)
334 tp->tags = (Z_ETagUnit **)
335 nmem_malloc(nmem, sizeof(Z_ETagUnit*)*num);
337 for ((ep = strchr(path, '/')) ; path ;
338 (void)((path = ep) && (ep = strchr(path, '/'))))
345 tagunit = read_tagunit(path, nmem, file, lineno);
348 yaz_log (YLOG_WARN, "%s%d: Bad tag unit at %s",
352 tp->tags[tp->num_tags++] = tagunit;
355 if (argc > 2 && !strcmp(argv[2], "variant"))
357 read_variant(argc-3, argv+3, nmem, file, lineno);
360 yaz_log(YLOG_WARN, "%s:%d: Unknown directive '%s'",
361 file, lineno, argv[0]);
368 * indent-tabs-mode: nil
370 * vim: shiftwidth=4 tabstop=8 expandtab