2 * Copyright (c) 1995-1997, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1997-09-29 13:18:59 adam
8 * Added function, oid_ent_to_oid, to replace the function
9 * oid_getoidbyent, which is not thread safe.
11 * Revision 1.10 1997/09/29 07:21:10 adam
12 * Added typecast to avoid warnings on MSVC.
14 * Revision 1.9 1997/09/17 12:10:35 adam
17 * Revision 1.8 1997/09/05 09:50:56 adam
18 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
20 * Revision 1.7 1997/05/14 06:54:02 adam
23 * Revision 1.6 1996/07/06 19:58:34 quinn
24 * System headerfiles gathered in yconfig
26 * Revision 1.5 1996/01/02 08:57:44 quinn
27 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
29 * Revision 1.4 1995/12/05 11:16:10 quinn
32 * Revision 1.3 1995/11/13 09:27:34 quinn
33 * Fiddling with the variant stuff.
35 * Revision 1.2 1995/11/01 16:34:56 quinn
36 * Making data1 look for tables in data1_tabpath
38 * Revision 1.1 1995/11/01 11:56:07 quinn
39 * Added Retrieval (data management) functions en masse.
57 static Z_Variant *read_variant(int argc, char **argv, ODR o)
59 Z_Variant *r = odr_malloc(o, sizeof(*r));
64 var1.proto = PROTO_Z3950;
65 var1.oclass = CLASS_VARSET;
66 var1.value = VAL_VAR1;
67 r->globalVariantSetId = odr_oiddup(o, oid_ent_to_oid(&var1, oid));
70 r->triples = odr_malloc(o, sizeof(Z_Triple*) * argc);
73 r->num_triples = argc;
74 for (i = 0; i < argc; i++)
80 if (sscanf(argv[i], "(%d,%d,%[^)])", &zclass, &type, value) < 3)
82 logf(LOG_WARN, "Syntax error in variant component '%s'",
86 t = r->triples[i] = odr_malloc(o, sizeof(Z_Triple));
88 t->zclass = odr_malloc(o, sizeof(int));
90 t->type = odr_malloc(o, sizeof(int));
93 * This is wrong.. we gotta look up the correct type for the
94 * variant, I guess... damn this stuff.
98 t->which = Z_Triple_null;
99 t->value.null = ODR_NULLVAL;
101 else if (isdigit(*value))
103 t->which = Z_Triple_integer;
104 t->value.integer = odr_malloc(o, sizeof(*t->value.integer));
105 *t->value.integer = atoi(value);
109 t->which = Z_Triple_internationalString;
110 t->value.internationalString = odr_malloc(o, strlen(value)+1);
111 strcpy(t->value.internationalString, value);
117 static Z_Occurrences *read_occurrences(char *occ, ODR o)
119 Z_Occurrences *op = odr_malloc(o, sizeof(*op));
124 op->which = Z_Occurrences_values;
125 op->u.values = odr_malloc(o, sizeof(Z_OccurValues));
126 op->u.values->start = odr_malloc(o, sizeof(int));
127 *op->u.values->start = 1;
128 op->u.values->howMany = 0;
130 else if (!strcmp(occ, "all"))
132 op->which = Z_Occurrences_all;
133 op->u.all = ODR_NULLVAL;
135 else if (!strcmp(occ, "last"))
137 op->which = Z_Occurrences_last;
138 op->u.all = ODR_NULLVAL;
142 Z_OccurValues *ov = odr_malloc(o, sizeof(*ov));
146 logf(LOG_WARN, "Bad occurrences-spec in %s", occ);
149 op->which = Z_Occurrences_values;
151 ov->start = odr_malloc(o, sizeof(*ov->start));
152 *ov->start = atoi(occ);
153 if ((p = strchr(occ, '+')))
155 ov->howMany = odr_malloc(o, sizeof(*ov->howMany));
156 *ov->howMany = atoi(p + 1);
165 static Z_ETagUnit *read_tagunit(char *buf, ODR o)
167 Z_ETagUnit *u = odr_malloc(o, sizeof(*u));
170 char value[512], occ[512];
174 u->which = Z_ETagUnit_wildPath;
175 u->u.wildPath = ODR_NULLVAL;
177 else if (*buf == '?')
179 u->which = Z_ETagUnit_wildThing;
181 u->u.wildThing = read_occurrences(buf+2, o);
183 u->u.wildThing = read_occurrences(0, o);
185 else if ((terms = sscanf(buf, "(%d,%[^)]):%[a-z0-9+]", &type, value,
191 int force_string = 0;
198 u->which = Z_ETagUnit_specificTag;
199 u->u.specificTag = t = odr_malloc(o, sizeof(*t));
200 t->tagType = odr_malloc(o, sizeof(*t->tagType));
202 t->tagValue = odr_malloc(o, sizeof(*t->tagValue));
203 if (!force_string && (numval = atoi(valp)))
205 t->tagValue->which = Z_StringOrNumeric_numeric;
206 t->tagValue->u.numeric = odr_malloc(o, sizeof(int));
207 *t->tagValue->u.numeric = numval;
211 t->tagValue->which = Z_StringOrNumeric_string;
212 t->tagValue->u.string = odr_malloc(o, strlen(valp)+1);
213 strcpy(t->tagValue->u.string, valp);
215 if (terms > 2) /* an occurrences-spec exists */
216 t->occurrences = read_occurrences(occ, o);
224 * Read an element-set specification from a file.
225 * NOTE: If !o, memory is allocated directly from the heap by odr_malloc().
227 Z_Espec1 *data1_read_espec1 (data1_handle dh, const char *file, ODR o)
230 int argc, size_esn = 0;
231 char *argv[50], line[512];
232 Z_Espec1 *res = odr_malloc(o, sizeof(*res));
234 if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
236 logf(LOG_WARN|LOG_ERRNO, "%s", file);
240 res->num_elementSetNames = 0;
241 res->elementSetNames = 0;
242 res->defaultVariantSetId = 0;
243 res->defaultVariantRequest = 0;
244 res->defaultTagType = 0;
245 res->num_elements = 0;
248 while ((argc = readconf_line(f, line, 512, argv, 50)))
249 if (!strcmp(argv[0], "elementsetnames"))
251 int nnames = argc-1, i;
255 logf(LOG_WARN, "%s: Empty elementsetnames directive",
260 res->elementSetNames = odr_malloc(o, sizeof(char**)*nnames);
261 for (i = 0; i < nnames; i++)
263 res->elementSetNames[i] = odr_malloc(o, strlen(argv[i+1])+1);
264 strcpy(res->elementSetNames[i], argv[i+1]);
266 res->num_elementSetNames = nnames;
268 else if (!strcmp(argv[0], "defaultvariantsetid"))
270 if (argc != 2 || !(res->defaultVariantSetId =
271 odr_getoidbystr(o, argv[1])))
273 logf(LOG_WARN, "%s: Bad defaultvariantsetid directive", file);
277 else if (!strcmp(argv[0], "defaulttagtype"))
281 logf(LOG_WARN, "%s: Bad defaulttagtype directive", file);
284 res->defaultTagType = odr_malloc(o, sizeof(int));
285 *res->defaultTagType = atoi(argv[1]);
287 else if (!strcmp(argv[0], "defaultvariantrequest"))
289 if (!(res->defaultVariantRequest = read_variant(argc-1, argv+1, o)))
291 logf(LOG_WARN, "%s: Bad defaultvariantrequest", file);
295 else if (!strcmp(argv[0], "simpleelement"))
297 Z_ElementRequest *er;
300 char *path = argv[1];
305 res->elements = odr_malloc(o, size_esn = 24*sizeof(er));
306 else if (res->num_elements >= (int) (size_esn/sizeof(er)))
311 Z_ElementRequest **oe = res->elements;
313 res->elements = odr_malloc (o, size_esn*sizeof(er));
314 memcpy (res->elements, oe, size_esn/2);
318 xrealloc(res->elements, size_esn*sizeof(er));
322 logf(LOG_WARN, "%s: Empty simpleelement directive", file);
326 res->elements[res->num_elements++] = er =
327 odr_malloc(o, sizeof(*er));
328 er->which = Z_ERequest_simpleElement;
329 er->u.simpleElement = se = odr_malloc(o, sizeof(*se));
330 se->variantRequest = 0;
331 se->path = tp = odr_malloc(o, sizeof(*tp));
334 * Parse the element selector.
336 for (num = 1, ep = path; (ep = strchr(ep, '/')); num++, ep++)
338 tp->tags = odr_malloc(o, sizeof(Z_ETagUnit*)*num);
340 for ((ep = strchr(path, '/')) ; path ;
341 (void)((path = ep) && (ep = strchr(path, '/'))))
347 tp->tags[tp->num_tags++] = read_tagunit(path, o);
350 if (argc > 2 && !strcmp(argv[2], "variant"))
351 se->variantRequest= read_variant(argc-3, argv+3, o);
355 logf(LOG_WARN, "%s: Unknown directive %s", file, argv[0]);