2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: pquery.c,v 1.10 2007-04-12 13:52:57 adam Exp $
9 * \brief Implements PQF parsing
16 #include <yaz/proto.h>
17 #include <yaz/oid_db.h>
18 #include <yaz/pquery.h>
20 struct yaz_pqf_parser {
21 const char *query_buf;
22 const char *query_ptr;
34 static Z_RPNStructure *rpn_structure(struct yaz_pqf_parser *li, ODR o,
35 int num_attr, int max_attr,
36 int *attr_list, char **attr_clist,
39 static int *query_oid_getvalbyname(struct yaz_pqf_parser *li, ODR o)
45 memcpy (buf, li->lex_buf, li->lex_len);
46 buf[li->lex_len] = '\0';
47 return yaz_string_to_oid_odr(yaz_oid_std(), CLASS_ATTSET, buf, o);
50 static int compare_term(struct yaz_pqf_parser *li, const char *src,
53 size_t len=strlen(src);
55 if (li->lex_len == len+off && !memcmp (li->lex_buf+off, src, len-off))
60 static int query_token(struct yaz_pqf_parser *li)
63 const char *sep_match;
64 const char **qptr = &li->query_ptr;
71 if ((sep_match = strchr (li->left_sep, **qptr)))
73 sep_char = li->right_sep[sep_match - li->left_sep];
78 if (**qptr == li->escape_char && isdigit (((const unsigned char *) *qptr)[1]))
84 while (**qptr && **qptr != sep_char)
96 if (sep_char == ' ' &&
97 li->lex_len >= 1 && li->lex_buf[0] == li->escape_char)
99 if (compare_term (li, "and", 1))
101 if (compare_term (li, "or", 1))
103 if (compare_term (li, "not", 1))
105 if (compare_term (li, "attr", 1))
107 if (compare_term (li, "set", 1))
109 if (compare_term (li, "attrset", 1))
111 if (compare_term (li, "prox", 1))
113 if (compare_term (li, "term", 1))
119 static int lex(struct yaz_pqf_parser *li)
121 return li->query_look = query_token(li);
124 static int escape_string(char *out_buf, const char *in, int len)
129 if (*in == '\\' && len > 0)
155 sscanf (s, "%x", &n);
172 sscanf (s, "%o", &n);
184 return out - out_buf;
187 static int p_query_parse_attr(struct yaz_pqf_parser *li, ODR o,
188 int num_attr, int *attr_list,
189 char **attr_clist, int **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, o);
197 if (attr_set[num_attr] == 0)
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] = 0;
220 if (*li->lex_buf < '0' || *li->lex_buf > '9')
222 li->error = YAZ_PQF_ERROR_BAD_INTEGER;
225 attr_list[2*num_attr] = atoi(li->lex_buf);
227 if (*cp >= '0' && *cp <= '9')
229 attr_list[2*num_attr+1] = atoi (cp);
230 attr_clist[num_attr] = 0;
234 int len = li->lex_len - (cp - li->lex_buf);
235 attr_list[2*num_attr+1] = 0;
236 attr_clist[num_attr] = (char *) odr_malloc (o, len+1);
237 len = escape_string(attr_clist[num_attr], cp, len);
238 attr_clist[num_attr][len] = '\0';
243 static Z_AttributesPlusTerm *rpn_term(struct yaz_pqf_parser *li, ODR o,
244 int num_attr, int *attr_list,
245 char **attr_clist, int **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 = attr_set[i];
283 elements[k]->which = Z_AttributeValue_complex;
284 elements[k]->value.complex = (Z_ComplexAttribute *)
285 odr_malloc (o, sizeof(Z_ComplexAttribute));
286 elements[k]->value.complex->num_list = 1;
287 elements[k]->value.complex->list =
288 (Z_StringOrNumeric **)
289 odr_malloc (o, 1 * sizeof(Z_StringOrNumeric *));
290 elements[k]->value.complex->list[0] =
291 (Z_StringOrNumeric *)
292 odr_malloc (o, sizeof(Z_StringOrNumeric));
293 elements[k]->value.complex->list[0]->which =
294 Z_StringOrNumeric_string;
295 elements[k]->value.complex->list[0]->u.string =
297 elements[k]->value.complex->semanticAction = (int **)
299 elements[k]->value.complex->num_semanticAction = 0;
303 elements[k]->which = Z_AttributeValue_numeric;
304 elements[k]->value.numeric = &attr_tmp[2*i+1];
310 zapt->attributes = (Z_AttributeList *)
311 odr_malloc (o, sizeof(*zapt->attributes));
312 zapt->attributes->num_attributes = num_attr;
313 zapt->attributes->attributes = elements;
317 term_octet->buf = (unsigned char *)odr_malloc (o, 1 + li->lex_len);
318 term_octet->size = term_octet->len =
319 escape_string ((char *) (term_octet->buf), li->lex_buf, li->lex_len);
320 term_octet->buf[term_octet->size] = 0; /* null terminate */
322 switch (li->term_type)
325 term->which = Z_Term_general;
326 term->u.general = term_octet;
328 case Z_Term_characterString:
329 term->which = Z_Term_characterString;
330 term->u.characterString = (char*) term_octet->buf;
331 /* null terminated above */
334 term->which = Z_Term_numeric;
335 term->u.numeric = odr_intdup (o, atoi((char*) (term_octet->buf)));
338 term->which = Z_Term_null;
339 term->u.null = odr_nullval();
341 case Z_Term_external:
342 term->which = Z_Term_external;
343 term->u.external = 0;
346 term->which = Z_Term_null;
347 term->u.null = odr_nullval();
353 static Z_Operand *rpn_simple(struct yaz_pqf_parser *li, ODR o,
354 int num_attr, int *attr_list, char **attr_clist,
359 zo = (Z_Operand *)odr_malloc (o, sizeof(*zo));
360 switch (li->query_look)
363 zo->which = Z_Operand_APT;
364 if (!(zo->u.attributesPlusTerm =
365 rpn_term (li, o, num_attr, attr_list, attr_clist, attr_set)))
373 li->error = YAZ_PQF_ERROR_MISSING;
376 zo->which = Z_Operand_resultSetId;
377 zo->u.resultSetId = (char *)odr_malloc (o, li->lex_len+1);
378 memcpy (zo->u.resultSetId, li->lex_buf, li->lex_len);
379 zo->u.resultSetId[li->lex_len] = '\0';
383 /* we're only called if one of the above types are seens so
384 this shouldn't happen */
385 li->error = YAZ_PQF_ERROR_INTERNAL;
391 static Z_ProximityOperator *rpn_proximity (struct yaz_pqf_parser *li, ODR o)
393 Z_ProximityOperator *p = (Z_ProximityOperator *)odr_malloc (o, sizeof(*p));
397 li->error = YAZ_PQF_ERROR_MISSING;
400 if (*li->lex_buf == '1')
401 p->exclusion = odr_intdup (o, 1);
402 else if (*li->lex_buf == '0')
403 p->exclusion = odr_intdup (o, 0);
404 else if (*li->lex_buf == 'v' || *li->lex_buf == 'n')
408 li->error = YAZ_PQF_ERROR_PROXIMITY;
414 li->error = YAZ_PQF_ERROR_MISSING;
417 if (*li->lex_buf >= '0' && *li->lex_buf <= '9')
418 p->distance = odr_intdup (o, atoi (li->lex_buf));
421 li->error = YAZ_PQF_ERROR_BAD_INTEGER;
427 li->error = YAZ_PQF_ERROR_MISSING;
430 if (*li->lex_buf == '1')
431 p->ordered = odr_intdup (o, 1);
432 else if (*li->lex_buf == '0')
433 p->ordered = odr_intdup (o, 0);
436 li->error = YAZ_PQF_ERROR_PROXIMITY;
442 li->error = YAZ_PQF_ERROR_MISSING;
445 if (*li->lex_buf >= '0' && *li->lex_buf <= '9')
446 p->relationType = odr_intdup (o, atoi (li->lex_buf));
449 li->error = YAZ_PQF_ERROR_BAD_INTEGER;
455 li->error = YAZ_PQF_ERROR_MISSING;
458 if (*li->lex_buf == 'k')
459 p->which = Z_ProximityOperator_known;
460 else if (*li->lex_buf == 'p')
461 p->which = Z_ProximityOperator_private;
463 p->which = atoi (li->lex_buf);
465 if (p->which != Z_ProximityOperator_known
466 && p->which != Z_ProximityOperator_private)
468 li->error = YAZ_PQF_ERROR_PROXIMITY;
474 li->error = YAZ_PQF_ERROR_MISSING;
477 if (*li->lex_buf >= '0' && *li->lex_buf <= '9')
478 p->u.known = odr_intdup (o, atoi(li->lex_buf));
481 li->error = YAZ_PQF_ERROR_BAD_INTEGER;
487 static Z_Complex *rpn_complex(struct yaz_pqf_parser *li, ODR o,
488 int num_attr, int max_attr,
489 int *attr_list, char **attr_clist,
495 zc = (Z_Complex *)odr_malloc (o, sizeof(*zc));
496 zo = (Z_Operator *)odr_malloc (o, sizeof(*zo));
498 switch (li->query_look)
501 zo->which = Z_Operator_and;
502 zo->u.op_and = odr_nullval();
505 zo->which = Z_Operator_or;
506 zo->u.op_or = odr_nullval();
509 zo->which = Z_Operator_and_not;
510 zo->u.and_not = odr_nullval();
513 zo->which = Z_Operator_prox;
514 zo->u.prox = rpn_proximity (li, o);
519 /* we're only called if one of the above types are seens so
520 this shouldn't happen */
521 li->error = YAZ_PQF_ERROR_INTERNAL;
526 rpn_structure(li, o, num_attr, max_attr, attr_list,
527 attr_clist, attr_set)))
530 rpn_structure(li, o, num_attr, max_attr, attr_list,
531 attr_clist, attr_set)))
536 static void rpn_term_type(struct yaz_pqf_parser *li, ODR o)
540 if (compare_term (li, "general", 0))
541 li->term_type = Z_Term_general;
542 else if (compare_term (li, "numeric", 0))
543 li->term_type = Z_Term_numeric;
544 else if (compare_term (li, "string", 0))
545 li->term_type = Z_Term_characterString;
546 else if (compare_term (li, "oid", 0))
547 li->term_type = Z_Term_oid;
548 else if (compare_term (li, "datetime", 0))
549 li->term_type = Z_Term_dateTime;
550 else if (compare_term (li, "null", 0))
551 li->term_type = Z_Term_null;
553 else if (compare_term(li, "range", 0))
555 /* prepare for external: range search .. */
556 li->term_type = Z_Term_external;
557 li->external_type = VAL_MULTISRCH2;
563 static Z_RPNStructure *rpn_structure(struct yaz_pqf_parser *li, ODR o,
564 int num_attr, int max_attr,
571 sz = (Z_RPNStructure *)odr_malloc (o, sizeof(*sz));
572 switch (li->query_look)
578 sz->which = Z_RPNStructure_complex;
579 if (!(sz->u.complex =
580 rpn_complex (li, o, num_attr, max_attr, attr_list,
581 attr_clist, attr_set)))
586 sz->which = Z_RPNStructure_simple;
588 rpn_simple (li, o, num_attr, attr_list,
589 attr_clist, attr_set)))
596 li->error = YAZ_PQF_ERROR_MISSING;
599 if (num_attr >= max_attr)
601 li->error = YAZ_PQF_ERROR_TOOMANY;
604 if (!p_query_parse_attr(li, o, num_attr, attr_list,
605 attr_clist, attr_set))
610 rpn_structure (li, o, num_attr, max_attr, attr_list,
611 attr_clist, attr_set);
614 rpn_term_type (li, o);
616 rpn_structure (li, o, num_attr, max_attr, attr_list,
617 attr_clist, attr_set);
618 case 0: /* operator/operand expected! */
619 li->error = YAZ_PQF_ERROR_MISSING;
625 Z_RPNQuery *p_query_rpn_mk(ODR o, struct yaz_pqf_parser *li, const char *qbuf)
628 int attr_array[1024];
629 char *attr_clist[512];
633 zq = (Z_RPNQuery *)odr_malloc (o, sizeof(*zq));
635 if (li->query_look == 'r')
638 top_set = query_oid_getvalbyname(li, o);
641 li->error = YAZ_PQF_ERROR_ATTSET;
648 top_set = yaz_string_to_oid_odr(yaz_oid_std(),
649 CLASS_ATTSET, OID_STR_BIB1, o);
652 zq->attributeSetId = top_set;
654 if (!zq->attributeSetId)
656 li->error = YAZ_PQF_ERROR_ATTSET;
660 if (!(zq->RPNStructure = rpn_structure(li, o, 0, 512,
661 attr_array, attr_clist, attr_set)))
665 li->error = YAZ_PQF_ERROR_EXTRA;
671 Z_RPNQuery *p_query_rpn(ODR o, const char *qbuf)
673 struct yaz_pqf_parser li;
677 li.right_sep = "}\"";
678 li.escape_char = '@';
679 li.term_type = Z_Term_general;
680 li.query_buf = li.query_ptr = qbuf;
682 return p_query_rpn_mk(o, &li, qbuf);
686 Z_AttributesPlusTerm *p_query_scan_mk(struct yaz_pqf_parser *li,
687 ODR o, oid_proto proto,
688 Odr_oid **attributeSetP,
692 char *attr_clist[512];
697 Z_AttributesPlusTerm *apt;
700 if (li->query_look == 'r')
703 top_set = query_oid_getvalbyname(li, o);
706 li->error = YAZ_PQF_ERROR_ATTSET;
713 top_set = yaz_string_to_oid_odr(yaz_oid_std(),
714 CLASS_ATTSET, OID_STR_BIB1, o);
716 *attributeSetP = top_set;
720 if (li->query_look == 'l')
725 li->error = YAZ_PQF_ERROR_MISSING;
728 if (num_attr >= max_attr)
730 li->error = YAZ_PQF_ERROR_TOOMANY;
733 if (!p_query_parse_attr(li, o, num_attr, attr_list,
734 attr_clist, attr_set))
739 else if (li->query_look == 'y')
742 rpn_term_type (li, o);
749 li->error = YAZ_PQF_ERROR_MISSING;
752 apt = rpn_term(li, o, num_attr, attr_list, attr_clist, attr_set);
756 if (li->query_look != 0)
758 li->error = YAZ_PQF_ERROR_EXTRA;
764 Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,
765 Odr_oid **attributeSetP,
768 struct yaz_pqf_parser li;
772 li.right_sep = "}\"";
773 li.escape_char = '@';
774 li.term_type = Z_Term_general;
775 li.query_buf = li.query_ptr = qbuf;
778 return p_query_scan_mk (&li, o, proto, attributeSetP, qbuf);
781 YAZ_PQF_Parser yaz_pqf_create (void)
783 YAZ_PQF_Parser p = (YAZ_PQF_Parser) xmalloc (sizeof(*p));
787 p->right_sep = "}\"";
788 p->escape_char = '@';
789 p->term_type = Z_Term_general;
794 void yaz_pqf_destroy(YAZ_PQF_Parser p)
799 Z_RPNQuery *yaz_pqf_parse(YAZ_PQF_Parser p, ODR o, const char *qbuf)
803 p->query_buf = p->query_ptr = qbuf;
805 return p_query_rpn_mk (o, p, qbuf);
808 Z_AttributesPlusTerm *yaz_pqf_scan(YAZ_PQF_Parser p, ODR o,
809 Odr_oid **attributeSetP,
814 p->query_buf = p->query_ptr = qbuf;
816 return p_query_scan_mk (p, o, PROTO_Z3950, attributeSetP, qbuf);
819 int yaz_pqf_error (YAZ_PQF_Parser p, const char **msg, size_t *off)
823 case YAZ_PQF_ERROR_NONE:
824 *msg = "no error"; break;
825 case YAZ_PQF_ERROR_EXTRA:
826 *msg = "extra token"; break;
827 case YAZ_PQF_ERROR_MISSING:
828 *msg = "missing token"; break;
829 case YAZ_PQF_ERROR_ATTSET:
830 *msg = "unknown attribute set"; break;
831 case YAZ_PQF_ERROR_TOOMANY:
832 *msg = "too many attributes"; break;
833 case YAZ_PQF_ERROR_BADATTR:
834 *msg = "bad attribute specification"; break;
835 case YAZ_PQF_ERROR_INTERNAL:
836 *msg = "internal error"; break;
837 case YAZ_PQF_ERROR_PROXIMITY:
838 *msg = "proximity error"; break;
839 case YAZ_PQF_ERROR_BAD_INTEGER:
840 *msg = "bad integer"; break;
842 *msg = "unknown error"; break;
844 *off = p->query_ptr - p->query_buf;
850 * indent-tabs-mode: nil
852 * vim: shiftwidth=4 tabstop=8 expandtab