2 * Copyright (c) 1995-2001, Index Data.
3 * See the file LICENSE for details.
6 * Revision 1.7 2001-05-09 23:31:35 adam
7 * String attribute values for PQF. Proper C-backslash escaping for PQF.
9 * Revision 1.6 2001/03/07 13:24:40 adam
10 * Member and_not in Z_Operator is kept for backwards compatibility.
11 * Added support for definition of CCL operators in field spec file.
13 * Revision 1.5 2001/02/21 13:46:54 adam
16 * Revision 1.4 1999/12/21 16:25:20 adam
17 * Fixed handling of default/inherited attributes.
19 * Revision 1.3 1999/12/20 15:20:13 adam
20 * Implemented ccl_pquery to convert from CCL tree to prefix query.
22 * Revision 1.2 1999/11/30 13:47:12 adam
23 * Improved installation. Moved header files to include/yaz.
25 * Revision 1.1 1999/06/08 10:10:16 adam
26 * New sub directory zutil. Moved YAZ Compiler to be part of YAZ tree.
28 * Revision 1.22 1999/04/20 09:56:49 adam
29 * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
30 * Modified all encoders/decoders to reflect this change.
32 * Revision 1.21 1998/10/13 16:03:37 adam
33 * Better checking for invalid OID's in p_query_rpn.
35 * Revision 1.20 1998/03/31 15:13:20 adam
36 * Development towards compiled ASN.1.
38 * Revision 1.19 1998/03/05 08:09:03 adam
39 * Minor change to make C++ happy.
41 * Revision 1.18 1998/02/11 11:53:36 adam
42 * Changed code so that it compiles as C++.
44 * Revision 1.17 1997/11/24 11:33:57 adam
45 * Using function odr_nullval() instead of global ODR_NULLVAL when
48 * Revision 1.16 1997/09/29 13:19:00 adam
49 * Added function, oid_ent_to_oid, to replace the function
50 * oid_getoidbyent, which is not thread safe.
52 * Revision 1.15 1997/09/29 07:13:43 adam
53 * Changed type of a few variables to avoid warnings.
55 * Revision 1.14 1997/09/22 12:33:41 adam
56 * Fixed bug introduced by previous commit.
58 * Revision 1.13 1997/09/17 12:10:42 adam
61 * Revision 1.12 1997/09/01 08:54:13 adam
62 * New windows NT/95 port using MSV5.0. Made prefix query handling
63 * thread safe. The function options ignores empty arguments when met.
65 * Revision 1.11 1996/11/11 13:15:29 adam
66 * Added proximity operator.
68 * Revision 1.10 1996/08/12 14:10:35 adam
69 * New function p_query_attset to define default attribute set.
71 * Revision 1.9 1996/03/15 11:03:46 adam
72 * Attribute set can be set globally for a query with the @attrset
73 * operator. The @attr operator has an optional attribute-set specifier
74 * that sets the attribute set locally.
76 * Revision 1.8 1996/01/02 11:46:56 quinn
77 * Changed 'operator' to 'roperator' to avoid C++ conflict.
79 * Revision 1.7 1995/09/29 17:12:36 quinn
82 * Revision 1.6 1995/09/27 15:03:03 quinn
83 * Modified function heads & prototypes.
85 * Revision 1.5 1995/06/15 12:31:02 quinn
86 * *** empty log message ***
88 * Revision 1.4 1995/06/15 07:45:19 quinn
91 * Revision 1.3 1995/06/14 11:06:35 adam
92 * Bug fix: Attributes wasn't interpreted correctly!
94 * Revision 1.2 1995/05/26 08:56:11 adam
95 * New function: p_query_scan.
97 * Revision 1.1 1995/05/22 15:31:49 adam
98 * New function, p_query_rpn, to convert from prefix (ascii) to rpn (asn).
106 #include <yaz/proto.h>
108 #include <yaz/pquery.h>
110 static oid_value p_query_dfset = VAL_NONE;
113 const char *query_buf;
123 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o, oid_proto,
124 int num_attr, int max_attr,
125 int *attr_list, char **attr_clist,
126 oid_value *attr_set);
128 static enum oid_value query_oid_getvalbyname (struct lex_info *li)
130 enum oid_value value;
133 if (li->lex_len > 31)
135 memcpy (buf, li->lex_buf, li->lex_len);
136 buf[li->lex_len] = '\0';
137 value = oid_getvalbyname (buf);
141 static int compare_term (struct lex_info *li, const char *src, size_t off)
143 size_t len=strlen(src);
145 if (li->lex_len == len+off && !memcmp (li->lex_buf+off, src, len-off))
150 static int query_token (struct lex_info *li)
153 const char *sep_match;
154 const char **qptr = &li->query_buf;
156 while (**qptr == ' ')
161 if ((sep_match = strchr (li->left_sep, **qptr)))
163 sep_char = li->right_sep[sep_match - li->left_sep];
168 while (**qptr && **qptr != sep_char)
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 int escape_string(char *out_buf, const char *in, int len)
212 if (*in == '\\' && len > 0)
238 sscanf (s, "%x", &n);
255 sscanf (s, "%o", &n);
267 return out - out_buf;
270 static int p_query_parse_attr(struct lex_info *li, ODR o,
271 int num_attr, int *attr_list,
272 char **attr_clist, oid_value *attr_set)
275 if (!(cp = strchr (li->lex_buf, '=')) ||
276 (size_t) (cp-li->lex_buf) > li->lex_len)
278 attr_set[num_attr] = query_oid_getvalbyname (li);
279 if (attr_set[num_attr] == VAL_NONE)
283 if (!(cp = strchr (li->lex_buf, '=')))
289 attr_set[num_attr] = attr_set[num_attr-1];
291 attr_set[num_attr] = VAL_NONE;
293 attr_list[2*num_attr] = atoi(li->lex_buf);
295 if (*cp >= '0' && *cp <= '9')
297 attr_list[2*num_attr+1] = atoi (cp);
298 attr_clist[num_attr] = 0;
302 int len = li->lex_len - (cp - li->lex_buf);
303 attr_list[2*num_attr+1] = 0;
304 attr_clist[num_attr] = odr_malloc (o, len+1);
305 len = escape_string(attr_clist[num_attr], cp, len);
306 attr_clist[num_attr][len] = '\0';
311 static Z_AttributesPlusTerm *rpn_term (struct lex_info *li, ODR o,
313 int num_attr, int *attr_list,
314 char **attr_clist, oid_value *attr_set)
316 Z_AttributesPlusTerm *zapt;
319 Z_AttributeElement **elements;
321 zapt = (Z_AttributesPlusTerm *)odr_malloc (o, sizeof(*zapt));
322 term_octet = (Odr_oct *)odr_malloc (o, sizeof(*term_octet));
323 term = (Z_Term *)odr_malloc (o, sizeof(*term));
326 elements = (Z_AttributeElement**)odr_nullval();
332 elements = (Z_AttributeElement**)
333 odr_malloc (o, num_attr * sizeof(*elements));
335 attr_tmp = (int *)odr_malloc (o, num_attr * 2 * sizeof(int));
336 memcpy (attr_tmp, attr_list, num_attr * 2 * sizeof(int));
337 for (i = num_attr; --i >= 0; )
340 for (j = i+1; j<num_attr; j++)
341 if (attr_tmp[2*j] == attr_tmp[2*i])
346 (Z_AttributeElement*)odr_malloc (o,sizeof(**elements));
347 elements[k]->attributeType = &attr_tmp[2*i];
348 if (attr_set[i] == VAL_NONE)
349 elements[k]->attributeSet = 0;
355 attrid.proto = PROTO_Z3950;
356 attrid.oclass = CLASS_ATTSET;
357 attrid.value = attr_set[i];
359 elements[k]->attributeSet =
360 odr_oiddup (o, oid_ent_to_oid (&attrid, oid));
364 elements[k]->which = Z_AttributeValue_complex;
365 elements[k]->value.complex = (Z_ComplexAttribute *)
366 odr_malloc (o, sizeof(Z_ComplexAttribute));
367 elements[k]->value.complex->num_list = 1;
368 elements[k]->value.complex->list =
369 (Z_StringOrNumeric **)
370 odr_malloc (o, 1 * sizeof(Z_StringOrNumeric *));
371 elements[k]->value.complex->list[0] =
372 (Z_StringOrNumeric *)
373 odr_malloc (o, sizeof(Z_StringOrNumeric));
374 elements[k]->value.complex->list[0]->which =
375 Z_StringOrNumeric_string;
376 elements[k]->value.complex->list[0]->u.string =
378 elements[k]->value.complex->semanticAction = (int **)
380 elements[k]->value.complex->num_semanticAction = 0;
384 elements[k]->which = Z_AttributeValue_numeric;
385 elements[k]->value.numeric = &attr_tmp[2*i+1];
392 zapt->attributes = (Z_AttributeList *)
393 odr_malloc (o, sizeof(*zapt->attributes));
394 zapt->attributes->num_attributes = num_attr;
395 zapt->attributes->attributes = elements;
397 zapt->num_attributes = num_attr;
398 zapt->attributeList = elements;
402 term->which = Z_Term_general;
403 term->u.general = term_octet;
404 term_octet->buf = (unsigned char *)odr_malloc (o, li->lex_len);
405 term_octet->size = term_octet->len =
406 escape_string (term_octet->buf, li->lex_buf, li->lex_len);
410 static Z_Operand *rpn_simple (struct lex_info *li, ODR o, oid_proto proto,
411 int num_attr, int *attr_list, char **attr_clist,
416 zo = (Z_Operand *)odr_malloc (o, sizeof(*zo));
417 switch (li->query_look)
420 zo->which = Z_Operand_APT;
421 if (!(zo->u.attributesPlusTerm =
422 rpn_term (li, o, proto, num_attr, attr_list, attr_clist,
431 zo->which = Z_Operand_resultSetId;
432 zo->u.resultSetId = (char *)odr_malloc (o, li->lex_len+1);
433 memcpy (zo->u.resultSetId, li->lex_buf, li->lex_len);
434 zo->u.resultSetId[li->lex_len] = '\0';
443 static Z_ProximityOperator *rpn_proximity (struct lex_info *li, ODR o)
445 Z_ProximityOperator *p = (Z_ProximityOperator *)odr_malloc (o, sizeof(*p));
449 if (*li->lex_buf == '1')
451 p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion));
454 else if (*li->lex_buf == '0')
456 p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion));
464 p->distance = (int *)odr_malloc (o, sizeof(*p->distance));
465 *p->distance = atoi (li->lex_buf);
469 p->ordered = (int *)odr_malloc (o, sizeof(*p->ordered));
470 *p->ordered = atoi (li->lex_buf);
474 p->relationType = (int *)odr_malloc (o, sizeof(*p->relationType));
475 *p->relationType = atoi (li->lex_buf);
479 if (*li->lex_buf == 'k')
481 else if (*li->lex_buf == 'p')
484 p->which = atoi (li->lex_buf);
489 p->which = Z_ProximityOperator_known;
490 p->u.known = (int *)odr_malloc (o, sizeof(*p->u.known));
491 *p->u.known = atoi (li->lex_buf);
493 p->proximityUnitCode = (int *)odr_malloc (o, sizeof(*p->proximityUnitCode));
494 *p->proximityUnitCode = atoi (li->lex_buf);
499 static Z_Complex *rpn_complex (struct lex_info *li, ODR o, oid_proto proto,
500 int num_attr, int max_attr,
501 int *attr_list, char **attr_clist,
507 zc = (Z_Complex *)odr_malloc (o, sizeof(*zc));
508 zo = (Z_Operator *)odr_malloc (o, sizeof(*zo));
510 switch (li->query_look)
513 zo->which = Z_Operator_and;
514 zo->u.and_not = odr_nullval();
517 zo->which = Z_Operator_or;
518 zo->u.and_not = odr_nullval();
521 zo->which = Z_Operator_and_not;
522 zo->u.and_not = odr_nullval();
525 zo->which = Z_Operator_prox;
526 zo->u.prox = rpn_proximity (li, o);
535 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
536 attr_clist, attr_set)))
539 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
540 attr_clist, attr_set)))
545 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o,
547 int num_attr, int max_attr,
554 sz = (Z_RPNStructure *)odr_malloc (o, sizeof(*sz));
555 switch (li->query_look)
561 sz->which = Z_RPNStructure_complex;
562 if (!(sz->u.complex =
563 rpn_complex (li, o, proto, num_attr, max_attr, attr_list,
564 attr_clist, attr_set)))
569 sz->which = Z_RPNStructure_simple;
571 rpn_simple (li, o, proto, num_attr, attr_list,
572 attr_clist, attr_set)))
579 if (num_attr >= max_attr)
581 p_query_parse_attr(li, o, num_attr, attr_list, attr_clist, attr_set);
585 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
586 attr_clist, attr_set);
591 if (compare_term (li, "general", 0))
592 li->term_type = Z_Term_general;
593 else if (compare_term (li, "numeric", 0))
594 li->term_type = Z_Term_numeric;
595 else if (compare_term (li, "string", 0))
596 li->term_type = Z_Term_characterString;
597 else if (compare_term (li, "oid", 0))
598 li->term_type = Z_Term_oid;
599 else if (compare_term (li, "datetime", 0))
600 li->term_type = Z_Term_dateTime;
601 else if (compare_term (li, "null", 0))
602 li->term_type = Z_Term_null;
605 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
606 attr_clist, attr_set);
607 case 0: /* operator/operand expected! */
613 Z_RPNQuery *p_query_rpn_mk (ODR o, struct lex_info *li, oid_proto proto,
617 int attr_array[1024];
618 char *attr_clist[512];
619 oid_value attr_set[512];
620 oid_value topSet = VAL_NONE;
624 zq = (Z_RPNQuery *)odr_malloc (o, sizeof(*zq));
626 if (li->query_look == 'r')
629 topSet = query_oid_getvalbyname (li);
630 if (topSet == VAL_NONE)
635 if (topSet == VAL_NONE)
636 topSet = p_query_dfset;
637 if (topSet == VAL_NONE)
640 oset.oclass = CLASS_ATTSET;
643 if (!oid_ent_to_oid (&oset, oid))
645 zq->attributeSetId = odr_oiddup (o, oid);
647 if (!(zq->RPNStructure = rpn_structure (li, o, proto, 0, 512,
648 attr_array, attr_clist, attr_set)))
653 Z_RPNQuery *p_query_rpn (ODR o, oid_proto proto,
659 li.right_sep = "}\"";
660 li.escape_char = '@';
661 li.term_type = Z_Term_general;
663 return p_query_rpn_mk (o, &li, proto, qbuf);
667 Z_AttributesPlusTerm *p_query_scan_mk (struct lex_info *li,
668 ODR o, oid_proto proto,
669 Odr_oid **attributeSetP,
673 char *attr_clist[512];
674 oid_value attr_set[512];
677 oid_value topSet = VAL_NONE;
682 if (li->query_look == 'r')
685 topSet = query_oid_getvalbyname (li);
689 if (topSet == VAL_NONE)
690 topSet = p_query_dfset;
691 if (topSet == VAL_NONE)
694 oset.oclass = CLASS_ATTSET;
697 *attributeSetP = odr_oiddup (o, oid_ent_to_oid (&oset, oid));
699 while (li->query_look == 'l')
704 if (num_attr >= max_attr)
706 p_query_parse_attr(li, o, num_attr, attr_list, attr_clist, attr_set);
712 return rpn_term (li, o, proto, num_attr, attr_list, attr_clist, attr_set);
715 Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,
716 Odr_oid **attributeSetP,
722 li.right_sep = "}\"";
723 li.escape_char = '@';
724 li.term_type = Z_Term_general;
727 return p_query_scan_mk (&li, o, proto, attributeSetP, qbuf);
730 int p_query_attset (const char *arg)
732 p_query_dfset = oid_getvalbyname (arg);
733 return (p_query_dfset == VAL_NONE) ? -1 : 0;