2 * Copyright (c) 1995-1998, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.22 1999-04-20 09:56:49 adam
8 * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
9 * Modified all encoders/decoders to reflect this change.
11 * Revision 1.21 1998/10/13 16:03:37 adam
12 * Better checking for invalid OID's in p_query_rpn.
14 * Revision 1.20 1998/03/31 15:13:20 adam
15 * Development towards compiled ASN.1.
17 * Revision 1.19 1998/03/05 08:09:03 adam
18 * Minor change to make C++ happy.
20 * Revision 1.18 1998/02/11 11:53:36 adam
21 * Changed code so that it compiles as C++.
23 * Revision 1.17 1997/11/24 11:33:57 adam
24 * Using function odr_nullval() instead of global ODR_NULLVAL when
27 * Revision 1.16 1997/09/29 13:19:00 adam
28 * Added function, oid_ent_to_oid, to replace the function
29 * oid_getoidbyent, which is not thread safe.
31 * Revision 1.15 1997/09/29 07:13:43 adam
32 * Changed type of a few variables to avoid warnings.
34 * Revision 1.14 1997/09/22 12:33:41 adam
35 * Fixed bug introduced by previous commit.
37 * Revision 1.13 1997/09/17 12:10:42 adam
40 * Revision 1.12 1997/09/01 08:54:13 adam
41 * New windows NT/95 port using MSV5.0. Made prefix query handling
42 * thread safe. The function options ignores empty arguments when met.
44 * Revision 1.11 1996/11/11 13:15:29 adam
45 * Added proximity operator.
47 * Revision 1.10 1996/08/12 14:10:35 adam
48 * New function p_query_attset to define default attribute set.
50 * Revision 1.9 1996/03/15 11:03:46 adam
51 * Attribute set can be set globally for a query with the @attrset
52 * operator. The @attr operator has an optional attribute-set specifier
53 * that sets the attribute set locally.
55 * Revision 1.8 1996/01/02 11:46:56 quinn
56 * Changed 'operator' to 'roperator' to avoid C++ conflict.
58 * Revision 1.7 1995/09/29 17:12:36 quinn
61 * Revision 1.6 1995/09/27 15:03:03 quinn
62 * Modified function heads & prototypes.
64 * Revision 1.5 1995/06/15 12:31:02 quinn
65 * *** empty log message ***
67 * Revision 1.4 1995/06/15 07:45:19 quinn
70 * Revision 1.3 1995/06/14 11:06:35 adam
71 * Bug fix: Attributes wasn't interpreted correctly!
73 * Revision 1.2 1995/05/26 08:56:11 adam
74 * New function: p_query_scan.
76 * Revision 1.1 1995/05/22 15:31:49 adam
77 * New function, p_query_rpn, to convert from prefix (ascii) to rpn (asn).
90 static oid_value p_query_dfset = VAL_NONE;
93 const char *query_buf;
103 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o, oid_proto,
104 int num_attr, int max_attr,
105 int *attr_list, oid_value *attr_set);
107 static enum oid_value query_oid_getvalbyname (struct lex_info *li)
109 enum oid_value value;
112 if (li->lex_len > 31)
114 memcpy (buf, li->lex_buf, li->lex_len);
115 buf[li->lex_len] = '\0';
116 value = oid_getvalbyname (buf);
120 static int compare_term (struct lex_info *li, const char *src, size_t off)
122 size_t len=strlen(src);
124 if (li->lex_len == len+off && !memcmp (li->lex_buf+off, src, len-off))
129 static int query_token (struct lex_info *li)
131 const char *sep_match;
132 const char **qptr = &li->query_buf;
134 while (**qptr == ' ')
139 if ((sep_match = strchr (li->left_sep, **qptr)))
141 int sep_index = sep_match - li->left_sep;
145 while (**qptr && **qptr != li->right_sep[sep_index])
156 while (**qptr && **qptr != ' ')
162 if (li->lex_len >= 1 && li->lex_buf[0] == li->escape_char)
164 if (compare_term (li, "and", 1))
166 if (compare_term (li, "or", 1))
168 if (compare_term (li, "not", 1))
170 if (compare_term (li, "attr", 1))
172 if (compare_term (li, "set", 1))
174 if (compare_term (li, "attrset", 1))
176 if (compare_term (li, "prox", 1))
178 if (compare_term (li, "term", 1))
184 static int lex (struct lex_info *li)
186 return li->query_look = query_token (li);
189 static Z_AttributesPlusTerm *rpn_term (struct lex_info *li, ODR o,
191 int num_attr, int *attr_list,
194 Z_AttributesPlusTerm *zapt;
197 Z_AttributeElement **elements;
199 zapt = (Z_AttributesPlusTerm *)odr_malloc (o, sizeof(*zapt));
200 term_octet = (Odr_oct *)odr_malloc (o, sizeof(*term_octet));
201 term = (Z_Term *)odr_malloc (o, sizeof(*term));
204 elements = (Z_AttributeElement**)odr_nullval();
210 elements = (Z_AttributeElement**)
211 odr_malloc (o, num_attr * sizeof(*elements));
213 attr_tmp = (int *)odr_malloc (o, num_attr * 2 * sizeof(int));
214 memcpy (attr_tmp, attr_list, num_attr * 2 * sizeof(int));
215 for (i = 0; i < num_attr; i++)
218 (Z_AttributeElement*)odr_malloc (o,sizeof(**elements));
219 elements[i]->attributeType = &attr_tmp[2*i];
220 if (attr_set[i] == VAL_NONE)
221 elements[i]->attributeSet = 0;
227 attrid.proto = PROTO_Z3950;
228 attrid.oclass = CLASS_ATTSET;
229 attrid.value = attr_set[i];
231 elements[i]->attributeSet =
232 odr_oiddup (o, oid_ent_to_oid (&attrid, oid));
234 elements[i]->which = Z_AttributeValue_numeric;
235 elements[i]->value.numeric = &attr_tmp[2*i+1];
239 zapt->attributes = (Z_AttributeList *)
240 odr_malloc (o, sizeof(*zapt->attributes));
241 zapt->attributes->num_attributes = num_attr;
242 zapt->attributes->attributes = elements;
244 zapt->num_attributes = num_attr;
245 zapt->attributeList = elements;
249 term->which = Z_Term_general;
250 term->u.general = term_octet;
251 term_octet->buf = (unsigned char *)odr_malloc (o, li->lex_len);
252 term_octet->size = term_octet->len = li->lex_len;
253 memcpy (term_octet->buf, li->lex_buf, li->lex_len);
257 static Z_Operand *rpn_simple (struct lex_info *li, ODR o, oid_proto proto,
258 int num_attr, int *attr_list,
263 zo = (Z_Operand *)odr_malloc (o, sizeof(*zo));
264 switch (li->query_look)
267 zo->which = Z_Operand_APT;
268 if (!(zo->u.attributesPlusTerm =
269 rpn_term (li, o, proto, num_attr, attr_list, attr_set)))
277 zo->which = Z_Operand_resultSetId;
278 zo->u.resultSetId = (char *)odr_malloc (o, li->lex_len+1);
279 memcpy (zo->u.resultSetId, li->lex_buf, li->lex_len);
280 zo->u.resultSetId[li->lex_len] = '\0';
289 static Z_ProximityOperator *rpn_proximity (struct lex_info *li, ODR o)
291 Z_ProximityOperator *p = (Z_ProximityOperator *)odr_malloc (o, sizeof(*p));
295 if (*li->lex_buf == '1')
297 p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion));
300 else if (*li->lex_buf == '0')
302 p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion));
310 p->distance = (int *)odr_malloc (o, sizeof(*p->distance));
311 *p->distance = atoi (li->lex_buf);
315 p->ordered = (int *)odr_malloc (o, sizeof(*p->ordered));
316 *p->ordered = atoi (li->lex_buf);
320 p->relationType = (int *)odr_malloc (o, sizeof(*p->relationType));
321 *p->relationType = atoi (li->lex_buf);
325 if (*li->lex_buf == 'k')
327 else if (*li->lex_buf == 'p')
330 p->which = atoi (li->lex_buf);
335 p->which = Z_ProximityOperator_known;
336 p->u.known = (int *)odr_malloc (o, sizeof(*p->u.known));
337 *p->u.known = atoi (li->lex_buf);
339 p->proximityUnitCode = (int *)odr_malloc (o, sizeof(*p->proximityUnitCode));
340 *p->proximityUnitCode = atoi (li->lex_buf);
345 static Z_Complex *rpn_complex (struct lex_info *li, ODR o, oid_proto proto,
346 int num_attr, int max_attr,
347 int *attr_list, oid_value *attr_set)
352 zc = (Z_Complex *)odr_malloc (o, sizeof(*zc));
353 zo = (Z_Operator *)odr_malloc (o, sizeof(*zo));
355 switch (li->query_look)
358 zo->which = Z_Operator_and;
359 zo->u.and = odr_nullval();
362 zo->which = Z_Operator_or;
363 zo->u.and = odr_nullval();
366 zo->which = Z_Operator_and_not;
367 zo->u.and = odr_nullval();
370 zo->which = Z_Operator_prox;
371 zo->u.prox = rpn_proximity (li, o);
380 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
384 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
390 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o,
392 int num_attr, int max_attr,
393 int *attr_list, oid_value *attr_set)
398 sz = (Z_RPNStructure *)odr_malloc (o, sizeof(*sz));
399 switch (li->query_look)
405 sz->which = Z_RPNStructure_complex;
406 if (!(sz->u.complex =
407 rpn_complex (li, o, proto, num_attr, max_attr, attr_list,
413 sz->which = Z_RPNStructure_simple;
415 rpn_simple (li, o, proto, num_attr, attr_list,
423 if (num_attr >= max_attr)
425 if (!(cp = strchr (li->lex_buf, '=')) ||
426 (size_t) (cp-li->lex_buf) > li->lex_len)
428 attr_set[num_attr] = query_oid_getvalbyname (li);
429 if (attr_set[num_attr] == VAL_NONE)
433 if (!(cp = strchr (li->lex_buf, '=')))
439 attr_set[num_attr] = attr_set[num_attr-1];
441 attr_set[num_attr] = VAL_NONE;
443 attr_list[2*num_attr] = atoi (li->lex_buf);
444 attr_list[2*num_attr+1] = atoi (cp+1);
448 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
454 if (compare_term (li, "general", 0))
455 li->term_type = Z_Term_general;
456 else if (compare_term (li, "numeric", 0))
457 li->term_type = Z_Term_numeric;
458 else if (compare_term (li, "string", 0))
459 li->term_type = Z_Term_characterString;
460 else if (compare_term (li, "oid", 0))
461 li->term_type = Z_Term_oid;
462 else if (compare_term (li, "datetime", 0))
463 li->term_type = Z_Term_dateTime;
464 else if (compare_term (li, "null", 0))
465 li->term_type = Z_Term_null;
468 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
470 case 0: /* operator/operand expected! */
476 Z_RPNQuery *p_query_rpn_mk (ODR o, struct lex_info *li, oid_proto proto,
480 int attr_array[1024];
481 oid_value attr_set[512];
482 oid_value topSet = VAL_NONE;
486 zq = (Z_RPNQuery *)odr_malloc (o, sizeof(*zq));
488 if (li->query_look == 'r')
491 topSet = query_oid_getvalbyname (li);
492 if (topSet == VAL_NONE)
497 if (topSet == VAL_NONE)
498 topSet = p_query_dfset;
499 if (topSet == VAL_NONE)
502 oset.oclass = CLASS_ATTSET;
505 if (!oid_ent_to_oid (&oset, oid))
507 zq->attributeSetId = odr_oiddup (o, oid);
509 if (!(zq->RPNStructure = rpn_structure (li, o, proto, 0, 512,
510 attr_array, attr_set)))
515 Z_RPNQuery *p_query_rpn (ODR o, oid_proto proto,
521 li.right_sep = "}\"";
522 li.escape_char = '@';
523 li.term_type = Z_Term_general;
525 return p_query_rpn_mk (o, &li, proto, qbuf);
528 Z_AttributesPlusTerm *p_query_scan_mk (struct lex_info *li,
529 ODR o, oid_proto proto,
530 Odr_oid **attributeSetP,
534 oid_value attr_set[512];
538 oid_value topSet = VAL_NONE;
543 if (li->query_look == 'r')
546 topSet = query_oid_getvalbyname (li);
550 if (topSet == VAL_NONE)
551 topSet = p_query_dfset;
552 if (topSet == VAL_NONE)
555 oset.oclass = CLASS_ATTSET;
558 *attributeSetP = odr_oiddup (o, oid_ent_to_oid (&oset, oid));
560 while (li->query_look == 'l')
565 if (num_attr >= max_attr)
568 if (!(cp = strchr (li->lex_buf, '=')) ||
569 (size_t) (cp-li->lex_buf) > li->lex_len)
571 attr_set[num_attr] = query_oid_getvalbyname (li);
574 if (!(cp = strchr (li->lex_buf, '=')))
580 attr_set[num_attr] = attr_set[num_attr-1];
582 attr_set[num_attr] = VAL_NONE;
584 attr_list[2*num_attr] = atoi (li->lex_buf);
585 attr_list[2*num_attr+1] = atoi (cp+1);
591 return rpn_term (li, o, proto, num_attr, attr_list, attr_set);
594 Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,
595 Odr_oid **attributeSetP,
601 li.right_sep = "}\"";
602 li.escape_char = '@';
603 li.term_type = Z_Term_general;
606 return p_query_scan_mk (&li, o, proto, attributeSetP, qbuf);
609 int p_query_attset (const char *arg)
611 p_query_dfset = oid_getvalbyname (arg);
612 return (p_query_dfset == VAL_NONE) ? -1 : 0;