2 * Copyright (c) 1995-1998, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.6 2001-03-07 13:24:40 adam
8 * Member and_not in Z_Operator is kept for backwards compatibility.
9 * Added support for definition of CCL operators in field spec file.
11 * Revision 1.5 2001/02/21 13:46:54 adam
14 * Revision 1.4 1999/12/21 16:25:20 adam
15 * Fixed handling of default/inherited attributes.
17 * Revision 1.3 1999/12/20 15:20:13 adam
18 * Implemented ccl_pquery to convert from CCL tree to prefix query.
20 * Revision 1.2 1999/11/30 13:47:12 adam
21 * Improved installation. Moved header files to include/yaz.
23 * Revision 1.1 1999/06/08 10:10:16 adam
24 * New sub directory zutil. Moved YAZ Compiler to be part of YAZ tree.
26 * Revision 1.22 1999/04/20 09:56:49 adam
27 * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
28 * Modified all encoders/decoders to reflect this change.
30 * Revision 1.21 1998/10/13 16:03:37 adam
31 * Better checking for invalid OID's in p_query_rpn.
33 * Revision 1.20 1998/03/31 15:13:20 adam
34 * Development towards compiled ASN.1.
36 * Revision 1.19 1998/03/05 08:09:03 adam
37 * Minor change to make C++ happy.
39 * Revision 1.18 1998/02/11 11:53:36 adam
40 * Changed code so that it compiles as C++.
42 * Revision 1.17 1997/11/24 11:33:57 adam
43 * Using function odr_nullval() instead of global ODR_NULLVAL when
46 * Revision 1.16 1997/09/29 13:19:00 adam
47 * Added function, oid_ent_to_oid, to replace the function
48 * oid_getoidbyent, which is not thread safe.
50 * Revision 1.15 1997/09/29 07:13:43 adam
51 * Changed type of a few variables to avoid warnings.
53 * Revision 1.14 1997/09/22 12:33:41 adam
54 * Fixed bug introduced by previous commit.
56 * Revision 1.13 1997/09/17 12:10:42 adam
59 * Revision 1.12 1997/09/01 08:54:13 adam
60 * New windows NT/95 port using MSV5.0. Made prefix query handling
61 * thread safe. The function options ignores empty arguments when met.
63 * Revision 1.11 1996/11/11 13:15:29 adam
64 * Added proximity operator.
66 * Revision 1.10 1996/08/12 14:10:35 adam
67 * New function p_query_attset to define default attribute set.
69 * Revision 1.9 1996/03/15 11:03:46 adam
70 * Attribute set can be set globally for a query with the @attrset
71 * operator. The @attr operator has an optional attribute-set specifier
72 * that sets the attribute set locally.
74 * Revision 1.8 1996/01/02 11:46:56 quinn
75 * Changed 'operator' to 'roperator' to avoid C++ conflict.
77 * Revision 1.7 1995/09/29 17:12:36 quinn
80 * Revision 1.6 1995/09/27 15:03:03 quinn
81 * Modified function heads & prototypes.
83 * Revision 1.5 1995/06/15 12:31:02 quinn
84 * *** empty log message ***
86 * Revision 1.4 1995/06/15 07:45:19 quinn
89 * Revision 1.3 1995/06/14 11:06:35 adam
90 * Bug fix: Attributes wasn't interpreted correctly!
92 * Revision 1.2 1995/05/26 08:56:11 adam
93 * New function: p_query_scan.
95 * Revision 1.1 1995/05/22 15:31:49 adam
96 * New function, p_query_rpn, to convert from prefix (ascii) to rpn (asn).
104 #include <yaz/proto.h>
106 #include <yaz/pquery.h>
108 static oid_value p_query_dfset = VAL_NONE;
111 const char *query_buf;
121 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o, oid_proto,
122 int num_attr, int max_attr,
123 int *attr_list, oid_value *attr_set);
125 static enum oid_value query_oid_getvalbyname (struct lex_info *li)
127 enum oid_value value;
130 if (li->lex_len > 31)
132 memcpy (buf, li->lex_buf, li->lex_len);
133 buf[li->lex_len] = '\0';
134 value = oid_getvalbyname (buf);
138 static int compare_term (struct lex_info *li, const char *src, size_t off)
140 size_t len=strlen(src);
142 if (li->lex_len == len+off && !memcmp (li->lex_buf+off, src, len-off))
147 static int query_token (struct lex_info *li)
149 const char *sep_match;
150 const char **qptr = &li->query_buf;
152 while (**qptr == ' ')
157 if ((sep_match = strchr (li->left_sep, **qptr)))
159 int sep_index = sep_match - li->left_sep;
163 while (**qptr && **qptr != li->right_sep[sep_index])
174 while (**qptr && **qptr != ' ')
180 if (li->lex_len >= 1 && li->lex_buf[0] == li->escape_char)
182 if (compare_term (li, "and", 1))
184 if (compare_term (li, "or", 1))
186 if (compare_term (li, "not", 1))
188 if (compare_term (li, "attr", 1))
190 if (compare_term (li, "set", 1))
192 if (compare_term (li, "attrset", 1))
194 if (compare_term (li, "prox", 1))
196 if (compare_term (li, "term", 1))
202 static int lex (struct lex_info *li)
204 return li->query_look = query_token (li);
207 static Z_AttributesPlusTerm *rpn_term (struct lex_info *li, ODR o,
209 int num_attr, int *attr_list,
212 Z_AttributesPlusTerm *zapt;
215 Z_AttributeElement **elements;
217 zapt = (Z_AttributesPlusTerm *)odr_malloc (o, sizeof(*zapt));
218 term_octet = (Odr_oct *)odr_malloc (o, sizeof(*term_octet));
219 term = (Z_Term *)odr_malloc (o, sizeof(*term));
222 elements = (Z_AttributeElement**)odr_nullval();
228 elements = (Z_AttributeElement**)
229 odr_malloc (o, num_attr * sizeof(*elements));
231 attr_tmp = (int *)odr_malloc (o, num_attr * 2 * sizeof(int));
232 memcpy (attr_tmp, attr_list, num_attr * 2 * sizeof(int));
233 for (i = num_attr; --i >= 0; )
236 for (j = i+1; j<num_attr; j++)
237 if (attr_tmp[2*j] == attr_tmp[2*i])
242 (Z_AttributeElement*)odr_malloc (o,sizeof(**elements));
243 elements[k]->attributeType = &attr_tmp[2*i];
244 if (attr_set[i] == VAL_NONE)
245 elements[k]->attributeSet = 0;
251 attrid.proto = PROTO_Z3950;
252 attrid.oclass = CLASS_ATTSET;
253 attrid.value = attr_set[i];
255 elements[k]->attributeSet =
256 odr_oiddup (o, oid_ent_to_oid (&attrid, oid));
258 elements[k]->which = Z_AttributeValue_numeric;
259 elements[k]->value.numeric = &attr_tmp[2*i+1];
265 zapt->attributes = (Z_AttributeList *)
266 odr_malloc (o, sizeof(*zapt->attributes));
267 zapt->attributes->num_attributes = num_attr;
268 zapt->attributes->attributes = elements;
270 zapt->num_attributes = num_attr;
271 zapt->attributeList = elements;
275 term->which = Z_Term_general;
276 term->u.general = term_octet;
277 term_octet->buf = (unsigned char *)odr_malloc (o, li->lex_len);
278 term_octet->size = term_octet->len = li->lex_len;
279 memcpy (term_octet->buf, li->lex_buf, li->lex_len);
283 static Z_Operand *rpn_simple (struct lex_info *li, ODR o, oid_proto proto,
284 int num_attr, int *attr_list,
289 zo = (Z_Operand *)odr_malloc (o, sizeof(*zo));
290 switch (li->query_look)
293 zo->which = Z_Operand_APT;
294 if (!(zo->u.attributesPlusTerm =
295 rpn_term (li, o, proto, num_attr, attr_list, attr_set)))
303 zo->which = Z_Operand_resultSetId;
304 zo->u.resultSetId = (char *)odr_malloc (o, li->lex_len+1);
305 memcpy (zo->u.resultSetId, li->lex_buf, li->lex_len);
306 zo->u.resultSetId[li->lex_len] = '\0';
315 static Z_ProximityOperator *rpn_proximity (struct lex_info *li, ODR o)
317 Z_ProximityOperator *p = (Z_ProximityOperator *)odr_malloc (o, sizeof(*p));
321 if (*li->lex_buf == '1')
323 p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion));
326 else if (*li->lex_buf == '0')
328 p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion));
336 p->distance = (int *)odr_malloc (o, sizeof(*p->distance));
337 *p->distance = atoi (li->lex_buf);
341 p->ordered = (int *)odr_malloc (o, sizeof(*p->ordered));
342 *p->ordered = atoi (li->lex_buf);
346 p->relationType = (int *)odr_malloc (o, sizeof(*p->relationType));
347 *p->relationType = atoi (li->lex_buf);
351 if (*li->lex_buf == 'k')
353 else if (*li->lex_buf == 'p')
356 p->which = atoi (li->lex_buf);
361 p->which = Z_ProximityOperator_known;
362 p->u.known = (int *)odr_malloc (o, sizeof(*p->u.known));
363 *p->u.known = atoi (li->lex_buf);
365 p->proximityUnitCode = (int *)odr_malloc (o, sizeof(*p->proximityUnitCode));
366 *p->proximityUnitCode = atoi (li->lex_buf);
371 static Z_Complex *rpn_complex (struct lex_info *li, ODR o, oid_proto proto,
372 int num_attr, int max_attr,
373 int *attr_list, oid_value *attr_set)
378 zc = (Z_Complex *)odr_malloc (o, sizeof(*zc));
379 zo = (Z_Operator *)odr_malloc (o, sizeof(*zo));
381 switch (li->query_look)
384 zo->which = Z_Operator_and;
385 zo->u.and_not = odr_nullval();
388 zo->which = Z_Operator_or;
389 zo->u.and_not = odr_nullval();
392 zo->which = Z_Operator_and_not;
393 zo->u.and_not = odr_nullval();
396 zo->which = Z_Operator_prox;
397 zo->u.prox = rpn_proximity (li, o);
406 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
410 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
416 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o,
418 int num_attr, int max_attr,
419 int *attr_list, oid_value *attr_set)
424 sz = (Z_RPNStructure *)odr_malloc (o, sizeof(*sz));
425 switch (li->query_look)
431 sz->which = Z_RPNStructure_complex;
432 if (!(sz->u.complex =
433 rpn_complex (li, o, proto, num_attr, max_attr, attr_list,
439 sz->which = Z_RPNStructure_simple;
441 rpn_simple (li, o, proto, num_attr, attr_list,
449 if (num_attr >= max_attr)
451 if (!(cp = strchr (li->lex_buf, '=')) ||
452 (size_t) (cp-li->lex_buf) > li->lex_len)
454 attr_set[num_attr] = query_oid_getvalbyname (li);
455 if (attr_set[num_attr] == VAL_NONE)
459 if (!(cp = strchr (li->lex_buf, '=')))
465 attr_set[num_attr] = attr_set[num_attr-1];
467 attr_set[num_attr] = VAL_NONE;
469 attr_list[2*num_attr] = atoi(li->lex_buf);
470 attr_list[2*num_attr+1] = atoi (cp+1);
474 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
480 if (compare_term (li, "general", 0))
481 li->term_type = Z_Term_general;
482 else if (compare_term (li, "numeric", 0))
483 li->term_type = Z_Term_numeric;
484 else if (compare_term (li, "string", 0))
485 li->term_type = Z_Term_characterString;
486 else if (compare_term (li, "oid", 0))
487 li->term_type = Z_Term_oid;
488 else if (compare_term (li, "datetime", 0))
489 li->term_type = Z_Term_dateTime;
490 else if (compare_term (li, "null", 0))
491 li->term_type = Z_Term_null;
494 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
496 case 0: /* operator/operand expected! */
502 Z_RPNQuery *p_query_rpn_mk (ODR o, struct lex_info *li, oid_proto proto,
506 int attr_array[1024];
507 oid_value attr_set[512];
508 oid_value topSet = VAL_NONE;
512 zq = (Z_RPNQuery *)odr_malloc (o, sizeof(*zq));
514 if (li->query_look == 'r')
517 topSet = query_oid_getvalbyname (li);
518 if (topSet == VAL_NONE)
523 if (topSet == VAL_NONE)
524 topSet = p_query_dfset;
525 if (topSet == VAL_NONE)
528 oset.oclass = CLASS_ATTSET;
531 if (!oid_ent_to_oid (&oset, oid))
533 zq->attributeSetId = odr_oiddup (o, oid);
535 if (!(zq->RPNStructure = rpn_structure (li, o, proto, 0, 512,
536 attr_array, attr_set)))
541 Z_RPNQuery *p_query_rpn (ODR o, oid_proto proto,
547 li.right_sep = "}\"";
548 li.escape_char = '@';
549 li.term_type = Z_Term_general;
551 return p_query_rpn_mk (o, &li, proto, qbuf);
554 Z_AttributesPlusTerm *p_query_scan_mk (struct lex_info *li,
555 ODR o, oid_proto proto,
556 Odr_oid **attributeSetP,
560 oid_value attr_set[512];
564 oid_value topSet = VAL_NONE;
569 if (li->query_look == 'r')
572 topSet = query_oid_getvalbyname (li);
576 if (topSet == VAL_NONE)
577 topSet = p_query_dfset;
578 if (topSet == VAL_NONE)
581 oset.oclass = CLASS_ATTSET;
584 *attributeSetP = odr_oiddup (o, oid_ent_to_oid (&oset, oid));
586 while (li->query_look == 'l')
591 if (num_attr >= max_attr)
594 if (!(cp = strchr (li->lex_buf, '=')) ||
595 (size_t) (cp-li->lex_buf) > li->lex_len)
597 attr_set[num_attr] = query_oid_getvalbyname (li);
600 if (!(cp = strchr (li->lex_buf, '=')))
606 attr_set[num_attr] = attr_set[num_attr-1];
608 attr_set[num_attr] = VAL_NONE;
610 attr_list[2*num_attr] = atoi (li->lex_buf);
611 attr_list[2*num_attr+1] = atoi (cp+1);
617 return rpn_term (li, o, proto, num_attr, attr_list, attr_set);
620 Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,
621 Odr_oid **attributeSetP,
627 li.right_sep = "}\"";
628 li.escape_char = '@';
629 li.term_type = Z_Term_general;
632 return p_query_scan_mk (&li, o, proto, attributeSetP, qbuf);
635 int p_query_attset (const char *arg)
637 p_query_dfset = oid_getvalbyname (arg);
638 return (p_query_dfset == VAL_NONE) ? -1 : 0;