2 * Copyright (c) 1995-1998, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.3 1999-12-20 15:20:13 adam
8 * Implemented ccl_pquery to convert from CCL tree to prefix query.
10 * Revision 1.2 1999/11/30 13:47:12 adam
11 * Improved installation. Moved header files to include/yaz.
13 * Revision 1.1 1999/06/08 10:10:16 adam
14 * New sub directory zutil. Moved YAZ Compiler to be part of YAZ tree.
16 * Revision 1.22 1999/04/20 09:56:49 adam
17 * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
18 * Modified all encoders/decoders to reflect this change.
20 * Revision 1.21 1998/10/13 16:03:37 adam
21 * Better checking for invalid OID's in p_query_rpn.
23 * Revision 1.20 1998/03/31 15:13:20 adam
24 * Development towards compiled ASN.1.
26 * Revision 1.19 1998/03/05 08:09:03 adam
27 * Minor change to make C++ happy.
29 * Revision 1.18 1998/02/11 11:53:36 adam
30 * Changed code so that it compiles as C++.
32 * Revision 1.17 1997/11/24 11:33:57 adam
33 * Using function odr_nullval() instead of global ODR_NULLVAL when
36 * Revision 1.16 1997/09/29 13:19:00 adam
37 * Added function, oid_ent_to_oid, to replace the function
38 * oid_getoidbyent, which is not thread safe.
40 * Revision 1.15 1997/09/29 07:13:43 adam
41 * Changed type of a few variables to avoid warnings.
43 * Revision 1.14 1997/09/22 12:33:41 adam
44 * Fixed bug introduced by previous commit.
46 * Revision 1.13 1997/09/17 12:10:42 adam
49 * Revision 1.12 1997/09/01 08:54:13 adam
50 * New windows NT/95 port using MSV5.0. Made prefix query handling
51 * thread safe. The function options ignores empty arguments when met.
53 * Revision 1.11 1996/11/11 13:15:29 adam
54 * Added proximity operator.
56 * Revision 1.10 1996/08/12 14:10:35 adam
57 * New function p_query_attset to define default attribute set.
59 * Revision 1.9 1996/03/15 11:03:46 adam
60 * Attribute set can be set globally for a query with the @attrset
61 * operator. The @attr operator has an optional attribute-set specifier
62 * that sets the attribute set locally.
64 * Revision 1.8 1996/01/02 11:46:56 quinn
65 * Changed 'operator' to 'roperator' to avoid C++ conflict.
67 * Revision 1.7 1995/09/29 17:12:36 quinn
70 * Revision 1.6 1995/09/27 15:03:03 quinn
71 * Modified function heads & prototypes.
73 * Revision 1.5 1995/06/15 12:31:02 quinn
74 * *** empty log message ***
76 * Revision 1.4 1995/06/15 07:45:19 quinn
79 * Revision 1.3 1995/06/14 11:06:35 adam
80 * Bug fix: Attributes wasn't interpreted correctly!
82 * Revision 1.2 1995/05/26 08:56:11 adam
83 * New function: p_query_scan.
85 * Revision 1.1 1995/05/22 15:31:49 adam
86 * New function, p_query_rpn, to convert from prefix (ascii) to rpn (asn).
94 #include <yaz/proto.h>
96 #include <yaz/pquery.h>
98 static oid_value p_query_dfset = VAL_NONE;
101 const char *query_buf;
111 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o, oid_proto,
112 int num_attr, int max_attr,
113 int *attr_list, oid_value *attr_set);
115 static enum oid_value query_oid_getvalbyname (struct lex_info *li)
117 enum oid_value value;
120 if (li->lex_len > 31)
122 memcpy (buf, li->lex_buf, li->lex_len);
123 buf[li->lex_len] = '\0';
124 value = oid_getvalbyname (buf);
128 static int compare_term (struct lex_info *li, const char *src, size_t off)
130 size_t len=strlen(src);
132 if (li->lex_len == len+off && !memcmp (li->lex_buf+off, src, len-off))
137 static int query_token (struct lex_info *li)
139 const char *sep_match;
140 const char **qptr = &li->query_buf;
142 while (**qptr == ' ')
147 if ((sep_match = strchr (li->left_sep, **qptr)))
149 int sep_index = sep_match - li->left_sep;
153 while (**qptr && **qptr != li->right_sep[sep_index])
164 while (**qptr && **qptr != ' ')
170 if (li->lex_len >= 1 && li->lex_buf[0] == li->escape_char)
172 if (compare_term (li, "and", 1))
174 if (compare_term (li, "or", 1))
176 if (compare_term (li, "not", 1))
178 if (compare_term (li, "attr", 1))
180 if (compare_term (li, "set", 1))
182 if (compare_term (li, "attrset", 1))
184 if (compare_term (li, "prox", 1))
186 if (compare_term (li, "term", 1))
192 static int lex (struct lex_info *li)
194 return li->query_look = query_token (li);
197 static Z_AttributesPlusTerm *rpn_term (struct lex_info *li, ODR o,
199 int num_attr, int *attr_list,
202 Z_AttributesPlusTerm *zapt;
205 Z_AttributeElement **elements;
207 zapt = (Z_AttributesPlusTerm *)odr_malloc (o, sizeof(*zapt));
208 term_octet = (Odr_oct *)odr_malloc (o, sizeof(*term_octet));
209 term = (Z_Term *)odr_malloc (o, sizeof(*term));
212 elements = (Z_AttributeElement**)odr_nullval();
218 elements = (Z_AttributeElement**)
219 odr_malloc (o, num_attr * sizeof(*elements));
221 attr_tmp = (int *)odr_malloc (o, num_attr * 2 * sizeof(int));
222 memcpy (attr_tmp, attr_list, num_attr * 2 * sizeof(int));
223 for (i = 0; i < num_attr; i++)
226 (Z_AttributeElement*)odr_malloc (o,sizeof(**elements));
227 elements[i]->attributeType = &attr_tmp[2*i];
228 if (attr_set[i] == VAL_NONE)
229 elements[i]->attributeSet = 0;
235 attrid.proto = PROTO_Z3950;
236 attrid.oclass = CLASS_ATTSET;
237 attrid.value = attr_set[i];
239 elements[i]->attributeSet =
240 odr_oiddup (o, oid_ent_to_oid (&attrid, oid));
242 elements[i]->which = Z_AttributeValue_numeric;
243 elements[i]->value.numeric = &attr_tmp[2*i+1];
247 zapt->attributes = (Z_AttributeList *)
248 odr_malloc (o, sizeof(*zapt->attributes));
249 zapt->attributes->num_attributes = num_attr;
250 zapt->attributes->attributes = elements;
252 zapt->num_attributes = num_attr;
253 zapt->attributeList = elements;
257 term->which = Z_Term_general;
258 term->u.general = term_octet;
259 term_octet->buf = (unsigned char *)odr_malloc (o, li->lex_len);
260 term_octet->size = term_octet->len = li->lex_len;
261 memcpy (term_octet->buf, li->lex_buf, li->lex_len);
265 static Z_Operand *rpn_simple (struct lex_info *li, ODR o, oid_proto proto,
266 int num_attr, int *attr_list,
271 zo = (Z_Operand *)odr_malloc (o, sizeof(*zo));
272 switch (li->query_look)
275 zo->which = Z_Operand_APT;
276 if (!(zo->u.attributesPlusTerm =
277 rpn_term (li, o, proto, num_attr, attr_list, attr_set)))
285 zo->which = Z_Operand_resultSetId;
286 zo->u.resultSetId = (char *)odr_malloc (o, li->lex_len+1);
287 memcpy (zo->u.resultSetId, li->lex_buf, li->lex_len);
288 zo->u.resultSetId[li->lex_len] = '\0';
297 static Z_ProximityOperator *rpn_proximity (struct lex_info *li, ODR o)
299 Z_ProximityOperator *p = (Z_ProximityOperator *)odr_malloc (o, sizeof(*p));
303 if (*li->lex_buf == '1')
305 p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion));
308 else if (*li->lex_buf == '0')
310 p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion));
318 p->distance = (int *)odr_malloc (o, sizeof(*p->distance));
319 *p->distance = atoi (li->lex_buf);
323 p->ordered = (int *)odr_malloc (o, sizeof(*p->ordered));
324 *p->ordered = atoi (li->lex_buf);
328 p->relationType = (int *)odr_malloc (o, sizeof(*p->relationType));
329 *p->relationType = atoi (li->lex_buf);
333 if (*li->lex_buf == 'k')
335 else if (*li->lex_buf == 'p')
338 p->which = atoi (li->lex_buf);
343 p->which = Z_ProximityOperator_known;
344 p->u.known = (int *)odr_malloc (o, sizeof(*p->u.known));
345 *p->u.known = atoi (li->lex_buf);
347 p->proximityUnitCode = (int *)odr_malloc (o, sizeof(*p->proximityUnitCode));
348 *p->proximityUnitCode = atoi (li->lex_buf);
353 static Z_Complex *rpn_complex (struct lex_info *li, ODR o, oid_proto proto,
354 int num_attr, int max_attr,
355 int *attr_list, oid_value *attr_set)
360 zc = (Z_Complex *)odr_malloc (o, sizeof(*zc));
361 zo = (Z_Operator *)odr_malloc (o, sizeof(*zo));
363 switch (li->query_look)
366 zo->which = Z_Operator_and;
367 zo->u.and = odr_nullval();
370 zo->which = Z_Operator_or;
371 zo->u.and = odr_nullval();
374 zo->which = Z_Operator_and_not;
375 zo->u.and = odr_nullval();
378 zo->which = Z_Operator_prox;
379 zo->u.prox = rpn_proximity (li, o);
388 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
392 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
398 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o,
400 int num_attr, int max_attr,
401 int *attr_list, oid_value *attr_set)
407 sz = (Z_RPNStructure *)odr_malloc (o, sizeof(*sz));
408 switch (li->query_look)
414 sz->which = Z_RPNStructure_complex;
415 if (!(sz->u.complex =
416 rpn_complex (li, o, proto, num_attr, max_attr, attr_list,
422 sz->which = Z_RPNStructure_simple;
424 rpn_simple (li, o, proto, num_attr, attr_list,
432 if (num_attr >= max_attr)
434 if (!(cp = strchr (li->lex_buf, '=')) ||
435 (size_t) (cp-li->lex_buf) > li->lex_len)
437 attr_set[num_attr] = query_oid_getvalbyname (li);
438 if (attr_set[num_attr] == VAL_NONE)
442 if (!(cp = strchr (li->lex_buf, '=')))
448 attr_set[num_attr] = attr_set[num_attr-1];
450 attr_set[num_attr] = VAL_NONE;
452 attrtype = atoi (li->lex_buf);
453 for (i = 0; i < num_attr; i++)
454 if (attrtype == attr_list[2*i])
456 attr_list[2*i+1] = atoi (cp+1);
461 attr_list[2*num_attr] = attrtype;
462 attr_list[2*num_attr+1] = atoi (cp+1);
467 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
473 if (compare_term (li, "general", 0))
474 li->term_type = Z_Term_general;
475 else if (compare_term (li, "numeric", 0))
476 li->term_type = Z_Term_numeric;
477 else if (compare_term (li, "string", 0))
478 li->term_type = Z_Term_characterString;
479 else if (compare_term (li, "oid", 0))
480 li->term_type = Z_Term_oid;
481 else if (compare_term (li, "datetime", 0))
482 li->term_type = Z_Term_dateTime;
483 else if (compare_term (li, "null", 0))
484 li->term_type = Z_Term_null;
487 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
489 case 0: /* operator/operand expected! */
495 Z_RPNQuery *p_query_rpn_mk (ODR o, struct lex_info *li, oid_proto proto,
499 int attr_array[1024];
500 oid_value attr_set[512];
501 oid_value topSet = VAL_NONE;
505 zq = (Z_RPNQuery *)odr_malloc (o, sizeof(*zq));
507 if (li->query_look == 'r')
510 topSet = query_oid_getvalbyname (li);
511 if (topSet == VAL_NONE)
516 if (topSet == VAL_NONE)
517 topSet = p_query_dfset;
518 if (topSet == VAL_NONE)
521 oset.oclass = CLASS_ATTSET;
524 if (!oid_ent_to_oid (&oset, oid))
526 zq->attributeSetId = odr_oiddup (o, oid);
528 if (!(zq->RPNStructure = rpn_structure (li, o, proto, 0, 512,
529 attr_array, attr_set)))
534 Z_RPNQuery *p_query_rpn (ODR o, oid_proto proto,
540 li.right_sep = "}\"";
541 li.escape_char = '@';
542 li.term_type = Z_Term_general;
544 return p_query_rpn_mk (o, &li, proto, qbuf);
547 Z_AttributesPlusTerm *p_query_scan_mk (struct lex_info *li,
548 ODR o, oid_proto proto,
549 Odr_oid **attributeSetP,
553 oid_value attr_set[512];
557 oid_value topSet = VAL_NONE;
562 if (li->query_look == 'r')
565 topSet = query_oid_getvalbyname (li);
569 if (topSet == VAL_NONE)
570 topSet = p_query_dfset;
571 if (topSet == VAL_NONE)
574 oset.oclass = CLASS_ATTSET;
577 *attributeSetP = odr_oiddup (o, oid_ent_to_oid (&oset, oid));
579 while (li->query_look == 'l')
584 if (num_attr >= max_attr)
587 if (!(cp = strchr (li->lex_buf, '=')) ||
588 (size_t) (cp-li->lex_buf) > li->lex_len)
590 attr_set[num_attr] = query_oid_getvalbyname (li);
593 if (!(cp = strchr (li->lex_buf, '=')))
599 attr_set[num_attr] = attr_set[num_attr-1];
601 attr_set[num_attr] = VAL_NONE;
603 attr_list[2*num_attr] = atoi (li->lex_buf);
604 attr_list[2*num_attr+1] = atoi (cp+1);
610 return rpn_term (li, o, proto, num_attr, attr_list, attr_set);
613 Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,
614 Odr_oid **attributeSetP,
620 li.right_sep = "}\"";
621 li.escape_char = '@';
622 li.term_type = Z_Term_general;
625 return p_query_scan_mk (&li, o, proto, attributeSetP, qbuf);
628 int p_query_attset (const char *arg)
630 p_query_dfset = oid_getvalbyname (arg);
631 return (p_query_dfset == VAL_NONE) ? -1 : 0;