2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1996-01-02 11:46:56 quinn
8 * Changed 'operator' to 'roperator' to avoid C++ conflict.
10 * Revision 1.7 1995/09/29 17:12:36 quinn
13 * Revision 1.6 1995/09/27 15:03:03 quinn
14 * Modified function heads & prototypes.
16 * Revision 1.5 1995/06/15 12:31:02 quinn
17 * *** empty log message ***
19 * Revision 1.4 1995/06/15 07:45:19 quinn
22 * Revision 1.3 1995/06/14 11:06:35 adam
23 * Bug fix: Attributes wasn't interpreted correctly!
25 * Revision 1.2 1995/05/26 08:56:11 adam
26 * New function: p_query_scan.
28 * Revision 1.1 1995/05/22 15:31:49 adam
29 * New function, p_query_rpn, to convert from prefix (ascii) to rpn (asn).
42 static const char *query_buf;
43 static const char *query_lex_buf;
44 static int query_lex_len;
45 static int query_look = 0;
46 static char *left_sep = "{\"";
47 static char *right_sep = "}\"";
48 static int escape_char = '@';
50 static Z_RPNStructure *rpn_structure (ODR o, int num_attr, int max_attr,
53 static int query_token (const char **qptr, const char **lex_buf, int *lex_len)
55 const char *sep_match;
62 if ((sep_match = strchr (left_sep, **qptr)))
64 int sep_index = sep_match - left_sep;
68 while (**qptr && **qptr != right_sep[sep_index])
79 while (**qptr && **qptr != ' ')
85 if (*lex_len >= 1 && (*lex_buf)[0] == escape_char)
87 if (*lex_len == 4 && !memcmp (*lex_buf+1, "and", 3))
89 if (*lex_len == 3 && !memcmp (*lex_buf+1, "or", 2))
91 if (*lex_len == 4 && !memcmp (*lex_buf+1, "not", 3))
93 if (*lex_len == 5 && !memcmp (*lex_buf+1, "attr", 4))
95 if (*lex_len == 4 && !memcmp (*lex_buf+1, "set", 3))
101 static int lex (void)
104 query_token (&query_buf, &query_lex_buf, &query_lex_len);
107 static Z_AttributesPlusTerm *rpn_term (ODR o, int num_attr, int *attr_list)
109 Z_AttributesPlusTerm *zapt;
113 zapt = odr_malloc (o, sizeof(*zapt));
114 term_octet = odr_malloc (o, sizeof(*term_octet));
115 term = odr_malloc (o, sizeof(*term));
117 zapt->num_attributes = num_attr;
123 zapt->attributeList = odr_malloc (o, num_attr *
124 sizeof(*zapt->attributeList));
126 attr_tmp = odr_malloc (o, num_attr * 2 * sizeof(int));
127 memcpy (attr_tmp, attr_list, num_attr * 2 * sizeof(int));
128 for (i = 0; i < num_attr; i++)
130 zapt->attributeList[i] =
131 odr_malloc (o,sizeof(**zapt->attributeList));
132 zapt->attributeList[i]->attributeType = &attr_tmp[2*i];
134 zapt->attributeList[i]->attributeSet = 0;
135 zapt->attributeList[i]->which = Z_AttributeValue_numeric;
136 zapt->attributeList[i]->value.numeric = &attr_tmp[2*i+1];
138 zapt->attributeList[i]->attributeValue = &attr_tmp[2*i+1];
143 zapt->attributeList = ODR_NULLVAL;
145 term->which = Z_Term_general;
146 term->u.general = term_octet;
147 term_octet->buf = odr_malloc (o, query_lex_len);
148 term_octet->size = term_octet->len = query_lex_len;
149 memcpy (term_octet->buf, query_lex_buf, query_lex_len);
153 static Z_Operand *rpn_simple (ODR o, int num_attr, int *attr_list)
157 zo = odr_malloc (o, sizeof(*zo));
161 zo->which = Z_Operand_APT;
162 if (!(zo->u.attributesPlusTerm = rpn_term (o, num_attr, attr_list)))
170 zo->which = Z_Operand_resultSetId;
171 zo->u.resultSetId = odr_malloc (o, query_lex_len+1);
172 memcpy (zo->u.resultSetId, query_lex_buf, query_lex_len);
173 zo->u.resultSetId[query_lex_len] = '\0';
182 static Z_Complex *rpn_complex (ODR o, int num_attr, int max_attr,
188 zc = odr_malloc (o, sizeof(*zc));
189 zo = odr_malloc (o, sizeof(*zo));
194 zo->which = Z_Operator_and;
195 zo->u.and = ODR_NULLVAL;
198 zo->which = Z_Operator_or;
199 zo->u.and = ODR_NULLVAL;
202 zo->which = Z_Operator_and_not;
203 zo->u.and = ODR_NULLVAL;
209 if (!(zc->s1 = rpn_structure (o, num_attr, max_attr, attr_list)))
211 if (!(zc->s2 = rpn_structure (o, num_attr, max_attr, attr_list)))
216 static Z_RPNStructure *rpn_structure (ODR o, int num_attr, int max_attr,
222 sz = odr_malloc (o, sizeof(*sz));
228 sz->which = Z_RPNStructure_complex;
229 if (!(sz->u.complex = rpn_complex (o, num_attr, max_attr, attr_list)))
234 sz->which = Z_RPNStructure_simple;
235 if (!(sz->u.simple = rpn_simple (o, num_attr, attr_list)))
242 if (!(cp = strchr (query_lex_buf, '=')))
244 if (num_attr >= max_attr)
246 attr_list[2*num_attr] = atoi (query_lex_buf);
247 attr_list[2*num_attr+1] = atoi (cp+1);
250 return rpn_structure (o, num_attr, max_attr, attr_list);
251 case 0: /* operator/operand expected! */
257 Z_RPNQuery *p_query_rpn (ODR o, const char *qbuf)
260 int attr_array[1024];
263 zq = odr_malloc (o, sizeof(*zq));
264 zq->attributeSetId = NULL;
266 if (!(zq->RPNStructure = rpn_structure (o, 0, 512, attr_array)))
271 Z_AttributesPlusTerm *p_query_scan (ODR o, const char *qbuf)
284 if (!(cp = strchr (query_lex_buf, '=')))
286 if (num_attr >= max_attr)
288 attr_list[2*num_attr] = atoi (query_lex_buf);
289 attr_list[2*num_attr+1] = atoi (cp+1);
294 return rpn_term (o, num_attr, attr_list);