2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: pquery.c,v 1.8 2006-04-01 11:45:23 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)
198 if (!(cp = strchr (li->lex_buf, '=')) ||
199 (size_t) (cp-li->lex_buf) > li->lex_len)
201 attr_set[num_attr] = query_oid_getvalbyname (li);
202 if (attr_set[num_attr] == VAL_NONE)
204 li->error = YAZ_PQF_ERROR_ATTSET;
209 li->error = YAZ_PQF_ERROR_MISSING;
212 if (!(cp = strchr (li->lex_buf, '=')))
214 li->error = YAZ_PQF_ERROR_BADATTR;
221 attr_set[num_attr] = attr_set[num_attr-1];
223 attr_set[num_attr] = VAL_NONE;
225 if (*li->lex_buf < '0' || *li->lex_buf > '9')
227 li->error = YAZ_PQF_ERROR_BAD_INTEGER;
230 attr_list[2*num_attr] = atoi(li->lex_buf);
232 if (*cp >= '0' && *cp <= '9')
234 attr_list[2*num_attr+1] = atoi (cp);
235 attr_clist[num_attr] = 0;
239 int len = li->lex_len - (cp - li->lex_buf);
240 attr_list[2*num_attr+1] = 0;
241 attr_clist[num_attr] = (char *) odr_malloc (o, len+1);
242 len = escape_string(attr_clist[num_attr], cp, len);
243 attr_clist[num_attr][len] = '\0';
248 static Z_AttributesPlusTerm *rpn_term (struct yaz_pqf_parser *li, ODR o,
250 int num_attr, int *attr_list,
251 char **attr_clist, oid_value *attr_set)
253 Z_AttributesPlusTerm *zapt;
256 Z_AttributeElement **elements;
258 zapt = (Z_AttributesPlusTerm *)odr_malloc (o, sizeof(*zapt));
259 term_octet = (Odr_oct *)odr_malloc (o, sizeof(*term_octet));
260 term = (Z_Term *)odr_malloc (o, sizeof(*term));
263 elements = (Z_AttributeElement**)odr_nullval();
269 elements = (Z_AttributeElement**)
270 odr_malloc (o, num_attr * sizeof(*elements));
272 attr_tmp = (int *)odr_malloc (o, num_attr * 2 * sizeof(int));
273 memcpy (attr_tmp, attr_list, num_attr * 2 * sizeof(int));
274 for (i = num_attr; --i >= 0; )
277 for (j = i+1; j<num_attr; j++)
278 if (attr_tmp[2*j] == attr_tmp[2*i])
283 (Z_AttributeElement*)odr_malloc (o,sizeof(**elements));
284 elements[k]->attributeType = &attr_tmp[2*i];
285 elements[k]->attributeSet =
286 yaz_oidval_to_z3950oid(o, CLASS_ATTSET, attr_set[i]);
290 elements[k]->which = Z_AttributeValue_complex;
291 elements[k]->value.complex = (Z_ComplexAttribute *)
292 odr_malloc (o, sizeof(Z_ComplexAttribute));
293 elements[k]->value.complex->num_list = 1;
294 elements[k]->value.complex->list =
295 (Z_StringOrNumeric **)
296 odr_malloc (o, 1 * sizeof(Z_StringOrNumeric *));
297 elements[k]->value.complex->list[0] =
298 (Z_StringOrNumeric *)
299 odr_malloc (o, sizeof(Z_StringOrNumeric));
300 elements[k]->value.complex->list[0]->which =
301 Z_StringOrNumeric_string;
302 elements[k]->value.complex->list[0]->u.string =
304 elements[k]->value.complex->semanticAction = (int **)
306 elements[k]->value.complex->num_semanticAction = 0;
310 elements[k]->which = Z_AttributeValue_numeric;
311 elements[k]->value.numeric = &attr_tmp[2*i+1];
317 zapt->attributes = (Z_AttributeList *)
318 odr_malloc (o, sizeof(*zapt->attributes));
319 zapt->attributes->num_attributes = num_attr;
320 zapt->attributes->attributes = elements;
324 term_octet->buf = (unsigned char *)odr_malloc (o, 1 + li->lex_len);
325 term_octet->size = term_octet->len =
326 escape_string ((char *) (term_octet->buf), li->lex_buf, li->lex_len);
327 term_octet->buf[term_octet->size] = 0; /* null terminate */
329 switch (li->term_type)
332 term->which = Z_Term_general;
333 term->u.general = term_octet;
335 case Z_Term_characterString:
336 term->which = Z_Term_characterString;
337 term->u.characterString = (char*) term_octet->buf;
338 /* null terminated above */
341 term->which = Z_Term_numeric;
342 term->u.numeric = odr_intdup (o, atoi((char*) (term_octet->buf)));
345 term->which = Z_Term_null;
346 term->u.null = odr_nullval();
348 case Z_Term_external:
349 term->which = Z_Term_external;
350 term->u.external = 0;
353 term->which = Z_Term_null;
354 term->u.null = odr_nullval();
360 static Z_Operand *rpn_simple (struct yaz_pqf_parser *li, ODR o, oid_proto proto,
361 int num_attr, int *attr_list, char **attr_clist,
366 zo = (Z_Operand *)odr_malloc (o, sizeof(*zo));
367 switch (li->query_look)
370 zo->which = Z_Operand_APT;
371 if (!(zo->u.attributesPlusTerm =
372 rpn_term (li, o, proto, num_attr, attr_list, attr_clist,
381 li->error = YAZ_PQF_ERROR_MISSING;
384 zo->which = Z_Operand_resultSetId;
385 zo->u.resultSetId = (char *)odr_malloc (o, li->lex_len+1);
386 memcpy (zo->u.resultSetId, li->lex_buf, li->lex_len);
387 zo->u.resultSetId[li->lex_len] = '\0';
391 /* we're only called if one of the above types are seens so
392 this shouldn't happen */
393 li->error = YAZ_PQF_ERROR_INTERNAL;
399 static Z_ProximityOperator *rpn_proximity (struct yaz_pqf_parser *li, ODR o)
401 Z_ProximityOperator *p = (Z_ProximityOperator *)odr_malloc (o, sizeof(*p));
405 li->error = YAZ_PQF_ERROR_MISSING;
408 if (*li->lex_buf == '1')
409 p->exclusion = odr_intdup (o, 1);
410 else if (*li->lex_buf == '0')
411 p->exclusion = odr_intdup (o, 0);
412 else if (*li->lex_buf == 'v' || *li->lex_buf == 'n')
416 li->error = YAZ_PQF_ERROR_PROXIMITY;
422 li->error = YAZ_PQF_ERROR_MISSING;
425 if (*li->lex_buf >= '0' && *li->lex_buf <= '9')
426 p->distance = odr_intdup (o, atoi (li->lex_buf));
429 li->error = YAZ_PQF_ERROR_BAD_INTEGER;
435 li->error = YAZ_PQF_ERROR_MISSING;
438 if (*li->lex_buf == '1')
439 p->ordered = odr_intdup (o, 1);
440 else if (*li->lex_buf == '0')
441 p->ordered = odr_intdup (o, 0);
444 li->error = YAZ_PQF_ERROR_PROXIMITY;
450 li->error = YAZ_PQF_ERROR_MISSING;
453 if (*li->lex_buf >= '0' && *li->lex_buf <= '9')
454 p->relationType = odr_intdup (o, atoi (li->lex_buf));
457 li->error = YAZ_PQF_ERROR_BAD_INTEGER;
463 li->error = YAZ_PQF_ERROR_MISSING;
466 if (*li->lex_buf == 'k')
467 p->which = Z_ProximityOperator_known;
468 else if (*li->lex_buf == 'p')
469 p->which = Z_ProximityOperator_private;
471 p->which = atoi (li->lex_buf);
473 if (p->which != Z_ProximityOperator_known
474 && p->which != Z_ProximityOperator_private)
476 li->error = YAZ_PQF_ERROR_PROXIMITY;
482 li->error = YAZ_PQF_ERROR_MISSING;
485 if (*li->lex_buf >= '0' && *li->lex_buf <= '9')
486 p->u.known = odr_intdup (o, atoi(li->lex_buf));
489 li->error = YAZ_PQF_ERROR_BAD_INTEGER;
495 static Z_Complex *rpn_complex (struct yaz_pqf_parser *li, ODR o, oid_proto proto,
496 int num_attr, int max_attr,
497 int *attr_list, char **attr_clist,
503 zc = (Z_Complex *)odr_malloc (o, sizeof(*zc));
504 zo = (Z_Operator *)odr_malloc (o, sizeof(*zo));
506 switch (li->query_look)
509 zo->which = Z_Operator_and;
510 zo->u.op_and = odr_nullval();
513 zo->which = Z_Operator_or;
514 zo->u.op_or = odr_nullval();
517 zo->which = Z_Operator_and_not;
518 zo->u.and_not = odr_nullval();
521 zo->which = Z_Operator_prox;
522 zo->u.prox = rpn_proximity (li, o);
527 /* we're only called if one of the above types are seens so
528 this shouldn't happen */
529 li->error = YAZ_PQF_ERROR_INTERNAL;
534 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
535 attr_clist, attr_set)))
538 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
539 attr_clist, attr_set)))
544 static void rpn_term_type (struct yaz_pqf_parser *li, ODR o)
548 if (compare_term (li, "general", 0))
549 li->term_type = Z_Term_general;
550 else if (compare_term (li, "numeric", 0))
551 li->term_type = Z_Term_numeric;
552 else if (compare_term (li, "string", 0))
553 li->term_type = Z_Term_characterString;
554 else if (compare_term (li, "oid", 0))
555 li->term_type = Z_Term_oid;
556 else if (compare_term (li, "datetime", 0))
557 li->term_type = Z_Term_dateTime;
558 else if (compare_term (li, "null", 0))
559 li->term_type = Z_Term_null;
560 else if (compare_term(li, "range", 0))
562 /* prepare for external: range search .. */
563 li->term_type = Z_Term_external;
564 li->external_type = VAL_MULTISRCH2;
569 static Z_RPNStructure *rpn_structure (struct yaz_pqf_parser *li, ODR o,
571 int num_attr, int max_attr,
578 sz = (Z_RPNStructure *)odr_malloc (o, sizeof(*sz));
579 switch (li->query_look)
585 sz->which = Z_RPNStructure_complex;
586 if (!(sz->u.complex =
587 rpn_complex (li, o, proto, num_attr, max_attr, attr_list,
588 attr_clist, attr_set)))
593 sz->which = Z_RPNStructure_simple;
595 rpn_simple (li, o, proto, num_attr, attr_list,
596 attr_clist, attr_set)))
603 li->error = YAZ_PQF_ERROR_MISSING;
606 if (num_attr >= max_attr)
608 li->error = YAZ_PQF_ERROR_TOOMANY;
611 if (!p_query_parse_attr(li, o, num_attr, attr_list,
612 attr_clist, attr_set))
617 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
618 attr_clist, attr_set);
621 rpn_term_type (li, o);
623 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
624 attr_clist, attr_set);
625 case 0: /* operator/operand expected! */
626 li->error = YAZ_PQF_ERROR_MISSING;
632 Z_RPNQuery *p_query_rpn_mk (ODR o, struct yaz_pqf_parser *li, oid_proto proto,
636 int attr_array[1024];
637 char *attr_clist[512];
638 oid_value attr_set[512];
639 oid_value topSet = VAL_NONE;
641 zq = (Z_RPNQuery *)odr_malloc (o, sizeof(*zq));
643 if (li->query_look == 'r')
646 topSet = query_oid_getvalbyname (li);
647 if (topSet == VAL_NONE)
649 li->error = YAZ_PQF_ERROR_ATTSET;
655 if (topSet == VAL_NONE)
656 topSet = p_query_dfset;
657 if (topSet == VAL_NONE)
660 zq->attributeSetId = yaz_oidval_to_z3950oid(o, CLASS_ATTSET, topSet);
662 if (!zq->attributeSetId)
664 li->error = YAZ_PQF_ERROR_ATTSET;
668 if (!(zq->RPNStructure = rpn_structure (li, o, proto, 0, 512,
669 attr_array, attr_clist, attr_set)))
673 li->error = YAZ_PQF_ERROR_EXTRA;
679 Z_RPNQuery *p_query_rpn (ODR o, oid_proto proto, const char *qbuf)
681 struct yaz_pqf_parser li;
685 li.right_sep = "}\"";
686 li.escape_char = '@';
687 li.term_type = Z_Term_general;
688 li.query_buf = li.query_ptr = qbuf;
690 return p_query_rpn_mk (o, &li, proto, qbuf);
694 Z_AttributesPlusTerm *p_query_scan_mk (struct yaz_pqf_parser *li,
695 ODR o, oid_proto proto,
696 Odr_oid **attributeSetP,
700 char *attr_clist[512];
701 oid_value attr_set[512];
704 oid_value topSet = VAL_NONE;
705 Z_AttributesPlusTerm *apt;
708 if (li->query_look == 'r')
711 topSet = query_oid_getvalbyname (li);
715 if (topSet == VAL_NONE)
716 topSet = p_query_dfset;
717 if (topSet == VAL_NONE)
720 *attributeSetP = yaz_oidval_to_z3950oid (o, CLASS_ATTSET, topSet);
724 if (li->query_look == 'l')
729 li->error = YAZ_PQF_ERROR_MISSING;
732 if (num_attr >= max_attr)
734 li->error = YAZ_PQF_ERROR_TOOMANY;
737 if (!p_query_parse_attr(li, o, num_attr, attr_list,
738 attr_clist, attr_set))
743 else if (li->query_look == 'y')
746 rpn_term_type (li, o);
753 li->error = YAZ_PQF_ERROR_MISSING;
756 apt = rpn_term (li, o, proto, num_attr, attr_list, attr_clist, attr_set);
760 if (li->query_look != 0)
762 li->error = YAZ_PQF_ERROR_EXTRA;
768 Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,
769 Odr_oid **attributeSetP,
772 struct yaz_pqf_parser li;
776 li.right_sep = "}\"";
777 li.escape_char = '@';
778 li.term_type = Z_Term_general;
779 li.query_buf = li.query_ptr = qbuf;
782 return p_query_scan_mk (&li, o, proto, attributeSetP, qbuf);
785 int p_query_attset (const char *arg)
787 p_query_dfset = oid_getvalbyname (arg);
788 return (p_query_dfset == VAL_NONE) ? -1 : 0;
791 YAZ_PQF_Parser yaz_pqf_create (void)
793 YAZ_PQF_Parser p = (YAZ_PQF_Parser) xmalloc (sizeof(*p));
797 p->right_sep = "}\"";
798 p->escape_char = '@';
799 p->term_type = Z_Term_general;
804 void yaz_pqf_destroy (YAZ_PQF_Parser p)
809 Z_RPNQuery *yaz_pqf_parse (YAZ_PQF_Parser p, ODR o, const char *qbuf)
813 p->query_buf = p->query_ptr = qbuf;
815 return p_query_rpn_mk (o, p, PROTO_Z3950, qbuf);
818 Z_AttributesPlusTerm *yaz_pqf_scan (YAZ_PQF_Parser p, ODR o,
819 Odr_oid **attributeSetP,
824 p->query_buf = p->query_ptr = qbuf;
826 return p_query_scan_mk (p, o, PROTO_Z3950, attributeSetP, qbuf);
829 int yaz_pqf_error (YAZ_PQF_Parser p, const char **msg, size_t *off)
833 case YAZ_PQF_ERROR_NONE:
834 *msg = "no error"; break;
835 case YAZ_PQF_ERROR_EXTRA:
836 *msg = "extra token"; break;
837 case YAZ_PQF_ERROR_MISSING:
838 *msg = "missing token"; break;
839 case YAZ_PQF_ERROR_ATTSET:
840 *msg = "unknown attribute set"; break;
841 case YAZ_PQF_ERROR_TOOMANY:
842 *msg = "too many attributes"; break;
843 case YAZ_PQF_ERROR_BADATTR:
844 *msg = "bad attribute specification"; break;
845 case YAZ_PQF_ERROR_INTERNAL:
846 *msg = "internal error"; break;
847 case YAZ_PQF_ERROR_PROXIMITY:
848 *msg = "proximity error"; break;
849 case YAZ_PQF_ERROR_BAD_INTEGER:
850 *msg = "bad integer"; break;
852 *msg = "unknown error"; break;
854 *off = p->query_ptr - p->query_buf;
860 * indent-tabs-mode: nil
862 * vim: shiftwidth=4 tabstop=8 expandtab