2 * Copyright (c) 1995-1997, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1997-09-22 12:33:41 adam
8 * Fixed bug introduced by previous commit.
10 * Revision 1.13 1997/09/17 12:10:42 adam
13 * Revision 1.12 1997/09/01 08:54:13 adam
14 * New windows NT/95 port using MSV5.0. Made prefix query handling
15 * thread safe. The function options ignores empty arguments when met.
17 * Revision 1.11 1996/11/11 13:15:29 adam
18 * Added proximity operator.
20 * Revision 1.10 1996/08/12 14:10:35 adam
21 * New function p_query_attset to define default attribute set.
23 * Revision 1.9 1996/03/15 11:03:46 adam
24 * Attribute set can be set globally for a query with the @attrset
25 * operator. The @attr operator has an optional attribute-set specifier
26 * that sets the attribute set locally.
28 * Revision 1.8 1996/01/02 11:46:56 quinn
29 * Changed 'operator' to 'roperator' to avoid C++ conflict.
31 * Revision 1.7 1995/09/29 17:12:36 quinn
34 * Revision 1.6 1995/09/27 15:03:03 quinn
35 * Modified function heads & prototypes.
37 * Revision 1.5 1995/06/15 12:31:02 quinn
38 * *** empty log message ***
40 * Revision 1.4 1995/06/15 07:45:19 quinn
43 * Revision 1.3 1995/06/14 11:06:35 adam
44 * Bug fix: Attributes wasn't interpreted correctly!
46 * Revision 1.2 1995/05/26 08:56:11 adam
47 * New function: p_query_scan.
49 * Revision 1.1 1995/05/22 15:31:49 adam
50 * New function, p_query_rpn, to convert from prefix (ascii) to rpn (asn).
63 static oid_value p_query_dfset = VAL_NONE;
66 const char *query_buf;
76 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o, oid_proto,
77 int num_attr, int max_attr,
78 int *attr_list, oid_value *attr_set);
80 static int query_oid_getvalbyname (struct lex_info *li)
86 memcpy (buf, li->lex_buf, li->lex_len);
87 buf[li->lex_len] = '\0';
88 return oid_getvalbyname (buf);
91 static int compare_term (struct lex_info *li, const char *src, int off)
93 size_t len=strlen(src);
95 if (li->lex_len == len+off && !memcmp (li->lex_buf+off, src, len-off))
100 static int query_token (struct lex_info *li)
102 const char *sep_match;
103 const char **qptr = &li->query_buf;
105 while (**qptr == ' ')
110 if ((sep_match = strchr (li->left_sep, **qptr)))
112 int sep_index = sep_match - li->left_sep;
116 while (**qptr && **qptr != li->right_sep[sep_index])
127 while (**qptr && **qptr != ' ')
133 if (li->lex_len >= 1 && li->lex_buf[0] == li->escape_char)
135 if (compare_term (li, "and", 1))
137 if (compare_term (li, "or", 1))
139 if (compare_term (li, "not", 1))
141 if (compare_term (li, "attr", 1))
143 if (compare_term (li, "set", 1))
145 if (compare_term (li, "attrset", 1))
147 if (compare_term (li, "prox", 1))
149 if (compare_term (li, "term", 1))
155 static int lex (struct lex_info *li)
157 return li->query_look = query_token (li);
160 static Z_AttributesPlusTerm *rpn_term (struct lex_info *li, ODR o,
162 int num_attr, int *attr_list,
165 Z_AttributesPlusTerm *zapt;
169 zapt = odr_malloc (o, sizeof(*zapt));
170 term_octet = odr_malloc (o, sizeof(*term_octet));
171 term = odr_malloc (o, sizeof(*term));
173 zapt->num_attributes = num_attr;
179 zapt->attributeList = odr_malloc (o, num_attr *
180 sizeof(*zapt->attributeList));
182 attr_tmp = odr_malloc (o, num_attr * 2 * sizeof(int));
183 memcpy (attr_tmp, attr_list, num_attr * 2 * sizeof(int));
184 for (i = 0; i < num_attr; i++)
186 zapt->attributeList[i] =
187 odr_malloc (o,sizeof(**zapt->attributeList));
188 zapt->attributeList[i]->attributeType = &attr_tmp[2*i];
190 if (attr_set[i] == VAL_NONE)
191 zapt->attributeList[i]->attributeSet = 0;
196 attrid.proto = PROTO_Z3950;
197 attrid.oclass = CLASS_ATTSET;
198 attrid.value = attr_set[i];
200 zapt->attributeList[i]->attributeSet =
201 odr_oiddup (o, oid_getoidbyent (&attrid));
203 zapt->attributeList[i]->which = Z_AttributeValue_numeric;
204 zapt->attributeList[i]->value.numeric = &attr_tmp[2*i+1];
206 zapt->attributeList[i]->attributeValue = &attr_tmp[2*i+1];
211 zapt->attributeList = ODR_NULLVAL;
213 term->which = Z_Term_general;
214 term->u.general = term_octet;
215 term_octet->buf = odr_malloc (o, li->lex_len);
216 term_octet->size = term_octet->len = li->lex_len;
217 memcpy (term_octet->buf, li->lex_buf, li->lex_len);
221 static Z_Operand *rpn_simple (struct lex_info *li, ODR o, oid_proto proto,
222 int num_attr, int *attr_list,
227 zo = odr_malloc (o, sizeof(*zo));
228 switch (li->query_look)
231 zo->which = Z_Operand_APT;
232 if (!(zo->u.attributesPlusTerm =
233 rpn_term (li, o, proto, num_attr, attr_list, attr_set)))
241 zo->which = Z_Operand_resultSetId;
242 zo->u.resultSetId = odr_malloc (o, li->lex_len+1);
243 memcpy (zo->u.resultSetId, li->lex_buf, li->lex_len);
244 zo->u.resultSetId[li->lex_len] = '\0';
253 static Z_ProximityOperator *rpn_proximity (struct lex_info *li, ODR o)
255 Z_ProximityOperator *p = odr_malloc (o, sizeof(*p));
259 if (*li->lex_buf == '1')
261 p->exclusion = odr_malloc (o, sizeof(*p->exclusion));
264 else if (*li->lex_buf == '0')
266 p->exclusion = odr_malloc (o, sizeof(*p->exclusion));
274 p->distance = odr_malloc (o, sizeof(*p->distance));
275 *p->distance = atoi (li->lex_buf);
279 p->ordered = odr_malloc (o, sizeof(*p->ordered));
280 *p->ordered = atoi (li->lex_buf);
284 p->relationType = odr_malloc (o, sizeof(*p->relationType));
285 *p->relationType = atoi (li->lex_buf);
289 if (*li->lex_buf == 'k')
291 else if (*li->lex_buf == 'p')
294 p->which = atoi (li->lex_buf);
298 p->proximityUnitCode = odr_malloc (o, sizeof(*p->proximityUnitCode));
299 *p->proximityUnitCode = atoi (li->lex_buf);
304 static Z_Complex *rpn_complex (struct lex_info *li, ODR o, oid_proto proto,
305 int num_attr, int max_attr,
306 int *attr_list, oid_value *attr_set)
311 zc = odr_malloc (o, sizeof(*zc));
312 zo = odr_malloc (o, sizeof(*zo));
314 switch (li->query_look)
317 zo->which = Z_Operator_and;
318 zo->u.and = ODR_NULLVAL;
321 zo->which = Z_Operator_or;
322 zo->u.and = ODR_NULLVAL;
325 zo->which = Z_Operator_and_not;
326 zo->u.and = ODR_NULLVAL;
329 zo->which = Z_Operator_prox;
330 zo->u.prox = rpn_proximity (li, o);
339 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
343 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
349 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o,
351 int num_attr, int max_attr,
352 int *attr_list, oid_value *attr_set)
357 sz = odr_malloc (o, sizeof(*sz));
358 switch (li->query_look)
364 sz->which = Z_RPNStructure_complex;
365 if (!(sz->u.complex =
366 rpn_complex (li, o, proto, num_attr, max_attr, attr_list,
372 sz->which = Z_RPNStructure_simple;
374 rpn_simple (li, o, proto, num_attr, attr_list,
382 if (num_attr >= max_attr)
384 if (!(cp = strchr (li->lex_buf, '=')) ||
385 (cp-li->lex_buf) > li->lex_len)
387 attr_set[num_attr] = query_oid_getvalbyname (li);
390 if (!(cp = strchr (li->lex_buf, '=')))
396 attr_set[num_attr] = attr_set[num_attr-1];
398 attr_set[num_attr] = VAL_NONE;
400 attr_list[2*num_attr] = atoi (li->lex_buf);
401 attr_list[2*num_attr+1] = atoi (cp+1);
405 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
411 if (compare_term (li, "general", 0))
412 li->term_type = Z_Term_general;
413 else if (compare_term (li, "numeric", 0))
414 li->term_type = Z_Term_numeric;
415 else if (compare_term (li, "string", 0))
416 li->term_type = Z_Term_characterString;
417 else if (compare_term (li, "oid", 0))
418 li->term_type = Z_Term_oid;
419 else if (compare_term (li, "datetime", 0))
420 li->term_type = Z_Term_dateTime;
421 else if (compare_term (li, "null", 0))
422 li->term_type = Z_Term_null;
425 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
427 case 0: /* operator/operand expected! */
433 Z_RPNQuery *p_query_rpn_mk (ODR o, struct lex_info *li, oid_proto proto,
437 int attr_array[1024];
438 oid_value attr_set[512];
439 oid_value topSet = VAL_NONE;
442 zq = odr_malloc (o, sizeof(*zq));
444 if (li->query_look == 'r')
447 topSet = query_oid_getvalbyname (li);
448 if (topSet == VAL_NONE)
453 if (topSet == VAL_NONE)
454 topSet = p_query_dfset;
455 if (topSet == VAL_NONE)
458 oset.oclass = CLASS_ATTSET;
461 zq->attributeSetId = odr_oiddup (o, oid_getoidbyent (&oset));
463 if (!(zq->RPNStructure = rpn_structure (li, o, proto, 0, 512,
464 attr_array, attr_set)))
469 Z_RPNQuery *p_query_rpn (ODR o, oid_proto proto,
475 li.right_sep = "}\"";
476 li.escape_char = '@';
477 li.term_type = Z_Term_general;
479 return p_query_rpn_mk (o, &li, proto, qbuf);
482 Z_AttributesPlusTerm *p_query_scan_mk (struct lex_info *li,
483 ODR o, oid_proto proto,
484 Odr_oid **attributeSetP,
488 oid_value attr_set[512];
492 oid_value topSet = VAL_NONE;
496 if (li->query_look == 'r')
499 topSet = query_oid_getvalbyname (li);
503 if (topSet == VAL_NONE)
504 topSet = p_query_dfset;
505 if (topSet == VAL_NONE)
508 oset.oclass = CLASS_ATTSET;
511 *attributeSetP = odr_oiddup (o, oid_getoidbyent (&oset));
513 while (li->query_look == 'l')
518 if (num_attr >= max_attr)
521 if (!(cp = strchr (li->lex_buf, '=')) ||
522 (cp-li->lex_buf) > li->lex_len)
524 attr_set[num_attr] = query_oid_getvalbyname (li);
527 if (!(cp = strchr (li->lex_buf, '=')))
533 attr_set[num_attr] = attr_set[num_attr-1];
535 attr_set[num_attr] = VAL_NONE;
537 attr_list[2*num_attr] = atoi (li->lex_buf);
538 attr_list[2*num_attr+1] = atoi (cp+1);
544 return rpn_term (li, o, proto, num_attr, attr_list, attr_set);
547 Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,
548 Odr_oid **attributeSetP,
554 li.right_sep = "}\"";
555 li.escape_char = '@';
556 li.term_type = Z_Term_general;
559 return p_query_scan_mk (&li, o, proto, attributeSetP, qbuf);
562 int p_query_attset (const char *arg)
564 p_query_dfset = oid_getvalbyname (arg);
565 return (p_query_dfset == VAL_NONE) ? -1 : 0;