2 * Copyright (c) 1995-2001, Index Data.
3 * See the file LICENSE for details.
6 * Revision 1.8 2001-07-19 19:14:53 adam
9 * Revision 1.7 2001/05/09 23:31:35 adam
10 * String attribute values for PQF. Proper C-backslash escaping for PQF.
12 * Revision 1.6 2001/03/07 13:24:40 adam
13 * Member and_not in Z_Operator is kept for backwards compatibility.
14 * Added support for definition of CCL operators in field spec file.
16 * Revision 1.5 2001/02/21 13:46:54 adam
19 * Revision 1.4 1999/12/21 16:25:20 adam
20 * Fixed handling of default/inherited attributes.
22 * Revision 1.3 1999/12/20 15:20:13 adam
23 * Implemented ccl_pquery to convert from CCL tree to prefix query.
25 * Revision 1.2 1999/11/30 13:47:12 adam
26 * Improved installation. Moved header files to include/yaz.
28 * Revision 1.1 1999/06/08 10:10:16 adam
29 * New sub directory zutil. Moved YAZ Compiler to be part of YAZ tree.
31 * Revision 1.22 1999/04/20 09:56:49 adam
32 * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
33 * Modified all encoders/decoders to reflect this change.
35 * Revision 1.21 1998/10/13 16:03:37 adam
36 * Better checking for invalid OID's in p_query_rpn.
38 * Revision 1.20 1998/03/31 15:13:20 adam
39 * Development towards compiled ASN.1.
41 * Revision 1.19 1998/03/05 08:09:03 adam
42 * Minor change to make C++ happy.
44 * Revision 1.18 1998/02/11 11:53:36 adam
45 * Changed code so that it compiles as C++.
47 * Revision 1.17 1997/11/24 11:33:57 adam
48 * Using function odr_nullval() instead of global ODR_NULLVAL when
51 * Revision 1.16 1997/09/29 13:19:00 adam
52 * Added function, oid_ent_to_oid, to replace the function
53 * oid_getoidbyent, which is not thread safe.
55 * Revision 1.15 1997/09/29 07:13:43 adam
56 * Changed type of a few variables to avoid warnings.
58 * Revision 1.14 1997/09/22 12:33:41 adam
59 * Fixed bug introduced by previous commit.
61 * Revision 1.13 1997/09/17 12:10:42 adam
64 * Revision 1.12 1997/09/01 08:54:13 adam
65 * New windows NT/95 port using MSV5.0. Made prefix query handling
66 * thread safe. The function options ignores empty arguments when met.
68 * Revision 1.11 1996/11/11 13:15:29 adam
69 * Added proximity operator.
71 * Revision 1.10 1996/08/12 14:10:35 adam
72 * New function p_query_attset to define default attribute set.
74 * Revision 1.9 1996/03/15 11:03:46 adam
75 * Attribute set can be set globally for a query with the @attrset
76 * operator. The @attr operator has an optional attribute-set specifier
77 * that sets the attribute set locally.
79 * Revision 1.8 1996/01/02 11:46:56 quinn
80 * Changed 'operator' to 'roperator' to avoid C++ conflict.
82 * Revision 1.7 1995/09/29 17:12:36 quinn
85 * Revision 1.6 1995/09/27 15:03:03 quinn
86 * Modified function heads & prototypes.
88 * Revision 1.5 1995/06/15 12:31:02 quinn
89 * *** empty log message ***
91 * Revision 1.4 1995/06/15 07:45:19 quinn
94 * Revision 1.3 1995/06/14 11:06:35 adam
95 * Bug fix: Attributes wasn't interpreted correctly!
97 * Revision 1.2 1995/05/26 08:56:11 adam
98 * New function: p_query_scan.
100 * Revision 1.1 1995/05/22 15:31:49 adam
101 * New function, p_query_rpn, to convert from prefix (ascii) to rpn (asn).
109 #include <yaz/proto.h>
111 #include <yaz/pquery.h>
113 static oid_value p_query_dfset = VAL_NONE;
116 const char *query_buf;
126 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o, oid_proto,
127 int num_attr, int max_attr,
128 int *attr_list, char **attr_clist,
129 oid_value *attr_set);
131 static enum oid_value query_oid_getvalbyname (struct lex_info *li)
133 enum oid_value value;
136 if (li->lex_len > 31)
138 memcpy (buf, li->lex_buf, li->lex_len);
139 buf[li->lex_len] = '\0';
140 value = oid_getvalbyname (buf);
144 static int compare_term (struct lex_info *li, const char *src, size_t off)
146 size_t len=strlen(src);
148 if (li->lex_len == len+off && !memcmp (li->lex_buf+off, src, len-off))
153 static int query_token (struct lex_info *li)
156 const char *sep_match;
157 const char **qptr = &li->query_buf;
159 while (**qptr == ' ')
164 if ((sep_match = strchr (li->left_sep, **qptr)))
166 sep_char = li->right_sep[sep_match - li->left_sep];
171 while (**qptr && **qptr != sep_char)
183 if (li->lex_len >= 1 && li->lex_buf[0] == li->escape_char)
185 if (compare_term (li, "and", 1))
187 if (compare_term (li, "or", 1))
189 if (compare_term (li, "not", 1))
191 if (compare_term (li, "attr", 1))
193 if (compare_term (li, "set", 1))
195 if (compare_term (li, "attrset", 1))
197 if (compare_term (li, "prox", 1))
199 if (compare_term (li, "term", 1))
205 static int lex (struct lex_info *li)
207 return li->query_look = query_token (li);
210 static int escape_string(char *out_buf, const char *in, int len)
215 if (*in == '\\' && len > 0)
241 sscanf (s, "%x", &n);
258 sscanf (s, "%o", &n);
270 return out - out_buf;
273 static int p_query_parse_attr(struct lex_info *li, ODR o,
274 int num_attr, int *attr_list,
275 char **attr_clist, oid_value *attr_set)
278 if (!(cp = strchr (li->lex_buf, '=')) ||
279 (size_t) (cp-li->lex_buf) > li->lex_len)
281 attr_set[num_attr] = query_oid_getvalbyname (li);
282 if (attr_set[num_attr] == VAL_NONE)
286 if (!(cp = strchr (li->lex_buf, '=')))
292 attr_set[num_attr] = attr_set[num_attr-1];
294 attr_set[num_attr] = VAL_NONE;
296 attr_list[2*num_attr] = atoi(li->lex_buf);
298 if (*cp >= '0' && *cp <= '9')
300 attr_list[2*num_attr+1] = atoi (cp);
301 attr_clist[num_attr] = 0;
305 int len = li->lex_len - (cp - li->lex_buf);
306 attr_list[2*num_attr+1] = 0;
307 attr_clist[num_attr] = (char *) odr_malloc (o, len+1);
308 len = escape_string(attr_clist[num_attr], cp, len);
309 attr_clist[num_attr][len] = '\0';
314 static Z_AttributesPlusTerm *rpn_term (struct lex_info *li, ODR o,
316 int num_attr, int *attr_list,
317 char **attr_clist, oid_value *attr_set)
319 Z_AttributesPlusTerm *zapt;
322 Z_AttributeElement **elements;
324 zapt = (Z_AttributesPlusTerm *)odr_malloc (o, sizeof(*zapt));
325 term_octet = (Odr_oct *)odr_malloc (o, sizeof(*term_octet));
326 term = (Z_Term *)odr_malloc (o, sizeof(*term));
329 elements = (Z_AttributeElement**)odr_nullval();
335 elements = (Z_AttributeElement**)
336 odr_malloc (o, num_attr * sizeof(*elements));
338 attr_tmp = (int *)odr_malloc (o, num_attr * 2 * sizeof(int));
339 memcpy (attr_tmp, attr_list, num_attr * 2 * sizeof(int));
340 for (i = num_attr; --i >= 0; )
343 for (j = i+1; j<num_attr; j++)
344 if (attr_tmp[2*j] == attr_tmp[2*i])
349 (Z_AttributeElement*)odr_malloc (o,sizeof(**elements));
350 elements[k]->attributeType = &attr_tmp[2*i];
351 if (attr_set[i] == VAL_NONE)
352 elements[k]->attributeSet = 0;
358 attrid.proto = PROTO_Z3950;
359 attrid.oclass = CLASS_ATTSET;
360 attrid.value = attr_set[i];
362 elements[k]->attributeSet =
363 odr_oiddup (o, oid_ent_to_oid (&attrid, oid));
367 elements[k]->which = Z_AttributeValue_complex;
368 elements[k]->value.complex = (Z_ComplexAttribute *)
369 odr_malloc (o, sizeof(Z_ComplexAttribute));
370 elements[k]->value.complex->num_list = 1;
371 elements[k]->value.complex->list =
372 (Z_StringOrNumeric **)
373 odr_malloc (o, 1 * sizeof(Z_StringOrNumeric *));
374 elements[k]->value.complex->list[0] =
375 (Z_StringOrNumeric *)
376 odr_malloc (o, sizeof(Z_StringOrNumeric));
377 elements[k]->value.complex->list[0]->which =
378 Z_StringOrNumeric_string;
379 elements[k]->value.complex->list[0]->u.string =
381 elements[k]->value.complex->semanticAction = (int **)
383 elements[k]->value.complex->num_semanticAction = 0;
387 elements[k]->which = Z_AttributeValue_numeric;
388 elements[k]->value.numeric = &attr_tmp[2*i+1];
395 zapt->attributes = (Z_AttributeList *)
396 odr_malloc (o, sizeof(*zapt->attributes));
397 zapt->attributes->num_attributes = num_attr;
398 zapt->attributes->attributes = elements;
400 zapt->num_attributes = num_attr;
401 zapt->attributeList = elements;
405 term->which = Z_Term_general;
406 term->u.general = term_octet;
407 term_octet->buf = (unsigned char *)odr_malloc (o, li->lex_len);
408 term_octet->size = term_octet->len =
409 escape_string ((char *) (term_octet->buf), li->lex_buf, li->lex_len);
413 static Z_Operand *rpn_simple (struct lex_info *li, ODR o, oid_proto proto,
414 int num_attr, int *attr_list, char **attr_clist,
419 zo = (Z_Operand *)odr_malloc (o, sizeof(*zo));
420 switch (li->query_look)
423 zo->which = Z_Operand_APT;
424 if (!(zo->u.attributesPlusTerm =
425 rpn_term (li, o, proto, num_attr, attr_list, attr_clist,
434 zo->which = Z_Operand_resultSetId;
435 zo->u.resultSetId = (char *)odr_malloc (o, li->lex_len+1);
436 memcpy (zo->u.resultSetId, li->lex_buf, li->lex_len);
437 zo->u.resultSetId[li->lex_len] = '\0';
446 static Z_ProximityOperator *rpn_proximity (struct lex_info *li, ODR o)
448 Z_ProximityOperator *p = (Z_ProximityOperator *)odr_malloc (o, sizeof(*p));
452 if (*li->lex_buf == '1')
454 p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion));
457 else if (*li->lex_buf == '0')
459 p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion));
467 p->distance = (int *)odr_malloc (o, sizeof(*p->distance));
468 *p->distance = atoi (li->lex_buf);
472 p->ordered = (int *)odr_malloc (o, sizeof(*p->ordered));
473 *p->ordered = atoi (li->lex_buf);
477 p->relationType = (int *)odr_malloc (o, sizeof(*p->relationType));
478 *p->relationType = atoi (li->lex_buf);
482 if (*li->lex_buf == 'k')
484 else if (*li->lex_buf == 'p')
487 p->which = atoi (li->lex_buf);
492 p->which = Z_ProximityOperator_known;
493 p->u.known = (int *)odr_malloc (o, sizeof(*p->u.known));
494 *p->u.known = atoi (li->lex_buf);
496 p->proximityUnitCode = (int *)odr_malloc (o, sizeof(*p->proximityUnitCode));
497 *p->proximityUnitCode = atoi (li->lex_buf);
502 static Z_Complex *rpn_complex (struct lex_info *li, ODR o, oid_proto proto,
503 int num_attr, int max_attr,
504 int *attr_list, char **attr_clist,
510 zc = (Z_Complex *)odr_malloc (o, sizeof(*zc));
511 zo = (Z_Operator *)odr_malloc (o, sizeof(*zo));
513 switch (li->query_look)
516 zo->which = Z_Operator_and;
517 zo->u.and_not = odr_nullval();
520 zo->which = Z_Operator_or;
521 zo->u.and_not = odr_nullval();
524 zo->which = Z_Operator_and_not;
525 zo->u.and_not = odr_nullval();
528 zo->which = Z_Operator_prox;
529 zo->u.prox = rpn_proximity (li, o);
538 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
539 attr_clist, attr_set)))
542 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
543 attr_clist, attr_set)))
548 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o,
550 int num_attr, int max_attr,
557 sz = (Z_RPNStructure *)odr_malloc (o, sizeof(*sz));
558 switch (li->query_look)
564 sz->which = Z_RPNStructure_complex;
565 if (!(sz->u.complex =
566 rpn_complex (li, o, proto, num_attr, max_attr, attr_list,
567 attr_clist, attr_set)))
572 sz->which = Z_RPNStructure_simple;
574 rpn_simple (li, o, proto, num_attr, attr_list,
575 attr_clist, attr_set)))
582 if (num_attr >= max_attr)
584 p_query_parse_attr(li, o, num_attr, attr_list, attr_clist, attr_set);
588 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
589 attr_clist, attr_set);
594 if (compare_term (li, "general", 0))
595 li->term_type = Z_Term_general;
596 else if (compare_term (li, "numeric", 0))
597 li->term_type = Z_Term_numeric;
598 else if (compare_term (li, "string", 0))
599 li->term_type = Z_Term_characterString;
600 else if (compare_term (li, "oid", 0))
601 li->term_type = Z_Term_oid;
602 else if (compare_term (li, "datetime", 0))
603 li->term_type = Z_Term_dateTime;
604 else if (compare_term (li, "null", 0))
605 li->term_type = Z_Term_null;
608 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
609 attr_clist, attr_set);
610 case 0: /* operator/operand expected! */
616 Z_RPNQuery *p_query_rpn_mk (ODR o, struct lex_info *li, oid_proto proto,
620 int attr_array[1024];
621 char *attr_clist[512];
622 oid_value attr_set[512];
623 oid_value topSet = VAL_NONE;
627 zq = (Z_RPNQuery *)odr_malloc (o, sizeof(*zq));
629 if (li->query_look == 'r')
632 topSet = query_oid_getvalbyname (li);
633 if (topSet == VAL_NONE)
638 if (topSet == VAL_NONE)
639 topSet = p_query_dfset;
640 if (topSet == VAL_NONE)
643 oset.oclass = CLASS_ATTSET;
646 if (!oid_ent_to_oid (&oset, oid))
648 zq->attributeSetId = odr_oiddup (o, oid);
650 if (!(zq->RPNStructure = rpn_structure (li, o, proto, 0, 512,
651 attr_array, attr_clist, attr_set)))
656 Z_RPNQuery *p_query_rpn (ODR o, oid_proto proto,
662 li.right_sep = "}\"";
663 li.escape_char = '@';
664 li.term_type = Z_Term_general;
666 return p_query_rpn_mk (o, &li, proto, qbuf);
670 Z_AttributesPlusTerm *p_query_scan_mk (struct lex_info *li,
671 ODR o, oid_proto proto,
672 Odr_oid **attributeSetP,
676 char *attr_clist[512];
677 oid_value attr_set[512];
680 oid_value topSet = VAL_NONE;
685 if (li->query_look == 'r')
688 topSet = query_oid_getvalbyname (li);
692 if (topSet == VAL_NONE)
693 topSet = p_query_dfset;
694 if (topSet == VAL_NONE)
697 oset.oclass = CLASS_ATTSET;
700 *attributeSetP = odr_oiddup (o, oid_ent_to_oid (&oset, oid));
702 while (li->query_look == 'l')
707 if (num_attr >= max_attr)
709 p_query_parse_attr(li, o, num_attr, attr_list, attr_clist, attr_set);
715 return rpn_term (li, o, proto, num_attr, attr_list, attr_clist, attr_set);
718 Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,
719 Odr_oid **attributeSetP,
725 li.right_sep = "}\"";
726 li.escape_char = '@';
727 li.term_type = Z_Term_general;
730 return p_query_scan_mk (&li, o, proto, attributeSetP, qbuf);
733 int p_query_attset (const char *arg)
735 p_query_dfset = oid_getvalbyname (arg);
736 return (p_query_dfset == VAL_NONE) ? -1 : 0;