2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.2 1995-05-26 08:56:11 adam
8 * New function: p_query_scan.
10 * Revision 1.1 1995/05/22 15:31:49 adam
11 * New function, p_query_rpn, to convert from prefix (ascii) to rpn (asn).
24 static const char *query_buf;
25 static const char *query_lex_buf;
26 static int query_lex_len;
27 static int query_look = 0;
28 static char *left_sep = "{\"";
29 static char *right_sep = "}\"";
30 static int escape_char = '@';
32 static Z_RPNStructure *rpn_structure (ODR o, int num_attr, int max_attr,
35 static int query_token (const char **qptr, const char **lex_buf, int *lex_len)
37 const char *sep_match;
44 if ((sep_match = strchr (left_sep, **qptr)))
46 int sep_index = sep_match - left_sep;
50 while (**qptr && **qptr != right_sep[sep_index])
61 while (**qptr && **qptr != ' ')
67 if (*lex_len >= 1 && (*lex_buf)[0] == escape_char)
69 if (*lex_len == 4 && !memcmp (*lex_buf+1, "and", 3))
71 if (*lex_len == 3 && !memcmp (*lex_buf+1, "or", 2))
73 if (*lex_len == 4 && !memcmp (*lex_buf+1, "not", 3))
75 if (*lex_len == 5 && !memcmp (*lex_buf+1, "attr", 4))
77 if (*lex_len == 4 && !memcmp (*lex_buf+1, "set", 3))
86 query_token (&query_buf, &query_lex_buf, &query_lex_len);
89 static Z_AttributesPlusTerm *rpn_term (ODR o, int num_attr, int *attr_list)
91 Z_AttributesPlusTerm *zapt;
95 zapt = odr_malloc (o, sizeof(*zapt));
96 term_octet = odr_malloc (o, sizeof(*term_octet));
97 term = odr_malloc (o, sizeof(*term));
99 zapt->num_attributes = num_attr;
103 zapt->attributeList = odr_malloc (o, num_attr *
104 sizeof(*zapt->attributeList));
105 for (i = 0; i < num_attr; i++)
107 zapt->attributeList[i] =
108 odr_malloc (o,sizeof(**zapt->attributeList));
109 zapt->attributeList[i]->attributeType = &attr_list[2*i];
110 zapt->attributeList[i]->attributeValue = &attr_list[2*i+1];
114 zapt->attributeList = ODR_NULLVAL;
116 term->which = Z_Term_general;
117 term->u.general = term_octet;
118 term_octet->buf = odr_malloc (o, query_lex_len);
119 term_octet->size = term_octet->len = query_lex_len;
120 memcpy (term_octet->buf, query_lex_buf, query_lex_len);
124 static Z_Operand *rpn_simple (ODR o, int num_attr, int *attr_list)
128 zo = odr_malloc (o, sizeof(*zo));
132 zo->which = Z_Operand_APT;
133 if (!(zo->u.attributesPlusTerm = rpn_term (o, num_attr, attr_list)))
141 zo->which = Z_Operand_resultSetId;
142 zo->u.resultSetId = odr_malloc (o, query_lex_len+1);
143 memcpy (zo->u.resultSetId, query_lex_buf, query_lex_len);
144 zo->u.resultSetId[query_lex_len] = '\0';
153 static Z_Complex *rpn_complex (ODR o, int num_attr, int max_attr,
159 zc = odr_malloc (o, sizeof(*zc));
160 zo = odr_malloc (o, sizeof(*zo));
165 zo->which = Z_Operator_and;
166 zo->u.and = ODR_NULLVAL;
169 zo->which = Z_Operator_or;
170 zo->u.and = ODR_NULLVAL;
173 zo->which = Z_Operator_and_not;
174 zo->u.and = ODR_NULLVAL;
180 if (!(zc->s1 = rpn_structure (o, num_attr, max_attr, attr_list)))
182 if (!(zc->s2 = rpn_structure (o, num_attr, max_attr, attr_list)))
187 static Z_RPNStructure *rpn_structure (ODR o, int num_attr, int max_attr,
193 sz = odr_malloc (o, sizeof(*sz));
199 sz->which = Z_RPNStructure_complex;
200 if (!(sz->u.complex = rpn_complex (o, num_attr, max_attr, attr_list)))
205 sz->which = Z_RPNStructure_simple;
206 if (!(sz->u.simple = rpn_simple (o, num_attr, attr_list)))
213 if (!(cp = strchr (query_lex_buf, '=')))
215 if (num_attr >= max_attr)
217 attr_list[2*num_attr] = atoi (query_lex_buf);
218 attr_list[2*num_attr+1] = atoi (cp+1);
221 return rpn_structure (o, num_attr, max_attr, attr_list);
222 case 0: /* operator/operand expected! */
228 Z_RPNQuery *p_query_rpn (ODR o, const char *qbuf)
231 int attr_array[1024];
234 zq = odr_malloc (o, sizeof(*zq));
235 zq->attributeSetId = NULL;
237 if (!(zq->RPNStructure = rpn_structure (o, 0, 512, attr_array)))
242 Z_AttributesPlusTerm *p_query_scan (ODR o, const char *qbuf)
255 if (!(cp = strchr (query_lex_buf, '=')))
257 if (num_attr >= max_attr)
259 attr_list[2*num_attr] = atoi (query_lex_buf);
260 attr_list[2*num_attr+1] = atoi (cp+1);
265 return rpn_term (o, num_attr, attr_list);