1 /* $Id: d1_espec.c,v 1.2 2002-10-22 13:19:50 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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
28 #include <yaz/proto.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(LOG_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 = (int *)nmem_malloc(nmem, sizeof(int));
66 t->type = (int *)nmem_malloc(nmem, sizeof(int));
69 * This is wrong.. we gotta look up the correct type for the
70 * variant, I guess... damn this stuff.
74 t->which = Z_Triple_null;
75 t->value.null = odr_nullval();
77 else if (d1_isdigit(*value))
79 t->which = Z_Triple_integer;
80 t->value.integer = (int *)
81 nmem_malloc(nmem, sizeof(*t->value.integer));
82 *t->value.integer = atoi(value);
86 t->which = Z_Triple_internationalString;
87 t->value.internationalString = (char *)
88 nmem_malloc(nmem, strlen(value)+1);
89 strcpy(t->value.internationalString, value);
95 static Z_Occurrences *read_occurrences(char *occ, NMEM nmem,
96 const char *file, int lineno)
98 Z_Occurrences *op = (Z_Occurrences *)nmem_malloc(nmem, sizeof(*op));
103 op->which = Z_Occurrences_values;
104 op->u.values = (Z_OccurValues *)
105 nmem_malloc(nmem, sizeof(Z_OccurValues));
106 op->u.values->start = (int *)nmem_malloc(nmem, sizeof(int));
107 *op->u.values->start = 1;
108 op->u.values->howMany = 0;
110 else if (!strcmp(occ, "all"))
112 op->which = Z_Occurrences_all;
113 op->u.all = odr_nullval();
115 else if (!strcmp(occ, "last"))
117 op->which = Z_Occurrences_last;
118 op->u.all = odr_nullval();
122 Z_OccurValues *ov = (Z_OccurValues *)nmem_malloc(nmem, sizeof(*ov));
124 if (!d1_isdigit(*occ))
126 yaz_log(LOG_WARN, "%s:%d: Bad occurrences-spec %s",
130 op->which = Z_Occurrences_values;
132 ov->start = (int *)nmem_malloc(nmem, sizeof(*ov->start));
133 *ov->start = atoi(occ);
134 if ((p = strchr(occ, '+')))
136 ov->howMany = (int *)nmem_malloc(nmem, sizeof(*ov->howMany));
137 *ov->howMany = atoi(p + 1);
146 static Z_ETagUnit *read_tagunit(char *buf, NMEM nmem,
147 const char *file, int lineno)
149 Z_ETagUnit *u = (Z_ETagUnit *)nmem_malloc(nmem, sizeof(*u));
152 char value[512], occ[512];
156 u->which = Z_ETagUnit_wildPath;
157 u->u.wildPath = odr_nullval();
159 else if (*buf == '?')
161 u->which = Z_ETagUnit_wildThing;
163 u->u.wildThing = read_occurrences(buf+2, nmem, file, lineno);
165 u->u.wildThing = read_occurrences(0, nmem, file, lineno);
167 else if ((terms = sscanf(buf, "(%d,%511[^)]):%511[a-zA-Z0-9+]",
168 &type, value, occ)) >= 2)
173 int force_string = 0;
180 u->which = Z_ETagUnit_specificTag;
181 u->u.specificTag = t = (Z_SpecificTag *)nmem_malloc(nmem, sizeof(*t));
182 t->tagType = (int *)nmem_malloc(nmem, sizeof(*t->tagType));
184 t->tagValue = (Z_StringOrNumeric *)
185 nmem_malloc(nmem, sizeof(*t->tagValue));
186 if (!force_string && (numval = atoi(valp)))
188 t->tagValue->which = Z_StringOrNumeric_numeric;
189 t->tagValue->u.numeric = (int *)nmem_malloc(nmem, sizeof(int));
190 *t->tagValue->u.numeric = numval;
194 t->tagValue->which = Z_StringOrNumeric_string;
195 t->tagValue->u.string = (char *)nmem_malloc(nmem, strlen(valp)+1);
196 strcpy(t->tagValue->u.string, valp);
198 if (terms > 2) /* an occurrences-spec exists */
199 t->occurrences = read_occurrences(occ, nmem, file, lineno);
207 * Read an element-set specification from a file.
208 * NOTE: If !o, memory is allocated directly from the heap by nmem_malloc().
210 Z_Espec1 *data1_read_espec1 (data1_handle dh, const char *file)
213 NMEM nmem = data1_nmem_get (dh);
215 int argc, size_esn = 0;
216 char *argv[50], line[512];
217 Z_Espec1 *res = (Z_Espec1 *)nmem_malloc(nmem, sizeof(*res));
219 if (!(f = data1_path_fopen(dh, file, "r")))
221 yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
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(LOG_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] = (char *)
250 nmem_malloc(nmem, strlen(argv[i+1])+1);
251 strcpy(res->elementSetNames[i], argv[i+1]);
253 res->num_elementSetNames = nnames;
255 else if (!strcmp(argv[0], "defaultvariantsetid"))
259 yaz_log(LOG_WARN, "%s:%d: Bad # of args for %s",
260 file, lineno, argv[0]);
263 if (!(res->defaultVariantSetId =
264 odr_getoidbystr_nmem(nmem, argv[1])))
266 yaz_log(LOG_WARN, "%s:%d: Bad defaultvariantsetid",
271 else if (!strcmp(argv[0], "defaulttagtype"))
275 yaz_log(LOG_WARN, "%s:%d: Bad # of args for %s",
276 file, lineno, argv[0]);
279 res->defaultTagType = (int *)nmem_malloc(nmem, sizeof(int));
280 *res->defaultTagType = atoi(argv[1]);
282 else if (!strcmp(argv[0], "defaultvariantrequest"))
284 if (!(res->defaultVariantRequest =
285 read_variant(argc-1, argv+1, nmem, file, lineno)))
287 yaz_log(LOG_WARN, "%s:%d: Bad defaultvariantrequest",
292 else if (!strcmp(argv[0], "simpleelement"))
294 Z_ElementRequest *er;
297 char *path = argv[1];
302 res->elements = (Z_ElementRequest **)
303 nmem_malloc(nmem, size_esn = 24*sizeof(er));
304 else if (res->num_elements >= (int) (size_esn/sizeof(er)))
306 Z_ElementRequest **oe = res->elements;
308 res->elements = (Z_ElementRequest **)
309 nmem_malloc (nmem, size_esn*sizeof(er));
310 memcpy (res->elements, oe, size_esn/2);
314 yaz_log(LOG_WARN, "%s:%d: Bad # of args for %s",
315 file, lineno, argv[0]);
319 res->elements[res->num_elements++] = er =
320 (Z_ElementRequest *)nmem_malloc(nmem, sizeof(*er));
321 er->which = Z_ERequest_simpleElement;
322 er->u.simpleElement = se = (Z_SimpleElement *)
323 nmem_malloc(nmem, sizeof(*se));
324 se->variantRequest = 0;
325 se->path = tp = (Z_ETagPath *)nmem_malloc(nmem, sizeof(*tp));
328 * Parse the element selector.
330 for (num = 1, ep = path; (ep = strchr(ep, '/')); num++, ep++)
332 tp->tags = (Z_ETagUnit **)
333 nmem_malloc(nmem, sizeof(Z_ETagUnit*)*num);
335 for ((ep = strchr(path, '/')) ; path ;
336 (void)((path = ep) && (ep = strchr(path, '/'))))
343 tagunit = read_tagunit(path, nmem, file, lineno);
346 yaz_log (LOG_WARN, "%s%d: Bad tag unit at %s",
350 tp->tags[tp->num_tags++] = tagunit;
353 if (argc > 2 && !strcmp(argv[2], "variant"))
355 read_variant(argc-3, argv+3, nmem, file, lineno);
358 yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'",
359 file, lineno, argv[0]);