2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: pquery.c,v 1.7 2006-02-19 18:34:13 adam Exp $
9 * \brief Implements PQF parsing
16 #include <yaz/proto.h>
18 #include <yaz/pquery.h>
20 static oid_value p_query_dfset = VAL_NONE;
22 struct yaz_pqf_parser {
23 const char *query_buf;
24 const char *query_ptr;
36 static Z_RPNStructure *rpn_structure (struct yaz_pqf_parser *li, ODR o,
38 int num_attr, int max_attr,
39 int *attr_list, char **attr_clist,
42 static enum oid_value query_oid_getvalbyname (struct yaz_pqf_parser *li)
49 memcpy (buf, li->lex_buf, li->lex_len);
50 buf[li->lex_len] = '\0';
51 value = oid_getvalbyname (buf);
55 static int compare_term (struct yaz_pqf_parser *li, const char *src,
58 size_t len=strlen(src);
60 if (li->lex_len == len+off && !memcmp (li->lex_buf+off, src, len-off))
65 static int query_token (struct yaz_pqf_parser *li)
68 const char *sep_match;
69 const char **qptr = &li->query_ptr;
76 if ((sep_match = strchr (li->left_sep, **qptr)))
78 sep_char = li->right_sep[sep_match - li->left_sep];
83 if (**qptr == li->escape_char && isdigit (((const unsigned char *) *qptr)[1]))
89 while (**qptr && **qptr != sep_char)
101 if (sep_char == ' ' &&
102 li->lex_len >= 1 && li->lex_buf[0] == li->escape_char)
104 if (compare_term (li, "and", 1))
106 if (compare_term (li, "or", 1))
108 if (compare_term (li, "not", 1))
110 if (compare_term (li, "attr", 1))
112 if (compare_term (li, "set", 1))
114 if (compare_term (li, "attrset", 1))
116 if (compare_term (li, "prox", 1))
118 if (compare_term (li, "term", 1))
124 static int lex (struct yaz_pqf_parser *li)
126 return li->query_look = query_token (li);
129 static int escape_string(char *out_buf, const char *in, int len)
134 if (*in == '\\' && len > 0)
160 sscanf (s, "%x", &n);
177 sscanf (s, "%o", &n);
189 return out - out_buf;
192 static int p_query_parse_attr(struct yaz_pqf_parser *li, ODR o,
193 int num_attr, int *attr_list,
194 char **attr_clist, oid_value *attr_set)
197 if (!(cp = strchr (li->lex_buf, '=')) ||
198 (size_t) (cp-li->lex_buf) > li->lex_len)
200 attr_set[num_attr] = query_oid_getvalbyname (li);
201 if (attr_set[num_attr] == VAL_NONE)
203 li->error = YAZ_PQF_ERROR_ATTSET;
208 li->error = YAZ_PQF_ERROR_MISSING;
211 if (!(cp = strchr (li->lex_buf, '=')))
213 li->error = YAZ_PQF_ERROR_BADATTR;
220 attr_set[num_attr] = attr_set[num_attr-1];
222 attr_set[num_attr] = VAL_NONE;
224 attr_list[2*num_attr] = atoi(li->lex_buf);
226 if (*cp >= '0' && *cp <= '9')
228 attr_list[2*num_attr+1] = atoi (cp);
229 attr_clist[num_attr] = 0;
233 int len = li->lex_len - (cp - li->lex_buf);
234 attr_list[2*num_attr+1] = 0;
235 attr_clist[num_attr] = (char *) odr_malloc (o, len+1);
236 len = escape_string(attr_clist[num_attr], cp, len);
237 attr_clist[num_attr][len] = '\0';
242 static Z_AttributesPlusTerm *rpn_term (struct yaz_pqf_parser *li, ODR o,
244 int num_attr, int *attr_list,
245 char **attr_clist, oid_value *attr_set)
247 Z_AttributesPlusTerm *zapt;
250 Z_AttributeElement **elements;
252 zapt = (Z_AttributesPlusTerm *)odr_malloc (o, sizeof(*zapt));
253 term_octet = (Odr_oct *)odr_malloc (o, sizeof(*term_octet));
254 term = (Z_Term *)odr_malloc (o, sizeof(*term));
257 elements = (Z_AttributeElement**)odr_nullval();
263 elements = (Z_AttributeElement**)
264 odr_malloc (o, num_attr * sizeof(*elements));
266 attr_tmp = (int *)odr_malloc (o, num_attr * 2 * sizeof(int));
267 memcpy (attr_tmp, attr_list, num_attr * 2 * sizeof(int));
268 for (i = num_attr; --i >= 0; )
271 for (j = i+1; j<num_attr; j++)
272 if (attr_tmp[2*j] == attr_tmp[2*i])
277 (Z_AttributeElement*)odr_malloc (o,sizeof(**elements));
278 elements[k]->attributeType = &attr_tmp[2*i];
279 elements[k]->attributeSet =
280 yaz_oidval_to_z3950oid(o, CLASS_ATTSET, attr_set[i]);
284 elements[k]->which = Z_AttributeValue_complex;
285 elements[k]->value.complex = (Z_ComplexAttribute *)
286 odr_malloc (o, sizeof(Z_ComplexAttribute));
287 elements[k]->value.complex->num_list = 1;
288 elements[k]->value.complex->list =
289 (Z_StringOrNumeric **)
290 odr_malloc (o, 1 * sizeof(Z_StringOrNumeric *));
291 elements[k]->value.complex->list[0] =
292 (Z_StringOrNumeric *)
293 odr_malloc (o, sizeof(Z_StringOrNumeric));
294 elements[k]->value.complex->list[0]->which =
295 Z_StringOrNumeric_string;
296 elements[k]->value.complex->list[0]->u.string =
298 elements[k]->value.complex->semanticAction = (int **)
300 elements[k]->value.complex->num_semanticAction = 0;
304 elements[k]->which = Z_AttributeValue_numeric;
305 elements[k]->value.numeric = &attr_tmp[2*i+1];
311 zapt->attributes = (Z_AttributeList *)
312 odr_malloc (o, sizeof(*zapt->attributes));
313 zapt->attributes->num_attributes = num_attr;
314 zapt->attributes->attributes = elements;
318 term_octet->buf = (unsigned char *)odr_malloc (o, 1 + li->lex_len);
319 term_octet->size = term_octet->len =
320 escape_string ((char *) (term_octet->buf), li->lex_buf, li->lex_len);
321 term_octet->buf[term_octet->size] = 0; /* null terminate */
323 switch (li->term_type)
326 term->which = Z_Term_general;
327 term->u.general = term_octet;
329 case Z_Term_characterString:
330 term->which = Z_Term_characterString;
331 term->u.characterString = (char*) term_octet->buf;
332 /* null terminated above */
335 term->which = Z_Term_numeric;
336 term->u.numeric = odr_intdup (o, atoi((char*) (term_octet->buf)));
339 term->which = Z_Term_null;
340 term->u.null = odr_nullval();
342 case Z_Term_external:
343 term->which = Z_Term_external;
344 term->u.external = 0;
347 term->which = Z_Term_null;
348 term->u.null = odr_nullval();
354 static Z_Operand *rpn_simple (struct yaz_pqf_parser *li, ODR o, oid_proto proto,
355 int num_attr, int *attr_list, char **attr_clist,
360 zo = (Z_Operand *)odr_malloc (o, sizeof(*zo));
361 switch (li->query_look)
364 zo->which = Z_Operand_APT;
365 if (!(zo->u.attributesPlusTerm =
366 rpn_term (li, o, proto, num_attr, attr_list, attr_clist,
375 li->error = YAZ_PQF_ERROR_MISSING;
378 zo->which = Z_Operand_resultSetId;
379 zo->u.resultSetId = (char *)odr_malloc (o, li->lex_len+1);
380 memcpy (zo->u.resultSetId, li->lex_buf, li->lex_len);
381 zo->u.resultSetId[li->lex_len] = '\0';
385 /* we're only called if one of the above types are seens so
386 this shouldn't happen */
387 li->error = YAZ_PQF_ERROR_INTERNAL;
393 static Z_ProximityOperator *rpn_proximity (struct yaz_pqf_parser *li, ODR o)
395 Z_ProximityOperator *p = (Z_ProximityOperator *)odr_malloc (o, sizeof(*p));
399 li->error = YAZ_PQF_ERROR_MISSING;
402 if (*li->lex_buf == '1')
403 p->exclusion = odr_intdup (o, 1);
404 else if (*li->lex_buf == '0')
405 p->exclusion = odr_intdup (o, 0);
411 li->error = YAZ_PQF_ERROR_MISSING;
414 p->distance = odr_intdup (o, atoi(li->lex_buf));
418 li->error = YAZ_PQF_ERROR_MISSING;
421 p->ordered = odr_intdup (o, atoi (li->lex_buf));
425 li->error = YAZ_PQF_ERROR_MISSING;
428 p->relationType = odr_intdup (o, atoi (li->lex_buf));
432 li->error = YAZ_PQF_ERROR_MISSING;
435 if (*li->lex_buf == 'k')
436 p->which = Z_ProximityOperator_known;
437 else if (*li->lex_buf == 'p')
438 p->which = Z_ProximityOperator_private;
440 p->which = atoi (li->lex_buf);
444 li->error = YAZ_PQF_ERROR_MISSING;
447 p->u.known = odr_intdup (o, atoi(li->lex_buf));
451 static Z_Complex *rpn_complex (struct yaz_pqf_parser *li, ODR o, oid_proto proto,
452 int num_attr, int max_attr,
453 int *attr_list, char **attr_clist,
459 zc = (Z_Complex *)odr_malloc (o, sizeof(*zc));
460 zo = (Z_Operator *)odr_malloc (o, sizeof(*zo));
462 switch (li->query_look)
465 zo->which = Z_Operator_and;
466 zo->u.op_and = odr_nullval();
469 zo->which = Z_Operator_or;
470 zo->u.op_or = odr_nullval();
473 zo->which = Z_Operator_and_not;
474 zo->u.and_not = odr_nullval();
477 zo->which = Z_Operator_prox;
478 zo->u.prox = rpn_proximity (li, o);
483 /* we're only called if one of the above types are seens so
484 this shouldn't happen */
485 li->error = YAZ_PQF_ERROR_INTERNAL;
490 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
491 attr_clist, attr_set)))
494 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
495 attr_clist, attr_set)))
500 static void rpn_term_type (struct yaz_pqf_parser *li, ODR o)
504 if (compare_term (li, "general", 0))
505 li->term_type = Z_Term_general;
506 else if (compare_term (li, "numeric", 0))
507 li->term_type = Z_Term_numeric;
508 else if (compare_term (li, "string", 0))
509 li->term_type = Z_Term_characterString;
510 else if (compare_term (li, "oid", 0))
511 li->term_type = Z_Term_oid;
512 else if (compare_term (li, "datetime", 0))
513 li->term_type = Z_Term_dateTime;
514 else if (compare_term (li, "null", 0))
515 li->term_type = Z_Term_null;
516 else if (compare_term(li, "range", 0))
518 /* prepare for external: range search .. */
519 li->term_type = Z_Term_external;
520 li->external_type = VAL_MULTISRCH2;
525 static Z_RPNStructure *rpn_structure (struct yaz_pqf_parser *li, ODR o,
527 int num_attr, int max_attr,
534 sz = (Z_RPNStructure *)odr_malloc (o, sizeof(*sz));
535 switch (li->query_look)
541 sz->which = Z_RPNStructure_complex;
542 if (!(sz->u.complex =
543 rpn_complex (li, o, proto, num_attr, max_attr, attr_list,
544 attr_clist, attr_set)))
549 sz->which = Z_RPNStructure_simple;
551 rpn_simple (li, o, proto, num_attr, attr_list,
552 attr_clist, attr_set)))
559 li->error = YAZ_PQF_ERROR_MISSING;
562 if (num_attr >= max_attr)
564 li->error = YAZ_PQF_ERROR_TOOMANY;
567 if (!p_query_parse_attr(li, o, num_attr, attr_list,
568 attr_clist, attr_set))
573 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
574 attr_clist, attr_set);
577 rpn_term_type (li, o);
579 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
580 attr_clist, attr_set);
581 case 0: /* operator/operand expected! */
582 li->error = YAZ_PQF_ERROR_MISSING;
588 Z_RPNQuery *p_query_rpn_mk (ODR o, struct yaz_pqf_parser *li, oid_proto proto,
592 int attr_array[1024];
593 char *attr_clist[512];
594 oid_value attr_set[512];
595 oid_value topSet = VAL_NONE;
597 zq = (Z_RPNQuery *)odr_malloc (o, sizeof(*zq));
599 if (li->query_look == 'r')
602 topSet = query_oid_getvalbyname (li);
603 if (topSet == VAL_NONE)
605 li->error = YAZ_PQF_ERROR_ATTSET;
611 if (topSet == VAL_NONE)
612 topSet = p_query_dfset;
613 if (topSet == VAL_NONE)
616 zq->attributeSetId = yaz_oidval_to_z3950oid(o, CLASS_ATTSET, topSet);
618 if (!zq->attributeSetId)
620 li->error = YAZ_PQF_ERROR_ATTSET;
624 if (!(zq->RPNStructure = rpn_structure (li, o, proto, 0, 512,
625 attr_array, attr_clist, attr_set)))
629 li->error = YAZ_PQF_ERROR_EXTRA;
635 Z_RPNQuery *p_query_rpn (ODR o, oid_proto proto, const char *qbuf)
637 struct yaz_pqf_parser li;
641 li.right_sep = "}\"";
642 li.escape_char = '@';
643 li.term_type = Z_Term_general;
644 li.query_buf = li.query_ptr = qbuf;
646 return p_query_rpn_mk (o, &li, proto, qbuf);
650 Z_AttributesPlusTerm *p_query_scan_mk (struct yaz_pqf_parser *li,
651 ODR o, oid_proto proto,
652 Odr_oid **attributeSetP,
656 char *attr_clist[512];
657 oid_value attr_set[512];
660 oid_value topSet = VAL_NONE;
661 Z_AttributesPlusTerm *apt;
664 if (li->query_look == 'r')
667 topSet = query_oid_getvalbyname (li);
671 if (topSet == VAL_NONE)
672 topSet = p_query_dfset;
673 if (topSet == VAL_NONE)
676 *attributeSetP = yaz_oidval_to_z3950oid (o, CLASS_ATTSET, topSet);
680 if (li->query_look == 'l')
685 li->error = YAZ_PQF_ERROR_MISSING;
688 if (num_attr >= max_attr)
690 li->error = YAZ_PQF_ERROR_TOOMANY;
693 if (!p_query_parse_attr(li, o, num_attr, attr_list,
694 attr_clist, attr_set))
699 else if (li->query_look == 'y')
702 rpn_term_type (li, o);
709 li->error = YAZ_PQF_ERROR_MISSING;
712 apt = rpn_term (li, o, proto, num_attr, attr_list, attr_clist, attr_set);
716 if (li->query_look != 0)
718 li->error = YAZ_PQF_ERROR_EXTRA;
724 Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,
725 Odr_oid **attributeSetP,
728 struct yaz_pqf_parser li;
732 li.right_sep = "}\"";
733 li.escape_char = '@';
734 li.term_type = Z_Term_general;
735 li.query_buf = li.query_ptr = qbuf;
738 return p_query_scan_mk (&li, o, proto, attributeSetP, qbuf);
741 int p_query_attset (const char *arg)
743 p_query_dfset = oid_getvalbyname (arg);
744 return (p_query_dfset == VAL_NONE) ? -1 : 0;
747 YAZ_PQF_Parser yaz_pqf_create (void)
749 YAZ_PQF_Parser p = (YAZ_PQF_Parser) xmalloc (sizeof(*p));
753 p->right_sep = "}\"";
754 p->escape_char = '@';
755 p->term_type = Z_Term_general;
760 void yaz_pqf_destroy (YAZ_PQF_Parser p)
765 Z_RPNQuery *yaz_pqf_parse (YAZ_PQF_Parser p, ODR o, const char *qbuf)
769 p->query_buf = p->query_ptr = qbuf;
771 return p_query_rpn_mk (o, p, PROTO_Z3950, qbuf);
774 Z_AttributesPlusTerm *yaz_pqf_scan (YAZ_PQF_Parser p, ODR o,
775 Odr_oid **attributeSetP,
780 p->query_buf = p->query_ptr = qbuf;
782 return p_query_scan_mk (p, o, PROTO_Z3950, attributeSetP, qbuf);
785 int yaz_pqf_error (YAZ_PQF_Parser p, const char **msg, size_t *off)
789 case YAZ_PQF_ERROR_NONE:
790 *msg = "no error"; break;
791 case YAZ_PQF_ERROR_EXTRA:
792 *msg = "extra token"; break;
793 case YAZ_PQF_ERROR_MISSING:
794 *msg = "missing token"; break;
795 case YAZ_PQF_ERROR_ATTSET:
796 *msg = "unknown attribute set"; break;
797 case YAZ_PQF_ERROR_TOOMANY:
798 *msg = "too many attributes"; break;
799 case YAZ_PQF_ERROR_BADATTR:
800 *msg = "bad attribute specification"; break;
801 case YAZ_PQF_ERROR_INTERNAL:
802 *msg = "internal error"; break;
804 *msg = "unknown error"; break;
806 *off = p->query_ptr - p->query_buf;
812 * indent-tabs-mode: nil
814 * vim: shiftwidth=4 tabstop=8 expandtab