1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2010 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <yaz/proto.h>
28 #include <idzebra/data1.h>
29 #include <yaz/oid_db.h>
31 static Z_Variant *read_variant(int argc, char **argv, NMEM nmem,
32 const char *file, int lineno)
34 Z_Variant *r = (Z_Variant *)nmem_malloc(nmem, sizeof(*r));
37 r->globalVariantSetId = odr_oiddup_nmem(nmem, yaz_oid_varset_variant_1);
39 r->triples = (Z_Triple **)nmem_malloc(nmem, sizeof(Z_Triple*) * argc);
42 r->num_triples = argc;
43 for (i = 0; i < argc; i++)
49 if (sscanf(argv[i], "(%d,%d,%511[^)])", &zclass, &type, value) < 3)
51 yaz_log(YLOG_WARN, "%s:%d: Syntax error in variant component '%s'",
52 file, lineno, argv[i]);
55 t = r->triples[i] = (Z_Triple *)nmem_malloc(nmem, sizeof(Z_Triple));
57 t->zclass = nmem_intdup(nmem, zclass);
58 t->type = nmem_intdup(nmem, type);
60 * This is wrong.. we gotta look up the correct type for the
61 * variant, I guess... damn this stuff.
65 t->which = Z_Triple_null;
66 t->value.null = odr_nullval();
68 else if (d1_isdigit(*value))
70 t->which = Z_Triple_integer;
71 t->value.integer = nmem_intdup(nmem, atoi(value));
75 t->which = Z_Triple_internationalString;
76 t->value.internationalString = nmem_strdup(nmem, value);
82 static Z_Occurrences *read_occurrences(char *occ, NMEM nmem,
83 const char *file, int lineno)
85 Z_Occurrences *op = (Z_Occurrences *)nmem_malloc(nmem, sizeof(*op));
90 op->which = Z_Occurrences_values;
91 op->u.values = (Z_OccurValues *)
92 nmem_malloc(nmem, sizeof(Z_OccurValues));
93 op->u.values->start = nmem_intdup(nmem, 1);
94 op->u.values->howMany = 0;
96 else if (!strcmp(occ, "all"))
98 op->which = Z_Occurrences_all;
99 op->u.all = odr_nullval();
101 else if (!strcmp(occ, "last"))
103 op->which = Z_Occurrences_last;
104 op->u.all = odr_nullval();
108 Z_OccurValues *ov = (Z_OccurValues *)nmem_malloc(nmem, sizeof(*ov));
110 if (!d1_isdigit(*occ))
112 yaz_log(YLOG_WARN, "%s:%d: Bad occurrences-spec %s",
116 op->which = Z_Occurrences_values;
118 ov->start = nmem_intdup(nmem, atoi(occ));
119 if ((p = strchr(occ, '+')))
120 ov->howMany = nmem_intdup(nmem, atoi(p + 1));
128 static Z_ETagUnit *read_tagunit(char *buf, NMEM nmem,
129 const char *file, int lineno)
131 Z_ETagUnit *u = (Z_ETagUnit *)nmem_malloc(nmem, sizeof(*u));
134 char value[512], occ[512];
138 u->which = Z_ETagUnit_wildPath;
139 u->u.wildPath = odr_nullval();
141 else if (*buf == '?')
143 u->which = Z_ETagUnit_wildThing;
145 u->u.wildThing = read_occurrences(buf+2, nmem, file, lineno);
147 u->u.wildThing = read_occurrences(0, nmem, file, lineno);
149 else if ((terms = sscanf(buf, "(%d,%511[^)]):%511[a-zA-Z0-9+]",
150 &type, value, occ)) >= 2)
155 int force_string = 0;
161 if (*valp && valp[strlen(valp)-1] == '\'')
164 u->which = Z_ETagUnit_specificTag;
165 u->u.specificTag = t = (Z_SpecificTag *)nmem_malloc(nmem, sizeof(*t));
166 t->tagType = nmem_intdup(nmem, type);
167 t->tagValue = (Z_StringOrNumeric *)
168 nmem_malloc(nmem, sizeof(*t->tagValue));
169 if (!force_string && isdigit(*(unsigned char *)valp))
172 t->tagValue->which = Z_StringOrNumeric_numeric;
173 t->tagValue->u.numeric = nmem_intdup(nmem, numval);
177 t->tagValue->which = Z_StringOrNumeric_string;
178 t->tagValue->u.string = nmem_strdup(nmem, valp);
180 if (terms > 2) /* an occurrences-spec exists */
181 t->occurrences = read_occurrences(occ, nmem, file, lineno);
185 else if ((terms = sscanf(buf, "%511[^)]", value)) >= 1)
190 u->which = Z_ETagUnit_specificTag;
191 u->u.specificTag = t = (Z_SpecificTag *)nmem_malloc(nmem, sizeof(*t));
192 t->tagType = nmem_intdup(nmem, 3);
193 t->tagValue = (Z_StringOrNumeric *)
194 nmem_malloc(nmem, sizeof(*t->tagValue));
195 t->tagValue->which = Z_StringOrNumeric_string;
196 t->tagValue->u.string = nmem_strdup(nmem, valp);
197 t->occurrences = read_occurrences("all", 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")))
222 res->num_elementSetNames = 0;
223 res->elementSetNames = 0;
224 res->defaultVariantSetId = 0;
225 res->defaultVariantRequest = 0;
226 res->defaultTagType = 0;
227 res->num_elements = 0;
230 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
231 if (!strcmp(argv[0], "elementsetnames"))
233 int nnames = argc-1, i;
237 yaz_log(YLOG_WARN, "%s:%d: Empty elementsetnames directive",
242 res->elementSetNames =
243 (char **)nmem_malloc(nmem, sizeof(char**)*nnames);
244 for (i = 0; i < nnames; i++)
246 res->elementSetNames[i] = nmem_strdup(nmem, argv[i+1]);
248 res->num_elementSetNames = nnames;
250 else if (!strcmp(argv[0], "defaultvariantsetid"))
254 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for %s",
255 file, lineno, argv[0]);
258 if (!(res->defaultVariantSetId =
259 odr_getoidbystr_nmem(nmem, argv[1])))
261 yaz_log(YLOG_WARN, "%s:%d: Bad defaultvariantsetid",
266 else if (!strcmp(argv[0], "defaulttagtype"))
270 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for %s",
271 file, lineno, argv[0]);
274 res->defaultTagType = nmem_intdup(nmem, atoi(argv[1]));
276 else if (!strcmp(argv[0], "defaultvariantrequest"))
278 if (!(res->defaultVariantRequest =
279 read_variant(argc-1, argv+1, nmem, file, lineno)))
281 yaz_log(YLOG_WARN, "%s:%d: Bad defaultvariantrequest",
286 else if (!strcmp(argv[0], "simpleelement"))
288 Z_ElementRequest *er;
291 char *path = argv[1];
296 res->elements = (Z_ElementRequest **)
297 nmem_malloc(nmem, size_esn = 24*sizeof(er));
298 else if (res->num_elements >= (int) (size_esn/sizeof(er)))
300 Z_ElementRequest **oe = res->elements;
302 res->elements = (Z_ElementRequest **)
303 nmem_malloc (nmem, size_esn*sizeof(er));
304 memcpy (res->elements, oe, size_esn/2);
308 yaz_log(YLOG_WARN, "%s:%d: Bad # of args for %s",
309 file, lineno, argv[0]);
313 res->elements[res->num_elements++] = er =
314 (Z_ElementRequest *)nmem_malloc(nmem, sizeof(*er));
315 er->which = Z_ERequest_simpleElement;
316 er->u.simpleElement = se = (Z_SimpleElement *)
317 nmem_malloc(nmem, sizeof(*se));
318 se->variantRequest = 0;
319 se->path = tp = (Z_ETagPath *)nmem_malloc(nmem, sizeof(*tp));
322 * Parse the element selector.
324 for (num = 1, ep = path; (ep = strchr(ep, '/')); num++, ep++)
326 tp->tags = (Z_ETagUnit **)
327 nmem_malloc(nmem, sizeof(Z_ETagUnit*)*num);
329 for ((ep = strchr(path, '/')) ; path ;
330 (void)((path = ep) && (ep = strchr(path, '/'))))
337 tagunit = read_tagunit(path, nmem, file, lineno);
340 yaz_log (YLOG_WARN, "%s%d: Bad tag unit at %s",
344 tp->tags[tp->num_tags++] = tagunit;
347 if (argc > 2 && !strcmp(argv[2], "variant"))
349 read_variant(argc-3, argv+3, nmem, file, lineno);
352 yaz_log(YLOG_WARN, "%s:%d: Unknown directive '%s'",
353 file, lineno, argv[0]);
360 * c-file-style: "Stroustrup"
361 * indent-tabs-mode: nil
363 * vim: shiftwidth=4 tabstop=8 expandtab