2 * Copyright (c) 1995-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: pquery.c,v 1.20 2003-01-06 08:20:29 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;
33 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 enum oid_value query_oid_getvalbyname (struct yaz_pqf_parser *li)
46 memcpy (buf, li->lex_buf, li->lex_len);
47 buf[li->lex_len] = '\0';
48 value = oid_getvalbyname (buf);
52 static int compare_term (struct yaz_pqf_parser *li, const char *src,
55 size_t len=strlen(src);
57 if (li->lex_len == len+off && !memcmp (li->lex_buf+off, src, len-off))
62 static int query_token (struct yaz_pqf_parser *li)
65 const char *sep_match;
66 const char **qptr = &li->query_ptr;
73 if ((sep_match = strchr (li->left_sep, **qptr)))
75 sep_char = li->right_sep[sep_match - li->left_sep];
80 if (**qptr == li->escape_char && isdigit ((*qptr)[1]))
86 while (**qptr && **qptr != sep_char)
98 if (sep_char == ' ' &&
99 li->lex_len >= 1 && li->lex_buf[0] == li->escape_char)
101 if (compare_term (li, "and", 1))
103 if (compare_term (li, "or", 1))
105 if (compare_term (li, "not", 1))
107 if (compare_term (li, "attr", 1))
109 if (compare_term (li, "set", 1))
111 if (compare_term (li, "attrset", 1))
113 if (compare_term (li, "prox", 1))
115 if (compare_term (li, "term", 1))
121 static int lex (struct yaz_pqf_parser *li)
123 return li->query_look = query_token (li);
126 static int escape_string(char *out_buf, const char *in, int len)
131 if (*in == '\\' && len > 0)
157 sscanf (s, "%x", &n);
174 sscanf (s, "%o", &n);
186 return out - out_buf;
189 static int p_query_parse_attr(struct yaz_pqf_parser *li, ODR o,
190 int num_attr, int *attr_list,
191 char **attr_clist, oid_value *attr_set)
194 if (!(cp = strchr (li->lex_buf, '=')) ||
195 (size_t) (cp-li->lex_buf) > li->lex_len)
197 attr_set[num_attr] = query_oid_getvalbyname (li);
198 if (attr_set[num_attr] == VAL_NONE)
200 li->error = YAZ_PQF_ERROR_ATTSET;
205 li->error = YAZ_PQF_ERROR_MISSING;
208 if (!(cp = strchr (li->lex_buf, '=')))
210 li->error = YAZ_PQF_ERROR_BADATTR;
217 attr_set[num_attr] = attr_set[num_attr-1];
219 attr_set[num_attr] = VAL_NONE;
221 attr_list[2*num_attr] = atoi(li->lex_buf);
223 if (*cp >= '0' && *cp <= '9')
225 attr_list[2*num_attr+1] = atoi (cp);
226 attr_clist[num_attr] = 0;
230 int len = li->lex_len - (cp - li->lex_buf);
231 attr_list[2*num_attr+1] = 0;
232 attr_clist[num_attr] = (char *) odr_malloc (o, len+1);
233 len = escape_string(attr_clist[num_attr], cp, len);
234 attr_clist[num_attr][len] = '\0';
239 static Z_AttributesPlusTerm *rpn_term (struct yaz_pqf_parser *li, ODR o,
241 int num_attr, int *attr_list,
242 char **attr_clist, oid_value *attr_set)
244 Z_AttributesPlusTerm *zapt;
247 Z_AttributeElement **elements;
249 zapt = (Z_AttributesPlusTerm *)odr_malloc (o, sizeof(*zapt));
250 term_octet = (Odr_oct *)odr_malloc (o, sizeof(*term_octet));
251 term = (Z_Term *)odr_malloc (o, sizeof(*term));
254 elements = (Z_AttributeElement**)odr_nullval();
260 elements = (Z_AttributeElement**)
261 odr_malloc (o, num_attr * sizeof(*elements));
263 attr_tmp = (int *)odr_malloc (o, num_attr * 2 * sizeof(int));
264 memcpy (attr_tmp, attr_list, num_attr * 2 * sizeof(int));
265 for (i = num_attr; --i >= 0; )
268 for (j = i+1; j<num_attr; j++)
269 if (attr_tmp[2*j] == attr_tmp[2*i])
274 (Z_AttributeElement*)odr_malloc (o,sizeof(**elements));
275 elements[k]->attributeType = &attr_tmp[2*i];
276 elements[k]->attributeSet =
277 yaz_oidval_to_z3950oid(o, CLASS_ATTSET, attr_set[i]);
281 elements[k]->which = Z_AttributeValue_complex;
282 elements[k]->value.complex = (Z_ComplexAttribute *)
283 odr_malloc (o, sizeof(Z_ComplexAttribute));
284 elements[k]->value.complex->num_list = 1;
285 elements[k]->value.complex->list =
286 (Z_StringOrNumeric **)
287 odr_malloc (o, 1 * sizeof(Z_StringOrNumeric *));
288 elements[k]->value.complex->list[0] =
289 (Z_StringOrNumeric *)
290 odr_malloc (o, sizeof(Z_StringOrNumeric));
291 elements[k]->value.complex->list[0]->which =
292 Z_StringOrNumeric_string;
293 elements[k]->value.complex->list[0]->u.string =
295 elements[k]->value.complex->semanticAction = (int **)
297 elements[k]->value.complex->num_semanticAction = 0;
301 elements[k]->which = Z_AttributeValue_numeric;
302 elements[k]->value.numeric = &attr_tmp[2*i+1];
308 zapt->attributes = (Z_AttributeList *)
309 odr_malloc (o, sizeof(*zapt->attributes));
310 zapt->attributes->num_attributes = num_attr;
311 zapt->attributes->attributes = elements;
315 term_octet->buf = (unsigned char *)odr_malloc (o, 1 + li->lex_len);
316 term_octet->size = term_octet->len =
317 escape_string ((char *) (term_octet->buf), li->lex_buf, li->lex_len);
318 term_octet->buf[term_octet->size] = 0; /* null terminate */
320 switch (li->term_type)
323 term->which = Z_Term_general;
324 term->u.general = term_octet;
326 case Z_Term_characterString:
327 term->which = Z_Term_characterString;
328 term->u.characterString = (char*) term_octet->buf;
329 /* null terminated above */
332 term->which = Z_Term_numeric;
333 term->u.numeric = odr_intdup (o, atoi((char*) (term_octet->buf)));
336 term->which = Z_Term_null;
337 term->u.null = odr_nullval();
339 case Z_Term_external:
340 term->which = Z_Term_external;
341 term->u.external = 0;
344 term->which = Z_Term_null;
345 term->u.null = odr_nullval();
351 static Z_Operand *rpn_simple (struct yaz_pqf_parser *li, ODR o, oid_proto proto,
352 int num_attr, int *attr_list, char **attr_clist,
357 zo = (Z_Operand *)odr_malloc (o, sizeof(*zo));
358 switch (li->query_look)
361 zo->which = Z_Operand_APT;
362 if (!(zo->u.attributesPlusTerm =
363 rpn_term (li, o, proto, num_attr, attr_list, attr_clist,
372 li->error = YAZ_PQF_ERROR_MISSING;
375 zo->which = Z_Operand_resultSetId;
376 zo->u.resultSetId = (char *)odr_malloc (o, li->lex_len+1);
377 memcpy (zo->u.resultSetId, li->lex_buf, li->lex_len);
378 zo->u.resultSetId[li->lex_len] = '\0';
382 /* we're only called if one of the above types are seens so
383 this shouldn't happen */
384 li->error = YAZ_PQF_ERROR_INTERNAL;
390 static Z_ProximityOperator *rpn_proximity (struct yaz_pqf_parser *li, ODR o)
392 Z_ProximityOperator *p = (Z_ProximityOperator *)odr_malloc (o, sizeof(*p));
396 li->error = YAZ_PQF_ERROR_MISSING;
399 if (*li->lex_buf == '1')
401 p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion));
404 else if (*li->lex_buf == '0')
406 p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion));
414 li->error = YAZ_PQF_ERROR_MISSING;
417 p->distance = (int *)odr_malloc (o, sizeof(*p->distance));
418 *p->distance = atoi (li->lex_buf);
422 li->error = YAZ_PQF_ERROR_MISSING;
425 p->ordered = (int *)odr_malloc (o, sizeof(*p->ordered));
426 *p->ordered = atoi (li->lex_buf);
430 li->error = YAZ_PQF_ERROR_MISSING;
433 p->relationType = (int *)odr_malloc (o, sizeof(*p->relationType));
434 *p->relationType = atoi (li->lex_buf);
438 li->error = YAZ_PQF_ERROR_MISSING;
441 if (*li->lex_buf == 'k')
443 else if (*li->lex_buf == 'p')
446 p->which = atoi (li->lex_buf);
450 li->error = YAZ_PQF_ERROR_MISSING;
453 p->which = Z_ProximityOperator_known;
454 p->u.known = (int *)odr_malloc (o, sizeof(*p->u.known));
455 *p->u.known = atoi (li->lex_buf);
459 static Z_Complex *rpn_complex (struct yaz_pqf_parser *li, ODR o, oid_proto proto,
460 int num_attr, int max_attr,
461 int *attr_list, char **attr_clist,
467 zc = (Z_Complex *)odr_malloc (o, sizeof(*zc));
468 zo = (Z_Operator *)odr_malloc (o, sizeof(*zo));
470 switch (li->query_look)
473 zo->which = Z_Operator_and;
474 zo->u.and_not = odr_nullval();
477 zo->which = Z_Operator_or;
478 zo->u.and_not = odr_nullval();
481 zo->which = Z_Operator_and_not;
482 zo->u.and_not = odr_nullval();
485 zo->which = Z_Operator_prox;
486 zo->u.prox = rpn_proximity (li, o);
491 /* we're only called if one of the above types are seens so
492 this shouldn't happen */
493 li->error = YAZ_PQF_ERROR_INTERNAL;
498 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
499 attr_clist, attr_set)))
502 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
503 attr_clist, attr_set)))
508 static void rpn_term_type (struct yaz_pqf_parser *li, ODR o)
512 if (compare_term (li, "general", 0))
513 li->term_type = Z_Term_general;
514 else if (compare_term (li, "numeric", 0))
515 li->term_type = Z_Term_numeric;
516 else if (compare_term (li, "string", 0))
517 li->term_type = Z_Term_characterString;
518 else if (compare_term (li, "oid", 0))
519 li->term_type = Z_Term_oid;
520 else if (compare_term (li, "datetime", 0))
521 li->term_type = Z_Term_dateTime;
522 else if (compare_term (li, "null", 0))
523 li->term_type = Z_Term_null;
524 else if (compare_term(li, "range", 0))
526 /* prepare for external: range search .. */
527 li->term_type = Z_Term_external;
528 li->external_type = VAL_MULTISRCH2;
533 static Z_RPNStructure *rpn_structure (struct yaz_pqf_parser *li, ODR o,
535 int num_attr, int max_attr,
542 sz = (Z_RPNStructure *)odr_malloc (o, sizeof(*sz));
543 switch (li->query_look)
549 sz->which = Z_RPNStructure_complex;
550 if (!(sz->u.complex =
551 rpn_complex (li, o, proto, num_attr, max_attr, attr_list,
552 attr_clist, attr_set)))
557 sz->which = Z_RPNStructure_simple;
559 rpn_simple (li, o, proto, num_attr, attr_list,
560 attr_clist, attr_set)))
567 li->error = YAZ_PQF_ERROR_MISSING;
570 if (num_attr >= max_attr)
572 li->error = YAZ_PQF_ERROR_TOOMANY;
575 if (!p_query_parse_attr(li, o, num_attr, attr_list,
576 attr_clist, attr_set))
581 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
582 attr_clist, attr_set);
585 rpn_term_type (li, o);
587 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
588 attr_clist, attr_set);
589 case 0: /* operator/operand expected! */
590 li->error = YAZ_PQF_ERROR_MISSING;
596 Z_RPNQuery *p_query_rpn_mk (ODR o, struct yaz_pqf_parser *li, oid_proto proto,
600 int attr_array[1024];
601 char *attr_clist[512];
602 oid_value attr_set[512];
603 oid_value topSet = VAL_NONE;
605 zq = (Z_RPNQuery *)odr_malloc (o, sizeof(*zq));
607 if (li->query_look == 'r')
610 topSet = query_oid_getvalbyname (li);
611 if (topSet == VAL_NONE)
613 li->error = YAZ_PQF_ERROR_ATTSET;
619 if (topSet == VAL_NONE)
620 topSet = p_query_dfset;
621 if (topSet == VAL_NONE)
624 zq->attributeSetId = yaz_oidval_to_z3950oid(o, CLASS_ATTSET, topSet);
626 if (!zq->attributeSetId)
628 li->error = YAZ_PQF_ERROR_ATTSET;
632 if (!(zq->RPNStructure = rpn_structure (li, o, proto, 0, 512,
633 attr_array, attr_clist, attr_set)))
637 li->error = YAZ_PQF_ERROR_EXTRA;
643 Z_RPNQuery *p_query_rpn (ODR o, oid_proto proto,
646 struct yaz_pqf_parser li;
650 li.right_sep = "}\"";
651 li.escape_char = '@';
652 li.term_type = Z_Term_general;
653 li.query_buf = li.query_ptr = qbuf;
655 return p_query_rpn_mk (o, &li, proto, qbuf);
659 Z_AttributesPlusTerm *p_query_scan_mk (struct yaz_pqf_parser *li,
660 ODR o, oid_proto proto,
661 Odr_oid **attributeSetP,
665 char *attr_clist[512];
666 oid_value attr_set[512];
669 oid_value topSet = VAL_NONE;
670 Z_AttributesPlusTerm *apt;
673 if (li->query_look == 'r')
676 topSet = query_oid_getvalbyname (li);
680 if (topSet == VAL_NONE)
681 topSet = p_query_dfset;
682 if (topSet == VAL_NONE)
685 *attributeSetP = yaz_oidval_to_z3950oid (o, CLASS_ATTSET, topSet);
689 if (li->query_look == 'l')
694 li->error = YAZ_PQF_ERROR_MISSING;
697 if (num_attr >= max_attr)
699 li->error = YAZ_PQF_ERROR_TOOMANY;
702 if (!p_query_parse_attr(li, o, num_attr, attr_list,
703 attr_clist, attr_set))
708 else if (li->query_look == 'y')
711 rpn_term_type (li, o);
718 li->error = YAZ_PQF_ERROR_MISSING;
721 apt = rpn_term (li, o, proto, num_attr, attr_list, attr_clist, attr_set);
725 if (li->query_look != 0)
727 li->error = YAZ_PQF_ERROR_EXTRA;
733 Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,
734 Odr_oid **attributeSetP,
737 struct yaz_pqf_parser li;
741 li.right_sep = "}\"";
742 li.escape_char = '@';
743 li.term_type = Z_Term_general;
744 li.query_buf = li.query_ptr = qbuf;
747 return p_query_scan_mk (&li, o, proto, attributeSetP, qbuf);
750 int p_query_attset (const char *arg)
752 p_query_dfset = oid_getvalbyname (arg);
753 return (p_query_dfset == VAL_NONE) ? -1 : 0;
756 YAZ_PQF_Parser yaz_pqf_create (void)
758 YAZ_PQF_Parser p = (YAZ_PQF_Parser) xmalloc (sizeof(*p));
762 p->right_sep = "}\"";
763 p->escape_char = '@';
764 p->term_type = Z_Term_general;
769 void yaz_pqf_destroy (YAZ_PQF_Parser p)
774 Z_RPNQuery *yaz_pqf_parse (YAZ_PQF_Parser p, ODR o, const char *qbuf)
778 p->query_buf = p->query_ptr = qbuf;
780 return p_query_rpn_mk (o, p, PROTO_Z3950, qbuf);
783 Z_AttributesPlusTerm *yaz_pqf_scan (YAZ_PQF_Parser p, ODR o,
784 Odr_oid **attributeSetP,
789 p->query_buf = p->query_ptr = qbuf;
791 return p_query_scan_mk (p, o, PROTO_Z3950, attributeSetP, qbuf);
794 int yaz_pqf_error (YAZ_PQF_Parser p, const char **msg, size_t *off)
798 case YAZ_PQF_ERROR_NONE:
799 *msg = "no error"; break;
800 case YAZ_PQF_ERROR_EXTRA:
801 *msg = "extra token"; break;
802 case YAZ_PQF_ERROR_MISSING:
803 *msg = "missing token"; break;
804 case YAZ_PQF_ERROR_ATTSET:
805 *msg = "unknown attribute set"; break;
806 case YAZ_PQF_ERROR_TOOMANY:
807 *msg = "too many attributes"; break;
808 case YAZ_PQF_ERROR_BADATTR:
809 *msg = "bad attribute specification"; break;
810 case YAZ_PQF_ERROR_INTERNAL:
811 *msg = "internal error"; break;
813 *msg = "unknown error"; break;
815 *off = p->query_ptr - p->query_buf;