2 * Copyright (c) 1995-1997, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.13 1997-09-17 12:10:42 adam
10 * Revision 1.12 1997/09/01 08:54:13 adam
11 * New windows NT/95 port using MSV5.0. Made prefix query handling
12 * thread safe. The function options ignores empty arguments when met.
14 * Revision 1.11 1996/11/11 13:15:29 adam
15 * Added proximity operator.
17 * Revision 1.10 1996/08/12 14:10:35 adam
18 * New function p_query_attset to define default attribute set.
20 * Revision 1.9 1996/03/15 11:03:46 adam
21 * Attribute set can be set globally for a query with the @attrset
22 * operator. The @attr operator has an optional attribute-set specifier
23 * that sets the attribute set locally.
25 * Revision 1.8 1996/01/02 11:46:56 quinn
26 * Changed 'operator' to 'roperator' to avoid C++ conflict.
28 * Revision 1.7 1995/09/29 17:12:36 quinn
31 * Revision 1.6 1995/09/27 15:03:03 quinn
32 * Modified function heads & prototypes.
34 * Revision 1.5 1995/06/15 12:31:02 quinn
35 * *** empty log message ***
37 * Revision 1.4 1995/06/15 07:45:19 quinn
40 * Revision 1.3 1995/06/14 11:06:35 adam
41 * Bug fix: Attributes wasn't interpreted correctly!
43 * Revision 1.2 1995/05/26 08:56:11 adam
44 * New function: p_query_scan.
46 * Revision 1.1 1995/05/22 15:31:49 adam
47 * New function, p_query_rpn, to convert from prefix (ascii) to rpn (asn).
60 static oid_value p_query_dfset = VAL_NONE;
63 const char *query_buf;
73 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o, oid_proto,
74 int num_attr, int max_attr,
75 int *attr_list, oid_value *attr_set);
77 static int query_oid_getvalbyname (struct lex_info *li)
83 memcpy (buf, li->lex_buf, li->lex_len);
84 buf[li->lex_len] = '\0';
85 return oid_getvalbyname (buf);
88 static int compare_term (struct lex_info *li, const char *src, int off)
90 size_t len=strlen(src);
92 if (li->lex_len == len && !memcmp (li->lex_buf+off, src, len-off))
97 static int query_token (struct lex_info *li)
99 const char *sep_match;
100 const char **qptr = &li->query_buf;
102 while (**qptr == ' ')
107 if ((sep_match = strchr (li->left_sep, **qptr)))
109 int sep_index = sep_match - li->left_sep;
113 while (**qptr && **qptr != li->right_sep[sep_index])
124 while (**qptr && **qptr != ' ')
130 if (li->lex_len >= 1 && li->lex_buf[0] == li->escape_char)
132 if (compare_term (li, "and", 1))
134 if (compare_term (li, "or", 1))
136 if (compare_term (li, "not", 1))
138 if (compare_term (li, "attr", 1))
140 if (compare_term (li, "set", 1))
142 if (compare_term (li, "attrset", 1))
144 if (compare_term (li, "prox", 1))
146 if (compare_term (li, "term", 1))
152 static int lex (struct lex_info *li)
154 return li->query_look = query_token (li);
157 static Z_AttributesPlusTerm *rpn_term (struct lex_info *li, ODR o,
159 int num_attr, int *attr_list,
162 Z_AttributesPlusTerm *zapt;
166 zapt = odr_malloc (o, sizeof(*zapt));
167 term_octet = odr_malloc (o, sizeof(*term_octet));
168 term = odr_malloc (o, sizeof(*term));
170 zapt->num_attributes = num_attr;
176 zapt->attributeList = odr_malloc (o, num_attr *
177 sizeof(*zapt->attributeList));
179 attr_tmp = odr_malloc (o, num_attr * 2 * sizeof(int));
180 memcpy (attr_tmp, attr_list, num_attr * 2 * sizeof(int));
181 for (i = 0; i < num_attr; i++)
183 zapt->attributeList[i] =
184 odr_malloc (o,sizeof(**zapt->attributeList));
185 zapt->attributeList[i]->attributeType = &attr_tmp[2*i];
187 if (attr_set[i] == VAL_NONE)
188 zapt->attributeList[i]->attributeSet = 0;
193 attrid.proto = PROTO_Z3950;
194 attrid.oclass = CLASS_ATTSET;
195 attrid.value = attr_set[i];
197 zapt->attributeList[i]->attributeSet =
198 odr_oiddup (o, oid_getoidbyent (&attrid));
200 zapt->attributeList[i]->which = Z_AttributeValue_numeric;
201 zapt->attributeList[i]->value.numeric = &attr_tmp[2*i+1];
203 zapt->attributeList[i]->attributeValue = &attr_tmp[2*i+1];
208 zapt->attributeList = ODR_NULLVAL;
210 term->which = Z_Term_general;
211 term->u.general = term_octet;
212 term_octet->buf = odr_malloc (o, li->lex_len);
213 term_octet->size = term_octet->len = li->lex_len;
214 memcpy (term_octet->buf, li->lex_buf, li->lex_len);
218 static Z_Operand *rpn_simple (struct lex_info *li, ODR o, oid_proto proto,
219 int num_attr, int *attr_list,
224 zo = odr_malloc (o, sizeof(*zo));
225 switch (li->query_look)
228 zo->which = Z_Operand_APT;
229 if (!(zo->u.attributesPlusTerm =
230 rpn_term (li, o, proto, num_attr, attr_list, attr_set)))
238 zo->which = Z_Operand_resultSetId;
239 zo->u.resultSetId = odr_malloc (o, li->lex_len+1);
240 memcpy (zo->u.resultSetId, li->lex_buf, li->lex_len);
241 zo->u.resultSetId[li->lex_len] = '\0';
250 static Z_ProximityOperator *rpn_proximity (struct lex_info *li, ODR o)
252 Z_ProximityOperator *p = odr_malloc (o, sizeof(*p));
256 if (*li->lex_buf == '1')
258 p->exclusion = odr_malloc (o, sizeof(*p->exclusion));
261 else if (*li->lex_buf == '0')
263 p->exclusion = odr_malloc (o, sizeof(*p->exclusion));
271 p->distance = odr_malloc (o, sizeof(*p->distance));
272 *p->distance = atoi (li->lex_buf);
276 p->ordered = odr_malloc (o, sizeof(*p->ordered));
277 *p->ordered = atoi (li->lex_buf);
281 p->relationType = odr_malloc (o, sizeof(*p->relationType));
282 *p->relationType = atoi (li->lex_buf);
286 if (*li->lex_buf == 'k')
288 else if (*li->lex_buf == 'p')
291 p->which = atoi (li->lex_buf);
295 p->proximityUnitCode = odr_malloc (o, sizeof(*p->proximityUnitCode));
296 *p->proximityUnitCode = atoi (li->lex_buf);
301 static Z_Complex *rpn_complex (struct lex_info *li, ODR o, oid_proto proto,
302 int num_attr, int max_attr,
303 int *attr_list, oid_value *attr_set)
308 zc = odr_malloc (o, sizeof(*zc));
309 zo = odr_malloc (o, sizeof(*zo));
311 switch (li->query_look)
314 zo->which = Z_Operator_and;
315 zo->u.and = ODR_NULLVAL;
318 zo->which = Z_Operator_or;
319 zo->u.and = ODR_NULLVAL;
322 zo->which = Z_Operator_and_not;
323 zo->u.and = ODR_NULLVAL;
326 zo->which = Z_Operator_prox;
327 zo->u.prox = rpn_proximity (li, o);
336 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
340 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
346 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o,
348 int num_attr, int max_attr,
349 int *attr_list, oid_value *attr_set)
354 sz = odr_malloc (o, sizeof(*sz));
355 switch (li->query_look)
361 sz->which = Z_RPNStructure_complex;
362 if (!(sz->u.complex =
363 rpn_complex (li, o, proto, num_attr, max_attr, attr_list,
369 sz->which = Z_RPNStructure_simple;
371 rpn_simple (li, o, proto, num_attr, attr_list,
379 if (num_attr >= max_attr)
381 if (!(cp = strchr (li->lex_buf, '=')) ||
382 (cp-li->lex_buf) > li->lex_len)
384 attr_set[num_attr] = query_oid_getvalbyname (li);
387 if (!(cp = strchr (li->lex_buf, '=')))
393 attr_set[num_attr] = attr_set[num_attr-1];
395 attr_set[num_attr] = VAL_NONE;
397 attr_list[2*num_attr] = atoi (li->lex_buf);
398 attr_list[2*num_attr+1] = atoi (cp+1);
402 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
408 if (compare_term (li, "general", 0))
409 li->term_type = Z_Term_general;
410 else if (compare_term (li, "numeric", 0))
411 li->term_type = Z_Term_numeric;
412 else if (compare_term (li, "string", 0))
413 li->term_type = Z_Term_characterString;
414 else if (compare_term (li, "oid", 0))
415 li->term_type = Z_Term_oid;
416 else if (compare_term (li, "datetime", 0))
417 li->term_type = Z_Term_dateTime;
418 else if (compare_term (li, "null", 0))
419 li->term_type = Z_Term_null;
422 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
424 case 0: /* operator/operand expected! */
430 Z_RPNQuery *p_query_rpn_mk (ODR o, struct lex_info *li, oid_proto proto,
434 int attr_array[1024];
435 oid_value attr_set[512];
436 oid_value topSet = VAL_NONE;
439 zq = odr_malloc (o, sizeof(*zq));
441 if (li->query_look == 'r')
444 topSet = query_oid_getvalbyname (li);
445 if (topSet == VAL_NONE)
450 if (topSet == VAL_NONE)
451 topSet = p_query_dfset;
452 if (topSet == VAL_NONE)
455 oset.oclass = CLASS_ATTSET;
458 zq->attributeSetId = odr_oiddup (o, oid_getoidbyent (&oset));
460 if (!(zq->RPNStructure = rpn_structure (li, o, proto, 0, 512,
461 attr_array, attr_set)))
466 Z_RPNQuery *p_query_rpn (ODR o, oid_proto proto,
472 li.right_sep = "}\"";
473 li.escape_char = '@';
474 li.term_type = Z_Term_general;
476 return p_query_rpn_mk (o, &li, proto, qbuf);
479 Z_AttributesPlusTerm *p_query_scan_mk (struct lex_info *li,
480 ODR o, oid_proto proto,
481 Odr_oid **attributeSetP,
485 oid_value attr_set[512];
489 oid_value topSet = VAL_NONE;
493 if (li->query_look == 'r')
496 topSet = query_oid_getvalbyname (li);
500 if (topSet == VAL_NONE)
501 topSet = p_query_dfset;
502 if (topSet == VAL_NONE)
505 oset.oclass = CLASS_ATTSET;
508 *attributeSetP = odr_oiddup (o, oid_getoidbyent (&oset));
510 while (li->query_look == 'l')
515 if (num_attr >= max_attr)
518 if (!(cp = strchr (li->lex_buf, '=')) ||
519 (cp-li->lex_buf) > li->lex_len)
521 attr_set[num_attr] = query_oid_getvalbyname (li);
524 if (!(cp = strchr (li->lex_buf, '=')))
530 attr_set[num_attr] = attr_set[num_attr-1];
532 attr_set[num_attr] = VAL_NONE;
534 attr_list[2*num_attr] = atoi (li->lex_buf);
535 attr_list[2*num_attr+1] = atoi (cp+1);
541 return rpn_term (li, o, proto, num_attr, attr_list, attr_set);
544 Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,
545 Odr_oid **attributeSetP,
551 li.right_sep = "}\"";
552 li.escape_char = '@';
553 li.term_type = Z_Term_general;
556 return p_query_scan_mk (&li, o, proto, attributeSetP, qbuf);
559 int p_query_attset (const char *arg)
561 p_query_dfset = oid_getvalbyname (arg);
562 return (p_query_dfset == VAL_NONE) ? -1 : 0;