2 * Copyright (c) 1995-1996, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1996-11-11 13:15:29 adam
8 * Added proximity operator.
10 * Revision 1.10 1996/08/12 14:10:35 adam
11 * New function p_query_attset to define default attribute set.
13 * Revision 1.9 1996/03/15 11:03:46 adam
14 * Attribute set can be set globally for a query with the @attrset
15 * operator. The @attr operator has an optional attribute-set specifier
16 * that sets the attribute set locally.
18 * Revision 1.8 1996/01/02 11:46:56 quinn
19 * Changed 'operator' to 'roperator' to avoid C++ conflict.
21 * Revision 1.7 1995/09/29 17:12:36 quinn
24 * Revision 1.6 1995/09/27 15:03:03 quinn
25 * Modified function heads & prototypes.
27 * Revision 1.5 1995/06/15 12:31:02 quinn
28 * *** empty log message ***
30 * Revision 1.4 1995/06/15 07:45:19 quinn
33 * Revision 1.3 1995/06/14 11:06:35 adam
34 * Bug fix: Attributes wasn't interpreted correctly!
36 * Revision 1.2 1995/05/26 08:56:11 adam
37 * New function: p_query_scan.
39 * Revision 1.1 1995/05/22 15:31:49 adam
40 * New function, p_query_rpn, to convert from prefix (ascii) to rpn (asn).
53 static oid_value p_query_dfset = VAL_NONE;
55 static const char *query_buf;
56 static const char *query_lex_buf;
57 static int query_lex_len;
58 static int query_look = 0;
59 static char *left_sep = "{\"";
60 static char *right_sep = "}\"";
61 static int escape_char = '@';
63 static Z_RPNStructure *rpn_structure (ODR o, oid_proto,
64 int num_attr, int max_attr,
65 int *attr_list, oid_value *attr_set);
67 static int query_oid_getvalbyname (void)
71 if (query_lex_len > 31)
73 memcpy (buf, query_lex_buf, query_lex_len);
74 buf[query_lex_len] = '\0';
75 return oid_getvalbyname (buf);
78 static int query_token (const char **qptr, const char **lex_buf, int *lex_len)
80 const char *sep_match;
87 if ((sep_match = strchr (left_sep, **qptr)))
89 int sep_index = sep_match - left_sep;
93 while (**qptr && **qptr != right_sep[sep_index])
104 while (**qptr && **qptr != ' ')
110 if (*lex_len >= 1 && (*lex_buf)[0] == escape_char)
112 if (*lex_len == 4 && !memcmp (*lex_buf+1, "and", 3))
114 if (*lex_len == 3 && !memcmp (*lex_buf+1, "or", 2))
116 if (*lex_len == 4 && !memcmp (*lex_buf+1, "not", 3))
118 if (*lex_len == 5 && !memcmp (*lex_buf+1, "attr", 4))
120 if (*lex_len == 4 && !memcmp (*lex_buf+1, "set", 3))
122 if (*lex_len == 8 && !memcmp (*lex_buf+1, "attrset", 7))
124 if (*lex_len == 5 && !memcmp (*lex_buf+1, "prox", 4))
130 static int lex (void)
133 query_token (&query_buf, &query_lex_buf, &query_lex_len);
136 static Z_AttributesPlusTerm *rpn_term (ODR o, oid_proto proto,
137 int num_attr, int *attr_list,
140 Z_AttributesPlusTerm *zapt;
144 zapt = odr_malloc (o, sizeof(*zapt));
145 term_octet = odr_malloc (o, sizeof(*term_octet));
146 term = odr_malloc (o, sizeof(*term));
148 zapt->num_attributes = num_attr;
154 zapt->attributeList = odr_malloc (o, num_attr *
155 sizeof(*zapt->attributeList));
157 attr_tmp = odr_malloc (o, num_attr * 2 * sizeof(int));
158 memcpy (attr_tmp, attr_list, num_attr * 2 * sizeof(int));
159 for (i = 0; i < num_attr; i++)
161 zapt->attributeList[i] =
162 odr_malloc (o,sizeof(**zapt->attributeList));
163 zapt->attributeList[i]->attributeType = &attr_tmp[2*i];
165 if (attr_set[i] == VAL_NONE)
166 zapt->attributeList[i]->attributeSet = 0;
171 attrid.proto = PROTO_Z3950;
172 attrid.oclass = CLASS_ATTSET;
173 attrid.value = attr_set[i];
175 zapt->attributeList[i]->attributeSet =
176 odr_oiddup (o, oid_getoidbyent (&attrid));
178 zapt->attributeList[i]->which = Z_AttributeValue_numeric;
179 zapt->attributeList[i]->value.numeric = &attr_tmp[2*i+1];
181 zapt->attributeList[i]->attributeValue = &attr_tmp[2*i+1];
186 zapt->attributeList = ODR_NULLVAL;
188 term->which = Z_Term_general;
189 term->u.general = term_octet;
190 term_octet->buf = odr_malloc (o, query_lex_len);
191 term_octet->size = term_octet->len = query_lex_len;
192 memcpy (term_octet->buf, query_lex_buf, query_lex_len);
196 static Z_Operand *rpn_simple (ODR o, oid_proto proto,
197 int num_attr, int *attr_list,
202 zo = odr_malloc (o, sizeof(*zo));
206 zo->which = Z_Operand_APT;
207 if (!(zo->u.attributesPlusTerm =
208 rpn_term (o, proto, num_attr, attr_list, attr_set)))
216 zo->which = Z_Operand_resultSetId;
217 zo->u.resultSetId = odr_malloc (o, query_lex_len+1);
218 memcpy (zo->u.resultSetId, query_lex_buf, query_lex_len);
219 zo->u.resultSetId[query_lex_len] = '\0';
228 static Z_ProximityOperator *rpn_proximity (ODR o)
230 Z_ProximityOperator *p = odr_malloc (o, sizeof(*p));
234 if (*query_lex_buf == '1')
236 p->exclusion = odr_malloc (o, sizeof(*p->exclusion));
239 else if (*query_lex_buf == '0')
241 p->exclusion = odr_malloc (o, sizeof(*p->exclusion));
249 p->distance = odr_malloc (o, sizeof(*p->distance));
250 *p->distance = atoi (query_lex_buf);
254 p->ordered = odr_malloc (o, sizeof(*p->ordered));
255 *p->ordered = atoi (query_lex_buf);
259 p->relationType = odr_malloc (o, sizeof(*p->relationType));
260 *p->relationType = atoi (query_lex_buf);
264 if (*query_lex_buf == 'k')
266 else if (*query_lex_buf == 'p')
269 p->which = atoi (query_lex_buf);
273 p->proximityUnitCode = odr_malloc (o, sizeof(*p->proximityUnitCode));
274 *p->proximityUnitCode = atoi (query_lex_buf);
279 static Z_Complex *rpn_complex (ODR o, oid_proto proto,
280 int num_attr, int max_attr,
281 int *attr_list, oid_value *attr_set)
286 zc = odr_malloc (o, sizeof(*zc));
287 zo = odr_malloc (o, sizeof(*zo));
292 zo->which = Z_Operator_and;
293 zo->u.and = ODR_NULLVAL;
296 zo->which = Z_Operator_or;
297 zo->u.and = ODR_NULLVAL;
300 zo->which = Z_Operator_and_not;
301 zo->u.and = ODR_NULLVAL;
304 zo->which = Z_Operator_prox;
305 zo->u.prox = rpn_proximity (o);
314 rpn_structure (o, proto, num_attr, max_attr, attr_list, attr_set)))
317 rpn_structure (o, proto, num_attr, max_attr, attr_list, attr_set)))
322 static Z_RPNStructure *rpn_structure (ODR o, oid_proto proto,
323 int num_attr, int max_attr,
324 int *attr_list, oid_value *attr_set)
329 sz = odr_malloc (o, sizeof(*sz));
336 sz->which = Z_RPNStructure_complex;
337 if (!(sz->u.complex =
338 rpn_complex (o, proto, num_attr, max_attr, attr_list, attr_set)))
343 sz->which = Z_RPNStructure_simple;
345 rpn_simple (o, proto, num_attr, attr_list, attr_set)))
352 if (num_attr >= max_attr)
354 if (!(cp = strchr (query_lex_buf, '=')) ||
355 (cp-query_lex_buf) > query_lex_len)
357 attr_set[num_attr] = query_oid_getvalbyname ();
360 if (!(cp = strchr (query_lex_buf, '=')))
366 attr_set[num_attr] = attr_set[num_attr-1];
368 attr_set[num_attr] = VAL_NONE;
370 attr_list[2*num_attr] = atoi (query_lex_buf);
371 attr_list[2*num_attr+1] = atoi (cp+1);
375 rpn_structure (o, proto, num_attr, max_attr, attr_list, attr_set);
376 case 0: /* operator/operand expected! */
382 Z_RPNQuery *p_query_rpn (ODR o, oid_proto proto, const char *qbuf)
385 int attr_array[1024];
386 oid_value attr_set[512];
387 oid_value topSet = VAL_NONE;
391 zq = odr_malloc (o, sizeof(*zq));
393 if (query_look == 'r')
396 topSet = query_oid_getvalbyname ();
397 if (topSet == VAL_NONE)
402 if (topSet == VAL_NONE)
403 topSet = p_query_dfset;
404 if (topSet == VAL_NONE)
407 oset.oclass = CLASS_ATTSET;
410 zq->attributeSetId = odr_oiddup (o, oid_getoidbyent (&oset));
412 if (!(zq->RPNStructure = rpn_structure (o, proto, 0, 512,
413 attr_array, attr_set)))
418 Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,
419 Odr_oid **attributeSetP,
423 oid_value attr_set[512];
427 oid_value topSet = VAL_NONE;
433 if (query_look == 'r')
436 topSet = query_oid_getvalbyname ();
440 if (topSet == VAL_NONE)
441 topSet = p_query_dfset;
442 if (topSet == VAL_NONE)
445 oset.oclass = CLASS_ATTSET;
448 *attributeSetP = odr_oiddup (o, oid_getoidbyent (&oset));
450 while (query_look == 'l')
455 if (num_attr >= max_attr)
458 if (!(cp = strchr (query_lex_buf, '=')) ||
459 (cp-query_lex_buf) > query_lex_len)
461 attr_set[num_attr] = query_oid_getvalbyname ();
464 if (!(cp = strchr (query_lex_buf, '=')))
470 attr_set[num_attr] = attr_set[num_attr-1];
472 attr_set[num_attr] = VAL_NONE;
474 attr_list[2*num_attr] = atoi (query_lex_buf);
475 attr_list[2*num_attr+1] = atoi (cp+1);
481 return rpn_term (o, proto, num_attr, attr_list, attr_set);
484 int p_query_attset (const char *arg)
486 p_query_dfset = oid_getvalbyname (arg);
487 return (p_query_dfset == VAL_NONE) ? -1 : 0;