2 * Copyright (c) 1995-2002, Index Data.
3 * See the file LICENSE for details.
5 * $Id: pquery.c,v 1.18 2002-09-24 08:05:42 adam Exp $
13 #include <yaz/proto.h>
15 #include <yaz/pquery.h>
17 static oid_value p_query_dfset = VAL_NONE;
19 struct yaz_pqf_parser {
20 const char *query_buf;
21 const char *query_ptr;
32 static Z_RPNStructure *rpn_structure (struct yaz_pqf_parser *li, ODR o,
34 int num_attr, int max_attr,
35 int *attr_list, char **attr_clist,
38 static enum oid_value query_oid_getvalbyname (struct yaz_pqf_parser *li)
45 memcpy (buf, li->lex_buf, li->lex_len);
46 buf[li->lex_len] = '\0';
47 value = oid_getvalbyname (buf);
51 static int compare_term (struct yaz_pqf_parser *li, const char *src,
54 size_t len=strlen(src);
56 if (li->lex_len == len+off && !memcmp (li->lex_buf+off, src, len-off))
61 static int query_token (struct yaz_pqf_parser *li)
64 const char *sep_match;
65 const char **qptr = &li->query_ptr;
72 if ((sep_match = strchr (li->left_sep, **qptr)))
74 sep_char = li->right_sep[sep_match - li->left_sep];
79 if (**qptr == li->escape_char && isdigit ((*qptr)[1]))
85 while (**qptr && **qptr != sep_char)
97 if (sep_char == ' ' &&
98 li->lex_len >= 1 && li->lex_buf[0] == li->escape_char)
100 if (compare_term (li, "and", 1))
102 if (compare_term (li, "or", 1))
104 if (compare_term (li, "not", 1))
106 if (compare_term (li, "attr", 1))
108 if (compare_term (li, "set", 1))
110 if (compare_term (li, "attrset", 1))
112 if (compare_term (li, "prox", 1))
114 if (compare_term (li, "term", 1))
120 static int lex (struct yaz_pqf_parser *li)
122 return li->query_look = query_token (li);
125 static int escape_string(char *out_buf, const char *in, int len)
130 if (*in == '\\' && len > 0)
156 sscanf (s, "%x", &n);
173 sscanf (s, "%o", &n);
185 return out - out_buf;
188 static int p_query_parse_attr(struct yaz_pqf_parser *li, ODR o,
189 int num_attr, int *attr_list,
190 char **attr_clist, oid_value *attr_set)
193 if (!(cp = strchr (li->lex_buf, '=')) ||
194 (size_t) (cp-li->lex_buf) > li->lex_len)
196 attr_set[num_attr] = query_oid_getvalbyname (li);
197 if (attr_set[num_attr] == VAL_NONE)
199 li->error = YAZ_PQF_ERROR_ATTSET;
204 li->error = YAZ_PQF_ERROR_MISSING;
207 if (!(cp = strchr (li->lex_buf, '=')))
209 li->error = YAZ_PQF_ERROR_BADATTR;
216 attr_set[num_attr] = attr_set[num_attr-1];
218 attr_set[num_attr] = VAL_NONE;
220 attr_list[2*num_attr] = atoi(li->lex_buf);
222 if (*cp >= '0' && *cp <= '9')
224 attr_list[2*num_attr+1] = atoi (cp);
225 attr_clist[num_attr] = 0;
229 int len = li->lex_len - (cp - li->lex_buf);
230 attr_list[2*num_attr+1] = 0;
231 attr_clist[num_attr] = (char *) odr_malloc (o, len+1);
232 len = escape_string(attr_clist[num_attr], cp, len);
233 attr_clist[num_attr][len] = '\0';
238 static Z_AttributesPlusTerm *rpn_term (struct yaz_pqf_parser *li, ODR o,
240 int num_attr, int *attr_list,
241 char **attr_clist, oid_value *attr_set)
243 Z_AttributesPlusTerm *zapt;
246 Z_AttributeElement **elements;
248 zapt = (Z_AttributesPlusTerm *)odr_malloc (o, sizeof(*zapt));
249 term_octet = (Odr_oct *)odr_malloc (o, sizeof(*term_octet));
250 term = (Z_Term *)odr_malloc (o, sizeof(*term));
253 elements = (Z_AttributeElement**)odr_nullval();
259 elements = (Z_AttributeElement**)
260 odr_malloc (o, num_attr * sizeof(*elements));
262 attr_tmp = (int *)odr_malloc (o, num_attr * 2 * sizeof(int));
263 memcpy (attr_tmp, attr_list, num_attr * 2 * sizeof(int));
264 for (i = num_attr; --i >= 0; )
267 for (j = i+1; j<num_attr; j++)
268 if (attr_tmp[2*j] == attr_tmp[2*i])
273 (Z_AttributeElement*)odr_malloc (o,sizeof(**elements));
274 elements[k]->attributeType = &attr_tmp[2*i];
275 elements[k]->attributeSet =
276 yaz_oidval_to_z3950oid(o, CLASS_ATTSET, attr_set[i]);
280 elements[k]->which = Z_AttributeValue_complex;
281 elements[k]->value.complex = (Z_ComplexAttribute *)
282 odr_malloc (o, sizeof(Z_ComplexAttribute));
283 elements[k]->value.complex->num_list = 1;
284 elements[k]->value.complex->list =
285 (Z_StringOrNumeric **)
286 odr_malloc (o, 1 * sizeof(Z_StringOrNumeric *));
287 elements[k]->value.complex->list[0] =
288 (Z_StringOrNumeric *)
289 odr_malloc (o, sizeof(Z_StringOrNumeric));
290 elements[k]->value.complex->list[0]->which =
291 Z_StringOrNumeric_string;
292 elements[k]->value.complex->list[0]->u.string =
294 elements[k]->value.complex->semanticAction = (int **)
296 elements[k]->value.complex->num_semanticAction = 0;
300 elements[k]->which = Z_AttributeValue_numeric;
301 elements[k]->value.numeric = &attr_tmp[2*i+1];
307 zapt->attributes = (Z_AttributeList *)
308 odr_malloc (o, sizeof(*zapt->attributes));
309 zapt->attributes->num_attributes = num_attr;
310 zapt->attributes->attributes = elements;
314 term_octet->buf = (unsigned char *)odr_malloc (o, 1 + li->lex_len);
315 term_octet->size = term_octet->len =
316 escape_string ((char *) (term_octet->buf), li->lex_buf, li->lex_len);
317 term_octet->buf[term_octet->size] = 0; /* null terminate */
319 switch (li->term_type)
322 term->which = Z_Term_general;
323 term->u.general = term_octet;
325 case Z_Term_characterString:
326 term->which = Z_Term_characterString;
327 term->u.characterString = (char*) term_octet->buf;
328 /* null terminated above */
331 term->which = Z_Term_numeric;
332 term->u.numeric = odr_intdup (o, atoi((char*) (term_octet->buf)));
335 term->which = Z_Term_null;
336 term->u.null = odr_nullval();
339 term->which = Z_Term_null;
340 term->u.null = odr_nullval();
346 static Z_Operand *rpn_simple (struct yaz_pqf_parser *li, ODR o, oid_proto proto,
347 int num_attr, int *attr_list, char **attr_clist,
352 zo = (Z_Operand *)odr_malloc (o, sizeof(*zo));
353 switch (li->query_look)
356 zo->which = Z_Operand_APT;
357 if (!(zo->u.attributesPlusTerm =
358 rpn_term (li, o, proto, num_attr, attr_list, attr_clist,
367 li->error = YAZ_PQF_ERROR_MISSING;
370 zo->which = Z_Operand_resultSetId;
371 zo->u.resultSetId = (char *)odr_malloc (o, li->lex_len+1);
372 memcpy (zo->u.resultSetId, li->lex_buf, li->lex_len);
373 zo->u.resultSetId[li->lex_len] = '\0';
377 /* we're only called if one of the above types are seens so
378 this shouldn't happen */
379 li->error = YAZ_PQF_ERROR_INTERNAL;
385 static Z_ProximityOperator *rpn_proximity (struct yaz_pqf_parser *li, ODR o)
387 Z_ProximityOperator *p = (Z_ProximityOperator *)odr_malloc (o, sizeof(*p));
391 li->error = YAZ_PQF_ERROR_MISSING;
394 if (*li->lex_buf == '1')
396 p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion));
399 else if (*li->lex_buf == '0')
401 p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion));
409 li->error = YAZ_PQF_ERROR_MISSING;
412 p->distance = (int *)odr_malloc (o, sizeof(*p->distance));
413 *p->distance = atoi (li->lex_buf);
417 li->error = YAZ_PQF_ERROR_MISSING;
420 p->ordered = (int *)odr_malloc (o, sizeof(*p->ordered));
421 *p->ordered = atoi (li->lex_buf);
425 li->error = YAZ_PQF_ERROR_MISSING;
428 p->relationType = (int *)odr_malloc (o, sizeof(*p->relationType));
429 *p->relationType = atoi (li->lex_buf);
433 li->error = YAZ_PQF_ERROR_MISSING;
436 if (*li->lex_buf == 'k')
438 else if (*li->lex_buf == 'p')
441 p->which = atoi (li->lex_buf);
445 li->error = YAZ_PQF_ERROR_MISSING;
448 p->which = Z_ProximityOperator_known;
449 p->u.known = (int *)odr_malloc (o, sizeof(*p->u.known));
450 *p->u.known = atoi (li->lex_buf);
454 static Z_Complex *rpn_complex (struct yaz_pqf_parser *li, ODR o, oid_proto proto,
455 int num_attr, int max_attr,
456 int *attr_list, char **attr_clist,
462 zc = (Z_Complex *)odr_malloc (o, sizeof(*zc));
463 zo = (Z_Operator *)odr_malloc (o, sizeof(*zo));
465 switch (li->query_look)
468 zo->which = Z_Operator_and;
469 zo->u.and_not = odr_nullval();
472 zo->which = Z_Operator_or;
473 zo->u.and_not = odr_nullval();
476 zo->which = Z_Operator_and_not;
477 zo->u.and_not = odr_nullval();
480 zo->which = Z_Operator_prox;
481 zo->u.prox = rpn_proximity (li, o);
486 /* we're only called if one of the above types are seens so
487 this shouldn't happen */
488 li->error = YAZ_PQF_ERROR_INTERNAL;
493 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
494 attr_clist, attr_set)))
497 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
498 attr_clist, attr_set)))
503 static void rpn_term_type (struct yaz_pqf_parser *li, ODR o)
507 if (compare_term (li, "general", 0))
508 li->term_type = Z_Term_general;
509 else if (compare_term (li, "numeric", 0))
510 li->term_type = Z_Term_numeric;
511 else if (compare_term (li, "string", 0))
512 li->term_type = Z_Term_characterString;
513 else if (compare_term (li, "oid", 0))
514 li->term_type = Z_Term_oid;
515 else if (compare_term (li, "datetime", 0))
516 li->term_type = Z_Term_dateTime;
517 else if (compare_term (li, "null", 0))
518 li->term_type = Z_Term_null;
522 static Z_RPNStructure *rpn_structure (struct yaz_pqf_parser *li, ODR o,
524 int num_attr, int max_attr,
531 sz = (Z_RPNStructure *)odr_malloc (o, sizeof(*sz));
532 switch (li->query_look)
538 sz->which = Z_RPNStructure_complex;
539 if (!(sz->u.complex =
540 rpn_complex (li, o, proto, num_attr, max_attr, attr_list,
541 attr_clist, attr_set)))
546 sz->which = Z_RPNStructure_simple;
548 rpn_simple (li, o, proto, num_attr, attr_list,
549 attr_clist, attr_set)))
556 li->error = YAZ_PQF_ERROR_MISSING;
559 if (num_attr >= max_attr)
561 li->error = YAZ_PQF_ERROR_TOOMANY;
564 if (!p_query_parse_attr(li, o, num_attr, attr_list,
565 attr_clist, attr_set))
570 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
571 attr_clist, attr_set);
574 rpn_term_type (li, o);
576 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
577 attr_clist, attr_set);
578 case 0: /* operator/operand expected! */
579 li->error = YAZ_PQF_ERROR_MISSING;
585 Z_RPNQuery *p_query_rpn_mk (ODR o, struct yaz_pqf_parser *li, oid_proto proto,
589 int attr_array[1024];
590 char *attr_clist[512];
591 oid_value attr_set[512];
592 oid_value topSet = VAL_NONE;
594 zq = (Z_RPNQuery *)odr_malloc (o, sizeof(*zq));
596 if (li->query_look == 'r')
599 topSet = query_oid_getvalbyname (li);
600 if (topSet == VAL_NONE)
602 li->error = YAZ_PQF_ERROR_ATTSET;
608 if (topSet == VAL_NONE)
609 topSet = p_query_dfset;
610 if (topSet == VAL_NONE)
613 zq->attributeSetId = yaz_oidval_to_z3950oid(o, CLASS_ATTSET, topSet);
615 if (!zq->attributeSetId)
617 li->error = YAZ_PQF_ERROR_ATTSET;
621 if (!(zq->RPNStructure = rpn_structure (li, o, proto, 0, 512,
622 attr_array, attr_clist, attr_set)))
626 li->error = YAZ_PQF_ERROR_EXTRA;
632 Z_RPNQuery *p_query_rpn (ODR o, oid_proto proto,
635 struct yaz_pqf_parser li;
639 li.right_sep = "}\"";
640 li.escape_char = '@';
641 li.term_type = Z_Term_general;
642 li.query_buf = li.query_ptr = qbuf;
644 return p_query_rpn_mk (o, &li, proto, qbuf);
648 Z_AttributesPlusTerm *p_query_scan_mk (struct yaz_pqf_parser *li,
649 ODR o, oid_proto proto,
650 Odr_oid **attributeSetP,
654 char *attr_clist[512];
655 oid_value attr_set[512];
658 oid_value topSet = VAL_NONE;
659 Z_AttributesPlusTerm *apt;
662 if (li->query_look == 'r')
665 topSet = query_oid_getvalbyname (li);
669 if (topSet == VAL_NONE)
670 topSet = p_query_dfset;
671 if (topSet == VAL_NONE)
674 *attributeSetP = yaz_oidval_to_z3950oid (o, CLASS_ATTSET, topSet);
678 if (li->query_look == 'l')
683 li->error = YAZ_PQF_ERROR_MISSING;
686 if (num_attr >= max_attr)
688 li->error = YAZ_PQF_ERROR_TOOMANY;
691 if (!p_query_parse_attr(li, o, num_attr, attr_list,
692 attr_clist, attr_set))
697 else if (li->query_look == 'y')
700 rpn_term_type (li, o);
707 li->error = YAZ_PQF_ERROR_MISSING;
710 apt = rpn_term (li, o, proto, num_attr, attr_list, attr_clist, attr_set);
714 if (li->query_look != 0)
716 li->error = YAZ_PQF_ERROR_EXTRA;
722 Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,
723 Odr_oid **attributeSetP,
726 struct yaz_pqf_parser li;
730 li.right_sep = "}\"";
731 li.escape_char = '@';
732 li.term_type = Z_Term_general;
733 li.query_buf = li.query_ptr = qbuf;
736 return p_query_scan_mk (&li, o, proto, attributeSetP, qbuf);
739 int p_query_attset (const char *arg)
741 p_query_dfset = oid_getvalbyname (arg);
742 return (p_query_dfset == VAL_NONE) ? -1 : 0;
745 YAZ_PQF_Parser yaz_pqf_create (void)
747 YAZ_PQF_Parser p = (YAZ_PQF_Parser) xmalloc (sizeof(*p));
751 p->right_sep = "}\"";
752 p->escape_char = '@';
753 p->term_type = Z_Term_general;
758 void yaz_pqf_destroy (YAZ_PQF_Parser p)
763 Z_RPNQuery *yaz_pqf_parse (YAZ_PQF_Parser p, ODR o, const char *qbuf)
767 p->query_buf = p->query_ptr = qbuf;
769 return p_query_rpn_mk (o, p, PROTO_Z3950, qbuf);
772 Z_AttributesPlusTerm *yaz_pqf_scan (YAZ_PQF_Parser p, ODR o,
773 Odr_oid **attributeSetP,
778 p->query_buf = p->query_ptr = qbuf;
780 return p_query_scan_mk (p, o, PROTO_Z3950, attributeSetP, qbuf);
783 int yaz_pqf_error (YAZ_PQF_Parser p, const char **msg, size_t *off)
787 case YAZ_PQF_ERROR_NONE:
788 *msg = "no error"; break;
789 case YAZ_PQF_ERROR_EXTRA:
790 *msg = "extra token"; break;
791 case YAZ_PQF_ERROR_MISSING:
792 *msg = "missing token"; break;
793 case YAZ_PQF_ERROR_ATTSET:
794 *msg = "unknown attribute set"; break;
795 case YAZ_PQF_ERROR_TOOMANY:
796 *msg = "too many attributes"; break;
797 case YAZ_PQF_ERROR_BADATTR:
798 *msg = "bad attribute specification"; break;
799 case YAZ_PQF_ERROR_INTERNAL:
800 *msg = "internal error"; break;
802 *msg = "unknown error"; break;
804 *off = p->query_ptr - p->query_buf;