2 * Copyright (c) 1995-1997, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.16 1997-09-29 13:19:00 adam
8 * Added function, oid_ent_to_oid, to replace the function
9 * oid_getoidbyent, which is not thread safe.
11 * Revision 1.15 1997/09/29 07:13:43 adam
12 * Changed type of a few variables to avoid warnings.
14 * Revision 1.14 1997/09/22 12:33:41 adam
15 * Fixed bug introduced by previous commit.
17 * Revision 1.13 1997/09/17 12:10:42 adam
20 * Revision 1.12 1997/09/01 08:54:13 adam
21 * New windows NT/95 port using MSV5.0. Made prefix query handling
22 * thread safe. The function options ignores empty arguments when met.
24 * Revision 1.11 1996/11/11 13:15:29 adam
25 * Added proximity operator.
27 * Revision 1.10 1996/08/12 14:10:35 adam
28 * New function p_query_attset to define default attribute set.
30 * Revision 1.9 1996/03/15 11:03:46 adam
31 * Attribute set can be set globally for a query with the @attrset
32 * operator. The @attr operator has an optional attribute-set specifier
33 * that sets the attribute set locally.
35 * Revision 1.8 1996/01/02 11:46:56 quinn
36 * Changed 'operator' to 'roperator' to avoid C++ conflict.
38 * Revision 1.7 1995/09/29 17:12:36 quinn
41 * Revision 1.6 1995/09/27 15:03:03 quinn
42 * Modified function heads & prototypes.
44 * Revision 1.5 1995/06/15 12:31:02 quinn
45 * *** empty log message ***
47 * Revision 1.4 1995/06/15 07:45:19 quinn
50 * Revision 1.3 1995/06/14 11:06:35 adam
51 * Bug fix: Attributes wasn't interpreted correctly!
53 * Revision 1.2 1995/05/26 08:56:11 adam
54 * New function: p_query_scan.
56 * Revision 1.1 1995/05/22 15:31:49 adam
57 * New function, p_query_rpn, to convert from prefix (ascii) to rpn (asn).
70 static oid_value p_query_dfset = VAL_NONE;
73 const char *query_buf;
83 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o, oid_proto,
84 int num_attr, int max_attr,
85 int *attr_list, oid_value *attr_set);
87 static int query_oid_getvalbyname (struct lex_info *li)
93 memcpy (buf, li->lex_buf, li->lex_len);
94 buf[li->lex_len] = '\0';
95 return oid_getvalbyname (buf);
98 static int compare_term (struct lex_info *li, const char *src, size_t off)
100 size_t len=strlen(src);
102 if (li->lex_len == len+off && !memcmp (li->lex_buf+off, src, len-off))
107 static int query_token (struct lex_info *li)
109 const char *sep_match;
110 const char **qptr = &li->query_buf;
112 while (**qptr == ' ')
117 if ((sep_match = strchr (li->left_sep, **qptr)))
119 int sep_index = sep_match - li->left_sep;
123 while (**qptr && **qptr != li->right_sep[sep_index])
134 while (**qptr && **qptr != ' ')
140 if (li->lex_len >= 1 && li->lex_buf[0] == li->escape_char)
142 if (compare_term (li, "and", 1))
144 if (compare_term (li, "or", 1))
146 if (compare_term (li, "not", 1))
148 if (compare_term (li, "attr", 1))
150 if (compare_term (li, "set", 1))
152 if (compare_term (li, "attrset", 1))
154 if (compare_term (li, "prox", 1))
156 if (compare_term (li, "term", 1))
162 static int lex (struct lex_info *li)
164 return li->query_look = query_token (li);
167 static Z_AttributesPlusTerm *rpn_term (struct lex_info *li, ODR o,
169 int num_attr, int *attr_list,
172 Z_AttributesPlusTerm *zapt;
176 zapt = odr_malloc (o, sizeof(*zapt));
177 term_octet = odr_malloc (o, sizeof(*term_octet));
178 term = odr_malloc (o, sizeof(*term));
180 zapt->num_attributes = num_attr;
186 zapt->attributeList = odr_malloc (o, num_attr *
187 sizeof(*zapt->attributeList));
189 attr_tmp = odr_malloc (o, num_attr * 2 * sizeof(int));
190 memcpy (attr_tmp, attr_list, num_attr * 2 * sizeof(int));
191 for (i = 0; i < num_attr; i++)
193 zapt->attributeList[i] =
194 odr_malloc (o,sizeof(**zapt->attributeList));
195 zapt->attributeList[i]->attributeType = &attr_tmp[2*i];
197 if (attr_set[i] == VAL_NONE)
198 zapt->attributeList[i]->attributeSet = 0;
204 attrid.proto = PROTO_Z3950;
205 attrid.oclass = CLASS_ATTSET;
206 attrid.value = attr_set[i];
208 zapt->attributeList[i]->attributeSet =
209 odr_oiddup (o, oid_ent_to_oid (&attrid, oid));
211 zapt->attributeList[i]->which = Z_AttributeValue_numeric;
212 zapt->attributeList[i]->value.numeric = &attr_tmp[2*i+1];
214 zapt->attributeList[i]->attributeValue = &attr_tmp[2*i+1];
219 zapt->attributeList = ODR_NULLVAL;
221 term->which = Z_Term_general;
222 term->u.general = term_octet;
223 term_octet->buf = odr_malloc (o, li->lex_len);
224 term_octet->size = term_octet->len = li->lex_len;
225 memcpy (term_octet->buf, li->lex_buf, li->lex_len);
229 static Z_Operand *rpn_simple (struct lex_info *li, ODR o, oid_proto proto,
230 int num_attr, int *attr_list,
235 zo = odr_malloc (o, sizeof(*zo));
236 switch (li->query_look)
239 zo->which = Z_Operand_APT;
240 if (!(zo->u.attributesPlusTerm =
241 rpn_term (li, o, proto, num_attr, attr_list, attr_set)))
249 zo->which = Z_Operand_resultSetId;
250 zo->u.resultSetId = odr_malloc (o, li->lex_len+1);
251 memcpy (zo->u.resultSetId, li->lex_buf, li->lex_len);
252 zo->u.resultSetId[li->lex_len] = '\0';
261 static Z_ProximityOperator *rpn_proximity (struct lex_info *li, ODR o)
263 Z_ProximityOperator *p = odr_malloc (o, sizeof(*p));
267 if (*li->lex_buf == '1')
269 p->exclusion = odr_malloc (o, sizeof(*p->exclusion));
272 else if (*li->lex_buf == '0')
274 p->exclusion = odr_malloc (o, sizeof(*p->exclusion));
282 p->distance = odr_malloc (o, sizeof(*p->distance));
283 *p->distance = atoi (li->lex_buf);
287 p->ordered = odr_malloc (o, sizeof(*p->ordered));
288 *p->ordered = atoi (li->lex_buf);
292 p->relationType = odr_malloc (o, sizeof(*p->relationType));
293 *p->relationType = atoi (li->lex_buf);
297 if (*li->lex_buf == 'k')
299 else if (*li->lex_buf == 'p')
302 p->which = atoi (li->lex_buf);
306 p->proximityUnitCode = odr_malloc (o, sizeof(*p->proximityUnitCode));
307 *p->proximityUnitCode = atoi (li->lex_buf);
312 static Z_Complex *rpn_complex (struct lex_info *li, ODR o, oid_proto proto,
313 int num_attr, int max_attr,
314 int *attr_list, oid_value *attr_set)
319 zc = odr_malloc (o, sizeof(*zc));
320 zo = odr_malloc (o, sizeof(*zo));
322 switch (li->query_look)
325 zo->which = Z_Operator_and;
326 zo->u.and = ODR_NULLVAL;
329 zo->which = Z_Operator_or;
330 zo->u.and = ODR_NULLVAL;
333 zo->which = Z_Operator_and_not;
334 zo->u.and = ODR_NULLVAL;
337 zo->which = Z_Operator_prox;
338 zo->u.prox = rpn_proximity (li, o);
347 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
351 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
357 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o,
359 int num_attr, int max_attr,
360 int *attr_list, oid_value *attr_set)
365 sz = odr_malloc (o, sizeof(*sz));
366 switch (li->query_look)
372 sz->which = Z_RPNStructure_complex;
373 if (!(sz->u.complex =
374 rpn_complex (li, o, proto, num_attr, max_attr, attr_list,
380 sz->which = Z_RPNStructure_simple;
382 rpn_simple (li, o, proto, num_attr, attr_list,
390 if (num_attr >= max_attr)
392 if (!(cp = strchr (li->lex_buf, '=')) ||
393 (size_t) (cp-li->lex_buf) > li->lex_len)
395 attr_set[num_attr] = query_oid_getvalbyname (li);
398 if (!(cp = strchr (li->lex_buf, '=')))
404 attr_set[num_attr] = attr_set[num_attr-1];
406 attr_set[num_attr] = VAL_NONE;
408 attr_list[2*num_attr] = atoi (li->lex_buf);
409 attr_list[2*num_attr+1] = atoi (cp+1);
413 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
419 if (compare_term (li, "general", 0))
420 li->term_type = Z_Term_general;
421 else if (compare_term (li, "numeric", 0))
422 li->term_type = Z_Term_numeric;
423 else if (compare_term (li, "string", 0))
424 li->term_type = Z_Term_characterString;
425 else if (compare_term (li, "oid", 0))
426 li->term_type = Z_Term_oid;
427 else if (compare_term (li, "datetime", 0))
428 li->term_type = Z_Term_dateTime;
429 else if (compare_term (li, "null", 0))
430 li->term_type = Z_Term_null;
433 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
435 case 0: /* operator/operand expected! */
441 Z_RPNQuery *p_query_rpn_mk (ODR o, struct lex_info *li, oid_proto proto,
445 int attr_array[1024];
446 oid_value attr_set[512];
447 oid_value topSet = VAL_NONE;
451 zq = odr_malloc (o, sizeof(*zq));
453 if (li->query_look == 'r')
456 topSet = query_oid_getvalbyname (li);
457 if (topSet == VAL_NONE)
462 if (topSet == VAL_NONE)
463 topSet = p_query_dfset;
464 if (topSet == VAL_NONE)
467 oset.oclass = CLASS_ATTSET;
470 zq->attributeSetId = odr_oiddup (o, oid_ent_to_oid (&oset, oid));
472 if (!(zq->RPNStructure = rpn_structure (li, o, proto, 0, 512,
473 attr_array, attr_set)))
478 Z_RPNQuery *p_query_rpn (ODR o, oid_proto proto,
484 li.right_sep = "}\"";
485 li.escape_char = '@';
486 li.term_type = Z_Term_general;
488 return p_query_rpn_mk (o, &li, proto, qbuf);
491 Z_AttributesPlusTerm *p_query_scan_mk (struct lex_info *li,
492 ODR o, oid_proto proto,
493 Odr_oid **attributeSetP,
497 oid_value attr_set[512];
501 oid_value topSet = VAL_NONE;
506 if (li->query_look == 'r')
509 topSet = query_oid_getvalbyname (li);
513 if (topSet == VAL_NONE)
514 topSet = p_query_dfset;
515 if (topSet == VAL_NONE)
518 oset.oclass = CLASS_ATTSET;
521 *attributeSetP = odr_oiddup (o, oid_ent_to_oid (&oset, oid));
523 while (li->query_look == 'l')
528 if (num_attr >= max_attr)
531 if (!(cp = strchr (li->lex_buf, '=')) ||
532 (size_t) (cp-li->lex_buf) > li->lex_len)
534 attr_set[num_attr] = query_oid_getvalbyname (li);
537 if (!(cp = strchr (li->lex_buf, '=')))
543 attr_set[num_attr] = attr_set[num_attr-1];
545 attr_set[num_attr] = VAL_NONE;
547 attr_list[2*num_attr] = atoi (li->lex_buf);
548 attr_list[2*num_attr+1] = atoi (cp+1);
554 return rpn_term (li, o, proto, num_attr, attr_list, attr_set);
557 Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,
558 Odr_oid **attributeSetP,
564 li.right_sep = "}\"";
565 li.escape_char = '@';
566 li.term_type = Z_Term_general;
569 return p_query_scan_mk (&li, o, proto, attributeSetP, qbuf);
572 int p_query_attset (const char *arg)
574 p_query_dfset = oid_getvalbyname (arg);
575 return (p_query_dfset == VAL_NONE) ? -1 : 0;