2 * Copyright (c) 1995-1998, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.1 1999-06-08 10:10:16 adam
8 * New sub directory zutil. Moved YAZ Compiler to be part of YAZ tree.
10 * Revision 1.22 1999/04/20 09:56:49 adam
11 * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
12 * Modified all encoders/decoders to reflect this change.
14 * Revision 1.21 1998/10/13 16:03:37 adam
15 * Better checking for invalid OID's in p_query_rpn.
17 * Revision 1.20 1998/03/31 15:13:20 adam
18 * Development towards compiled ASN.1.
20 * Revision 1.19 1998/03/05 08:09:03 adam
21 * Minor change to make C++ happy.
23 * Revision 1.18 1998/02/11 11:53:36 adam
24 * Changed code so that it compiles as C++.
26 * Revision 1.17 1997/11/24 11:33:57 adam
27 * Using function odr_nullval() instead of global ODR_NULLVAL when
30 * Revision 1.16 1997/09/29 13:19:00 adam
31 * Added function, oid_ent_to_oid, to replace the function
32 * oid_getoidbyent, which is not thread safe.
34 * Revision 1.15 1997/09/29 07:13:43 adam
35 * Changed type of a few variables to avoid warnings.
37 * Revision 1.14 1997/09/22 12:33:41 adam
38 * Fixed bug introduced by previous commit.
40 * Revision 1.13 1997/09/17 12:10:42 adam
43 * Revision 1.12 1997/09/01 08:54:13 adam
44 * New windows NT/95 port using MSV5.0. Made prefix query handling
45 * thread safe. The function options ignores empty arguments when met.
47 * Revision 1.11 1996/11/11 13:15:29 adam
48 * Added proximity operator.
50 * Revision 1.10 1996/08/12 14:10:35 adam
51 * New function p_query_attset to define default attribute set.
53 * Revision 1.9 1996/03/15 11:03:46 adam
54 * Attribute set can be set globally for a query with the @attrset
55 * operator. The @attr operator has an optional attribute-set specifier
56 * that sets the attribute set locally.
58 * Revision 1.8 1996/01/02 11:46:56 quinn
59 * Changed 'operator' to 'roperator' to avoid C++ conflict.
61 * Revision 1.7 1995/09/29 17:12:36 quinn
64 * Revision 1.6 1995/09/27 15:03:03 quinn
65 * Modified function heads & prototypes.
67 * Revision 1.5 1995/06/15 12:31:02 quinn
68 * *** empty log message ***
70 * Revision 1.4 1995/06/15 07:45:19 quinn
73 * Revision 1.3 1995/06/14 11:06:35 adam
74 * Bug fix: Attributes wasn't interpreted correctly!
76 * Revision 1.2 1995/05/26 08:56:11 adam
77 * New function: p_query_scan.
79 * Revision 1.1 1995/05/22 15:31:49 adam
80 * New function, p_query_rpn, to convert from prefix (ascii) to rpn (asn).
93 static oid_value p_query_dfset = VAL_NONE;
96 const char *query_buf;
106 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o, oid_proto,
107 int num_attr, int max_attr,
108 int *attr_list, oid_value *attr_set);
110 static enum oid_value query_oid_getvalbyname (struct lex_info *li)
112 enum oid_value value;
115 if (li->lex_len > 31)
117 memcpy (buf, li->lex_buf, li->lex_len);
118 buf[li->lex_len] = '\0';
119 value = oid_getvalbyname (buf);
123 static int compare_term (struct lex_info *li, const char *src, size_t off)
125 size_t len=strlen(src);
127 if (li->lex_len == len+off && !memcmp (li->lex_buf+off, src, len-off))
132 static int query_token (struct lex_info *li)
134 const char *sep_match;
135 const char **qptr = &li->query_buf;
137 while (**qptr == ' ')
142 if ((sep_match = strchr (li->left_sep, **qptr)))
144 int sep_index = sep_match - li->left_sep;
148 while (**qptr && **qptr != li->right_sep[sep_index])
159 while (**qptr && **qptr != ' ')
165 if (li->lex_len >= 1 && li->lex_buf[0] == li->escape_char)
167 if (compare_term (li, "and", 1))
169 if (compare_term (li, "or", 1))
171 if (compare_term (li, "not", 1))
173 if (compare_term (li, "attr", 1))
175 if (compare_term (li, "set", 1))
177 if (compare_term (li, "attrset", 1))
179 if (compare_term (li, "prox", 1))
181 if (compare_term (li, "term", 1))
187 static int lex (struct lex_info *li)
189 return li->query_look = query_token (li);
192 static Z_AttributesPlusTerm *rpn_term (struct lex_info *li, ODR o,
194 int num_attr, int *attr_list,
197 Z_AttributesPlusTerm *zapt;
200 Z_AttributeElement **elements;
202 zapt = (Z_AttributesPlusTerm *)odr_malloc (o, sizeof(*zapt));
203 term_octet = (Odr_oct *)odr_malloc (o, sizeof(*term_octet));
204 term = (Z_Term *)odr_malloc (o, sizeof(*term));
207 elements = (Z_AttributeElement**)odr_nullval();
213 elements = (Z_AttributeElement**)
214 odr_malloc (o, num_attr * sizeof(*elements));
216 attr_tmp = (int *)odr_malloc (o, num_attr * 2 * sizeof(int));
217 memcpy (attr_tmp, attr_list, num_attr * 2 * sizeof(int));
218 for (i = 0; i < num_attr; i++)
221 (Z_AttributeElement*)odr_malloc (o,sizeof(**elements));
222 elements[i]->attributeType = &attr_tmp[2*i];
223 if (attr_set[i] == VAL_NONE)
224 elements[i]->attributeSet = 0;
230 attrid.proto = PROTO_Z3950;
231 attrid.oclass = CLASS_ATTSET;
232 attrid.value = attr_set[i];
234 elements[i]->attributeSet =
235 odr_oiddup (o, oid_ent_to_oid (&attrid, oid));
237 elements[i]->which = Z_AttributeValue_numeric;
238 elements[i]->value.numeric = &attr_tmp[2*i+1];
242 zapt->attributes = (Z_AttributeList *)
243 odr_malloc (o, sizeof(*zapt->attributes));
244 zapt->attributes->num_attributes = num_attr;
245 zapt->attributes->attributes = elements;
247 zapt->num_attributes = num_attr;
248 zapt->attributeList = elements;
252 term->which = Z_Term_general;
253 term->u.general = term_octet;
254 term_octet->buf = (unsigned char *)odr_malloc (o, li->lex_len);
255 term_octet->size = term_octet->len = li->lex_len;
256 memcpy (term_octet->buf, li->lex_buf, li->lex_len);
260 static Z_Operand *rpn_simple (struct lex_info *li, ODR o, oid_proto proto,
261 int num_attr, int *attr_list,
266 zo = (Z_Operand *)odr_malloc (o, sizeof(*zo));
267 switch (li->query_look)
270 zo->which = Z_Operand_APT;
271 if (!(zo->u.attributesPlusTerm =
272 rpn_term (li, o, proto, num_attr, attr_list, attr_set)))
280 zo->which = Z_Operand_resultSetId;
281 zo->u.resultSetId = (char *)odr_malloc (o, li->lex_len+1);
282 memcpy (zo->u.resultSetId, li->lex_buf, li->lex_len);
283 zo->u.resultSetId[li->lex_len] = '\0';
292 static Z_ProximityOperator *rpn_proximity (struct lex_info *li, ODR o)
294 Z_ProximityOperator *p = (Z_ProximityOperator *)odr_malloc (o, sizeof(*p));
298 if (*li->lex_buf == '1')
300 p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion));
303 else if (*li->lex_buf == '0')
305 p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion));
313 p->distance = (int *)odr_malloc (o, sizeof(*p->distance));
314 *p->distance = atoi (li->lex_buf);
318 p->ordered = (int *)odr_malloc (o, sizeof(*p->ordered));
319 *p->ordered = atoi (li->lex_buf);
323 p->relationType = (int *)odr_malloc (o, sizeof(*p->relationType));
324 *p->relationType = atoi (li->lex_buf);
328 if (*li->lex_buf == 'k')
330 else if (*li->lex_buf == 'p')
333 p->which = atoi (li->lex_buf);
338 p->which = Z_ProximityOperator_known;
339 p->u.known = (int *)odr_malloc (o, sizeof(*p->u.known));
340 *p->u.known = atoi (li->lex_buf);
342 p->proximityUnitCode = (int *)odr_malloc (o, sizeof(*p->proximityUnitCode));
343 *p->proximityUnitCode = atoi (li->lex_buf);
348 static Z_Complex *rpn_complex (struct lex_info *li, ODR o, oid_proto proto,
349 int num_attr, int max_attr,
350 int *attr_list, oid_value *attr_set)
355 zc = (Z_Complex *)odr_malloc (o, sizeof(*zc));
356 zo = (Z_Operator *)odr_malloc (o, sizeof(*zo));
358 switch (li->query_look)
361 zo->which = Z_Operator_and;
362 zo->u.and = odr_nullval();
365 zo->which = Z_Operator_or;
366 zo->u.and = odr_nullval();
369 zo->which = Z_Operator_and_not;
370 zo->u.and = odr_nullval();
373 zo->which = Z_Operator_prox;
374 zo->u.prox = rpn_proximity (li, o);
383 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
387 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
393 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o,
395 int num_attr, int max_attr,
396 int *attr_list, oid_value *attr_set)
401 sz = (Z_RPNStructure *)odr_malloc (o, sizeof(*sz));
402 switch (li->query_look)
408 sz->which = Z_RPNStructure_complex;
409 if (!(sz->u.complex =
410 rpn_complex (li, o, proto, num_attr, max_attr, attr_list,
416 sz->which = Z_RPNStructure_simple;
418 rpn_simple (li, o, proto, num_attr, attr_list,
426 if (num_attr >= max_attr)
428 if (!(cp = strchr (li->lex_buf, '=')) ||
429 (size_t) (cp-li->lex_buf) > li->lex_len)
431 attr_set[num_attr] = query_oid_getvalbyname (li);
432 if (attr_set[num_attr] == VAL_NONE)
436 if (!(cp = strchr (li->lex_buf, '=')))
442 attr_set[num_attr] = attr_set[num_attr-1];
444 attr_set[num_attr] = VAL_NONE;
446 attr_list[2*num_attr] = atoi (li->lex_buf);
447 attr_list[2*num_attr+1] = atoi (cp+1);
451 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
457 if (compare_term (li, "general", 0))
458 li->term_type = Z_Term_general;
459 else if (compare_term (li, "numeric", 0))
460 li->term_type = Z_Term_numeric;
461 else if (compare_term (li, "string", 0))
462 li->term_type = Z_Term_characterString;
463 else if (compare_term (li, "oid", 0))
464 li->term_type = Z_Term_oid;
465 else if (compare_term (li, "datetime", 0))
466 li->term_type = Z_Term_dateTime;
467 else if (compare_term (li, "null", 0))
468 li->term_type = Z_Term_null;
471 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
473 case 0: /* operator/operand expected! */
479 Z_RPNQuery *p_query_rpn_mk (ODR o, struct lex_info *li, oid_proto proto,
483 int attr_array[1024];
484 oid_value attr_set[512];
485 oid_value topSet = VAL_NONE;
489 zq = (Z_RPNQuery *)odr_malloc (o, sizeof(*zq));
491 if (li->query_look == 'r')
494 topSet = query_oid_getvalbyname (li);
495 if (topSet == VAL_NONE)
500 if (topSet == VAL_NONE)
501 topSet = p_query_dfset;
502 if (topSet == VAL_NONE)
505 oset.oclass = CLASS_ATTSET;
508 if (!oid_ent_to_oid (&oset, oid))
510 zq->attributeSetId = odr_oiddup (o, oid);
512 if (!(zq->RPNStructure = rpn_structure (li, o, proto, 0, 512,
513 attr_array, attr_set)))
518 Z_RPNQuery *p_query_rpn (ODR o, oid_proto proto,
524 li.right_sep = "}\"";
525 li.escape_char = '@';
526 li.term_type = Z_Term_general;
528 return p_query_rpn_mk (o, &li, proto, qbuf);
531 Z_AttributesPlusTerm *p_query_scan_mk (struct lex_info *li,
532 ODR o, oid_proto proto,
533 Odr_oid **attributeSetP,
537 oid_value attr_set[512];
541 oid_value topSet = VAL_NONE;
546 if (li->query_look == 'r')
549 topSet = query_oid_getvalbyname (li);
553 if (topSet == VAL_NONE)
554 topSet = p_query_dfset;
555 if (topSet == VAL_NONE)
558 oset.oclass = CLASS_ATTSET;
561 *attributeSetP = odr_oiddup (o, oid_ent_to_oid (&oset, oid));
563 while (li->query_look == 'l')
568 if (num_attr >= max_attr)
571 if (!(cp = strchr (li->lex_buf, '=')) ||
572 (size_t) (cp-li->lex_buf) > li->lex_len)
574 attr_set[num_attr] = query_oid_getvalbyname (li);
577 if (!(cp = strchr (li->lex_buf, '=')))
583 attr_set[num_attr] = attr_set[num_attr-1];
585 attr_set[num_attr] = VAL_NONE;
587 attr_list[2*num_attr] = atoi (li->lex_buf);
588 attr_list[2*num_attr+1] = atoi (cp+1);
594 return rpn_term (li, o, proto, num_attr, attr_list, attr_set);
597 Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,
598 Odr_oid **attributeSetP,
604 li.right_sep = "}\"";
605 li.escape_char = '@';
606 li.term_type = Z_Term_general;
609 return p_query_scan_mk (&li, o, proto, attributeSetP, qbuf);
612 int p_query_attset (const char *arg)
614 p_query_dfset = oid_getvalbyname (arg);
615 return (p_query_dfset == VAL_NONE) ? -1 : 0;