2 * Copyright (c) 1995-1996, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 1997-09-01 08:54:13 adam
8 * New windows NT/95 port using MSV5.0. Made prefix query handling
9 * thread safe. The function options ignores empty arguments when met.
11 * Revision 1.11 1996/11/11 13:15:29 adam
12 * Added proximity operator.
14 * Revision 1.10 1996/08/12 14:10:35 adam
15 * New function p_query_attset to define default attribute set.
17 * Revision 1.9 1996/03/15 11:03:46 adam
18 * Attribute set can be set globally for a query with the @attrset
19 * operator. The @attr operator has an optional attribute-set specifier
20 * that sets the attribute set locally.
22 * Revision 1.8 1996/01/02 11:46:56 quinn
23 * Changed 'operator' to 'roperator' to avoid C++ conflict.
25 * Revision 1.7 1995/09/29 17:12:36 quinn
28 * Revision 1.6 1995/09/27 15:03:03 quinn
29 * Modified function heads & prototypes.
31 * Revision 1.5 1995/06/15 12:31:02 quinn
32 * *** empty log message ***
34 * Revision 1.4 1995/06/15 07:45:19 quinn
37 * Revision 1.3 1995/06/14 11:06:35 adam
38 * Bug fix: Attributes wasn't interpreted correctly!
40 * Revision 1.2 1995/05/26 08:56:11 adam
41 * New function: p_query_scan.
43 * Revision 1.1 1995/05/22 15:31:49 adam
44 * New function, p_query_rpn, to convert from prefix (ascii) to rpn (asn).
57 static oid_value p_query_dfset = VAL_NONE;
60 const char *query_buf;
69 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o, oid_proto,
70 int num_attr, int max_attr,
71 int *attr_list, oid_value *attr_set);
73 static int query_oid_getvalbyname (struct lex_info *li)
79 memcpy (buf, li->lex_buf, li->lex_len);
80 buf[li->lex_len] = '\0';
81 return oid_getvalbyname (buf);
84 static int query_token (struct lex_info *li)
86 const char *sep_match;
87 const char **qptr = &li->query_buf;
94 if ((sep_match = strchr (li->left_sep, **qptr)))
96 int sep_index = sep_match - li->left_sep;
100 while (**qptr && **qptr != li->right_sep[sep_index])
111 while (**qptr && **qptr != ' ')
117 if (li->lex_len >= 1 && li->lex_buf[0] == li->escape_char)
119 if (li->lex_len == 4 && !memcmp (li->lex_buf+1, "and", 3))
121 if (li->lex_len == 3 && !memcmp (li->lex_buf+1, "or", 2))
123 if (li->lex_len == 4 && !memcmp (li->lex_buf+1, "not", 3))
125 if (li->lex_len == 5 && !memcmp (li->lex_buf+1, "attr", 4))
127 if (li->lex_len == 4 && !memcmp (li->lex_buf+1, "set", 3))
129 if (li->lex_len == 8 && !memcmp (li->lex_buf+1, "attrset", 7))
131 if (li->lex_len == 5 && !memcmp (li->lex_buf+1, "prox", 4))
137 static int lex (struct lex_info *li)
139 return li->query_look = query_token (li);
142 static Z_AttributesPlusTerm *rpn_term (struct lex_info *li, ODR o,
144 int num_attr, int *attr_list,
147 Z_AttributesPlusTerm *zapt;
151 zapt = odr_malloc (o, sizeof(*zapt));
152 term_octet = odr_malloc (o, sizeof(*term_octet));
153 term = odr_malloc (o, sizeof(*term));
155 zapt->num_attributes = num_attr;
161 zapt->attributeList = odr_malloc (o, num_attr *
162 sizeof(*zapt->attributeList));
164 attr_tmp = odr_malloc (o, num_attr * 2 * sizeof(int));
165 memcpy (attr_tmp, attr_list, num_attr * 2 * sizeof(int));
166 for (i = 0; i < num_attr; i++)
168 zapt->attributeList[i] =
169 odr_malloc (o,sizeof(**zapt->attributeList));
170 zapt->attributeList[i]->attributeType = &attr_tmp[2*i];
172 if (attr_set[i] == VAL_NONE)
173 zapt->attributeList[i]->attributeSet = 0;
178 attrid.proto = PROTO_Z3950;
179 attrid.oclass = CLASS_ATTSET;
180 attrid.value = attr_set[i];
182 zapt->attributeList[i]->attributeSet =
183 odr_oiddup (o, oid_getoidbyent (&attrid));
185 zapt->attributeList[i]->which = Z_AttributeValue_numeric;
186 zapt->attributeList[i]->value.numeric = &attr_tmp[2*i+1];
188 zapt->attributeList[i]->attributeValue = &attr_tmp[2*i+1];
193 zapt->attributeList = ODR_NULLVAL;
195 term->which = Z_Term_general;
196 term->u.general = term_octet;
197 term_octet->buf = odr_malloc (o, li->lex_len);
198 term_octet->size = term_octet->len = li->lex_len;
199 memcpy (term_octet->buf, li->lex_buf, li->lex_len);
203 static Z_Operand *rpn_simple (struct lex_info *li, ODR o, oid_proto proto,
204 int num_attr, int *attr_list,
209 zo = odr_malloc (o, sizeof(*zo));
210 switch (li->query_look)
213 zo->which = Z_Operand_APT;
214 if (!(zo->u.attributesPlusTerm =
215 rpn_term (li, o, proto, num_attr, attr_list, attr_set)))
223 zo->which = Z_Operand_resultSetId;
224 zo->u.resultSetId = odr_malloc (o, li->lex_len+1);
225 memcpy (zo->u.resultSetId, li->lex_buf, li->lex_len);
226 zo->u.resultSetId[li->lex_len] = '\0';
235 static Z_ProximityOperator *rpn_proximity (struct lex_info *li, ODR o)
237 Z_ProximityOperator *p = odr_malloc (o, sizeof(*p));
241 if (*li->lex_buf == '1')
243 p->exclusion = odr_malloc (o, sizeof(*p->exclusion));
246 else if (*li->lex_buf == '0')
248 p->exclusion = odr_malloc (o, sizeof(*p->exclusion));
256 p->distance = odr_malloc (o, sizeof(*p->distance));
257 *p->distance = atoi (li->lex_buf);
261 p->ordered = odr_malloc (o, sizeof(*p->ordered));
262 *p->ordered = atoi (li->lex_buf);
266 p->relationType = odr_malloc (o, sizeof(*p->relationType));
267 *p->relationType = atoi (li->lex_buf);
271 if (*li->lex_buf == 'k')
273 else if (*li->lex_buf == 'p')
276 p->which = atoi (li->lex_buf);
280 p->proximityUnitCode = odr_malloc (o, sizeof(*p->proximityUnitCode));
281 *p->proximityUnitCode = atoi (li->lex_buf);
286 static Z_Complex *rpn_complex (struct lex_info *li, ODR o, oid_proto proto,
287 int num_attr, int max_attr,
288 int *attr_list, oid_value *attr_set)
293 zc = odr_malloc (o, sizeof(*zc));
294 zo = odr_malloc (o, sizeof(*zo));
296 switch (li->query_look)
299 zo->which = Z_Operator_and;
300 zo->u.and = ODR_NULLVAL;
303 zo->which = Z_Operator_or;
304 zo->u.and = ODR_NULLVAL;
307 zo->which = Z_Operator_and_not;
308 zo->u.and = ODR_NULLVAL;
311 zo->which = Z_Operator_prox;
312 zo->u.prox = rpn_proximity (li, o);
321 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
325 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
331 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o,
333 int num_attr, int max_attr,
334 int *attr_list, oid_value *attr_set)
339 sz = odr_malloc (o, sizeof(*sz));
340 switch (li->query_look)
346 sz->which = Z_RPNStructure_complex;
347 if (!(sz->u.complex =
348 rpn_complex (li, o, proto, num_attr, max_attr, attr_list,
354 sz->which = Z_RPNStructure_simple;
356 rpn_simple (li, o, proto, num_attr, attr_list,
364 if (num_attr >= max_attr)
366 if (!(cp = strchr (li->lex_buf, '=')) ||
367 (cp-li->lex_buf) > li->lex_len)
369 attr_set[num_attr] = query_oid_getvalbyname (li);
372 if (!(cp = strchr (li->lex_buf, '=')))
378 attr_set[num_attr] = attr_set[num_attr-1];
380 attr_set[num_attr] = VAL_NONE;
382 attr_list[2*num_attr] = atoi (li->lex_buf);
383 attr_list[2*num_attr+1] = atoi (cp+1);
387 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
389 case 0: /* operator/operand expected! */
395 Z_RPNQuery *p_query_rpn_mk (ODR o, struct lex_info *li, oid_proto proto,
399 int attr_array[1024];
400 oid_value attr_set[512];
401 oid_value topSet = VAL_NONE;
404 zq = odr_malloc (o, sizeof(*zq));
406 if (li->query_look == 'r')
409 topSet = query_oid_getvalbyname (li);
410 if (topSet == VAL_NONE)
415 if (topSet == VAL_NONE)
416 topSet = p_query_dfset;
417 if (topSet == VAL_NONE)
420 oset.oclass = CLASS_ATTSET;
423 zq->attributeSetId = odr_oiddup (o, oid_getoidbyent (&oset));
425 if (!(zq->RPNStructure = rpn_structure (li, o, proto, 0, 512,
426 attr_array, attr_set)))
431 Z_RPNQuery *p_query_rpn (ODR o, oid_proto proto,
437 li.right_sep = "}\"";
438 li.escape_char = '@';
440 return p_query_rpn_mk (o, &li, proto, qbuf);
443 Z_AttributesPlusTerm *p_query_scan_mk (struct lex_info *li,
444 ODR o, oid_proto proto,
445 Odr_oid **attributeSetP,
449 oid_value attr_set[512];
453 oid_value topSet = VAL_NONE;
457 if (li->query_look == 'r')
460 topSet = query_oid_getvalbyname (li);
464 if (topSet == VAL_NONE)
465 topSet = p_query_dfset;
466 if (topSet == VAL_NONE)
469 oset.oclass = CLASS_ATTSET;
472 *attributeSetP = odr_oiddup (o, oid_getoidbyent (&oset));
474 while (li->query_look == 'l')
479 if (num_attr >= max_attr)
482 if (!(cp = strchr (li->lex_buf, '=')) ||
483 (cp-li->lex_buf) > li->lex_len)
485 attr_set[num_attr] = query_oid_getvalbyname (li);
488 if (!(cp = strchr (li->lex_buf, '=')))
494 attr_set[num_attr] = attr_set[num_attr-1];
496 attr_set[num_attr] = VAL_NONE;
498 attr_list[2*num_attr] = atoi (li->lex_buf);
499 attr_list[2*num_attr+1] = atoi (cp+1);
505 return rpn_term (li, o, proto, num_attr, attr_list, attr_set);
508 Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,
509 Odr_oid **attributeSetP,
515 li.right_sep = "}\"";
516 li.escape_char = '@';
519 return p_query_scan_mk (&li, o, proto, attributeSetP, qbuf);
522 int p_query_attset (const char *arg)
524 p_query_dfset = oid_getvalbyname (arg);
525 return (p_query_dfset == VAL_NONE) ? -1 : 0;