2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.1 1995-05-22 15:31:49 adam
8 * New function, p_query_rpn, to convert from prefix (ascii) to rpn (asn).
21 static const char *query_buf;
22 static const char *query_lex_buf;
23 static int query_lex_len;
24 static int query_look = 0;
25 static char *left_sep = "{\"";
26 static char *right_sep = "}\"";
27 static int escape_char = '@';
29 static Z_RPNStructure *rpn_structure (ODR o, int num_attr, int max_attr,
32 static int query_token (const char **qptr, const char **lex_buf, int *lex_len)
34 const char *sep_match;
41 if ((sep_match = strchr (left_sep, **qptr)))
43 int sep_index = sep_match - left_sep;
47 while (**qptr && **qptr != right_sep[sep_index])
58 while (**qptr && **qptr != ' ')
64 if (*lex_len >= 1 && (*lex_buf)[0] == escape_char)
66 if (*lex_len == 4 && !memcmp (*lex_buf+1, "and", 3))
68 if (*lex_len == 3 && !memcmp (*lex_buf+1, "or", 2))
70 if (*lex_len == 4 && !memcmp (*lex_buf+1, "not", 3))
72 if (*lex_len == 5 && !memcmp (*lex_buf+1, "attr", 4))
74 if (*lex_len == 4 && !memcmp (*lex_buf+1, "set", 3))
80 static void lex (void)
82 query_look = query_token (&query_buf, &query_lex_buf, &query_lex_len);
85 static Z_AttributesPlusTerm *rpn_term (ODR o, int num_attr, int *attr_list)
87 Z_AttributesPlusTerm *zapt;
91 zapt = odr_malloc (o, sizeof(*zapt));
92 term_octet = odr_malloc (o, sizeof(*term_octet));
93 term = odr_malloc (o, sizeof(*term));
95 zapt->num_attributes = num_attr;
99 zapt->attributeList = odr_malloc (o, num_attr *
100 sizeof(*zapt->attributeList));
101 for (i = 0; i < num_attr; i++)
103 zapt->attributeList[i] =
104 odr_malloc (o,sizeof(**zapt->attributeList));
105 zapt->attributeList[i]->attributeType = &attr_list[2*i];
106 zapt->attributeList[i]->attributeValue = &attr_list[2*i+1];
110 zapt->attributeList = ODR_NULLVAL;
112 term->which = Z_Term_general;
113 term->u.general = term_octet;
114 term_octet->buf = odr_malloc (o, query_lex_len);
115 term_octet->size = term_octet->len = query_lex_len;
116 memcpy (term_octet->buf, query_lex_buf, query_lex_len);
120 static Z_Operand *rpn_simple (ODR o, int num_attr, int *attr_list)
124 zo = odr_malloc (o, sizeof(*zo));
128 zo->which = Z_Operand_APT;
129 if (!(zo->u.attributesPlusTerm = rpn_term (o, num_attr, attr_list)))
137 zo->which = Z_Operand_resultSetId;
138 zo->u.resultSetId = odr_malloc (o, query_lex_len+1);
139 memcpy (zo->u.resultSetId, query_lex_buf, query_lex_len);
140 zo->u.resultSetId[query_lex_len] = '\0';
149 static Z_Complex *rpn_complex (ODR o, int num_attr, int max_attr,
155 zc = odr_malloc (o, sizeof(*zc));
156 zo = odr_malloc (o, sizeof(*zo));
161 zo->which = Z_Operator_and;
162 zo->u.and = ODR_NULLVAL;
165 zo->which = Z_Operator_or;
166 zo->u.and = ODR_NULLVAL;
169 zo->which = Z_Operator_and_not;
170 zo->u.and = ODR_NULLVAL;
176 if (!(zc->s1 = rpn_structure (o, num_attr, max_attr, attr_list)))
178 if (!(zc->s2 = rpn_structure (o, num_attr, max_attr, attr_list)))
183 static Z_RPNStructure *rpn_structure (ODR o, int num_attr, int max_attr,
189 sz = odr_malloc (o, sizeof(*sz));
195 sz->which = Z_RPNStructure_complex;
196 if (!(sz->u.complex = rpn_complex (o, num_attr, max_attr, attr_list)))
201 sz->which = Z_RPNStructure_simple;
202 if (!(sz->u.simple = rpn_simple (o, num_attr, attr_list)))
209 if (!(cp = strchr (query_lex_buf, '=')))
211 if (num_attr >= max_attr)
213 attr_list[2*num_attr] = atoi (query_lex_buf);
214 attr_list[2*num_attr+1] = atoi (cp+1);
217 return rpn_structure (o, num_attr, max_attr, attr_list);
218 case 0: /* operator/operand expected! */
224 Z_RPNQuery *p_query_rpn (ODR o, const char *qbuf)
227 int attr_array[1024];
230 zq = odr_malloc (o, sizeof(*zq));
231 zq->attributeSetId = NULL;
233 if (!(zq->RPNStructure = rpn_structure (o, 0, 512, attr_array)))