2 * Copyright (c) 1995-1997, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.15 1997-09-29 07:13:43 adam
8 * Changed type of a few variables to avoid warnings.
10 * Revision 1.14 1997/09/22 12:33:41 adam
11 * Fixed bug introduced by previous commit.
13 * Revision 1.13 1997/09/17 12:10:42 adam
16 * Revision 1.12 1997/09/01 08:54:13 adam
17 * New windows NT/95 port using MSV5.0. Made prefix query handling
18 * thread safe. The function options ignores empty arguments when met.
20 * Revision 1.11 1996/11/11 13:15:29 adam
21 * Added proximity operator.
23 * Revision 1.10 1996/08/12 14:10:35 adam
24 * New function p_query_attset to define default attribute set.
26 * Revision 1.9 1996/03/15 11:03:46 adam
27 * Attribute set can be set globally for a query with the @attrset
28 * operator. The @attr operator has an optional attribute-set specifier
29 * that sets the attribute set locally.
31 * Revision 1.8 1996/01/02 11:46:56 quinn
32 * Changed 'operator' to 'roperator' to avoid C++ conflict.
34 * Revision 1.7 1995/09/29 17:12:36 quinn
37 * Revision 1.6 1995/09/27 15:03:03 quinn
38 * Modified function heads & prototypes.
40 * Revision 1.5 1995/06/15 12:31:02 quinn
41 * *** empty log message ***
43 * Revision 1.4 1995/06/15 07:45:19 quinn
46 * Revision 1.3 1995/06/14 11:06:35 adam
47 * Bug fix: Attributes wasn't interpreted correctly!
49 * Revision 1.2 1995/05/26 08:56:11 adam
50 * New function: p_query_scan.
52 * Revision 1.1 1995/05/22 15:31:49 adam
53 * New function, p_query_rpn, to convert from prefix (ascii) to rpn (asn).
66 static oid_value p_query_dfset = VAL_NONE;
69 const char *query_buf;
79 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o, oid_proto,
80 int num_attr, int max_attr,
81 int *attr_list, oid_value *attr_set);
83 static int query_oid_getvalbyname (struct lex_info *li)
89 memcpy (buf, li->lex_buf, li->lex_len);
90 buf[li->lex_len] = '\0';
91 return oid_getvalbyname (buf);
94 static int compare_term (struct lex_info *li, const char *src, size_t off)
96 size_t len=strlen(src);
98 if (li->lex_len == len+off && !memcmp (li->lex_buf+off, src, len-off))
103 static int query_token (struct lex_info *li)
105 const char *sep_match;
106 const char **qptr = &li->query_buf;
108 while (**qptr == ' ')
113 if ((sep_match = strchr (li->left_sep, **qptr)))
115 int sep_index = sep_match - li->left_sep;
119 while (**qptr && **qptr != li->right_sep[sep_index])
130 while (**qptr && **qptr != ' ')
136 if (li->lex_len >= 1 && li->lex_buf[0] == li->escape_char)
138 if (compare_term (li, "and", 1))
140 if (compare_term (li, "or", 1))
142 if (compare_term (li, "not", 1))
144 if (compare_term (li, "attr", 1))
146 if (compare_term (li, "set", 1))
148 if (compare_term (li, "attrset", 1))
150 if (compare_term (li, "prox", 1))
152 if (compare_term (li, "term", 1))
158 static int lex (struct lex_info *li)
160 return li->query_look = query_token (li);
163 static Z_AttributesPlusTerm *rpn_term (struct lex_info *li, ODR o,
165 int num_attr, int *attr_list,
168 Z_AttributesPlusTerm *zapt;
172 zapt = odr_malloc (o, sizeof(*zapt));
173 term_octet = odr_malloc (o, sizeof(*term_octet));
174 term = odr_malloc (o, sizeof(*term));
176 zapt->num_attributes = num_attr;
182 zapt->attributeList = odr_malloc (o, num_attr *
183 sizeof(*zapt->attributeList));
185 attr_tmp = odr_malloc (o, num_attr * 2 * sizeof(int));
186 memcpy (attr_tmp, attr_list, num_attr * 2 * sizeof(int));
187 for (i = 0; i < num_attr; i++)
189 zapt->attributeList[i] =
190 odr_malloc (o,sizeof(**zapt->attributeList));
191 zapt->attributeList[i]->attributeType = &attr_tmp[2*i];
193 if (attr_set[i] == VAL_NONE)
194 zapt->attributeList[i]->attributeSet = 0;
199 attrid.proto = PROTO_Z3950;
200 attrid.oclass = CLASS_ATTSET;
201 attrid.value = attr_set[i];
203 zapt->attributeList[i]->attributeSet =
204 odr_oiddup (o, oid_getoidbyent (&attrid));
206 zapt->attributeList[i]->which = Z_AttributeValue_numeric;
207 zapt->attributeList[i]->value.numeric = &attr_tmp[2*i+1];
209 zapt->attributeList[i]->attributeValue = &attr_tmp[2*i+1];
214 zapt->attributeList = ODR_NULLVAL;
216 term->which = Z_Term_general;
217 term->u.general = term_octet;
218 term_octet->buf = odr_malloc (o, li->lex_len);
219 term_octet->size = term_octet->len = li->lex_len;
220 memcpy (term_octet->buf, li->lex_buf, li->lex_len);
224 static Z_Operand *rpn_simple (struct lex_info *li, ODR o, oid_proto proto,
225 int num_attr, int *attr_list,
230 zo = odr_malloc (o, sizeof(*zo));
231 switch (li->query_look)
234 zo->which = Z_Operand_APT;
235 if (!(zo->u.attributesPlusTerm =
236 rpn_term (li, o, proto, num_attr, attr_list, attr_set)))
244 zo->which = Z_Operand_resultSetId;
245 zo->u.resultSetId = odr_malloc (o, li->lex_len+1);
246 memcpy (zo->u.resultSetId, li->lex_buf, li->lex_len);
247 zo->u.resultSetId[li->lex_len] = '\0';
256 static Z_ProximityOperator *rpn_proximity (struct lex_info *li, ODR o)
258 Z_ProximityOperator *p = odr_malloc (o, sizeof(*p));
262 if (*li->lex_buf == '1')
264 p->exclusion = odr_malloc (o, sizeof(*p->exclusion));
267 else if (*li->lex_buf == '0')
269 p->exclusion = odr_malloc (o, sizeof(*p->exclusion));
277 p->distance = odr_malloc (o, sizeof(*p->distance));
278 *p->distance = atoi (li->lex_buf);
282 p->ordered = odr_malloc (o, sizeof(*p->ordered));
283 *p->ordered = atoi (li->lex_buf);
287 p->relationType = odr_malloc (o, sizeof(*p->relationType));
288 *p->relationType = atoi (li->lex_buf);
292 if (*li->lex_buf == 'k')
294 else if (*li->lex_buf == 'p')
297 p->which = atoi (li->lex_buf);
301 p->proximityUnitCode = odr_malloc (o, sizeof(*p->proximityUnitCode));
302 *p->proximityUnitCode = atoi (li->lex_buf);
307 static Z_Complex *rpn_complex (struct lex_info *li, ODR o, oid_proto proto,
308 int num_attr, int max_attr,
309 int *attr_list, oid_value *attr_set)
314 zc = odr_malloc (o, sizeof(*zc));
315 zo = odr_malloc (o, sizeof(*zo));
317 switch (li->query_look)
320 zo->which = Z_Operator_and;
321 zo->u.and = ODR_NULLVAL;
324 zo->which = Z_Operator_or;
325 zo->u.and = ODR_NULLVAL;
328 zo->which = Z_Operator_and_not;
329 zo->u.and = ODR_NULLVAL;
332 zo->which = Z_Operator_prox;
333 zo->u.prox = rpn_proximity (li, o);
342 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
346 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
352 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o,
354 int num_attr, int max_attr,
355 int *attr_list, oid_value *attr_set)
360 sz = odr_malloc (o, sizeof(*sz));
361 switch (li->query_look)
367 sz->which = Z_RPNStructure_complex;
368 if (!(sz->u.complex =
369 rpn_complex (li, o, proto, num_attr, max_attr, attr_list,
375 sz->which = Z_RPNStructure_simple;
377 rpn_simple (li, o, proto, num_attr, attr_list,
385 if (num_attr >= max_attr)
387 if (!(cp = strchr (li->lex_buf, '=')) ||
388 (size_t) (cp-li->lex_buf) > li->lex_len)
390 attr_set[num_attr] = query_oid_getvalbyname (li);
393 if (!(cp = strchr (li->lex_buf, '=')))
399 attr_set[num_attr] = attr_set[num_attr-1];
401 attr_set[num_attr] = VAL_NONE;
403 attr_list[2*num_attr] = atoi (li->lex_buf);
404 attr_list[2*num_attr+1] = atoi (cp+1);
408 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
414 if (compare_term (li, "general", 0))
415 li->term_type = Z_Term_general;
416 else if (compare_term (li, "numeric", 0))
417 li->term_type = Z_Term_numeric;
418 else if (compare_term (li, "string", 0))
419 li->term_type = Z_Term_characterString;
420 else if (compare_term (li, "oid", 0))
421 li->term_type = Z_Term_oid;
422 else if (compare_term (li, "datetime", 0))
423 li->term_type = Z_Term_dateTime;
424 else if (compare_term (li, "null", 0))
425 li->term_type = Z_Term_null;
428 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
430 case 0: /* operator/operand expected! */
436 Z_RPNQuery *p_query_rpn_mk (ODR o, struct lex_info *li, oid_proto proto,
440 int attr_array[1024];
441 oid_value attr_set[512];
442 oid_value topSet = VAL_NONE;
445 zq = odr_malloc (o, sizeof(*zq));
447 if (li->query_look == 'r')
450 topSet = query_oid_getvalbyname (li);
451 if (topSet == VAL_NONE)
456 if (topSet == VAL_NONE)
457 topSet = p_query_dfset;
458 if (topSet == VAL_NONE)
461 oset.oclass = CLASS_ATTSET;
464 zq->attributeSetId = odr_oiddup (o, oid_getoidbyent (&oset));
466 if (!(zq->RPNStructure = rpn_structure (li, o, proto, 0, 512,
467 attr_array, attr_set)))
472 Z_RPNQuery *p_query_rpn (ODR o, oid_proto proto,
478 li.right_sep = "}\"";
479 li.escape_char = '@';
480 li.term_type = Z_Term_general;
482 return p_query_rpn_mk (o, &li, proto, qbuf);
485 Z_AttributesPlusTerm *p_query_scan_mk (struct lex_info *li,
486 ODR o, oid_proto proto,
487 Odr_oid **attributeSetP,
491 oid_value attr_set[512];
495 oid_value topSet = VAL_NONE;
499 if (li->query_look == 'r')
502 topSet = query_oid_getvalbyname (li);
506 if (topSet == VAL_NONE)
507 topSet = p_query_dfset;
508 if (topSet == VAL_NONE)
511 oset.oclass = CLASS_ATTSET;
514 *attributeSetP = odr_oiddup (o, oid_getoidbyent (&oset));
516 while (li->query_look == 'l')
521 if (num_attr >= max_attr)
524 if (!(cp = strchr (li->lex_buf, '=')) ||
525 (size_t) (cp-li->lex_buf) > li->lex_len)
527 attr_set[num_attr] = query_oid_getvalbyname (li);
530 if (!(cp = strchr (li->lex_buf, '=')))
536 attr_set[num_attr] = attr_set[num_attr-1];
538 attr_set[num_attr] = VAL_NONE;
540 attr_list[2*num_attr] = atoi (li->lex_buf);
541 attr_list[2*num_attr+1] = atoi (cp+1);
547 return rpn_term (li, o, proto, num_attr, attr_list, attr_set);
550 Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,
551 Odr_oid **attributeSetP,
557 li.right_sep = "}\"";
558 li.escape_char = '@';
559 li.term_type = Z_Term_general;
562 return p_query_scan_mk (&li, o, proto, attributeSetP, qbuf);
565 int p_query_attset (const char *arg)
567 p_query_dfset = oid_getvalbyname (arg);
568 return (p_query_dfset == VAL_NONE) ? -1 : 0;