2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1996-03-15 11:03:46 adam
8 * Attribute set can be set globally for a query with the @attrset
9 * operator. The @attr operator has an optional attribute-set specifier
10 * that sets the attribute set locally.
12 * Revision 1.8 1996/01/02 11:46:56 quinn
13 * Changed 'operator' to 'roperator' to avoid C++ conflict.
15 * Revision 1.7 1995/09/29 17:12:36 quinn
18 * Revision 1.6 1995/09/27 15:03:03 quinn
19 * Modified function heads & prototypes.
21 * Revision 1.5 1995/06/15 12:31:02 quinn
22 * *** empty log message ***
24 * Revision 1.4 1995/06/15 07:45:19 quinn
27 * Revision 1.3 1995/06/14 11:06:35 adam
28 * Bug fix: Attributes wasn't interpreted correctly!
30 * Revision 1.2 1995/05/26 08:56:11 adam
31 * New function: p_query_scan.
33 * Revision 1.1 1995/05/22 15:31:49 adam
34 * New function, p_query_rpn, to convert from prefix (ascii) to rpn (asn).
47 static const char *query_buf;
48 static const char *query_lex_buf;
49 static int query_lex_len;
50 static int query_look = 0;
51 static char *left_sep = "{\"";
52 static char *right_sep = "}\"";
53 static int escape_char = '@';
55 static Z_RPNStructure *rpn_structure (ODR o, oid_proto,
56 int num_attr, int max_attr,
57 int *attr_list, oid_value *attr_set);
59 static int query_oid_getvalbyname (void)
63 if (query_lex_len > 31)
65 memcpy (buf, query_lex_buf, query_lex_len);
66 buf[query_lex_len] = '\0';
67 return oid_getvalbyname (buf);
70 static int query_token (const char **qptr, const char **lex_buf, int *lex_len)
72 const char *sep_match;
79 if ((sep_match = strchr (left_sep, **qptr)))
81 int sep_index = sep_match - left_sep;
85 while (**qptr && **qptr != right_sep[sep_index])
96 while (**qptr && **qptr != ' ')
102 if (*lex_len >= 1 && (*lex_buf)[0] == escape_char)
104 if (*lex_len == 4 && !memcmp (*lex_buf+1, "and", 3))
106 if (*lex_len == 3 && !memcmp (*lex_buf+1, "or", 2))
108 if (*lex_len == 4 && !memcmp (*lex_buf+1, "not", 3))
110 if (*lex_len == 5 && !memcmp (*lex_buf+1, "attr", 4))
112 if (*lex_len == 4 && !memcmp (*lex_buf+1, "set", 3))
114 if (*lex_len == 8 && !memcmp (*lex_buf+1, "attrset", 7))
120 static int lex (void)
123 query_token (&query_buf, &query_lex_buf, &query_lex_len);
126 static Z_AttributesPlusTerm *rpn_term (ODR o, oid_proto proto,
127 int num_attr, int *attr_list,
130 Z_AttributesPlusTerm *zapt;
134 zapt = odr_malloc (o, sizeof(*zapt));
135 term_octet = odr_malloc (o, sizeof(*term_octet));
136 term = odr_malloc (o, sizeof(*term));
138 zapt->num_attributes = num_attr;
144 zapt->attributeList = odr_malloc (o, num_attr *
145 sizeof(*zapt->attributeList));
147 attr_tmp = odr_malloc (o, num_attr * 2 * sizeof(int));
148 memcpy (attr_tmp, attr_list, num_attr * 2 * sizeof(int));
149 for (i = 0; i < num_attr; i++)
151 zapt->attributeList[i] =
152 odr_malloc (o,sizeof(**zapt->attributeList));
153 zapt->attributeList[i]->attributeType = &attr_tmp[2*i];
155 if (attr_set[i] == VAL_NONE)
156 zapt->attributeList[i]->attributeSet = 0;
161 attrid.proto = PROTO_Z3950;
162 attrid.oclass = CLASS_ATTSET;
163 attrid.value = attr_set[i];
165 zapt->attributeList[i]->attributeSet =
166 odr_oiddup (o, oid_getoidbyent (&attrid));
168 zapt->attributeList[i]->which = Z_AttributeValue_numeric;
169 zapt->attributeList[i]->value.numeric = &attr_tmp[2*i+1];
171 zapt->attributeList[i]->attributeValue = &attr_tmp[2*i+1];
176 zapt->attributeList = ODR_NULLVAL;
178 term->which = Z_Term_general;
179 term->u.general = term_octet;
180 term_octet->buf = odr_malloc (o, query_lex_len);
181 term_octet->size = term_octet->len = query_lex_len;
182 memcpy (term_octet->buf, query_lex_buf, query_lex_len);
186 static Z_Operand *rpn_simple (ODR o, oid_proto proto,
187 int num_attr, int *attr_list,
192 zo = odr_malloc (o, sizeof(*zo));
196 zo->which = Z_Operand_APT;
197 if (!(zo->u.attributesPlusTerm =
198 rpn_term (o, proto, num_attr, attr_list, attr_set)))
206 zo->which = Z_Operand_resultSetId;
207 zo->u.resultSetId = odr_malloc (o, query_lex_len+1);
208 memcpy (zo->u.resultSetId, query_lex_buf, query_lex_len);
209 zo->u.resultSetId[query_lex_len] = '\0';
218 static Z_Complex *rpn_complex (ODR o, oid_proto proto,
219 int num_attr, int max_attr,
220 int *attr_list, oid_value *attr_set)
225 zc = odr_malloc (o, sizeof(*zc));
226 zo = odr_malloc (o, sizeof(*zo));
231 zo->which = Z_Operator_and;
232 zo->u.and = ODR_NULLVAL;
235 zo->which = Z_Operator_or;
236 zo->u.and = ODR_NULLVAL;
239 zo->which = Z_Operator_and_not;
240 zo->u.and = ODR_NULLVAL;
247 rpn_structure (o, proto, num_attr, max_attr, attr_list, attr_set)))
250 rpn_structure (o, proto, num_attr, max_attr, attr_list, attr_set)))
255 static Z_RPNStructure *rpn_structure (ODR o, oid_proto proto,
256 int num_attr, int max_attr,
257 int *attr_list, oid_value *attr_set)
262 sz = odr_malloc (o, sizeof(*sz));
268 sz->which = Z_RPNStructure_complex;
269 if (!(sz->u.complex =
270 rpn_complex (o, proto, num_attr, max_attr, attr_list, attr_set)))
275 sz->which = Z_RPNStructure_simple;
277 rpn_simple (o, proto, num_attr, attr_list, attr_set)))
284 if (num_attr >= max_attr)
286 if (!(cp = strchr (query_lex_buf, '=')) ||
287 (cp-query_lex_buf) > query_lex_len)
289 attr_set[num_attr] = query_oid_getvalbyname ();
292 if (!(cp = strchr (query_lex_buf, '=')))
298 attr_set[num_attr] = attr_set[num_attr-1];
300 attr_set[num_attr] = VAL_NONE;
302 attr_list[2*num_attr] = atoi (query_lex_buf);
303 attr_list[2*num_attr+1] = atoi (cp+1);
307 rpn_structure (o, proto, num_attr, max_attr, attr_list, attr_set);
308 case 0: /* operator/operand expected! */
314 Z_RPNQuery *p_query_rpn (ODR o, oid_proto proto, const char *qbuf)
317 int attr_array[1024];
318 oid_value attr_set[512];
319 oid_value topSet = VAL_NONE;
323 zq = odr_malloc (o, sizeof(*zq));
325 if (query_look == 'r')
328 topSet = query_oid_getvalbyname ();
329 if (topSet == VAL_NONE)
334 if (topSet == VAL_NONE)
337 oset.oclass = CLASS_ATTSET;
340 zq->attributeSetId = odr_oiddup (o, oid_getoidbyent (&oset));
342 if (!(zq->RPNStructure = rpn_structure (o, proto, 0, 512,
343 attr_array, attr_set)))
348 Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,
349 Odr_oid **attributeSetP,
353 oid_value attr_set[512];
357 oid_value topSet = VAL_NONE;
363 if (query_look == 'r')
366 topSet = query_oid_getvalbyname ();
370 if (topSet == VAL_NONE)
373 oset.oclass = CLASS_ATTSET;
376 *attributeSetP = odr_oiddup (o, oid_getoidbyent (&oset));
378 while (query_look == 'l')
383 if (num_attr >= max_attr)
386 if (!(cp = strchr (query_lex_buf, '=')) ||
387 (cp-query_lex_buf) > query_lex_len)
389 attr_set[num_attr] = query_oid_getvalbyname ();
392 if (!(cp = strchr (query_lex_buf, '=')))
398 attr_set[num_attr] = attr_set[num_attr-1];
400 attr_set[num_attr] = VAL_NONE;
402 attr_list[2*num_attr] = atoi (query_lex_buf);
403 attr_list[2*num_attr+1] = atoi (cp+1);
409 return rpn_term (o, proto, num_attr, attr_list, attr_set);