2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.16 1999-08-27 09:40:32 adam
8 * Renamed logf function to yaz_log. Removed VC++ project files.
10 * Revision 1.15 1998/10/13 16:09:49 adam
11 * Added support for arbitrary OID's for tagsets, schemas and attribute sets.
12 * Added support for multiple attribute set references and tagset references
13 * from an abstract syntax file.
14 * Fixed many bad logs-calls in routines that read the various
15 * specifications regarding data1 (*.abs,*.att,...) and made the messages
16 * consistent whenever possible.
17 * Added extra 'lineno' argument to function readconf_line.
19 * Revision 1.14 1998/02/11 11:53:35 adam
20 * Changed code so that it compiles as C++.
22 * Revision 1.13 1997/11/24 11:33:56 adam
23 * Using function odr_nullval() instead of global ODR_NULLVAL when
26 * Revision 1.12 1997/10/31 12:20:09 adam
27 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
28 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
29 * Bug fix: missing fclose in data1_read_espec1.
31 * Revision 1.11 1997/09/29 13:18:59 adam
32 * Added function, oid_ent_to_oid, to replace the function
33 * oid_getoidbyent, which is not thread safe.
35 * Revision 1.10 1997/09/29 07:21:10 adam
36 * Added typecast to avoid warnings on MSVC.
38 * Revision 1.9 1997/09/17 12:10:35 adam
41 * Revision 1.8 1997/09/05 09:50:56 adam
42 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
44 * Revision 1.7 1997/05/14 06:54:02 adam
47 * Revision 1.6 1996/07/06 19:58:34 quinn
48 * System headerfiles gathered in yconfig
50 * Revision 1.5 1996/01/02 08:57:44 quinn
51 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
53 * Revision 1.4 1995/12/05 11:16:10 quinn
56 * Revision 1.3 1995/11/13 09:27:34 quinn
57 * Fiddling with the variant stuff.
59 * Revision 1.2 1995/11/01 16:34:56 quinn
60 * Making data1 look for tables in data1_tabpath
62 * Revision 1.1 1995/11/01 11:56:07 quinn
63 * Added Retrieval (data management) functions en masse.
78 static Z_Variant *read_variant(int argc, char **argv, NMEM nmem,
79 const char *file, int lineno)
81 Z_Variant *r = (Z_Variant *)nmem_malloc(nmem, sizeof(*r));
86 var1.proto = PROTO_Z3950;
87 var1.oclass = CLASS_VARSET;
88 var1.value = VAL_VAR1;
89 r->globalVariantSetId = odr_oiddup_nmem(nmem, oid_ent_to_oid(&var1, oid));
92 r->triples = (Z_Triple **)nmem_malloc(nmem, sizeof(Z_Triple*) * argc);
95 r->num_triples = argc;
96 for (i = 0; i < argc; i++)
102 if (sscanf(argv[i], "(%d,%d,%[^)])", &zclass, &type, value) < 3)
104 yaz_log(LOG_WARN, "%s:%d: Syntax error in variant component '%s'",
105 file, lineno, argv[i]);
108 t = r->triples[i] = (Z_Triple *)nmem_malloc(nmem, sizeof(Z_Triple));
110 t->zclass = (int *)nmem_malloc(nmem, sizeof(int));
112 t->type = (int *)nmem_malloc(nmem, sizeof(int));
115 * This is wrong.. we gotta look up the correct type for the
116 * variant, I guess... damn this stuff.
120 t->which = Z_Triple_null;
121 t->value.null = odr_nullval();
123 else if (isdigit(*value))
125 t->which = Z_Triple_integer;
126 t->value.integer = (int *)
127 nmem_malloc(nmem, sizeof(*t->value.integer));
128 *t->value.integer = atoi(value);
132 t->which = Z_Triple_internationalString;
133 t->value.internationalString = (char *)
134 nmem_malloc(nmem, strlen(value)+1);
135 strcpy(t->value.internationalString, value);
141 static Z_Occurrences *read_occurrences(char *occ, NMEM nmem,
142 const char *file, int lineno)
144 Z_Occurrences *op = (Z_Occurrences *)nmem_malloc(nmem, sizeof(*op));
149 op->which = Z_Occurrences_values;
150 op->u.values = (Z_OccurValues *)
151 nmem_malloc(nmem, sizeof(Z_OccurValues));
152 op->u.values->start = (int *)nmem_malloc(nmem, sizeof(int));
153 *op->u.values->start = 1;
154 op->u.values->howMany = 0;
156 else if (!strcmp(occ, "all"))
158 op->which = Z_Occurrences_all;
159 op->u.all = odr_nullval();
161 else if (!strcmp(occ, "last"))
163 op->which = Z_Occurrences_last;
164 op->u.all = odr_nullval();
168 Z_OccurValues *ov = (Z_OccurValues *)nmem_malloc(nmem, sizeof(*ov));
172 yaz_log(LOG_WARN, "%s:%d: Bad occurrences-spec %s",
176 op->which = Z_Occurrences_values;
178 ov->start = (int *)nmem_malloc(nmem, sizeof(*ov->start));
179 *ov->start = atoi(occ);
180 if ((p = strchr(occ, '+')))
182 ov->howMany = (int *)nmem_malloc(nmem, sizeof(*ov->howMany));
183 *ov->howMany = atoi(p + 1);
192 static Z_ETagUnit *read_tagunit(char *buf, NMEM nmem,
193 const char *file, int lineno)
195 Z_ETagUnit *u = (Z_ETagUnit *)nmem_malloc(nmem, sizeof(*u));
198 char value[512], occ[512];
202 u->which = Z_ETagUnit_wildPath;
203 u->u.wildPath = odr_nullval();
205 else if (*buf == '?')
207 u->which = Z_ETagUnit_wildThing;
209 u->u.wildThing = read_occurrences(buf+2, nmem, file, lineno);
211 u->u.wildThing = read_occurrences(0, nmem, file, lineno);
213 else if ((terms = sscanf(buf, "(%d,%[^)]):%[a-z0-9+]", &type, value,
219 int force_string = 0;
226 u->which = Z_ETagUnit_specificTag;
227 u->u.specificTag = t = (Z_SpecificTag *)nmem_malloc(nmem, sizeof(*t));
228 t->tagType = (int *)nmem_malloc(nmem, sizeof(*t->tagType));
230 t->tagValue = (Z_StringOrNumeric *)
231 nmem_malloc(nmem, sizeof(*t->tagValue));
232 if (!force_string && (numval = atoi(valp)))
234 t->tagValue->which = Z_StringOrNumeric_numeric;
235 t->tagValue->u.numeric = (int *)nmem_malloc(nmem, sizeof(int));
236 *t->tagValue->u.numeric = numval;
240 t->tagValue->which = Z_StringOrNumeric_string;
241 t->tagValue->u.string = (char *)nmem_malloc(nmem, strlen(valp)+1);
242 strcpy(t->tagValue->u.string, valp);
244 if (terms > 2) /* an occurrences-spec exists */
245 t->occurrences = read_occurrences(occ, nmem, file, lineno);
253 * Read an element-set specification from a file.
254 * NOTE: If !o, memory is allocated directly from the heap by nmem_malloc().
256 Z_Espec1 *data1_read_espec1 (data1_handle dh, const char *file)
259 NMEM nmem = data1_nmem_get (dh);
261 int argc, size_esn = 0;
262 char *argv[50], line[512];
263 Z_Espec1 *res = (Z_Espec1 *)nmem_malloc(nmem, sizeof(*res));
265 if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
267 yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
271 res->num_elementSetNames = 0;
272 res->elementSetNames = 0;
273 res->defaultVariantSetId = 0;
274 res->defaultVariantRequest = 0;
275 res->defaultTagType = 0;
276 res->num_elements = 0;
279 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
280 if (!strcmp(argv[0], "elementsetnames"))
282 int nnames = argc-1, i;
286 yaz_log(LOG_WARN, "%s:%d: Empty elementsetnames directive",
291 res->elementSetNames =
292 (char **)nmem_malloc(nmem, sizeof(char**)*nnames);
293 for (i = 0; i < nnames; i++)
295 res->elementSetNames[i] = (char *)
296 nmem_malloc(nmem, strlen(argv[i+1])+1);
297 strcpy(res->elementSetNames[i], argv[i+1]);
299 res->num_elementSetNames = nnames;
301 else if (!strcmp(argv[0], "defaultvariantsetid"))
305 yaz_log(LOG_WARN, "%s:%d: Bad # of args for %s",
306 file, lineno, argv[0]);
309 if (!(res->defaultVariantSetId =
310 odr_getoidbystr_nmem(nmem, argv[1])))
312 yaz_log(LOG_WARN, "%s:%d: Bad defaultvariantsetid",
317 else if (!strcmp(argv[0], "defaulttagtype"))
321 yaz_log(LOG_WARN, "%s:%d: Bad # of args for %s",
322 file, lineno, argv[0]);
325 res->defaultTagType = (int *)nmem_malloc(nmem, sizeof(int));
326 *res->defaultTagType = atoi(argv[1]);
328 else if (!strcmp(argv[0], "defaultvariantrequest"))
330 if (!(res->defaultVariantRequest =
331 read_variant(argc-1, argv+1, nmem, file, lineno)))
333 yaz_log(LOG_WARN, "%s:%d: Bad defaultvariantrequest",
338 else if (!strcmp(argv[0], "simpleelement"))
340 Z_ElementRequest *er;
343 char *path = argv[1];
348 res->elements = (Z_ElementRequest **)
349 nmem_malloc(nmem, size_esn = 24*sizeof(er));
350 else if (res->num_elements >= (int) (size_esn/sizeof(er)))
352 Z_ElementRequest **oe = res->elements;
354 res->elements = (Z_ElementRequest **)
355 nmem_malloc (nmem, size_esn*sizeof(er));
356 memcpy (res->elements, oe, size_esn/2);
360 yaz_log(LOG_WARN, "%s:%d: Bad # of args for %s",
361 file, lineno, argv[0]);
365 res->elements[res->num_elements++] = er =
366 (Z_ElementRequest *)nmem_malloc(nmem, sizeof(*er));
367 er->which = Z_ERequest_simpleElement;
368 er->u.simpleElement = se = (Z_SimpleElement *)
369 nmem_malloc(nmem, sizeof(*se));
370 se->variantRequest = 0;
371 se->path = tp = (Z_ETagPath *)nmem_malloc(nmem, sizeof(*tp));
374 * Parse the element selector.
376 for (num = 1, ep = path; (ep = strchr(ep, '/')); num++, ep++)
378 tp->tags = (Z_ETagUnit **)
379 nmem_malloc(nmem, sizeof(Z_ETagUnit*)*num);
381 for ((ep = strchr(path, '/')) ; path ;
382 (void)((path = ep) && (ep = strchr(path, '/'))))
388 tp->tags[tp->num_tags++] =
389 read_tagunit(path, nmem, file, lineno);
392 if (argc > 2 && !strcmp(argv[2], "variant"))
394 read_variant(argc-3, argv+3, nmem, file, lineno);
397 yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'",
398 file, lineno, argv[0]);