2 * Copyright (c) 1995-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: pquery.c,v 1.2 2004-10-15 00:19:00 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 ((*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')
404 p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion));
407 else if (*li->lex_buf == '0')
409 p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion));
417 li->error = YAZ_PQF_ERROR_MISSING;
420 p->distance = (int *)odr_malloc (o, sizeof(*p->distance));
421 *p->distance = atoi (li->lex_buf);
425 li->error = YAZ_PQF_ERROR_MISSING;
428 p->ordered = (int *)odr_malloc (o, sizeof(*p->ordered));
429 *p->ordered = atoi (li->lex_buf);
433 li->error = YAZ_PQF_ERROR_MISSING;
436 p->relationType = (int *)odr_malloc (o, sizeof(*p->relationType));
437 *p->relationType = atoi (li->lex_buf);
441 li->error = YAZ_PQF_ERROR_MISSING;
444 if (*li->lex_buf == 'k')
446 else if (*li->lex_buf == 'p')
449 p->which = atoi (li->lex_buf);
453 li->error = YAZ_PQF_ERROR_MISSING;
456 p->which = Z_ProximityOperator_known;
457 p->u.known = (int *)odr_malloc (o, sizeof(*p->u.known));
458 *p->u.known = atoi (li->lex_buf);
462 static Z_Complex *rpn_complex (struct yaz_pqf_parser *li, ODR o, oid_proto proto,
463 int num_attr, int max_attr,
464 int *attr_list, char **attr_clist,
470 zc = (Z_Complex *)odr_malloc (o, sizeof(*zc));
471 zo = (Z_Operator *)odr_malloc (o, sizeof(*zo));
473 switch (li->query_look)
476 zo->which = Z_Operator_and;
477 zo->u.and_not = odr_nullval();
480 zo->which = Z_Operator_or;
481 zo->u.and_not = odr_nullval();
484 zo->which = Z_Operator_and_not;
485 zo->u.and_not = odr_nullval();
488 zo->which = Z_Operator_prox;
489 zo->u.prox = rpn_proximity (li, o);
494 /* we're only called if one of the above types are seens so
495 this shouldn't happen */
496 li->error = YAZ_PQF_ERROR_INTERNAL;
501 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
502 attr_clist, attr_set)))
505 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
506 attr_clist, attr_set)))
511 static void rpn_term_type (struct yaz_pqf_parser *li, ODR o)
515 if (compare_term (li, "general", 0))
516 li->term_type = Z_Term_general;
517 else if (compare_term (li, "numeric", 0))
518 li->term_type = Z_Term_numeric;
519 else if (compare_term (li, "string", 0))
520 li->term_type = Z_Term_characterString;
521 else if (compare_term (li, "oid", 0))
522 li->term_type = Z_Term_oid;
523 else if (compare_term (li, "datetime", 0))
524 li->term_type = Z_Term_dateTime;
525 else if (compare_term (li, "null", 0))
526 li->term_type = Z_Term_null;
527 else if (compare_term(li, "range", 0))
529 /* prepare for external: range search .. */
530 li->term_type = Z_Term_external;
531 li->external_type = VAL_MULTISRCH2;
536 static Z_RPNStructure *rpn_structure (struct yaz_pqf_parser *li, ODR o,
538 int num_attr, int max_attr,
545 sz = (Z_RPNStructure *)odr_malloc (o, sizeof(*sz));
546 switch (li->query_look)
552 sz->which = Z_RPNStructure_complex;
553 if (!(sz->u.complex =
554 rpn_complex (li, o, proto, num_attr, max_attr, attr_list,
555 attr_clist, attr_set)))
560 sz->which = Z_RPNStructure_simple;
562 rpn_simple (li, o, proto, num_attr, attr_list,
563 attr_clist, attr_set)))
570 li->error = YAZ_PQF_ERROR_MISSING;
573 if (num_attr >= max_attr)
575 li->error = YAZ_PQF_ERROR_TOOMANY;
578 if (!p_query_parse_attr(li, o, num_attr, attr_list,
579 attr_clist, attr_set))
584 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
585 attr_clist, attr_set);
588 rpn_term_type (li, o);
590 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
591 attr_clist, attr_set);
592 case 0: /* operator/operand expected! */
593 li->error = YAZ_PQF_ERROR_MISSING;
599 Z_RPNQuery *p_query_rpn_mk (ODR o, struct yaz_pqf_parser *li, oid_proto proto,
603 int attr_array[1024];
604 char *attr_clist[512];
605 oid_value attr_set[512];
606 oid_value topSet = VAL_NONE;
608 zq = (Z_RPNQuery *)odr_malloc (o, sizeof(*zq));
610 if (li->query_look == 'r')
613 topSet = query_oid_getvalbyname (li);
614 if (topSet == VAL_NONE)
616 li->error = YAZ_PQF_ERROR_ATTSET;
622 if (topSet == VAL_NONE)
623 topSet = p_query_dfset;
624 if (topSet == VAL_NONE)
627 zq->attributeSetId = yaz_oidval_to_z3950oid(o, CLASS_ATTSET, topSet);
629 if (!zq->attributeSetId)
631 li->error = YAZ_PQF_ERROR_ATTSET;
635 if (!(zq->RPNStructure = rpn_structure (li, o, proto, 0, 512,
636 attr_array, attr_clist, attr_set)))
640 li->error = YAZ_PQF_ERROR_EXTRA;
646 Z_RPNQuery *p_query_rpn (ODR o, oid_proto proto,
649 struct yaz_pqf_parser li;
653 li.right_sep = "}\"";
654 li.escape_char = '@';
655 li.term_type = Z_Term_general;
656 li.query_buf = li.query_ptr = qbuf;
658 return p_query_rpn_mk (o, &li, proto, qbuf);
662 Z_AttributesPlusTerm *p_query_scan_mk (struct yaz_pqf_parser *li,
663 ODR o, oid_proto proto,
664 Odr_oid **attributeSetP,
668 char *attr_clist[512];
669 oid_value attr_set[512];
672 oid_value topSet = VAL_NONE;
673 Z_AttributesPlusTerm *apt;
676 if (li->query_look == 'r')
679 topSet = query_oid_getvalbyname (li);
683 if (topSet == VAL_NONE)
684 topSet = p_query_dfset;
685 if (topSet == VAL_NONE)
688 *attributeSetP = yaz_oidval_to_z3950oid (o, CLASS_ATTSET, topSet);
692 if (li->query_look == 'l')
697 li->error = YAZ_PQF_ERROR_MISSING;
700 if (num_attr >= max_attr)
702 li->error = YAZ_PQF_ERROR_TOOMANY;
705 if (!p_query_parse_attr(li, o, num_attr, attr_list,
706 attr_clist, attr_set))
711 else if (li->query_look == 'y')
714 rpn_term_type (li, o);
721 li->error = YAZ_PQF_ERROR_MISSING;
724 apt = rpn_term (li, o, proto, num_attr, attr_list, attr_clist, attr_set);
728 if (li->query_look != 0)
730 li->error = YAZ_PQF_ERROR_EXTRA;
736 Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,
737 Odr_oid **attributeSetP,
740 struct yaz_pqf_parser li;
744 li.right_sep = "}\"";
745 li.escape_char = '@';
746 li.term_type = Z_Term_general;
747 li.query_buf = li.query_ptr = qbuf;
750 return p_query_scan_mk (&li, o, proto, attributeSetP, qbuf);
753 int p_query_attset (const char *arg)
755 p_query_dfset = oid_getvalbyname (arg);
756 return (p_query_dfset == VAL_NONE) ? -1 : 0;
759 YAZ_PQF_Parser yaz_pqf_create (void)
761 YAZ_PQF_Parser p = (YAZ_PQF_Parser) xmalloc (sizeof(*p));
765 p->right_sep = "}\"";
766 p->escape_char = '@';
767 p->term_type = Z_Term_general;
772 void yaz_pqf_destroy (YAZ_PQF_Parser p)
777 Z_RPNQuery *yaz_pqf_parse (YAZ_PQF_Parser p, ODR o, const char *qbuf)
781 p->query_buf = p->query_ptr = qbuf;
783 return p_query_rpn_mk (o, p, PROTO_Z3950, qbuf);
786 Z_AttributesPlusTerm *yaz_pqf_scan (YAZ_PQF_Parser p, ODR o,
787 Odr_oid **attributeSetP,
792 p->query_buf = p->query_ptr = qbuf;
794 return p_query_scan_mk (p, o, PROTO_Z3950, attributeSetP, qbuf);
797 int yaz_pqf_error (YAZ_PQF_Parser p, const char **msg, size_t *off)
801 case YAZ_PQF_ERROR_NONE:
802 *msg = "no error"; break;
803 case YAZ_PQF_ERROR_EXTRA:
804 *msg = "extra token"; break;
805 case YAZ_PQF_ERROR_MISSING:
806 *msg = "missing token"; break;
807 case YAZ_PQF_ERROR_ATTSET:
808 *msg = "unknown attribute set"; break;
809 case YAZ_PQF_ERROR_TOOMANY:
810 *msg = "too many attributes"; break;
811 case YAZ_PQF_ERROR_BADATTR:
812 *msg = "bad attribute specification"; break;
813 case YAZ_PQF_ERROR_INTERNAL:
814 *msg = "internal error"; break;
816 *msg = "unknown error"; break;
818 *off = p->query_ptr - p->query_buf;